Lines Matching +full:csi +full:- +full:2

1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Rockchip ISP1 Driver - CSI-2 Receiver
16 #include <linux/phy/phy-mipi-dphy.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-fwnode.h>
21 #include "rkisp1-common.h"
22 #include "rkisp1-csi.h"
34 rkisp1_csi_get_pad_fmt(struct rkisp1_csi *csi, in rkisp1_csi_get_pad_fmt() argument
39 .pads = csi->pad_cfg in rkisp1_csi_get_pad_fmt()
42 lockdep_assert_held(&csi->lock); in rkisp1_csi_get_pad_fmt()
45 return v4l2_subdev_get_try_format(&csi->sd, sd_state, pad); in rkisp1_csi_get_pad_fmt()
47 return v4l2_subdev_get_try_format(&csi->sd, &state, pad); in rkisp1_csi_get_pad_fmt()
54 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_link_sensor() local
57 s_asd->pixel_rate_ctrl = v4l2_ctrl_find(sd->ctrl_handler, in rkisp1_csi_link_sensor()
59 if (!s_asd->pixel_rate_ctrl) { in rkisp1_csi_link_sensor()
60 dev_err(rkisp1->dev, "No pixel rate control in subdev %s\n", in rkisp1_csi_link_sensor()
61 sd->name); in rkisp1_csi_link_sensor()
62 return -EINVAL; in rkisp1_csi_link_sensor()
65 /* Create the link from the sensor to the CSI receiver. */ in rkisp1_csi_link_sensor()
66 ret = media_create_pad_link(&sd->entity, source_pad, in rkisp1_csi_link_sensor()
67 &csi->sd.entity, RKISP1_CSI_PAD_SINK, in rkisp1_csi_link_sensor()
68 !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0); in rkisp1_csi_link_sensor()
70 dev_err(csi->rkisp1->dev, "failed to link src pad of %s\n", in rkisp1_csi_link_sensor()
71 sd->name); in rkisp1_csi_link_sensor()
78 static int rkisp1_csi_config(struct rkisp1_csi *csi, in rkisp1_csi_config() argument
81 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_config()
82 unsigned int lanes = sensor->lanes; in rkisp1_csi_config()
86 return -EINVAL; in rkisp1_csi_config()
88 mipi_ctrl = RKISP1_CIF_MIPI_CTRL_NUM_LANES(lanes - 1) | in rkisp1_csi_config()
95 /* V12 could also use a newer csi2-host, but we don't want that yet */ in rkisp1_csi_config()
96 if (rkisp1->info->isp_ver == RKISP1_V12) in rkisp1_csi_config()
101 RKISP1_CIF_MIPI_DATA_SEL_DT(csi->sink_fmt->mipi_dt) | in rkisp1_csi_config()
117 dev_dbg(rkisp1->dev, "\n MIPI_CTRL 0x%08x\n" in rkisp1_csi_config()
129 static void rkisp1_csi_enable(struct rkisp1_csi *csi) in rkisp1_csi_enable() argument
131 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_enable()
139 static void rkisp1_csi_disable(struct rkisp1_csi *csi) in rkisp1_csi_disable() argument
141 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_disable()
153 static int rkisp1_csi_start(struct rkisp1_csi *csi, in rkisp1_csi_start() argument
156 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_start()
162 ret = rkisp1_csi_config(csi, sensor); in rkisp1_csi_start()
166 pixel_clock = v4l2_ctrl_g_ctrl_int64(sensor->pixel_rate_ctrl); in rkisp1_csi_start()
168 dev_err(rkisp1->dev, "Invalid pixel rate value\n"); in rkisp1_csi_start()
169 return -EINVAL; in rkisp1_csi_start()
172 phy_mipi_dphy_get_default_config(pixel_clock, csi->sink_fmt->bus_width, in rkisp1_csi_start()
173 sensor->lanes, cfg); in rkisp1_csi_start()
174 phy_set_mode(csi->dphy, PHY_MODE_MIPI_DPHY); in rkisp1_csi_start()
175 phy_configure(csi->dphy, &opts); in rkisp1_csi_start()
176 phy_power_on(csi->dphy); in rkisp1_csi_start()
178 rkisp1_csi_enable(csi); in rkisp1_csi_start()
189 static void rkisp1_csi_stop(struct rkisp1_csi *csi) in rkisp1_csi_stop() argument
191 rkisp1_csi_disable(csi); in rkisp1_csi_stop()
193 phy_power_off(csi->dphy); in rkisp1_csi_stop()
218 rkisp1->csi.is_dphy_errctrl_disabled = true; in rkisp1_csi_isr()
230 if (rkisp1->csi.is_dphy_errctrl_disabled) { in rkisp1_csi_isr()
234 rkisp1->csi.is_dphy_errctrl_disabled = false; in rkisp1_csi_isr()
237 rkisp1->debug.mipi_error++; in rkisp1_csi_isr()
243 /* ----------------------------------------------------------------------------
251 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_enum_mbus_code() local
255 if (code->pad == RKISP1_CSI_PAD_SRC) { in rkisp1_csi_enum_mbus_code()
258 if (code->index) in rkisp1_csi_enum_mbus_code()
259 return -EINVAL; in rkisp1_csi_enum_mbus_code()
261 mutex_lock(&csi->lock); in rkisp1_csi_enum_mbus_code()
263 sink_fmt = rkisp1_csi_get_pad_fmt(csi, sd_state, in rkisp1_csi_enum_mbus_code()
265 code->which); in rkisp1_csi_enum_mbus_code()
266 code->code = sink_fmt->code; in rkisp1_csi_enum_mbus_code()
268 mutex_unlock(&csi->lock); in rkisp1_csi_enum_mbus_code()
278 return -EINVAL; in rkisp1_csi_enum_mbus_code()
280 if (!(fmt->direction & RKISP1_ISP_SD_SINK)) in rkisp1_csi_enum_mbus_code()
283 if (code->index == pos) { in rkisp1_csi_enum_mbus_code()
284 code->code = fmt->mbus_code; in rkisp1_csi_enum_mbus_code()
291 return -EINVAL; in rkisp1_csi_enum_mbus_code()
304 sink_fmt->width = RKISP1_DEFAULT_WIDTH; in rkisp1_csi_init_config()
305 sink_fmt->height = RKISP1_DEFAULT_HEIGHT; in rkisp1_csi_init_config()
306 sink_fmt->field = V4L2_FIELD_NONE; in rkisp1_csi_init_config()
307 sink_fmt->code = RKISP1_CSI_DEF_FMT; in rkisp1_csi_init_config()
318 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_get_fmt() local
320 mutex_lock(&csi->lock); in rkisp1_csi_get_fmt()
321 fmt->format = *rkisp1_csi_get_pad_fmt(csi, sd_state, fmt->pad, in rkisp1_csi_get_fmt()
322 fmt->which); in rkisp1_csi_get_fmt()
323 mutex_unlock(&csi->lock); in rkisp1_csi_get_fmt()
332 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_set_fmt() local
337 if (fmt->pad == RKISP1_CSI_PAD_SRC) in rkisp1_csi_set_fmt()
340 mutex_lock(&csi->lock); in rkisp1_csi_set_fmt()
342 sink_fmt = rkisp1_csi_get_pad_fmt(csi, sd_state, RKISP1_CSI_PAD_SINK, in rkisp1_csi_set_fmt()
343 fmt->which); in rkisp1_csi_set_fmt()
345 sink_fmt->code = fmt->format.code; in rkisp1_csi_set_fmt()
347 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); in rkisp1_csi_set_fmt()
348 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) { in rkisp1_csi_set_fmt()
349 sink_fmt->code = RKISP1_CSI_DEF_FMT; in rkisp1_csi_set_fmt()
350 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); in rkisp1_csi_set_fmt()
353 sink_fmt->width = clamp_t(u32, fmt->format.width, in rkisp1_csi_set_fmt()
356 sink_fmt->height = clamp_t(u32, fmt->format.height, in rkisp1_csi_set_fmt()
360 fmt->format = *sink_fmt; in rkisp1_csi_set_fmt()
362 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) in rkisp1_csi_set_fmt()
363 csi->sink_fmt = mbus_info; in rkisp1_csi_set_fmt()
366 src_fmt = rkisp1_csi_get_pad_fmt(csi, sd_state, RKISP1_CSI_PAD_SRC, in rkisp1_csi_set_fmt()
367 fmt->which); in rkisp1_csi_set_fmt()
370 mutex_unlock(&csi->lock); in rkisp1_csi_set_fmt()
375 /* ----------------------------------------------------------------------------
381 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_s_stream() local
382 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_s_stream()
389 v4l2_subdev_call(csi->source, video, s_stream, false); in rkisp1_csi_s_stream()
391 rkisp1_csi_stop(csi); in rkisp1_csi_s_stream()
396 source_pad = media_entity_remote_source_pad_unique(&sd->entity); in rkisp1_csi_s_stream()
398 dev_dbg(rkisp1->dev, "Failed to get source for CSI: %ld\n", in rkisp1_csi_s_stream()
400 return -EPIPE; in rkisp1_csi_s_stream()
403 source = media_entity_to_v4l2_subdev(source_pad->entity); in rkisp1_csi_s_stream()
406 return -EPIPE; in rkisp1_csi_s_stream()
409 source_asd = container_of(source->asd, struct rkisp1_sensor_async, asd); in rkisp1_csi_s_stream()
410 if (source_asd->mbus_type != V4L2_MBUS_CSI2_DPHY) in rkisp1_csi_s_stream()
411 return -EINVAL; in rkisp1_csi_s_stream()
413 mutex_lock(&csi->lock); in rkisp1_csi_s_stream()
414 ret = rkisp1_csi_start(csi, source_asd); in rkisp1_csi_s_stream()
415 mutex_unlock(&csi->lock); in rkisp1_csi_s_stream()
421 rkisp1_csi_stop(csi); in rkisp1_csi_s_stream()
425 csi->source = source; in rkisp1_csi_s_stream()
430 /* ----------------------------------------------------------------------------
456 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_register() local
462 csi->rkisp1 = rkisp1; in rkisp1_csi_register()
463 mutex_init(&csi->lock); in rkisp1_csi_register()
465 sd = &csi->sd; in rkisp1_csi_register()
467 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in rkisp1_csi_register()
468 sd->entity.ops = &rkisp1_csi_media_ops; in rkisp1_csi_register()
469 sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in rkisp1_csi_register()
470 sd->owner = THIS_MODULE; in rkisp1_csi_register()
471 strscpy(sd->name, RKISP1_CSI_DEV_NAME, sizeof(sd->name)); in rkisp1_csi_register()
473 pads = csi->pads; in rkisp1_csi_register()
479 csi->sink_fmt = rkisp1_mbus_info_get_by_code(RKISP1_CSI_DEF_FMT); in rkisp1_csi_register()
481 ret = media_entity_pads_init(&sd->entity, RKISP1_CSI_PAD_NUM, pads); in rkisp1_csi_register()
485 state.pads = csi->pad_cfg; in rkisp1_csi_register()
488 ret = v4l2_device_register_subdev(&csi->rkisp1->v4l2_dev, sd); in rkisp1_csi_register()
490 dev_err(sd->dev, "Failed to register csi receiver subdev\n"); in rkisp1_csi_register()
497 media_entity_cleanup(&sd->entity); in rkisp1_csi_register()
498 mutex_destroy(&csi->lock); in rkisp1_csi_register()
499 csi->rkisp1 = NULL; in rkisp1_csi_register()
505 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_unregister() local
507 if (!csi->rkisp1) in rkisp1_csi_unregister()
510 v4l2_device_unregister_subdev(&csi->sd); in rkisp1_csi_unregister()
511 media_entity_cleanup(&csi->sd.entity); in rkisp1_csi_unregister()
512 mutex_destroy(&csi->lock); in rkisp1_csi_unregister()
517 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_init() local
519 csi->rkisp1 = rkisp1; in rkisp1_csi_init()
521 csi->dphy = devm_phy_get(rkisp1->dev, "dphy"); in rkisp1_csi_init()
522 if (IS_ERR(csi->dphy)) in rkisp1_csi_init()
523 return dev_err_probe(rkisp1->dev, PTR_ERR(csi->dphy), in rkisp1_csi_init()
524 "Couldn't get the MIPI D-PHY\n"); in rkisp1_csi_init()
526 phy_init(csi->dphy); in rkisp1_csi_init()
533 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_cleanup() local
535 phy_exit(csi->dphy); in rkisp1_csi_cleanup()