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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) STMicroelectronics SA 2014
* Authors: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
*/
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <media/v4l2-event.h>
#include <media/v4l2-ioctl.h>
#include "bdisp.h"
#define BDISP_MAX_CTRL_NUM 10
#define BDISP_WORK_TIMEOUT ((100 * HZ) / 1000)
/* User configuration change */
#define BDISP_PARAMS BIT(0) /* Config updated */
#define BDISP_SRC_FMT BIT(1) /* Source set */
#define BDISP_DST_FMT BIT(2) /* Destination set */
#define BDISP_CTX_STOP_REQ BIT(3) /* Stop request */
#define BDISP_CTX_ABORT BIT(4) /* Abort while device run */
#define BDISP_MIN_W 1
#define BDISP_MAX_W 8191
#define BDISP_MIN_H 1
#define BDISP_MAX_H 8191
#define fh_to_ctx(__fh) container_of(__fh, struct bdisp_ctx, fh)
enum bdisp_dev_flags {
ST_M2M_OPEN, /* Driver opened */
ST_M2M_RUNNING, /* HW device running */
ST_M2M_SUSPENDED, /* Driver suspended */
ST_M2M_SUSPENDING, /* Driver being suspended */
};
static const struct bdisp_fmt bdisp_formats[] = {
/* ARGB888. [31:0] A:R:G:B 8:8:8:8 little endian */
{
.pixelformat = V4L2_PIX_FMT_ABGR32, /* is actually ARGB */
.nb_planes = 1,
.bpp = 32,
.bpp_plane0 = 32,
.w_align = 1,
.h_align = 1
},
/* XRGB888. [31:0] x:R:G:B 8:8:8:8 little endian */
{
.pixelformat = V4L2_PIX_FMT_XBGR32, /* is actually xRGB */
.nb_planes = 1,
.bpp = 32,
.bpp_plane0 = 32,
.w_align = 1,
.h_align = 1
},
/* RGB565. [15:0] R:G:B 5:6:5 little endian */
{
.pixelformat = V4L2_PIX_FMT_RGB565,
.nb_planes = 1,
.bpp = 16,
.bpp_plane0 = 16,
.w_align = 1,
.h_align = 1
},
/* NV12. YUV420SP - 1 plane for Y + 1 plane for (CbCr) */
{
.pixelformat = V4L2_PIX_FMT_NV12,
.nb_planes = 2,
.bpp = 12,
.bpp_plane0 = 8,
.w_align = 2,
.h_align = 2
},
/* RGB888. [23:0] B:G:R 8:8:8 little endian */
{
.pixelformat = V4L2_PIX_FMT_RGB24,
.nb_planes = 1,
.bpp = 24,
.bpp_plane0 = 24,
.w_align = 1,
.h_align = 1
},
/* YU12. YUV420P - 1 plane for Y + 1 plane for Cb + 1 plane for Cr
* To keep as the LAST element of this table (no support on capture)
*/
{
.pixelformat = V4L2_PIX_FMT_YUV420,
.nb_planes = 3,
.bpp = 12,
.bpp_plane0 = 8,
.w_align = 2,
.h_align = 2
}
};
/* Default format : HD ARGB32*/
#define BDISP_DEF_WIDTH 1920
#define BDISP_DEF_HEIGHT 1080
static const struct bdisp_frame bdisp_dflt_fmt = {
.width = BDISP_DEF_WIDTH,
.height = BDISP_DEF_HEIGHT,
.fmt = &bdisp_formats[0],
.field = V4L2_FIELD_NONE,
.bytesperline = BDISP_DEF_WIDTH * 4,
.sizeimage = BDISP_DEF_WIDTH * BDISP_DEF_HEIGHT * 4,
.colorspace = V4L2_COLORSPACE_REC709,
.crop = {0, 0, BDISP_DEF_WIDTH, BDISP_DEF_HEIGHT},
.paddr = {0, 0, 0, 0}
};
static inline void bdisp_ctx_state_lock_set(u32 state, struct bdisp_ctx *ctx)
{
unsigned long flags;
spin_lock_irqsave(&ctx->bdisp_dev->slock, flags);
ctx->state |= state;
spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags);
}
static inline void bdisp_ctx_state_lock_clear(u32 state, struct bdisp_ctx *ctx)
{
unsigned long flags;
spin_lock_irqsave(&ctx->bdisp_dev->slock, flags);
ctx->state &= ~state;
spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags);
}
static inline bool bdisp_ctx_state_is_set(u32 mask, struct bdisp_ctx *ctx)
{
unsigned long flags;
bool ret;
spin_lock_irqsave(&ctx->bdisp_dev->slock, flags);
ret = (ctx->state & mask) == mask;
spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags);
return ret;
}
static const struct bdisp_fmt *bdisp_find_fmt(u32 pixelformat)
{
const struct bdisp_fmt *fmt;
unsigned int i;
for (i = 0; i < ARRAY_SIZE(bdisp_formats); i++) {
fmt = &bdisp_formats[i];
if (fmt->pixelformat == pixelformat)
return fmt;
}
return NULL;
}
static struct bdisp_frame *ctx_get_frame(struct bdisp_ctx *ctx,
enum v4l2_buf_type type)
{
switch (type) {
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
return &ctx->src;
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
return &ctx->dst;
default:
dev_err(ctx->bdisp_dev->dev,
"Wrong buffer/video queue type (%d)\n", type);
break;
}
return ERR_PTR(-EINVAL);
}
static void bdisp_job_finish(struct bdisp_ctx *ctx, int vb_state)
{
struct vb2_v4l2_buffer *src_vb, *dst_vb;
if (WARN(!ctx || !ctx->fh.m2m_ctx, "Null hardware context\n"))
return;
dev_dbg(ctx->bdisp_dev->dev, "%s\n", __func__);
src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
if (src_vb && dst_vb) {
dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
dst_vb->timecode = src_vb->timecode;
dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
dst_vb->flags |= src_vb->flags &
V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
v4l2_m2m_buf_done(src_vb, vb_state);
v4l2_m2m_buf_done(dst_vb, vb_state);
v4l2_m2m_job_finish(ctx->bdisp_dev->m2m.m2m_dev,
ctx->fh.m2m_ctx);
}
}
static int bdisp_ctx_stop_req(struct bdisp_ctx *ctx)
{
struct bdisp_ctx *curr_ctx;
struct bdisp_dev *bdisp = ctx->bdisp_dev;
int ret;
dev_dbg(ctx->bdisp_dev->dev, "%s\n", __func__);
cancel_delayed_work(&bdisp->timeout_work);
curr_ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev);
if (!test_bit(ST_M2M_RUNNING, &bdisp->state) || (curr_ctx != ctx))
return 0;
bdisp_ctx_state_lock_set(BDISP_CTX_STOP_REQ, ctx);
ret = wait_event_timeout(bdisp->irq_queue,
!bdisp_ctx_state_is_set(BDISP_CTX_STOP_REQ, ctx),
BDISP_WORK_TIMEOUT);
if (!ret) {
dev_err(ctx->bdisp_dev->dev, "%s IRQ timeout\n", __func__);
return -ETIMEDOUT;
}
return 0;
}
static void __bdisp_job_abort(struct bdisp_ctx *ctx)
{
int ret;
ret = bdisp_ctx_stop_req(ctx);
if ((ret == -ETIMEDOUT) || (ctx->state & BDISP_CTX_ABORT)) {
bdisp_ctx_state_lock_clear(BDISP_CTX_STOP_REQ | BDISP_CTX_ABORT,
ctx);
bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR);
}
}
static void bdisp_job_abort(void *priv)
{
__bdisp_job_abort((struct bdisp_ctx *)priv);
}
static int bdisp_get_addr(struct bdisp_ctx *ctx, struct vb2_buffer *vb,
struct bdisp_frame *frame, dma_addr_t *paddr)
{
if (!vb || !frame)
return -EINVAL;
paddr[0] = vb2_dma_contig_plane_dma_addr(vb, 0);
if (frame->fmt->nb_planes > 1)
/* UV (NV12) or U (420P) */
paddr[1] = (dma_addr_t)(paddr[0] +
frame->bytesperline * frame->height);
if (frame->fmt->nb_planes > 2)
/* V (420P) */
paddr[2] = (dma_addr_t)(paddr[1] +
(frame->bytesperline * frame->height) / 4);
if (frame->fmt->nb_planes > 3)
dev_dbg(ctx->bdisp_dev->dev, "ignoring some planes\n");
dev_dbg(ctx->bdisp_dev->dev,
"%s plane[0]=%pad plane[1]=%pad plane[2]=%pad\n",
__func__, &paddr[0], &paddr[1], &paddr[2]);
return 0;
}
static int bdisp_get_bufs(struct bdisp_ctx *ctx)
{
struct bdisp_frame *src, *dst;
struct vb2_v4l2_buffer *src_vb, *dst_vb;
int ret;
src = &ctx->src;
dst = &ctx->dst;
src_vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
ret = bdisp_get_addr(ctx, &src_vb->vb2_buf, src, src->paddr);
if (ret)
return ret;
dst_vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
ret = bdisp_get_addr(ctx, &dst_vb->vb2_buf, dst, dst->paddr);
if (ret)
return ret;
dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
return 0;
}
static void bdisp_device_run(void *priv)
{
struct bdisp_ctx *ctx = priv;
struct bdisp_dev *bdisp;
unsigned long flags;
int err = 0;
if (WARN(!ctx, "Null hardware context\n"))
return;
bdisp = ctx->bdisp_dev;
dev_dbg(bdisp->dev, "%s\n", __func__);
spin_lock_irqsave(&bdisp->slock, flags);
if (bdisp->m2m.ctx != ctx) {
dev_dbg(bdisp->dev, "ctx updated: %p -> %p\n",
bdisp->m2m.ctx, ctx);
ctx->state |= BDISP_PARAMS;
bdisp->m2m.ctx = ctx;
}
if (ctx->state & BDISP_CTX_STOP_REQ) {
ctx->state &= ~BDISP_CTX_STOP_REQ;
ctx->state |= BDISP_CTX_ABORT;
wake_up(&bdisp->irq_queue);
goto out;
}
err = bdisp_get_bufs(ctx);
if (err) {
dev_err(bdisp->dev, "cannot get address\n");
goto out;
}
bdisp_dbg_perf_begin(bdisp);
err = bdisp_hw_reset(bdisp);
if (err) {
dev_err(bdisp->dev, "could not get HW ready\n");
goto out;
}
err = bdisp_hw_update(ctx);
if (err) {
dev_err(bdisp->dev, "could not send HW request\n");
goto out;
}
queue_delayed_work(bdisp->work_queue, &bdisp->timeout_work,
BDISP_WORK_TIMEOUT);
set_bit(ST_M2M_RUNNING, &bdisp->state);
out:
ctx->state &= ~BDISP_PARAMS;
spin_unlock_irqrestore(&bdisp->slock, flags);
if (err)
bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR);
}
static const struct v4l2_m2m_ops bdisp_m2m_ops = {
.device_run = bdisp_device_run,
.job_abort = bdisp_job_abort,
};
static int __bdisp_s_ctrl(struct bdisp_ctx *ctx, struct v4l2_ctrl *ctrl)
{
if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
return 0;
switch (ctrl->id) {
case V4L2_CID_HFLIP:
ctx->hflip = ctrl->val;
break;
case V4L2_CID_VFLIP:
ctx->vflip = ctrl->val;
break;
default:
dev_err(ctx->bdisp_dev->dev, "unknown control %d\n", ctrl->id);
return -EINVAL;
}
ctx->state |= BDISP_PARAMS;
return 0;
}
static int bdisp_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct bdisp_ctx *ctx = container_of(ctrl->handler, struct bdisp_ctx,
ctrl_handler);
unsigned long flags;
int ret;
spin_lock_irqsave(&ctx->bdisp_dev->slock, flags);
ret = __bdisp_s_ctrl(ctx, ctrl);
spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags);
return ret;
}
static const struct v4l2_ctrl_ops bdisp_c_ops = {
.s_ctrl = bdisp_s_ctrl,
};
static int bdisp_ctrls_create(struct bdisp_ctx *ctx)
{
if (ctx->ctrls_rdy)
return 0;
v4l2_ctrl_handler_init(&ctx->ctrl_handler, BDISP_MAX_CTRL_NUM);
ctx->bdisp_ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
&bdisp_c_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
ctx->bdisp_ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
&bdisp_c_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
if (ctx->ctrl_handler.error) {
int err = ctx->ctrl_handler.error;
v4l2_ctrl_handler_free(&ctx->ctrl_handler);
return err;
}
ctx->ctrls_rdy = true;
return 0;
}
static void bdisp_ctrls_delete(struct bdisp_ctx *ctx)
{
if (ctx->ctrls_rdy) {
v4l2_ctrl_handler_free(&ctx->ctrl_handler);
ctx->ctrls_rdy = false;
}
}
static int bdisp_queue_setup(struct vb2_queue *vq,
unsigned int *nb_buf, unsigned int *nb_planes,
unsigned int sizes[], struct device *alloc_devs[])
{
struct bdisp_ctx *ctx = vb2_get_drv_priv(vq);
struct bdisp_frame *frame = ctx_get_frame(ctx, vq->type);
if (IS_ERR(frame)) {
dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame);
return PTR_ERR(frame);
}
if (!frame->fmt) {
dev_err(ctx->bdisp_dev->dev, "Invalid format\n");
return -EINVAL;
}
if (*nb_planes)
return sizes[0] < frame->sizeimage ? -EINVAL : 0;
*nb_planes = 1;
sizes[0] = frame->sizeimage;
return 0;
}
static int bdisp_buf_prepare(struct vb2_buffer *vb)
{
struct bdisp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
struct bdisp_frame *frame = ctx_get_frame(ctx, vb->vb2_queue->type);
if (IS_ERR(frame)) {
dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame);
return PTR_ERR(frame);
}
if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
vb2_set_plane_payload(vb, 0, frame->sizeimage);
return 0;
}
static void bdisp_buf_queue(struct vb2_buffer *vb)
{
struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
struct bdisp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
/* return to V4L2 any 0-size buffer so it can be dequeued by user */
if (!vb2_get_plane_payload(vb, 0)) {
dev_dbg(ctx->bdisp_dev->dev, "0 data buffer, skip it\n");
vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
return;
}
if (ctx->fh.m2m_ctx)
v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
}
static int bdisp_start_streaming(struct vb2_queue *q, unsigned int count)
{
struct bdisp_ctx *ctx = q->drv_priv;
struct vb2_v4l2_buffer *buf;
int ret = pm_runtime_get_sync(ctx->bdisp_dev->dev);
if (ret < 0) {
dev_err(ctx->bdisp_dev->dev, "failed to set runtime PM\n");
if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx)))
v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
} else {
while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx)))
v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED);
}
return ret;
}
return 0;
}
static void bdisp_stop_streaming(struct vb2_queue *q)
{
struct bdisp_ctx *ctx = q->drv_priv;
__bdisp_job_abort(ctx);
pm_runtime_put(ctx->bdisp_dev->dev);
}
static const struct vb2_ops bdisp_qops = {
.queue_setup = bdisp_queue_setup,
.buf_prepare = bdisp_buf_prepare,
.buf_queue = bdisp_buf_queue,
.wait_prepare = vb2_ops_wait_prepare,
.wait_finish = vb2_ops_wait_finish,
.stop_streaming = bdisp_stop_streaming,
.start_streaming = bdisp_start_streaming,
};
static int queue_init(void *priv,
struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
{
struct bdisp_ctx *ctx = priv;
int ret;
memset(src_vq, 0, sizeof(*src_vq));
src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
src_vq->drv_priv = ctx;
src_vq->ops = &bdisp_qops;
src_vq->mem_ops = &vb2_dma_contig_memops;
src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
src_vq->lock = &ctx->bdisp_dev->lock;
src_vq->dev = ctx->bdisp_dev->v4l2_dev.dev;
ret = vb2_queue_init(src_vq);
if (ret)
return ret;
memset(dst_vq, 0, sizeof(*dst_vq));
dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
dst_vq->drv_priv = ctx;
dst_vq->ops = &bdisp_qops;
dst_vq->mem_ops = &vb2_dma_contig_memops;
dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
dst_vq->lock = &ctx->bdisp_dev->lock;
dst_vq->dev = ctx->bdisp_dev->v4l2_dev.dev;
return vb2_queue_init(dst_vq);
}
static int bdisp_open(struct file *file)
{
struct bdisp_dev *bdisp = video_drvdata(file);
struct bdisp_ctx *ctx = NULL;
int ret;
if (mutex_lock_interruptible(&bdisp->lock))
return -ERESTARTSYS;
/* Allocate memory for both context and node */
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
if (!ctx) {
ret = -ENOMEM;
goto unlock;
}
ctx->bdisp_dev = bdisp;
if (bdisp_hw_alloc_nodes(ctx)) {
dev_err(bdisp->dev, "no memory for nodes\n");
ret = -ENOMEM;
goto mem_ctx;
}
v4l2_fh_init(&ctx->fh, bdisp->m2m.vdev);
ret = bdisp_ctrls_create(ctx);
if (ret) {
dev_err(bdisp->dev, "Failed to create control\n");
goto error_fh;
}
/* Use separate control handler per file handle */
ctx->fh.ctrl_handler = &ctx->ctrl_handler;
file->private_data = &ctx->fh;
v4l2_fh_add(&ctx->fh);
/* Default format */
ctx->src = bdisp_dflt_fmt;
ctx->dst = bdisp_dflt_fmt;
/* Setup the device context for mem2mem mode. */
ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(bdisp->m2m.m2m_dev, ctx,
queue_init);
if (IS_ERR(ctx->fh.m2m_ctx)) {
dev_err(bdisp->dev, "Failed to initialize m2m context\n");
ret = PTR_ERR(ctx->fh.m2m_ctx);
goto error_ctrls;
}
bdisp->m2m.refcnt++;
set_bit(ST_M2M_OPEN, &bdisp->state);
dev_dbg(bdisp->dev, "driver opened, ctx = 0x%p\n", ctx);
mutex_unlock(&bdisp->lock);
return 0;
error_ctrls:
bdisp_ctrls_delete(ctx);
v4l2_fh_del(&ctx->fh);
error_fh:
v4l2_fh_exit(&ctx->fh);
bdisp_hw_free_nodes(ctx);
mem_ctx:
kfree(ctx);
unlock:
mutex_unlock(&bdisp->lock);
return ret;
}
static int bdisp_release(struct file *file)
{
struct bdisp_ctx *ctx = fh_to_ctx(file->private_data);
struct bdisp_dev *bdisp = ctx->bdisp_dev;
dev_dbg(bdisp->dev, "%s\n", __func__);
mutex_lock(&bdisp->lock);
v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
bdisp_ctrls_delete(ctx);
v4l2_fh_del(&ctx->fh);
v4l2_fh_exit(&ctx->fh);
if (--bdisp->m2m.refcnt <= 0)
clear_bit(ST_M2M_OPEN, &bdisp->state);
bdisp_hw_free_nodes(ctx);
kfree(ctx);
mutex_unlock(&bdisp->lock);
return 0;
}
static const struct v4l2_file_operations bdisp_fops = {
.owner = THIS_MODULE,
.open = bdisp_open,
.release = bdisp_release,
.poll = v4l2_m2m_fop_poll,
.unlocked_ioctl = video_ioctl2,
.mmap = v4l2_m2m_fop_mmap,
};
static int bdisp_querycap(struct file *file, void *fh,
struct v4l2_capability *cap)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
struct bdisp_dev *bdisp = ctx->bdisp_dev;
strscpy(cap->driver, bdisp->pdev->name, sizeof(cap->driver));
strscpy(cap->card, bdisp->pdev->name, sizeof(cap->card));
snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s%d",
BDISP_NAME, bdisp->id);
return 0;
}
static int bdisp_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
const struct bdisp_fmt *fmt;
if (f->index >= ARRAY_SIZE(bdisp_formats))
return -EINVAL;
fmt = &bdisp_formats[f->index];
if ((fmt->pixelformat == V4L2_PIX_FMT_YUV420) &&
(f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
dev_dbg(ctx->bdisp_dev->dev, "No YU12 on capture\n");
return -EINVAL;
}
f->pixelformat = fmt->pixelformat;
return 0;
}
static int bdisp_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
struct v4l2_pix_format *pix;
struct bdisp_frame *frame = ctx_get_frame(ctx, f->type);
if (IS_ERR(frame)) {
dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame);
return PTR_ERR(frame);
}
pix = &f->fmt.pix;
pix->width = frame->width;
pix->height = frame->height;
pix->pixelformat = frame->fmt->pixelformat;
pix->field = frame->field;
pix->bytesperline = frame->bytesperline;
pix->sizeimage = frame->sizeimage;
pix->colorspace = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ?
frame->colorspace : bdisp_dflt_fmt.colorspace;
return 0;
}
static int bdisp_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
struct v4l2_pix_format *pix = &f->fmt.pix;
const struct bdisp_fmt *format;
u32 in_w, in_h;
format = bdisp_find_fmt(pix->pixelformat);
if (!format) {
dev_dbg(ctx->bdisp_dev->dev, "Unknown format 0x%x\n",
pix->pixelformat);
return -EINVAL;
}
/* YUV420P only supported for VIDEO_OUTPUT */
if ((format->pixelformat == V4L2_PIX_FMT_YUV420) &&
(f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
dev_dbg(ctx->bdisp_dev->dev, "No YU12 on capture\n");
return -EINVAL;
}
/* Field (interlaced only supported on OUTPUT) */
if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
(pix->field != V4L2_FIELD_INTERLACED))
pix->field = V4L2_FIELD_NONE;
/* Adjust width & height */
in_w = pix->width;
in_h = pix->height;
v4l_bound_align_image(&pix->width,
BDISP_MIN_W, BDISP_MAX_W,
ffs(format->w_align) - 1,
&pix->height,
BDISP_MIN_H, BDISP_MAX_H,
ffs(format->h_align) - 1,
0);
if ((pix->width != in_w) || (pix->height != in_h))
dev_dbg(ctx->bdisp_dev->dev,
"%s size updated: %dx%d -> %dx%d\n", __func__,
in_w, in_h, pix->width, pix->height);
pix->bytesperline = (pix->width * format->bpp_plane0) / 8;
pix->sizeimage = (pix->width * pix->height * format->bpp) / 8;
if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
pix->colorspace = bdisp_dflt_fmt.colorspace;
return 0;
}
static int bdisp_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
struct vb2_queue *vq;
struct bdisp_frame *frame;
struct v4l2_pix_format *pix;
int ret;
u32 state;
ret = bdisp_try_fmt(file, fh, f);
if (ret) {
dev_err(ctx->bdisp_dev->dev, "Cannot set format\n");
return ret;
}
vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
if (vb2_is_streaming(vq)) {
dev_err(ctx->bdisp_dev->dev, "queue (%d) busy\n", f->type);
return -EBUSY;
}
frame = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ?
&ctx->src : &ctx->dst;
pix = &f->fmt.pix;
frame->fmt = bdisp_find_fmt(pix->pixelformat);
if (!frame->fmt) {
dev_err(ctx->bdisp_dev->dev, "Unknown format 0x%x\n",
pix->pixelformat);
return -EINVAL;
}
frame->width = pix->width;
frame->height = pix->height;
frame->bytesperline = pix->bytesperline;
frame->sizeimage = pix->sizeimage;
frame->field = pix->field;
if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
frame->colorspace = pix->colorspace;
frame->crop.width = frame->width;
frame->crop.height = frame->height;
frame->crop.left = 0;
frame->crop.top = 0;
state = BDISP_PARAMS;
state |= (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
BDISP_DST_FMT : BDISP_SRC_FMT;
bdisp_ctx_state_lock_set(state, ctx);
return 0;
}
static int bdisp_g_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct bdisp_frame *frame;
struct bdisp_ctx *ctx = fh_to_ctx(fh);
frame = ctx_get_frame(ctx, s->type);
if (IS_ERR(frame)) {
dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame);
return PTR_ERR(frame);
}
switch (s->type) {
case V4L2_BUF_TYPE_VIDEO_OUTPUT:
switch (s->target) {
case V4L2_SEL_TGT_CROP:
/* cropped frame */
s->r = frame->crop;
break;
case V4L2_SEL_TGT_CROP_DEFAULT:
case V4L2_SEL_TGT_CROP_BOUNDS:
/* complete frame */
s->r.left = 0;
s->r.top = 0;
s->r.width = frame->width;
s->r.height = frame->height;
break;
default:
dev_err(ctx->bdisp_dev->dev, "Invalid target\n");
return -EINVAL;
}
break;
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
switch (s->target) {
case V4L2_SEL_TGT_COMPOSE:
case V4L2_SEL_TGT_COMPOSE_PADDED:
/* composed (cropped) frame */
s->r = frame->crop;
break;
case V4L2_SEL_TGT_COMPOSE_DEFAULT:
case V4L2_SEL_TGT_COMPOSE_BOUNDS:
/* complete frame */
s->r.left = 0;
s->r.top = 0;
s->r.width = frame->width;
s->r.height = frame->height;
break;
default:
dev_err(ctx->bdisp_dev->dev, "Invalid target\n");
return -EINVAL;
}
break;
default:
dev_err(ctx->bdisp_dev->dev, "Invalid type\n");
return -EINVAL;
}
return 0;
}
static int is_rect_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
{
/* Return 1 if a is enclosed in b, or 0 otherwise. */
if (a->left < b->left || a->top < b->top)
return 0;
if (a->left + a->width > b->left + b->width)
return 0;
if (a->top + a->height > b->top + b->height)
return 0;
return 1;
}
static int bdisp_s_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct bdisp_frame *frame;
struct bdisp_ctx *ctx = fh_to_ctx(fh);
struct v4l2_rect *in, out;
bool valid = false;
if ((s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) &&
(s->target == V4L2_SEL_TGT_CROP))
valid = true;
if ((s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
(s->target == V4L2_SEL_TGT_COMPOSE))
valid = true;
if (!valid) {
dev_err(ctx->bdisp_dev->dev, "Invalid type / target\n");
return -EINVAL;
}
frame = ctx_get_frame(ctx, s->type);
if (IS_ERR(frame)) {
dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame);
return PTR_ERR(frame);
}
in = &s->r;
out = *in;
/* Align and check origin */
out.left = ALIGN(in->left, frame->fmt->w_align);
out.top = ALIGN(in->top, frame->fmt->h_align);
if ((out.left < 0) || (out.left >= frame->width) ||
(out.top < 0) || (out.top >= frame->height)) {
dev_err(ctx->bdisp_dev->dev,
"Invalid crop: %dx%d@(%d,%d) vs frame: %dx%d\n",
out.width, out.height, out.left, out.top,
frame->width, frame->height);
return -EINVAL;
}
/* Align and check size */
out.width = ALIGN(in->width, frame->fmt->w_align);
out.height = ALIGN(in->height, frame->fmt->w_align);
if (((out.left + out.width) > frame->width) ||
((out.top + out.height) > frame->height)) {
dev_err(ctx->bdisp_dev->dev,
"Invalid crop: %dx%d@(%d,%d) vs frame: %dx%d\n",
out.width, out.height, out.left, out.top,
frame->width, frame->height);
return -EINVAL;
}
/* Checks adjust constraints flags */
if (s->flags & V4L2_SEL_FLAG_LE && !is_rect_enclosed(&out, in))
return -ERANGE;
if (s->flags & V4L2_SEL_FLAG_GE && !is_rect_enclosed(in, &out))
return -ERANGE;
if ((out.left != in->left) || (out.top != in->top) ||
(out.width != in->width) || (out.height != in->height)) {
dev_dbg(ctx->bdisp_dev->dev,
"%s crop updated: %dx%d@(%d,%d) -> %dx%d@(%d,%d)\n",
__func__, in->width, in->height, in->left, in->top,
out.width, out.height, out.left, out.top);
*in = out;
}
frame->crop = out;
bdisp_ctx_state_lock_set(BDISP_PARAMS, ctx);
return 0;
}
static int bdisp_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
{
struct bdisp_ctx *ctx = fh_to_ctx(fh);
if ((type == V4L2_BUF_TYPE_VIDEO_OUTPUT) &&
!bdisp_ctx_state_is_set(BDISP_SRC_FMT, ctx)) {
dev_err(ctx->bdisp_dev->dev, "src not defined\n");
return -EINVAL;
}
if ((type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
!bdisp_ctx_state_is_set(BDISP_DST_FMT, ctx)) {
dev_err(ctx->bdisp_dev->dev, "dst not defined\n");
return -EINVAL;
}
return v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, type);
}
static const struct v4l2_ioctl_ops bdisp_ioctl_ops = {
.vidioc_querycap = bdisp_querycap,
.vidioc_enum_fmt_vid_cap = bdisp_enum_fmt,
.vidioc_enum_fmt_vid_out = bdisp_enum_fmt,
.vidioc_g_fmt_vid_cap = bdisp_g_fmt,
.vidioc_g_fmt_vid_out = bdisp_g_fmt,
.vidioc_try_fmt_vid_cap = bdisp_try_fmt,
.vidioc_try_fmt_vid_out = bdisp_try_fmt,
.vidioc_s_fmt_vid_cap = bdisp_s_fmt,
.vidioc_s_fmt_vid_out = bdisp_s_fmt,
.vidioc_g_selection = bdisp_g_selection,
.vidioc_s_selection = bdisp_s_selection,
.vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
.vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
.vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
.vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
.vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
.vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
.vidioc_streamon = bdisp_streamon,
.vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
static int bdisp_register_device(struct bdisp_dev *bdisp)
{
int ret;
if (!bdisp)
return -ENODEV;
bdisp->vdev.fops = &bdisp_fops;
bdisp->vdev.ioctl_ops = &bdisp_ioctl_ops;
bdisp->vdev.release = video_device_release_empty;
bdisp->vdev.lock = &bdisp->lock;
bdisp->vdev.vfl_dir = VFL_DIR_M2M;
bdisp->vdev.v4l2_dev = &bdisp->v4l2_dev;
bdisp->vdev.device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M;
snprintf(bdisp->vdev.name, sizeof(bdisp->vdev.name), "%s.%d",
BDISP_NAME, bdisp->id);
video_set_drvdata(&bdisp->vdev, bdisp);
bdisp->m2m.vdev = &bdisp->vdev;
bdisp->m2m.m2m_dev = v4l2_m2m_init(&bdisp_m2m_ops);
if (IS_ERR(bdisp->m2m.m2m_dev)) {
dev_err(bdisp->dev, "failed to initialize v4l2-m2m device\n");
return PTR_ERR(bdisp->m2m.m2m_dev);
}
ret = video_register_device(&bdisp->vdev, VFL_TYPE_GRABBER, -1);
if (ret) {
dev_err(bdisp->dev,
"%s(): failed to register video device\n", __func__);
v4l2_m2m_release(bdisp->m2m.m2m_dev);
return ret;
}
return 0;
}
static void bdisp_unregister_device(struct bdisp_dev *bdisp)
{
if (!bdisp)
return;
if (bdisp->m2m.m2m_dev)
v4l2_m2m_release(bdisp->m2m.m2m_dev);
video_unregister_device(bdisp->m2m.vdev);
}
static irqreturn_t bdisp_irq_thread(int irq, void *priv)
{
struct bdisp_dev *bdisp = priv;
struct bdisp_ctx *ctx;
spin_lock(&bdisp->slock);
bdisp_dbg_perf_end(bdisp);
cancel_delayed_work(&bdisp->timeout_work);
if (!test_and_clear_bit(ST_M2M_RUNNING, &bdisp->state))
goto isr_unlock;
if (test_and_clear_bit(ST_M2M_SUSPENDING, &bdisp->state)) {
set_bit(ST_M2M_SUSPENDED, &bdisp->state);
wake_up(&bdisp->irq_queue);
goto isr_unlock;
}
ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev);
if (!ctx || !ctx->fh.m2m_ctx)
goto isr_unlock;
spin_unlock(&bdisp->slock);
bdisp_job_finish(ctx, VB2_BUF_STATE_DONE);
if (bdisp_ctx_state_is_set(BDISP_CTX_STOP_REQ, ctx)) {
bdisp_ctx_state_lock_clear(BDISP_CTX_STOP_REQ, ctx);
wake_up(&bdisp->irq_queue);
}
return IRQ_HANDLED;
isr_unlock:
spin_unlock(&bdisp->slock);
return IRQ_HANDLED;
}
static irqreturn_t bdisp_irq_handler(int irq, void *priv)
{
if (bdisp_hw_get_and_clear_irq((struct bdisp_dev *)priv))
return IRQ_NONE;
else
return IRQ_WAKE_THREAD;
}
static void bdisp_irq_timeout(struct work_struct *ptr)
{
struct delayed_work *twork = to_delayed_work(ptr);
struct bdisp_dev *bdisp = container_of(twork, struct bdisp_dev,
timeout_work);
struct bdisp_ctx *ctx;
ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev);
dev_err(ctx->bdisp_dev->dev, "Device work timeout\n");
spin_lock(&bdisp->slock);
clear_bit(ST_M2M_RUNNING, &bdisp->state);
spin_unlock(&bdisp->slock);
bdisp_hw_reset(bdisp);
bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR);
}
static int bdisp_m2m_suspend(struct bdisp_dev *bdisp)
{
unsigned long flags;
int timeout;
spin_lock_irqsave(&bdisp->slock, flags);
if (!test_bit(ST_M2M_RUNNING, &bdisp->state)) {
spin_unlock_irqrestore(&bdisp->slock, flags);
return 0;
}
clear_bit(ST_M2M_SUSPENDED, &bdisp->state);
set_bit(ST_M2M_SUSPENDING, &bdisp->state);
spin_unlock_irqrestore(&bdisp->slock, flags);
timeout = wait_event_timeout(bdisp->irq_queue,
test_bit(ST_M2M_SUSPENDED, &bdisp->state),
BDISP_WORK_TIMEOUT);
clear_bit(ST_M2M_SUSPENDING, &bdisp->state);
if (!timeout) {
dev_err(bdisp->dev, "%s IRQ timeout\n", __func__);
return -EAGAIN;
}
return 0;
}
static int bdisp_m2m_resume(struct bdisp_dev *bdisp)
{
struct bdisp_ctx *ctx;
unsigned long flags;
spin_lock_irqsave(&bdisp->slock, flags);
ctx = bdisp->m2m.ctx;
bdisp->m2m.ctx = NULL;
spin_unlock_irqrestore(&bdisp->slock, flags);
if (test_and_clear_bit(ST_M2M_SUSPENDED, &bdisp->state))
bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR);
return 0;
}
static int bdisp_runtime_resume(struct device *dev)
{
struct bdisp_dev *bdisp = dev_get_drvdata(dev);
int ret = clk_enable(bdisp->clock);
if (ret)
return ret;
return bdisp_m2m_resume(bdisp);
}
static int bdisp_runtime_suspend(struct device *dev)
{
struct bdisp_dev *bdisp = dev_get_drvdata(dev);
int ret = bdisp_m2m_suspend(bdisp);
if (!ret)
clk_disable(bdisp->clock);
return ret;
}
static int bdisp_resume(struct device *dev)
{
struct bdisp_dev *bdisp = dev_get_drvdata(dev);
unsigned long flags;
int opened;
spin_lock_irqsave(&bdisp->slock, flags);
opened = test_bit(ST_M2M_OPEN, &bdisp->state);
spin_unlock_irqrestore(&bdisp->slock, flags);
if (!opened)
return 0;
if (!pm_runtime_suspended(dev))
return bdisp_runtime_resume(dev);
return 0;
}
static int bdisp_suspend(struct device *dev)
{
if (!pm_runtime_suspended(dev))
return bdisp_runtime_suspend(dev);
return 0;
}
static const struct dev_pm_ops bdisp_pm_ops = {
.suspend = bdisp_suspend,
.resume = bdisp_resume,
.runtime_suspend = bdisp_runtime_suspend,
.runtime_resume = bdisp_runtime_resume,
};
static int bdisp_remove(struct platform_device *pdev)
{
struct bdisp_dev *bdisp = platform_get_drvdata(pdev);
bdisp_unregister_device(bdisp);
bdisp_hw_free_filters(bdisp->dev);
pm_runtime_disable(&pdev->dev);
bdisp_debugfs_remove(bdisp);
v4l2_device_unregister(&bdisp->v4l2_dev);
if (!IS_ERR(bdisp->clock))
clk_unprepare(bdisp->clock);
dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name);
return 0;
}
static int bdisp_probe(struct platform_device *pdev)
{
struct bdisp_dev *bdisp;
struct resource *res;
struct device *dev = &pdev->dev;
int ret;
dev_dbg(dev, "%s\n", __func__);
bdisp = devm_kzalloc(dev, sizeof(struct bdisp_dev), GFP_KERNEL);
if (!bdisp)
return -ENOMEM;
ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
if (ret)
return ret;
bdisp->pdev = pdev;
bdisp->dev = dev;
platform_set_drvdata(pdev, bdisp);
if (dev->of_node)
bdisp->id = of_alias_get_id(pdev->dev.of_node, BDISP_NAME);
else
bdisp->id = pdev->id;
init_waitqueue_head(&bdisp->irq_queue);
INIT_DELAYED_WORK(&bdisp->timeout_work, bdisp_irq_timeout);
bdisp->work_queue = create_workqueue(BDISP_NAME);
spin_lock_init(&bdisp->slock);
mutex_init(&bdisp->lock);
/* get resources */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
bdisp->regs = devm_ioremap_resource(dev, res);
if (IS_ERR(bdisp->regs)) {
dev_err(dev, "failed to get regs\n");
return PTR_ERR(bdisp->regs);
}
bdisp->clock = devm_clk_get(dev, BDISP_NAME);
if (IS_ERR(bdisp->clock)) {
dev_err(dev, "failed to get clock\n");
return PTR_ERR(bdisp->clock);
}
ret = clk_prepare(bdisp->clock);
if (ret < 0) {
dev_err(dev, "clock prepare failed\n");
bdisp->clock = ERR_PTR(-EINVAL);
return ret;
}
res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (!res) {
dev_err(dev, "failed to get IRQ resource\n");
ret = -EINVAL;
goto err_clk;
}
ret = devm_request_threaded_irq(dev, res->start, bdisp_irq_handler,
bdisp_irq_thread, IRQF_ONESHOT,
pdev->name, bdisp);
if (ret) {
dev_err(dev, "failed to install irq\n");
goto err_clk;
}
/* v4l2 register */
ret = v4l2_device_register(dev, &bdisp->v4l2_dev);
if (ret) {
dev_err(dev, "failed to register\n");
goto err_clk;
}
/* Debug */
ret = bdisp_debugfs_create(bdisp);
if (ret) {
dev_err(dev, "failed to create debugfs\n");
goto err_v4l2;
}
/* Power management */
pm_runtime_enable(dev);
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "failed to set PM\n");
goto err_dbg;
}
/* Filters */
if (bdisp_hw_alloc_filters(bdisp->dev)) {
dev_err(bdisp->dev, "no memory for filters\n");
ret = -ENOMEM;
goto err_pm;
}
/* Register */
ret = bdisp_register_device(bdisp);
if (ret) {
dev_err(dev, "failed to register\n");
goto err_filter;
}
dev_info(dev, "%s%d registered as /dev/video%d\n", BDISP_NAME,
bdisp->id, bdisp->vdev.num);
pm_runtime_put(dev);
return 0;
err_filter:
bdisp_hw_free_filters(bdisp->dev);
err_pm:
pm_runtime_put(dev);
err_dbg:
bdisp_debugfs_remove(bdisp);
err_v4l2:
v4l2_device_unregister(&bdisp->v4l2_dev);
err_clk:
if (!IS_ERR(bdisp->clock))
clk_unprepare(bdisp->clock);
return ret;
}
static const struct of_device_id bdisp_match_types[] = {
{
.compatible = "st,stih407-bdisp",
},
{ /* end node */ }
};
MODULE_DEVICE_TABLE(of, bdisp_match_types);
static struct platform_driver bdisp_driver = {
.probe = bdisp_probe,
.remove = bdisp_remove,
.driver = {
.name = BDISP_NAME,
.of_match_table = bdisp_match_types,
.pm = &bdisp_pm_ops,
},
};
module_platform_driver(bdisp_driver);
MODULE_DESCRIPTION("2D blitter for STMicroelectronics SoC");
MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
MODULE_LICENSE("GPL");
|