1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BCM283x / BCM271x Unicam Capture Driver 4 * 5 * Copyright (C) 2017-2020 - Raspberry Pi (Trading) Ltd. 6 * Copyright (C) 2024 - Ideas on Board 7 * 8 * Dave Stevenson <dave.stevenson@raspberrypi.com> 9 * 10 * Based on TI am437x driver by 11 * Benoit Parrot <bparrot@ti.com> 12 * Lad, Prabhakar <prabhakar.csengg@gmail.com> 13 * 14 * and TI CAL camera interface driver by 15 * Benoit Parrot <bparrot@ti.com> 16 * 17 * This driver directly controls the Unicam peripheral - there is no 18 * involvement with the VideoCore firmware. Unicam receives CSI-2 or CCP2 data 19 * and writes it into SDRAM. The only potential processing options are to 20 * repack Bayer data into an alternate format, and applying windowing. The 21 * repacking does not shift the data, so can repack V4L2_PIX_FMT_Sxxxx10P to 22 * V4L2_PIX_FMT_Sxxxx10, or V4L2_PIX_FMT_Sxxxx12P to V4L2_PIX_FMT_Sxxxx12, but 23 * not generically up to V4L2_PIX_FMT_Sxxxx16. Support for windowing may be 24 * added later. 25 * 26 * It should be possible to connect this driver to any sensor with a suitable 27 * output interface and V4L2 subdevice driver. 28 */ 29 30 #include <linux/clk.h> 31 #include <linux/delay.h> 32 #include <linux/device.h> 33 #include <linux/dma-mapping.h> 34 #include <linux/err.h> 35 #include <linux/interrupt.h> 36 #include <linux/io.h> 37 #include <linux/module.h> 38 #include <linux/of.h> 39 #include <linux/of_device.h> 40 #include <linux/platform_device.h> 41 #include <linux/pm_runtime.h> 42 #include <linux/slab.h> 43 #include <linux/videodev2.h> 44 45 #include <media/mipi-csi2.h> 46 #include <media/v4l2-async.h> 47 #include <media/v4l2-common.h> 48 #include <media/v4l2-dev.h> 49 #include <media/v4l2-device.h> 50 #include <media/v4l2-event.h> 51 #include <media/v4l2-ioctl.h> 52 #include <media/v4l2-fwnode.h> 53 #include <media/v4l2-mc.h> 54 #include <media/v4l2-subdev.h> 55 #include <media/videobuf2-dma-contig.h> 56 57 #include "bcm2835-unicam-regs.h" 58 59 #define UNICAM_MODULE_NAME "unicam" 60 61 /* 62 * Unicam must request a minimum of 250Mhz from the VPU clock. 63 * Otherwise the input FIFOs overrun and cause image corruption. 64 */ 65 #define UNICAM_MIN_VPU_CLOCK_RATE (250 * 1000 * 1000) 66 67 /* Unicam has an internal DMA alignment constraint of 16 bytes for each line. */ 68 #define UNICAM_DMA_BPL_ALIGNMENT 16 69 70 /* 71 * The image stride is stored in a 16 bit register, and needs to be aligned to 72 * the DMA constraint. As the ISP in the same SoC has a 32 bytes alignment 73 * constraint on its input, set the image stride alignment to 32 bytes here as 74 * well to avoid incompatible configurations. 75 */ 76 #define UNICAM_IMAGE_BPL_ALIGNMENT 32 77 #define UNICAM_IMAGE_MAX_BPL ((1U << 16) - UNICAM_IMAGE_BPL_ALIGNMENT) 78 79 /* 80 * Max width is therefore determined by the max stride divided by the number of 81 * bits per pixel. Take 32bpp as a worst case. No imposed limit on the height, 82 * so adopt a square image for want of anything better. 83 */ 84 #define UNICAM_IMAGE_MIN_WIDTH 16 85 #define UNICAM_IMAGE_MIN_HEIGHT 16 86 #define UNICAM_IMAGE_MAX_WIDTH (UNICAM_IMAGE_MAX_BPL / 4) 87 #define UNICAM_IMAGE_MAX_HEIGHT UNICAM_IMAGE_MAX_WIDTH 88 89 /* 90 * There's no intrinsic limits on the width and height for embedded data. Use 91 * the same maximum values as for the image, to avoid overflows in the image 92 * size computation. 93 */ 94 #define UNICAM_META_MIN_WIDTH 1 95 #define UNICAM_META_MIN_HEIGHT 1 96 #define UNICAM_META_MAX_WIDTH UNICAM_IMAGE_MAX_WIDTH 97 #define UNICAM_META_MAX_HEIGHT UNICAM_IMAGE_MAX_HEIGHT 98 99 /* 100 * Size of the dummy buffer. Can be any size really, but the DMA 101 * allocation works in units of page sizes. 102 */ 103 #define UNICAM_DUMMY_BUF_SIZE PAGE_SIZE 104 105 enum unicam_pad { 106 UNICAM_SD_PAD_SINK, 107 UNICAM_SD_PAD_SOURCE_IMAGE, 108 UNICAM_SD_PAD_SOURCE_METADATA, 109 UNICAM_SD_NUM_PADS 110 }; 111 112 enum unicam_node_type { 113 UNICAM_IMAGE_NODE, 114 UNICAM_METADATA_NODE, 115 UNICAM_MAX_NODES 116 }; 117 118 /* 119 * struct unicam_format_info - Unicam media bus format information 120 * @fourcc: V4L2 pixel format FCC identifier. 0 if n/a. 121 * @unpacked_fourcc: V4L2 pixel format FCC identifier if the data is expanded 122 * out to 16bpp. 0 if n/a. 123 * @code: V4L2 media bus format code. 124 * @depth: Bits per pixel as delivered from the source. 125 * @csi_dt: CSI data type. 126 * @unpack: PUM value when unpacking to @unpacked_fourcc 127 */ 128 struct unicam_format_info { 129 u32 fourcc; 130 u32 unpacked_fourcc; 131 u32 code; 132 u8 depth; 133 u8 csi_dt; 134 u8 unpack; 135 }; 136 137 struct unicam_buffer { 138 struct vb2_v4l2_buffer vb; 139 struct list_head list; 140 dma_addr_t dma_addr; 141 unsigned int size; 142 }; 143 144 static inline struct unicam_buffer *to_unicam_buffer(struct vb2_buffer *vb) 145 { 146 return container_of(vb, struct unicam_buffer, vb.vb2_buf); 147 } 148 149 struct unicam_node { 150 bool registered; 151 unsigned int id; 152 153 /* Pointer to the current v4l2_buffer */ 154 struct unicam_buffer *cur_frm; 155 /* Pointer to the next v4l2_buffer */ 156 struct unicam_buffer *next_frm; 157 /* Used to store current pixel format */ 158 struct v4l2_format fmt; 159 /* Buffer queue used in video-buf */ 160 struct vb2_queue buffer_queue; 161 /* Queue of filled frames */ 162 struct list_head dma_queue; 163 /* IRQ lock for DMA queue */ 164 spinlock_t dma_queue_lock; 165 /* Identifies video device for this channel */ 166 struct video_device video_dev; 167 /* Pointer to the parent handle */ 168 struct unicam_device *dev; 169 struct media_pad pad; 170 /* 171 * Dummy buffer intended to be used by unicam 172 * if we have no other queued buffers to swap to. 173 */ 174 struct unicam_buffer dummy_buf; 175 void *dummy_buf_cpu_addr; 176 }; 177 178 struct unicam_device { 179 struct kref kref; 180 181 /* peripheral base address */ 182 void __iomem *base; 183 /* clock gating base address */ 184 void __iomem *clk_gate_base; 185 /* lp clock handle */ 186 struct clk *clock; 187 /* vpu clock handle */ 188 struct clk *vpu_clock; 189 /* V4l2 device */ 190 struct v4l2_device v4l2_dev; 191 struct media_device mdev; 192 193 /* parent device */ 194 struct device *dev; 195 /* subdevice async notifier */ 196 struct v4l2_async_notifier notifier; 197 unsigned int sequence; 198 bool frame_started; 199 200 /* Sensor node */ 201 struct { 202 struct v4l2_subdev *subdev; 203 struct media_pad *pad; 204 } sensor; 205 206 /* Internal subdev */ 207 struct { 208 struct v4l2_subdev sd; 209 struct media_pad pads[UNICAM_SD_NUM_PADS]; 210 unsigned int enabled_streams; 211 } subdev; 212 213 enum v4l2_mbus_type bus_type; 214 /* 215 * Stores bus.mipi_csi2.flags for CSI2 sensors, or 216 * bus.mipi_csi1.strobe for CCP2. 217 */ 218 unsigned int bus_flags; 219 unsigned int max_data_lanes; 220 221 struct { 222 struct media_pipeline pipe; 223 unsigned int num_data_lanes; 224 unsigned int nodes; 225 } pipe; 226 227 /* Lock used for the video devices of both nodes */ 228 struct mutex lock; 229 struct unicam_node node[UNICAM_MAX_NODES]; 230 }; 231 232 static inline struct unicam_device * 233 notifier_to_unicam_device(struct v4l2_async_notifier *notifier) 234 { 235 return container_of(notifier, struct unicam_device, notifier); 236 } 237 238 static inline struct unicam_device * 239 sd_to_unicam_device(struct v4l2_subdev *sd) 240 { 241 return container_of(sd, struct unicam_device, subdev.sd); 242 } 243 244 static void unicam_release(struct kref *kref) 245 { 246 struct unicam_device *unicam = 247 container_of(kref, struct unicam_device, kref); 248 249 if (unicam->mdev.dev) 250 media_device_cleanup(&unicam->mdev); 251 252 mutex_destroy(&unicam->lock); 253 kfree(unicam); 254 } 255 256 static struct unicam_device *unicam_get(struct unicam_device *unicam) 257 { 258 kref_get(&unicam->kref); 259 260 return unicam; 261 } 262 263 static void unicam_put(struct unicam_device *unicam) 264 { 265 kref_put(&unicam->kref, unicam_release); 266 } 267 268 /* ----------------------------------------------------------------------------- 269 * Misc helper functions 270 */ 271 272 static inline bool unicam_sd_pad_is_source(u32 pad) 273 { 274 /* Camera RX has 1 sink pad, and N source pads */ 275 return pad != UNICAM_SD_PAD_SINK; 276 } 277 278 static inline bool is_metadata_node(struct unicam_node *node) 279 { 280 return node->video_dev.device_caps & V4L2_CAP_META_CAPTURE; 281 } 282 283 static inline bool is_image_node(struct unicam_node *node) 284 { 285 return node->video_dev.device_caps & V4L2_CAP_VIDEO_CAPTURE; 286 } 287 288 /* ----------------------------------------------------------------------------- 289 * Format data table and helper functions 290 */ 291 292 static const struct v4l2_mbus_framefmt unicam_default_image_format = { 293 .width = 640, 294 .height = 480, 295 .code = MEDIA_BUS_FMT_UYVY8_1X16, 296 .field = V4L2_FIELD_NONE, 297 .colorspace = V4L2_COLORSPACE_SRGB, 298 .ycbcr_enc = V4L2_YCBCR_ENC_601, 299 .quantization = V4L2_QUANTIZATION_LIM_RANGE, 300 .xfer_func = V4L2_XFER_FUNC_SRGB, 301 .flags = 0, 302 }; 303 304 static const struct v4l2_mbus_framefmt unicam_default_meta_format = { 305 .width = 640, 306 .height = 2, 307 .code = MEDIA_BUS_FMT_META_8, 308 .field = V4L2_FIELD_NONE, 309 }; 310 311 static const struct unicam_format_info unicam_image_formats[] = { 312 /* YUV Formats */ 313 { 314 .fourcc = V4L2_PIX_FMT_YUYV, 315 .code = MEDIA_BUS_FMT_YUYV8_1X16, 316 .depth = 16, 317 .csi_dt = MIPI_CSI2_DT_YUV422_8B, 318 }, { 319 .fourcc = V4L2_PIX_FMT_UYVY, 320 .code = MEDIA_BUS_FMT_UYVY8_1X16, 321 .depth = 16, 322 .csi_dt = MIPI_CSI2_DT_YUV422_8B, 323 }, { 324 .fourcc = V4L2_PIX_FMT_YVYU, 325 .code = MEDIA_BUS_FMT_YVYU8_1X16, 326 .depth = 16, 327 .csi_dt = MIPI_CSI2_DT_YUV422_8B, 328 }, { 329 .fourcc = V4L2_PIX_FMT_VYUY, 330 .code = MEDIA_BUS_FMT_VYUY8_1X16, 331 .depth = 16, 332 .csi_dt = MIPI_CSI2_DT_YUV422_8B, 333 }, { 334 /* RGB Formats */ 335 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */ 336 .code = MEDIA_BUS_FMT_RGB565_1X16, 337 .depth = 16, 338 .csi_dt = MIPI_CSI2_DT_RGB565, 339 }, { 340 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */ 341 .code = MEDIA_BUS_FMT_RGB888_1X24, 342 .depth = 24, 343 .csi_dt = MIPI_CSI2_DT_RGB888, 344 }, { 345 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */ 346 .code = MEDIA_BUS_FMT_BGR888_1X24, 347 .depth = 24, 348 .csi_dt = MIPI_CSI2_DT_RGB888, 349 }, { 350 /* Bayer Formats */ 351 .fourcc = V4L2_PIX_FMT_SBGGR8, 352 .code = MEDIA_BUS_FMT_SBGGR8_1X8, 353 .depth = 8, 354 .csi_dt = MIPI_CSI2_DT_RAW8, 355 }, { 356 .fourcc = V4L2_PIX_FMT_SGBRG8, 357 .code = MEDIA_BUS_FMT_SGBRG8_1X8, 358 .depth = 8, 359 .csi_dt = MIPI_CSI2_DT_RAW8, 360 }, { 361 .fourcc = V4L2_PIX_FMT_SGRBG8, 362 .code = MEDIA_BUS_FMT_SGRBG8_1X8, 363 .depth = 8, 364 .csi_dt = MIPI_CSI2_DT_RAW8, 365 }, { 366 .fourcc = V4L2_PIX_FMT_SRGGB8, 367 .code = MEDIA_BUS_FMT_SRGGB8_1X8, 368 .depth = 8, 369 .csi_dt = MIPI_CSI2_DT_RAW8, 370 }, { 371 .fourcc = V4L2_PIX_FMT_SBGGR10P, 372 .unpacked_fourcc = V4L2_PIX_FMT_SBGGR10, 373 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 374 .depth = 10, 375 .csi_dt = MIPI_CSI2_DT_RAW10, 376 .unpack = UNICAM_PUM_UNPACK10, 377 }, { 378 .fourcc = V4L2_PIX_FMT_SGBRG10P, 379 .unpacked_fourcc = V4L2_PIX_FMT_SGBRG10, 380 .code = MEDIA_BUS_FMT_SGBRG10_1X10, 381 .depth = 10, 382 .csi_dt = MIPI_CSI2_DT_RAW10, 383 .unpack = UNICAM_PUM_UNPACK10, 384 }, { 385 .fourcc = V4L2_PIX_FMT_SGRBG10P, 386 .unpacked_fourcc = V4L2_PIX_FMT_SGRBG10, 387 .code = MEDIA_BUS_FMT_SGRBG10_1X10, 388 .depth = 10, 389 .csi_dt = MIPI_CSI2_DT_RAW10, 390 .unpack = UNICAM_PUM_UNPACK10, 391 }, { 392 .fourcc = V4L2_PIX_FMT_SRGGB10P, 393 .unpacked_fourcc = V4L2_PIX_FMT_SRGGB10, 394 .code = MEDIA_BUS_FMT_SRGGB10_1X10, 395 .depth = 10, 396 .csi_dt = MIPI_CSI2_DT_RAW10, 397 .unpack = UNICAM_PUM_UNPACK10, 398 }, { 399 .fourcc = V4L2_PIX_FMT_SBGGR12P, 400 .unpacked_fourcc = V4L2_PIX_FMT_SBGGR12, 401 .code = MEDIA_BUS_FMT_SBGGR12_1X12, 402 .depth = 12, 403 .csi_dt = MIPI_CSI2_DT_RAW12, 404 .unpack = UNICAM_PUM_UNPACK12, 405 }, { 406 .fourcc = V4L2_PIX_FMT_SGBRG12P, 407 .unpacked_fourcc = V4L2_PIX_FMT_SGBRG12, 408 .code = MEDIA_BUS_FMT_SGBRG12_1X12, 409 .depth = 12, 410 .csi_dt = MIPI_CSI2_DT_RAW12, 411 .unpack = UNICAM_PUM_UNPACK12, 412 }, { 413 .fourcc = V4L2_PIX_FMT_SGRBG12P, 414 .unpacked_fourcc = V4L2_PIX_FMT_SGRBG12, 415 .code = MEDIA_BUS_FMT_SGRBG12_1X12, 416 .depth = 12, 417 .csi_dt = MIPI_CSI2_DT_RAW12, 418 .unpack = UNICAM_PUM_UNPACK12, 419 }, { 420 .fourcc = V4L2_PIX_FMT_SRGGB12P, 421 .unpacked_fourcc = V4L2_PIX_FMT_SRGGB12, 422 .code = MEDIA_BUS_FMT_SRGGB12_1X12, 423 .depth = 12, 424 .csi_dt = MIPI_CSI2_DT_RAW12, 425 .unpack = UNICAM_PUM_UNPACK12, 426 }, { 427 .fourcc = V4L2_PIX_FMT_SBGGR14P, 428 .unpacked_fourcc = V4L2_PIX_FMT_SBGGR14, 429 .code = MEDIA_BUS_FMT_SBGGR14_1X14, 430 .depth = 14, 431 .csi_dt = MIPI_CSI2_DT_RAW14, 432 .unpack = UNICAM_PUM_UNPACK14, 433 }, { 434 .fourcc = V4L2_PIX_FMT_SGBRG14P, 435 .unpacked_fourcc = V4L2_PIX_FMT_SGBRG14, 436 .code = MEDIA_BUS_FMT_SGBRG14_1X14, 437 .depth = 14, 438 .csi_dt = MIPI_CSI2_DT_RAW14, 439 .unpack = UNICAM_PUM_UNPACK14, 440 }, { 441 .fourcc = V4L2_PIX_FMT_SGRBG14P, 442 .unpacked_fourcc = V4L2_PIX_FMT_SGRBG14, 443 .code = MEDIA_BUS_FMT_SGRBG14_1X14, 444 .depth = 14, 445 .csi_dt = MIPI_CSI2_DT_RAW14, 446 .unpack = UNICAM_PUM_UNPACK14, 447 }, { 448 .fourcc = V4L2_PIX_FMT_SRGGB14P, 449 .unpacked_fourcc = V4L2_PIX_FMT_SRGGB14, 450 .code = MEDIA_BUS_FMT_SRGGB14_1X14, 451 .depth = 14, 452 .csi_dt = MIPI_CSI2_DT_RAW14, 453 .unpack = UNICAM_PUM_UNPACK14, 454 }, { 455 /* 16 bit Bayer formats could be supported. */ 456 457 /* Greyscale formats */ 458 .fourcc = V4L2_PIX_FMT_GREY, 459 .code = MEDIA_BUS_FMT_Y8_1X8, 460 .depth = 8, 461 .csi_dt = MIPI_CSI2_DT_RAW8, 462 }, { 463 .fourcc = V4L2_PIX_FMT_Y10P, 464 .unpacked_fourcc = V4L2_PIX_FMT_Y10, 465 .code = MEDIA_BUS_FMT_Y10_1X10, 466 .depth = 10, 467 .csi_dt = MIPI_CSI2_DT_RAW10, 468 .unpack = UNICAM_PUM_UNPACK10, 469 }, { 470 .fourcc = V4L2_PIX_FMT_Y12P, 471 .unpacked_fourcc = V4L2_PIX_FMT_Y12, 472 .code = MEDIA_BUS_FMT_Y12_1X12, 473 .depth = 12, 474 .csi_dt = MIPI_CSI2_DT_RAW12, 475 .unpack = UNICAM_PUM_UNPACK12, 476 }, { 477 .fourcc = V4L2_PIX_FMT_Y14P, 478 .unpacked_fourcc = V4L2_PIX_FMT_Y14, 479 .code = MEDIA_BUS_FMT_Y14_1X14, 480 .depth = 14, 481 .csi_dt = MIPI_CSI2_DT_RAW14, 482 .unpack = UNICAM_PUM_UNPACK14, 483 }, 484 }; 485 486 static const struct unicam_format_info unicam_meta_formats[] = { 487 { 488 .fourcc = V4L2_META_FMT_GENERIC_8, 489 .code = MEDIA_BUS_FMT_META_8, 490 .depth = 8, 491 }, { 492 .fourcc = V4L2_META_FMT_GENERIC_CSI2_10, 493 .code = MEDIA_BUS_FMT_META_10, 494 .depth = 10, 495 }, { 496 .fourcc = V4L2_META_FMT_GENERIC_CSI2_12, 497 .code = MEDIA_BUS_FMT_META_12, 498 .depth = 12, 499 }, { 500 .fourcc = V4L2_META_FMT_GENERIC_CSI2_14, 501 .code = MEDIA_BUS_FMT_META_14, 502 .depth = 14, 503 }, 504 }; 505 506 /* Format setup functions */ 507 static const struct unicam_format_info * 508 unicam_find_format_by_code(u32 code, u32 pad) 509 { 510 const struct unicam_format_info *formats; 511 unsigned int num_formats; 512 unsigned int i; 513 514 if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) { 515 formats = unicam_image_formats; 516 num_formats = ARRAY_SIZE(unicam_image_formats); 517 } else { 518 formats = unicam_meta_formats; 519 num_formats = ARRAY_SIZE(unicam_meta_formats); 520 } 521 522 for (i = 0; i < num_formats; i++) { 523 if (formats[i].code == code) 524 return &formats[i]; 525 } 526 527 return NULL; 528 } 529 530 static const struct unicam_format_info * 531 unicam_find_format_by_fourcc(u32 fourcc, u32 pad) 532 { 533 const struct unicam_format_info *formats; 534 unsigned int num_formats; 535 unsigned int i; 536 537 if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) { 538 formats = unicam_image_formats; 539 num_formats = ARRAY_SIZE(unicam_image_formats); 540 } else { 541 formats = unicam_meta_formats; 542 num_formats = ARRAY_SIZE(unicam_meta_formats); 543 } 544 545 for (i = 0; i < num_formats; ++i) { 546 if (formats[i].fourcc == fourcc || 547 formats[i].unpacked_fourcc == fourcc) 548 return &formats[i]; 549 } 550 551 return NULL; 552 } 553 554 static void unicam_calc_image_size_bpl(struct unicam_device *unicam, 555 const struct unicam_format_info *fmtinfo, 556 struct v4l2_pix_format *pix) 557 { 558 u32 min_bpl; 559 560 v4l_bound_align_image(&pix->width, UNICAM_IMAGE_MIN_WIDTH, 561 UNICAM_IMAGE_MAX_WIDTH, 2, 562 &pix->height, UNICAM_IMAGE_MIN_HEIGHT, 563 UNICAM_IMAGE_MAX_HEIGHT, 0, 0); 564 565 /* Unpacking always goes to 16bpp */ 566 if (pix->pixelformat == fmtinfo->unpacked_fourcc) 567 min_bpl = pix->width * 2; 568 else 569 min_bpl = pix->width * fmtinfo->depth / 8; 570 min_bpl = ALIGN(min_bpl, UNICAM_IMAGE_BPL_ALIGNMENT); 571 572 pix->bytesperline = ALIGN(pix->bytesperline, UNICAM_IMAGE_BPL_ALIGNMENT); 573 pix->bytesperline = clamp_t(unsigned int, pix->bytesperline, min_bpl, 574 UNICAM_IMAGE_MAX_BPL); 575 576 pix->sizeimage = pix->height * pix->bytesperline; 577 } 578 579 static void unicam_calc_meta_size_bpl(struct unicam_device *unicam, 580 const struct unicam_format_info *fmtinfo, 581 struct v4l2_meta_format *meta) 582 { 583 v4l_bound_align_image(&meta->width, UNICAM_META_MIN_WIDTH, 584 UNICAM_META_MAX_WIDTH, 0, 585 &meta->height, UNICAM_META_MIN_HEIGHT, 586 UNICAM_META_MAX_HEIGHT, 0, 0); 587 588 meta->bytesperline = ALIGN(meta->width * fmtinfo->depth / 8, 589 UNICAM_DMA_BPL_ALIGNMENT); 590 meta->buffersize = meta->height * meta->bytesperline; 591 } 592 593 /* ----------------------------------------------------------------------------- 594 * Hardware handling 595 */ 596 597 static inline void unicam_clk_write(struct unicam_device *unicam, u32 val) 598 { 599 /* Pass the CM_PASSWORD along with the value. */ 600 writel(val | 0x5a000000, unicam->clk_gate_base); 601 } 602 603 static inline u32 unicam_reg_read(struct unicam_device *unicam, u32 offset) 604 { 605 return readl(unicam->base + offset); 606 } 607 608 static inline void unicam_reg_write(struct unicam_device *unicam, u32 offset, u32 val) 609 { 610 writel(val, unicam->base + offset); 611 } 612 613 static inline int unicam_get_field(u32 value, u32 mask) 614 { 615 return (value & mask) >> __ffs(mask); 616 } 617 618 static inline void unicam_set_field(u32 *valp, u32 field, u32 mask) 619 { 620 u32 val = *valp; 621 622 val &= ~mask; 623 val |= (field << __ffs(mask)) & mask; 624 *valp = val; 625 } 626 627 static inline void unicam_reg_write_field(struct unicam_device *unicam, u32 offset, 628 u32 field, u32 mask) 629 { 630 u32 val = unicam_reg_read(unicam, offset); 631 632 unicam_set_field(&val, field, mask); 633 unicam_reg_write(unicam, offset, val); 634 } 635 636 static void unicam_wr_dma_addr(struct unicam_node *node, 637 struct unicam_buffer *buf) 638 { 639 /* 640 * Due to a HW bug causing buffer overruns in circular buffer mode under 641 * certain (not yet fully known) conditions, the dummy buffer allocation 642 * is set to a a single page size, but the hardware gets programmed with 643 * a buffer size of 0. 644 */ 645 dma_addr_t endaddr = buf->dma_addr + 646 (buf != &node->dummy_buf ? buf->size : 0); 647 648 if (node->id == UNICAM_IMAGE_NODE) { 649 unicam_reg_write(node->dev, UNICAM_IBSA0, buf->dma_addr); 650 unicam_reg_write(node->dev, UNICAM_IBEA0, endaddr); 651 } else { 652 unicam_reg_write(node->dev, UNICAM_DBSA0, buf->dma_addr); 653 unicam_reg_write(node->dev, UNICAM_DBEA0, endaddr); 654 } 655 } 656 657 static unsigned int unicam_get_lines_done(struct unicam_device *unicam) 658 { 659 struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE]; 660 unsigned int stride = node->fmt.fmt.pix.bytesperline; 661 struct unicam_buffer *frm = node->cur_frm; 662 dma_addr_t cur_addr; 663 664 if (!frm) 665 return 0; 666 667 cur_addr = unicam_reg_read(unicam, UNICAM_IBWP); 668 return (unsigned int)(cur_addr - frm->dma_addr) / stride; 669 } 670 671 static void unicam_schedule_next_buffer(struct unicam_node *node) 672 { 673 struct unicam_buffer *buf; 674 675 buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list); 676 node->next_frm = buf; 677 list_del(&buf->list); 678 679 unicam_wr_dma_addr(node, buf); 680 } 681 682 static void unicam_schedule_dummy_buffer(struct unicam_node *node) 683 { 684 int node_id = is_image_node(node) ? UNICAM_IMAGE_NODE : UNICAM_METADATA_NODE; 685 686 dev_dbg(node->dev->dev, "Scheduling dummy buffer for node %d\n", node_id); 687 688 unicam_wr_dma_addr(node, &node->dummy_buf); 689 690 node->next_frm = NULL; 691 } 692 693 static void unicam_process_buffer_complete(struct unicam_node *node, 694 unsigned int sequence) 695 { 696 node->cur_frm->vb.field = node->fmt.fmt.pix.field; 697 node->cur_frm->vb.sequence = sequence; 698 699 vb2_buffer_done(&node->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE); 700 } 701 702 static void unicam_queue_event_sof(struct unicam_device *unicam) 703 { 704 struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE]; 705 struct v4l2_event event = { 706 .type = V4L2_EVENT_FRAME_SYNC, 707 .u.frame_sync.frame_sequence = unicam->sequence, 708 }; 709 710 v4l2_event_queue(&node->video_dev, &event); 711 } 712 713 static irqreturn_t unicam_isr(int irq, void *dev) 714 { 715 struct unicam_device *unicam = dev; 716 unsigned int lines_done = unicam_get_lines_done(dev); 717 unsigned int sequence = unicam->sequence; 718 unsigned int i; 719 u32 ista, sta; 720 bool fe; 721 u64 ts; 722 723 sta = unicam_reg_read(unicam, UNICAM_STA); 724 /* Write value back to clear the interrupts */ 725 unicam_reg_write(unicam, UNICAM_STA, sta); 726 727 ista = unicam_reg_read(unicam, UNICAM_ISTA); 728 /* Write value back to clear the interrupts */ 729 unicam_reg_write(unicam, UNICAM_ISTA, ista); 730 731 dev_dbg(unicam->dev, "ISR: ISTA: 0x%X, STA: 0x%X, sequence %d, lines done %d\n", 732 ista, sta, sequence, lines_done); 733 734 if (!(sta & (UNICAM_IS | UNICAM_PI0))) 735 return IRQ_HANDLED; 736 737 /* 738 * Look for either the Frame End interrupt or the Packet Capture status 739 * to signal a frame end. 740 */ 741 fe = ista & UNICAM_FEI || sta & UNICAM_PI0; 742 743 /* 744 * We must run the frame end handler first. If we have a valid next_frm 745 * and we get a simultaneout FE + FS interrupt, running the FS handler 746 * first would null out the next_frm ptr and we would have lost the 747 * buffer forever. 748 */ 749 if (fe) { 750 bool inc_seq = unicam->frame_started; 751 752 /* 753 * Ensure we have swapped buffers already as we can't 754 * stop the peripheral. If no buffer is available, use a 755 * dummy buffer to dump out frames until we get a new buffer 756 * to use. 757 */ 758 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) { 759 struct unicam_node *node = &unicam->node[i]; 760 761 if (!vb2_start_streaming_called(&node->buffer_queue)) 762 continue; 763 764 /* 765 * If cur_frm == next_frm, it means we have not had 766 * a chance to swap buffers, likely due to having 767 * multiple interrupts occurring simultaneously (like FE 768 * + FS + LS). In this case, we cannot signal the buffer 769 * as complete, as the HW will reuse that buffer. 770 */ 771 if (node->cur_frm && node->cur_frm != node->next_frm) { 772 unicam_process_buffer_complete(node, sequence); 773 inc_seq = true; 774 } 775 node->cur_frm = node->next_frm; 776 } 777 778 /* 779 * Increment the sequence number conditionally on either a FS 780 * having already occurred, or in the FE + FS condition as 781 * caught in the FE handler above. This ensures the sequence 782 * number corresponds to the frames generated by the sensor, not 783 * the frames dequeued to userland. 784 */ 785 if (inc_seq) { 786 unicam->sequence++; 787 unicam->frame_started = false; 788 } 789 } 790 791 if (ista & UNICAM_FSI) { 792 /* 793 * Timestamp is to be when the first data byte was captured, 794 * aka frame start. 795 */ 796 ts = ktime_get_ns(); 797 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) { 798 struct unicam_node *node = &unicam->node[i]; 799 800 if (!vb2_start_streaming_called(&node->buffer_queue)) 801 continue; 802 803 if (node->cur_frm) 804 node->cur_frm->vb.vb2_buf.timestamp = ts; 805 else 806 dev_dbg(unicam->v4l2_dev.dev, 807 "ISR: [%d] Dropping frame, buffer not available at FS\n", 808 i); 809 /* 810 * Set the next frame output to go to a dummy frame 811 * if we have not managed to obtain another frame 812 * from the queue. 813 */ 814 unicam_schedule_dummy_buffer(node); 815 } 816 817 unicam_queue_event_sof(unicam); 818 unicam->frame_started = true; 819 } 820 821 /* 822 * Cannot swap buffer at frame end, there may be a race condition 823 * where the HW does not actually swap it if the new frame has 824 * already started. 825 */ 826 if (ista & (UNICAM_FSI | UNICAM_LCI) && !fe) { 827 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) { 828 struct unicam_node *node = &unicam->node[i]; 829 830 if (!vb2_start_streaming_called(&node->buffer_queue)) 831 continue; 832 833 spin_lock(&node->dma_queue_lock); 834 if (!list_empty(&node->dma_queue) && !node->next_frm) 835 unicam_schedule_next_buffer(node); 836 spin_unlock(&node->dma_queue_lock); 837 } 838 } 839 840 return IRQ_HANDLED; 841 } 842 843 static void unicam_set_packing_config(struct unicam_device *unicam, 844 const struct unicam_format_info *fmtinfo) 845 { 846 struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE]; 847 u32 pack, unpack; 848 u32 val; 849 850 if (node->fmt.fmt.pix.pixelformat == fmtinfo->fourcc) { 851 unpack = UNICAM_PUM_NONE; 852 pack = UNICAM_PPM_NONE; 853 } else { 854 unpack = fmtinfo->unpack; 855 /* Repacking is always to 16bpp */ 856 pack = UNICAM_PPM_PACK16; 857 } 858 859 val = 0; 860 unicam_set_field(&val, unpack, UNICAM_PUM_MASK); 861 unicam_set_field(&val, pack, UNICAM_PPM_MASK); 862 unicam_reg_write(unicam, UNICAM_IPIPE, val); 863 } 864 865 static void unicam_cfg_image_id(struct unicam_device *unicam, u8 vc, u8 dt) 866 { 867 if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) { 868 /* CSI2 mode */ 869 unicam_reg_write(unicam, UNICAM_IDI0, (vc << 6) | dt); 870 } else { 871 /* CCP2 mode */ 872 unicam_reg_write(unicam, UNICAM_IDI0, 0x80 | dt); 873 } 874 } 875 876 static void unicam_enable_ed(struct unicam_device *unicam) 877 { 878 u32 val = unicam_reg_read(unicam, UNICAM_DCS); 879 880 unicam_set_field(&val, 2, UNICAM_EDL_MASK); 881 /* Do not wrap at the end of the embedded data buffer */ 882 unicam_set_field(&val, 0, UNICAM_DBOB); 883 884 unicam_reg_write(unicam, UNICAM_DCS, val); 885 } 886 887 static int unicam_get_image_vc_dt(struct unicam_device *unicam, 888 struct v4l2_subdev_state *state, 889 u8 *vc, u8 *dt) 890 { 891 struct v4l2_mbus_frame_desc fd; 892 u32 stream; 893 int ret; 894 895 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, 896 UNICAM_SD_PAD_SOURCE_IMAGE, 897 0, NULL, &stream); 898 if (ret) 899 return ret; 900 901 ret = v4l2_subdev_call(unicam->sensor.subdev, pad, get_frame_desc, 902 unicam->sensor.pad->index, &fd); 903 if (ret) 904 return ret; 905 906 /* Only CSI-2 supports DTs. */ 907 if (fd.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2) 908 return -EINVAL; 909 910 for (unsigned int i = 0; i < fd.num_entries; ++i) { 911 const struct v4l2_mbus_frame_desc_entry *fde = &fd.entry[i]; 912 913 if (fde->stream == stream) { 914 *vc = fde->bus.csi2.vc; 915 *dt = fde->bus.csi2.dt; 916 return 0; 917 } 918 } 919 920 return -EINVAL; 921 } 922 923 static void unicam_start_rx(struct unicam_device *unicam, 924 struct v4l2_subdev_state *state) 925 { 926 struct unicam_node *node = &unicam->node[UNICAM_IMAGE_NODE]; 927 const struct unicam_format_info *fmtinfo; 928 const struct v4l2_mbus_framefmt *fmt; 929 unsigned int line_int_freq; 930 u8 vc, dt; 931 u32 val; 932 int ret; 933 934 fmt = v4l2_subdev_state_get_format(state, UNICAM_SD_PAD_SOURCE_IMAGE, 0); 935 fmtinfo = unicam_find_format_by_code(fmt->code, 936 UNICAM_SD_PAD_SOURCE_IMAGE); 937 if (WARN_ON(!fmtinfo)) 938 return; 939 940 /* 941 * Enable lane clocks. The register is structured as follows: 942 * 943 * [9:8] - DAT3 944 * [7:6] - DAT2 945 * [5:4] - DAT1 946 * [3:2] - DAT0 947 * [1:0] - CLK 948 * 949 * Enabled lane must be set to b01, and disabled lanes to b00. The clock 950 * lane is always enabled. 951 */ 952 val = 0x155 & GENMASK(unicam->pipe.num_data_lanes * 2 + 1, 0); 953 unicam_clk_write(unicam, val); 954 955 /* Basic init */ 956 unicam_reg_write(unicam, UNICAM_CTRL, UNICAM_MEM); 957 958 /* Enable analogue control, and leave in reset. */ 959 val = UNICAM_AR; 960 unicam_set_field(&val, 7, UNICAM_CTATADJ_MASK); 961 unicam_set_field(&val, 7, UNICAM_PTATADJ_MASK); 962 unicam_reg_write(unicam, UNICAM_ANA, val); 963 usleep_range(1000, 2000); 964 965 /* Come out of reset */ 966 unicam_reg_write_field(unicam, UNICAM_ANA, 0, UNICAM_AR); 967 968 /* Peripheral reset */ 969 unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPR); 970 unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPR); 971 972 unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPE); 973 974 /* Enable Rx control. */ 975 val = unicam_reg_read(unicam, UNICAM_CTRL); 976 if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) { 977 unicam_set_field(&val, UNICAM_CPM_CSI2, UNICAM_CPM_MASK); 978 unicam_set_field(&val, UNICAM_DCM_STROBE, UNICAM_DCM_MASK); 979 } else { 980 unicam_set_field(&val, UNICAM_CPM_CCP2, UNICAM_CPM_MASK); 981 unicam_set_field(&val, unicam->bus_flags, UNICAM_DCM_MASK); 982 } 983 /* Packet framer timeout */ 984 unicam_set_field(&val, 0xf, UNICAM_PFT_MASK); 985 unicam_set_field(&val, 128, UNICAM_OET_MASK); 986 unicam_reg_write(unicam, UNICAM_CTRL, val); 987 988 unicam_reg_write(unicam, UNICAM_IHWIN, 0); 989 unicam_reg_write(unicam, UNICAM_IVWIN, 0); 990 991 /* AXI bus access QoS setup */ 992 val = unicam_reg_read(unicam, UNICAM_PRI); 993 unicam_set_field(&val, 0, UNICAM_BL_MASK); 994 unicam_set_field(&val, 0, UNICAM_BS_MASK); 995 unicam_set_field(&val, 0xe, UNICAM_PP_MASK); 996 unicam_set_field(&val, 8, UNICAM_NP_MASK); 997 unicam_set_field(&val, 2, UNICAM_PT_MASK); 998 unicam_set_field(&val, 1, UNICAM_PE); 999 unicam_reg_write(unicam, UNICAM_PRI, val); 1000 1001 unicam_reg_write_field(unicam, UNICAM_ANA, 0, UNICAM_DDL); 1002 1003 val = UNICAM_FSIE | UNICAM_FEIE | UNICAM_IBOB; 1004 line_int_freq = max(fmt->height >> 2, 128); 1005 unicam_set_field(&val, line_int_freq, UNICAM_LCIE_MASK); 1006 unicam_reg_write(unicam, UNICAM_ICTL, val); 1007 unicam_reg_write(unicam, UNICAM_STA, UNICAM_STA_MASK_ALL); 1008 unicam_reg_write(unicam, UNICAM_ISTA, UNICAM_ISTA_MASK_ALL); 1009 1010 /* tclk_term_en */ 1011 unicam_reg_write_field(unicam, UNICAM_CLT, 2, UNICAM_CLT1_MASK); 1012 /* tclk_settle */ 1013 unicam_reg_write_field(unicam, UNICAM_CLT, 6, UNICAM_CLT2_MASK); 1014 /* td_term_en */ 1015 unicam_reg_write_field(unicam, UNICAM_DLT, 2, UNICAM_DLT1_MASK); 1016 /* ths_settle */ 1017 unicam_reg_write_field(unicam, UNICAM_DLT, 6, UNICAM_DLT2_MASK); 1018 /* trx_enable */ 1019 unicam_reg_write_field(unicam, UNICAM_DLT, 0, UNICAM_DLT3_MASK); 1020 1021 unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_SOE); 1022 1023 /* Packet compare setup - required to avoid missing frame ends */ 1024 val = 0; 1025 unicam_set_field(&val, 1, UNICAM_PCE); 1026 unicam_set_field(&val, 1, UNICAM_GI); 1027 unicam_set_field(&val, 1, UNICAM_CPH); 1028 unicam_set_field(&val, 0, UNICAM_PCVC_MASK); 1029 unicam_set_field(&val, 1, UNICAM_PCDT_MASK); 1030 unicam_reg_write(unicam, UNICAM_CMP0, val); 1031 1032 /* Enable clock lane and set up terminations */ 1033 val = 0; 1034 if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) { 1035 /* CSI2 */ 1036 unicam_set_field(&val, 1, UNICAM_CLE); 1037 unicam_set_field(&val, 1, UNICAM_CLLPE); 1038 if (!(unicam->bus_flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK)) { 1039 unicam_set_field(&val, 1, UNICAM_CLTRE); 1040 unicam_set_field(&val, 1, UNICAM_CLHSE); 1041 } 1042 } else { 1043 /* CCP2 */ 1044 unicam_set_field(&val, 1, UNICAM_CLE); 1045 unicam_set_field(&val, 1, UNICAM_CLHSE); 1046 unicam_set_field(&val, 1, UNICAM_CLTRE); 1047 } 1048 unicam_reg_write(unicam, UNICAM_CLK, val); 1049 1050 /* 1051 * Enable required data lanes with appropriate terminations. 1052 * The same value needs to be written to UNICAM_DATn registers for 1053 * the active lanes, and 0 for inactive ones. 1054 */ 1055 val = 0; 1056 if (unicam->bus_type == V4L2_MBUS_CSI2_DPHY) { 1057 /* CSI2 */ 1058 unicam_set_field(&val, 1, UNICAM_DLE); 1059 unicam_set_field(&val, 1, UNICAM_DLLPE); 1060 if (!(unicam->bus_flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK)) { 1061 unicam_set_field(&val, 1, UNICAM_DLTRE); 1062 unicam_set_field(&val, 1, UNICAM_DLHSE); 1063 } 1064 } else { 1065 /* CCP2 */ 1066 unicam_set_field(&val, 1, UNICAM_DLE); 1067 unicam_set_field(&val, 1, UNICAM_DLHSE); 1068 unicam_set_field(&val, 1, UNICAM_DLTRE); 1069 } 1070 unicam_reg_write(unicam, UNICAM_DAT0, val); 1071 1072 if (unicam->pipe.num_data_lanes == 1) 1073 val = 0; 1074 unicam_reg_write(unicam, UNICAM_DAT1, val); 1075 1076 if (unicam->max_data_lanes > 2) { 1077 /* 1078 * Registers UNICAM_DAT2 and UNICAM_DAT3 only valid if the 1079 * instance supports more than 2 data lanes. 1080 */ 1081 if (unicam->pipe.num_data_lanes == 2) 1082 val = 0; 1083 unicam_reg_write(unicam, UNICAM_DAT2, val); 1084 1085 if (unicam->pipe.num_data_lanes == 3) 1086 val = 0; 1087 unicam_reg_write(unicam, UNICAM_DAT3, val); 1088 } 1089 1090 unicam_reg_write(unicam, UNICAM_IBLS, 1091 node->fmt.fmt.pix.bytesperline); 1092 unicam_wr_dma_addr(node, node->cur_frm); 1093 unicam_set_packing_config(unicam, fmtinfo); 1094 1095 ret = unicam_get_image_vc_dt(unicam, state, &vc, &dt); 1096 if (ret) { 1097 /* 1098 * If the source doesn't support frame descriptors, default to 1099 * VC 0 and use the DT corresponding to the format. 1100 */ 1101 vc = 0; 1102 dt = fmtinfo->csi_dt; 1103 } 1104 1105 unicam_cfg_image_id(unicam, vc, dt); 1106 1107 val = unicam_reg_read(unicam, UNICAM_MISC); 1108 unicam_set_field(&val, 1, UNICAM_FL0); 1109 unicam_set_field(&val, 1, UNICAM_FL1); 1110 unicam_reg_write(unicam, UNICAM_MISC, val); 1111 1112 /* Enable peripheral */ 1113 unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPE); 1114 1115 /* Load image pointers */ 1116 unicam_reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_LIP_MASK); 1117 1118 /* 1119 * Enable trigger only for the first frame to 1120 * sync correctly to the FS from the source. 1121 */ 1122 unicam_reg_write_field(unicam, UNICAM_ICTL, 1, UNICAM_TFC); 1123 } 1124 1125 static void unicam_start_metadata(struct unicam_device *unicam) 1126 { 1127 struct unicam_node *node = &unicam->node[UNICAM_METADATA_NODE]; 1128 1129 unicam_enable_ed(unicam); 1130 unicam_wr_dma_addr(node, node->cur_frm); 1131 unicam_reg_write_field(unicam, UNICAM_DCS, 1, UNICAM_LDP); 1132 } 1133 1134 static void unicam_disable(struct unicam_device *unicam) 1135 { 1136 /* Analogue lane control disable */ 1137 unicam_reg_write_field(unicam, UNICAM_ANA, 1, UNICAM_DDL); 1138 1139 /* Stop the output engine */ 1140 unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_SOE); 1141 1142 /* Disable the data lanes. */ 1143 unicam_reg_write(unicam, UNICAM_DAT0, 0); 1144 unicam_reg_write(unicam, UNICAM_DAT1, 0); 1145 1146 if (unicam->max_data_lanes > 2) { 1147 unicam_reg_write(unicam, UNICAM_DAT2, 0); 1148 unicam_reg_write(unicam, UNICAM_DAT3, 0); 1149 } 1150 1151 /* Peripheral reset */ 1152 unicam_reg_write_field(unicam, UNICAM_CTRL, 1, UNICAM_CPR); 1153 usleep_range(50, 100); 1154 unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPR); 1155 1156 /* Disable peripheral */ 1157 unicam_reg_write_field(unicam, UNICAM_CTRL, 0, UNICAM_CPE); 1158 1159 /* Clear ED setup */ 1160 unicam_reg_write(unicam, UNICAM_DCS, 0); 1161 1162 /* Disable all lane clocks */ 1163 unicam_clk_write(unicam, 0); 1164 } 1165 1166 /* ----------------------------------------------------------------------------- 1167 * V4L2 subdev operations 1168 */ 1169 1170 static int __unicam_subdev_set_routing(struct v4l2_subdev *sd, 1171 struct v4l2_subdev_state *state, 1172 struct v4l2_subdev_krouting *routing) 1173 { 1174 struct v4l2_subdev_route *route; 1175 int ret; 1176 1177 ret = v4l2_subdev_routing_validate(sd, routing, 1178 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1); 1179 if (ret) 1180 return ret; 1181 1182 ret = v4l2_subdev_set_routing(sd, state, routing); 1183 if (ret) 1184 return ret; 1185 1186 for_each_active_route(&state->routing, route) { 1187 const struct v4l2_mbus_framefmt *def_fmt; 1188 struct v4l2_mbus_framefmt *fmt; 1189 1190 if (route->source_pad == UNICAM_SD_PAD_SOURCE_IMAGE) 1191 def_fmt = &unicam_default_image_format; 1192 else 1193 def_fmt = &unicam_default_meta_format; 1194 1195 fmt = v4l2_subdev_state_get_format(state, route->sink_pad, 1196 route->sink_stream); 1197 *fmt = *def_fmt; 1198 fmt = v4l2_subdev_state_get_format(state, route->source_pad, 1199 route->source_stream); 1200 *fmt = *def_fmt; 1201 } 1202 1203 return 0; 1204 } 1205 1206 static int unicam_subdev_init_state(struct v4l2_subdev *sd, 1207 struct v4l2_subdev_state *state) 1208 { 1209 struct v4l2_subdev_route routes[] = { 1210 { 1211 .sink_pad = UNICAM_SD_PAD_SINK, 1212 .sink_stream = 0, 1213 .source_pad = UNICAM_SD_PAD_SOURCE_IMAGE, 1214 .source_stream = 0, 1215 .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE, 1216 }, 1217 }; 1218 1219 struct v4l2_subdev_krouting routing = { 1220 .len_routes = ARRAY_SIZE(routes), 1221 .num_routes = ARRAY_SIZE(routes), 1222 .routes = routes, 1223 }; 1224 1225 /* Initialize routing to single route to the fist source pad. */ 1226 return __unicam_subdev_set_routing(sd, state, &routing); 1227 } 1228 1229 static int unicam_subdev_enum_mbus_code(struct v4l2_subdev *sd, 1230 struct v4l2_subdev_state *state, 1231 struct v4l2_subdev_mbus_code_enum *code) 1232 { 1233 u32 pad, stream; 1234 int ret; 1235 1236 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, 1237 code->pad, code->stream, 1238 &pad, &stream); 1239 if (ret) 1240 return ret; 1241 1242 if (unicam_sd_pad_is_source(code->pad)) { 1243 /* No transcoding, source and sink codes must match. */ 1244 const struct v4l2_mbus_framefmt *fmt; 1245 1246 fmt = v4l2_subdev_state_get_format(state, pad, stream); 1247 if (!fmt) 1248 return -EINVAL; 1249 1250 if (code->index > 0) 1251 return -EINVAL; 1252 1253 code->code = fmt->code; 1254 } else { 1255 const struct unicam_format_info *formats; 1256 unsigned int num_formats; 1257 1258 if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) { 1259 formats = unicam_image_formats; 1260 num_formats = ARRAY_SIZE(unicam_image_formats); 1261 } else { 1262 formats = unicam_meta_formats; 1263 num_formats = ARRAY_SIZE(unicam_meta_formats); 1264 } 1265 1266 if (code->index >= num_formats) 1267 return -EINVAL; 1268 1269 code->code = formats[code->index].code; 1270 } 1271 1272 return 0; 1273 } 1274 1275 static int unicam_subdev_enum_frame_size(struct v4l2_subdev *sd, 1276 struct v4l2_subdev_state *state, 1277 struct v4l2_subdev_frame_size_enum *fse) 1278 { 1279 u32 pad, stream; 1280 int ret; 1281 1282 if (fse->index > 0) 1283 return -EINVAL; 1284 1285 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, fse->pad, 1286 fse->stream, &pad, 1287 &stream); 1288 if (ret) 1289 return ret; 1290 1291 if (unicam_sd_pad_is_source(fse->pad)) { 1292 /* No transcoding, source and sink formats must match. */ 1293 const struct v4l2_mbus_framefmt *fmt; 1294 1295 fmt = v4l2_subdev_state_get_format(state, pad, stream); 1296 if (!fmt) 1297 return -EINVAL; 1298 1299 if (fse->code != fmt->code) 1300 return -EINVAL; 1301 1302 fse->min_width = fmt->width; 1303 fse->max_width = fmt->width; 1304 fse->min_height = fmt->height; 1305 fse->max_height = fmt->height; 1306 } else { 1307 const struct unicam_format_info *fmtinfo; 1308 1309 fmtinfo = unicam_find_format_by_code(fse->code, pad); 1310 if (!fmtinfo) 1311 return -EINVAL; 1312 1313 if (pad == UNICAM_SD_PAD_SOURCE_IMAGE) { 1314 fse->min_width = UNICAM_IMAGE_MIN_WIDTH; 1315 fse->max_width = UNICAM_IMAGE_MAX_WIDTH; 1316 fse->min_height = UNICAM_IMAGE_MIN_HEIGHT; 1317 fse->max_height = UNICAM_IMAGE_MAX_HEIGHT; 1318 } else { 1319 fse->min_width = UNICAM_META_MIN_WIDTH; 1320 fse->max_width = UNICAM_META_MAX_WIDTH; 1321 fse->min_height = UNICAM_META_MIN_HEIGHT; 1322 fse->max_height = UNICAM_META_MAX_HEIGHT; 1323 } 1324 } 1325 1326 return 0; 1327 } 1328 1329 static int unicam_subdev_set_format(struct v4l2_subdev *sd, 1330 struct v4l2_subdev_state *state, 1331 struct v4l2_subdev_format *format) 1332 { 1333 struct unicam_device *unicam = sd_to_unicam_device(sd); 1334 struct v4l2_mbus_framefmt *sink_format, *source_format; 1335 const struct unicam_format_info *fmtinfo; 1336 u32 source_pad, source_stream; 1337 int ret; 1338 1339 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && 1340 unicam->subdev.enabled_streams) 1341 return -EBUSY; 1342 1343 /* No transcoding, source and sink formats must match. */ 1344 if (unicam_sd_pad_is_source(format->pad)) 1345 return v4l2_subdev_get_fmt(sd, state, format); 1346 1347 /* 1348 * Allowed formats for the stream on the sink pad depend on what source 1349 * pad the stream is routed to. Find the corresponding source pad and 1350 * use it to validate the media bus code. 1351 */ 1352 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, 1353 format->pad, format->stream, 1354 &source_pad, &source_stream); 1355 if (ret) 1356 return ret; 1357 1358 fmtinfo = unicam_find_format_by_code(format->format.code, source_pad); 1359 if (!fmtinfo) { 1360 fmtinfo = source_pad == UNICAM_SD_PAD_SOURCE_IMAGE 1361 ? &unicam_image_formats[0] : &unicam_meta_formats[0]; 1362 format->format.code = fmtinfo->code; 1363 } 1364 1365 if (source_pad == UNICAM_SD_PAD_SOURCE_IMAGE) { 1366 format->format.width = clamp_t(unsigned int, 1367 format->format.width, 1368 UNICAM_IMAGE_MIN_WIDTH, 1369 UNICAM_IMAGE_MAX_WIDTH); 1370 format->format.height = clamp_t(unsigned int, 1371 format->format.height, 1372 UNICAM_IMAGE_MIN_HEIGHT, 1373 UNICAM_IMAGE_MAX_HEIGHT); 1374 format->format.field = V4L2_FIELD_NONE; 1375 } else { 1376 format->format.width = clamp_t(unsigned int, 1377 format->format.width, 1378 UNICAM_META_MIN_WIDTH, 1379 UNICAM_META_MAX_WIDTH); 1380 format->format.height = clamp_t(unsigned int, 1381 format->format.height, 1382 UNICAM_META_MIN_HEIGHT, 1383 UNICAM_META_MAX_HEIGHT); 1384 format->format.field = V4L2_FIELD_NONE; 1385 1386 /* Colorspace don't apply to metadata. */ 1387 format->format.colorspace = 0; 1388 format->format.ycbcr_enc = 0; 1389 format->format.quantization = 0; 1390 format->format.xfer_func = 0; 1391 } 1392 1393 sink_format = v4l2_subdev_state_get_format(state, format->pad, 1394 format->stream); 1395 source_format = v4l2_subdev_state_get_format(state, source_pad, 1396 source_stream); 1397 *sink_format = format->format; 1398 *source_format = format->format; 1399 1400 return 0; 1401 } 1402 1403 static int unicam_subdev_set_routing(struct v4l2_subdev *sd, 1404 struct v4l2_subdev_state *state, 1405 enum v4l2_subdev_format_whence which, 1406 struct v4l2_subdev_krouting *routing) 1407 { 1408 struct unicam_device *unicam = sd_to_unicam_device(sd); 1409 1410 if (which == V4L2_SUBDEV_FORMAT_ACTIVE && unicam->subdev.enabled_streams) 1411 return -EBUSY; 1412 1413 return __unicam_subdev_set_routing(sd, state, routing); 1414 } 1415 1416 static int unicam_sd_enable_streams(struct v4l2_subdev *sd, 1417 struct v4l2_subdev_state *state, u32 pad, 1418 u64 streams_mask) 1419 { 1420 struct unicam_device *unicam = sd_to_unicam_device(sd); 1421 u32 other_pad, other_stream; 1422 int ret; 1423 1424 if (!unicam->subdev.enabled_streams) { 1425 /* Configure and start Unicam. */ 1426 unicam->sequence = 0; 1427 1428 if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE)) 1429 unicam_start_metadata(unicam); 1430 1431 unicam->frame_started = false; 1432 unicam_start_rx(unicam, state); 1433 } 1434 1435 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, pad, 0, 1436 &other_pad, &other_stream); 1437 if (ret) 1438 return ret; 1439 1440 ret = v4l2_subdev_enable_streams(unicam->sensor.subdev, 1441 unicam->sensor.pad->index, 1442 BIT(other_stream)); 1443 if (ret) { 1444 dev_err(unicam->dev, "stream on failed in subdev\n"); 1445 return ret; 1446 } 1447 1448 unicam->subdev.enabled_streams |= BIT(other_stream); 1449 1450 return 0; 1451 } 1452 1453 static int unicam_sd_disable_streams(struct v4l2_subdev *sd, 1454 struct v4l2_subdev_state *state, u32 pad, 1455 u64 streams_mask) 1456 { 1457 struct unicam_device *unicam = sd_to_unicam_device(sd); 1458 u32 other_pad, other_stream; 1459 int ret; 1460 1461 ret = v4l2_subdev_routing_find_opposite_end(&state->routing, pad, 0, 1462 &other_pad, &other_stream); 1463 if (ret) 1464 return ret; 1465 1466 v4l2_subdev_disable_streams(unicam->sensor.subdev, 1467 unicam->sensor.pad->index, 1468 BIT(other_stream)); 1469 1470 unicam->subdev.enabled_streams &= ~BIT(other_stream); 1471 1472 if (!unicam->subdev.enabled_streams) 1473 unicam_disable(unicam); 1474 1475 return 0; 1476 } 1477 1478 static const struct v4l2_subdev_pad_ops unicam_subdev_pad_ops = { 1479 .enum_mbus_code = unicam_subdev_enum_mbus_code, 1480 .enum_frame_size = unicam_subdev_enum_frame_size, 1481 .get_fmt = v4l2_subdev_get_fmt, 1482 .set_fmt = unicam_subdev_set_format, 1483 .set_routing = unicam_subdev_set_routing, 1484 .enable_streams = unicam_sd_enable_streams, 1485 .disable_streams = unicam_sd_disable_streams, 1486 }; 1487 1488 static const struct v4l2_subdev_ops unicam_subdev_ops = { 1489 .pad = &unicam_subdev_pad_ops, 1490 }; 1491 1492 static const struct v4l2_subdev_internal_ops unicam_subdev_internal_ops = { 1493 .init_state = unicam_subdev_init_state, 1494 }; 1495 1496 static const struct media_entity_operations unicam_subdev_media_ops = { 1497 .link_validate = v4l2_subdev_link_validate, 1498 .has_pad_interdep = v4l2_subdev_has_pad_interdep, 1499 }; 1500 1501 static int unicam_subdev_init(struct unicam_device *unicam) 1502 { 1503 struct v4l2_subdev *sd = &unicam->subdev.sd; 1504 int ret; 1505 1506 v4l2_subdev_init(sd, &unicam_subdev_ops); 1507 sd->internal_ops = &unicam_subdev_internal_ops; 1508 v4l2_set_subdevdata(sd, unicam); 1509 1510 sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 1511 sd->entity.ops = &unicam_subdev_media_ops; 1512 sd->dev = unicam->dev; 1513 sd->owner = THIS_MODULE; 1514 sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS; 1515 1516 strscpy(sd->name, "unicam", sizeof(sd->name)); 1517 1518 unicam->subdev.pads[UNICAM_SD_PAD_SINK].flags = MEDIA_PAD_FL_SINK; 1519 unicam->subdev.pads[UNICAM_SD_PAD_SOURCE_IMAGE].flags = MEDIA_PAD_FL_SOURCE; 1520 unicam->subdev.pads[UNICAM_SD_PAD_SOURCE_METADATA].flags = MEDIA_PAD_FL_SOURCE; 1521 1522 ret = media_entity_pads_init(&sd->entity, ARRAY_SIZE(unicam->subdev.pads), 1523 unicam->subdev.pads); 1524 if (ret) { 1525 dev_err(unicam->dev, "Failed to initialize media entity: %d\n", 1526 ret); 1527 return ret; 1528 } 1529 1530 ret = v4l2_subdev_init_finalize(sd); 1531 if (ret) { 1532 dev_err(unicam->dev, "Failed to initialize subdev: %d\n", ret); 1533 goto err_entity; 1534 } 1535 1536 ret = v4l2_device_register_subdev(&unicam->v4l2_dev, sd); 1537 if (ret) { 1538 dev_err(unicam->dev, "Failed to register subdev: %d\n", ret); 1539 goto err_subdev; 1540 } 1541 1542 return 0; 1543 1544 err_subdev: 1545 v4l2_subdev_cleanup(sd); 1546 err_entity: 1547 media_entity_cleanup(&sd->entity); 1548 return ret; 1549 } 1550 1551 static void unicam_subdev_cleanup(struct unicam_device *unicam) 1552 { 1553 v4l2_subdev_cleanup(&unicam->subdev.sd); 1554 media_entity_cleanup(&unicam->subdev.sd.entity); 1555 } 1556 1557 /* ----------------------------------------------------------------------------- 1558 * Videobuf2 queue operations 1559 */ 1560 1561 static int unicam_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 1562 unsigned int *nplanes, unsigned int sizes[], 1563 struct device *alloc_devs[]) 1564 { 1565 struct unicam_node *node = vb2_get_drv_priv(vq); 1566 u32 size = is_image_node(node) ? node->fmt.fmt.pix.sizeimage 1567 : node->fmt.fmt.meta.buffersize; 1568 1569 if (*nplanes) { 1570 if (sizes[0] < size) { 1571 dev_dbg(node->dev->dev, "sizes[0] %i < size %u\n", 1572 sizes[0], size); 1573 return -EINVAL; 1574 } 1575 size = sizes[0]; 1576 } 1577 1578 *nplanes = 1; 1579 sizes[0] = size; 1580 1581 return 0; 1582 } 1583 1584 static int unicam_buffer_prepare(struct vb2_buffer *vb) 1585 { 1586 struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue); 1587 struct unicam_buffer *buf = to_unicam_buffer(vb); 1588 u32 size = is_image_node(node) ? node->fmt.fmt.pix.sizeimage 1589 : node->fmt.fmt.meta.buffersize; 1590 1591 if (vb2_plane_size(vb, 0) < size) { 1592 dev_dbg(node->dev->dev, 1593 "data will not fit into plane (%lu < %u)\n", 1594 vb2_plane_size(vb, 0), size); 1595 return -EINVAL; 1596 } 1597 1598 buf->dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0); 1599 buf->size = size; 1600 1601 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size); 1602 1603 return 0; 1604 } 1605 1606 static void unicam_return_buffers(struct unicam_node *node, 1607 enum vb2_buffer_state state) 1608 { 1609 struct unicam_buffer *buf, *tmp; 1610 1611 list_for_each_entry_safe(buf, tmp, &node->dma_queue, list) { 1612 list_del(&buf->list); 1613 vb2_buffer_done(&buf->vb.vb2_buf, state); 1614 } 1615 1616 if (node->cur_frm) 1617 vb2_buffer_done(&node->cur_frm->vb.vb2_buf, 1618 state); 1619 if (node->next_frm && node->cur_frm != node->next_frm) 1620 vb2_buffer_done(&node->next_frm->vb.vb2_buf, 1621 state); 1622 1623 node->cur_frm = NULL; 1624 node->next_frm = NULL; 1625 } 1626 1627 static int unicam_num_data_lanes(struct unicam_device *unicam) 1628 { 1629 struct v4l2_mbus_config mbus_config = { 0 }; 1630 unsigned int num_data_lanes; 1631 int ret; 1632 1633 if (unicam->bus_type != V4L2_MBUS_CSI2_DPHY) 1634 return unicam->max_data_lanes; 1635 1636 ret = v4l2_subdev_call(unicam->sensor.subdev, pad, get_mbus_config, 1637 unicam->sensor.pad->index, &mbus_config); 1638 if (ret == -ENOIOCTLCMD) 1639 return unicam->max_data_lanes; 1640 1641 if (ret < 0) { 1642 dev_err(unicam->dev, "Failed to get mbus config: %d\n", ret); 1643 return ret; 1644 } 1645 1646 num_data_lanes = mbus_config.bus.mipi_csi2.num_data_lanes; 1647 1648 if (num_data_lanes != 1 && num_data_lanes != 2 && num_data_lanes != 4) { 1649 dev_err(unicam->dev, 1650 "Device %s has requested %u data lanes, invalid\n", 1651 unicam->sensor.subdev->name, num_data_lanes); 1652 return -EINVAL; 1653 } 1654 1655 if (num_data_lanes > unicam->max_data_lanes) { 1656 dev_err(unicam->dev, 1657 "Device %s has requested %u data lanes, >%u configured in DT\n", 1658 unicam->sensor.subdev->name, num_data_lanes, 1659 unicam->max_data_lanes); 1660 return -EINVAL; 1661 } 1662 1663 return num_data_lanes; 1664 } 1665 1666 static int unicam_start_streaming(struct vb2_queue *vq, unsigned int count) 1667 { 1668 struct unicam_node *node = vb2_get_drv_priv(vq); 1669 struct unicam_device *unicam = node->dev; 1670 struct unicam_buffer *buf; 1671 struct media_pipeline_pad_iter iter; 1672 struct media_pad *pad; 1673 unsigned long flags; 1674 int ret; 1675 1676 dev_dbg(unicam->dev, "Starting stream on %s device\n", 1677 is_metadata_node(node) ? "metadata" : "image"); 1678 1679 /* 1680 * Start the pipeline. This validates all links, and populates the 1681 * pipeline structure. 1682 */ 1683 ret = video_device_pipeline_start(&node->video_dev, &unicam->pipe.pipe); 1684 if (ret < 0) { 1685 dev_dbg(unicam->dev, "Failed to start media pipeline: %d\n", ret); 1686 goto err_buffers; 1687 } 1688 1689 /* 1690 * Determine which video nodes are included in the pipeline, and get the 1691 * number of data lanes. 1692 */ 1693 if (unicam->pipe.pipe.start_count == 1) { 1694 unicam->pipe.nodes = 0; 1695 1696 media_pipeline_for_each_pad(&unicam->pipe.pipe, &iter, pad) { 1697 if (pad->entity != &unicam->subdev.sd.entity) 1698 continue; 1699 1700 if (pad->index == UNICAM_SD_PAD_SOURCE_IMAGE) 1701 unicam->pipe.nodes |= BIT(UNICAM_IMAGE_NODE); 1702 else if (pad->index == UNICAM_SD_PAD_SOURCE_METADATA) 1703 unicam->pipe.nodes |= BIT(UNICAM_METADATA_NODE); 1704 } 1705 1706 if (!(unicam->pipe.nodes & BIT(UNICAM_IMAGE_NODE))) { 1707 dev_dbg(unicam->dev, 1708 "Pipeline does not include image node\n"); 1709 ret = -EPIPE; 1710 goto err_pipeline; 1711 } 1712 1713 ret = unicam_num_data_lanes(unicam); 1714 if (ret < 0) 1715 goto err_pipeline; 1716 1717 unicam->pipe.num_data_lanes = ret; 1718 1719 dev_dbg(unicam->dev, "Running with %u data lanes, nodes %u\n", 1720 unicam->pipe.num_data_lanes, unicam->pipe.nodes); 1721 } 1722 1723 /* Arm the node with the first buffer from the DMA queue. */ 1724 spin_lock_irqsave(&node->dma_queue_lock, flags); 1725 buf = list_first_entry(&node->dma_queue, struct unicam_buffer, list); 1726 node->cur_frm = buf; 1727 node->next_frm = buf; 1728 list_del(&buf->list); 1729 spin_unlock_irqrestore(&node->dma_queue_lock, flags); 1730 1731 /* 1732 * Wait for all the video devices in the pipeline to have been started 1733 * before starting the hardware. In the general case, this would 1734 * prevent capturing multiple streams independently. However, the 1735 * Unicam DMA engines are not generic, they have been designed to 1736 * capture image data and embedded data from the same camera sensor. 1737 * Not only does the main use case not benefit from independent 1738 * capture, it requires proper synchronization of the streams at start 1739 * time. 1740 */ 1741 if (unicam->pipe.pipe.start_count < hweight32(unicam->pipe.nodes)) 1742 return 0; 1743 1744 ret = pm_runtime_resume_and_get(unicam->dev); 1745 if (ret < 0) { 1746 dev_err(unicam->dev, "PM runtime resume failed: %d\n", ret); 1747 goto err_pipeline; 1748 } 1749 1750 /* Enable the streams on the source. */ 1751 ret = v4l2_subdev_enable_streams(&unicam->subdev.sd, 1752 UNICAM_SD_PAD_SOURCE_IMAGE, 1753 BIT(0)); 1754 if (ret < 0) { 1755 dev_err(unicam->dev, "stream on failed in subdev\n"); 1756 goto err_pm_put; 1757 } 1758 1759 if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE)) { 1760 ret = v4l2_subdev_enable_streams(&unicam->subdev.sd, 1761 UNICAM_SD_PAD_SOURCE_METADATA, 1762 BIT(0)); 1763 if (ret < 0) { 1764 dev_err(unicam->dev, "stream on failed in subdev\n"); 1765 goto err_disable_streams; 1766 } 1767 } 1768 1769 return 0; 1770 1771 err_disable_streams: 1772 v4l2_subdev_disable_streams(&unicam->subdev.sd, 1773 UNICAM_SD_PAD_SOURCE_IMAGE, BIT(0)); 1774 err_pm_put: 1775 pm_runtime_put_sync(unicam->dev); 1776 err_pipeline: 1777 video_device_pipeline_stop(&node->video_dev); 1778 err_buffers: 1779 unicam_return_buffers(node, VB2_BUF_STATE_QUEUED); 1780 return ret; 1781 } 1782 1783 static void unicam_stop_streaming(struct vb2_queue *vq) 1784 { 1785 struct unicam_node *node = vb2_get_drv_priv(vq); 1786 struct unicam_device *unicam = node->dev; 1787 1788 /* Stop the hardware when the first video device gets stopped. */ 1789 if (unicam->pipe.pipe.start_count == hweight32(unicam->pipe.nodes)) { 1790 if (unicam->pipe.nodes & BIT(UNICAM_METADATA_NODE)) 1791 v4l2_subdev_disable_streams(&unicam->subdev.sd, 1792 UNICAM_SD_PAD_SOURCE_METADATA, 1793 BIT(0)); 1794 1795 v4l2_subdev_disable_streams(&unicam->subdev.sd, 1796 UNICAM_SD_PAD_SOURCE_IMAGE, 1797 BIT(0)); 1798 1799 pm_runtime_put(unicam->dev); 1800 } 1801 1802 video_device_pipeline_stop(&node->video_dev); 1803 1804 /* Clear all queued buffers for the node */ 1805 unicam_return_buffers(node, VB2_BUF_STATE_ERROR); 1806 } 1807 1808 static void unicam_buffer_queue(struct vb2_buffer *vb) 1809 { 1810 struct unicam_node *node = vb2_get_drv_priv(vb->vb2_queue); 1811 struct unicam_buffer *buf = to_unicam_buffer(vb); 1812 1813 spin_lock_irq(&node->dma_queue_lock); 1814 list_add_tail(&buf->list, &node->dma_queue); 1815 spin_unlock_irq(&node->dma_queue_lock); 1816 } 1817 1818 static const struct vb2_ops unicam_video_qops = { 1819 .queue_setup = unicam_queue_setup, 1820 .buf_prepare = unicam_buffer_prepare, 1821 .start_streaming = unicam_start_streaming, 1822 .stop_streaming = unicam_stop_streaming, 1823 .buf_queue = unicam_buffer_queue, 1824 }; 1825 1826 /* ----------------------------------------------------------------------------- 1827 * V4L2 video device operations 1828 */ 1829 1830 static int unicam_querycap(struct file *file, void *priv, 1831 struct v4l2_capability *cap) 1832 { 1833 strscpy(cap->driver, UNICAM_MODULE_NAME, sizeof(cap->driver)); 1834 strscpy(cap->card, UNICAM_MODULE_NAME, sizeof(cap->card)); 1835 1836 cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_META_CAPTURE; 1837 1838 return 0; 1839 } 1840 1841 static int unicam_enum_fmt_vid(struct file *file, void *priv, 1842 struct v4l2_fmtdesc *f) 1843 { 1844 unsigned int index; 1845 unsigned int i; 1846 1847 for (i = 0, index = 0; i < ARRAY_SIZE(unicam_image_formats); i++) { 1848 if (f->mbus_code && unicam_image_formats[i].code != f->mbus_code) 1849 continue; 1850 1851 if (index == f->index) { 1852 f->pixelformat = unicam_image_formats[i].fourcc; 1853 return 0; 1854 } 1855 1856 index++; 1857 1858 if (!unicam_image_formats[i].unpacked_fourcc) 1859 continue; 1860 1861 if (index == f->index) { 1862 f->pixelformat = unicam_image_formats[i].unpacked_fourcc; 1863 return 0; 1864 } 1865 1866 index++; 1867 } 1868 1869 return -EINVAL; 1870 } 1871 1872 static int unicam_g_fmt_vid(struct file *file, void *priv, 1873 struct v4l2_format *f) 1874 { 1875 struct unicam_node *node = video_drvdata(file); 1876 1877 *f = node->fmt; 1878 1879 return 0; 1880 } 1881 1882 static void __unicam_try_fmt_vid(struct unicam_node *node, 1883 struct v4l2_pix_format *pix) 1884 { 1885 const struct unicam_format_info *fmtinfo; 1886 1887 /* 1888 * Default to the first format if the requested pixel format code isn't 1889 * supported. 1890 */ 1891 fmtinfo = unicam_find_format_by_fourcc(pix->pixelformat, 1892 UNICAM_SD_PAD_SOURCE_IMAGE); 1893 if (!fmtinfo) { 1894 fmtinfo = &unicam_image_formats[0]; 1895 pix->pixelformat = fmtinfo->fourcc; 1896 } 1897 1898 unicam_calc_image_size_bpl(node->dev, fmtinfo, pix); 1899 1900 if (pix->field == V4L2_FIELD_ANY) 1901 pix->field = V4L2_FIELD_NONE; 1902 } 1903 1904 static int unicam_try_fmt_vid(struct file *file, void *priv, 1905 struct v4l2_format *f) 1906 { 1907 struct unicam_node *node = video_drvdata(file); 1908 1909 __unicam_try_fmt_vid(node, &f->fmt.pix); 1910 return 0; 1911 } 1912 1913 static int unicam_s_fmt_vid(struct file *file, void *priv, 1914 struct v4l2_format *f) 1915 { 1916 struct unicam_node *node = video_drvdata(file); 1917 1918 if (vb2_is_busy(&node->buffer_queue)) 1919 return -EBUSY; 1920 1921 __unicam_try_fmt_vid(node, &f->fmt.pix); 1922 node->fmt = *f; 1923 1924 return 0; 1925 } 1926 1927 static int unicam_enum_fmt_meta(struct file *file, void *priv, 1928 struct v4l2_fmtdesc *f) 1929 { 1930 unsigned int i, index; 1931 1932 for (i = 0, index = 0; i < ARRAY_SIZE(unicam_meta_formats); i++) { 1933 if (f->mbus_code && unicam_meta_formats[i].code != f->mbus_code) 1934 continue; 1935 1936 if (index == f->index) { 1937 f->pixelformat = unicam_meta_formats[i].fourcc; 1938 f->type = V4L2_BUF_TYPE_META_CAPTURE; 1939 f->flags = V4L2_FMT_FLAG_META_LINE_BASED; 1940 return 0; 1941 } 1942 1943 index++; 1944 } 1945 1946 return -EINVAL; 1947 } 1948 1949 static int unicam_g_fmt_meta(struct file *file, void *priv, 1950 struct v4l2_format *f) 1951 { 1952 struct unicam_node *node = video_drvdata(file); 1953 1954 f->fmt.meta = node->fmt.fmt.meta; 1955 1956 return 0; 1957 } 1958 1959 static const struct unicam_format_info * 1960 __unicam_try_fmt_meta(struct unicam_node *node, struct v4l2_meta_format *meta) 1961 { 1962 const struct unicam_format_info *fmtinfo; 1963 1964 /* 1965 * Default to the first format if the requested pixel format code isn't 1966 * supported. 1967 */ 1968 fmtinfo = unicam_find_format_by_fourcc(meta->dataformat, 1969 UNICAM_SD_PAD_SOURCE_METADATA); 1970 if (!fmtinfo) { 1971 fmtinfo = &unicam_meta_formats[0]; 1972 meta->dataformat = fmtinfo->fourcc; 1973 } 1974 1975 unicam_calc_meta_size_bpl(node->dev, fmtinfo, meta); 1976 1977 return fmtinfo; 1978 } 1979 1980 static int unicam_try_fmt_meta(struct file *file, void *priv, 1981 struct v4l2_format *f) 1982 { 1983 struct unicam_node *node = video_drvdata(file); 1984 1985 __unicam_try_fmt_meta(node, &f->fmt.meta); 1986 return 0; 1987 } 1988 1989 static int unicam_s_fmt_meta(struct file *file, void *priv, 1990 struct v4l2_format *f) 1991 { 1992 struct unicam_node *node = video_drvdata(file); 1993 1994 if (vb2_is_busy(&node->buffer_queue)) 1995 return -EBUSY; 1996 1997 __unicam_try_fmt_meta(node, &f->fmt.meta); 1998 node->fmt = *f; 1999 2000 return 0; 2001 } 2002 2003 static int unicam_enum_framesizes(struct file *file, void *fh, 2004 struct v4l2_frmsizeenum *fsize) 2005 { 2006 struct unicam_node *node = video_drvdata(file); 2007 int ret = -EINVAL; 2008 2009 if (fsize->index > 0) 2010 return ret; 2011 2012 if (is_image_node(node)) { 2013 if (!unicam_find_format_by_fourcc(fsize->pixel_format, 2014 UNICAM_SD_PAD_SOURCE_IMAGE)) 2015 return ret; 2016 2017 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 2018 fsize->stepwise.min_width = UNICAM_IMAGE_MIN_WIDTH; 2019 fsize->stepwise.max_width = UNICAM_IMAGE_MAX_WIDTH; 2020 fsize->stepwise.step_width = 1; 2021 fsize->stepwise.min_height = UNICAM_IMAGE_MIN_HEIGHT; 2022 fsize->stepwise.max_height = UNICAM_IMAGE_MAX_HEIGHT; 2023 fsize->stepwise.step_height = 1; 2024 } else { 2025 if (!unicam_find_format_by_fourcc(fsize->pixel_format, 2026 UNICAM_SD_PAD_SOURCE_METADATA)) 2027 return ret; 2028 2029 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 2030 fsize->stepwise.min_width = UNICAM_META_MIN_WIDTH; 2031 fsize->stepwise.max_width = UNICAM_META_MAX_WIDTH; 2032 fsize->stepwise.step_width = 1; 2033 fsize->stepwise.min_height = UNICAM_META_MIN_HEIGHT; 2034 fsize->stepwise.max_height = UNICAM_META_MAX_HEIGHT; 2035 fsize->stepwise.step_height = 1; 2036 } 2037 2038 return 0; 2039 } 2040 2041 static int unicam_log_status(struct file *file, void *fh) 2042 { 2043 struct unicam_node *node = video_drvdata(file); 2044 struct unicam_device *unicam = node->dev; 2045 u32 reg; 2046 2047 /* status for sub devices */ 2048 v4l2_device_call_all(&unicam->v4l2_dev, 0, core, log_status); 2049 2050 dev_info(unicam->dev, "-----Receiver status-----\n"); 2051 dev_info(unicam->dev, "V4L2 width/height: %ux%u\n", 2052 node->fmt.fmt.pix.width, node->fmt.fmt.pix.height); 2053 dev_info(unicam->dev, "V4L2 format: %08x\n", 2054 node->fmt.fmt.pix.pixelformat); 2055 reg = unicam_reg_read(unicam, UNICAM_IPIPE); 2056 dev_info(unicam->dev, "Unpacking/packing: %u / %u\n", 2057 unicam_get_field(reg, UNICAM_PUM_MASK), 2058 unicam_get_field(reg, UNICAM_PPM_MASK)); 2059 dev_info(unicam->dev, "----Live data----\n"); 2060 dev_info(unicam->dev, "Programmed stride: %4u\n", 2061 unicam_reg_read(unicam, UNICAM_IBLS)); 2062 dev_info(unicam->dev, "Detected resolution: %ux%u\n", 2063 unicam_reg_read(unicam, UNICAM_IHSTA), 2064 unicam_reg_read(unicam, UNICAM_IVSTA)); 2065 dev_info(unicam->dev, "Write pointer: %08x\n", 2066 unicam_reg_read(unicam, UNICAM_IBWP)); 2067 2068 return 0; 2069 } 2070 2071 static int unicam_subscribe_event(struct v4l2_fh *fh, 2072 const struct v4l2_event_subscription *sub) 2073 { 2074 switch (sub->type) { 2075 case V4L2_EVENT_FRAME_SYNC: 2076 return v4l2_event_subscribe(fh, sub, 2, NULL); 2077 default: 2078 return -EINVAL; 2079 } 2080 } 2081 2082 static const struct v4l2_ioctl_ops unicam_ioctl_ops = { 2083 .vidioc_querycap = unicam_querycap, 2084 2085 .vidioc_enum_fmt_vid_cap = unicam_enum_fmt_vid, 2086 .vidioc_g_fmt_vid_cap = unicam_g_fmt_vid, 2087 .vidioc_try_fmt_vid_cap = unicam_try_fmt_vid, 2088 .vidioc_s_fmt_vid_cap = unicam_s_fmt_vid, 2089 2090 .vidioc_enum_fmt_meta_cap = unicam_enum_fmt_meta, 2091 .vidioc_g_fmt_meta_cap = unicam_g_fmt_meta, 2092 .vidioc_try_fmt_meta_cap = unicam_try_fmt_meta, 2093 .vidioc_s_fmt_meta_cap = unicam_s_fmt_meta, 2094 2095 .vidioc_enum_framesizes = unicam_enum_framesizes, 2096 2097 .vidioc_reqbufs = vb2_ioctl_reqbufs, 2098 .vidioc_create_bufs = vb2_ioctl_create_bufs, 2099 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 2100 .vidioc_querybuf = vb2_ioctl_querybuf, 2101 .vidioc_qbuf = vb2_ioctl_qbuf, 2102 .vidioc_dqbuf = vb2_ioctl_dqbuf, 2103 .vidioc_expbuf = vb2_ioctl_expbuf, 2104 .vidioc_streamon = vb2_ioctl_streamon, 2105 .vidioc_streamoff = vb2_ioctl_streamoff, 2106 2107 .vidioc_log_status = unicam_log_status, 2108 .vidioc_subscribe_event = unicam_subscribe_event, 2109 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 2110 }; 2111 2112 /* unicam capture driver file operations */ 2113 static const struct v4l2_file_operations unicam_fops = { 2114 .owner = THIS_MODULE, 2115 .open = v4l2_fh_open, 2116 .release = vb2_fop_release, 2117 .poll = vb2_fop_poll, 2118 .unlocked_ioctl = video_ioctl2, 2119 .mmap = vb2_fop_mmap, 2120 }; 2121 2122 static int unicam_video_link_validate(struct media_link *link) 2123 { 2124 struct video_device *vdev = 2125 media_entity_to_video_device(link->sink->entity); 2126 struct v4l2_subdev *sd = 2127 media_entity_to_v4l2_subdev(link->source->entity); 2128 struct unicam_node *node = video_get_drvdata(vdev); 2129 const u32 pad = is_image_node(node) ? UNICAM_SD_PAD_SOURCE_IMAGE 2130 : UNICAM_SD_PAD_SOURCE_METADATA; 2131 const struct v4l2_mbus_framefmt *format; 2132 struct v4l2_subdev_state *state; 2133 int ret = 0; 2134 2135 state = v4l2_subdev_lock_and_get_active_state(sd); 2136 2137 format = v4l2_subdev_state_get_format(state, pad, 0); 2138 if (!format) { 2139 ret = -EINVAL; 2140 goto out; 2141 } 2142 2143 if (is_image_node(node)) { 2144 const struct v4l2_pix_format *fmt = &node->fmt.fmt.pix; 2145 const struct unicam_format_info *fmtinfo; 2146 2147 fmtinfo = unicam_find_format_by_fourcc(fmt->pixelformat, 2148 UNICAM_SD_PAD_SOURCE_IMAGE); 2149 if (WARN_ON(!fmtinfo)) { 2150 ret = -EPIPE; 2151 goto out; 2152 } 2153 2154 if (fmtinfo->code != format->code || 2155 fmt->height != format->height || 2156 fmt->width != format->width || 2157 fmt->field != format->field) { 2158 dev_dbg(node->dev->dev, 2159 "image: (%u x %u) 0x%08x %s != (%u x %u) 0x%08x %s\n", 2160 fmt->width, fmt->height, fmtinfo->code, 2161 v4l2_field_names[fmt->field], 2162 format->width, format->height, format->code, 2163 v4l2_field_names[format->field]); 2164 ret = -EPIPE; 2165 } 2166 } else { 2167 const struct v4l2_meta_format *fmt = &node->fmt.fmt.meta; 2168 2169 const struct unicam_format_info *fmtinfo; 2170 2171 fmtinfo = unicam_find_format_by_fourcc(fmt->dataformat, 2172 UNICAM_SD_PAD_SOURCE_METADATA); 2173 if (WARN_ON(!fmtinfo)) { 2174 ret = -EPIPE; 2175 goto out; 2176 } 2177 2178 if (fmtinfo->code != format->code || 2179 fmt->height != format->height || 2180 fmt->width != format->width) { 2181 dev_dbg(node->dev->dev, 2182 "meta: (%u x %u) 0x%04x != (%u x %u) 0x%04x\n", 2183 fmt->width, fmt->height, fmtinfo->code, 2184 format->width, format->height, format->code); 2185 ret = -EPIPE; 2186 } 2187 } 2188 2189 out: 2190 v4l2_subdev_unlock_state(state); 2191 return ret; 2192 } 2193 2194 static const struct media_entity_operations unicam_video_media_ops = { 2195 .link_validate = unicam_video_link_validate, 2196 }; 2197 2198 static void unicam_node_release(struct video_device *vdev) 2199 { 2200 struct unicam_node *node = video_get_drvdata(vdev); 2201 2202 unicam_put(node->dev); 2203 } 2204 2205 static void unicam_set_default_format(struct unicam_node *node) 2206 { 2207 if (is_image_node(node)) { 2208 struct v4l2_pix_format *fmt = &node->fmt.fmt.pix; 2209 const struct unicam_format_info *fmtinfo = 2210 &unicam_image_formats[0]; 2211 2212 node->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2213 2214 v4l2_fill_pix_format(fmt, &unicam_default_image_format); 2215 fmt->pixelformat = fmtinfo->fourcc; 2216 unicam_calc_image_size_bpl(node->dev, fmtinfo, fmt); 2217 } else { 2218 struct v4l2_meta_format *fmt = &node->fmt.fmt.meta; 2219 const struct unicam_format_info *fmtinfo = 2220 &unicam_meta_formats[0]; 2221 2222 node->fmt.type = V4L2_BUF_TYPE_META_CAPTURE; 2223 2224 fmt->dataformat = fmtinfo->fourcc; 2225 fmt->width = unicam_default_meta_format.width; 2226 fmt->height = unicam_default_meta_format.height; 2227 unicam_calc_meta_size_bpl(node->dev, fmtinfo, fmt); 2228 } 2229 } 2230 2231 static int unicam_register_node(struct unicam_device *unicam, 2232 enum unicam_node_type type) 2233 { 2234 const u32 pad_index = type == UNICAM_IMAGE_NODE 2235 ? UNICAM_SD_PAD_SOURCE_IMAGE 2236 : UNICAM_SD_PAD_SOURCE_METADATA; 2237 struct unicam_node *node = &unicam->node[type]; 2238 struct video_device *vdev = &node->video_dev; 2239 struct vb2_queue *q = &node->buffer_queue; 2240 int ret; 2241 2242 node->dev = unicam_get(unicam); 2243 node->id = type; 2244 2245 spin_lock_init(&node->dma_queue_lock); 2246 2247 INIT_LIST_HEAD(&node->dma_queue); 2248 2249 /* Initialize the videobuf2 queue. */ 2250 q->type = type == UNICAM_IMAGE_NODE ? V4L2_BUF_TYPE_VIDEO_CAPTURE 2251 : V4L2_BUF_TYPE_META_CAPTURE; 2252 q->io_modes = VB2_MMAP | VB2_DMABUF; 2253 q->drv_priv = node; 2254 q->ops = &unicam_video_qops; 2255 q->mem_ops = &vb2_dma_contig_memops; 2256 q->buf_struct_size = sizeof(struct unicam_buffer); 2257 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 2258 q->lock = &unicam->lock; 2259 q->min_queued_buffers = 1; 2260 q->dev = unicam->dev; 2261 2262 ret = vb2_queue_init(q); 2263 if (ret) { 2264 dev_err(unicam->dev, "vb2_queue_init() failed\n"); 2265 goto err_unicam_put; 2266 } 2267 2268 /* Initialize the video device. */ 2269 vdev->release = unicam_node_release; 2270 vdev->fops = &unicam_fops; 2271 vdev->ioctl_ops = &unicam_ioctl_ops; 2272 vdev->v4l2_dev = &unicam->v4l2_dev; 2273 vdev->vfl_dir = VFL_DIR_RX; 2274 vdev->queue = q; 2275 vdev->lock = &unicam->lock; 2276 vdev->device_caps = type == UNICAM_IMAGE_NODE 2277 ? V4L2_CAP_VIDEO_CAPTURE : V4L2_CAP_META_CAPTURE; 2278 vdev->device_caps |= V4L2_CAP_STREAMING | V4L2_CAP_IO_MC; 2279 vdev->entity.ops = &unicam_video_media_ops; 2280 2281 snprintf(vdev->name, sizeof(vdev->name), "%s-%s", UNICAM_MODULE_NAME, 2282 type == UNICAM_IMAGE_NODE ? "image" : "embedded"); 2283 2284 video_set_drvdata(vdev, node); 2285 2286 if (type == UNICAM_IMAGE_NODE) 2287 vdev->entity.flags |= MEDIA_ENT_FL_DEFAULT; 2288 2289 node->pad.flags = MEDIA_PAD_FL_SINK; 2290 2291 ret = media_entity_pads_init(&vdev->entity, 1, &node->pad); 2292 if (ret) 2293 goto err_unicam_put; 2294 2295 node->dummy_buf.size = UNICAM_DUMMY_BUF_SIZE; 2296 node->dummy_buf_cpu_addr = dma_alloc_coherent(unicam->dev, 2297 node->dummy_buf.size, 2298 &node->dummy_buf.dma_addr, 2299 GFP_KERNEL); 2300 if (!node->dummy_buf_cpu_addr) { 2301 dev_err(unicam->dev, "Unable to allocate dummy buffer.\n"); 2302 ret = -ENOMEM; 2303 goto err_entity_cleanup; 2304 } 2305 2306 unicam_set_default_format(node); 2307 2308 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 2309 if (ret) { 2310 dev_err(unicam->dev, "Unable to register video device %s\n", 2311 vdev->name); 2312 goto err_dma_free; 2313 } 2314 2315 node->registered = true; 2316 2317 ret = media_create_pad_link(&unicam->subdev.sd.entity, 2318 pad_index, 2319 &node->video_dev.entity, 2320 0, 2321 MEDIA_LNK_FL_ENABLED | 2322 MEDIA_LNK_FL_IMMUTABLE); 2323 if (ret) { 2324 /* 2325 * No need for cleanup, the caller will unregister the 2326 * video device, which will drop the reference on the 2327 * device and trigger the cleanup. 2328 */ 2329 dev_err(unicam->dev, "Unable to create pad link for %s\n", 2330 unicam->sensor.subdev->name); 2331 return ret; 2332 } 2333 2334 return 0; 2335 2336 err_dma_free: 2337 dma_free_coherent(unicam->dev, node->dummy_buf.size, 2338 node->dummy_buf_cpu_addr, 2339 node->dummy_buf.dma_addr); 2340 err_entity_cleanup: 2341 media_entity_cleanup(&vdev->entity); 2342 err_unicam_put: 2343 unicam_put(unicam); 2344 return ret; 2345 } 2346 2347 static void unicam_unregister_nodes(struct unicam_device *unicam) 2348 { 2349 unsigned int i; 2350 2351 for (i = 0; i < ARRAY_SIZE(unicam->node); i++) { 2352 struct unicam_node *node = &unicam->node[i]; 2353 2354 if (node->registered) { 2355 vb2_video_unregister_device(&node->video_dev); 2356 node->registered = false; 2357 } 2358 2359 if (node->dummy_buf_cpu_addr) 2360 dma_free_coherent(unicam->dev, node->dummy_buf.size, 2361 node->dummy_buf_cpu_addr, 2362 node->dummy_buf.dma_addr); 2363 } 2364 } 2365 2366 /* ----------------------------------------------------------------------------- 2367 * Power management 2368 */ 2369 2370 static int unicam_runtime_resume(struct device *dev) 2371 { 2372 struct unicam_device *unicam = dev_get_drvdata(dev); 2373 int ret; 2374 2375 ret = clk_set_min_rate(unicam->vpu_clock, UNICAM_MIN_VPU_CLOCK_RATE); 2376 if (ret) { 2377 dev_err(unicam->dev, "failed to set up VPU clock\n"); 2378 return ret; 2379 } 2380 2381 ret = clk_prepare_enable(unicam->vpu_clock); 2382 if (ret) { 2383 dev_err(unicam->dev, "Failed to enable VPU clock: %d\n", ret); 2384 goto err_vpu_clock; 2385 } 2386 2387 ret = clk_set_rate(unicam->clock, 100 * 1000 * 1000); 2388 if (ret) { 2389 dev_err(unicam->dev, "failed to set up CSI clock\n"); 2390 goto err_vpu_prepare; 2391 } 2392 2393 ret = clk_prepare_enable(unicam->clock); 2394 if (ret) { 2395 dev_err(unicam->dev, "Failed to enable CSI clock: %d\n", ret); 2396 goto err_vpu_prepare; 2397 } 2398 2399 return 0; 2400 2401 err_vpu_prepare: 2402 clk_disable_unprepare(unicam->vpu_clock); 2403 err_vpu_clock: 2404 if (clk_set_min_rate(unicam->vpu_clock, 0)) 2405 dev_err(unicam->dev, "failed to reset the VPU clock\n"); 2406 2407 return ret; 2408 } 2409 2410 static int unicam_runtime_suspend(struct device *dev) 2411 { 2412 struct unicam_device *unicam = dev_get_drvdata(dev); 2413 2414 clk_disable_unprepare(unicam->clock); 2415 2416 if (clk_set_min_rate(unicam->vpu_clock, 0)) 2417 dev_err(unicam->dev, "failed to reset the VPU clock\n"); 2418 2419 clk_disable_unprepare(unicam->vpu_clock); 2420 2421 return 0; 2422 } 2423 2424 static const struct dev_pm_ops unicam_pm_ops = { 2425 RUNTIME_PM_OPS(unicam_runtime_suspend, unicam_runtime_resume, NULL) 2426 }; 2427 2428 /* ----------------------------------------------------------------------------- 2429 * V4L2 async notifier 2430 */ 2431 2432 static int unicam_async_bound(struct v4l2_async_notifier *notifier, 2433 struct v4l2_subdev *subdev, 2434 struct v4l2_async_connection *asc) 2435 { 2436 struct unicam_device *unicam = notifier_to_unicam_device(notifier); 2437 struct media_pad *sink = &unicam->subdev.pads[UNICAM_SD_PAD_SINK]; 2438 struct media_pad *source; 2439 int ret; 2440 2441 dev_dbg(unicam->dev, "Using sensor %s for capture\n", 2442 subdev->name); 2443 2444 ret = v4l2_create_fwnode_links_to_pad(subdev, sink, MEDIA_LNK_FL_ENABLED | 2445 MEDIA_LNK_FL_IMMUTABLE); 2446 if (ret) 2447 return ret; 2448 2449 source = media_pad_remote_pad_unique(sink); 2450 if (IS_ERR(source)) { 2451 dev_err(unicam->dev, "No connected sensor pad\n"); 2452 return PTR_ERR(source); 2453 } 2454 2455 unicam->sensor.subdev = subdev; 2456 unicam->sensor.pad = source; 2457 2458 return 0; 2459 } 2460 2461 static int unicam_async_complete(struct v4l2_async_notifier *notifier) 2462 { 2463 struct unicam_device *unicam = notifier_to_unicam_device(notifier); 2464 int ret; 2465 2466 ret = unicam_register_node(unicam, UNICAM_IMAGE_NODE); 2467 if (ret) { 2468 dev_err(unicam->dev, "Unable to register image video device.\n"); 2469 goto unregister; 2470 } 2471 2472 ret = unicam_register_node(unicam, UNICAM_METADATA_NODE); 2473 if (ret) { 2474 dev_err(unicam->dev, "Unable to register metadata video device.\n"); 2475 goto unregister; 2476 } 2477 2478 ret = v4l2_device_register_subdev_nodes(&unicam->v4l2_dev); 2479 if (ret) { 2480 dev_err(unicam->dev, "Unable to register subdev nodes.\n"); 2481 goto unregister; 2482 } 2483 2484 return 0; 2485 2486 unregister: 2487 unicam_unregister_nodes(unicam); 2488 unicam_put(unicam); 2489 2490 return ret; 2491 } 2492 2493 static const struct v4l2_async_notifier_operations unicam_async_ops = { 2494 .bound = unicam_async_bound, 2495 .complete = unicam_async_complete, 2496 }; 2497 2498 static int unicam_async_nf_init(struct unicam_device *unicam) 2499 { 2500 struct v4l2_fwnode_endpoint ep = { }; 2501 struct fwnode_handle *ep_handle; 2502 struct v4l2_async_connection *asc; 2503 int ret; 2504 2505 ret = of_property_read_u32(unicam->dev->of_node, "brcm,num-data-lanes", 2506 &unicam->max_data_lanes); 2507 if (ret < 0) { 2508 dev_err(unicam->dev, "Missing %s DT property\n", 2509 "brcm,num-data-lanes"); 2510 return -EINVAL; 2511 } 2512 2513 /* Get and parse the local endpoint. */ 2514 ep_handle = fwnode_graph_get_endpoint_by_id(dev_fwnode(unicam->dev), 0, 0, 2515 FWNODE_GRAPH_ENDPOINT_NEXT); 2516 if (!ep_handle) { 2517 dev_err(unicam->dev, "No endpoint found\n"); 2518 return -ENODEV; 2519 } 2520 2521 ret = v4l2_fwnode_endpoint_parse(ep_handle, &ep); 2522 if (ret) { 2523 dev_err(unicam->dev, "Failed to parse endpoint: %d\n", ret); 2524 goto error; 2525 } 2526 2527 unicam->bus_type = ep.bus_type; 2528 2529 switch (ep.bus_type) { 2530 case V4L2_MBUS_CSI2_DPHY: { 2531 unsigned int num_data_lanes = ep.bus.mipi_csi2.num_data_lanes; 2532 2533 if (num_data_lanes != 1 && num_data_lanes != 2 && 2534 num_data_lanes != 4) { 2535 dev_err(unicam->dev, "%u data lanes not supported\n", 2536 num_data_lanes); 2537 ret = -EINVAL; 2538 goto error; 2539 } 2540 2541 if (num_data_lanes > unicam->max_data_lanes) { 2542 dev_err(unicam->dev, 2543 "Endpoint uses %u data lanes when %u are supported\n", 2544 num_data_lanes, unicam->max_data_lanes); 2545 ret = -EINVAL; 2546 goto error; 2547 } 2548 2549 unicam->max_data_lanes = num_data_lanes; 2550 unicam->bus_flags = ep.bus.mipi_csi2.flags; 2551 break; 2552 } 2553 2554 case V4L2_MBUS_CCP2: 2555 unicam->max_data_lanes = 1; 2556 unicam->bus_flags = ep.bus.mipi_csi1.strobe; 2557 break; 2558 2559 default: 2560 /* Unsupported bus type */ 2561 dev_err(unicam->dev, "Unsupported bus type %u\n", ep.bus_type); 2562 ret = -EINVAL; 2563 goto error; 2564 } 2565 2566 /* Initialize and register the async notifier. */ 2567 v4l2_async_nf_init(&unicam->notifier, &unicam->v4l2_dev); 2568 2569 asc = v4l2_async_nf_add_fwnode_remote(&unicam->notifier, ep_handle, 2570 struct v4l2_async_connection); 2571 fwnode_handle_put(ep_handle); 2572 ep_handle = NULL; 2573 2574 if (IS_ERR(asc)) { 2575 ret = PTR_ERR(asc); 2576 dev_err(unicam->dev, "Failed to add entry to notifier: %d\n", 2577 ret); 2578 goto error; 2579 } 2580 2581 unicam->notifier.ops = &unicam_async_ops; 2582 2583 ret = v4l2_async_nf_register(&unicam->notifier); 2584 if (ret) { 2585 dev_err(unicam->dev, "Error registering device notifier: %d\n", 2586 ret); 2587 goto error; 2588 } 2589 2590 return 0; 2591 2592 error: 2593 fwnode_handle_put(ep_handle); 2594 return ret; 2595 } 2596 2597 /* ----------------------------------------------------------------------------- 2598 * Probe & remove 2599 */ 2600 2601 static int unicam_media_init(struct unicam_device *unicam) 2602 { 2603 int ret; 2604 2605 unicam->mdev.dev = unicam->dev; 2606 strscpy(unicam->mdev.model, UNICAM_MODULE_NAME, 2607 sizeof(unicam->mdev.model)); 2608 unicam->mdev.hw_revision = 0; 2609 2610 media_device_init(&unicam->mdev); 2611 2612 unicam->v4l2_dev.mdev = &unicam->mdev; 2613 2614 ret = v4l2_device_register(unicam->dev, &unicam->v4l2_dev); 2615 if (ret < 0) { 2616 dev_err(unicam->dev, "Unable to register v4l2 device\n"); 2617 goto err_media_cleanup; 2618 } 2619 2620 ret = media_device_register(&unicam->mdev); 2621 if (ret < 0) { 2622 dev_err(unicam->dev, 2623 "Unable to register media-controller device\n"); 2624 goto err_v4l2_unregister; 2625 } 2626 2627 return 0; 2628 2629 err_v4l2_unregister: 2630 v4l2_device_unregister(&unicam->v4l2_dev); 2631 err_media_cleanup: 2632 media_device_cleanup(&unicam->mdev); 2633 return ret; 2634 } 2635 2636 static int unicam_probe(struct platform_device *pdev) 2637 { 2638 struct unicam_device *unicam; 2639 int ret; 2640 2641 unicam = kzalloc_obj(*unicam); 2642 if (!unicam) 2643 return -ENOMEM; 2644 2645 kref_init(&unicam->kref); 2646 mutex_init(&unicam->lock); 2647 2648 unicam->dev = &pdev->dev; 2649 platform_set_drvdata(pdev, unicam); 2650 2651 unicam->base = devm_platform_ioremap_resource_byname(pdev, "unicam"); 2652 if (IS_ERR(unicam->base)) { 2653 ret = PTR_ERR(unicam->base); 2654 goto err_unicam_put; 2655 } 2656 2657 unicam->clk_gate_base = devm_platform_ioremap_resource_byname(pdev, "cmi"); 2658 if (IS_ERR(unicam->clk_gate_base)) { 2659 ret = PTR_ERR(unicam->clk_gate_base); 2660 goto err_unicam_put; 2661 } 2662 2663 unicam->clock = devm_clk_get(&pdev->dev, "lp"); 2664 if (IS_ERR(unicam->clock)) { 2665 dev_err(unicam->dev, "Failed to get lp clock\n"); 2666 ret = PTR_ERR(unicam->clock); 2667 goto err_unicam_put; 2668 } 2669 2670 unicam->vpu_clock = devm_clk_get(&pdev->dev, "vpu"); 2671 if (IS_ERR(unicam->vpu_clock)) { 2672 dev_err(unicam->dev, "Failed to get vpu clock\n"); 2673 ret = PTR_ERR(unicam->vpu_clock); 2674 goto err_unicam_put; 2675 } 2676 2677 ret = platform_get_irq(pdev, 0); 2678 if (ret < 0) 2679 goto err_unicam_put; 2680 2681 ret = devm_request_irq(&pdev->dev, ret, unicam_isr, 0, 2682 "unicam_capture0", unicam); 2683 if (ret) { 2684 dev_err(&pdev->dev, "Unable to request interrupt\n"); 2685 goto err_unicam_put; 2686 } 2687 2688 /* Enable the block power domain. */ 2689 pm_runtime_enable(&pdev->dev); 2690 2691 ret = unicam_media_init(unicam); 2692 if (ret) 2693 goto err_pm_runtime; 2694 2695 ret = unicam_subdev_init(unicam); 2696 if (ret) 2697 goto err_media_unregister; 2698 2699 ret = unicam_async_nf_init(unicam); 2700 if (ret) 2701 goto err_subdev_unregister; 2702 2703 return 0; 2704 2705 err_subdev_unregister: 2706 unicam_subdev_cleanup(unicam); 2707 err_media_unregister: 2708 media_device_unregister(&unicam->mdev); 2709 err_pm_runtime: 2710 pm_runtime_disable(&pdev->dev); 2711 err_unicam_put: 2712 unicam_put(unicam); 2713 2714 return ret; 2715 } 2716 2717 static void unicam_remove(struct platform_device *pdev) 2718 { 2719 struct unicam_device *unicam = platform_get_drvdata(pdev); 2720 2721 unicam_unregister_nodes(unicam); 2722 v4l2_device_unregister(&unicam->v4l2_dev); 2723 media_device_unregister(&unicam->mdev); 2724 v4l2_async_nf_unregister(&unicam->notifier); 2725 2726 unicam_subdev_cleanup(unicam); 2727 2728 unicam_put(unicam); 2729 2730 pm_runtime_disable(&pdev->dev); 2731 } 2732 2733 static const struct of_device_id unicam_of_match[] = { 2734 { .compatible = "brcm,bcm2835-unicam", }, 2735 { /* sentinel */ }, 2736 }; 2737 MODULE_DEVICE_TABLE(of, unicam_of_match); 2738 2739 static struct platform_driver unicam_driver = { 2740 .probe = unicam_probe, 2741 .remove = unicam_remove, 2742 .driver = { 2743 .name = UNICAM_MODULE_NAME, 2744 .pm = pm_ptr(&unicam_pm_ops), 2745 .of_match_table = unicam_of_match, 2746 }, 2747 }; 2748 2749 module_platform_driver(unicam_driver); 2750 2751 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>"); 2752 MODULE_DESCRIPTION("BCM2835 Unicam driver"); 2753 MODULE_LICENSE("GPL"); 2754