1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for the Texas Instruments DS90UB913 video serializer 4 * 5 * Based on a driver from Luca Ceresoli <luca@lucaceresoli.net> 6 * 7 * Copyright (c) 2019 Luca Ceresoli <luca@lucaceresoli.net> 8 * Copyright (c) 2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> 9 */ 10 11 #include <linux/clk-provider.h> 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <linux/fwnode.h> 15 #include <linux/gpio/driver.h> 16 #include <linux/i2c-atr.h> 17 #include <linux/i2c.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/property.h> 21 #include <linux/regmap.h> 22 23 #include <media/i2c/ds90ub9xx.h> 24 #include <media/v4l2-fwnode.h> 25 #include <media/v4l2-mediabus.h> 26 #include <media/v4l2-subdev.h> 27 28 #define UB913_PAD_SINK 0 29 #define UB913_PAD_SOURCE 1 30 31 /* 32 * UB913 has 4 gpios, but gpios 3 and 4 are reserved for external oscillator 33 * mode. Thus we only support 2 gpios for now. 34 */ 35 #define UB913_NUM_GPIOS 2 36 37 #define UB913_REG_RESET_CTL 0x01 38 #define UB913_REG_RESET_CTL_DIGITAL_RESET_1 BIT(1) 39 #define UB913_REG_RESET_CTL_DIGITAL_RESET_0 BIT(0) 40 41 #define UB913_REG_GENERAL_CFG 0x03 42 #define UB913_REG_GENERAL_CFG_CRC_ERR_RESET BIT(5) 43 #define UB913_REG_GENERAL_CFG_PCLK_RISING BIT(0) 44 45 #define UB913_REG_MODE_SEL 0x05 46 #define UB913_REG_MODE_SEL_MODE_OVERRIDE BIT(5) 47 #define UB913_REG_MODE_SEL_MODE_UP_TO_DATE BIT(4) 48 #define UB913_REG_MODE_SEL_MODE_MASK GENMASK(3, 0) 49 50 #define UB913_REG_CRC_ERRORS_LSB 0x0a 51 #define UB913_REG_CRC_ERRORS_MSB 0x0b 52 53 #define UB913_REG_GENERAL_STATUS 0x0c 54 55 #define UB913_REG_GPIO_CFG(n) (0x0d + (n)) 56 #define UB913_REG_GPIO_CFG_ENABLE(n) BIT(0 + (n) * 4) 57 #define UB913_REG_GPIO_CFG_DIR_INPUT(n) BIT(1 + (n) * 4) 58 #define UB913_REG_GPIO_CFG_REMOTE_EN(n) BIT(2 + (n) * 4) 59 #define UB913_REG_GPIO_CFG_OUT_VAL(n) BIT(3 + (n) * 4) 60 #define UB913_REG_GPIO_CFG_MASK(n) (0xf << ((n) * 4)) 61 62 #define UB913_REG_SCL_HIGH_TIME 0x11 63 #define UB913_REG_SCL_LOW_TIME 0x12 64 65 #define UB913_REG_PLL_OVR 0x35 66 67 struct ub913_data { 68 struct i2c_client *client; 69 struct regmap *regmap; 70 struct clk *clkin; 71 72 struct gpio_chip gpio_chip; 73 74 struct v4l2_subdev sd; 75 struct media_pad pads[2]; 76 77 struct v4l2_async_notifier notifier; 78 79 struct v4l2_subdev *source_sd; 80 u16 source_sd_pad; 81 82 u64 enabled_source_streams; 83 84 struct clk_hw *clkout_clk_hw; 85 86 struct ds90ub9xx_platform_data *plat_data; 87 88 bool pclk_polarity_rising; 89 }; 90 91 static inline struct ub913_data *sd_to_ub913(struct v4l2_subdev *sd) 92 { 93 return container_of(sd, struct ub913_data, sd); 94 } 95 96 struct ub913_format_info { 97 u32 incode; 98 u32 outcode; 99 }; 100 101 static const struct ub913_format_info ub913_formats[] = { 102 /* Only RAW10 with 8-bit payload is supported at the moment */ 103 { .incode = MEDIA_BUS_FMT_YUYV8_2X8, .outcode = MEDIA_BUS_FMT_YUYV8_1X16 }, 104 { .incode = MEDIA_BUS_FMT_UYVY8_2X8, .outcode = MEDIA_BUS_FMT_UYVY8_1X16 }, 105 { .incode = MEDIA_BUS_FMT_VYUY8_2X8, .outcode = MEDIA_BUS_FMT_VYUY8_1X16 }, 106 { .incode = MEDIA_BUS_FMT_YVYU8_2X8, .outcode = MEDIA_BUS_FMT_YVYU8_1X16 }, 107 }; 108 109 static const struct ub913_format_info *ub913_find_format(u32 incode) 110 { 111 unsigned int i; 112 113 for (i = 0; i < ARRAY_SIZE(ub913_formats); i++) { 114 if (ub913_formats[i].incode == incode) 115 return &ub913_formats[i]; 116 } 117 118 return NULL; 119 } 120 121 static int ub913_read(const struct ub913_data *priv, u8 reg, u8 *val) 122 { 123 unsigned int v; 124 int ret; 125 126 ret = regmap_read(priv->regmap, reg, &v); 127 if (ret < 0) { 128 dev_err(&priv->client->dev, 129 "Cannot read register 0x%02x: %d!\n", reg, ret); 130 return ret; 131 } 132 133 *val = v; 134 return 0; 135 } 136 137 static int ub913_write(const struct ub913_data *priv, u8 reg, u8 val) 138 { 139 int ret; 140 141 ret = regmap_write(priv->regmap, reg, val); 142 if (ret < 0) 143 dev_err(&priv->client->dev, 144 "Cannot write register 0x%02x: %d!\n", reg, ret); 145 146 return ret; 147 } 148 149 /* 150 * GPIO chip 151 */ 152 static int ub913_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 153 { 154 return GPIO_LINE_DIRECTION_OUT; 155 } 156 157 static int ub913_gpio_direction_out(struct gpio_chip *gc, unsigned int offset, 158 int value) 159 { 160 struct ub913_data *priv = gpiochip_get_data(gc); 161 unsigned int reg_idx = offset / 2; 162 unsigned int field_idx = offset % 2; 163 164 return regmap_update_bits(priv->regmap, UB913_REG_GPIO_CFG(reg_idx), 165 UB913_REG_GPIO_CFG_MASK(field_idx), 166 UB913_REG_GPIO_CFG_ENABLE(field_idx) | 167 (value ? UB913_REG_GPIO_CFG_OUT_VAL(field_idx) : 168 0)); 169 } 170 171 static void ub913_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 172 { 173 ub913_gpio_direction_out(gc, offset, value); 174 } 175 176 static int ub913_gpio_of_xlate(struct gpio_chip *gc, 177 const struct of_phandle_args *gpiospec, 178 u32 *flags) 179 { 180 if (flags) 181 *flags = gpiospec->args[1]; 182 183 return gpiospec->args[0]; 184 } 185 186 static int ub913_gpiochip_probe(struct ub913_data *priv) 187 { 188 struct device *dev = &priv->client->dev; 189 struct gpio_chip *gc = &priv->gpio_chip; 190 int ret; 191 192 /* Initialize GPIOs 0 and 1 to local control, tri-state */ 193 ub913_write(priv, UB913_REG_GPIO_CFG(0), 0); 194 195 gc->label = dev_name(dev); 196 gc->parent = dev; 197 gc->owner = THIS_MODULE; 198 gc->base = -1; 199 gc->can_sleep = true; 200 gc->ngpio = UB913_NUM_GPIOS; 201 gc->get_direction = ub913_gpio_get_direction; 202 gc->direction_output = ub913_gpio_direction_out; 203 gc->set = ub913_gpio_set; 204 gc->of_xlate = ub913_gpio_of_xlate; 205 gc->of_gpio_n_cells = 2; 206 207 ret = gpiochip_add_data(gc, priv); 208 if (ret) { 209 dev_err(dev, "Failed to add GPIOs: %d\n", ret); 210 return ret; 211 } 212 213 return 0; 214 } 215 216 static void ub913_gpiochip_remove(struct ub913_data *priv) 217 { 218 gpiochip_remove(&priv->gpio_chip); 219 } 220 221 static const struct regmap_config ub913_regmap_config = { 222 .name = "ds90ub913", 223 .reg_bits = 8, 224 .val_bits = 8, 225 .reg_format_endian = REGMAP_ENDIAN_DEFAULT, 226 .val_format_endian = REGMAP_ENDIAN_DEFAULT, 227 }; 228 229 /* 230 * V4L2 231 */ 232 233 static int ub913_enable_streams(struct v4l2_subdev *sd, 234 struct v4l2_subdev_state *state, u32 pad, 235 u64 streams_mask) 236 { 237 struct ub913_data *priv = sd_to_ub913(sd); 238 u64 sink_streams; 239 int ret; 240 241 sink_streams = v4l2_subdev_state_xlate_streams(state, UB913_PAD_SOURCE, 242 UB913_PAD_SINK, 243 &streams_mask); 244 245 ret = v4l2_subdev_enable_streams(priv->source_sd, priv->source_sd_pad, 246 sink_streams); 247 if (ret) 248 return ret; 249 250 priv->enabled_source_streams |= streams_mask; 251 252 return 0; 253 } 254 255 static int ub913_disable_streams(struct v4l2_subdev *sd, 256 struct v4l2_subdev_state *state, u32 pad, 257 u64 streams_mask) 258 { 259 struct ub913_data *priv = sd_to_ub913(sd); 260 u64 sink_streams; 261 int ret; 262 263 sink_streams = v4l2_subdev_state_xlate_streams(state, UB913_PAD_SOURCE, 264 UB913_PAD_SINK, 265 &streams_mask); 266 267 ret = v4l2_subdev_disable_streams(priv->source_sd, priv->source_sd_pad, 268 sink_streams); 269 if (ret) 270 return ret; 271 272 priv->enabled_source_streams &= ~streams_mask; 273 274 return 0; 275 } 276 277 static int _ub913_set_routing(struct v4l2_subdev *sd, 278 struct v4l2_subdev_state *state, 279 struct v4l2_subdev_krouting *routing) 280 { 281 static const struct v4l2_mbus_framefmt in_format = { 282 .width = 640, 283 .height = 480, 284 .code = MEDIA_BUS_FMT_UYVY8_2X8, 285 .field = V4L2_FIELD_NONE, 286 .colorspace = V4L2_COLORSPACE_SRGB, 287 .ycbcr_enc = V4L2_YCBCR_ENC_601, 288 .quantization = V4L2_QUANTIZATION_LIM_RANGE, 289 .xfer_func = V4L2_XFER_FUNC_SRGB, 290 }; 291 static const struct v4l2_mbus_framefmt out_format = { 292 .width = 640, 293 .height = 480, 294 .code = MEDIA_BUS_FMT_UYVY8_1X16, 295 .field = V4L2_FIELD_NONE, 296 .colorspace = V4L2_COLORSPACE_SRGB, 297 .ycbcr_enc = V4L2_YCBCR_ENC_601, 298 .quantization = V4L2_QUANTIZATION_LIM_RANGE, 299 .xfer_func = V4L2_XFER_FUNC_SRGB, 300 }; 301 struct v4l2_subdev_stream_configs *stream_configs; 302 unsigned int i; 303 int ret; 304 305 /* 306 * Note: we can only support up to V4L2_FRAME_DESC_ENTRY_MAX, until 307 * frame desc is made dynamically allocated. 308 */ 309 310 if (routing->num_routes > V4L2_FRAME_DESC_ENTRY_MAX) 311 return -EINVAL; 312 313 ret = v4l2_subdev_routing_validate(sd, routing, 314 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1); 315 if (ret) 316 return ret; 317 318 ret = v4l2_subdev_set_routing(sd, state, routing); 319 if (ret) 320 return ret; 321 322 stream_configs = &state->stream_configs; 323 324 for (i = 0; i < stream_configs->num_configs; i++) { 325 if (stream_configs->configs[i].pad == UB913_PAD_SINK) 326 stream_configs->configs[i].fmt = in_format; 327 else 328 stream_configs->configs[i].fmt = out_format; 329 } 330 331 return 0; 332 } 333 334 static int ub913_set_routing(struct v4l2_subdev *sd, 335 struct v4l2_subdev_state *state, 336 enum v4l2_subdev_format_whence which, 337 struct v4l2_subdev_krouting *routing) 338 { 339 struct ub913_data *priv = sd_to_ub913(sd); 340 341 if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->enabled_source_streams) 342 return -EBUSY; 343 344 return _ub913_set_routing(sd, state, routing); 345 } 346 347 static int ub913_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad, 348 struct v4l2_mbus_frame_desc *fd) 349 { 350 struct ub913_data *priv = sd_to_ub913(sd); 351 const struct v4l2_subdev_krouting *routing; 352 struct v4l2_mbus_frame_desc source_fd; 353 struct v4l2_subdev_route *route; 354 struct v4l2_subdev_state *state; 355 int ret; 356 357 if (pad != UB913_PAD_SOURCE) 358 return -EINVAL; 359 360 ret = v4l2_subdev_call(priv->source_sd, pad, get_frame_desc, 361 priv->source_sd_pad, &source_fd); 362 if (ret) 363 return ret; 364 365 fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL; 366 367 state = v4l2_subdev_lock_and_get_active_state(sd); 368 369 routing = &state->routing; 370 371 for_each_active_route(routing, route) { 372 unsigned int i; 373 374 if (route->source_pad != pad) 375 continue; 376 377 for (i = 0; i < source_fd.num_entries; i++) { 378 if (source_fd.entry[i].stream == route->sink_stream) 379 break; 380 } 381 382 if (i == source_fd.num_entries) { 383 dev_err(&priv->client->dev, 384 "Failed to find stream from source frame desc\n"); 385 ret = -EPIPE; 386 goto out_unlock; 387 } 388 389 fd->entry[fd->num_entries].stream = route->source_stream; 390 fd->entry[fd->num_entries].flags = source_fd.entry[i].flags; 391 fd->entry[fd->num_entries].length = source_fd.entry[i].length; 392 fd->entry[fd->num_entries].pixelcode = 393 source_fd.entry[i].pixelcode; 394 395 fd->num_entries++; 396 } 397 398 out_unlock: 399 v4l2_subdev_unlock_state(state); 400 401 return ret; 402 } 403 404 static int ub913_set_fmt(struct v4l2_subdev *sd, 405 struct v4l2_subdev_state *state, 406 struct v4l2_subdev_format *format) 407 { 408 struct ub913_data *priv = sd_to_ub913(sd); 409 struct v4l2_mbus_framefmt *fmt; 410 const struct ub913_format_info *finfo; 411 412 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && 413 priv->enabled_source_streams) 414 return -EBUSY; 415 416 /* Source format is fully defined by the sink format, so not settable */ 417 if (format->pad == UB913_PAD_SOURCE) 418 return v4l2_subdev_get_fmt(sd, state, format); 419 420 finfo = ub913_find_format(format->format.code); 421 if (!finfo) { 422 finfo = &ub913_formats[0]; 423 format->format.code = finfo->incode; 424 } 425 426 /* Set sink format */ 427 fmt = v4l2_subdev_state_get_stream_format(state, format->pad, 428 format->stream); 429 if (!fmt) 430 return -EINVAL; 431 432 *fmt = format->format; 433 434 /* Propagate to source format, and adjust the mbus code */ 435 fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad, 436 format->stream); 437 if (!fmt) 438 return -EINVAL; 439 440 format->format.code = finfo->outcode; 441 442 *fmt = format->format; 443 444 return 0; 445 } 446 447 static int ub913_init_cfg(struct v4l2_subdev *sd, 448 struct v4l2_subdev_state *state) 449 { 450 struct v4l2_subdev_route routes[] = { 451 { 452 .sink_pad = UB913_PAD_SINK, 453 .sink_stream = 0, 454 .source_pad = UB913_PAD_SOURCE, 455 .source_stream = 0, 456 .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE, 457 }, 458 }; 459 460 struct v4l2_subdev_krouting routing = { 461 .num_routes = ARRAY_SIZE(routes), 462 .routes = routes, 463 }; 464 465 return _ub913_set_routing(sd, state, &routing); 466 } 467 468 static int ub913_log_status(struct v4l2_subdev *sd) 469 { 470 struct ub913_data *priv = sd_to_ub913(sd); 471 struct device *dev = &priv->client->dev; 472 u8 v = 0, v1 = 0, v2 = 0; 473 474 ub913_read(priv, UB913_REG_MODE_SEL, &v); 475 dev_info(dev, "MODE_SEL %#02x\n", v); 476 477 ub913_read(priv, UB913_REG_CRC_ERRORS_LSB, &v1); 478 ub913_read(priv, UB913_REG_CRC_ERRORS_MSB, &v2); 479 dev_info(dev, "CRC errors %u\n", v1 | (v2 << 8)); 480 481 /* clear CRC errors */ 482 ub913_read(priv, UB913_REG_GENERAL_CFG, &v); 483 ub913_write(priv, UB913_REG_GENERAL_CFG, 484 v | UB913_REG_GENERAL_CFG_CRC_ERR_RESET); 485 ub913_write(priv, UB913_REG_GENERAL_CFG, v); 486 487 ub913_read(priv, UB913_REG_GENERAL_STATUS, &v); 488 dev_info(dev, "GENERAL_STATUS %#02x\n", v); 489 490 ub913_read(priv, UB913_REG_PLL_OVR, &v); 491 dev_info(dev, "PLL_OVR %#02x\n", v); 492 493 return 0; 494 } 495 496 static const struct v4l2_subdev_core_ops ub913_subdev_core_ops = { 497 .log_status = ub913_log_status, 498 }; 499 500 static const struct v4l2_subdev_pad_ops ub913_pad_ops = { 501 .enable_streams = ub913_enable_streams, 502 .disable_streams = ub913_disable_streams, 503 .set_routing = ub913_set_routing, 504 .get_frame_desc = ub913_get_frame_desc, 505 .get_fmt = v4l2_subdev_get_fmt, 506 .set_fmt = ub913_set_fmt, 507 .init_cfg = ub913_init_cfg, 508 }; 509 510 static const struct v4l2_subdev_ops ub913_subdev_ops = { 511 .core = &ub913_subdev_core_ops, 512 .pad = &ub913_pad_ops, 513 }; 514 515 static const struct media_entity_operations ub913_entity_ops = { 516 .link_validate = v4l2_subdev_link_validate, 517 }; 518 519 static int ub913_notify_bound(struct v4l2_async_notifier *notifier, 520 struct v4l2_subdev *source_subdev, 521 struct v4l2_async_connection *asd) 522 { 523 struct ub913_data *priv = sd_to_ub913(notifier->sd); 524 struct device *dev = &priv->client->dev; 525 int ret; 526 527 ret = media_entity_get_fwnode_pad(&source_subdev->entity, 528 source_subdev->fwnode, 529 MEDIA_PAD_FL_SOURCE); 530 if (ret < 0) { 531 dev_err(dev, "Failed to find pad for %s\n", 532 source_subdev->name); 533 return ret; 534 } 535 536 priv->source_sd = source_subdev; 537 priv->source_sd_pad = ret; 538 539 ret = media_create_pad_link(&source_subdev->entity, priv->source_sd_pad, 540 &priv->sd.entity, UB913_PAD_SINK, 541 MEDIA_LNK_FL_ENABLED | 542 MEDIA_LNK_FL_IMMUTABLE); 543 if (ret) { 544 dev_err(dev, "Unable to link %s:%u -> %s:0\n", 545 source_subdev->name, priv->source_sd_pad, 546 priv->sd.name); 547 return ret; 548 } 549 550 return 0; 551 } 552 553 static const struct v4l2_async_notifier_operations ub913_notify_ops = { 554 .bound = ub913_notify_bound, 555 }; 556 557 static int ub913_v4l2_notifier_register(struct ub913_data *priv) 558 { 559 struct device *dev = &priv->client->dev; 560 struct v4l2_async_connection *asd; 561 struct fwnode_handle *ep_fwnode; 562 int ret; 563 564 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 565 UB913_PAD_SINK, 0, 0); 566 if (!ep_fwnode) { 567 dev_err(dev, "No graph endpoint\n"); 568 return -ENODEV; 569 } 570 571 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd); 572 573 asd = v4l2_async_nf_add_fwnode_remote(&priv->notifier, ep_fwnode, 574 struct v4l2_async_connection); 575 576 fwnode_handle_put(ep_fwnode); 577 578 if (IS_ERR(asd)) { 579 dev_err(dev, "Failed to add subdev: %ld", PTR_ERR(asd)); 580 v4l2_async_nf_cleanup(&priv->notifier); 581 return PTR_ERR(asd); 582 } 583 584 priv->notifier.ops = &ub913_notify_ops; 585 586 ret = v4l2_async_nf_register(&priv->notifier); 587 if (ret) { 588 dev_err(dev, "Failed to register subdev_notifier"); 589 v4l2_async_nf_cleanup(&priv->notifier); 590 return ret; 591 } 592 593 return 0; 594 } 595 596 static void ub913_v4l2_nf_unregister(struct ub913_data *priv) 597 { 598 v4l2_async_nf_unregister(&priv->notifier); 599 v4l2_async_nf_cleanup(&priv->notifier); 600 } 601 602 static int ub913_register_clkout(struct ub913_data *priv) 603 { 604 struct device *dev = &priv->client->dev; 605 const char *name; 606 int ret; 607 608 name = kasprintf(GFP_KERNEL, "ds90ub913.%s.clk_out", dev_name(dev)); 609 if (!name) 610 return -ENOMEM; 611 612 priv->clkout_clk_hw = devm_clk_hw_register_fixed_factor(dev, name, 613 __clk_get_name(priv->clkin), 0, 1, 2); 614 615 kfree(name); 616 617 if (IS_ERR(priv->clkout_clk_hw)) 618 return dev_err_probe(dev, PTR_ERR(priv->clkout_clk_hw), 619 "Cannot register clkout hw\n"); 620 621 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, 622 priv->clkout_clk_hw); 623 if (ret) 624 return dev_err_probe(dev, ret, 625 "Cannot add OF clock provider\n"); 626 627 return 0; 628 } 629 630 static int ub913_i2c_master_init(struct ub913_data *priv) 631 { 632 /* i2c fast mode */ 633 u32 scl_high = 600 + 300; /* high period + rise time, ns */ 634 u32 scl_low = 1300 + 300; /* low period + fall time, ns */ 635 unsigned long ref; 636 int ret; 637 638 ref = clk_get_rate(priv->clkin) / 2; 639 640 scl_high = div64_u64((u64)scl_high * ref, 1000000000); 641 scl_low = div64_u64((u64)scl_low * ref, 1000000000); 642 643 ret = ub913_write(priv, UB913_REG_SCL_HIGH_TIME, scl_high); 644 if (ret) 645 return ret; 646 647 ret = ub913_write(priv, UB913_REG_SCL_LOW_TIME, scl_low); 648 if (ret) 649 return ret; 650 651 return 0; 652 } 653 654 static int ub913_add_i2c_adapter(struct ub913_data *priv) 655 { 656 struct device *dev = &priv->client->dev; 657 struct fwnode_handle *i2c_handle; 658 int ret; 659 660 i2c_handle = device_get_named_child_node(dev, "i2c"); 661 if (!i2c_handle) 662 return 0; 663 664 ret = i2c_atr_add_adapter(priv->plat_data->atr, priv->plat_data->port, 665 dev, i2c_handle); 666 667 fwnode_handle_put(i2c_handle); 668 669 if (ret) 670 return ret; 671 672 return 0; 673 } 674 675 static int ub913_parse_dt(struct ub913_data *priv) 676 { 677 struct device *dev = &priv->client->dev; 678 struct v4l2_fwnode_endpoint vep = { 679 .bus_type = V4L2_MBUS_PARALLEL, 680 }; 681 struct fwnode_handle *ep_fwnode; 682 int ret; 683 684 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 685 UB913_PAD_SINK, 0, 0); 686 if (!ep_fwnode) 687 return dev_err_probe(dev, -ENOENT, "No sink endpoint\n"); 688 689 ret = v4l2_fwnode_endpoint_parse(ep_fwnode, &vep); 690 691 fwnode_handle_put(ep_fwnode); 692 693 if (ret) 694 return dev_err_probe(dev, ret, 695 "failed to parse sink endpoint data\n"); 696 697 if (vep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 698 priv->pclk_polarity_rising = true; 699 else if (vep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING) 700 priv->pclk_polarity_rising = false; 701 else 702 return dev_err_probe(dev, -EINVAL, 703 "bad value for 'pclk-sample'\n"); 704 705 return 0; 706 } 707 708 static int ub913_hw_init(struct ub913_data *priv) 709 { 710 struct device *dev = &priv->client->dev; 711 bool mode_override; 712 u8 mode; 713 int ret; 714 u8 v; 715 716 ret = ub913_read(priv, UB913_REG_MODE_SEL, &v); 717 if (ret) 718 return ret; 719 720 if (!(v & UB913_REG_MODE_SEL_MODE_UP_TO_DATE)) 721 return dev_err_probe(dev, -ENODEV, 722 "Mode value not stabilized\n"); 723 724 mode_override = v & UB913_REG_MODE_SEL_MODE_OVERRIDE; 725 mode = v & UB913_REG_MODE_SEL_MODE_MASK; 726 727 dev_dbg(dev, "mode from %s: %#x\n", 728 mode_override ? "reg" : "deserializer", mode); 729 730 ret = ub913_i2c_master_init(priv); 731 if (ret) 732 return dev_err_probe(dev, ret, "i2c master init failed\n"); 733 734 ub913_read(priv, UB913_REG_GENERAL_CFG, &v); 735 v &= ~UB913_REG_GENERAL_CFG_PCLK_RISING; 736 v |= priv->pclk_polarity_rising ? UB913_REG_GENERAL_CFG_PCLK_RISING : 0; 737 ub913_write(priv, UB913_REG_GENERAL_CFG, v); 738 739 return 0; 740 } 741 742 static int ub913_subdev_init(struct ub913_data *priv) 743 { 744 struct device *dev = &priv->client->dev; 745 int ret; 746 747 v4l2_i2c_subdev_init(&priv->sd, priv->client, &ub913_subdev_ops); 748 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_STREAMS; 749 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 750 priv->sd.entity.ops = &ub913_entity_ops; 751 752 priv->pads[0].flags = MEDIA_PAD_FL_SINK; 753 priv->pads[1].flags = MEDIA_PAD_FL_SOURCE; 754 755 ret = media_entity_pads_init(&priv->sd.entity, 2, priv->pads); 756 if (ret) 757 return dev_err_probe(dev, ret, "Failed to init pads\n"); 758 759 ret = v4l2_subdev_init_finalize(&priv->sd); 760 if (ret) 761 goto err_entity_cleanup; 762 763 ret = ub913_v4l2_notifier_register(priv); 764 if (ret) { 765 dev_err_probe(dev, ret, 766 "v4l2 subdev notifier register failed\n"); 767 goto err_subdev_cleanup; 768 } 769 770 ret = v4l2_async_register_subdev(&priv->sd); 771 if (ret) { 772 dev_err_probe(dev, ret, "v4l2_async_register_subdev error\n"); 773 goto err_unreg_notif; 774 } 775 776 return 0; 777 778 err_unreg_notif: 779 ub913_v4l2_nf_unregister(priv); 780 err_subdev_cleanup: 781 v4l2_subdev_cleanup(&priv->sd); 782 err_entity_cleanup: 783 media_entity_cleanup(&priv->sd.entity); 784 785 return ret; 786 } 787 788 static void ub913_subdev_uninit(struct ub913_data *priv) 789 { 790 v4l2_async_unregister_subdev(&priv->sd); 791 ub913_v4l2_nf_unregister(priv); 792 v4l2_subdev_cleanup(&priv->sd); 793 fwnode_handle_put(priv->sd.fwnode); 794 media_entity_cleanup(&priv->sd.entity); 795 } 796 797 static int ub913_probe(struct i2c_client *client) 798 { 799 struct device *dev = &client->dev; 800 struct ub913_data *priv; 801 int ret; 802 803 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 804 if (!priv) 805 return -ENOMEM; 806 807 priv->client = client; 808 809 priv->plat_data = dev_get_platdata(&client->dev); 810 if (!priv->plat_data) 811 return dev_err_probe(dev, -ENODEV, "Platform data missing\n"); 812 813 priv->regmap = devm_regmap_init_i2c(client, &ub913_regmap_config); 814 if (IS_ERR(priv->regmap)) 815 return dev_err_probe(dev, PTR_ERR(priv->regmap), 816 "Failed to init regmap\n"); 817 818 /* 819 * ub913 can also work without ext clock, but that is not supported by 820 * the driver yet. 821 */ 822 priv->clkin = devm_clk_get(dev, "clkin"); 823 if (IS_ERR(priv->clkin)) 824 return dev_err_probe(dev, PTR_ERR(priv->clkin), 825 "Cannot get CLKIN\n"); 826 827 ret = ub913_parse_dt(priv); 828 if (ret) 829 return ret; 830 831 ret = ub913_hw_init(priv); 832 if (ret) 833 return ret; 834 835 ret = ub913_gpiochip_probe(priv); 836 if (ret) 837 return dev_err_probe(dev, ret, "Failed to init gpiochip\n"); 838 839 ret = ub913_register_clkout(priv); 840 if (ret) { 841 dev_err_probe(dev, ret, "Failed to register clkout\n"); 842 goto err_gpiochip_remove; 843 } 844 845 ret = ub913_subdev_init(priv); 846 if (ret) 847 goto err_gpiochip_remove; 848 849 ret = ub913_add_i2c_adapter(priv); 850 if (ret) { 851 dev_err_probe(dev, ret, "failed to add remote i2c adapter\n"); 852 goto err_subdev_uninit; 853 } 854 855 return 0; 856 857 err_subdev_uninit: 858 ub913_subdev_uninit(priv); 859 err_gpiochip_remove: 860 ub913_gpiochip_remove(priv); 861 862 return ret; 863 } 864 865 static void ub913_remove(struct i2c_client *client) 866 { 867 struct v4l2_subdev *sd = i2c_get_clientdata(client); 868 struct ub913_data *priv = sd_to_ub913(sd); 869 870 i2c_atr_del_adapter(priv->plat_data->atr, priv->plat_data->port); 871 872 ub913_subdev_uninit(priv); 873 874 ub913_gpiochip_remove(priv); 875 } 876 877 static const struct i2c_device_id ub913_id[] = { { "ds90ub913a-q1", 0 }, {} }; 878 MODULE_DEVICE_TABLE(i2c, ub913_id); 879 880 static const struct of_device_id ub913_dt_ids[] = { 881 { .compatible = "ti,ds90ub913a-q1" }, 882 {} 883 }; 884 MODULE_DEVICE_TABLE(of, ub913_dt_ids); 885 886 static struct i2c_driver ds90ub913_driver = { 887 .probe = ub913_probe, 888 .remove = ub913_remove, 889 .id_table = ub913_id, 890 .driver = { 891 .name = "ds90ub913a", 892 .of_match_table = ub913_dt_ids, 893 }, 894 }; 895 module_i2c_driver(ds90ub913_driver); 896 897 MODULE_LICENSE("GPL"); 898 MODULE_DESCRIPTION("Texas Instruments DS90UB913 FPD-Link III Serializer Driver"); 899 MODULE_AUTHOR("Luca Ceresoli <luca@lucaceresoli.net>"); 900 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>"); 901 MODULE_IMPORT_NS(I2C_ATR); 902