Lines Matching +full:mipi +full:- +full:csi
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"
37 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_link_sensor() local
40 s_asd->pixel_rate_ctrl = v4l2_ctrl_find(sd->ctrl_handler, in rkisp1_csi_link_sensor()
42 if (!s_asd->pixel_rate_ctrl) { in rkisp1_csi_link_sensor()
43 dev_err(rkisp1->dev, "No pixel rate control in subdev %s\n", in rkisp1_csi_link_sensor()
44 sd->name); in rkisp1_csi_link_sensor()
45 return -EINVAL; in rkisp1_csi_link_sensor()
48 /* Create the link from the sensor to the CSI receiver. */ in rkisp1_csi_link_sensor()
49 ret = media_create_pad_link(&sd->entity, source_pad, in rkisp1_csi_link_sensor()
50 &csi->sd.entity, RKISP1_CSI_PAD_SINK, in rkisp1_csi_link_sensor()
51 !s_asd->index ? MEDIA_LNK_FL_ENABLED : 0); in rkisp1_csi_link_sensor()
53 dev_err(csi->rkisp1->dev, "failed to link src pad of %s\n", in rkisp1_csi_link_sensor()
54 sd->name); in rkisp1_csi_link_sensor()
61 static int rkisp1_csi_config(struct rkisp1_csi *csi, in rkisp1_csi_config() argument
65 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_config()
66 unsigned int lanes = sensor->lanes; in rkisp1_csi_config()
70 return -EINVAL; in rkisp1_csi_config()
72 mipi_ctrl = RKISP1_CIF_MIPI_CTRL_NUM_LANES(lanes - 1) | in rkisp1_csi_config()
79 /* V12 could also use a newer csi2-host, but we don't want that yet */ in rkisp1_csi_config()
80 if (rkisp1->info->isp_ver == RKISP1_V12) in rkisp1_csi_config()
85 RKISP1_CIF_MIPI_DATA_SEL_DT(format->mipi_dt) | in rkisp1_csi_config()
88 /* Clear MIPI interrupts */ in rkisp1_csi_config()
101 dev_dbg(rkisp1->dev, "\n MIPI_CTRL 0x%08x\n" in rkisp1_csi_config()
113 static void rkisp1_csi_enable(struct rkisp1_csi *csi) in rkisp1_csi_enable() argument
115 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_enable()
123 static void rkisp1_csi_disable(struct rkisp1_csi *csi) in rkisp1_csi_disable() argument
125 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_disable()
128 /* Mask MIPI interrupts. */ in rkisp1_csi_disable()
136 * even after this, but it will return immediately as the MIPI in rkisp1_csi_disable()
139 synchronize_irq(rkisp1->irqs[RKISP1_IRQ_MIPI]); in rkisp1_csi_disable()
141 /* Clear MIPI interrupt status */ in rkisp1_csi_disable()
149 static int rkisp1_csi_start(struct rkisp1_csi *csi, in rkisp1_csi_start() argument
153 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_start()
159 ret = rkisp1_csi_config(csi, sensor, format); in rkisp1_csi_start()
163 pixel_clock = v4l2_ctrl_g_ctrl_int64(sensor->pixel_rate_ctrl); in rkisp1_csi_start()
165 dev_err(rkisp1->dev, "Invalid pixel rate value\n"); in rkisp1_csi_start()
166 return -EINVAL; in rkisp1_csi_start()
169 phy_mipi_dphy_get_default_config(pixel_clock, format->bus_width, in rkisp1_csi_start()
170 sensor->lanes, cfg); in rkisp1_csi_start()
171 phy_set_mode(csi->dphy, PHY_MODE_MIPI_DPHY); in rkisp1_csi_start()
172 phy_configure(csi->dphy, &opts); in rkisp1_csi_start()
173 phy_power_on(csi->dphy); in rkisp1_csi_start()
175 rkisp1_csi_enable(csi); in rkisp1_csi_start()
179 * the MIPI interface and before starting the sensor output. in rkisp1_csi_start()
186 static void rkisp1_csi_stop(struct rkisp1_csi *csi) in rkisp1_csi_stop() argument
188 rkisp1_csi_disable(csi); in rkisp1_csi_stop()
190 phy_power_off(csi->dphy); in rkisp1_csi_stop()
199 if (!rkisp1->irqs_enabled) in rkisp1_csi_isr()
218 rkisp1->csi.is_dphy_errctrl_disabled = true; in rkisp1_csi_isr()
222 * Enable DPHY errctrl interrupt again, if mipi have receive in rkisp1_csi_isr()
227 * Enable DPHY errctrl interrupt again, if mipi have receive 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 /* ----------------------------------------------------------------------------
254 if (code->pad == RKISP1_CSI_PAD_SRC) { in rkisp1_csi_enum_mbus_code()
257 if (code->index) in rkisp1_csi_enum_mbus_code()
258 return -EINVAL; in rkisp1_csi_enum_mbus_code()
262 code->code = sink_fmt->code; in rkisp1_csi_enum_mbus_code()
272 return -EINVAL; in rkisp1_csi_enum_mbus_code()
274 if (!(fmt->direction & RKISP1_ISP_SD_SINK)) in rkisp1_csi_enum_mbus_code()
277 if (code->index == pos) { in rkisp1_csi_enum_mbus_code()
278 code->code = fmt->mbus_code; in rkisp1_csi_enum_mbus_code()
285 return -EINVAL; in rkisp1_csi_enum_mbus_code()
296 sink_fmt->width = RKISP1_DEFAULT_WIDTH; in rkisp1_csi_init_state()
297 sink_fmt->height = RKISP1_DEFAULT_HEIGHT; in rkisp1_csi_init_state()
298 sink_fmt->field = V4L2_FIELD_NONE; in rkisp1_csi_init_state()
299 sink_fmt->code = RKISP1_CSI_DEF_FMT; in rkisp1_csi_init_state()
310 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_set_fmt() local
315 if (fmt->pad == RKISP1_CSI_PAD_SRC) in rkisp1_csi_set_fmt()
320 sink_fmt->code = fmt->format.code; in rkisp1_csi_set_fmt()
322 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); in rkisp1_csi_set_fmt()
323 if (!mbus_info || !(mbus_info->direction & RKISP1_ISP_SD_SINK)) { in rkisp1_csi_set_fmt()
324 sink_fmt->code = RKISP1_CSI_DEF_FMT; in rkisp1_csi_set_fmt()
325 mbus_info = rkisp1_mbus_info_get_by_code(sink_fmt->code); in rkisp1_csi_set_fmt()
328 sink_fmt->width = clamp_t(u32, fmt->format.width, in rkisp1_csi_set_fmt()
330 csi->rkisp1->info->max_width); in rkisp1_csi_set_fmt()
331 sink_fmt->height = clamp_t(u32, fmt->format.height, in rkisp1_csi_set_fmt()
333 csi->rkisp1->info->max_height); in rkisp1_csi_set_fmt()
335 fmt->format = *sink_fmt; in rkisp1_csi_set_fmt()
344 /* ----------------------------------------------------------------------------
350 struct rkisp1_csi *csi = to_rkisp1_csi(sd); in rkisp1_csi_s_stream() local
351 struct rkisp1_device *rkisp1 = csi->rkisp1; in rkisp1_csi_s_stream()
362 v4l2_subdev_call(csi->source, video, s_stream, false); in rkisp1_csi_s_stream()
364 rkisp1_csi_stop(csi); in rkisp1_csi_s_stream()
369 source_pad = media_entity_remote_source_pad_unique(&sd->entity); in rkisp1_csi_s_stream()
371 dev_dbg(rkisp1->dev, "Failed to get source for CSI: %ld\n", in rkisp1_csi_s_stream()
373 return -EPIPE; in rkisp1_csi_s_stream()
376 source = media_entity_to_v4l2_subdev(source_pad->entity); in rkisp1_csi_s_stream()
379 return -EPIPE; in rkisp1_csi_s_stream()
384 return -EPIPE; in rkisp1_csi_s_stream()
387 if (source_asd->mbus_type != V4L2_MBUS_CSI2_DPHY) in rkisp1_csi_s_stream()
388 return -EINVAL; in rkisp1_csi_s_stream()
392 format = rkisp1_mbus_info_get_by_code(sink_fmt->code); in rkisp1_csi_s_stream()
395 ret = rkisp1_csi_start(csi, source_asd, format); in rkisp1_csi_s_stream()
401 rkisp1_csi_stop(csi); in rkisp1_csi_s_stream()
405 csi->source = source; in rkisp1_csi_s_stream()
410 /* ----------------------------------------------------------------------------
439 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_register() local
444 csi->rkisp1 = rkisp1; in rkisp1_csi_register()
446 sd = &csi->sd; in rkisp1_csi_register()
448 sd->internal_ops = &rkisp1_csi_internal_ops; in rkisp1_csi_register()
449 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in rkisp1_csi_register()
450 sd->entity.ops = &rkisp1_csi_media_ops; in rkisp1_csi_register()
451 sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; in rkisp1_csi_register()
452 sd->owner = THIS_MODULE; in rkisp1_csi_register()
453 strscpy(sd->name, RKISP1_CSI_DEV_NAME, sizeof(sd->name)); in rkisp1_csi_register()
455 pads = csi->pads; in rkisp1_csi_register()
461 ret = media_entity_pads_init(&sd->entity, RKISP1_CSI_PAD_NUM, pads); in rkisp1_csi_register()
469 ret = v4l2_device_register_subdev(&csi->rkisp1->v4l2_dev, sd); in rkisp1_csi_register()
471 dev_err(sd->dev, "Failed to register csi receiver subdev\n"); in rkisp1_csi_register()
480 media_entity_cleanup(&sd->entity); in rkisp1_csi_register()
481 csi->rkisp1 = NULL; in rkisp1_csi_register()
487 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_unregister() local
489 if (!csi->rkisp1) in rkisp1_csi_unregister()
492 v4l2_device_unregister_subdev(&csi->sd); in rkisp1_csi_unregister()
493 v4l2_subdev_cleanup(&csi->sd); in rkisp1_csi_unregister()
494 media_entity_cleanup(&csi->sd.entity); in rkisp1_csi_unregister()
499 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_init() local
501 csi->rkisp1 = rkisp1; in rkisp1_csi_init()
503 csi->dphy = devm_phy_get(rkisp1->dev, "dphy"); in rkisp1_csi_init()
504 if (IS_ERR(csi->dphy)) in rkisp1_csi_init()
505 return dev_err_probe(rkisp1->dev, PTR_ERR(csi->dphy), in rkisp1_csi_init()
506 "Couldn't get the MIPI D-PHY\n"); in rkisp1_csi_init()
508 phy_init(csi->dphy); in rkisp1_csi_init()
515 struct rkisp1_csi *csi = &rkisp1->csi; in rkisp1_csi_cleanup() local
517 phy_exit(csi->dphy); in rkisp1_csi_cleanup()