1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2021 Renesas Electronics Corp. 4 * 5 * Driver for Renesas R-Car ISP Channel Selector 6 * 7 * The ISP hardware is capable of more than just channel selection, features 8 * such as demosaicing, white balance control and color space conversion are 9 * also possible. These more advanced features are not supported by the driver 10 * due to lack of documentation. 11 */ 12 13 #include <linux/module.h> 14 #include <linux/mutex.h> 15 #include <linux/of.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/reset.h> 19 20 #include <media/mipi-csi2.h> 21 #include <media/v4l2-subdev.h> 22 23 #define ISPINPUTSEL0_REG 0x0008 24 #define ISPINPUTSEL0_SEL_CSI0 BIT(31) 25 26 #define ISPSTART_REG 0x0014 27 #define ISPSTART_START 0xffff 28 #define ISPSTART_STOP 0x0000 29 30 #define ISPPROCMODE_DT_REG(n) (0x1100 + (0x4 * (n))) 31 #define ISPPROCMODE_DT_PROC_MODE_VCn(vc, pm) (((pm) & 0x3f) << (8 * (vc))) 32 33 #define ISPCS_FILTER_ID_CH_REG(n) (0x3000 + (0x0100 * (n))) 34 35 #define ISPCS_DT_CODE03_CH_REG(n) (0x3008 + (0x100 * (n))) 36 #define ISPCS_DT_CODE03_EN3 BIT(31) 37 #define ISPCS_DT_CODE03_DT3(dt) (((dt) & 0x3f) << 24) 38 #define ISPCS_DT_CODE03_EN2 BIT(23) 39 #define ISPCS_DT_CODE03_DT2(dt) (((dt) & 0x3f) << 16) 40 #define ISPCS_DT_CODE03_EN1 BIT(15) 41 #define ISPCS_DT_CODE03_DT1(dt) (((dt) & 0x3f) << 8) 42 #define ISPCS_DT_CODE03_EN0 BIT(7) 43 #define ISPCS_DT_CODE03_DT0(dt) ((dt) & 0x3f) 44 45 struct rcar_isp_format { 46 u32 code; 47 unsigned int datatype; 48 unsigned int procmode; 49 }; 50 51 static const struct rcar_isp_format rcar_isp_formats[] = { 52 { 53 .code = MEDIA_BUS_FMT_RGB888_1X24, 54 .datatype = MIPI_CSI2_DT_RGB888, 55 .procmode = 0x15 56 }, { 57 .code = MEDIA_BUS_FMT_Y10_1X10, 58 .datatype = MIPI_CSI2_DT_RAW10, 59 .procmode = 0x10, 60 }, { 61 .code = MEDIA_BUS_FMT_UYVY8_1X16, 62 .datatype = MIPI_CSI2_DT_YUV422_8B, 63 .procmode = 0x0c, 64 }, { 65 .code = MEDIA_BUS_FMT_YUYV8_1X16, 66 .datatype = MIPI_CSI2_DT_YUV422_8B, 67 .procmode = 0x0c, 68 }, { 69 .code = MEDIA_BUS_FMT_UYVY8_2X8, 70 .datatype = MIPI_CSI2_DT_YUV422_8B, 71 .procmode = 0x0c, 72 }, { 73 .code = MEDIA_BUS_FMT_YUYV10_2X10, 74 .datatype = MIPI_CSI2_DT_YUV422_8B, 75 .procmode = 0x0c, 76 }, { 77 .code = MEDIA_BUS_FMT_SBGGR8_1X8, 78 .datatype = MIPI_CSI2_DT_RAW8, 79 .procmode = 0x00, 80 }, { 81 .code = MEDIA_BUS_FMT_SGBRG8_1X8, 82 .datatype = MIPI_CSI2_DT_RAW8, 83 .procmode = 0x00, 84 }, { 85 .code = MEDIA_BUS_FMT_SGRBG8_1X8, 86 .datatype = MIPI_CSI2_DT_RAW8, 87 .procmode = 0x00, 88 }, { 89 .code = MEDIA_BUS_FMT_SRGGB8_1X8, 90 .datatype = MIPI_CSI2_DT_RAW8, 91 .procmode = 0x00, 92 }, { 93 .code = MEDIA_BUS_FMT_SBGGR10_1X10, 94 .datatype = MIPI_CSI2_DT_RAW10, 95 .procmode = 0x01, 96 }, { 97 .code = MEDIA_BUS_FMT_SGBRG10_1X10, 98 .datatype = MIPI_CSI2_DT_RAW10, 99 .procmode = 0x01, 100 }, { 101 .code = MEDIA_BUS_FMT_SGRBG10_1X10, 102 .datatype = MIPI_CSI2_DT_RAW10, 103 .procmode = 0x01, 104 }, { 105 .code = MEDIA_BUS_FMT_SRGGB10_1X10, 106 .datatype = MIPI_CSI2_DT_RAW10, 107 .procmode = 0x01, 108 }, { 109 .code = MEDIA_BUS_FMT_SBGGR12_1X12, 110 .datatype = MIPI_CSI2_DT_RAW12, 111 .procmode = 0x02, 112 }, { 113 .code = MEDIA_BUS_FMT_SGBRG12_1X12, 114 .datatype = MIPI_CSI2_DT_RAW12, 115 .procmode = 0x02, 116 }, { 117 .code = MEDIA_BUS_FMT_SGRBG12_1X12, 118 .datatype = MIPI_CSI2_DT_RAW12, 119 .procmode = 0x02, 120 }, { 121 .code = MEDIA_BUS_FMT_SRGGB12_1X12, 122 .datatype = MIPI_CSI2_DT_RAW12, 123 .procmode = 0x02, 124 }, 125 }; 126 127 static const struct rcar_isp_format *risp_code_to_fmt(unsigned int code) 128 { 129 unsigned int i; 130 131 for (i = 0; i < ARRAY_SIZE(rcar_isp_formats); i++) { 132 if (rcar_isp_formats[i].code == code) 133 return &rcar_isp_formats[i]; 134 } 135 136 return NULL; 137 } 138 139 enum rcar_isp_input { 140 RISP_CSI_INPUT0, 141 RISP_CSI_INPUT1, 142 }; 143 144 enum rcar_isp_pads { 145 RCAR_ISP_SINK, 146 RCAR_ISP_PORT0, 147 RCAR_ISP_PORT1, 148 RCAR_ISP_PORT2, 149 RCAR_ISP_PORT3, 150 RCAR_ISP_PORT4, 151 RCAR_ISP_PORT5, 152 RCAR_ISP_PORT6, 153 RCAR_ISP_PORT7, 154 RCAR_ISP_NUM_PADS, 155 }; 156 157 struct rcar_isp { 158 struct device *dev; 159 void __iomem *csbase; 160 struct reset_control *rstc; 161 162 enum rcar_isp_input csi_input; 163 164 struct v4l2_subdev subdev; 165 struct media_pad pads[RCAR_ISP_NUM_PADS]; 166 167 struct v4l2_async_notifier notifier; 168 struct v4l2_subdev *remote; 169 unsigned int remote_pad; 170 171 int stream_count; 172 }; 173 174 static inline struct rcar_isp *sd_to_isp(struct v4l2_subdev *sd) 175 { 176 return container_of(sd, struct rcar_isp, subdev); 177 } 178 179 static inline struct rcar_isp *notifier_to_isp(struct v4l2_async_notifier *n) 180 { 181 return container_of(n, struct rcar_isp, notifier); 182 } 183 184 static void risp_write_cs(struct rcar_isp *isp, u32 offset, u32 value) 185 { 186 iowrite32(value, isp->csbase + offset); 187 } 188 189 static u32 risp_read_cs(struct rcar_isp *isp, u32 offset) 190 { 191 return ioread32(isp->csbase + offset); 192 } 193 194 static int risp_power_on(struct rcar_isp *isp) 195 { 196 int ret; 197 198 ret = pm_runtime_resume_and_get(isp->dev); 199 if (ret < 0) 200 return ret; 201 202 ret = reset_control_deassert(isp->rstc); 203 if (ret < 0) { 204 pm_runtime_put(isp->dev); 205 return ret; 206 } 207 208 return 0; 209 } 210 211 static void risp_power_off(struct rcar_isp *isp) 212 { 213 reset_control_assert(isp->rstc); 214 pm_runtime_put(isp->dev); 215 } 216 217 static int risp_start(struct rcar_isp *isp, struct v4l2_subdev_state *state) 218 { 219 const struct v4l2_mbus_framefmt *fmt; 220 const struct rcar_isp_format *format; 221 unsigned int vc; 222 u32 sel_csi = 0; 223 int ret; 224 225 fmt = v4l2_subdev_state_get_format(state, RCAR_ISP_SINK); 226 if (!fmt) 227 return -EINVAL; 228 229 format = risp_code_to_fmt(fmt->code); 230 if (!format) { 231 dev_err(isp->dev, "Unsupported bus format\n"); 232 return -EINVAL; 233 } 234 235 ret = risp_power_on(isp); 236 if (ret) { 237 dev_err(isp->dev, "Failed to power on ISP\n"); 238 return ret; 239 } 240 241 /* Select CSI-2 input source. */ 242 if (isp->csi_input == RISP_CSI_INPUT1) 243 sel_csi = ISPINPUTSEL0_SEL_CSI0; 244 245 risp_write_cs(isp, ISPINPUTSEL0_REG, 246 risp_read_cs(isp, ISPINPUTSEL0_REG) | sel_csi); 247 248 /* Configure Channel Selector. */ 249 for (vc = 0; vc < 4; vc++) { 250 u8 ch = vc + 4; 251 u8 dt = format->datatype; 252 253 risp_write_cs(isp, ISPCS_FILTER_ID_CH_REG(ch), BIT(vc)); 254 risp_write_cs(isp, ISPCS_DT_CODE03_CH_REG(ch), 255 ISPCS_DT_CODE03_EN3 | ISPCS_DT_CODE03_DT3(dt) | 256 ISPCS_DT_CODE03_EN2 | ISPCS_DT_CODE03_DT2(dt) | 257 ISPCS_DT_CODE03_EN1 | ISPCS_DT_CODE03_DT1(dt) | 258 ISPCS_DT_CODE03_EN0 | ISPCS_DT_CODE03_DT0(dt)); 259 } 260 261 /* Setup processing method. */ 262 risp_write_cs(isp, ISPPROCMODE_DT_REG(format->datatype), 263 ISPPROCMODE_DT_PROC_MODE_VCn(3, format->procmode) | 264 ISPPROCMODE_DT_PROC_MODE_VCn(2, format->procmode) | 265 ISPPROCMODE_DT_PROC_MODE_VCn(1, format->procmode) | 266 ISPPROCMODE_DT_PROC_MODE_VCn(0, format->procmode)); 267 268 /* Start ISP. */ 269 risp_write_cs(isp, ISPSTART_REG, ISPSTART_START); 270 271 ret = v4l2_subdev_enable_streams(isp->remote, isp->remote_pad, 272 BIT_ULL(0)); 273 if (ret) 274 risp_power_off(isp); 275 276 return ret; 277 } 278 279 static void risp_stop(struct rcar_isp *isp) 280 { 281 v4l2_subdev_disable_streams(isp->remote, isp->remote_pad, BIT_ULL(0)); 282 283 /* Stop ISP. */ 284 risp_write_cs(isp, ISPSTART_REG, ISPSTART_STOP); 285 286 risp_power_off(isp); 287 } 288 289 static int risp_enable_streams(struct v4l2_subdev *sd, 290 struct v4l2_subdev_state *state, u32 source_pad, 291 u64 source_streams_mask) 292 { 293 struct rcar_isp *isp = sd_to_isp(sd); 294 int ret = 0; 295 296 if (source_streams_mask != 1) 297 return -EINVAL; 298 299 if (!isp->remote) 300 return -ENODEV; 301 302 if (isp->stream_count == 0) { 303 ret = risp_start(isp, state); 304 if (ret) 305 return ret; 306 } 307 308 isp->stream_count += 1; 309 310 return ret; 311 } 312 313 static int risp_disable_streams(struct v4l2_subdev *sd, 314 struct v4l2_subdev_state *state, u32 source_pad, 315 u64 source_streams_mask) 316 { 317 struct rcar_isp *isp = sd_to_isp(sd); 318 319 if (source_streams_mask != 1) 320 return -EINVAL; 321 322 if (!isp->remote) 323 return -ENODEV; 324 325 if (isp->stream_count == 1) 326 risp_stop(isp); 327 328 isp->stream_count -= 1; 329 330 return 0; 331 } 332 333 static int risp_set_pad_format(struct v4l2_subdev *sd, 334 struct v4l2_subdev_state *state, 335 struct v4l2_subdev_format *format) 336 { 337 struct v4l2_mbus_framefmt *framefmt; 338 339 if (format->pad > RCAR_ISP_SINK) 340 return v4l2_subdev_get_fmt(sd, state, format); 341 342 if (!risp_code_to_fmt(format->format.code)) 343 format->format.code = rcar_isp_formats[0].code; 344 345 for (unsigned int i = 0; i < RCAR_ISP_NUM_PADS; i++) { 346 framefmt = v4l2_subdev_state_get_format(state, i); 347 *framefmt = format->format; 348 } 349 350 return 0; 351 } 352 353 static const struct v4l2_subdev_pad_ops risp_pad_ops = { 354 .enable_streams = risp_enable_streams, 355 .disable_streams = risp_disable_streams, 356 .set_fmt = risp_set_pad_format, 357 .get_fmt = v4l2_subdev_get_fmt, 358 .link_validate = v4l2_subdev_link_validate_default, 359 }; 360 361 static const struct v4l2_subdev_ops rcar_isp_subdev_ops = { 362 .pad = &risp_pad_ops, 363 }; 364 365 /* ----------------------------------------------------------------------------- 366 * Async handling and registration of subdevices and links 367 */ 368 369 static int risp_notify_bound(struct v4l2_async_notifier *notifier, 370 struct v4l2_subdev *subdev, 371 struct v4l2_async_connection *asd) 372 { 373 struct rcar_isp *isp = notifier_to_isp(notifier); 374 int pad; 375 376 pad = media_entity_get_fwnode_pad(&subdev->entity, asd->match.fwnode, 377 MEDIA_PAD_FL_SOURCE); 378 if (pad < 0) { 379 dev_err(isp->dev, "Failed to find pad for %s\n", subdev->name); 380 return pad; 381 } 382 383 isp->remote = subdev; 384 isp->remote_pad = pad; 385 386 dev_dbg(isp->dev, "Bound %s pad: %d\n", subdev->name, pad); 387 388 return media_create_pad_link(&subdev->entity, pad, 389 &isp->subdev.entity, 0, 390 MEDIA_LNK_FL_ENABLED | 391 MEDIA_LNK_FL_IMMUTABLE); 392 } 393 394 static void risp_notify_unbind(struct v4l2_async_notifier *notifier, 395 struct v4l2_subdev *subdev, 396 struct v4l2_async_connection *asd) 397 { 398 struct rcar_isp *isp = notifier_to_isp(notifier); 399 400 isp->remote = NULL; 401 402 dev_dbg(isp->dev, "Unbind %s\n", subdev->name); 403 } 404 405 static const struct v4l2_async_notifier_operations risp_notify_ops = { 406 .bound = risp_notify_bound, 407 .unbind = risp_notify_unbind, 408 }; 409 410 static int risp_parse_dt(struct rcar_isp *isp) 411 { 412 struct v4l2_async_connection *asd; 413 struct fwnode_handle *fwnode; 414 struct fwnode_handle *ep; 415 unsigned int id; 416 int ret; 417 418 for (id = 0; id < 2; id++) { 419 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(isp->dev), 420 0, id, 0); 421 if (ep) 422 break; 423 } 424 425 if (!ep) { 426 dev_err(isp->dev, "Not connected to subdevice\n"); 427 return -EINVAL; 428 } 429 430 if (id == 1) 431 isp->csi_input = RISP_CSI_INPUT1; 432 433 fwnode = fwnode_graph_get_remote_endpoint(ep); 434 fwnode_handle_put(ep); 435 436 dev_dbg(isp->dev, "Found '%pOF'\n", to_of_node(fwnode)); 437 438 v4l2_async_subdev_nf_init(&isp->notifier, &isp->subdev); 439 isp->notifier.ops = &risp_notify_ops; 440 441 asd = v4l2_async_nf_add_fwnode(&isp->notifier, fwnode, 442 struct v4l2_async_connection); 443 fwnode_handle_put(fwnode); 444 if (IS_ERR(asd)) 445 return PTR_ERR(asd); 446 447 ret = v4l2_async_nf_register(&isp->notifier); 448 if (ret) 449 v4l2_async_nf_cleanup(&isp->notifier); 450 451 return ret; 452 } 453 454 /* ----------------------------------------------------------------------------- 455 * Platform Device Driver 456 */ 457 458 static const struct media_entity_operations risp_entity_ops = { 459 .link_validate = v4l2_subdev_link_validate, 460 }; 461 462 static int risp_probe_resources(struct rcar_isp *isp, 463 struct platform_device *pdev) 464 { 465 struct resource *res; 466 467 /* 468 * For backward compatibility allow cs base to be the only reg if no 469 * reg-names are set in DT. 470 */ 471 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cs"); 472 if (!res) 473 isp->csbase = devm_platform_ioremap_resource(pdev, 0); 474 else 475 isp->csbase = devm_ioremap_resource(&pdev->dev, res); 476 477 if (IS_ERR(isp->csbase)) 478 return PTR_ERR(isp->csbase); 479 480 isp->rstc = devm_reset_control_get(&pdev->dev, NULL); 481 482 return PTR_ERR_OR_ZERO(isp->rstc); 483 } 484 485 static const struct of_device_id risp_of_id_table[] = { 486 { .compatible = "renesas,r8a779a0-isp" }, 487 { .compatible = "renesas,r8a779g0-isp" }, 488 /* Keep above for compatibility with old DTB files. */ 489 { .compatible = "renesas,rcar-gen4-isp" }, 490 { /* sentinel */ } 491 }; 492 MODULE_DEVICE_TABLE(of, risp_of_id_table); 493 494 static int risp_probe(struct platform_device *pdev) 495 { 496 struct rcar_isp *isp; 497 unsigned int i; 498 int ret; 499 500 isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL); 501 if (!isp) 502 return -ENOMEM; 503 504 isp->dev = &pdev->dev; 505 506 ret = risp_probe_resources(isp, pdev); 507 if (ret) { 508 dev_err(isp->dev, "Failed to get resources\n"); 509 return ret; 510 } 511 512 platform_set_drvdata(pdev, isp); 513 514 pm_runtime_enable(&pdev->dev); 515 516 ret = risp_parse_dt(isp); 517 if (ret) 518 goto error_pm; 519 520 isp->subdev.owner = THIS_MODULE; 521 isp->subdev.dev = &pdev->dev; 522 v4l2_subdev_init(&isp->subdev, &rcar_isp_subdev_ops); 523 v4l2_set_subdevdata(&isp->subdev, &pdev->dev); 524 snprintf(isp->subdev.name, sizeof(isp->subdev.name), "%s %s", 525 KBUILD_MODNAME, dev_name(&pdev->dev)); 526 isp->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE; 527 528 isp->subdev.entity.function = MEDIA_ENT_F_VID_MUX; 529 isp->subdev.entity.ops = &risp_entity_ops; 530 531 isp->pads[RCAR_ISP_SINK].flags = MEDIA_PAD_FL_SINK; 532 for (i = RCAR_ISP_PORT0; i < RCAR_ISP_NUM_PADS; i++) 533 isp->pads[i].flags = MEDIA_PAD_FL_SOURCE; 534 535 ret = media_entity_pads_init(&isp->subdev.entity, RCAR_ISP_NUM_PADS, 536 isp->pads); 537 if (ret) 538 goto error_notifier; 539 540 ret = v4l2_subdev_init_finalize(&isp->subdev); 541 if (ret) 542 goto error_notifier; 543 544 ret = v4l2_async_register_subdev(&isp->subdev); 545 if (ret < 0) 546 goto error_subdev; 547 548 dev_info(isp->dev, "Using CSI-2 input: %u\n", isp->csi_input); 549 550 return 0; 551 552 error_subdev: 553 v4l2_subdev_cleanup(&isp->subdev); 554 error_notifier: 555 v4l2_async_nf_unregister(&isp->notifier); 556 v4l2_async_nf_cleanup(&isp->notifier); 557 error_pm: 558 pm_runtime_disable(&pdev->dev); 559 560 return ret; 561 } 562 563 static void risp_remove(struct platform_device *pdev) 564 { 565 struct rcar_isp *isp = platform_get_drvdata(pdev); 566 567 v4l2_async_nf_unregister(&isp->notifier); 568 v4l2_async_nf_cleanup(&isp->notifier); 569 570 v4l2_async_unregister_subdev(&isp->subdev); 571 v4l2_subdev_cleanup(&isp->subdev); 572 573 pm_runtime_disable(&pdev->dev); 574 } 575 576 static struct platform_driver rcar_isp_driver = { 577 .driver = { 578 .name = "rcar-isp", 579 .suppress_bind_attrs = true, 580 .of_match_table = risp_of_id_table, 581 }, 582 .probe = risp_probe, 583 .remove = risp_remove, 584 }; 585 586 module_platform_driver(rcar_isp_driver); 587 588 MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>"); 589 MODULE_DESCRIPTION("Renesas R-Car ISP Channel Selector driver"); 590 MODULE_LICENSE("GPL"); 591