1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for ST MIPID02 CSI-2 to PARALLEL bridge 4 * 5 * Copyright (C) STMicroelectronics SA 2019 6 * Authors: Mickael Guene <mickael.guene@st.com> 7 * for STMicroelectronics. 8 * 9 * 10 */ 11 12 #include <linux/clk.h> 13 #include <linux/delay.h> 14 #include <linux/gpio/consumer.h> 15 #include <linux/i2c.h> 16 #include <linux/module.h> 17 #include <linux/of_graph.h> 18 #include <linux/regulator/consumer.h> 19 #include <media/v4l2-async.h> 20 #include <media/v4l2-ctrls.h> 21 #include <media/v4l2-device.h> 22 #include <media/v4l2-fwnode.h> 23 #include <media/v4l2-subdev.h> 24 25 #define MIPID02_CLK_LANE_WR_REG1 0x01 26 #define MIPID02_CLK_LANE_REG1 0x02 27 #define MIPID02_CLK_LANE_REG3 0x04 28 #define MIPID02_DATA_LANE0_REG1 0x05 29 #define MIPID02_DATA_LANE0_REG2 0x06 30 #define MIPID02_DATA_LANE1_REG1 0x09 31 #define MIPID02_DATA_LANE1_REG2 0x0a 32 #define MIPID02_MODE_REG1 0x14 33 #define MIPID02_MODE_REG2 0x15 34 #define MIPID02_DATA_ID_RREG 0x17 35 #define MIPID02_DATA_SELECTION_CTRL 0x19 36 #define MIPID02_PIX_WIDTH_CTRL 0x1e 37 #define MIPID02_PIX_WIDTH_CTRL_EMB 0x1f 38 39 /* Bits definition for MIPID02_CLK_LANE_REG1 */ 40 #define CLK_ENABLE BIT(0) 41 /* Bits definition for MIPID02_CLK_LANE_REG3 */ 42 #define CLK_MIPI_CSI BIT(1) 43 /* Bits definition for MIPID02_DATA_LANE0_REG1 */ 44 #define DATA_ENABLE BIT(0) 45 /* Bits definition for MIPID02_DATA_LANEx_REG2 */ 46 #define DATA_MIPI_CSI BIT(0) 47 /* Bits definition for MIPID02_MODE_REG1 */ 48 #define MODE_DATA_SWAP BIT(2) 49 #define MODE_NO_BYPASS BIT(6) 50 /* Bits definition for MIPID02_MODE_REG2 */ 51 #define MODE_HSYNC_ACTIVE_HIGH BIT(1) 52 #define MODE_VSYNC_ACTIVE_HIGH BIT(2) 53 #define MODE_PCLK_SAMPLE_RISING BIT(3) 54 /* Bits definition for MIPID02_DATA_SELECTION_CTRL */ 55 #define SELECTION_MANUAL_DATA BIT(2) 56 #define SELECTION_MANUAL_WIDTH BIT(3) 57 58 static const u32 mipid02_supported_fmt_codes[] = { 59 MEDIA_BUS_FMT_SBGGR8_1X8, MEDIA_BUS_FMT_SGBRG8_1X8, 60 MEDIA_BUS_FMT_SGRBG8_1X8, MEDIA_BUS_FMT_SRGGB8_1X8, 61 MEDIA_BUS_FMT_SBGGR10_1X10, MEDIA_BUS_FMT_SGBRG10_1X10, 62 MEDIA_BUS_FMT_SGRBG10_1X10, MEDIA_BUS_FMT_SRGGB10_1X10, 63 MEDIA_BUS_FMT_SBGGR12_1X12, MEDIA_BUS_FMT_SGBRG12_1X12, 64 MEDIA_BUS_FMT_SGRBG12_1X12, MEDIA_BUS_FMT_SRGGB12_1X12, 65 MEDIA_BUS_FMT_YUYV8_1X16, MEDIA_BUS_FMT_YVYU8_1X16, 66 MEDIA_BUS_FMT_UYVY8_1X16, MEDIA_BUS_FMT_VYUY8_1X16, 67 MEDIA_BUS_FMT_RGB565_1X16, MEDIA_BUS_FMT_BGR888_1X24, 68 MEDIA_BUS_FMT_RGB565_2X8_LE, MEDIA_BUS_FMT_RGB565_2X8_BE, 69 MEDIA_BUS_FMT_YUYV8_2X8, MEDIA_BUS_FMT_YVYU8_2X8, 70 MEDIA_BUS_FMT_UYVY8_2X8, MEDIA_BUS_FMT_VYUY8_2X8, 71 MEDIA_BUS_FMT_JPEG_1X8 72 }; 73 74 /* regulator supplies */ 75 static const char * const mipid02_supply_name[] = { 76 "VDDE", /* 1.8V digital I/O supply */ 77 "VDDIN", /* 1V8 voltage regulator supply */ 78 }; 79 80 #define MIPID02_NUM_SUPPLIES ARRAY_SIZE(mipid02_supply_name) 81 82 #define MIPID02_SINK_0 0 83 #define MIPID02_SINK_1 1 84 #define MIPID02_SOURCE 2 85 #define MIPID02_PAD_NB 3 86 87 struct mipid02_dev { 88 struct i2c_client *i2c_client; 89 struct regulator_bulk_data supplies[MIPID02_NUM_SUPPLIES]; 90 struct v4l2_subdev sd; 91 struct media_pad pad[MIPID02_PAD_NB]; 92 struct clk *xclk; 93 struct gpio_desc *reset_gpio; 94 /* endpoints info */ 95 struct v4l2_fwnode_endpoint rx; 96 u64 link_frequency; 97 struct v4l2_fwnode_endpoint tx; 98 /* remote source */ 99 struct v4l2_async_notifier notifier; 100 struct v4l2_subdev *s_subdev; 101 /* registers */ 102 struct { 103 u8 clk_lane_reg1; 104 u8 data_lane0_reg1; 105 u8 data_lane1_reg1; 106 u8 mode_reg1; 107 u8 mode_reg2; 108 u8 data_selection_ctrl; 109 u8 data_id_rreg; 110 u8 pix_width_ctrl; 111 u8 pix_width_ctrl_emb; 112 } r; 113 /* lock to protect all members below */ 114 struct mutex lock; 115 bool streaming; 116 struct v4l2_mbus_framefmt fmt; 117 }; 118 119 static int bpp_from_code(__u32 code) 120 { 121 switch (code) { 122 case MEDIA_BUS_FMT_SBGGR8_1X8: 123 case MEDIA_BUS_FMT_SGBRG8_1X8: 124 case MEDIA_BUS_FMT_SGRBG8_1X8: 125 case MEDIA_BUS_FMT_SRGGB8_1X8: 126 return 8; 127 case MEDIA_BUS_FMT_SBGGR10_1X10: 128 case MEDIA_BUS_FMT_SGBRG10_1X10: 129 case MEDIA_BUS_FMT_SGRBG10_1X10: 130 case MEDIA_BUS_FMT_SRGGB10_1X10: 131 return 10; 132 case MEDIA_BUS_FMT_SBGGR12_1X12: 133 case MEDIA_BUS_FMT_SGBRG12_1X12: 134 case MEDIA_BUS_FMT_SGRBG12_1X12: 135 case MEDIA_BUS_FMT_SRGGB12_1X12: 136 return 12; 137 case MEDIA_BUS_FMT_YUYV8_1X16: 138 case MEDIA_BUS_FMT_YVYU8_1X16: 139 case MEDIA_BUS_FMT_UYVY8_1X16: 140 case MEDIA_BUS_FMT_VYUY8_1X16: 141 case MEDIA_BUS_FMT_RGB565_1X16: 142 case MEDIA_BUS_FMT_YUYV8_2X8: 143 case MEDIA_BUS_FMT_YVYU8_2X8: 144 case MEDIA_BUS_FMT_UYVY8_2X8: 145 case MEDIA_BUS_FMT_VYUY8_2X8: 146 case MEDIA_BUS_FMT_RGB565_2X8_LE: 147 case MEDIA_BUS_FMT_RGB565_2X8_BE: 148 return 16; 149 case MEDIA_BUS_FMT_BGR888_1X24: 150 return 24; 151 default: 152 return 0; 153 } 154 } 155 156 static u8 data_type_from_code(__u32 code) 157 { 158 switch (code) { 159 case MEDIA_BUS_FMT_SBGGR8_1X8: 160 case MEDIA_BUS_FMT_SGBRG8_1X8: 161 case MEDIA_BUS_FMT_SGRBG8_1X8: 162 case MEDIA_BUS_FMT_SRGGB8_1X8: 163 return 0x2a; 164 case MEDIA_BUS_FMT_SBGGR10_1X10: 165 case MEDIA_BUS_FMT_SGBRG10_1X10: 166 case MEDIA_BUS_FMT_SGRBG10_1X10: 167 case MEDIA_BUS_FMT_SRGGB10_1X10: 168 return 0x2b; 169 case MEDIA_BUS_FMT_SBGGR12_1X12: 170 case MEDIA_BUS_FMT_SGBRG12_1X12: 171 case MEDIA_BUS_FMT_SGRBG12_1X12: 172 case MEDIA_BUS_FMT_SRGGB12_1X12: 173 return 0x2c; 174 case MEDIA_BUS_FMT_YUYV8_1X16: 175 case MEDIA_BUS_FMT_YVYU8_1X16: 176 case MEDIA_BUS_FMT_UYVY8_1X16: 177 case MEDIA_BUS_FMT_VYUY8_1X16: 178 case MEDIA_BUS_FMT_YUYV8_2X8: 179 case MEDIA_BUS_FMT_YVYU8_2X8: 180 case MEDIA_BUS_FMT_UYVY8_2X8: 181 case MEDIA_BUS_FMT_VYUY8_2X8: 182 return 0x1e; 183 case MEDIA_BUS_FMT_BGR888_1X24: 184 return 0x24; 185 case MEDIA_BUS_FMT_RGB565_1X16: 186 case MEDIA_BUS_FMT_RGB565_2X8_LE: 187 case MEDIA_BUS_FMT_RGB565_2X8_BE: 188 return 0x22; 189 default: 190 return 0; 191 } 192 } 193 194 static void init_format(struct v4l2_mbus_framefmt *fmt) 195 { 196 fmt->code = MEDIA_BUS_FMT_SBGGR8_1X8; 197 fmt->field = V4L2_FIELD_NONE; 198 fmt->colorspace = V4L2_COLORSPACE_SRGB; 199 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(V4L2_COLORSPACE_SRGB); 200 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 201 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(V4L2_COLORSPACE_SRGB); 202 fmt->width = 640; 203 fmt->height = 480; 204 } 205 206 static __u32 get_fmt_code(__u32 code) 207 { 208 unsigned int i; 209 210 for (i = 0; i < ARRAY_SIZE(mipid02_supported_fmt_codes); i++) { 211 if (code == mipid02_supported_fmt_codes[i]) 212 return code; 213 } 214 215 return mipid02_supported_fmt_codes[0]; 216 } 217 218 static __u32 serial_to_parallel_code(__u32 serial) 219 { 220 if (serial == MEDIA_BUS_FMT_RGB565_1X16) 221 return MEDIA_BUS_FMT_RGB565_2X8_LE; 222 if (serial == MEDIA_BUS_FMT_YUYV8_1X16) 223 return MEDIA_BUS_FMT_YUYV8_2X8; 224 if (serial == MEDIA_BUS_FMT_YVYU8_1X16) 225 return MEDIA_BUS_FMT_YVYU8_2X8; 226 if (serial == MEDIA_BUS_FMT_UYVY8_1X16) 227 return MEDIA_BUS_FMT_UYVY8_2X8; 228 if (serial == MEDIA_BUS_FMT_VYUY8_1X16) 229 return MEDIA_BUS_FMT_VYUY8_2X8; 230 if (serial == MEDIA_BUS_FMT_BGR888_1X24) 231 return MEDIA_BUS_FMT_BGR888_3X8; 232 233 return serial; 234 } 235 236 static inline struct mipid02_dev *to_mipid02_dev(struct v4l2_subdev *sd) 237 { 238 return container_of(sd, struct mipid02_dev, sd); 239 } 240 241 static int mipid02_read_reg(struct mipid02_dev *bridge, u16 reg, u8 *val) 242 { 243 struct i2c_client *client = bridge->i2c_client; 244 struct i2c_msg msg[2]; 245 u8 buf[2]; 246 int ret; 247 248 buf[0] = reg >> 8; 249 buf[1] = reg & 0xff; 250 251 msg[0].addr = client->addr; 252 msg[0].flags = client->flags; 253 msg[0].buf = buf; 254 msg[0].len = sizeof(buf); 255 256 msg[1].addr = client->addr; 257 msg[1].flags = client->flags | I2C_M_RD; 258 msg[1].buf = val; 259 msg[1].len = 1; 260 261 ret = i2c_transfer(client->adapter, msg, 2); 262 if (ret < 0) { 263 dev_dbg(&client->dev, "%s: %x i2c_transfer, reg: %x => %d\n", 264 __func__, client->addr, reg, ret); 265 return ret; 266 } 267 268 return 0; 269 } 270 271 static int mipid02_write_reg(struct mipid02_dev *bridge, u16 reg, u8 val) 272 { 273 struct i2c_client *client = bridge->i2c_client; 274 struct i2c_msg msg; 275 u8 buf[3]; 276 int ret; 277 278 buf[0] = reg >> 8; 279 buf[1] = reg & 0xff; 280 buf[2] = val; 281 282 msg.addr = client->addr; 283 msg.flags = client->flags; 284 msg.buf = buf; 285 msg.len = sizeof(buf); 286 287 ret = i2c_transfer(client->adapter, &msg, 1); 288 if (ret < 0) { 289 dev_dbg(&client->dev, "%s: i2c_transfer, reg: %x => %d\n", 290 __func__, reg, ret); 291 return ret; 292 } 293 294 return 0; 295 } 296 297 static int mipid02_get_regulators(struct mipid02_dev *bridge) 298 { 299 unsigned int i; 300 301 for (i = 0; i < MIPID02_NUM_SUPPLIES; i++) 302 bridge->supplies[i].supply = mipid02_supply_name[i]; 303 304 return devm_regulator_bulk_get(&bridge->i2c_client->dev, 305 MIPID02_NUM_SUPPLIES, 306 bridge->supplies); 307 } 308 309 static void mipid02_apply_reset(struct mipid02_dev *bridge) 310 { 311 gpiod_set_value_cansleep(bridge->reset_gpio, 0); 312 usleep_range(5000, 10000); 313 gpiod_set_value_cansleep(bridge->reset_gpio, 1); 314 usleep_range(5000, 10000); 315 gpiod_set_value_cansleep(bridge->reset_gpio, 0); 316 usleep_range(5000, 10000); 317 } 318 319 static int mipid02_set_power_on(struct mipid02_dev *bridge) 320 { 321 struct i2c_client *client = bridge->i2c_client; 322 int ret; 323 324 ret = clk_prepare_enable(bridge->xclk); 325 if (ret) { 326 dev_err(&client->dev, "%s: failed to enable clock\n", __func__); 327 return ret; 328 } 329 330 ret = regulator_bulk_enable(MIPID02_NUM_SUPPLIES, 331 bridge->supplies); 332 if (ret) { 333 dev_err(&client->dev, "%s: failed to enable regulators\n", 334 __func__); 335 goto xclk_off; 336 } 337 338 if (bridge->reset_gpio) { 339 dev_dbg(&client->dev, "apply reset"); 340 mipid02_apply_reset(bridge); 341 } else { 342 dev_dbg(&client->dev, "don't apply reset"); 343 usleep_range(5000, 10000); 344 } 345 346 return 0; 347 348 xclk_off: 349 clk_disable_unprepare(bridge->xclk); 350 return ret; 351 } 352 353 static void mipid02_set_power_off(struct mipid02_dev *bridge) 354 { 355 regulator_bulk_disable(MIPID02_NUM_SUPPLIES, bridge->supplies); 356 clk_disable_unprepare(bridge->xclk); 357 } 358 359 static int mipid02_detect(struct mipid02_dev *bridge) 360 { 361 u8 reg; 362 363 /* 364 * There is no version registers. Just try to read register 365 * MIPID02_CLK_LANE_WR_REG1. 366 */ 367 return mipid02_read_reg(bridge, MIPID02_CLK_LANE_WR_REG1, ®); 368 } 369 370 static u32 mipid02_get_link_freq_from_cid_link_freq(struct mipid02_dev *bridge, 371 struct v4l2_subdev *subdev) 372 { 373 struct v4l2_querymenu qm = {.id = V4L2_CID_LINK_FREQ, }; 374 struct v4l2_ctrl *ctrl; 375 int ret; 376 377 ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_LINK_FREQ); 378 if (!ctrl) 379 return 0; 380 qm.index = v4l2_ctrl_g_ctrl(ctrl); 381 382 ret = v4l2_querymenu(subdev->ctrl_handler, &qm); 383 if (ret) 384 return 0; 385 386 return qm.value; 387 } 388 389 static u32 mipid02_get_link_freq_from_cid_pixel_rate(struct mipid02_dev *bridge, 390 struct v4l2_subdev *subdev) 391 { 392 struct v4l2_fwnode_endpoint *ep = &bridge->rx; 393 struct v4l2_ctrl *ctrl; 394 u32 pixel_clock; 395 u32 bpp = bpp_from_code(bridge->fmt.code); 396 397 ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE); 398 if (!ctrl) 399 return 0; 400 pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl); 401 402 return pixel_clock * bpp / (2 * ep->bus.mipi_csi2.num_data_lanes); 403 } 404 405 /* 406 * We need to know link frequency to setup clk_lane_reg1 timings. Link frequency 407 * will be computed using connected device V4L2_CID_PIXEL_RATE, bit per pixel 408 * and number of lanes. 409 */ 410 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge) 411 { 412 struct i2c_client *client = bridge->i2c_client; 413 struct v4l2_subdev *subdev = bridge->s_subdev; 414 u32 link_freq; 415 416 link_freq = mipid02_get_link_freq_from_cid_link_freq(bridge, subdev); 417 if (!link_freq) { 418 link_freq = mipid02_get_link_freq_from_cid_pixel_rate(bridge, 419 subdev); 420 if (!link_freq) { 421 dev_err(&client->dev, "Failed to get link frequency"); 422 return -EINVAL; 423 } 424 } 425 426 dev_dbg(&client->dev, "detect link_freq = %d Hz", link_freq); 427 bridge->r.clk_lane_reg1 |= (2000000000 / link_freq) << 2; 428 429 return 0; 430 } 431 432 static int mipid02_configure_clk_lane(struct mipid02_dev *bridge) 433 { 434 struct i2c_client *client = bridge->i2c_client; 435 struct v4l2_fwnode_endpoint *ep = &bridge->rx; 436 bool *polarities = ep->bus.mipi_csi2.lane_polarities; 437 438 /* midid02 doesn't support clock lane remapping */ 439 if (ep->bus.mipi_csi2.clock_lane != 0) { 440 dev_err(&client->dev, "clk lane must be map to lane 0\n"); 441 return -EINVAL; 442 } 443 bridge->r.clk_lane_reg1 |= (polarities[0] << 1) | CLK_ENABLE; 444 445 return 0; 446 } 447 448 static int mipid02_configure_data0_lane(struct mipid02_dev *bridge, int nb, 449 bool are_lanes_swap, bool *polarities) 450 { 451 bool are_pin_swap = are_lanes_swap ? polarities[2] : polarities[1]; 452 453 if (nb == 1 && are_lanes_swap) 454 return 0; 455 456 /* 457 * data lane 0 as pin swap polarity reversed compared to clock and 458 * data lane 1 459 */ 460 if (!are_pin_swap) 461 bridge->r.data_lane0_reg1 = 1 << 1; 462 bridge->r.data_lane0_reg1 |= DATA_ENABLE; 463 464 return 0; 465 } 466 467 static int mipid02_configure_data1_lane(struct mipid02_dev *bridge, int nb, 468 bool are_lanes_swap, bool *polarities) 469 { 470 bool are_pin_swap = are_lanes_swap ? polarities[1] : polarities[2]; 471 472 if (nb == 1 && !are_lanes_swap) 473 return 0; 474 475 if (are_pin_swap) 476 bridge->r.data_lane1_reg1 = 1 << 1; 477 bridge->r.data_lane1_reg1 |= DATA_ENABLE; 478 479 return 0; 480 } 481 482 static int mipid02_configure_from_rx(struct mipid02_dev *bridge) 483 { 484 struct v4l2_fwnode_endpoint *ep = &bridge->rx; 485 bool are_lanes_swap = ep->bus.mipi_csi2.data_lanes[0] == 2; 486 bool *polarities = ep->bus.mipi_csi2.lane_polarities; 487 int nb = ep->bus.mipi_csi2.num_data_lanes; 488 int ret; 489 490 ret = mipid02_configure_clk_lane(bridge); 491 if (ret) 492 return ret; 493 494 ret = mipid02_configure_data0_lane(bridge, nb, are_lanes_swap, 495 polarities); 496 if (ret) 497 return ret; 498 499 ret = mipid02_configure_data1_lane(bridge, nb, are_lanes_swap, 500 polarities); 501 if (ret) 502 return ret; 503 504 bridge->r.mode_reg1 |= are_lanes_swap ? MODE_DATA_SWAP : 0; 505 bridge->r.mode_reg1 |= (nb - 1) << 1; 506 507 return mipid02_configure_from_rx_speed(bridge); 508 } 509 510 static int mipid02_configure_from_tx(struct mipid02_dev *bridge) 511 { 512 struct v4l2_fwnode_endpoint *ep = &bridge->tx; 513 514 bridge->r.data_selection_ctrl = SELECTION_MANUAL_WIDTH; 515 bridge->r.pix_width_ctrl = ep->bus.parallel.bus_width; 516 bridge->r.pix_width_ctrl_emb = ep->bus.parallel.bus_width; 517 if (ep->bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 518 bridge->r.mode_reg2 |= MODE_HSYNC_ACTIVE_HIGH; 519 if (ep->bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) 520 bridge->r.mode_reg2 |= MODE_VSYNC_ACTIVE_HIGH; 521 if (ep->bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 522 bridge->r.mode_reg2 |= MODE_PCLK_SAMPLE_RISING; 523 524 return 0; 525 } 526 527 static int mipid02_configure_from_code(struct mipid02_dev *bridge) 528 { 529 u8 data_type; 530 531 bridge->r.data_id_rreg = 0; 532 533 if (bridge->fmt.code != MEDIA_BUS_FMT_JPEG_1X8) { 534 bridge->r.data_selection_ctrl |= SELECTION_MANUAL_DATA; 535 536 data_type = data_type_from_code(bridge->fmt.code); 537 if (!data_type) 538 return -EINVAL; 539 bridge->r.data_id_rreg = data_type; 540 } 541 542 return 0; 543 } 544 545 static int mipid02_stream_disable(struct mipid02_dev *bridge) 546 { 547 struct i2c_client *client = bridge->i2c_client; 548 int ret; 549 550 /* Disable all lanes */ 551 ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 0); 552 if (ret) 553 goto error; 554 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 0); 555 if (ret) 556 goto error; 557 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 0); 558 if (ret) 559 goto error; 560 error: 561 if (ret) 562 dev_err(&client->dev, "failed to stream off %d", ret); 563 564 return ret; 565 } 566 567 static int mipid02_stream_enable(struct mipid02_dev *bridge) 568 { 569 struct i2c_client *client = bridge->i2c_client; 570 int ret = -EINVAL; 571 572 if (!bridge->s_subdev) 573 goto error; 574 575 memset(&bridge->r, 0, sizeof(bridge->r)); 576 /* build registers content */ 577 ret = mipid02_configure_from_rx(bridge); 578 if (ret) 579 goto error; 580 ret = mipid02_configure_from_tx(bridge); 581 if (ret) 582 goto error; 583 ret = mipid02_configure_from_code(bridge); 584 if (ret) 585 goto error; 586 587 /* write mipi registers */ 588 ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG1, 589 bridge->r.clk_lane_reg1); 590 if (ret) 591 goto error; 592 ret = mipid02_write_reg(bridge, MIPID02_CLK_LANE_REG3, CLK_MIPI_CSI); 593 if (ret) 594 goto error; 595 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG1, 596 bridge->r.data_lane0_reg1); 597 if (ret) 598 goto error; 599 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE0_REG2, 600 DATA_MIPI_CSI); 601 if (ret) 602 goto error; 603 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG1, 604 bridge->r.data_lane1_reg1); 605 if (ret) 606 goto error; 607 ret = mipid02_write_reg(bridge, MIPID02_DATA_LANE1_REG2, 608 DATA_MIPI_CSI); 609 if (ret) 610 goto error; 611 ret = mipid02_write_reg(bridge, MIPID02_MODE_REG1, 612 MODE_NO_BYPASS | bridge->r.mode_reg1); 613 if (ret) 614 goto error; 615 ret = mipid02_write_reg(bridge, MIPID02_MODE_REG2, 616 bridge->r.mode_reg2); 617 if (ret) 618 goto error; 619 ret = mipid02_write_reg(bridge, MIPID02_DATA_ID_RREG, 620 bridge->r.data_id_rreg); 621 if (ret) 622 goto error; 623 ret = mipid02_write_reg(bridge, MIPID02_DATA_SELECTION_CTRL, 624 bridge->r.data_selection_ctrl); 625 if (ret) 626 goto error; 627 ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL, 628 bridge->r.pix_width_ctrl); 629 if (ret) 630 goto error; 631 ret = mipid02_write_reg(bridge, MIPID02_PIX_WIDTH_CTRL_EMB, 632 bridge->r.pix_width_ctrl_emb); 633 if (ret) 634 goto error; 635 636 return 0; 637 638 error: 639 dev_err(&client->dev, "failed to stream on %d", ret); 640 mipid02_stream_disable(bridge); 641 642 return ret; 643 } 644 645 static int mipid02_s_stream(struct v4l2_subdev *sd, int enable) 646 { 647 struct mipid02_dev *bridge = to_mipid02_dev(sd); 648 struct i2c_client *client = bridge->i2c_client; 649 int ret = 0; 650 651 dev_dbg(&client->dev, "%s : requested %d / current = %d", __func__, 652 enable, bridge->streaming); 653 mutex_lock(&bridge->lock); 654 655 if (bridge->streaming == enable) 656 goto out; 657 658 ret = enable ? mipid02_stream_enable(bridge) : 659 mipid02_stream_disable(bridge); 660 if (!ret) 661 bridge->streaming = enable; 662 663 out: 664 dev_dbg(&client->dev, "%s current now = %d / %d", __func__, 665 bridge->streaming, ret); 666 mutex_unlock(&bridge->lock); 667 668 return ret; 669 } 670 671 static int mipid02_enum_mbus_code(struct v4l2_subdev *sd, 672 struct v4l2_subdev_state *sd_state, 673 struct v4l2_subdev_mbus_code_enum *code) 674 { 675 struct mipid02_dev *bridge = to_mipid02_dev(sd); 676 int ret = 0; 677 678 switch (code->pad) { 679 case MIPID02_SINK_0: 680 if (code->index >= ARRAY_SIZE(mipid02_supported_fmt_codes)) 681 ret = -EINVAL; 682 else 683 code->code = mipid02_supported_fmt_codes[code->index]; 684 break; 685 case MIPID02_SOURCE: 686 if (code->index == 0) 687 code->code = serial_to_parallel_code(bridge->fmt.code); 688 else 689 ret = -EINVAL; 690 break; 691 default: 692 ret = -EINVAL; 693 } 694 695 return ret; 696 } 697 698 static int mipid02_get_fmt(struct v4l2_subdev *sd, 699 struct v4l2_subdev_state *sd_state, 700 struct v4l2_subdev_format *format) 701 { 702 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 703 struct mipid02_dev *bridge = to_mipid02_dev(sd); 704 struct i2c_client *client = bridge->i2c_client; 705 struct v4l2_mbus_framefmt *fmt; 706 707 dev_dbg(&client->dev, "%s probe %d", __func__, format->pad); 708 709 if (format->pad >= MIPID02_PAD_NB) 710 return -EINVAL; 711 /* second CSI-2 pad not yet supported */ 712 if (format->pad == MIPID02_SINK_1) 713 return -EINVAL; 714 715 if (format->which == V4L2_SUBDEV_FORMAT_TRY) 716 fmt = v4l2_subdev_get_try_format(&bridge->sd, sd_state, 717 format->pad); 718 else 719 fmt = &bridge->fmt; 720 721 mutex_lock(&bridge->lock); 722 723 *mbus_fmt = *fmt; 724 /* code may need to be converted for source */ 725 if (format->pad == MIPID02_SOURCE) 726 mbus_fmt->code = serial_to_parallel_code(mbus_fmt->code); 727 728 mutex_unlock(&bridge->lock); 729 730 return 0; 731 } 732 733 static void mipid02_set_fmt_source(struct v4l2_subdev *sd, 734 struct v4l2_subdev_state *sd_state, 735 struct v4l2_subdev_format *format) 736 { 737 struct mipid02_dev *bridge = to_mipid02_dev(sd); 738 739 /* source pad mirror sink pad */ 740 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) 741 format->format = bridge->fmt; 742 else 743 format->format = *v4l2_subdev_get_try_format(sd, sd_state, 744 MIPID02_SINK_0); 745 746 /* but code may need to be converted */ 747 format->format.code = serial_to_parallel_code(format->format.code); 748 749 /* only apply format for V4L2_SUBDEV_FORMAT_TRY case */ 750 if (format->which != V4L2_SUBDEV_FORMAT_TRY) 751 return; 752 753 *v4l2_subdev_get_try_format(sd, sd_state, MIPID02_SOURCE) = 754 format->format; 755 } 756 757 static void mipid02_set_fmt_sink(struct v4l2_subdev *sd, 758 struct v4l2_subdev_state *sd_state, 759 struct v4l2_subdev_format *format) 760 { 761 struct mipid02_dev *bridge = to_mipid02_dev(sd); 762 struct v4l2_mbus_framefmt *fmt; 763 764 format->format.code = get_fmt_code(format->format.code); 765 766 if (format->which == V4L2_SUBDEV_FORMAT_TRY) 767 fmt = v4l2_subdev_get_try_format(sd, sd_state, format->pad); 768 else 769 fmt = &bridge->fmt; 770 771 *fmt = format->format; 772 773 /* Propagate the format change to the source pad */ 774 mipid02_set_fmt_source(sd, sd_state, format); 775 } 776 777 static int mipid02_set_fmt(struct v4l2_subdev *sd, 778 struct v4l2_subdev_state *sd_state, 779 struct v4l2_subdev_format *format) 780 { 781 struct mipid02_dev *bridge = to_mipid02_dev(sd); 782 struct i2c_client *client = bridge->i2c_client; 783 int ret = 0; 784 785 dev_dbg(&client->dev, "%s for %d", __func__, format->pad); 786 787 if (format->pad >= MIPID02_PAD_NB) 788 return -EINVAL; 789 /* second CSI-2 pad not yet supported */ 790 if (format->pad == MIPID02_SINK_1) 791 return -EINVAL; 792 793 mutex_lock(&bridge->lock); 794 795 if (bridge->streaming) { 796 ret = -EBUSY; 797 goto error; 798 } 799 800 if (format->pad == MIPID02_SOURCE) 801 mipid02_set_fmt_source(sd, sd_state, format); 802 else 803 mipid02_set_fmt_sink(sd, sd_state, format); 804 805 error: 806 mutex_unlock(&bridge->lock); 807 808 return ret; 809 } 810 811 static const struct v4l2_subdev_video_ops mipid02_video_ops = { 812 .s_stream = mipid02_s_stream, 813 }; 814 815 static const struct v4l2_subdev_pad_ops mipid02_pad_ops = { 816 .enum_mbus_code = mipid02_enum_mbus_code, 817 .get_fmt = mipid02_get_fmt, 818 .set_fmt = mipid02_set_fmt, 819 }; 820 821 static const struct v4l2_subdev_ops mipid02_subdev_ops = { 822 .video = &mipid02_video_ops, 823 .pad = &mipid02_pad_ops, 824 }; 825 826 static const struct media_entity_operations mipid02_subdev_entity_ops = { 827 .link_validate = v4l2_subdev_link_validate, 828 }; 829 830 static int mipid02_async_bound(struct v4l2_async_notifier *notifier, 831 struct v4l2_subdev *s_subdev, 832 struct v4l2_async_subdev *asd) 833 { 834 struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd); 835 struct i2c_client *client = bridge->i2c_client; 836 int source_pad; 837 int ret; 838 839 dev_dbg(&client->dev, "sensor_async_bound call %p", s_subdev); 840 841 source_pad = media_entity_get_fwnode_pad(&s_subdev->entity, 842 s_subdev->fwnode, 843 MEDIA_PAD_FL_SOURCE); 844 if (source_pad < 0) { 845 dev_err(&client->dev, "Couldn't find output pad for subdev %s\n", 846 s_subdev->name); 847 return source_pad; 848 } 849 850 ret = media_create_pad_link(&s_subdev->entity, source_pad, 851 &bridge->sd.entity, 0, 852 MEDIA_LNK_FL_ENABLED | 853 MEDIA_LNK_FL_IMMUTABLE); 854 if (ret) { 855 dev_err(&client->dev, "Couldn't create media link %d", ret); 856 return ret; 857 } 858 859 bridge->s_subdev = s_subdev; 860 861 return 0; 862 } 863 864 static void mipid02_async_unbind(struct v4l2_async_notifier *notifier, 865 struct v4l2_subdev *s_subdev, 866 struct v4l2_async_subdev *asd) 867 { 868 struct mipid02_dev *bridge = to_mipid02_dev(notifier->sd); 869 870 bridge->s_subdev = NULL; 871 } 872 873 static const struct v4l2_async_notifier_operations mipid02_notifier_ops = { 874 .bound = mipid02_async_bound, 875 .unbind = mipid02_async_unbind, 876 }; 877 878 static int mipid02_parse_rx_ep(struct mipid02_dev *bridge) 879 { 880 struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_CSI2_DPHY }; 881 struct i2c_client *client = bridge->i2c_client; 882 struct v4l2_async_subdev *asd; 883 struct device_node *ep_node; 884 int ret; 885 886 /* parse rx (endpoint 0) */ 887 ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node, 888 0, 0); 889 if (!ep_node) { 890 dev_err(&client->dev, "unable to find port0 ep"); 891 ret = -EINVAL; 892 goto error; 893 } 894 895 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep); 896 if (ret) { 897 dev_err(&client->dev, "Could not parse v4l2 endpoint %d\n", 898 ret); 899 goto error_of_node_put; 900 } 901 902 /* do some sanity checks */ 903 if (ep.bus.mipi_csi2.num_data_lanes > 2) { 904 dev_err(&client->dev, "max supported data lanes is 2 / got %d", 905 ep.bus.mipi_csi2.num_data_lanes); 906 ret = -EINVAL; 907 goto error_of_node_put; 908 } 909 910 /* register it for later use */ 911 bridge->rx = ep; 912 913 /* register async notifier so we get noticed when sensor is connected */ 914 v4l2_async_nf_init(&bridge->notifier); 915 asd = v4l2_async_nf_add_fwnode_remote(&bridge->notifier, 916 of_fwnode_handle(ep_node), 917 struct v4l2_async_subdev); 918 of_node_put(ep_node); 919 920 if (IS_ERR(asd)) { 921 dev_err(&client->dev, "fail to register asd to notifier %ld", 922 PTR_ERR(asd)); 923 return PTR_ERR(asd); 924 } 925 bridge->notifier.ops = &mipid02_notifier_ops; 926 927 ret = v4l2_async_subdev_nf_register(&bridge->sd, &bridge->notifier); 928 if (ret) 929 v4l2_async_nf_cleanup(&bridge->notifier); 930 931 return ret; 932 933 error_of_node_put: 934 of_node_put(ep_node); 935 error: 936 937 return ret; 938 } 939 940 static int mipid02_parse_tx_ep(struct mipid02_dev *bridge) 941 { 942 struct v4l2_fwnode_endpoint ep = { .bus_type = V4L2_MBUS_PARALLEL }; 943 struct i2c_client *client = bridge->i2c_client; 944 struct device_node *ep_node; 945 int ret; 946 947 /* parse tx (endpoint 2) */ 948 ep_node = of_graph_get_endpoint_by_regs(bridge->i2c_client->dev.of_node, 949 2, 0); 950 if (!ep_node) { 951 dev_err(&client->dev, "unable to find port1 ep"); 952 ret = -EINVAL; 953 goto error; 954 } 955 956 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep_node), &ep); 957 if (ret) { 958 dev_err(&client->dev, "Could not parse v4l2 endpoint\n"); 959 goto error_of_node_put; 960 } 961 962 of_node_put(ep_node); 963 bridge->tx = ep; 964 965 return 0; 966 967 error_of_node_put: 968 of_node_put(ep_node); 969 error: 970 971 return -EINVAL; 972 } 973 974 static int mipid02_probe(struct i2c_client *client) 975 { 976 struct device *dev = &client->dev; 977 struct mipid02_dev *bridge; 978 u32 clk_freq; 979 int ret; 980 981 bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL); 982 if (!bridge) 983 return -ENOMEM; 984 985 init_format(&bridge->fmt); 986 987 bridge->i2c_client = client; 988 v4l2_i2c_subdev_init(&bridge->sd, client, &mipid02_subdev_ops); 989 990 /* got and check clock */ 991 bridge->xclk = devm_clk_get(dev, "xclk"); 992 if (IS_ERR(bridge->xclk)) { 993 dev_err(dev, "failed to get xclk\n"); 994 return PTR_ERR(bridge->xclk); 995 } 996 997 clk_freq = clk_get_rate(bridge->xclk); 998 if (clk_freq < 6000000 || clk_freq > 27000000) { 999 dev_err(dev, "xclk freq must be in 6-27 Mhz range. got %d Hz\n", 1000 clk_freq); 1001 return -EINVAL; 1002 } 1003 1004 bridge->reset_gpio = devm_gpiod_get_optional(dev, "reset", 1005 GPIOD_OUT_HIGH); 1006 1007 if (IS_ERR(bridge->reset_gpio)) { 1008 dev_err(dev, "failed to get reset GPIO\n"); 1009 return PTR_ERR(bridge->reset_gpio); 1010 } 1011 1012 ret = mipid02_get_regulators(bridge); 1013 if (ret) { 1014 dev_err(dev, "failed to get regulators %d", ret); 1015 return ret; 1016 } 1017 1018 mutex_init(&bridge->lock); 1019 bridge->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1020 bridge->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 1021 bridge->sd.entity.ops = &mipid02_subdev_entity_ops; 1022 bridge->pad[0].flags = MEDIA_PAD_FL_SINK; 1023 bridge->pad[1].flags = MEDIA_PAD_FL_SINK; 1024 bridge->pad[2].flags = MEDIA_PAD_FL_SOURCE; 1025 ret = media_entity_pads_init(&bridge->sd.entity, MIPID02_PAD_NB, 1026 bridge->pad); 1027 if (ret) { 1028 dev_err(&client->dev, "pads init failed %d", ret); 1029 goto mutex_cleanup; 1030 } 1031 1032 /* enable clock, power and reset device if available */ 1033 ret = mipid02_set_power_on(bridge); 1034 if (ret) 1035 goto entity_cleanup; 1036 1037 ret = mipid02_detect(bridge); 1038 if (ret) { 1039 dev_err(&client->dev, "failed to detect mipid02 %d", ret); 1040 goto power_off; 1041 } 1042 1043 ret = mipid02_parse_tx_ep(bridge); 1044 if (ret) { 1045 dev_err(&client->dev, "failed to parse tx %d", ret); 1046 goto power_off; 1047 } 1048 1049 ret = mipid02_parse_rx_ep(bridge); 1050 if (ret) { 1051 dev_err(&client->dev, "failed to parse rx %d", ret); 1052 goto power_off; 1053 } 1054 1055 ret = v4l2_async_register_subdev(&bridge->sd); 1056 if (ret < 0) { 1057 dev_err(&client->dev, "v4l2_async_register_subdev failed %d", 1058 ret); 1059 goto unregister_notifier; 1060 } 1061 1062 dev_info(&client->dev, "mipid02 device probe successfully"); 1063 1064 return 0; 1065 1066 unregister_notifier: 1067 v4l2_async_nf_unregister(&bridge->notifier); 1068 v4l2_async_nf_cleanup(&bridge->notifier); 1069 power_off: 1070 mipid02_set_power_off(bridge); 1071 entity_cleanup: 1072 media_entity_cleanup(&bridge->sd.entity); 1073 mutex_cleanup: 1074 mutex_destroy(&bridge->lock); 1075 1076 return ret; 1077 } 1078 1079 static void mipid02_remove(struct i2c_client *client) 1080 { 1081 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1082 struct mipid02_dev *bridge = to_mipid02_dev(sd); 1083 1084 v4l2_async_nf_unregister(&bridge->notifier); 1085 v4l2_async_nf_cleanup(&bridge->notifier); 1086 v4l2_async_unregister_subdev(&bridge->sd); 1087 mipid02_set_power_off(bridge); 1088 media_entity_cleanup(&bridge->sd.entity); 1089 mutex_destroy(&bridge->lock); 1090 } 1091 1092 static const struct of_device_id mipid02_dt_ids[] = { 1093 { .compatible = "st,st-mipid02" }, 1094 { /* sentinel */ } 1095 }; 1096 MODULE_DEVICE_TABLE(of, mipid02_dt_ids); 1097 1098 static struct i2c_driver mipid02_i2c_driver = { 1099 .driver = { 1100 .name = "st-mipid02", 1101 .of_match_table = mipid02_dt_ids, 1102 }, 1103 .probe = mipid02_probe, 1104 .remove = mipid02_remove, 1105 }; 1106 1107 module_i2c_driver(mipid02_i2c_driver); 1108 1109 MODULE_AUTHOR("Mickael Guene <mickael.guene@st.com>"); 1110 MODULE_DESCRIPTION("STMicroelectronics MIPID02 CSI-2 bridge driver"); 1111 MODULE_LICENSE("GPL v2"); 1112