0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/io.h>
0013 #include <linux/delay.h>
0014 #include <linux/device.h>
0015 #include <linux/dma-mapping.h>
0016 #include <linux/err.h>
0017 #include <linux/errno.h>
0018 #include <linux/fs.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/kernel.h>
0021 #include <linux/mm.h>
0022 #include <linux/moduleparam.h>
0023 #include <linux/of.h>
0024 #include <linux/of_graph.h>
0025 #include <linux/time.h>
0026 #include <linux/platform_device.h>
0027 #include <linux/clk.h>
0028 #include <linux/sched.h>
0029 #include <linux/slab.h>
0030 #include <linux/dmaengine.h>
0031 #include <linux/dma/pxa-dma.h>
0032
0033 #include <media/v4l2-async.h>
0034 #include <media/v4l2-common.h>
0035 #include <media/v4l2-ctrls.h>
0036 #include <media/v4l2-device.h>
0037 #include <media/v4l2-event.h>
0038 #include <media/v4l2-ioctl.h>
0039 #include <media/v4l2-fwnode.h>
0040
0041 #include <media/videobuf2-dma-sg.h>
0042
0043 #include <linux/videodev2.h>
0044
0045 #include <linux/platform_data/media/camera-pxa.h>
0046
0047 #define PXA_CAM_VERSION "0.0.6"
0048 #define PXA_CAM_DRV_NAME "pxa27x-camera"
0049
0050 #define DEFAULT_WIDTH 640
0051 #define DEFAULT_HEIGHT 480
0052
0053
0054 #define CICR0 0x0000
0055 #define CICR1 0x0004
0056 #define CICR2 0x0008
0057 #define CICR3 0x000C
0058 #define CICR4 0x0010
0059 #define CISR 0x0014
0060 #define CIFR 0x0018
0061 #define CITOR 0x001C
0062 #define CIBR0 0x0028
0063 #define CIBR1 0x0030
0064 #define CIBR2 0x0038
0065
0066 #define CICR0_DMAEN (1UL << 31)
0067 #define CICR0_PAR_EN (1 << 30)
0068 #define CICR0_SL_CAP_EN (1 << 29)
0069 #define CICR0_ENB (1 << 28)
0070 #define CICR0_DIS (1 << 27)
0071 #define CICR0_SIM (0x7 << 24)
0072 #define CICR0_TOM (1 << 9)
0073 #define CICR0_RDAVM (1 << 8)
0074 #define CICR0_FEM (1 << 7)
0075 #define CICR0_EOLM (1 << 6)
0076 #define CICR0_PERRM (1 << 5)
0077 #define CICR0_QDM (1 << 4)
0078 #define CICR0_CDM (1 << 3)
0079 #define CICR0_SOFM (1 << 2)
0080 #define CICR0_EOFM (1 << 1)
0081 #define CICR0_FOM (1 << 0)
0082
0083 #define CICR1_TBIT (1UL << 31)
0084 #define CICR1_RGBT_CONV (0x3 << 29)
0085 #define CICR1_PPL (0x7ff << 15)
0086 #define CICR1_RGB_CONV (0x7 << 12)
0087 #define CICR1_RGB_F (1 << 11)
0088 #define CICR1_YCBCR_F (1 << 10)
0089 #define CICR1_RGB_BPP (0x7 << 7)
0090 #define CICR1_RAW_BPP (0x3 << 5)
0091 #define CICR1_COLOR_SP (0x3 << 3)
0092 #define CICR1_DW (0x7 << 0)
0093
0094 #define CICR2_BLW (0xff << 24)
0095
0096 #define CICR2_ELW (0xff << 16)
0097
0098 #define CICR2_HSW (0x3f << 10)
0099 #define CICR2_BFPW (0x3f << 3)
0100
0101 #define CICR2_FSW (0x7 << 0)
0102
0103
0104 #define CICR3_BFW (0xff << 24)
0105
0106 #define CICR3_EFW (0xff << 16)
0107
0108 #define CICR3_VSW (0x3f << 10)
0109 #define CICR3_BFPW (0x3f << 3)
0110
0111 #define CICR3_LPF (0x7ff << 0)
0112
0113 #define CICR4_MCLK_DLY (0x3 << 24)
0114 #define CICR4_PCLK_EN (1 << 23)
0115 #define CICR4_PCP (1 << 22)
0116 #define CICR4_HSP (1 << 21)
0117 #define CICR4_VSP (1 << 20)
0118 #define CICR4_MCLK_EN (1 << 19)
0119 #define CICR4_FR_RATE (0x7 << 8)
0120 #define CICR4_DIV (0xff << 0)
0121
0122 #define CISR_FTO (1 << 15)
0123 #define CISR_RDAV_2 (1 << 14)
0124 #define CISR_RDAV_1 (1 << 13)
0125 #define CISR_RDAV_0 (1 << 12)
0126 #define CISR_FEMPTY_2 (1 << 11)
0127 #define CISR_FEMPTY_1 (1 << 10)
0128 #define CISR_FEMPTY_0 (1 << 9)
0129 #define CISR_EOL (1 << 8)
0130 #define CISR_PAR_ERR (1 << 7)
0131 #define CISR_CQD (1 << 6)
0132 #define CISR_CDD (1 << 5)
0133 #define CISR_SOF (1 << 4)
0134 #define CISR_EOF (1 << 3)
0135 #define CISR_IFO_2 (1 << 2)
0136 #define CISR_IFO_1 (1 << 1)
0137 #define CISR_IFO_0 (1 << 0)
0138
0139 #define CIFR_FLVL2 (0x7f << 23)
0140 #define CIFR_FLVL1 (0x7f << 16)
0141 #define CIFR_FLVL0 (0xff << 8)
0142 #define CIFR_THL_0 (0x3 << 4)
0143 #define CIFR_RESET_F (1 << 3)
0144 #define CIFR_FEN2 (1 << 2)
0145 #define CIFR_FEN1 (1 << 1)
0146 #define CIFR_FEN0 (1 << 0)
0147
0148 #define CICR0_SIM_MP (0 << 24)
0149 #define CICR0_SIM_SP (1 << 24)
0150 #define CICR0_SIM_MS (2 << 24)
0151 #define CICR0_SIM_EP (3 << 24)
0152 #define CICR0_SIM_ES (4 << 24)
0153
0154 #define CICR1_DW_VAL(x) ((x) & CICR1_DW)
0155 #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL)
0156 #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP)
0157 #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP)
0158 #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV)
0159
0160 #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW)
0161 #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW)
0162 #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW)
0163 #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW)
0164 #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW)
0165
0166 #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW)
0167 #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW)
0168 #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW)
0169 #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF)
0170
0171 #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
0172 CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
0173 CICR0_EOFM | CICR0_FOM)
0174
0175 #define sensor_call(cam, o, f, args...) \
0176 v4l2_subdev_call(cam->sensor, o, f, ##args)
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 enum pxa_mbus_packing {
0192 PXA_MBUS_PACKING_NONE,
0193 PXA_MBUS_PACKING_2X8_PADHI,
0194 PXA_MBUS_PACKING_EXTEND16,
0195 };
0196
0197
0198
0199
0200
0201
0202 enum pxa_mbus_order {
0203 PXA_MBUS_ORDER_LE,
0204 PXA_MBUS_ORDER_BE,
0205 };
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 enum pxa_mbus_layout {
0219 PXA_MBUS_LAYOUT_PACKED = 0,
0220 PXA_MBUS_LAYOUT_PLANAR_2Y_U_V,
0221 PXA_MBUS_LAYOUT_PLANAR_2Y_C,
0222 PXA_MBUS_LAYOUT_PLANAR_Y_C,
0223 };
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 struct pxa_mbus_pixelfmt {
0236 const char *name;
0237 u32 fourcc;
0238 enum pxa_mbus_packing packing;
0239 enum pxa_mbus_order order;
0240 enum pxa_mbus_layout layout;
0241 u8 bits_per_sample;
0242 };
0243
0244
0245
0246
0247
0248
0249 struct pxa_mbus_lookup {
0250 u32 code;
0251 struct pxa_mbus_pixelfmt fmt;
0252 };
0253
0254 static const struct pxa_mbus_lookup mbus_fmt[] = {
0255 {
0256 .code = MEDIA_BUS_FMT_YUYV8_2X8,
0257 .fmt = {
0258 .fourcc = V4L2_PIX_FMT_YUYV,
0259 .name = "YUYV",
0260 .bits_per_sample = 8,
0261 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0262 .order = PXA_MBUS_ORDER_LE,
0263 .layout = PXA_MBUS_LAYOUT_PACKED,
0264 },
0265 }, {
0266 .code = MEDIA_BUS_FMT_YVYU8_2X8,
0267 .fmt = {
0268 .fourcc = V4L2_PIX_FMT_YVYU,
0269 .name = "YVYU",
0270 .bits_per_sample = 8,
0271 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0272 .order = PXA_MBUS_ORDER_LE,
0273 .layout = PXA_MBUS_LAYOUT_PACKED,
0274 },
0275 }, {
0276 .code = MEDIA_BUS_FMT_UYVY8_2X8,
0277 .fmt = {
0278 .fourcc = V4L2_PIX_FMT_UYVY,
0279 .name = "UYVY",
0280 .bits_per_sample = 8,
0281 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0282 .order = PXA_MBUS_ORDER_LE,
0283 .layout = PXA_MBUS_LAYOUT_PACKED,
0284 },
0285 }, {
0286 .code = MEDIA_BUS_FMT_VYUY8_2X8,
0287 .fmt = {
0288 .fourcc = V4L2_PIX_FMT_VYUY,
0289 .name = "VYUY",
0290 .bits_per_sample = 8,
0291 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0292 .order = PXA_MBUS_ORDER_LE,
0293 .layout = PXA_MBUS_LAYOUT_PACKED,
0294 },
0295 }, {
0296 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
0297 .fmt = {
0298 .fourcc = V4L2_PIX_FMT_RGB555,
0299 .name = "RGB555",
0300 .bits_per_sample = 8,
0301 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0302 .order = PXA_MBUS_ORDER_LE,
0303 .layout = PXA_MBUS_LAYOUT_PACKED,
0304 },
0305 }, {
0306 .code = MEDIA_BUS_FMT_RGB555_2X8_PADHI_BE,
0307 .fmt = {
0308 .fourcc = V4L2_PIX_FMT_RGB555X,
0309 .name = "RGB555X",
0310 .bits_per_sample = 8,
0311 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0312 .order = PXA_MBUS_ORDER_BE,
0313 .layout = PXA_MBUS_LAYOUT_PACKED,
0314 },
0315 }, {
0316 .code = MEDIA_BUS_FMT_RGB565_2X8_LE,
0317 .fmt = {
0318 .fourcc = V4L2_PIX_FMT_RGB565,
0319 .name = "RGB565",
0320 .bits_per_sample = 8,
0321 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0322 .order = PXA_MBUS_ORDER_LE,
0323 .layout = PXA_MBUS_LAYOUT_PACKED,
0324 },
0325 }, {
0326 .code = MEDIA_BUS_FMT_RGB565_2X8_BE,
0327 .fmt = {
0328 .fourcc = V4L2_PIX_FMT_RGB565X,
0329 .name = "RGB565X",
0330 .bits_per_sample = 8,
0331 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0332 .order = PXA_MBUS_ORDER_BE,
0333 .layout = PXA_MBUS_LAYOUT_PACKED,
0334 },
0335 }, {
0336 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
0337 .fmt = {
0338 .fourcc = V4L2_PIX_FMT_SBGGR8,
0339 .name = "Bayer 8 BGGR",
0340 .bits_per_sample = 8,
0341 .packing = PXA_MBUS_PACKING_NONE,
0342 .order = PXA_MBUS_ORDER_LE,
0343 .layout = PXA_MBUS_LAYOUT_PACKED,
0344 },
0345 }, {
0346 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
0347 .fmt = {
0348 .fourcc = V4L2_PIX_FMT_SGBRG8,
0349 .name = "Bayer 8 GBRG",
0350 .bits_per_sample = 8,
0351 .packing = PXA_MBUS_PACKING_NONE,
0352 .order = PXA_MBUS_ORDER_LE,
0353 .layout = PXA_MBUS_LAYOUT_PACKED,
0354 },
0355 }, {
0356 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
0357 .fmt = {
0358 .fourcc = V4L2_PIX_FMT_SGRBG8,
0359 .name = "Bayer 8 GRBG",
0360 .bits_per_sample = 8,
0361 .packing = PXA_MBUS_PACKING_NONE,
0362 .order = PXA_MBUS_ORDER_LE,
0363 .layout = PXA_MBUS_LAYOUT_PACKED,
0364 },
0365 }, {
0366 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
0367 .fmt = {
0368 .fourcc = V4L2_PIX_FMT_SRGGB8,
0369 .name = "Bayer 8 RGGB",
0370 .bits_per_sample = 8,
0371 .packing = PXA_MBUS_PACKING_NONE,
0372 .order = PXA_MBUS_ORDER_LE,
0373 .layout = PXA_MBUS_LAYOUT_PACKED,
0374 },
0375 }, {
0376 .code = MEDIA_BUS_FMT_SBGGR10_1X10,
0377 .fmt = {
0378 .fourcc = V4L2_PIX_FMT_SBGGR10,
0379 .name = "Bayer 10 BGGR",
0380 .bits_per_sample = 10,
0381 .packing = PXA_MBUS_PACKING_EXTEND16,
0382 .order = PXA_MBUS_ORDER_LE,
0383 .layout = PXA_MBUS_LAYOUT_PACKED,
0384 },
0385 }, {
0386 .code = MEDIA_BUS_FMT_Y8_1X8,
0387 .fmt = {
0388 .fourcc = V4L2_PIX_FMT_GREY,
0389 .name = "Grey",
0390 .bits_per_sample = 8,
0391 .packing = PXA_MBUS_PACKING_NONE,
0392 .order = PXA_MBUS_ORDER_LE,
0393 .layout = PXA_MBUS_LAYOUT_PACKED,
0394 },
0395 }, {
0396 .code = MEDIA_BUS_FMT_Y10_1X10,
0397 .fmt = {
0398 .fourcc = V4L2_PIX_FMT_Y10,
0399 .name = "Grey 10bit",
0400 .bits_per_sample = 10,
0401 .packing = PXA_MBUS_PACKING_EXTEND16,
0402 .order = PXA_MBUS_ORDER_LE,
0403 .layout = PXA_MBUS_LAYOUT_PACKED,
0404 },
0405 }, {
0406 .code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE,
0407 .fmt = {
0408 .fourcc = V4L2_PIX_FMT_SBGGR10,
0409 .name = "Bayer 10 BGGR",
0410 .bits_per_sample = 8,
0411 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0412 .order = PXA_MBUS_ORDER_LE,
0413 .layout = PXA_MBUS_LAYOUT_PACKED,
0414 },
0415 }, {
0416 .code = MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE,
0417 .fmt = {
0418 .fourcc = V4L2_PIX_FMT_SBGGR10,
0419 .name = "Bayer 10 BGGR",
0420 .bits_per_sample = 8,
0421 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0422 .order = PXA_MBUS_ORDER_BE,
0423 .layout = PXA_MBUS_LAYOUT_PACKED,
0424 },
0425 }, {
0426 .code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE,
0427 .fmt = {
0428 .fourcc = V4L2_PIX_FMT_RGB444,
0429 .name = "RGB444",
0430 .bits_per_sample = 8,
0431 .packing = PXA_MBUS_PACKING_2X8_PADHI,
0432 .order = PXA_MBUS_ORDER_BE,
0433 .layout = PXA_MBUS_LAYOUT_PACKED,
0434 },
0435 }, {
0436 .code = MEDIA_BUS_FMT_UYVY8_1X16,
0437 .fmt = {
0438 .fourcc = V4L2_PIX_FMT_UYVY,
0439 .name = "UYVY 16bit",
0440 .bits_per_sample = 16,
0441 .packing = PXA_MBUS_PACKING_EXTEND16,
0442 .order = PXA_MBUS_ORDER_LE,
0443 .layout = PXA_MBUS_LAYOUT_PACKED,
0444 },
0445 }, {
0446 .code = MEDIA_BUS_FMT_VYUY8_1X16,
0447 .fmt = {
0448 .fourcc = V4L2_PIX_FMT_VYUY,
0449 .name = "VYUY 16bit",
0450 .bits_per_sample = 16,
0451 .packing = PXA_MBUS_PACKING_EXTEND16,
0452 .order = PXA_MBUS_ORDER_LE,
0453 .layout = PXA_MBUS_LAYOUT_PACKED,
0454 },
0455 }, {
0456 .code = MEDIA_BUS_FMT_YUYV8_1X16,
0457 .fmt = {
0458 .fourcc = V4L2_PIX_FMT_YUYV,
0459 .name = "YUYV 16bit",
0460 .bits_per_sample = 16,
0461 .packing = PXA_MBUS_PACKING_EXTEND16,
0462 .order = PXA_MBUS_ORDER_LE,
0463 .layout = PXA_MBUS_LAYOUT_PACKED,
0464 },
0465 }, {
0466 .code = MEDIA_BUS_FMT_YVYU8_1X16,
0467 .fmt = {
0468 .fourcc = V4L2_PIX_FMT_YVYU,
0469 .name = "YVYU 16bit",
0470 .bits_per_sample = 16,
0471 .packing = PXA_MBUS_PACKING_EXTEND16,
0472 .order = PXA_MBUS_ORDER_LE,
0473 .layout = PXA_MBUS_LAYOUT_PACKED,
0474 },
0475 }, {
0476 .code = MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8,
0477 .fmt = {
0478 .fourcc = V4L2_PIX_FMT_SGRBG10DPCM8,
0479 .name = "Bayer 10 BGGR DPCM 8",
0480 .bits_per_sample = 8,
0481 .packing = PXA_MBUS_PACKING_NONE,
0482 .order = PXA_MBUS_ORDER_LE,
0483 .layout = PXA_MBUS_LAYOUT_PACKED,
0484 },
0485 }, {
0486 .code = MEDIA_BUS_FMT_SGBRG10_1X10,
0487 .fmt = {
0488 .fourcc = V4L2_PIX_FMT_SGBRG10,
0489 .name = "Bayer 10 GBRG",
0490 .bits_per_sample = 10,
0491 .packing = PXA_MBUS_PACKING_EXTEND16,
0492 .order = PXA_MBUS_ORDER_LE,
0493 .layout = PXA_MBUS_LAYOUT_PACKED,
0494 },
0495 }, {
0496 .code = MEDIA_BUS_FMT_SGRBG10_1X10,
0497 .fmt = {
0498 .fourcc = V4L2_PIX_FMT_SGRBG10,
0499 .name = "Bayer 10 GRBG",
0500 .bits_per_sample = 10,
0501 .packing = PXA_MBUS_PACKING_EXTEND16,
0502 .order = PXA_MBUS_ORDER_LE,
0503 .layout = PXA_MBUS_LAYOUT_PACKED,
0504 },
0505 }, {
0506 .code = MEDIA_BUS_FMT_SRGGB10_1X10,
0507 .fmt = {
0508 .fourcc = V4L2_PIX_FMT_SRGGB10,
0509 .name = "Bayer 10 RGGB",
0510 .bits_per_sample = 10,
0511 .packing = PXA_MBUS_PACKING_EXTEND16,
0512 .order = PXA_MBUS_ORDER_LE,
0513 .layout = PXA_MBUS_LAYOUT_PACKED,
0514 },
0515 }, {
0516 .code = MEDIA_BUS_FMT_SBGGR12_1X12,
0517 .fmt = {
0518 .fourcc = V4L2_PIX_FMT_SBGGR12,
0519 .name = "Bayer 12 BGGR",
0520 .bits_per_sample = 12,
0521 .packing = PXA_MBUS_PACKING_EXTEND16,
0522 .order = PXA_MBUS_ORDER_LE,
0523 .layout = PXA_MBUS_LAYOUT_PACKED,
0524 },
0525 }, {
0526 .code = MEDIA_BUS_FMT_SGBRG12_1X12,
0527 .fmt = {
0528 .fourcc = V4L2_PIX_FMT_SGBRG12,
0529 .name = "Bayer 12 GBRG",
0530 .bits_per_sample = 12,
0531 .packing = PXA_MBUS_PACKING_EXTEND16,
0532 .order = PXA_MBUS_ORDER_LE,
0533 .layout = PXA_MBUS_LAYOUT_PACKED,
0534 },
0535 }, {
0536 .code = MEDIA_BUS_FMT_SGRBG12_1X12,
0537 .fmt = {
0538 .fourcc = V4L2_PIX_FMT_SGRBG12,
0539 .name = "Bayer 12 GRBG",
0540 .bits_per_sample = 12,
0541 .packing = PXA_MBUS_PACKING_EXTEND16,
0542 .order = PXA_MBUS_ORDER_LE,
0543 .layout = PXA_MBUS_LAYOUT_PACKED,
0544 },
0545 }, {
0546 .code = MEDIA_BUS_FMT_SRGGB12_1X12,
0547 .fmt = {
0548 .fourcc = V4L2_PIX_FMT_SRGGB12,
0549 .name = "Bayer 12 RGGB",
0550 .bits_per_sample = 12,
0551 .packing = PXA_MBUS_PACKING_EXTEND16,
0552 .order = PXA_MBUS_ORDER_LE,
0553 .layout = PXA_MBUS_LAYOUT_PACKED,
0554 },
0555 },
0556 };
0557
0558 static s32 pxa_mbus_bytes_per_line(u32 width, const struct pxa_mbus_pixelfmt *mf)
0559 {
0560 if (mf->layout != PXA_MBUS_LAYOUT_PACKED)
0561 return width * mf->bits_per_sample / 8;
0562
0563 switch (mf->packing) {
0564 case PXA_MBUS_PACKING_NONE:
0565 return width * mf->bits_per_sample / 8;
0566 case PXA_MBUS_PACKING_2X8_PADHI:
0567 case PXA_MBUS_PACKING_EXTEND16:
0568 return width * 2;
0569 }
0570 return -EINVAL;
0571 }
0572
0573 static s32 pxa_mbus_image_size(const struct pxa_mbus_pixelfmt *mf,
0574 u32 bytes_per_line, u32 height)
0575 {
0576 if (mf->layout == PXA_MBUS_LAYOUT_PACKED)
0577 return bytes_per_line * height;
0578
0579 switch (mf->packing) {
0580 case PXA_MBUS_PACKING_2X8_PADHI:
0581 return bytes_per_line * height * 2;
0582 default:
0583 return -EINVAL;
0584 }
0585 }
0586
0587 static const struct pxa_mbus_pixelfmt *pxa_mbus_find_fmtdesc(
0588 u32 code,
0589 const struct pxa_mbus_lookup *lookup,
0590 int n)
0591 {
0592 int i;
0593
0594 for (i = 0; i < n; i++)
0595 if (lookup[i].code == code)
0596 return &lookup[i].fmt;
0597
0598 return NULL;
0599 }
0600
0601 static const struct pxa_mbus_pixelfmt *pxa_mbus_get_fmtdesc(
0602 u32 code)
0603 {
0604 return pxa_mbus_find_fmtdesc(code, mbus_fmt, ARRAY_SIZE(mbus_fmt));
0605 }
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617 struct pxa_camera_format_xlate {
0618 u32 code;
0619 const struct pxa_mbus_pixelfmt *host_fmt;
0620 };
0621
0622
0623
0624
0625 enum pxa_camera_active_dma {
0626 DMA_Y = 0x1,
0627 DMA_U = 0x2,
0628 DMA_V = 0x4,
0629 };
0630
0631
0632 struct pxa_buffer {
0633
0634 struct vb2_v4l2_buffer vbuf;
0635 struct list_head queue;
0636 u32 code;
0637 int nb_planes;
0638
0639 struct dma_async_tx_descriptor *descs[3];
0640 dma_cookie_t cookie[3];
0641 struct scatterlist *sg[3];
0642 int sg_len[3];
0643 size_t plane_sizes[3];
0644 int inwork;
0645 enum pxa_camera_active_dma active_dma;
0646 };
0647
0648 struct pxa_camera_dev {
0649 struct v4l2_device v4l2_dev;
0650 struct video_device vdev;
0651 struct v4l2_async_notifier notifier;
0652 struct vb2_queue vb2_vq;
0653 struct v4l2_subdev *sensor;
0654 struct pxa_camera_format_xlate *user_formats;
0655 const struct pxa_camera_format_xlate *current_fmt;
0656 struct v4l2_pix_format current_pix;
0657
0658
0659
0660
0661
0662
0663 struct clk *clk;
0664
0665 unsigned int irq;
0666 void __iomem *base;
0667
0668 int channels;
0669 struct dma_chan *dma_chans[3];
0670
0671 struct pxacamera_platform_data *pdata;
0672 struct resource *res;
0673 unsigned long platform_flags;
0674 unsigned long ciclk;
0675 unsigned long mclk;
0676 u32 mclk_divisor;
0677 u16 width_flags;
0678
0679 struct list_head capture;
0680
0681 spinlock_t lock;
0682 struct mutex mlock;
0683 unsigned int buf_sequence;
0684
0685 struct pxa_buffer *active;
0686 struct tasklet_struct task_eof;
0687
0688 u32 save_cicr[5];
0689 };
0690
0691 struct pxa_cam {
0692 unsigned long flags;
0693 };
0694
0695 static const char *pxa_cam_driver_description = "PXA_Camera";
0696
0697
0698
0699
0700 static const struct pxa_camera_format_xlate
0701 *pxa_mbus_xlate_by_fourcc(struct pxa_camera_format_xlate *user_formats,
0702 unsigned int fourcc)
0703 {
0704 unsigned int i;
0705
0706 for (i = 0; user_formats[i].code; i++)
0707 if (user_formats[i].host_fmt->fourcc == fourcc)
0708 return user_formats + i;
0709 return NULL;
0710 }
0711
0712 static struct pxa_camera_format_xlate *pxa_mbus_build_fmts_xlate(
0713 struct v4l2_device *v4l2_dev, struct v4l2_subdev *subdev,
0714 int (*get_formats)(struct v4l2_device *, unsigned int,
0715 struct pxa_camera_format_xlate *xlate))
0716 {
0717 unsigned int i, fmts = 0, raw_fmts = 0;
0718 int ret;
0719 struct v4l2_subdev_mbus_code_enum code = {
0720 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
0721 };
0722 struct pxa_camera_format_xlate *user_formats;
0723
0724 while (!v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
0725 raw_fmts++;
0726 code.index++;
0727 }
0728
0729
0730
0731
0732
0733 for (i = 0; i < raw_fmts; i++) {
0734 ret = get_formats(v4l2_dev, i, NULL);
0735 if (ret < 0)
0736 return ERR_PTR(ret);
0737 fmts += ret;
0738 }
0739
0740 if (!fmts)
0741 return ERR_PTR(-ENXIO);
0742
0743 user_formats = kcalloc(fmts + 1, sizeof(*user_formats), GFP_KERNEL);
0744 if (!user_formats)
0745 return ERR_PTR(-ENOMEM);
0746
0747
0748 fmts = 0;
0749 for (i = 0; i < raw_fmts; i++) {
0750 ret = get_formats(v4l2_dev, i, user_formats + fmts);
0751 if (ret < 0)
0752 goto egfmt;
0753 fmts += ret;
0754 }
0755 user_formats[fmts].code = 0;
0756
0757 return user_formats;
0758 egfmt:
0759 kfree(user_formats);
0760 return ERR_PTR(ret);
0761 }
0762
0763
0764
0765
0766 static struct pxa_buffer *vb2_to_pxa_buffer(struct vb2_buffer *vb)
0767 {
0768 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0769
0770 return container_of(vbuf, struct pxa_buffer, vbuf);
0771 }
0772
0773 static struct device *pcdev_to_dev(struct pxa_camera_dev *pcdev)
0774 {
0775 return pcdev->v4l2_dev.dev;
0776 }
0777
0778 static struct pxa_camera_dev *v4l2_dev_to_pcdev(struct v4l2_device *v4l2_dev)
0779 {
0780 return container_of(v4l2_dev, struct pxa_camera_dev, v4l2_dev);
0781 }
0782
0783 static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
0784 enum pxa_camera_active_dma act_dma);
0785
0786 static void pxa_camera_dma_irq_y(void *data)
0787 {
0788 struct pxa_camera_dev *pcdev = data;
0789
0790 pxa_camera_dma_irq(pcdev, DMA_Y);
0791 }
0792
0793 static void pxa_camera_dma_irq_u(void *data)
0794 {
0795 struct pxa_camera_dev *pcdev = data;
0796
0797 pxa_camera_dma_irq(pcdev, DMA_U);
0798 }
0799
0800 static void pxa_camera_dma_irq_v(void *data)
0801 {
0802 struct pxa_camera_dev *pcdev = data;
0803
0804 pxa_camera_dma_irq(pcdev, DMA_V);
0805 }
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819 static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
0820 struct pxa_buffer *buf, int channel,
0821 struct scatterlist *sg, int sglen)
0822 {
0823 struct dma_chan *dma_chan = pcdev->dma_chans[channel];
0824 struct dma_async_tx_descriptor *tx;
0825
0826 tx = dmaengine_prep_slave_sg(dma_chan, sg, sglen, DMA_DEV_TO_MEM,
0827 DMA_PREP_INTERRUPT | DMA_CTRL_REUSE);
0828 if (!tx) {
0829 dev_err(pcdev_to_dev(pcdev),
0830 "dmaengine_prep_slave_sg failed\n");
0831 goto fail;
0832 }
0833
0834 tx->callback_param = pcdev;
0835 switch (channel) {
0836 case 0:
0837 tx->callback = pxa_camera_dma_irq_y;
0838 break;
0839 case 1:
0840 tx->callback = pxa_camera_dma_irq_u;
0841 break;
0842 case 2:
0843 tx->callback = pxa_camera_dma_irq_v;
0844 break;
0845 }
0846
0847 buf->descs[channel] = tx;
0848 return 0;
0849 fail:
0850 dev_dbg(pcdev_to_dev(pcdev),
0851 "%s (vb=%p) dma_tx=%p\n",
0852 __func__, buf, tx);
0853
0854 return -ENOMEM;
0855 }
0856
0857 static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
0858 struct pxa_buffer *buf)
0859 {
0860 buf->active_dma = DMA_Y;
0861 if (buf->nb_planes == 3)
0862 buf->active_dma |= DMA_U | DMA_V;
0863 }
0864
0865
0866
0867
0868
0869
0870
0871
0872 static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
0873 {
0874 int i;
0875
0876 for (i = 0; i < pcdev->channels; i++) {
0877 dev_dbg(pcdev_to_dev(pcdev),
0878 "%s (channel=%d)\n", __func__, i);
0879 dma_async_issue_pending(pcdev->dma_chans[i]);
0880 }
0881 }
0882
0883 static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
0884 {
0885 int i;
0886
0887 for (i = 0; i < pcdev->channels; i++) {
0888 dev_dbg(pcdev_to_dev(pcdev),
0889 "%s (channel=%d)\n", __func__, i);
0890 dmaengine_terminate_all(pcdev->dma_chans[i]);
0891 }
0892 }
0893
0894 static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
0895 struct pxa_buffer *buf)
0896 {
0897 int i;
0898
0899 for (i = 0; i < pcdev->channels; i++) {
0900 buf->cookie[i] = dmaengine_submit(buf->descs[i]);
0901 dev_dbg(pcdev_to_dev(pcdev),
0902 "%s (channel=%d) : submit vb=%p cookie=%d\n",
0903 __func__, i, buf, buf->descs[i]->cookie);
0904 }
0905 }
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915 static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
0916 {
0917 unsigned long cicr0;
0918
0919 dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
0920 __raw_writel(__raw_readl(pcdev->base + CISR), pcdev->base + CISR);
0921
0922 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
0923 cicr0 &= ~CICR0_EOFM;
0924 __raw_writel(cicr0, pcdev->base + CICR0);
0925 }
0926
0927 static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
0928 {
0929 unsigned long cicr0;
0930
0931 pxa_dma_stop_channels(pcdev);
0932
0933 cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
0934 __raw_writel(cicr0, pcdev->base + CICR0);
0935
0936 pcdev->active = NULL;
0937 dev_dbg(pcdev_to_dev(pcdev), "%s\n", __func__);
0938 }
0939
0940 static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
0941 struct pxa_buffer *buf,
0942 enum vb2_buffer_state state)
0943 {
0944 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
0945 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
0946
0947
0948 list_del_init(&buf->queue);
0949 vb->timestamp = ktime_get_ns();
0950 vbuf->sequence = pcdev->buf_sequence++;
0951 vbuf->field = V4L2_FIELD_NONE;
0952 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
0953 dev_dbg(pcdev_to_dev(pcdev), "%s dequeued buffer (buf=0x%p)\n",
0954 __func__, buf);
0955
0956 if (list_empty(&pcdev->capture)) {
0957 pxa_camera_stop_capture(pcdev);
0958 return;
0959 }
0960
0961 pcdev->active = list_entry(pcdev->capture.next,
0962 struct pxa_buffer, queue);
0963 }
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983 static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev,
0984 dma_cookie_t last_submitted,
0985 dma_cookie_t last_issued)
0986 {
0987 bool is_dma_stopped = last_submitted != last_issued;
0988
0989 dev_dbg(pcdev_to_dev(pcdev),
0990 "%s : top queued buffer=%p, is_dma_stopped=%d\n",
0991 __func__, pcdev->active, is_dma_stopped);
0992
0993 if (pcdev->active && is_dma_stopped)
0994 pxa_camera_start_capture(pcdev);
0995 }
0996
0997 static void pxa_camera_dma_irq(struct pxa_camera_dev *pcdev,
0998 enum pxa_camera_active_dma act_dma)
0999 {
1000 struct pxa_buffer *buf, *last_buf;
1001 unsigned long flags;
1002 u32 camera_status, overrun;
1003 int chan;
1004 enum dma_status last_status;
1005 dma_cookie_t last_issued;
1006
1007 spin_lock_irqsave(&pcdev->lock, flags);
1008
1009 camera_status = __raw_readl(pcdev->base + CISR);
1010 dev_dbg(pcdev_to_dev(pcdev), "camera dma irq, cisr=0x%x dma=%d\n",
1011 camera_status, act_dma);
1012 overrun = CISR_IFO_0;
1013 if (pcdev->channels == 3)
1014 overrun |= CISR_IFO_1 | CISR_IFO_2;
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 if (!pcdev->active)
1029 goto out;
1030
1031 buf = pcdev->active;
1032 WARN_ON(buf->inwork || list_empty(&buf->queue));
1033
1034
1035
1036
1037
1038 switch (act_dma) {
1039 case DMA_U:
1040 chan = 1;
1041 break;
1042 case DMA_V:
1043 chan = 2;
1044 break;
1045 default:
1046 chan = 0;
1047 break;
1048 }
1049 last_buf = list_entry(pcdev->capture.prev,
1050 struct pxa_buffer, queue);
1051 last_status = dma_async_is_tx_complete(pcdev->dma_chans[chan],
1052 last_buf->cookie[chan],
1053 NULL, &last_issued);
1054 if (camera_status & overrun &&
1055 last_status != DMA_COMPLETE) {
1056 dev_dbg(pcdev_to_dev(pcdev), "FIFO overrun! CISR: %x\n",
1057 camera_status);
1058 pxa_camera_stop_capture(pcdev);
1059 list_for_each_entry(buf, &pcdev->capture, queue)
1060 pxa_dma_add_tail_buf(pcdev, buf);
1061 pxa_camera_start_capture(pcdev);
1062 goto out;
1063 }
1064 buf->active_dma &= ~act_dma;
1065 if (!buf->active_dma) {
1066 pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_DONE);
1067 pxa_camera_check_link_miss(pcdev, last_buf->cookie[chan],
1068 last_issued);
1069 }
1070
1071 out:
1072 spin_unlock_irqrestore(&pcdev->lock, flags);
1073 }
1074
1075 static u32 mclk_get_divisor(struct platform_device *pdev,
1076 struct pxa_camera_dev *pcdev)
1077 {
1078 unsigned long mclk = pcdev->mclk;
1079 u32 div;
1080 unsigned long lcdclk;
1081
1082 lcdclk = clk_get_rate(pcdev->clk);
1083 pcdev->ciclk = lcdclk;
1084
1085
1086 if (mclk > lcdclk / 4) {
1087 mclk = lcdclk / 4;
1088 dev_warn(&pdev->dev,
1089 "Limiting master clock to %lu\n", mclk);
1090 }
1091
1092
1093 div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
1094
1095
1096 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1097 pcdev->mclk = lcdclk / (2 * (div + 1));
1098
1099 dev_dbg(&pdev->dev, "LCD clock %luHz, target freq %luHz, divisor %u\n",
1100 lcdclk, mclk, div);
1101
1102 return div;
1103 }
1104
1105 static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
1106 unsigned long pclk)
1107 {
1108
1109 u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
1110
1111 __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
1112 }
1113
1114 static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
1115 {
1116 u32 cicr4 = 0;
1117
1118
1119 __raw_writel(0x3ff, pcdev->base + CICR0);
1120
1121 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1122 cicr4 |= CICR4_PCLK_EN;
1123 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1124 cicr4 |= CICR4_MCLK_EN;
1125 if (pcdev->platform_flags & PXA_CAMERA_PCP)
1126 cicr4 |= CICR4_PCP;
1127 if (pcdev->platform_flags & PXA_CAMERA_HSP)
1128 cicr4 |= CICR4_HSP;
1129 if (pcdev->platform_flags & PXA_CAMERA_VSP)
1130 cicr4 |= CICR4_VSP;
1131
1132 __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
1133
1134 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1135
1136 recalculate_fifo_timeout(pcdev, pcdev->mclk);
1137 else
1138
1139 recalculate_fifo_timeout(pcdev, 13000000);
1140
1141 clk_prepare_enable(pcdev->clk);
1142 }
1143
1144 static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
1145 {
1146 clk_disable_unprepare(pcdev->clk);
1147 }
1148
1149 static void pxa_camera_eof(struct tasklet_struct *t)
1150 {
1151 struct pxa_camera_dev *pcdev = from_tasklet(pcdev, t, task_eof);
1152 unsigned long cifr;
1153 struct pxa_buffer *buf;
1154
1155 dev_dbg(pcdev_to_dev(pcdev),
1156 "Camera interrupt status 0x%x\n",
1157 __raw_readl(pcdev->base + CISR));
1158
1159
1160 cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
1161 __raw_writel(cifr, pcdev->base + CIFR);
1162
1163 pcdev->active = list_first_entry(&pcdev->capture,
1164 struct pxa_buffer, queue);
1165 buf = pcdev->active;
1166 pxa_videobuf_set_actdma(pcdev, buf);
1167
1168 pxa_dma_start_channels(pcdev);
1169 }
1170
1171 static irqreturn_t pxa_camera_irq(int irq, void *data)
1172 {
1173 struct pxa_camera_dev *pcdev = data;
1174 unsigned long status, cicr0;
1175
1176 status = __raw_readl(pcdev->base + CISR);
1177 dev_dbg(pcdev_to_dev(pcdev),
1178 "Camera interrupt status 0x%lx\n", status);
1179
1180 if (!status)
1181 return IRQ_NONE;
1182
1183 __raw_writel(status, pcdev->base + CISR);
1184
1185 if (status & CISR_EOF) {
1186 cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
1187 __raw_writel(cicr0, pcdev->base + CICR0);
1188 tasklet_schedule(&pcdev->task_eof);
1189 }
1190
1191 return IRQ_HANDLED;
1192 }
1193
1194 static void pxa_camera_setup_cicr(struct pxa_camera_dev *pcdev,
1195 unsigned long flags, __u32 pixfmt)
1196 {
1197 unsigned long dw, bpp;
1198 u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
1199 int ret = sensor_call(pcdev, sensor, g_skip_top_lines, &y_skip_top);
1200
1201 if (ret < 0)
1202 y_skip_top = 0;
1203
1204
1205
1206
1207
1208 switch (pcdev->current_fmt->host_fmt->bits_per_sample) {
1209 case 10:
1210 dw = 4;
1211 bpp = 0x40;
1212 break;
1213 case 9:
1214 dw = 3;
1215 bpp = 0x20;
1216 break;
1217 default:
1218
1219
1220
1221
1222 case 8:
1223 dw = 2;
1224 bpp = 0;
1225 }
1226
1227 if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
1228 cicr4 |= CICR4_PCLK_EN;
1229 if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
1230 cicr4 |= CICR4_MCLK_EN;
1231 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
1232 cicr4 |= CICR4_PCP;
1233 if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
1234 cicr4 |= CICR4_HSP;
1235 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1236 cicr4 |= CICR4_VSP;
1237
1238 cicr0 = __raw_readl(pcdev->base + CICR0);
1239 if (cicr0 & CICR0_ENB)
1240 __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
1241
1242 cicr1 = CICR1_PPL_VAL(pcdev->current_pix.width - 1) | bpp | dw;
1243
1244 switch (pixfmt) {
1245 case V4L2_PIX_FMT_YUV422P:
1246 pcdev->channels = 3;
1247 cicr1 |= CICR1_YCBCR_F;
1248
1249
1250
1251
1252
1253
1254
1255 fallthrough;
1256 case V4L2_PIX_FMT_UYVY:
1257 case V4L2_PIX_FMT_VYUY:
1258 case V4L2_PIX_FMT_YUYV:
1259 case V4L2_PIX_FMT_YVYU:
1260 cicr1 |= CICR1_COLOR_SP_VAL(2);
1261 break;
1262 case V4L2_PIX_FMT_RGB555:
1263 cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
1264 CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
1265 break;
1266 case V4L2_PIX_FMT_RGB565:
1267 cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
1268 break;
1269 }
1270
1271 cicr2 = 0;
1272 cicr3 = CICR3_LPF_VAL(pcdev->current_pix.height - 1) |
1273 CICR3_BFW_VAL(min((u32)255, y_skip_top));
1274 cicr4 |= pcdev->mclk_divisor;
1275
1276 __raw_writel(cicr1, pcdev->base + CICR1);
1277 __raw_writel(cicr2, pcdev->base + CICR2);
1278 __raw_writel(cicr3, pcdev->base + CICR3);
1279 __raw_writel(cicr4, pcdev->base + CICR4);
1280
1281
1282 cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
1283 CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
1284 cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
1285 __raw_writel(cicr0, pcdev->base + CICR0);
1286 }
1287
1288
1289
1290
1291 static void pxa_buffer_cleanup(struct pxa_buffer *buf)
1292 {
1293 int i;
1294
1295 for (i = 0; i < 3 && buf->descs[i]; i++) {
1296 dmaengine_desc_free(buf->descs[i]);
1297 kfree(buf->sg[i]);
1298 buf->descs[i] = NULL;
1299 buf->sg[i] = NULL;
1300 buf->sg_len[i] = 0;
1301 buf->plane_sizes[i] = 0;
1302 }
1303 buf->nb_planes = 0;
1304 }
1305
1306 static int pxa_buffer_init(struct pxa_camera_dev *pcdev,
1307 struct pxa_buffer *buf)
1308 {
1309 struct vb2_buffer *vb = &buf->vbuf.vb2_buf;
1310 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
1311 int nb_channels = pcdev->channels;
1312 int i, ret = 0;
1313 unsigned long size = vb2_plane_size(vb, 0);
1314
1315 switch (nb_channels) {
1316 case 1:
1317 buf->plane_sizes[0] = size;
1318 break;
1319 case 3:
1320 buf->plane_sizes[0] = size / 2;
1321 buf->plane_sizes[1] = size / 4;
1322 buf->plane_sizes[2] = size / 4;
1323 break;
1324 default:
1325 return -EINVAL;
1326 }
1327 buf->nb_planes = nb_channels;
1328
1329 ret = sg_split(sgt->sgl, sgt->nents, 0, nb_channels,
1330 buf->plane_sizes, buf->sg, buf->sg_len, GFP_KERNEL);
1331 if (ret < 0) {
1332 dev_err(pcdev_to_dev(pcdev),
1333 "sg_split failed: %d\n", ret);
1334 return ret;
1335 }
1336 for (i = 0; i < nb_channels; i++) {
1337 ret = pxa_init_dma_channel(pcdev, buf, i,
1338 buf->sg[i], buf->sg_len[i]);
1339 if (ret) {
1340 pxa_buffer_cleanup(buf);
1341 return ret;
1342 }
1343 }
1344 INIT_LIST_HEAD(&buf->queue);
1345
1346 return ret;
1347 }
1348
1349 static void pxac_vb2_cleanup(struct vb2_buffer *vb)
1350 {
1351 struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
1352 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
1353
1354 dev_dbg(pcdev_to_dev(pcdev),
1355 "%s(vb=%p)\n", __func__, vb);
1356 pxa_buffer_cleanup(buf);
1357 }
1358
1359 static void pxac_vb2_queue(struct vb2_buffer *vb)
1360 {
1361 struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
1362 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
1363
1364 dev_dbg(pcdev_to_dev(pcdev),
1365 "%s(vb=%p) nb_channels=%d size=%lu active=%p\n",
1366 __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0),
1367 pcdev->active);
1368
1369 list_add_tail(&buf->queue, &pcdev->capture);
1370
1371 pxa_dma_add_tail_buf(pcdev, buf);
1372 }
1373
1374
1375
1376
1377
1378
1379
1380 static int pxac_vb2_prepare(struct vb2_buffer *vb)
1381 {
1382 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
1383 struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
1384 int ret = 0;
1385 #ifdef DEBUG
1386 int i;
1387 #endif
1388
1389 switch (pcdev->channels) {
1390 case 1:
1391 case 3:
1392 vb2_set_plane_payload(vb, 0, pcdev->current_pix.sizeimage);
1393 break;
1394 default:
1395 return -EINVAL;
1396 }
1397
1398 dev_dbg(pcdev_to_dev(pcdev),
1399 "%s (vb=%p) nb_channels=%d size=%lu\n",
1400 __func__, vb, pcdev->channels, vb2_get_plane_payload(vb, 0));
1401
1402 WARN_ON(!pcdev->current_fmt);
1403
1404 #ifdef DEBUG
1405
1406
1407
1408
1409 for (i = 0; i < vb->num_planes; i++)
1410 memset((void *)vb2_plane_vaddr(vb, i),
1411 0xaa, vb2_get_plane_payload(vb, i));
1412 #endif
1413
1414
1415
1416
1417
1418 buf->inwork = 0;
1419 pxa_videobuf_set_actdma(pcdev, buf);
1420
1421 return ret;
1422 }
1423
1424 static int pxac_vb2_init(struct vb2_buffer *vb)
1425 {
1426 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vb->vb2_queue);
1427 struct pxa_buffer *buf = vb2_to_pxa_buffer(vb);
1428
1429 dev_dbg(pcdev_to_dev(pcdev),
1430 "%s(nb_channels=%d)\n",
1431 __func__, pcdev->channels);
1432
1433 return pxa_buffer_init(pcdev, buf);
1434 }
1435
1436 static int pxac_vb2_queue_setup(struct vb2_queue *vq,
1437 unsigned int *nbufs,
1438 unsigned int *num_planes, unsigned int sizes[],
1439 struct device *alloc_devs[])
1440 {
1441 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
1442 int size = pcdev->current_pix.sizeimage;
1443
1444 dev_dbg(pcdev_to_dev(pcdev),
1445 "%s(vq=%p nbufs=%d num_planes=%d size=%d)\n",
1446 __func__, vq, *nbufs, *num_planes, size);
1447
1448
1449
1450
1451
1452 if (*num_planes)
1453 return sizes[0] < size ? -EINVAL : 0;
1454
1455 *num_planes = 1;
1456 switch (pcdev->channels) {
1457 case 1:
1458 case 3:
1459 sizes[0] = size;
1460 break;
1461 default:
1462 return -EINVAL;
1463 }
1464
1465 if (!*nbufs)
1466 *nbufs = 1;
1467
1468 return 0;
1469 }
1470
1471 static int pxac_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
1472 {
1473 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
1474
1475 dev_dbg(pcdev_to_dev(pcdev), "%s(count=%d) active=%p\n",
1476 __func__, count, pcdev->active);
1477
1478 pcdev->buf_sequence = 0;
1479 if (!pcdev->active)
1480 pxa_camera_start_capture(pcdev);
1481
1482 return 0;
1483 }
1484
1485 static void pxac_vb2_stop_streaming(struct vb2_queue *vq)
1486 {
1487 struct pxa_camera_dev *pcdev = vb2_get_drv_priv(vq);
1488 struct pxa_buffer *buf, *tmp;
1489
1490 dev_dbg(pcdev_to_dev(pcdev), "%s active=%p\n",
1491 __func__, pcdev->active);
1492 pxa_camera_stop_capture(pcdev);
1493
1494 list_for_each_entry_safe(buf, tmp, &pcdev->capture, queue)
1495 pxa_camera_wakeup(pcdev, buf, VB2_BUF_STATE_ERROR);
1496 }
1497
1498 static const struct vb2_ops pxac_vb2_ops = {
1499 .queue_setup = pxac_vb2_queue_setup,
1500 .buf_init = pxac_vb2_init,
1501 .buf_prepare = pxac_vb2_prepare,
1502 .buf_queue = pxac_vb2_queue,
1503 .buf_cleanup = pxac_vb2_cleanup,
1504 .start_streaming = pxac_vb2_start_streaming,
1505 .stop_streaming = pxac_vb2_stop_streaming,
1506 .wait_prepare = vb2_ops_wait_prepare,
1507 .wait_finish = vb2_ops_wait_finish,
1508 };
1509
1510 static int pxa_camera_init_videobuf2(struct pxa_camera_dev *pcdev)
1511 {
1512 int ret;
1513 struct vb2_queue *vq = &pcdev->vb2_vq;
1514
1515 memset(vq, 0, sizeof(*vq));
1516 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1517 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1518 vq->drv_priv = pcdev;
1519 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1520 vq->buf_struct_size = sizeof(struct pxa_buffer);
1521 vq->dev = pcdev->v4l2_dev.dev;
1522
1523 vq->ops = &pxac_vb2_ops;
1524 vq->mem_ops = &vb2_dma_sg_memops;
1525 vq->lock = &pcdev->mlock;
1526
1527 ret = vb2_queue_init(vq);
1528 dev_dbg(pcdev_to_dev(pcdev),
1529 "vb2_queue_init(vq=%p): %d\n", vq, ret);
1530
1531 return ret;
1532 }
1533
1534
1535
1536
1537 static int pxa_camera_set_bus_param(struct pxa_camera_dev *pcdev)
1538 {
1539 unsigned int bus_width = pcdev->current_fmt->host_fmt->bits_per_sample;
1540 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
1541 u32 pixfmt = pcdev->current_fmt->host_fmt->fourcc;
1542 int mbus_config;
1543 int ret;
1544
1545 if (!((1 << (bus_width - 1)) & pcdev->width_flags)) {
1546 dev_err(pcdev_to_dev(pcdev), "Unsupported bus width %u",
1547 bus_width);
1548 return -EINVAL;
1549 }
1550
1551 pcdev->channels = 1;
1552
1553
1554 mbus_config = 0;
1555 if (pcdev->platform_flags & PXA_CAMERA_MASTER)
1556 mbus_config |= V4L2_MBUS_MASTER;
1557 else
1558 mbus_config |= V4L2_MBUS_SLAVE;
1559
1560 if (pcdev->platform_flags & PXA_CAMERA_HSP)
1561 mbus_config |= V4L2_MBUS_HSYNC_ACTIVE_HIGH;
1562 else
1563 mbus_config |= V4L2_MBUS_HSYNC_ACTIVE_LOW;
1564
1565 if (pcdev->platform_flags & PXA_CAMERA_VSP)
1566 mbus_config |= V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1567 else
1568 mbus_config |= V4L2_MBUS_VSYNC_ACTIVE_LOW;
1569
1570 if (pcdev->platform_flags & PXA_CAMERA_PCP)
1571 mbus_config |= V4L2_MBUS_PCLK_SAMPLE_RISING;
1572 else
1573 mbus_config |= V4L2_MBUS_PCLK_SAMPLE_FALLING;
1574 mbus_config |= V4L2_MBUS_DATA_ACTIVE_HIGH;
1575
1576 ret = sensor_call(pcdev, pad, get_mbus_config, 0, &cfg);
1577 if (ret < 0 && ret != -ENOIOCTLCMD) {
1578 dev_err(pcdev_to_dev(pcdev),
1579 "Failed to call get_mbus_config: %d\n", ret);
1580 return ret;
1581 }
1582
1583
1584
1585
1586
1587
1588
1589
1590 if (cfg.bus.parallel.flags != mbus_config) {
1591 unsigned int pxa_mbus_role = mbus_config & (V4L2_MBUS_MASTER |
1592 V4L2_MBUS_SLAVE);
1593 unsigned int flags = cfg.bus.parallel.flags;
1594
1595 if (pxa_mbus_role != (flags & (V4L2_MBUS_MASTER |
1596 V4L2_MBUS_SLAVE))) {
1597 dev_err(pcdev_to_dev(pcdev),
1598 "Unsupported mbus configuration: bus mastering\n");
1599 return -EINVAL;
1600 }
1601
1602 if (flags & V4L2_MBUS_DATA_ACTIVE_LOW) {
1603 dev_err(pcdev_to_dev(pcdev),
1604 "Unsupported mbus configuration: DATA_ACTIVE_LOW\n");
1605 return -EINVAL;
1606 }
1607 }
1608
1609 pxa_camera_setup_cicr(pcdev, cfg.bus.parallel.flags, pixfmt);
1610
1611 return 0;
1612 }
1613
1614 static const struct pxa_mbus_pixelfmt pxa_camera_formats[] = {
1615 {
1616 .fourcc = V4L2_PIX_FMT_YUV422P,
1617 .name = "Planar YUV422 16 bit",
1618 .bits_per_sample = 8,
1619 .packing = PXA_MBUS_PACKING_2X8_PADHI,
1620 .order = PXA_MBUS_ORDER_LE,
1621 .layout = PXA_MBUS_LAYOUT_PLANAR_2Y_U_V,
1622 },
1623 };
1624
1625
1626 static bool pxa_camera_packing_supported(const struct pxa_mbus_pixelfmt *fmt)
1627 {
1628 return fmt->packing == PXA_MBUS_PACKING_NONE ||
1629 (fmt->bits_per_sample == 8 &&
1630 fmt->packing == PXA_MBUS_PACKING_2X8_PADHI) ||
1631 (fmt->bits_per_sample > 8 &&
1632 fmt->packing == PXA_MBUS_PACKING_EXTEND16);
1633 }
1634
1635 static int pxa_camera_get_formats(struct v4l2_device *v4l2_dev,
1636 unsigned int idx,
1637 struct pxa_camera_format_xlate *xlate)
1638 {
1639 struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
1640 int formats = 0, ret;
1641 struct v4l2_subdev_mbus_code_enum code = {
1642 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1643 .index = idx,
1644 };
1645 const struct pxa_mbus_pixelfmt *fmt;
1646
1647 ret = sensor_call(pcdev, pad, enum_mbus_code, NULL, &code);
1648 if (ret < 0)
1649
1650 return 0;
1651
1652 fmt = pxa_mbus_get_fmtdesc(code.code);
1653 if (!fmt) {
1654 dev_err(pcdev_to_dev(pcdev),
1655 "Invalid format code #%u: %d\n", idx, code.code);
1656 return 0;
1657 }
1658
1659 switch (code.code) {
1660 case MEDIA_BUS_FMT_UYVY8_2X8:
1661 formats++;
1662 if (xlate) {
1663 xlate->host_fmt = &pxa_camera_formats[0];
1664 xlate->code = code.code;
1665 xlate++;
1666 dev_dbg(pcdev_to_dev(pcdev),
1667 "Providing format %s using code %d\n",
1668 pxa_camera_formats[0].name, code.code);
1669 }
1670 fallthrough;
1671 case MEDIA_BUS_FMT_VYUY8_2X8:
1672 case MEDIA_BUS_FMT_YUYV8_2X8:
1673 case MEDIA_BUS_FMT_YVYU8_2X8:
1674 case MEDIA_BUS_FMT_RGB565_2X8_LE:
1675 case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
1676 if (xlate)
1677 dev_dbg(pcdev_to_dev(pcdev),
1678 "Providing format %s packed\n",
1679 fmt->name);
1680 break;
1681 default:
1682 if (!pxa_camera_packing_supported(fmt))
1683 return 0;
1684 if (xlate)
1685 dev_dbg(pcdev_to_dev(pcdev),
1686 "Providing format %s in pass-through mode\n",
1687 fmt->name);
1688 break;
1689 }
1690
1691
1692 formats++;
1693 if (xlate) {
1694 xlate->host_fmt = fmt;
1695 xlate->code = code.code;
1696 xlate++;
1697 }
1698
1699 return formats;
1700 }
1701
1702 static int pxa_camera_build_formats(struct pxa_camera_dev *pcdev)
1703 {
1704 struct pxa_camera_format_xlate *xlate;
1705
1706 xlate = pxa_mbus_build_fmts_xlate(&pcdev->v4l2_dev, pcdev->sensor,
1707 pxa_camera_get_formats);
1708 if (IS_ERR(xlate))
1709 return PTR_ERR(xlate);
1710
1711 pcdev->user_formats = xlate;
1712 return 0;
1713 }
1714
1715 static void pxa_camera_destroy_formats(struct pxa_camera_dev *pcdev)
1716 {
1717 kfree(pcdev->user_formats);
1718 }
1719
1720 static int pxa_camera_check_frame(u32 width, u32 height)
1721 {
1722
1723 return height < 32 || height > 2048 || width < 48 || width > 2048 ||
1724 (width & 0x01);
1725 }
1726
1727 #ifdef CONFIG_VIDEO_ADV_DEBUG
1728 static int pxac_vidioc_g_register(struct file *file, void *priv,
1729 struct v4l2_dbg_register *reg)
1730 {
1731 struct pxa_camera_dev *pcdev = video_drvdata(file);
1732
1733 if (reg->reg > CIBR2)
1734 return -ERANGE;
1735
1736 reg->val = __raw_readl(pcdev->base + reg->reg);
1737 reg->size = sizeof(__u32);
1738 return 0;
1739 }
1740
1741 static int pxac_vidioc_s_register(struct file *file, void *priv,
1742 const struct v4l2_dbg_register *reg)
1743 {
1744 struct pxa_camera_dev *pcdev = video_drvdata(file);
1745
1746 if (reg->reg > CIBR2)
1747 return -ERANGE;
1748 if (reg->size != sizeof(__u32))
1749 return -EINVAL;
1750 __raw_writel(reg->val, pcdev->base + reg->reg);
1751 return 0;
1752 }
1753 #endif
1754
1755 static int pxac_vidioc_enum_fmt_vid_cap(struct file *filp, void *priv,
1756 struct v4l2_fmtdesc *f)
1757 {
1758 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1759 const struct pxa_mbus_pixelfmt *format;
1760 unsigned int idx;
1761
1762 for (idx = 0; pcdev->user_formats[idx].code; idx++);
1763 if (f->index >= idx)
1764 return -EINVAL;
1765
1766 format = pcdev->user_formats[f->index].host_fmt;
1767 f->pixelformat = format->fourcc;
1768 return 0;
1769 }
1770
1771 static int pxac_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1772 struct v4l2_format *f)
1773 {
1774 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1775 struct v4l2_pix_format *pix = &f->fmt.pix;
1776
1777 pix->width = pcdev->current_pix.width;
1778 pix->height = pcdev->current_pix.height;
1779 pix->bytesperline = pcdev->current_pix.bytesperline;
1780 pix->sizeimage = pcdev->current_pix.sizeimage;
1781 pix->field = pcdev->current_pix.field;
1782 pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
1783 pix->colorspace = pcdev->current_pix.colorspace;
1784 dev_dbg(pcdev_to_dev(pcdev), "current_fmt->fourcc: 0x%08x\n",
1785 pcdev->current_fmt->host_fmt->fourcc);
1786 return 0;
1787 }
1788
1789 static int pxac_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1790 struct v4l2_format *f)
1791 {
1792 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1793 const struct pxa_camera_format_xlate *xlate;
1794 struct v4l2_pix_format *pix = &f->fmt.pix;
1795 struct v4l2_subdev_pad_config pad_cfg;
1796 struct v4l2_subdev_state pad_state = {
1797 .pads = &pad_cfg
1798 };
1799 struct v4l2_subdev_format format = {
1800 .which = V4L2_SUBDEV_FORMAT_TRY,
1801 };
1802 struct v4l2_mbus_framefmt *mf = &format.format;
1803 __u32 pixfmt = pix->pixelformat;
1804 int ret;
1805
1806 xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats, pixfmt);
1807 if (!xlate) {
1808 dev_warn(pcdev_to_dev(pcdev), "Format %x not found\n", pixfmt);
1809 return -EINVAL;
1810 }
1811
1812
1813
1814
1815
1816
1817
1818 v4l_bound_align_image(&pix->width, 48, 2048, 1,
1819 &pix->height, 32, 2048, 0,
1820 pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
1821
1822 v4l2_fill_mbus_format(mf, pix, xlate->code);
1823 ret = sensor_call(pcdev, pad, set_fmt, &pad_state, &format);
1824 if (ret < 0)
1825 return ret;
1826
1827 v4l2_fill_pix_format(pix, mf);
1828
1829
1830 switch (mf->field) {
1831 case V4L2_FIELD_ANY:
1832 case V4L2_FIELD_NONE:
1833 pix->field = V4L2_FIELD_NONE;
1834 break;
1835 default:
1836
1837 dev_err(pcdev_to_dev(pcdev), "Field type %d unsupported.\n",
1838 mf->field);
1839 return -EINVAL;
1840 }
1841
1842 ret = pxa_mbus_bytes_per_line(pix->width, xlate->host_fmt);
1843 if (ret < 0)
1844 return ret;
1845
1846 pix->bytesperline = ret;
1847 ret = pxa_mbus_image_size(xlate->host_fmt, pix->bytesperline,
1848 pix->height);
1849 if (ret < 0)
1850 return ret;
1851
1852 pix->sizeimage = ret;
1853 return 0;
1854 }
1855
1856 static int pxac_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1857 struct v4l2_format *f)
1858 {
1859 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1860 const struct pxa_camera_format_xlate *xlate;
1861 struct v4l2_pix_format *pix = &f->fmt.pix;
1862 struct v4l2_subdev_format format = {
1863 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1864 };
1865 unsigned long flags;
1866 int ret, is_busy;
1867
1868 dev_dbg(pcdev_to_dev(pcdev),
1869 "s_fmt_vid_cap(pix=%dx%d:%x)\n",
1870 pix->width, pix->height, pix->pixelformat);
1871
1872 spin_lock_irqsave(&pcdev->lock, flags);
1873 is_busy = pcdev->active || vb2_is_busy(&pcdev->vb2_vq);
1874 spin_unlock_irqrestore(&pcdev->lock, flags);
1875
1876 if (is_busy)
1877 return -EBUSY;
1878
1879 ret = pxac_vidioc_try_fmt_vid_cap(filp, priv, f);
1880 if (ret)
1881 return ret;
1882
1883 xlate = pxa_mbus_xlate_by_fourcc(pcdev->user_formats,
1884 pix->pixelformat);
1885 v4l2_fill_mbus_format(&format.format, pix, xlate->code);
1886 ret = sensor_call(pcdev, pad, set_fmt, NULL, &format);
1887 if (ret < 0) {
1888 dev_warn(pcdev_to_dev(pcdev),
1889 "Failed to configure for format %x\n",
1890 pix->pixelformat);
1891 } else if (pxa_camera_check_frame(pix->width, pix->height)) {
1892 dev_warn(pcdev_to_dev(pcdev),
1893 "Camera driver produced an unsupported frame %dx%d\n",
1894 pix->width, pix->height);
1895 return -EINVAL;
1896 }
1897
1898 pcdev->current_fmt = xlate;
1899 pcdev->current_pix = *pix;
1900
1901 ret = pxa_camera_set_bus_param(pcdev);
1902 return ret;
1903 }
1904
1905 static int pxac_vidioc_querycap(struct file *file, void *priv,
1906 struct v4l2_capability *cap)
1907 {
1908 strscpy(cap->bus_info, "platform:pxa-camera", sizeof(cap->bus_info));
1909 strscpy(cap->driver, PXA_CAM_DRV_NAME, sizeof(cap->driver));
1910 strscpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
1911 return 0;
1912 }
1913
1914 static int pxac_vidioc_enum_input(struct file *file, void *priv,
1915 struct v4l2_input *i)
1916 {
1917 if (i->index > 0)
1918 return -EINVAL;
1919
1920 i->type = V4L2_INPUT_TYPE_CAMERA;
1921 strscpy(i->name, "Camera", sizeof(i->name));
1922
1923 return 0;
1924 }
1925
1926 static int pxac_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1927 {
1928 *i = 0;
1929
1930 return 0;
1931 }
1932
1933 static int pxac_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1934 {
1935 if (i > 0)
1936 return -EINVAL;
1937
1938 return 0;
1939 }
1940
1941 static int pxac_sensor_set_power(struct pxa_camera_dev *pcdev, int on)
1942 {
1943 int ret;
1944
1945 ret = sensor_call(pcdev, core, s_power, on);
1946 if (ret == -ENOIOCTLCMD)
1947 ret = 0;
1948 if (ret) {
1949 dev_warn(pcdev_to_dev(pcdev),
1950 "Failed to put subdevice in %s mode: %d\n",
1951 on ? "normal operation" : "power saving", ret);
1952 }
1953
1954 return ret;
1955 }
1956
1957 static int pxac_fops_camera_open(struct file *filp)
1958 {
1959 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1960 int ret;
1961
1962 mutex_lock(&pcdev->mlock);
1963 ret = v4l2_fh_open(filp);
1964 if (ret < 0)
1965 goto out;
1966
1967 if (!v4l2_fh_is_singular_file(filp))
1968 goto out;
1969
1970 ret = pxac_sensor_set_power(pcdev, 1);
1971 if (ret)
1972 v4l2_fh_release(filp);
1973 out:
1974 mutex_unlock(&pcdev->mlock);
1975 return ret;
1976 }
1977
1978 static int pxac_fops_camera_release(struct file *filp)
1979 {
1980 struct pxa_camera_dev *pcdev = video_drvdata(filp);
1981 int ret;
1982 bool fh_singular;
1983
1984 mutex_lock(&pcdev->mlock);
1985
1986 fh_singular = v4l2_fh_is_singular_file(filp);
1987
1988 ret = _vb2_fop_release(filp, NULL);
1989
1990 if (fh_singular)
1991 ret = pxac_sensor_set_power(pcdev, 0);
1992
1993 mutex_unlock(&pcdev->mlock);
1994
1995 return ret;
1996 }
1997
1998 static const struct v4l2_file_operations pxa_camera_fops = {
1999 .owner = THIS_MODULE,
2000 .open = pxac_fops_camera_open,
2001 .release = pxac_fops_camera_release,
2002 .read = vb2_fop_read,
2003 .poll = vb2_fop_poll,
2004 .mmap = vb2_fop_mmap,
2005 .unlocked_ioctl = video_ioctl2,
2006 };
2007
2008 static const struct v4l2_ioctl_ops pxa_camera_ioctl_ops = {
2009 .vidioc_querycap = pxac_vidioc_querycap,
2010
2011 .vidioc_enum_input = pxac_vidioc_enum_input,
2012 .vidioc_g_input = pxac_vidioc_g_input,
2013 .vidioc_s_input = pxac_vidioc_s_input,
2014
2015 .vidioc_enum_fmt_vid_cap = pxac_vidioc_enum_fmt_vid_cap,
2016 .vidioc_g_fmt_vid_cap = pxac_vidioc_g_fmt_vid_cap,
2017 .vidioc_s_fmt_vid_cap = pxac_vidioc_s_fmt_vid_cap,
2018 .vidioc_try_fmt_vid_cap = pxac_vidioc_try_fmt_vid_cap,
2019
2020 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2021 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2022 .vidioc_querybuf = vb2_ioctl_querybuf,
2023 .vidioc_qbuf = vb2_ioctl_qbuf,
2024 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2025 .vidioc_expbuf = vb2_ioctl_expbuf,
2026 .vidioc_streamon = vb2_ioctl_streamon,
2027 .vidioc_streamoff = vb2_ioctl_streamoff,
2028 #ifdef CONFIG_VIDEO_ADV_DEBUG
2029 .vidioc_g_register = pxac_vidioc_g_register,
2030 .vidioc_s_register = pxac_vidioc_s_register,
2031 #endif
2032 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2033 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2034 };
2035
2036 static const struct video_device pxa_camera_videodev_template = {
2037 .name = "pxa-camera",
2038 .minor = -1,
2039 .fops = &pxa_camera_fops,
2040 .ioctl_ops = &pxa_camera_ioctl_ops,
2041 .release = video_device_release_empty,
2042 .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING,
2043 };
2044
2045 static int pxa_camera_sensor_bound(struct v4l2_async_notifier *notifier,
2046 struct v4l2_subdev *subdev,
2047 struct v4l2_async_subdev *asd)
2048 {
2049 int err;
2050 struct v4l2_device *v4l2_dev = notifier->v4l2_dev;
2051 struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(v4l2_dev);
2052 struct video_device *vdev = &pcdev->vdev;
2053 struct v4l2_pix_format *pix = &pcdev->current_pix;
2054 struct v4l2_subdev_format format = {
2055 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
2056 };
2057 struct v4l2_mbus_framefmt *mf = &format.format;
2058
2059 dev_info(pcdev_to_dev(pcdev), "%s(): trying to bind a device\n",
2060 __func__);
2061 mutex_lock(&pcdev->mlock);
2062 *vdev = pxa_camera_videodev_template;
2063 vdev->v4l2_dev = v4l2_dev;
2064 vdev->lock = &pcdev->mlock;
2065 pcdev->sensor = subdev;
2066 pcdev->vdev.queue = &pcdev->vb2_vq;
2067 pcdev->vdev.v4l2_dev = &pcdev->v4l2_dev;
2068 pcdev->vdev.ctrl_handler = subdev->ctrl_handler;
2069 video_set_drvdata(&pcdev->vdev, pcdev);
2070
2071 err = pxa_camera_build_formats(pcdev);
2072 if (err) {
2073 dev_err(pcdev_to_dev(pcdev), "building formats failed: %d\n",
2074 err);
2075 goto out;
2076 }
2077
2078 pcdev->current_fmt = pcdev->user_formats;
2079 pix->field = V4L2_FIELD_NONE;
2080 pix->width = DEFAULT_WIDTH;
2081 pix->height = DEFAULT_HEIGHT;
2082 pix->bytesperline =
2083 pxa_mbus_bytes_per_line(pix->width,
2084 pcdev->current_fmt->host_fmt);
2085 pix->sizeimage =
2086 pxa_mbus_image_size(pcdev->current_fmt->host_fmt,
2087 pix->bytesperline, pix->height);
2088 pix->pixelformat = pcdev->current_fmt->host_fmt->fourcc;
2089 v4l2_fill_mbus_format(mf, pix, pcdev->current_fmt->code);
2090
2091 err = pxac_sensor_set_power(pcdev, 1);
2092 if (err)
2093 goto out;
2094
2095 err = sensor_call(pcdev, pad, set_fmt, NULL, &format);
2096 if (err)
2097 goto out_sensor_poweroff;
2098
2099 v4l2_fill_pix_format(pix, mf);
2100 pr_info("%s(): colorspace=0x%x pixfmt=0x%x\n",
2101 __func__, pix->colorspace, pix->pixelformat);
2102
2103 err = pxa_camera_init_videobuf2(pcdev);
2104 if (err)
2105 goto out_sensor_poweroff;
2106
2107 err = video_register_device(&pcdev->vdev, VFL_TYPE_VIDEO, -1);
2108 if (err) {
2109 v4l2_err(v4l2_dev, "register video device failed: %d\n", err);
2110 pcdev->sensor = NULL;
2111 } else {
2112 dev_info(pcdev_to_dev(pcdev),
2113 "PXA Camera driver attached to camera %s\n",
2114 subdev->name);
2115 }
2116
2117 out_sensor_poweroff:
2118 err = pxac_sensor_set_power(pcdev, 0);
2119 out:
2120 mutex_unlock(&pcdev->mlock);
2121 return err;
2122 }
2123
2124 static void pxa_camera_sensor_unbind(struct v4l2_async_notifier *notifier,
2125 struct v4l2_subdev *subdev,
2126 struct v4l2_async_subdev *asd)
2127 {
2128 struct pxa_camera_dev *pcdev = v4l2_dev_to_pcdev(notifier->v4l2_dev);
2129
2130 mutex_lock(&pcdev->mlock);
2131 dev_info(pcdev_to_dev(pcdev),
2132 "PXA Camera driver detached from camera %s\n",
2133 subdev->name);
2134
2135
2136 __raw_writel(0x3ff, pcdev->base + CICR0);
2137
2138
2139 pxa_dma_stop_channels(pcdev);
2140
2141 pxa_camera_destroy_formats(pcdev);
2142
2143 video_unregister_device(&pcdev->vdev);
2144 pcdev->sensor = NULL;
2145
2146 mutex_unlock(&pcdev->mlock);
2147 }
2148
2149 static const struct v4l2_async_notifier_operations pxa_camera_sensor_ops = {
2150 .bound = pxa_camera_sensor_bound,
2151 .unbind = pxa_camera_sensor_unbind,
2152 };
2153
2154
2155
2156
2157 static int pxa_camera_suspend(struct device *dev)
2158 {
2159 struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
2160 int i = 0, ret = 0;
2161
2162 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
2163 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
2164 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
2165 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
2166 pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
2167
2168 if (pcdev->sensor)
2169 ret = pxac_sensor_set_power(pcdev, 0);
2170
2171 return ret;
2172 }
2173
2174 static int pxa_camera_resume(struct device *dev)
2175 {
2176 struct pxa_camera_dev *pcdev = dev_get_drvdata(dev);
2177 int i = 0, ret = 0;
2178
2179 __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
2180 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
2181 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
2182 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
2183 __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
2184
2185 if (pcdev->sensor) {
2186 ret = pxac_sensor_set_power(pcdev, 1);
2187 }
2188
2189
2190 if (!ret && pcdev->active)
2191 pxa_camera_start_capture(pcdev);
2192
2193 return ret;
2194 }
2195
2196 static int pxa_camera_pdata_from_dt(struct device *dev,
2197 struct pxa_camera_dev *pcdev)
2198 {
2199 u32 mclk_rate;
2200 struct v4l2_async_subdev *asd;
2201 struct device_node *np = dev->of_node;
2202 struct v4l2_fwnode_endpoint ep = { .bus_type = 0 };
2203 int err = of_property_read_u32(np, "clock-frequency",
2204 &mclk_rate);
2205 if (!err) {
2206 pcdev->platform_flags |= PXA_CAMERA_MCLK_EN;
2207 pcdev->mclk = mclk_rate;
2208 }
2209
2210 np = of_graph_get_next_endpoint(np, NULL);
2211 if (!np) {
2212 dev_err(dev, "could not find endpoint\n");
2213 return -EINVAL;
2214 }
2215
2216 err = v4l2_fwnode_endpoint_parse(of_fwnode_handle(np), &ep);
2217 if (err) {
2218 dev_err(dev, "could not parse endpoint\n");
2219 goto out;
2220 }
2221
2222 switch (ep.bus.parallel.bus_width) {
2223 case 4:
2224 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_4;
2225 break;
2226 case 5:
2227 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_5;
2228 break;
2229 case 8:
2230 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_8;
2231 break;
2232 case 9:
2233 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_9;
2234 break;
2235 case 10:
2236 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
2237 break;
2238 default:
2239 break;
2240 }
2241
2242 if (ep.bus.parallel.flags & V4L2_MBUS_MASTER)
2243 pcdev->platform_flags |= PXA_CAMERA_MASTER;
2244 if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
2245 pcdev->platform_flags |= PXA_CAMERA_HSP;
2246 if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
2247 pcdev->platform_flags |= PXA_CAMERA_VSP;
2248 if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
2249 pcdev->platform_flags |= PXA_CAMERA_PCLK_EN | PXA_CAMERA_PCP;
2250 if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
2251 pcdev->platform_flags |= PXA_CAMERA_PCLK_EN;
2252
2253 asd = v4l2_async_nf_add_fwnode_remote(&pcdev->notifier,
2254 of_fwnode_handle(np),
2255 struct v4l2_async_subdev);
2256 if (IS_ERR(asd))
2257 err = PTR_ERR(asd);
2258 out:
2259 of_node_put(np);
2260
2261 return err;
2262 }
2263
2264 static int pxa_camera_probe(struct platform_device *pdev)
2265 {
2266 struct pxa_camera_dev *pcdev;
2267 struct resource *res;
2268 void __iomem *base;
2269 struct dma_slave_config config = {
2270 .src_addr_width = 0,
2271 .src_maxburst = 8,
2272 .direction = DMA_DEV_TO_MEM,
2273 };
2274 int irq;
2275 int err = 0, i;
2276
2277 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2278 irq = platform_get_irq(pdev, 0);
2279 if (!res || irq < 0)
2280 return -ENODEV;
2281
2282 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
2283 if (!pcdev) {
2284 dev_err(&pdev->dev, "Could not allocate pcdev\n");
2285 return -ENOMEM;
2286 }
2287
2288 pcdev->clk = devm_clk_get(&pdev->dev, NULL);
2289 if (IS_ERR(pcdev->clk))
2290 return PTR_ERR(pcdev->clk);
2291
2292 v4l2_async_nf_init(&pcdev->notifier);
2293 pcdev->res = res;
2294 pcdev->pdata = pdev->dev.platform_data;
2295 if (pcdev->pdata) {
2296 struct v4l2_async_subdev *asd;
2297
2298 pcdev->platform_flags = pcdev->pdata->flags;
2299 pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
2300 asd = v4l2_async_nf_add_i2c(&pcdev->notifier,
2301 pcdev->pdata->sensor_i2c_adapter_id,
2302 pcdev->pdata->sensor_i2c_address,
2303 struct v4l2_async_subdev);
2304 if (IS_ERR(asd))
2305 err = PTR_ERR(asd);
2306 } else if (pdev->dev.of_node) {
2307 err = pxa_camera_pdata_from_dt(&pdev->dev, pcdev);
2308 } else {
2309 return -ENODEV;
2310 }
2311 if (err < 0)
2312 return err;
2313
2314 if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
2315 PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
2316
2317
2318
2319
2320 dev_warn(&pdev->dev, "WARNING! Platform hasn't set available data widths, using default 10 bit\n");
2321 pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
2322 }
2323 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)
2324 pcdev->width_flags = 1 << 7;
2325 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)
2326 pcdev->width_flags |= 1 << 8;
2327 if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)
2328 pcdev->width_flags |= 1 << 9;
2329 if (!pcdev->mclk) {
2330 dev_warn(&pdev->dev,
2331 "mclk == 0! Please, fix your platform data. Using default 20MHz\n");
2332 pcdev->mclk = 20000000;
2333 }
2334
2335 pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
2336
2337 INIT_LIST_HEAD(&pcdev->capture);
2338 spin_lock_init(&pcdev->lock);
2339 mutex_init(&pcdev->mlock);
2340
2341
2342
2343
2344 base = devm_ioremap_resource(&pdev->dev, res);
2345 if (IS_ERR(base))
2346 return PTR_ERR(base);
2347
2348 pcdev->irq = irq;
2349 pcdev->base = base;
2350
2351
2352 pcdev->dma_chans[0] = dma_request_chan(&pdev->dev, "CI_Y");
2353 if (IS_ERR(pcdev->dma_chans[0])) {
2354 dev_err(&pdev->dev, "Can't request DMA for Y\n");
2355 return PTR_ERR(pcdev->dma_chans[0]);
2356 }
2357
2358 pcdev->dma_chans[1] = dma_request_chan(&pdev->dev, "CI_U");
2359 if (IS_ERR(pcdev->dma_chans[1])) {
2360 dev_err(&pdev->dev, "Can't request DMA for U\n");
2361 err = PTR_ERR(pcdev->dma_chans[1]);
2362 goto exit_free_dma_y;
2363 }
2364
2365 pcdev->dma_chans[2] = dma_request_chan(&pdev->dev, "CI_V");
2366 if (IS_ERR(pcdev->dma_chans[2])) {
2367 dev_err(&pdev->dev, "Can't request DMA for V\n");
2368 err = PTR_ERR(pcdev->dma_chans[2]);
2369 goto exit_free_dma_u;
2370 }
2371
2372 for (i = 0; i < 3; i++) {
2373 config.src_addr = pcdev->res->start + CIBR0 + i * 8;
2374 err = dmaengine_slave_config(pcdev->dma_chans[i], &config);
2375 if (err < 0) {
2376 dev_err(&pdev->dev, "dma slave config failed: %d\n",
2377 err);
2378 goto exit_free_dma;
2379 }
2380 }
2381
2382
2383 err = devm_request_irq(&pdev->dev, pcdev->irq, pxa_camera_irq, 0,
2384 PXA_CAM_DRV_NAME, pcdev);
2385 if (err) {
2386 dev_err(&pdev->dev, "Camera interrupt register failed\n");
2387 goto exit_free_dma;
2388 }
2389
2390 tasklet_setup(&pcdev->task_eof, pxa_camera_eof);
2391
2392 pxa_camera_activate(pcdev);
2393
2394 platform_set_drvdata(pdev, pcdev);
2395 err = v4l2_device_register(&pdev->dev, &pcdev->v4l2_dev);
2396 if (err)
2397 goto exit_deactivate;
2398
2399 err = pxa_camera_init_videobuf2(pcdev);
2400 if (err)
2401 goto exit_notifier_cleanup;
2402
2403 pcdev->notifier.ops = &pxa_camera_sensor_ops;
2404 err = v4l2_async_nf_register(&pcdev->v4l2_dev, &pcdev->notifier);
2405 if (err)
2406 goto exit_notifier_cleanup;
2407
2408 return 0;
2409 exit_notifier_cleanup:
2410 v4l2_async_nf_cleanup(&pcdev->notifier);
2411 v4l2_device_unregister(&pcdev->v4l2_dev);
2412 exit_deactivate:
2413 pxa_camera_deactivate(pcdev);
2414 tasklet_kill(&pcdev->task_eof);
2415 exit_free_dma:
2416 dma_release_channel(pcdev->dma_chans[2]);
2417 exit_free_dma_u:
2418 dma_release_channel(pcdev->dma_chans[1]);
2419 exit_free_dma_y:
2420 dma_release_channel(pcdev->dma_chans[0]);
2421 return err;
2422 }
2423
2424 static int pxa_camera_remove(struct platform_device *pdev)
2425 {
2426 struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
2427
2428 pxa_camera_deactivate(pcdev);
2429 tasklet_kill(&pcdev->task_eof);
2430 dma_release_channel(pcdev->dma_chans[0]);
2431 dma_release_channel(pcdev->dma_chans[1]);
2432 dma_release_channel(pcdev->dma_chans[2]);
2433
2434 v4l2_async_nf_unregister(&pcdev->notifier);
2435 v4l2_async_nf_cleanup(&pcdev->notifier);
2436
2437 v4l2_device_unregister(&pcdev->v4l2_dev);
2438
2439 dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
2440
2441 return 0;
2442 }
2443
2444 static const struct dev_pm_ops pxa_camera_pm = {
2445 .suspend = pxa_camera_suspend,
2446 .resume = pxa_camera_resume,
2447 };
2448
2449 static const struct of_device_id pxa_camera_of_match[] = {
2450 { .compatible = "marvell,pxa270-qci", },
2451 {},
2452 };
2453 MODULE_DEVICE_TABLE(of, pxa_camera_of_match);
2454
2455 static struct platform_driver pxa_camera_driver = {
2456 .driver = {
2457 .name = PXA_CAM_DRV_NAME,
2458 .pm = &pxa_camera_pm,
2459 .of_match_table = of_match_ptr(pxa_camera_of_match),
2460 },
2461 .probe = pxa_camera_probe,
2462 .remove = pxa_camera_remove,
2463 };
2464
2465 module_platform_driver(pxa_camera_driver);
2466
2467 MODULE_DESCRIPTION("PXA27x Camera Driver");
2468 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
2469 MODULE_LICENSE("GPL");
2470 MODULE_VERSION(PXA_CAM_VERSION);
2471 MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);