1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Microchip CSI2 Demux Controller (CSI2DC) driver 4 * 5 * Copyright (C) 2018 Microchip Technology, Inc. 6 * 7 * Author: Eugen Hristev <eugen.hristev@microchip.com> 8 * 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/module.h> 13 #include <linux/of_graph.h> 14 #include <linux/platform_device.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/videodev2.h> 17 18 #include <media/v4l2-fwnode.h> 19 #include <media/v4l2-subdev.h> 20 21 /* Global configuration register */ 22 #define CSI2DC_GCFG 0x0 23 24 /* MIPI sensor pixel clock is free running */ 25 #define CSI2DC_GCFG_MIPIFRN BIT(0) 26 /* GPIO parallel interface selection */ 27 #define CSI2DC_GCFG_GPIOSEL BIT(1) 28 /* Output waveform inter-line minimum delay */ 29 #define CSI2DC_GCFG_HLC(v) ((v) << 4) 30 #define CSI2DC_GCFG_HLC_MASK GENMASK(7, 4) 31 /* SAMA7G5 requires a HLC delay of 15 */ 32 #define SAMA7G5_HLC (15) 33 34 /* Global control register */ 35 #define CSI2DC_GCTLR 0x04 36 #define CSI2DC_GCTLR_SWRST BIT(0) 37 38 /* Global status register */ 39 #define CSI2DC_GS 0x08 40 41 /* SSP interrupt status register */ 42 #define CSI2DC_SSPIS 0x28 43 /* Pipe update register */ 44 #define CSI2DC_PU 0xc0 45 /* Video pipe attributes update */ 46 #define CSI2DC_PU_VP BIT(0) 47 48 /* Pipe update status register */ 49 #define CSI2DC_PUS 0xc4 50 51 /* Video pipeline Interrupt Status Register */ 52 #define CSI2DC_VPISR 0xf4 53 54 /* Video pipeline enable register */ 55 #define CSI2DC_VPE 0xf8 56 #define CSI2DC_VPE_ENABLE BIT(0) 57 58 /* Video pipeline configuration register */ 59 #define CSI2DC_VPCFG 0xfc 60 /* Data type */ 61 #define CSI2DC_VPCFG_DT(v) ((v) << 0) 62 #define CSI2DC_VPCFG_DT_MASK GENMASK(5, 0) 63 /* Virtual channel identifier */ 64 #define CSI2DC_VPCFG_VC(v) ((v) << 6) 65 #define CSI2DC_VPCFG_VC_MASK GENMASK(7, 6) 66 /* Decompression enable */ 67 #define CSI2DC_VPCFG_DE BIT(8) 68 /* Decoder mode */ 69 #define CSI2DC_VPCFG_DM(v) ((v) << 9) 70 #define CSI2DC_VPCFG_DM_DECODER8TO12 0 71 /* Decoder predictor 2 selection */ 72 #define CSI2DC_VPCFG_DP2 BIT(12) 73 /* Recommended memory storage */ 74 #define CSI2DC_VPCFG_RMS BIT(13) 75 /* Post adjustment */ 76 #define CSI2DC_VPCFG_PA BIT(14) 77 78 /* Video pipeline column register */ 79 #define CSI2DC_VPCOL 0x100 80 /* Column number */ 81 #define CSI2DC_VPCOL_COL(v) ((v) << 0) 82 #define CSI2DC_VPCOL_COL_MASK GENMASK(15, 0) 83 84 /* Video pipeline row register */ 85 #define CSI2DC_VPROW 0x104 86 /* Row number */ 87 #define CSI2DC_VPROW_ROW(v) ((v) << 0) 88 #define CSI2DC_VPROW_ROW_MASK GENMASK(15, 0) 89 90 /* Version register */ 91 #define CSI2DC_VERSION 0x1fc 92 93 /* register read/write helpers */ 94 #define csi2dc_readl(st, reg) readl_relaxed((st)->base + (reg)) 95 #define csi2dc_writel(st, reg, val) writel_relaxed((val), \ 96 (st)->base + (reg)) 97 98 /* supported RAW data types */ 99 #define CSI2DC_DT_RAW6 0x28 100 #define CSI2DC_DT_RAW7 0x29 101 #define CSI2DC_DT_RAW8 0x2a 102 #define CSI2DC_DT_RAW10 0x2b 103 #define CSI2DC_DT_RAW12 0x2c 104 #define CSI2DC_DT_RAW14 0x2d 105 /* YUV data types */ 106 #define CSI2DC_DT_YUV422_8B 0x1e 107 108 /* 109 * struct csi2dc_format - CSI2DC format type struct 110 * @mbus_code: Media bus code for the format 111 * @dt: Data type constant for this format 112 */ 113 struct csi2dc_format { 114 u32 mbus_code; 115 u32 dt; 116 }; 117 118 static const struct csi2dc_format csi2dc_formats[] = { 119 { 120 .mbus_code = MEDIA_BUS_FMT_SRGGB8_1X8, 121 .dt = CSI2DC_DT_RAW8, 122 }, { 123 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8, 124 .dt = CSI2DC_DT_RAW8, 125 }, { 126 .mbus_code = MEDIA_BUS_FMT_SGRBG8_1X8, 127 .dt = CSI2DC_DT_RAW8, 128 }, { 129 .mbus_code = MEDIA_BUS_FMT_SGBRG8_1X8, 130 .dt = CSI2DC_DT_RAW8, 131 }, { 132 .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10, 133 .dt = CSI2DC_DT_RAW10, 134 }, { 135 .mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10, 136 .dt = CSI2DC_DT_RAW10, 137 }, { 138 .mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10, 139 .dt = CSI2DC_DT_RAW10, 140 }, { 141 .mbus_code = MEDIA_BUS_FMT_SGBRG10_1X10, 142 .dt = CSI2DC_DT_RAW10, 143 }, { 144 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8, 145 .dt = CSI2DC_DT_YUV422_8B, 146 }, 147 }; 148 149 enum mipi_csi_pads { 150 CSI2DC_PAD_SINK = 0, 151 CSI2DC_PAD_SOURCE = 1, 152 CSI2DC_PADS_NUM = 2, 153 }; 154 155 /* 156 * struct csi2dc_device - CSI2DC device driver data/config struct 157 * @base: Register map base address 158 * @csi2dc_sd: v4l2 subdevice for the csi2dc device 159 * This is the subdevice that the csi2dc device itself 160 * registers in v4l2 subsystem 161 * @dev: struct device for this csi2dc device 162 * @pclk: Peripheral clock reference 163 * Input clock that clocks the hardware block internal 164 * logic 165 * @scck: Sensor Controller clock reference 166 * Input clock that is used to generate the pixel clock 167 * @format: Current saved format used in g/s fmt 168 * @cur_fmt: Current state format 169 * @try_fmt: Try format that is being tried 170 * @pads: Media entity pads for the csi2dc subdevice 171 * @clk_gated: Whether the clock is gated or free running 172 * @video_pipe: Whether video pipeline is configured 173 * @parallel_mode: The underlying subdevice is connected on a parallel bus 174 * @vc: Current set virtual channel 175 * @notifier: Async notifier that is used to bound the underlying 176 * subdevice to the csi2dc subdevice 177 * @input_sd: Reference to the underlying subdevice bound to the 178 * csi2dc subdevice 179 * @remote_pad: Pad number of the underlying subdevice that is linked 180 * to the csi2dc subdevice sink pad. 181 */ 182 struct csi2dc_device { 183 void __iomem *base; 184 struct v4l2_subdev csi2dc_sd; 185 struct device *dev; 186 struct clk *pclk; 187 struct clk *scck; 188 189 struct v4l2_mbus_framefmt format; 190 191 const struct csi2dc_format *cur_fmt; 192 const struct csi2dc_format *try_fmt; 193 194 struct media_pad pads[CSI2DC_PADS_NUM]; 195 196 bool clk_gated; 197 bool video_pipe; 198 bool parallel_mode; 199 u32 vc; 200 201 struct v4l2_async_notifier notifier; 202 203 struct v4l2_subdev *input_sd; 204 205 u32 remote_pad; 206 }; 207 208 static inline struct csi2dc_device * 209 csi2dc_sd_to_csi2dc_device(struct v4l2_subdev *csi2dc_sd) 210 { 211 return container_of(csi2dc_sd, struct csi2dc_device, csi2dc_sd); 212 } 213 214 static int csi2dc_enum_mbus_code(struct v4l2_subdev *csi2dc_sd, 215 struct v4l2_subdev_state *sd_state, 216 struct v4l2_subdev_mbus_code_enum *code) 217 { 218 if (code->index >= ARRAY_SIZE(csi2dc_formats)) 219 return -EINVAL; 220 221 code->code = csi2dc_formats[code->index].mbus_code; 222 223 return 0; 224 } 225 226 static int csi2dc_get_fmt(struct v4l2_subdev *csi2dc_sd, 227 struct v4l2_subdev_state *sd_state, 228 struct v4l2_subdev_format *format) 229 { 230 struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd); 231 struct v4l2_mbus_framefmt *v4l2_try_fmt; 232 233 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 234 v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state, 235 format->pad); 236 format->format = *v4l2_try_fmt; 237 238 return 0; 239 } 240 241 format->format = csi2dc->format; 242 243 return 0; 244 } 245 246 static int csi2dc_set_fmt(struct v4l2_subdev *csi2dc_sd, 247 const struct v4l2_subdev_client_info *ci, 248 struct v4l2_subdev_state *sd_state, 249 struct v4l2_subdev_format *req_fmt) 250 { 251 struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd); 252 const struct csi2dc_format *fmt, *try_fmt = NULL; 253 struct v4l2_mbus_framefmt *v4l2_try_fmt; 254 unsigned int i; 255 256 /* 257 * Setting the source pad is disabled. 258 * The same format is being propagated from the sink to source. 259 */ 260 if (req_fmt->pad == CSI2DC_PAD_SOURCE) 261 return -EINVAL; 262 263 for (i = 0; i < ARRAY_SIZE(csi2dc_formats); i++) { 264 fmt = &csi2dc_formats[i]; 265 if (req_fmt->format.code == fmt->mbus_code) 266 try_fmt = fmt; 267 fmt++; 268 } 269 270 /* in case we could not find the desired format, default to something */ 271 if (!try_fmt) { 272 try_fmt = &csi2dc_formats[0]; 273 274 dev_dbg(csi2dc->dev, 275 "CSI2DC unsupported format 0x%x, defaulting to 0x%x\n", 276 req_fmt->format.code, csi2dc_formats[0].mbus_code); 277 } 278 279 req_fmt->format.code = try_fmt->mbus_code; 280 req_fmt->format.colorspace = V4L2_COLORSPACE_SRGB; 281 req_fmt->format.field = V4L2_FIELD_NONE; 282 283 if (req_fmt->which == V4L2_SUBDEV_FORMAT_TRY) { 284 v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state, 285 req_fmt->pad); 286 *v4l2_try_fmt = req_fmt->format; 287 /* Trying on the sink pad makes the source pad change too */ 288 v4l2_try_fmt = v4l2_subdev_state_get_format(sd_state, 289 CSI2DC_PAD_SOURCE); 290 *v4l2_try_fmt = req_fmt->format; 291 292 /* if we are just trying, we are done */ 293 return 0; 294 } 295 296 /* save the format for later requests */ 297 csi2dc->format = req_fmt->format; 298 299 /* update config */ 300 csi2dc->cur_fmt = try_fmt; 301 302 dev_dbg(csi2dc->dev, "new format set: 0x%x @%dx%d\n", 303 csi2dc->format.code, csi2dc->format.width, 304 csi2dc->format.height); 305 306 return 0; 307 } 308 309 static int csi2dc_power(struct csi2dc_device *csi2dc, int on) 310 { 311 int ret = 0; 312 313 if (on) { 314 ret = clk_prepare_enable(csi2dc->pclk); 315 if (ret) { 316 dev_err(csi2dc->dev, "failed to enable pclk:%d\n", ret); 317 return ret; 318 } 319 320 ret = clk_prepare_enable(csi2dc->scck); 321 if (ret) { 322 dev_err(csi2dc->dev, "failed to enable scck:%d\n", ret); 323 clk_disable_unprepare(csi2dc->pclk); 324 return ret; 325 } 326 327 /* if powering up, deassert reset line */ 328 csi2dc_writel(csi2dc, CSI2DC_GCTLR, CSI2DC_GCTLR_SWRST); 329 } else { 330 /* if powering down, assert reset line */ 331 csi2dc_writel(csi2dc, CSI2DC_GCTLR, 0); 332 333 clk_disable_unprepare(csi2dc->scck); 334 clk_disable_unprepare(csi2dc->pclk); 335 } 336 337 return ret; 338 } 339 340 static int csi2dc_get_mbus_config(struct csi2dc_device *csi2dc) 341 { 342 struct v4l2_mbus_config mbus_config = { 0 }; 343 int ret; 344 345 ret = v4l2_subdev_call(csi2dc->input_sd, pad, get_mbus_config, 346 csi2dc->remote_pad, &mbus_config); 347 if (ret == -ENOIOCTLCMD) { 348 dev_dbg(csi2dc->dev, 349 "no remote mbus configuration available\n"); 350 return 0; 351 } 352 353 if (ret) { 354 dev_err(csi2dc->dev, 355 "failed to get remote mbus configuration\n"); 356 return 0; 357 } 358 359 dev_dbg(csi2dc->dev, "subdev sending on channel %d\n", csi2dc->vc); 360 361 csi2dc->clk_gated = mbus_config.bus.parallel.flags & 362 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; 363 364 dev_dbg(csi2dc->dev, "mbus_config: %s clock\n", 365 csi2dc->clk_gated ? "gated" : "free running"); 366 367 return 0; 368 } 369 370 static void csi2dc_vp_update(struct csi2dc_device *csi2dc) 371 { 372 u32 vp, gcfg; 373 374 if (!csi2dc->video_pipe) { 375 dev_err(csi2dc->dev, "video pipeline unavailable\n"); 376 return; 377 } 378 379 if (csi2dc->parallel_mode) { 380 /* In parallel mode, GPIO parallel interface must be selected */ 381 gcfg = csi2dc_readl(csi2dc, CSI2DC_GCFG); 382 gcfg |= CSI2DC_GCFG_GPIOSEL; 383 csi2dc_writel(csi2dc, CSI2DC_GCFG, gcfg); 384 return; 385 } 386 387 /* serial video pipeline */ 388 389 csi2dc_writel(csi2dc, CSI2DC_GCFG, 390 (SAMA7G5_HLC & CSI2DC_GCFG_HLC_MASK) | 391 (csi2dc->clk_gated ? 0 : CSI2DC_GCFG_MIPIFRN)); 392 393 vp = CSI2DC_VPCFG_DT(csi2dc->cur_fmt->dt) & CSI2DC_VPCFG_DT_MASK; 394 vp |= CSI2DC_VPCFG_VC(csi2dc->vc) & CSI2DC_VPCFG_VC_MASK; 395 vp &= ~CSI2DC_VPCFG_DE; 396 vp |= CSI2DC_VPCFG_DM(CSI2DC_VPCFG_DM_DECODER8TO12); 397 vp &= ~CSI2DC_VPCFG_DP2; 398 vp &= ~CSI2DC_VPCFG_RMS; 399 vp |= CSI2DC_VPCFG_PA; 400 401 csi2dc_writel(csi2dc, CSI2DC_VPCFG, vp); 402 csi2dc_writel(csi2dc, CSI2DC_VPE, CSI2DC_VPE_ENABLE); 403 csi2dc_writel(csi2dc, CSI2DC_PU, CSI2DC_PU_VP); 404 } 405 406 static int csi2dc_s_stream(struct v4l2_subdev *csi2dc_sd, int enable) 407 { 408 struct csi2dc_device *csi2dc = csi2dc_sd_to_csi2dc_device(csi2dc_sd); 409 int ret; 410 411 if (enable) { 412 ret = pm_runtime_resume_and_get(csi2dc->dev); 413 if (ret < 0) 414 return ret; 415 416 csi2dc_get_mbus_config(csi2dc); 417 418 csi2dc_vp_update(csi2dc); 419 420 return v4l2_subdev_call(csi2dc->input_sd, video, s_stream, 421 true); 422 } 423 424 dev_dbg(csi2dc->dev, 425 "Last frame received: VPCOLR = %u, VPROWR= %u, VPISR = %x\n", 426 csi2dc_readl(csi2dc, CSI2DC_VPCOL), 427 csi2dc_readl(csi2dc, CSI2DC_VPROW), 428 csi2dc_readl(csi2dc, CSI2DC_VPISR)); 429 430 /* stop streaming scenario */ 431 ret = v4l2_subdev_call(csi2dc->input_sd, video, s_stream, false); 432 433 pm_runtime_put_sync(csi2dc->dev); 434 435 return ret; 436 } 437 438 static int csi2dc_init_state(struct v4l2_subdev *csi2dc_sd, 439 struct v4l2_subdev_state *sd_state) 440 { 441 struct v4l2_mbus_framefmt *v4l2_try_fmt = 442 v4l2_subdev_state_get_format(sd_state, 0); 443 444 v4l2_try_fmt->height = 480; 445 v4l2_try_fmt->width = 640; 446 v4l2_try_fmt->code = csi2dc_formats[0].mbus_code; 447 v4l2_try_fmt->colorspace = V4L2_COLORSPACE_SRGB; 448 v4l2_try_fmt->field = V4L2_FIELD_NONE; 449 v4l2_try_fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 450 v4l2_try_fmt->quantization = V4L2_QUANTIZATION_DEFAULT; 451 v4l2_try_fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT; 452 453 return 0; 454 } 455 456 static const struct media_entity_operations csi2dc_entity_ops = { 457 .link_validate = v4l2_subdev_link_validate, 458 }; 459 460 static const struct v4l2_subdev_pad_ops csi2dc_pad_ops = { 461 .enum_mbus_code = csi2dc_enum_mbus_code, 462 .set_fmt = csi2dc_set_fmt, 463 .get_fmt = csi2dc_get_fmt, 464 }; 465 466 static const struct v4l2_subdev_video_ops csi2dc_video_ops = { 467 .s_stream = csi2dc_s_stream, 468 }; 469 470 static const struct v4l2_subdev_ops csi2dc_subdev_ops = { 471 .pad = &csi2dc_pad_ops, 472 .video = &csi2dc_video_ops, 473 }; 474 475 static const struct v4l2_subdev_internal_ops csi2dc_internal_ops = { 476 .init_state = csi2dc_init_state, 477 }; 478 479 static int csi2dc_async_bound(struct v4l2_async_notifier *notifier, 480 struct v4l2_subdev *subdev, 481 struct v4l2_async_connection *asd) 482 { 483 struct csi2dc_device *csi2dc = container_of(notifier, 484 struct csi2dc_device, notifier); 485 int pad; 486 int ret; 487 488 csi2dc->input_sd = subdev; 489 490 pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode, 491 MEDIA_PAD_FL_SOURCE); 492 if (pad < 0) { 493 dev_err(csi2dc->dev, "Failed to find pad for %s\n", 494 subdev->name); 495 return pad; 496 } 497 498 csi2dc->remote_pad = pad; 499 500 ret = media_create_pad_link(&csi2dc->input_sd->entity, 501 csi2dc->remote_pad, 502 &csi2dc->csi2dc_sd.entity, 0, 503 MEDIA_LNK_FL_ENABLED); 504 if (ret) { 505 dev_err(csi2dc->dev, 506 "Failed to create pad link: %s to %s\n", 507 csi2dc->input_sd->entity.name, 508 csi2dc->csi2dc_sd.entity.name); 509 return ret; 510 } 511 512 dev_dbg(csi2dc->dev, "link with %s pad: %d\n", 513 csi2dc->input_sd->name, csi2dc->remote_pad); 514 515 return ret; 516 } 517 518 static const struct v4l2_async_notifier_operations csi2dc_async_ops = { 519 .bound = csi2dc_async_bound, 520 }; 521 522 static int csi2dc_prepare_notifier(struct csi2dc_device *csi2dc, 523 struct fwnode_handle *input_fwnode) 524 { 525 struct v4l2_async_connection *asd; 526 int ret = 0; 527 528 v4l2_async_subdev_nf_init(&csi2dc->notifier, &csi2dc->csi2dc_sd); 529 530 asd = v4l2_async_nf_add_fwnode_remote(&csi2dc->notifier, 531 input_fwnode, 532 struct v4l2_async_connection); 533 534 fwnode_handle_put(input_fwnode); 535 536 if (IS_ERR(asd)) { 537 ret = PTR_ERR(asd); 538 dev_err(csi2dc->dev, 539 "failed to add async notifier for node %pOF: %d\n", 540 to_of_node(input_fwnode), ret); 541 v4l2_async_nf_cleanup(&csi2dc->notifier); 542 return ret; 543 } 544 545 csi2dc->notifier.ops = &csi2dc_async_ops; 546 547 ret = v4l2_async_nf_register(&csi2dc->notifier); 548 if (ret) { 549 dev_err(csi2dc->dev, "fail to register async notifier: %d\n", 550 ret); 551 v4l2_async_nf_cleanup(&csi2dc->notifier); 552 } 553 554 return ret; 555 } 556 557 static int csi2dc_of_parse(struct csi2dc_device *csi2dc, 558 struct device_node *of_node) 559 { 560 struct fwnode_handle *input_fwnode, *output_fwnode; 561 struct v4l2_fwnode_endpoint input_endpoint = { 0 }, 562 output_endpoint = { 0 }; 563 int ret; 564 565 input_fwnode = fwnode_graph_get_next_endpoint(of_fwnode_handle(of_node), 566 NULL); 567 if (!input_fwnode) { 568 dev_err(csi2dc->dev, 569 "missing port node at %pOF, input node is mandatory.\n", 570 of_node); 571 return -EINVAL; 572 } 573 574 ret = v4l2_fwnode_endpoint_parse(input_fwnode, &input_endpoint); 575 if (ret) { 576 dev_err(csi2dc->dev, "endpoint not defined at %pOF\n", of_node); 577 goto csi2dc_of_parse_err; 578 } 579 580 if (input_endpoint.bus_type == V4L2_MBUS_PARALLEL || 581 input_endpoint.bus_type == V4L2_MBUS_BT656) { 582 csi2dc->parallel_mode = true; 583 dev_dbg(csi2dc->dev, 584 "subdevice connected on parallel interface\n"); 585 } 586 587 if (input_endpoint.bus_type == V4L2_MBUS_CSI2_DPHY) { 588 csi2dc->clk_gated = input_endpoint.bus.mipi_csi2.flags & 589 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK; 590 dev_dbg(csi2dc->dev, 591 "subdevice connected on serial interface\n"); 592 dev_dbg(csi2dc->dev, "DT: %s clock\n", 593 csi2dc->clk_gated ? "gated" : "free running"); 594 } 595 596 output_fwnode = fwnode_graph_get_next_endpoint 597 (of_fwnode_handle(of_node), input_fwnode); 598 599 if (output_fwnode) 600 ret = v4l2_fwnode_endpoint_parse(output_fwnode, 601 &output_endpoint); 602 603 fwnode_handle_put(output_fwnode); 604 605 if (!output_fwnode || ret) { 606 dev_info(csi2dc->dev, 607 "missing output node at %pOF, data pipe available only.\n", 608 of_node); 609 } else { 610 if (output_endpoint.bus_type != V4L2_MBUS_PARALLEL && 611 output_endpoint.bus_type != V4L2_MBUS_BT656) { 612 dev_err(csi2dc->dev, 613 "output port must be parallel/bt656.\n"); 614 ret = -EINVAL; 615 goto csi2dc_of_parse_err; 616 } 617 618 csi2dc->video_pipe = true; 619 620 dev_dbg(csi2dc->dev, 621 "block %pOF [%d.%d]->[%d.%d] video pipeline\n", 622 of_node, input_endpoint.base.port, 623 input_endpoint.base.id, output_endpoint.base.port, 624 output_endpoint.base.id); 625 } 626 627 /* prepare async notifier for subdevice completion */ 628 return csi2dc_prepare_notifier(csi2dc, input_fwnode); 629 630 csi2dc_of_parse_err: 631 fwnode_handle_put(input_fwnode); 632 return ret; 633 } 634 635 static void csi2dc_default_format(struct csi2dc_device *csi2dc) 636 { 637 csi2dc->cur_fmt = &csi2dc_formats[0]; 638 639 csi2dc->format.height = 480; 640 csi2dc->format.width = 640; 641 csi2dc->format.code = csi2dc_formats[0].mbus_code; 642 csi2dc->format.colorspace = V4L2_COLORSPACE_SRGB; 643 csi2dc->format.field = V4L2_FIELD_NONE; 644 csi2dc->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 645 csi2dc->format.quantization = V4L2_QUANTIZATION_DEFAULT; 646 csi2dc->format.xfer_func = V4L2_XFER_FUNC_DEFAULT; 647 } 648 649 static int csi2dc_probe(struct platform_device *pdev) 650 { 651 struct device *dev = &pdev->dev; 652 struct csi2dc_device *csi2dc; 653 int ret = 0; 654 u32 ver; 655 656 csi2dc = devm_kzalloc(dev, sizeof(*csi2dc), GFP_KERNEL); 657 if (!csi2dc) 658 return -ENOMEM; 659 660 csi2dc->dev = dev; 661 662 csi2dc->base = devm_platform_ioremap_resource(pdev, 0); 663 if (IS_ERR(csi2dc->base)) { 664 dev_err(dev, "base address not set\n"); 665 return PTR_ERR(csi2dc->base); 666 } 667 668 csi2dc->pclk = devm_clk_get(dev, "pclk"); 669 if (IS_ERR(csi2dc->pclk)) { 670 ret = PTR_ERR(csi2dc->pclk); 671 dev_err(dev, "failed to get pclk: %d\n", ret); 672 return ret; 673 } 674 675 csi2dc->scck = devm_clk_get(dev, "scck"); 676 if (IS_ERR(csi2dc->scck)) { 677 ret = PTR_ERR(csi2dc->scck); 678 dev_err(dev, "failed to get scck: %d\n", ret); 679 return ret; 680 } 681 682 v4l2_subdev_init(&csi2dc->csi2dc_sd, &csi2dc_subdev_ops); 683 csi2dc->csi2dc_sd.internal_ops = &csi2dc_internal_ops; 684 685 csi2dc->csi2dc_sd.owner = THIS_MODULE; 686 csi2dc->csi2dc_sd.dev = dev; 687 snprintf(csi2dc->csi2dc_sd.name, sizeof(csi2dc->csi2dc_sd.name), 688 "csi2dc"); 689 690 csi2dc->csi2dc_sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 691 csi2dc->csi2dc_sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 692 csi2dc->csi2dc_sd.entity.ops = &csi2dc_entity_ops; 693 694 platform_set_drvdata(pdev, csi2dc); 695 696 ret = csi2dc_of_parse(csi2dc, dev->of_node); 697 if (ret) 698 goto csi2dc_probe_cleanup_entity; 699 700 csi2dc->pads[CSI2DC_PAD_SINK].flags = MEDIA_PAD_FL_SINK; 701 if (csi2dc->video_pipe) 702 csi2dc->pads[CSI2DC_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE; 703 704 ret = media_entity_pads_init(&csi2dc->csi2dc_sd.entity, 705 csi2dc->video_pipe ? CSI2DC_PADS_NUM : 1, 706 csi2dc->pads); 707 if (ret < 0) { 708 dev_err(dev, "media entity init failed\n"); 709 goto csi2dc_probe_cleanup_notifier; 710 } 711 712 csi2dc_default_format(csi2dc); 713 714 /* turn power on to validate capabilities */ 715 ret = csi2dc_power(csi2dc, true); 716 if (ret < 0) 717 goto csi2dc_probe_cleanup_notifier; 718 719 pm_runtime_set_active(dev); 720 pm_runtime_enable(dev); 721 ver = csi2dc_readl(csi2dc, CSI2DC_VERSION); 722 723 /* 724 * we must register the subdev after PM runtime has been requested, 725 * otherwise we might bound immediately and request pm_runtime_resume 726 * before runtime_enable. 727 */ 728 ret = v4l2_async_register_subdev(&csi2dc->csi2dc_sd); 729 if (ret) { 730 dev_err(csi2dc->dev, "failed to register the subdevice\n"); 731 goto csi2dc_probe_cleanup_notifier; 732 } 733 734 dev_info(dev, "Microchip CSI2DC version %x\n", ver); 735 736 return 0; 737 738 csi2dc_probe_cleanup_notifier: 739 v4l2_async_nf_unregister(&csi2dc->notifier); 740 v4l2_async_nf_cleanup(&csi2dc->notifier); 741 csi2dc_probe_cleanup_entity: 742 media_entity_cleanup(&csi2dc->csi2dc_sd.entity); 743 744 return ret; 745 } 746 747 static void csi2dc_remove(struct platform_device *pdev) 748 { 749 struct csi2dc_device *csi2dc = platform_get_drvdata(pdev); 750 751 pm_runtime_disable(&pdev->dev); 752 753 v4l2_async_unregister_subdev(&csi2dc->csi2dc_sd); 754 v4l2_async_nf_unregister(&csi2dc->notifier); 755 v4l2_async_nf_cleanup(&csi2dc->notifier); 756 media_entity_cleanup(&csi2dc->csi2dc_sd.entity); 757 } 758 759 static int __maybe_unused csi2dc_runtime_suspend(struct device *dev) 760 { 761 struct csi2dc_device *csi2dc = dev_get_drvdata(dev); 762 763 return csi2dc_power(csi2dc, false); 764 } 765 766 static int __maybe_unused csi2dc_runtime_resume(struct device *dev) 767 { 768 struct csi2dc_device *csi2dc = dev_get_drvdata(dev); 769 770 return csi2dc_power(csi2dc, true); 771 } 772 773 static const struct dev_pm_ops csi2dc_dev_pm_ops = { 774 SET_RUNTIME_PM_OPS(csi2dc_runtime_suspend, csi2dc_runtime_resume, NULL) 775 }; 776 777 static const struct of_device_id csi2dc_of_match[] = { 778 { .compatible = "microchip,sama7g5-csi2dc" }, 779 { } 780 }; 781 782 MODULE_DEVICE_TABLE(of, csi2dc_of_match); 783 784 static struct platform_driver csi2dc_driver = { 785 .probe = csi2dc_probe, 786 .remove = csi2dc_remove, 787 .driver = { 788 .name = "microchip-csi2dc", 789 .pm = &csi2dc_dev_pm_ops, 790 .of_match_table = of_match_ptr(csi2dc_of_match), 791 }, 792 }; 793 794 module_platform_driver(csi2dc_driver); 795 796 MODULE_AUTHOR("Eugen Hristev <eugen.hristev@microchip.com>"); 797 MODULE_DESCRIPTION("Microchip CSI2 Demux Controller driver"); 798 MODULE_LICENSE("GPL v2"); 799