diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen2.c b/drivers/media/platform/qcom/camss/camss-csid-gen2.c index eadcb2f7e3aaa..c9908184a1003 100644 --- a/drivers/media/platform/qcom/camss/camss-csid-gen2.c +++ b/drivers/media/platform/qcom/camss/camss-csid-gen2.c @@ -7,6 +7,7 @@ * Copyright (C) 2020 Linaro Ltd. */ #include +#include #include #include #include @@ -171,17 +172,22 @@ #define TPG_COLOR_BOX_CFG_MODE 0 #define TPG_COLOR_BOX_PATTERN_SEL 2 +#define CSID_TPG_COMMON_GEN_CFG 0x648 + static void __csid_configure_rx(struct csid_device *csid, struct csid_phy_config *phy, int vc) { u8 lane_cnt = csid->phy.lane_cnt; + u32 lane_assign = phy->lane_assign; int val; - if (!lane_cnt) + if (!lane_cnt || csid->testgen.enabled) { lane_cnt = 4; + lane_assign = 0x3210; + } val = (lane_cnt - 1) << CSI2_RX_CFG0_NUM_ACTIVE_LANES; - val |= phy->lane_assign << CSI2_RX_CFG0_DL0_INPUT_SEL; + val |= lane_assign << CSI2_RX_CFG0_DL0_INPUT_SEL; val |= phy->csiphy_id << CSI2_RX_CFG0_PHY_NUM_SEL; writel_relaxed(val, csid->base + CSID_CSI2_RX_CFG0); @@ -240,11 +246,14 @@ static void __csid_configure_testgen(struct csid_device *csid, u8 enable, u8 por val |= format->decode_format << TPG_DT_n_CFG_2_ENCODE_FORMAT; writel_relaxed(val, csid->base + CSID_TPG_DT_n_CFG_2(0)); - writel_relaxed(0, csid->base + CSID_TPG_COLOR_BARS_CFG); + writel_relaxed(1 << TPG_COLOR_BARS_CFG_SPLIT_EN, + csid->base + CSID_TPG_COLOR_BARS_CFG); writel_relaxed(0, csid->base + CSID_TPG_COLOR_BOX_CFG); + writel_relaxed(0, csid->base + CSID_TPG_COMMON_GEN_CFG); - val = enable << TPG_CTRL_TEST_EN; + /* Prepare TPG configuration with generator disabled; enabled after RDI is armed */ + val = 0 << TPG_CTRL_TEST_EN; val |= 1 << TPG_CTRL_FS_PKT_EN; val |= 1 << TPG_CTRL_FE_PKT_EN; val |= (lane_cnt - 1) << TPG_CTRL_NUM_ACTIVE_LANES; @@ -279,8 +288,15 @@ static void __csid_configure_rdi_stream(struct csid_device *csid, u8 enable, u8 val = 1 << RDI_CFG0_BYTE_CNTR_EN; val |= 1 << RDI_CFG0_FORMAT_MEASURE_EN; val |= 1 << RDI_CFG0_TIMESTAMP_EN; - /* note: for non-RDI path, this should be format->decode_format */ - val |= DECODE_FORMAT_PAYLOAD_ONLY << RDI_CFG0_DECODE_FORMAT; + if (input_format->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE || + input_format->code == MEDIA_BUS_FMT_Y10_2X8_PADHI_LE) { + val |= format->decode_format << RDI_CFG0_DECODE_FORMAT; + val |= PLAIN_FORMAT_PLAIN16 << RDI_CFG0_PLAIN_FORMAT; + val |= PLAIN_ALIGNMENT_LSB << RDI_CFG0_PLAIN_ALIGNMENT; + } else { + /* note: for non-RDI path, this should be format->decode_format */ + val |= DECODE_FORMAT_PAYLOAD_ONLY << RDI_CFG0_DECODE_FORMAT; + } val |= format->data_type << RDI_CFG0_DATA_TYPE; val |= vc << RDI_CFG0_VIRTUAL_CHANNEL; val |= dt_id << RDI_CFG0_DT_ID; @@ -290,53 +306,115 @@ static void __csid_configure_rdi_stream(struct csid_device *csid, u8 enable, u8 val = 2 << RDI_CFG1_TIMESTAMP_STB_SEL; writel_relaxed(val, csid->base + CSID_RDI_CFG1(port)); - val = 1; - writel_relaxed(val, csid->base + CSID_RDI_FRM_DROP_PERIOD(port)); - - val = 0; - writel_relaxed(val, csid->base + CSID_RDI_FRM_DROP_PATTERN(port)); - - val = 1; - writel_relaxed(val, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PERIOD(port)); - - val = 0; - writel_relaxed(val, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PATTERN(port)); - - val = 1; - writel_relaxed(val, csid->base + CSID_RDI_RPP_PIX_DROP_PERIOD(port)); + /* Disable frame/pixel/line dropping and IRQ subsampling */ + writel_relaxed(1, csid->base + CSID_RDI_FRM_DROP_PERIOD(port)); + writel_relaxed(0, csid->base + CSID_RDI_FRM_DROP_PATTERN(port)); - val = 0; - writel_relaxed(val, csid->base + CSID_RDI_RPP_PIX_DROP_PATTERN(port)); + writel_relaxed(1, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PERIOD(port)); + writel_relaxed(0, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PATTERN(port)); - val = 1; - writel_relaxed(val, csid->base + CSID_RDI_RPP_LINE_DROP_PERIOD(port)); + writel_relaxed(0, csid->base + CSID_RDI_RPP_PIX_DROP_PATTERN(port)); + writel_relaxed(1, csid->base + CSID_RDI_RPP_PIX_DROP_PERIOD(port)); - val = 0; - writel_relaxed(val, csid->base + CSID_RDI_RPP_LINE_DROP_PATTERN(port)); + writel_relaxed(0, csid->base + CSID_RDI_RPP_LINE_DROP_PATTERN(port)); + writel_relaxed(1, csid->base + CSID_RDI_RPP_LINE_DROP_PERIOD(port)); val = 0; writel_relaxed(val, csid->base + CSID_RDI_CTRL(port)); val = readl_relaxed(csid->base + CSID_RDI_CFG0(port)); - val |= enable << RDI_CFG0_ENABLE; + if (enable) + val |= BIT(RDI_CFG0_ENABLE); + else + val &= ~BIT(RDI_CFG0_ENABLE); writel_relaxed(val, csid->base + CSID_RDI_CFG0(port)); } +static void csid_reset_retain_sw_reg(struct csid_device *csid) +{ + u32 status = 0; + int to = 1000; + + /* + * Strobe 0x1e preserves SW registers while resetting the data-path + * state machine. + */ + writel_relaxed(1, csid->base + CSID_TOP_IRQ_CLEAR); + writel_relaxed(1, csid->base + CSID_IRQ_CMD); + writel_relaxed(0x1e, csid->base + CSID_RST_STROBES); + /* Ensure reset strobe reaches hardware before polling IRQ status */ + wmb(); + + while (to-- > 0) { + status = readl_relaxed(csid->base + CSID_TOP_IRQ_STATUS); + if (status & 0x1) + break; + udelay(10); + } + + writel_relaxed(1, csid->base + CSID_TOP_IRQ_CLEAR); + writel_relaxed(1, csid->base + CSID_IRQ_CMD); +} + static void csid_configure_stream(struct csid_device *csid, u8 enable) { struct csid_testgen_config *tg = &csid->testgen; u8 i; + writel_relaxed(~0u, csid->base + CSID_CSI2_RX_IRQ_CLEAR); + writel_relaxed(~0u, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(0)); + writel_relaxed(1 << IRQ_CMD_CLEAR, csid->base + CSID_IRQ_CMD); + /* Loop through all enabled ports and configure a stream for each */ - for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++) - if (csid->phy.en_vc & BIT(i)) { + for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++) { + if (!(csid->phy.en_vc & BIT(i))) + continue; + + if (enable) { if (tg->enabled) __csid_configure_testgen(csid, enable, i, 0); - __csid_configure_rdi_stream(csid, enable, i, 0); __csid_configure_rx(csid, &csid->phy, 0); + __csid_configure_rdi_stream(csid, enable, i, 0); + /* Ensure RX/RDI programming completes before datapath reset */ + wmb(); + + csid_reset_retain_sw_reg(csid); + + /* Resume RDI channel at frame boundary */ __csid_ctrl_rdi(csid, enable, i); + /* Ensure RDI resume command completes before TPG enable */ + wmb(); + + if (tg->enabled) { + u8 lane_cnt = csid->phy.lane_cnt ? csid->phy.lane_cnt : 4; + u32 tpg_val = (1 << TPG_CTRL_TEST_EN) | + (1 << TPG_CTRL_FS_PKT_EN) | + (1 << TPG_CTRL_FE_PKT_EN) | + ((lane_cnt - 1) << TPG_CTRL_NUM_ACTIVE_LANES) | + (0x80 << TPG_CTRL_CYCLES_BETWEEN_PKTS) | + (0x4 << TPG_CTRL_NUM_TRAIL_BYTES); + + writel_relaxed(tpg_val, csid->base + CSID_TPG_CTRL); + /* Ensure TPG start reaches hardware */ + wmb(); + } + } else { + if (tg->enabled) { + writel_relaxed(0, csid->base + CSID_TPG_CTRL); + /* Ensure TPG stop reaches hardware before delay */ + wmb(); + udelay(10); + } + __csid_ctrl_rdi(csid, 0, i); + /* Ensure RDI halt reaches hardware before waiting for EOF */ + wmb(); + msleep(40); + __csid_configure_rdi_stream(csid, 0, i, 0); + /* Ensure RDI stream disable completes before returning */ + wmb(); } + } } static int csid_configure_testgen_pattern(struct csid_device *csid, s32 val) diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c index 48459b46a981b..62559a5cfc27f 100644 --- a/drivers/media/platform/qcom/camss/camss-csid.c +++ b/drivers/media/platform/qcom/camss/camss-csid.c @@ -173,6 +173,20 @@ static const struct csid_format_info formats_4_1[] = { 10, 1, }, + { + MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, + { + MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, }; static const struct csid_format_info formats_4_7[] = { @@ -323,6 +337,20 @@ static const struct csid_format_info formats_4_7[] = { 10, 1, }, + { + MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, + { + MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, }; static const struct csid_format_info formats_gen2[] = { @@ -424,6 +452,20 @@ static const struct csid_format_info formats_gen2[] = { 10, 1, }, + { + MEDIA_BUS_FMT_Y10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, + { + MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, + MIPI_CSI2_DT_RAW10, + DECODE_FORMAT_UNCOMPRESSED_10_BIT, + 16, + 1, + }, { MEDIA_BUS_FMT_SBGGR12_1X12, MIPI_CSI2_DT_RAW12, @@ -694,53 +736,80 @@ static int csid_set_power(struct v4l2_subdev *sd, int on) if (ret < 0) return ret; - ret = pm_runtime_resume_and_get(dev); - if (ret < 0) - return ret; + if (!csid->powered_on) { + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) { + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } - ret = regulator_bulk_enable(csid->num_supplies, - csid->supplies); - if (ret < 0) { - pm_runtime_put_sync(dev); - return ret; - } + ret = regulator_bulk_enable(csid->num_supplies, + csid->supplies); + if (ret < 0) { + pm_runtime_put_sync(dev); + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } - ret = csid_set_clock_rates(csid); - if (ret < 0) { - regulator_bulk_disable(csid->num_supplies, - csid->supplies); - pm_runtime_put_sync(dev); - return ret; - } + ret = csid_set_clock_rates(csid); + if (ret < 0) { + regulator_bulk_disable(csid->num_supplies, + csid->supplies); + pm_runtime_put_sync(dev); + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } - ret = camss_enable_clocks(csid->nclocks, csid->clock, dev); - if (ret < 0) { - regulator_bulk_disable(csid->num_supplies, - csid->supplies); - pm_runtime_put_sync(dev); - return ret; + ret = camss_enable_clocks(csid->nclocks, csid->clock, dev); + if (ret < 0) { + regulator_bulk_disable(csid->num_supplies, + csid->supplies); + pm_runtime_put_sync(dev); + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } + + enable_irq(csid->irq); + if (camss->res->version == CAMSS_8150) + csid->powered_on = true; + } else { + ret = pm_runtime_resume_and_get(dev); + if (ret < 0) { + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } + + ret = csid_set_clock_rates(csid); + if (ret < 0) { + pm_runtime_put_sync(dev); + csid->res->parent_dev_ops->put(camss, csid->id); + return ret; + } } csid->phy.need_vc_update = true; - enable_irq(csid->irq); - ret = csid->res->hw_ops->reset(csid); if (ret < 0) { - disable_irq(csid->irq); - camss_disable_clocks(csid->nclocks, csid->clock); - regulator_bulk_disable(csid->num_supplies, - csid->supplies); + if (!csid->powered_on) { + disable_irq(csid->irq); + camss_disable_clocks(csid->nclocks, csid->clock); + regulator_bulk_disable(csid->num_supplies, + csid->supplies); + } pm_runtime_put_sync(dev); + csid->res->parent_dev_ops->put(camss, csid->id); return ret; } csid->res->hw_ops->hw_version(csid); } else { - disable_irq(csid->irq); - camss_disable_clocks(csid->nclocks, csid->clock); - regulator_bulk_disable(csid->num_supplies, - csid->supplies); + if (!csid->powered_on) { + disable_irq(csid->irq); + camss_disable_clocks(csid->nclocks, csid->clock); + regulator_bulk_disable(csid->num_supplies, + csid->supplies); + } pm_runtime_put_sync(dev); csid->res->parent_dev_ops->put(camss, csid->id); } @@ -777,7 +846,7 @@ static int csid_set_stream(struct v4l2_subdev *sd, int enable) return -ENOLINK; } - if (csid->phy.need_vc_update) { + if (csid->phy.need_vc_update || !enable) { csid->res->hw_ops->configure_stream(csid, enable); csid->phy.need_vc_update = false; } diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h index 5296b10f6bac8..cdec5064358b3 100644 --- a/drivers/media/platform/qcom/camss/camss-csid.h +++ b/drivers/media/platform/qcom/camss/camss-csid.h @@ -162,6 +162,7 @@ struct csid_device { struct completion reset_complete; struct csid_testgen_config testgen; bool tpg_linked; + bool powered_on; struct csid_phy_config phy; struct v4l2_mbus_framefmt fmt[MSM_CSID_PADS_NUM]; struct v4l2_ctrl_handler ctrls; diff --git a/drivers/media/platform/qcom/camss/camss-csiphy.c b/drivers/media/platform/qcom/camss/camss-csiphy.c index 539ac4888b608..48ffbdf9e6def 100644 --- a/drivers/media/platform/qcom/camss/camss-csiphy.c +++ b/drivers/media/platform/qcom/camss/camss-csiphy.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include #include #include #include @@ -131,10 +133,10 @@ static u8 csiphy_get_bpp(const struct csiphy_format_info *formats, } /* - * csiphy_set_clock_rates - Calculate and set clock rates on CSIPHY module + * csiphy_set_clock_rates_legacy - Calculate and set clock rates on CSIPHY module * @csiphy: CSIPHY device */ -static int csiphy_set_clock_rates(struct csiphy_device *csiphy) +static int csiphy_set_clock_rates_legacy(struct csiphy_device *csiphy) { struct device *dev = csiphy->camss->dev; s64 link_freq; @@ -200,7 +202,7 @@ static int csiphy_set_clock_rates(struct csiphy_device *csiphy) * * Return 0 on success or a negative error code otherwise */ -static int csiphy_set_power(struct v4l2_subdev *sd, int on) +static int csiphy_set_power_legacy(struct v4l2_subdev *sd, int on) { struct csiphy_device *csiphy = v4l2_get_subdevdata(sd); struct device *dev = csiphy->camss->dev; @@ -219,7 +221,7 @@ static int csiphy_set_power(struct v4l2_subdev *sd, int on) return ret; } - ret = csiphy_set_clock_rates(csiphy); + ret = csiphy_set_clock_rates_legacy(csiphy); if (ret < 0) { regulator_bulk_disable(csiphy->num_supplies, csiphy->supplies); @@ -254,7 +256,7 @@ static int csiphy_set_power(struct v4l2_subdev *sd, int on) } /* - * csiphy_stream_on - Enable streaming on CSIPHY module + * csiphy_stream_on_legacy - Enable streaming on CSIPHY module * @csiphy: CSIPHY device * * Helper function to enable streaming on CSIPHY module. @@ -262,7 +264,7 @@ static int csiphy_set_power(struct v4l2_subdev *sd, int on) * * Return 0 on success or a negative error code otherwise */ -static int csiphy_stream_on(struct csiphy_device *csiphy) +static int csiphy_stream_on_legacy(struct csiphy_device *csiphy) { struct csiphy_config *cfg = &csiphy->cfg; s64 link_freq; @@ -306,11 +308,94 @@ static int csiphy_stream_on(struct csiphy_device *csiphy) * * Helper function to disable streaming on CSIPHY module */ -static void csiphy_stream_off(struct csiphy_device *csiphy) +static void csiphy_stream_off_legacy(struct csiphy_device *csiphy) { csiphy->res->hw_ops->lanes_disable(csiphy, &csiphy->cfg); } +/* + * csiphy_stream_on - Enable streaming on external generic PHY CSIPHY module + * @csiphy: CSIPHY device + * + * Return 0 on success or a negative error code otherwise + */ +static int csiphy_stream_on(struct csiphy_device *csiphy) +{ + u8 bpp = csiphy_get_bpp(csiphy->res->formats->formats, + csiphy->res->formats->nformats, + csiphy->fmt[MSM_CSIPHY_PAD_SINK].code); + struct csiphy_lanes_cfg *lncfg = &csiphy->cfg.csi2->lane_cfg; + struct phy_configure_opts_mipi_dphy *dphy_cfg; + union phy_configure_opts dphy_opts = { 0 }; + struct device *dev = csiphy->camss->dev; + u8 num_lanes = lncfg->num_data; + s64 link_freq; + int ret; + + dphy_cfg = &dphy_opts.mipi_dphy; + + link_freq = camss_get_link_freq(&csiphy->subdev.entity, bpp, num_lanes); + if (link_freq <= 0) { + dev_err(dev, + "Cannot get CSI2 transmitter's link frequency\n"); + return -EINVAL; + } + + ret = phy_mipi_dphy_get_default_config_for_hsclk(link_freq, num_lanes, + dphy_cfg); + if (ret < 0) { + dev_err(dev, "Invalid config for link frequency %lld\n", + link_freq); + return ret; + } + + ret = phy_set_mode(csiphy->phy, PHY_MODE_MIPI_DPHY); + if (ret) { + dev_err(dev, "Failed to set phy mode\n"); + return ret; + } + + ret = phy_configure(csiphy->phy, &dphy_opts); + if (ret) { + dev_err(dev, "failed to configure MIPI D-PHY\n"); + return ret; + } + + return phy_power_on(csiphy->phy); +} + +/* + * csiphy_stream_off - Disable streaming on external generic PHY CSIPHY module + * @csiphy: CSIPHY device + */ +static void csiphy_stream_off(struct csiphy_device *csiphy) +{ + phy_power_off(csiphy->phy); +} + +/* + * csiphy_set_stream_legacy - Enable/disable streaming on legacy CSIPHY module + * @sd: CSIPHY V4L2 subdevice + * @enable: Requested streaming state + * + * Return 0 on success or a negative error code otherwise + */ +static int csiphy_set_stream_legacy(struct v4l2_subdev *sd, int enable) +{ + struct csiphy_device *csiphy = v4l2_get_subdevdata(sd); + int ret = 0; + + if (enable) { + ret = csiphy_stream_on_legacy(csiphy); + if (!ret) + camss_set_privacy_led(csiphy->camss, csiphy->id, true); + } else { + camss_set_privacy_led(csiphy->camss, csiphy->id, false); + csiphy_stream_off_legacy(csiphy); + } + + return ret; +} /* * csiphy_set_stream - Enable/disable streaming on CSIPHY module @@ -324,10 +409,14 @@ static int csiphy_set_stream(struct v4l2_subdev *sd, int enable) struct csiphy_device *csiphy = v4l2_get_subdevdata(sd); int ret = 0; - if (enable) + if (enable) { ret = csiphy_stream_on(csiphy); - else + if (!ret) + camss_set_privacy_led(csiphy->camss, csiphy->id, true); + } else { + camss_set_privacy_led(csiphy->camss, csiphy->id, false); csiphy_stream_off(csiphy); + } return ret; } @@ -572,16 +661,18 @@ csiphy_match_clock_name(const char *clock_name, const char *format, ...) } /* - * msm_csiphy_subdev_init - Initialize CSIPHY device structure and resources + * msm_csiphy_subdev_init_legacy - Initialize CSIPHY device structure and resources + * @camss: CAMSS device * @csiphy: CSIPHY device * @res: CSIPHY module resources table * @id: CSIPHY module id * * Return 0 on success or a negative error code otherwise */ -int msm_csiphy_subdev_init(struct camss *camss, - struct csiphy_device *csiphy, - const struct camss_subdev_resources *res, u8 id) +int msm_csiphy_subdev_init_legacy(struct camss *camss, + struct csiphy_device *csiphy, + const struct camss_subdev_resources *res, + u8 id) { struct device *dev = camss->dev; struct platform_device *pdev = to_platform_device(dev); @@ -709,6 +800,61 @@ int msm_csiphy_subdev_init(struct camss *camss, return ret; } +/* + * msm_csiphy_subdev_init - Initialize CSIPHY subdev backed by a generic PHY + * @camss: CAMSS structure + * @port: DT port index + * + * Return 0 on success or absence of link, negative error code otherwise + */ +int msm_csiphy_subdev_init(struct camss *camss, u8 port) +{ + const struct camss_subdev_resources *res = &camss->res->csiphy_res[port]; + struct csiphy_device *csiphy = &camss->csiphy[port]; + struct device *dev = camss->dev; + struct device_node *ep, *port_np, *remote; + int ret; + + ep = of_graph_get_endpoint_by_regs(dev->of_node, port, -1); + if (!ep) + return 0; + + port_np = of_get_parent(ep); + remote = of_graph_get_remote_port_parent(ep); + of_node_put(ep); + if (!remote) { + of_node_put(port_np); + return 0; + } + + if (!of_device_is_available(remote)) { + of_node_put(remote); + of_node_put(port_np); + return 0; + } + + csiphy->phy = devm_of_phy_get_by_index(dev, port_np, 0); + of_node_put(port_np); + if (IS_ERR(csiphy->phy)) + csiphy->phy = devm_of_phy_get_by_index(dev, dev->of_node, port); + if (IS_ERR(csiphy->phy)) + csiphy->phy = devm_of_phy_get(dev, remote, NULL); + of_node_put(remote); + if (IS_ERR(csiphy->phy)) { + ret = PTR_ERR(csiphy->phy); + dev_err(dev, "port %d: phy lookup failed: %d\n", port, ret); + return ret; + } + + csiphy->camss = camss; + csiphy->id = res->csiphy.id; + csiphy->res = &res->csiphy; + + snprintf(csiphy->name, ARRAY_SIZE(csiphy->name), "csi%d", csiphy->id); + + return 0; +} + /* * csiphy_link_setup - Setup CSIPHY connections * @entity: Pointer to media entity structure @@ -743,8 +889,12 @@ static int csiphy_link_setup(struct media_entity *entity, return 0; } -static const struct v4l2_subdev_core_ops csiphy_core_ops = { - .s_power = csiphy_set_power, +static const struct v4l2_subdev_core_ops csiphy_core_ops_legacy = { + .s_power = csiphy_set_power_legacy, +}; + +static const struct v4l2_subdev_video_ops csiphy_video_ops_legacy = { + .s_stream = csiphy_set_stream_legacy, }; static const struct v4l2_subdev_video_ops csiphy_video_ops = { @@ -758,8 +908,13 @@ static const struct v4l2_subdev_pad_ops csiphy_pad_ops = { .set_fmt = csiphy_set_format, }; +static const struct v4l2_subdev_ops csiphy_v4l2_ops_legacy = { + .core = &csiphy_core_ops_legacy, + .video = &csiphy_video_ops_legacy, + .pad = &csiphy_pad_ops, +}; + static const struct v4l2_subdev_ops csiphy_v4l2_ops = { - .core = &csiphy_core_ops, .video = &csiphy_video_ops, .pad = &csiphy_pad_ops, }; @@ -785,10 +940,14 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy, { struct v4l2_subdev *sd = &csiphy->subdev; struct media_pad *pads = csiphy->pads; - struct device *dev = csiphy->camss->dev; + struct camss *camss = csiphy->camss; + struct device *dev = camss->dev; int ret; - v4l2_subdev_init(sd, &csiphy_v4l2_ops); + if (camss->legacy_phy) + v4l2_subdev_init(sd, &csiphy_v4l2_ops_legacy); + else + v4l2_subdev_init(sd, &csiphy_v4l2_ops); sd->internal_ops = &csiphy_v4l2_internal_ops; sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; snprintf(sd->name, ARRAY_SIZE(sd->name), "%s%d", @@ -816,9 +975,22 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy, ret = v4l2_device_register_subdev(v4l2_dev, sd); if (ret < 0) { dev_err(dev, "Failed to register subdev: %d\n", ret); - media_entity_cleanup(&sd->entity); + goto media_entity_err; } + if (!IS_ERR_OR_NULL(csiphy->phy)) { + ret = phy_init(csiphy->phy); + if (ret) { + dev_err(dev, "%s init fail %d\n", csiphy->name, ret); + v4l2_device_unregister_subdev(sd); + goto media_entity_err; + } + } + + return 0; + +media_entity_err: + media_entity_cleanup(&sd->entity); return ret; } @@ -828,6 +1000,8 @@ int msm_csiphy_register_entity(struct csiphy_device *csiphy, */ void msm_csiphy_unregister_entity(struct csiphy_device *csiphy) { + if (!IS_ERR_OR_NULL(csiphy->phy)) + phy_exit(csiphy->phy); v4l2_device_unregister_subdev(&csiphy->subdev); media_entity_cleanup(&csiphy->subdev.entity); } diff --git a/drivers/media/platform/qcom/camss/camss-csiphy.h b/drivers/media/platform/qcom/camss/camss-csiphy.h index 9d9657b82f748..bf09b70221a5a 100644 --- a/drivers/media/platform/qcom/camss/camss-csiphy.h +++ b/drivers/media/platform/qcom/camss/camss-csiphy.h @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #define MSM_CSIPHY_PAD_SINK 0 #define MSM_CSIPHY_PAD_SRC 1 #define MSM_CSIPHY_PADS_NUM 2 +#define MSM_CSIPHY_MAX_DATA_LANE 4 #define CSIPHY_GRP_ID 1 @@ -97,6 +99,7 @@ struct csiphy_device_regs { struct csiphy_device { struct camss *camss; + struct phy *phy; u8 id; struct v4l2_subdev subdev; struct media_pad pads[MSM_CSIPHY_PADS_NUM]; @@ -104,6 +107,7 @@ struct csiphy_device { void __iomem *base_clk_mux; u32 irq; char irq_name[30]; + char name[16]; struct camss_clock *clock; bool *rate_set; int nclocks; @@ -118,9 +122,12 @@ struct csiphy_device { struct camss_subdev_resources; -int msm_csiphy_subdev_init(struct camss *camss, - struct csiphy_device *csiphy, - const struct camss_subdev_resources *res, u8 id); +int msm_csiphy_subdev_init_legacy(struct camss *camss, + struct csiphy_device *csiphy, + const struct camss_subdev_resources *res, + u8 id); + +int msm_csiphy_subdev_init(struct camss *camss, u8 port); int msm_csiphy_register_entity(struct csiphy_device *csiphy, struct v4l2_device *v4l2_dev); diff --git a/drivers/media/platform/qcom/camss/camss-vfe-17x.c b/drivers/media/platform/qcom/camss/camss-vfe-17x.c index e5ee7e717b3bc..a741c57b996ea 100644 --- a/drivers/media/platform/qcom/camss/camss-vfe-17x.c +++ b/drivers/media/platform/qcom/camss/camss-vfe-17x.c @@ -115,7 +115,11 @@ #define STATUS0_COMP_OVERWRITE BIT(12) #define STATUS0_OVERFLOW BIT(13) #define STATUS0_VIOLATION BIT(14) -/* WM_CLIENT_BUF_DONE defined for buffers 0:19 */ +#define STATUS0_BUS_ERROR_MASK (STATUS0_COMP_ERROR | \ + STATUS0_COMP_OVERWRITE | \ + STATUS0_OVERFLOW | \ + STATUS0_VIOLATION) +/* WM_CLIENT_BUF_DONE defined for buffers 0:23 on Full VFE175, 0:3 on VFE Lite */ #define STATUS1_WM_CLIENT_BUF_DONE(n) BIT(n) #define STATUS1_EARLY_DONE BIT(24) #define STATUS2_DUAL_COMP0_BUF_DONE BIT(0) @@ -137,9 +141,12 @@ #define VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER (0x2080) #define VFE_BUS_WM_ADDR_SYNC_NO_SYNC (0x2084) -#define BUS_VER2_MAX_CLIENTS (24) +#define BUS_VER2_MAX_CLIENTS (20) +#define BUS_VER2_LITE_MAX_CLIENTS (4) #define WM_ADDR_NO_SYNC_DEFAULT_VAL \ ((1 << BUS_VER2_MAX_CLIENTS) - 1) +#define WM_ADDR_NO_SYNC_LITE_VAL \ + ((1 << BUS_VER2_LITE_MAX_CLIENTS) - 1) #define VFE_BUS_WM_CGC_OVERRIDE (0x200c) #define WM_CGC_OVERRIDE_ALL (0xFFFFF) @@ -183,18 +190,31 @@ static inline void vfe_reg_set(struct vfe_device *vfe, u32 reg, u32 set_bits) static void vfe_global_reset(struct vfe_device *vfe) { - u32 reset_bits = GLOBAL_RESET_CMD_CORE | - GLOBAL_RESET_CMD_CAMIF | - GLOBAL_RESET_CMD_BUS | - GLOBAL_RESET_CMD_BUS_BDG | - GLOBAL_RESET_CMD_REGISTER | - GLOBAL_RESET_CMD_TESTGEN | - GLOBAL_RESET_CMD_DSP | - GLOBAL_RESET_CMD_IDLE_CGC | - GLOBAL_RESET_CMD_RDI0 | - GLOBAL_RESET_CMD_RDI1 | - GLOBAL_RESET_CMD_RDI2 | - GLOBAL_RESET_CMD_RDI3; + u32 reset_bits; + + if (vfe_is_lite(vfe)) { + reset_bits = GLOBAL_RESET_CMD_CORE | + GLOBAL_RESET_CMD_BUS | + GLOBAL_RESET_CMD_REGISTER | + GLOBAL_RESET_CMD_IDLE_CGC | + GLOBAL_RESET_CMD_RDI0 | + GLOBAL_RESET_CMD_RDI1 | + GLOBAL_RESET_CMD_RDI2 | + GLOBAL_RESET_CMD_RDI3; + } else { + reset_bits = GLOBAL_RESET_CMD_CORE | + GLOBAL_RESET_CMD_CAMIF | + GLOBAL_RESET_CMD_BUS | + GLOBAL_RESET_CMD_BUS_BDG | + GLOBAL_RESET_CMD_REGISTER | + GLOBAL_RESET_CMD_TESTGEN | + GLOBAL_RESET_CMD_DSP | + GLOBAL_RESET_CMD_IDLE_CGC | + GLOBAL_RESET_CMD_RDI0 | + GLOBAL_RESET_CMD_RDI1 | + GLOBAL_RESET_CMD_RDI2 | + GLOBAL_RESET_CMD_RDI3; + } writel_relaxed(BIT(31), vfe->base + VFE_IRQ_MASK_0); @@ -206,12 +226,19 @@ static void vfe_global_reset(struct vfe_device *vfe) static void vfe_wm_start(struct vfe_device *vfe, u8 wm, struct vfe_line *line) { + struct v4l2_pix_format_mplane *pix = + &line->video_out.active_fmt.fmt.pix_mp; + u32 stride = pix->plane_fmt[0].bytesperline ? + pix->plane_fmt[0].bytesperline : pix->width; + u32 height = pix->height; u32 val; - /*Set Debug Registers*/ - val = DEBUG_STATUS_CFG_STATUS0(1) | - DEBUG_STATUS_CFG_STATUS0(7); - writel_relaxed(val, vfe->base + VFE_BUS_WM_DEBUG_STATUS_CFG); + /* Set Debug Registers (Full VFE only; VFE Lite does not have 0x226c) */ + if (!vfe_is_lite(vfe)) { + val = DEBUG_STATUS_CFG_STATUS0(1) | + DEBUG_STATUS_CFG_STATUS0(7); + writel_relaxed(val, vfe->base + VFE_BUS_WM_DEBUG_STATUS_CFG); + } /* BUS_WM_INPUT_IF_ADDR_SYNC_FRAME_HEADER */ writel_relaxed(0, vfe->base + VFE_BUS_WM_ADDR_SYNC_FRAME_HEADER); @@ -223,34 +250,60 @@ static void vfe_wm_start(struct vfe_device *vfe, u8 wm, struct vfe_line *line) writel_relaxed(0x0, vfe->base + VFE_BUS_WM_TEST_BUS_CTRL); /* if addr_no_sync has default value then config the addr no sync reg */ - val = WM_ADDR_NO_SYNC_DEFAULT_VAL; + val = vfe_is_lite(vfe) ? WM_ADDR_NO_SYNC_LITE_VAL : + WM_ADDR_NO_SYNC_DEFAULT_VAL; writel_relaxed(val, vfe->base + VFE_BUS_WM_ADDR_SYNC_NO_SYNC); writel_relaxed(0xf, vfe->base + VFE_BUS_WM_BURST_LIMIT(wm)); - - val = WM_BUFFER_DEFAULT_WIDTH; - writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_WIDTH_CFG(wm)); - - val = 0; - writel_relaxed(val, vfe->base + VFE_BUS_WM_BUFFER_HEIGHT_CFG(wm)); - - val = 0; - writel_relaxed(val, vfe->base + VFE_BUS_WM_PACKER_CFG(wm)); // XXX 1 for PLAIN8? - - /* Configure stride for RDIs */ - val = WM_STRIDE_DEFAULT_STRIDE; - writel_relaxed(val, vfe->base + VFE_BUS_WM_STRIDE(wm)); - - /* Enable WM */ - val = 1 << WM_CFG_EN | - MODE_MIPI_RAW << WM_CFG_MODE; - writel_relaxed(val, vfe->base + VFE_BUS_WM_CFG(wm)); + writel_relaxed(stride * height, vfe->base + VFE_BUS_WM_FRAME_INC(wm)); + + /* Qualcomm RDI bus defaults: width and stride = 0xFF01, height = 0, packer = 0 */ + writel_relaxed(WM_BUFFER_DEFAULT_WIDTH, + vfe->base + VFE_BUS_WM_BUFFER_WIDTH_CFG(wm)); + writel_relaxed(0, vfe->base + VFE_BUS_WM_BUFFER_HEIGHT_CFG(wm)); + writel_relaxed(0, vfe->base + VFE_BUS_WM_PACKER_CFG(wm)); + writel_relaxed(WM_STRIDE_DEFAULT_STRIDE, + vfe->base + VFE_BUS_WM_STRIDE(wm)); + + /* No dropped frames, one IRQ per frame */ + writel_relaxed(0, vfe->base + VFE_BUS_WM_FRAMEDROP_PERIOD(wm)); + writel_relaxed(1, vfe->base + VFE_BUS_WM_FRAMEDROP_PATTERN(wm)); + writel_relaxed(0, vfe->base + VFE_BUS_WM_IRQ_SUBSAMPLE_PERIOD(wm)); + writel_relaxed(1, vfe->base + VFE_BUS_WM_IRQ_SUBSAMPLE_PATTERN(wm)); + + /* + * Subscribe only to WM_CLIENT_BUF_DONE(wm) in Bus IRQ Mask 1 (keeping + * Bus IRQ Mask 0 for hardware error reporting) so vfe_buf_done() fires + * once per frame. Defer enabling WM_CFG until vfe_wm_update() writes + * the first buffer IOVA into the 2-deep IMAGE_ADDR FIFO. + */ + val = readl_relaxed(vfe->base + VFE_BUS_IRQ_MASK(0)); + val |= STATUS0_BUS_ERROR_MASK; + writel_relaxed(val, vfe->base + VFE_BUS_IRQ_MASK(0)); + + val = readl_relaxed(vfe->base + VFE_BUS_IRQ_MASK(1)); + val |= STATUS1_WM_CLIENT_BUF_DONE(wm); + writel_relaxed(val, vfe->base + VFE_BUS_IRQ_MASK(1)); + + /* Ensure WM configuration writes complete before buffer enqueue */ + wmb(); } static void vfe_wm_stop(struct vfe_device *vfe, u8 wm) { + u32 val; + + /* Unsubscribe WM IRQ in Bus IRQ Mask 1 */ + val = readl_relaxed(vfe->base + VFE_BUS_IRQ_MASK(1)); + val &= ~STATUS1_WM_CLIENT_BUF_DONE(wm); + writel_relaxed(val, vfe->base + VFE_BUS_IRQ_MASK(1)); + /* Ensure IRQ mask update completes before disabling WM */ + wmb(); + /* Disable WM */ writel_relaxed(0, vfe->base + VFE_BUS_WM_CFG(wm)); + /* Ensure WM disable write reaches hardware */ + wmb(); } static void vfe_wm_update(struct vfe_device *vfe, u8 wm, u32 addr, @@ -259,9 +312,20 @@ static void vfe_wm_update(struct vfe_device *vfe, u8 wm, u32 addr, struct v4l2_pix_format_mplane *pix = &line->video_out.active_fmt.fmt.pix_mp; u32 stride = pix->plane_fmt[0].bytesperline; + u32 dummy_iova = (u32)vfe->camss->dummy_buf_iova; + + if (!addr) + addr = dummy_iova; writel_relaxed(addr, vfe->base + VFE_BUS_WM_IMAGE_ADDR(wm)); - writel_relaxed(stride * pix->height, vfe->base + VFE_BUS_WM_FRAME_INC(wm)); + writel_relaxed(addr == dummy_iova ? 0 : (stride * pix->height), + vfe->base + VFE_BUS_WM_FRAME_INC(wm)); + /* Ensure IMAGE_ADDR and FRAME_INC land before enabling WM_CFG */ + wmb(); + writel_relaxed((1 << WM_CFG_EN) | (MODE_MIPI_RAW << WM_CFG_MODE), + vfe->base + VFE_BUS_WM_CFG(wm)); + /* Ensure WM enable write reaches hardware before REG_UPDATE */ + wmb(); } static void vfe_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id) @@ -285,12 +349,33 @@ static inline void vfe_reg_update_clear(struct vfe_device *vfe, static void vfe_enable_irq_common(struct vfe_device *vfe) { - vfe_reg_set(vfe, VFE_IRQ_MASK_0, ~0u); - vfe_reg_set(vfe, VFE_IRQ_MASK_1, ~0u); + /* + * Bit 31: STATUS_0_RESET_ACK + * Bits 27-30: RDI0-3 SOF + * Bits 4, 7, 9: BUS_TOP_IRQ (routes VFE_BUS_IRQ_STATUS[0..2] to top IRQ) + * Bits 5-8: RDI0-3 REG_UPDATE + */ + writel_relaxed(STATUS_0_RESET_ACK | 0x780001e0 | BIT(4) | BIT(7) | BIT(9), + vfe->base + VFE_IRQ_MASK_0); + writel_relaxed(MASK_1_CAMIF_ERROR | MASK_1_VIOLATION | + MASK_1_BUS_BDG_HALT_ACK, + vfe->base + VFE_IRQ_MASK_1); + + /* + * VFE_CORE_CFG (0x050): keep 0 so local SOF latches registers + * (BIT(4) selects extern_reg_update slave mode). + */ + writel_relaxed(0, vfe->base + VFE_CORE_CFG); + + /* Override clock gating for bus bridge (VFE TOP 0x3C) */ + writel_relaxed(0xffffffff, vfe->base + 0x03c); + /* Ensure core/bridge configuration lands before bus IRQ masks */ + wmb(); - writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(0)); - writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(1)); - writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_MASK(2)); + /* Enable bus error IRQs only */ + writel_relaxed(STATUS0_BUS_ERROR_MASK, vfe->base + VFE_BUS_IRQ_MASK(0)); + writel_relaxed(0, vfe->base + VFE_BUS_IRQ_MASK(1)); + writel_relaxed(STATUS2_DUAL_COMP_ERROR, vfe->base + VFE_BUS_IRQ_MASK(2)); } static void vfe_isr_halt_ack(struct vfe_device *vfe) @@ -337,7 +422,7 @@ static irqreturn_t vfe_isr(int irq, void *dev) writel_relaxed(status0, vfe->base + VFE_IRQ_CLEAR_0); writel_relaxed(status1, vfe->base + VFE_IRQ_CLEAR_1); - for (i = VFE_LINE_RDI0; i < vfe->res->line_num; i++) { + for (i = 0; i < 3; i++) { vfe_bus_status[i] = readl_relaxed(vfe->base + VFE_BUS_IRQ_STATUS(i)); writel_relaxed(vfe_bus_status[i], vfe->base + VFE_BUS_IRQ_CLEAR(i)); } @@ -348,15 +433,24 @@ static irqreturn_t vfe_isr(int irq, void *dev) writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD); writel_relaxed(1, vfe->base + VFE_BUS_IRQ_CLEAR_GLOBAL); + if (vfe_bus_status[0] & STATUS0_BUS_ERROR_MASK) + dev_err_ratelimited(vfe->camss->dev, + "vfe%d bus error: bus0=0x%08x st0=0x%08x st1=0x%08x\n", + vfe->id, vfe_bus_status[0], status0, status1); + if (status0 & STATUS_0_RESET_ACK) vfe->isr_ops.reset_ack(vfe); + if (status1 & STATUS_1_VIOLATION) + vfe_violation_read(vfe); + for (i = VFE_LINE_RDI0; i < vfe->res->line_num; i++) if (status0 & STATUS_0_RDI_REG_UPDATE(i)) vfe->isr_ops.reg_update(vfe, i); for (i = VFE_LINE_RDI0; i < vfe->res->line_num; i++) - if (status0 & STATUS_1_RDI_SOF(i)) + if ((status1 & STATUS_1_RDI_SOF(i)) || + (status0 & BIT(27 + i))) vfe->isr_ops.sof(vfe, i); for (i = 0; i < MSM_VFE_COMPOSITE_IRQ_NUM; i++) @@ -364,62 +458,38 @@ static irqreturn_t vfe_isr(int irq, void *dev) vfe->isr_ops.comp_done(vfe, i); for (wm = 0; wm < MSM_VFE_IMAGE_MASTERS_NUM; wm++) - if (status0 & BIT(9)) - if (vfe_bus_status[1] & STATUS1_WM_CLIENT_BUF_DONE(wm)) - vfe->isr_ops.wm_done(vfe, wm); + if (vfe_bus_status[1] & STATUS1_WM_CLIENT_BUF_DONE(wm)) + vfe->isr_ops.wm_done(vfe, wm); return IRQ_HANDLED; } /* - * vfe_halt - Trigger halt on VFE module and wait to complete + * vfe_halt - Mask and clear pending interrupts on VFE module * @vfe: VFE device * * Return 0 on success or a negative error code otherwise */ static int vfe_halt(struct vfe_device *vfe) { - /* rely on vfe_disable_output() to stop the VFE */ - return 0; -} - -static int vfe_get_output(struct vfe_line *line) -{ - struct vfe_device *vfe = to_vfe(line); - struct vfe_output *output; - unsigned long flags; - int wm_idx; - - spin_lock_irqsave(&vfe->output_lock, flags); - - output = &line->output; - if (output->state > VFE_OUTPUT_RESERVED) { - dev_err(vfe->camss->dev, "Output is running\n"); - goto error; - } - - output->wm_num = 1; - - wm_idx = vfe_reserve_wm(vfe, line->id); - if (wm_idx < 0) { - dev_err(vfe->camss->dev, "Can not reserve wm\n"); - goto error_get_wm; - } - output->wm_idx[0] = wm_idx; - - output->drop_update_idx = 0; + writel_relaxed(0, vfe->base + VFE_IRQ_MASK_0); + writel_relaxed(0, vfe->base + VFE_IRQ_MASK_1); + writel_relaxed(~0u, vfe->base + VFE_IRQ_CLEAR_0); + writel_relaxed(~0u, vfe->base + VFE_IRQ_CLEAR_1); + writel_relaxed(CMD_GLOBAL_CLEAR, vfe->base + VFE_IRQ_CMD); - spin_unlock_irqrestore(&vfe->output_lock, flags); + writel_relaxed(0, vfe->base + VFE_BUS_IRQ_MASK(0)); + writel_relaxed(0, vfe->base + VFE_BUS_IRQ_MASK(1)); + writel_relaxed(0, vfe->base + VFE_BUS_IRQ_MASK(2)); + writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_CLEAR(0)); + writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_CLEAR(1)); + writel_relaxed(~0u, vfe->base + VFE_BUS_IRQ_CLEAR(2)); + writel_relaxed(1, vfe->base + VFE_BUS_IRQ_CLEAR_GLOBAL); + vfe->reg_update = 0; + /* Ensure IRQ mask/clear writes reach hardware before halt completes */ + wmb(); return 0; - -error_get_wm: - vfe_release_wm(vfe, output->wm_idx[0]); - output->state = VFE_OUTPUT_OFF; -error: - spin_unlock_irqrestore(&vfe->output_lock, flags); - - return -EINVAL; } /* @@ -442,7 +512,7 @@ static int vfe_enable(struct vfe_line *line) mutex_unlock(&vfe->stream_lock); - ret = vfe_get_output(line); + ret = vfe_get_output_v2(line); if (ret < 0) goto error_get_output; @@ -507,52 +577,7 @@ static void vfe_isr_reg_update(struct vfe_device *vfe, enum vfe_line_id line_id) */ static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm) { - struct vfe_line *line = &vfe->line[vfe->wm_output_map[wm]]; - struct camss_buffer *ready_buf; - struct vfe_output *output; - unsigned long flags; - u32 index; - u64 ts = ktime_get_ns(); - - spin_lock_irqsave(&vfe->output_lock, flags); - - if (vfe->wm_output_map[wm] == VFE_LINE_NONE) { - dev_err_ratelimited(vfe->camss->dev, - "Received wm done for unmapped index\n"); - goto out_unlock; - } - output = &vfe->line[vfe->wm_output_map[wm]].output; - - ready_buf = output->buf[0]; - if (!ready_buf) { - dev_err_ratelimited(vfe->camss->dev, - "Missing ready buf %d!\n", output->state); - goto out_unlock; - } - - ready_buf->vb.vb2_buf.timestamp = ts; - ready_buf->vb.sequence = output->sequence++; - - index = 0; - output->buf[0] = output->buf[1]; - if (output->buf[0]) - index = 1; - - output->buf[index] = vfe_buf_get_pending(output); - - if (output->buf[index]) - vfe_wm_update(vfe, output->wm_idx[0], output->buf[index]->addr[0], line); - else - output->gen2.active_num--; - - spin_unlock_irqrestore(&vfe->output_lock, flags); - - vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); - - return; - -out_unlock: - spin_unlock_irqrestore(&vfe->output_lock, flags); + vfe_buf_done(vfe, wm); } static const struct vfe_isr_ops vfe_isr_ops_170 = { diff --git a/drivers/media/platform/qcom/camss/camss-vfe.c b/drivers/media/platform/qcom/camss/camss-vfe.c index 319d191589884..4bf32fa9d55ad 100644 --- a/drivers/media/platform/qcom/camss/camss-vfe.c +++ b/drivers/media/platform/qcom/camss/camss-vfe.c @@ -9,6 +9,8 @@ */ #include #include +#include +#include #include #include #include @@ -346,6 +348,7 @@ static u32 vfe_src_pad_code(struct vfe_line *line, u32 sink_code, case CAMSS_6350: case CAMSS_7280: case CAMSS_8x96: + case CAMSS_8150: case CAMSS_8250: case CAMSS_8280XP: case CAMSS_8300: @@ -447,9 +450,11 @@ u32 vfe_hw_version(struct vfe_device *vfe) * @vfe: VFE Device * @wm: Write master id */ +#define VFE_DUMMY_BUF ((struct camss_buffer *)1UL) + void vfe_buf_done(struct vfe_device *vfe, int wm) { - struct vfe_line *line = &vfe->line[vfe->wm_output_map[wm]]; + struct vfe_line *line; const struct vfe_hw_ops *ops = vfe->res->hw_ops; struct camss_buffer *ready_buf; struct vfe_output *output; @@ -457,44 +462,64 @@ void vfe_buf_done(struct vfe_device *vfe, int wm) u32 index; u64 ts = ktime_get_ns(); + if (wm < 0 || wm >= MSM_VFE_IMAGE_MASTERS_NUM) + return; + spin_lock_irqsave(&vfe->output_lock, flags); - if (vfe->wm_output_map[wm] == VFE_LINE_NONE) { + if (vfe->wm_output_map[wm] == VFE_LINE_NONE || + vfe->wm_output_map[wm] >= vfe->res->line_num) { dev_err_ratelimited(vfe->camss->dev, "Received wm done for unmapped index\n"); goto out_unlock; } - output = &vfe->line[vfe->wm_output_map[wm]].output; + line = &vfe->line[vfe->wm_output_map[wm]]; + output = &line->output; ready_buf = output->buf[0]; - if (!ready_buf) { - dev_err_ratelimited(vfe->camss->dev, - "Missing ready buf %d!\n", output->state); + if (!ready_buf) goto out_unlock; - } - ready_buf->vb.vb2_buf.timestamp = ts; - ready_buf->vb.sequence = output->sequence++; + if (ready_buf != VFE_DUMMY_BUF) { + ready_buf->vb.vb2_buf.timestamp = ts; + ready_buf->vb.sequence = output->sequence++; + } + /* + * Titan 17x VFE_BUS_WM_IMAGE_ADDR is a 2-deep hardware FIFO: + * buf[0] just completed; buf[1] is now active in hardware for the next + * frame, so shift buf[1] -> buf[0] and push the next pending buffer + * into FIFO slot 1. Fall back to VFE_DUMMY_BUF only on total underrun. + */ index = 0; output->buf[0] = output->buf[1]; + output->buf[1] = NULL; if (output->buf[0]) index = 1; output->buf[index] = vfe_buf_get_pending(output); if (output->buf[index]) { + output->gen2.active_num = index + 1; ops->vfe_wm_update(vfe, output->wm_idx[0], output->buf[index]->addr[0], line); ops->reg_update(vfe, line->id); + } else if (!output->buf[0] && vfe->camss->dummy_buf_iova) { + output->buf[0] = VFE_DUMMY_BUF; + output->gen2.active_num = 1; + ops->vfe_wm_update(vfe, output->wm_idx[0], + vfe->camss->dummy_buf_iova, + line); + ops->reg_update(vfe, line->id); } else { - output->gen2.active_num--; + output->gen2.active_num = output->buf[0] ? 1 : 0; } spin_unlock_irqrestore(&vfe->output_lock, flags); - vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); + if (ready_buf != VFE_DUMMY_BUF) + vb2_buffer_done(&ready_buf->vb.vb2_buf, VB2_BUF_STATE_DONE); return; @@ -545,6 +570,8 @@ int vfe_enable_output_v2(struct vfe_line *line) ops->vfe_wm_start(vfe, output->wm_idx[0], line); + output->buf[0] = NULL; + output->buf[1] = NULL; for (i = 0; i < CAMSS_INIT_BUF_COUNT; i++) { output->buf[i] = vfe_buf_get_pending(output); if (!output->buf[i]) @@ -555,6 +582,14 @@ int vfe_enable_output_v2(struct vfe_line *line) ops->reg_update(vfe, line->id); } + if (output->gen2.active_num == 0 && vfe->camss->dummy_buf_iova) { + output->buf[0] = VFE_DUMMY_BUF; + output->gen2.active_num = 1; + ops->vfe_wm_update(vfe, output->wm_idx[0], + vfe->camss->dummy_buf_iova, line); + ops->reg_update(vfe, line->id); + } + spin_unlock_irqrestore(&vfe->output_lock, flags); return 0; @@ -773,23 +808,6 @@ void vfe_buf_add_pending(struct vfe_output *output, list_add_tail(&buffer->queue, &output->pending_bufs); } -/* - * vfe_buf_flush_pending - Flush all pending buffers. - * @output: VFE output - * @state: vb2 buffer state - */ -static void vfe_buf_flush_pending(struct vfe_output *output, - enum vb2_buffer_state state) -{ - struct camss_buffer *buf; - struct camss_buffer *t; - - list_for_each_entry_safe(buf, t, &output->pending_bufs, queue) { - vb2_buffer_done(&buf->vb.vb2_buf, state); - list_del(&buf->queue); - } -} - int vfe_put_output(struct vfe_line *line) { struct vfe_device *vfe = to_vfe(line); @@ -821,6 +839,11 @@ static int vfe_disable_output(struct vfe_line *line) output->gen2.active_num = 0; spin_unlock_irqrestore(&vfe->output_lock, flags); + if (vfe->camss->res->version == CAMSS_8150) { + vfe->res->hw_ops->reg_update(vfe, line->id); + return 0; + } + return vfe_reset(vfe); } @@ -839,14 +862,32 @@ int vfe_disable(struct vfe_line *line) if (ret) goto error; + if (vfe->camss->res->version == CAMSS_8150) { + /* + * Wait for the next frame boundary while CSID/CSIPHY/sensor + * are still streaming so VFE latches WM_CFG = 0 and completes + * any in-flight AXI burst cleanly without a post-stream + * GLOBAL_RESET_CMD_BUS (which deadlocks the SC8180X NoC). + */ + msleep(50); + } + vfe_put_output(line); mutex_lock(&vfe->stream_lock); vfe->stream_count--; + if (vfe->camss->res->version == CAMSS_8150 && !vfe->stream_count) { + if (vfe->res->hw_ops->vfe_halt) + vfe->res->hw_ops->vfe_halt(vfe); + vfe_reset_output_maps(vfe); + } mutex_unlock(&vfe->stream_lock); + if (vfe->camss->res->version == CAMSS_8150) + synchronize_irq(vfe->irq); + error: return ret; } @@ -861,7 +902,7 @@ void vfe_isr_comp_done(struct vfe_device *vfe, u8 comp) unsigned int i; for (i = 0; i < ARRAY_SIZE(vfe->wm_output_map); i++) - if (vfe->wm_output_map[i] == VFE_LINE_PIX) { + if (vfe->wm_output_map[i] != VFE_LINE_NONE) { vfe->isr_ops.wm_done(vfe, i); break; } @@ -918,6 +959,9 @@ static int vfe_match_clock_names(struct vfe_device *vfe, return (!strcmp(clock->name, vfe_name) || !strcmp(clock->name, vfe_lite_name) || !strcmp(clock->name, "vfe_lite") || + (vfe->res->is_lite && + !strncmp(clock->name, "vfe_lite", 8) && + isdigit((unsigned char)clock->name[8])) || !strcmp(clock->name, "camnoc_axi") || !strcmp(clock->name, "camnoc_rt_axi")); } @@ -997,9 +1041,14 @@ static int vfe_set_clock_rates(struct vfe_device *vfe) } /* if sensor pixel clock is not available */ - /* set highest possible VFE clock rate */ - if (min_rate == 0) + if (min_rate == 0 && + vfe->camss->res->version == CAMSS_8150) { + for (j = 0; j < clock->nfreqs - 1; j++) + if (clock->freq[j] >= 400000000) + break; + } else if (min_rate == 0) { j = clock->nfreqs - 1; + } rate = clk_round_rate(clock->clk, clock->freq[j]); if (rate < 0) { @@ -1089,32 +1138,48 @@ int vfe_get(struct vfe_device *vfe) mutex_lock(&vfe->power_lock); if (vfe->power_count == 0) { - ret = vfe->res->hw_ops->pm_domain_on(vfe); - if (ret < 0) - goto error_pm_domain; + if (!vfe->powered_on) { + ret = vfe->res->hw_ops->pm_domain_on(vfe); + if (ret < 0) + goto error_pm_domain; - ret = pm_runtime_resume_and_get(vfe->camss->dev); - if (ret < 0) - goto error_domain_off; + ret = pm_runtime_resume_and_get(vfe->camss->dev); + if (ret < 0) + goto error_domain_off; - ret = vfe_set_clock_rates(vfe); - if (ret < 0) - goto error_pm_runtime_get; + ret = vfe_set_clock_rates(vfe); + if (ret < 0) + goto error_pm_runtime_get; - ret = camss_enable_clocks(vfe->nclocks, vfe->clock, - vfe->camss->dev); - if (ret < 0) - goto error_pm_runtime_get; + ret = camss_enable_clocks(vfe->nclocks, vfe->clock, + vfe->camss->dev); + if (ret < 0) + goto error_pm_runtime_get; - ret = vfe_reset(vfe); - if (ret < 0) - goto error_reset; + enable_irq(vfe->irq); - vfe_reset_output_maps(vfe); + ret = vfe_reset(vfe); + if (ret < 0) + goto error_reset; - vfe_init_outputs(vfe); + vfe_reset_output_maps(vfe); + vfe_init_outputs(vfe); + vfe->res->hw_ops->hw_version(vfe); - vfe->res->hw_ops->hw_version(vfe); + if (vfe->camss->res->version == CAMSS_8150) + vfe->powered_on = true; + } else { + ret = vfe_set_clock_rates(vfe); + if (ret < 0) + goto error_pm_domain; + + ret = vfe_reset(vfe); + if (ret < 0) + goto error_pm_domain; + + vfe_reset_output_maps(vfe); + vfe_init_outputs(vfe); + } } else { ret = vfe_check_clock_rates(vfe); if (ret < 0) @@ -1127,6 +1192,7 @@ int vfe_get(struct vfe_device *vfe) return 0; error_reset: + disable_irq(vfe->irq); camss_disable_clocks(vfe->nclocks, vfe->clock); error_pm_runtime_get: @@ -1156,9 +1222,12 @@ void vfe_put(struct vfe_device *vfe) vfe->was_streaming = 0; vfe->res->hw_ops->vfe_halt(vfe); } - camss_disable_clocks(vfe->nclocks, vfe->clock); - pm_runtime_put_sync(vfe->camss->dev); - vfe->res->hw_ops->pm_domain_off(vfe); + if (!vfe->powered_on) { + disable_irq(vfe->irq); + camss_disable_clocks(vfe->nclocks, vfe->clock); + pm_runtime_put_sync(vfe->camss->dev); + vfe->res->hw_ops->pm_domain_off(vfe); + } } vfe->power_count--; @@ -1183,27 +1252,51 @@ int vfe_flush_buffers(struct camss_video *vid, struct vfe_line *line = container_of(vid, struct vfe_line, video_out); struct vfe_device *vfe = to_vfe(line); struct vfe_output *output; + struct camss_buffer *buf, *t; + struct camss_buffer *b0 = NULL, *b1 = NULL, *blast = NULL; + struct list_head local_pending; unsigned long flags; output = &line->output; + INIT_LIST_HEAD(&local_pending); spin_lock_irqsave(&vfe->output_lock, flags); - vfe_buf_flush_pending(output, state); + list_splice_init(&output->pending_bufs, &local_pending); - if (output->buf[0]) - vb2_buffer_done(&output->buf[0]->vb.vb2_buf, state); + if (output->buf[0]) { + b0 = output->buf[0]; + output->buf[0] = NULL; + } - if (output->buf[1]) - vb2_buffer_done(&output->buf[1]->vb.vb2_buf, state); + if (output->buf[1]) { + b1 = output->buf[1]; + output->buf[1] = NULL; + } if (output->last_buffer) { - vb2_buffer_done(&output->last_buffer->vb.vb2_buf, state); + blast = output->last_buffer; output->last_buffer = NULL; } + output->gen2.active_num = 0; + spin_unlock_irqrestore(&vfe->output_lock, flags); + list_for_each_entry_safe(buf, t, &local_pending, queue) { + vb2_buffer_done(&buf->vb.vb2_buf, state); + list_del(&buf->queue); + } + + if (b0 && b0 != VFE_DUMMY_BUF) + vb2_buffer_done(&b0->vb.vb2_buf, state); + + if (b1 && b1 != VFE_DUMMY_BUF) + vb2_buffer_done(&b1->vb.vb2_buf, state); + + if (blast && blast != VFE_DUMMY_BUF) + vb2_buffer_done(&blast->vb.vb2_buf, state); + return 0; } @@ -1851,7 +1944,8 @@ int msm_vfe_subdev_init(struct camss *camss, struct vfe_device *vfe, snprintf(vfe->irq_name, sizeof(vfe->irq_name), "%s_%s%d", dev_name(dev), MSM_VFE_NAME, id); ret = devm_request_irq(dev, vfe->irq, vfe->res->hw_ops->isr, - IRQF_TRIGGER_RISING, vfe->irq_name, vfe); + IRQF_TRIGGER_RISING | IRQF_NO_AUTOEN, + vfe->irq_name, vfe); if (ret < 0) { dev_err(dev, "request_irq failed: %d\n", ret); return ret; @@ -2006,6 +2100,7 @@ static int vfe_bpl_align_rdi(struct vfe_device *vfe) case CAMSS_6150: case CAMSS_6350: case CAMSS_7280: + case CAMSS_8150: case CAMSS_8250: case CAMSS_8280XP: case CAMSS_8300: @@ -2171,9 +2266,6 @@ void msm_vfe_unregister_entities(struct vfe_device *vfe) { int i; - mutex_destroy(&vfe->power_lock); - mutex_destroy(&vfe->stream_lock); - for (i = 0; i < vfe->res->line_num; i++) { struct v4l2_subdev *sd = &vfe->line[i].subdev; struct camss_video *video_out = &vfe->line[i].video_out; @@ -2182,6 +2274,15 @@ void msm_vfe_unregister_entities(struct vfe_device *vfe) v4l2_device_unregister_subdev(sd); media_entity_cleanup(&sd->entity); } + + if (vfe->power_count > 0 || vfe->powered_on) { + disable_irq(vfe->irq); + vfe->power_count = 0; + vfe->powered_on = false; + } + + mutex_destroy(&vfe->power_lock); + mutex_destroy(&vfe->stream_lock); } bool vfe_is_lite(struct vfe_device *vfe) diff --git a/drivers/media/platform/qcom/camss/camss-vfe.h b/drivers/media/platform/qcom/camss/camss-vfe.h index ae9dad353a378..59b6caf392a60 100644 --- a/drivers/media/platform/qcom/camss/camss-vfe.h +++ b/drivers/media/platform/qcom/camss/camss-vfe.h @@ -163,6 +163,7 @@ struct vfe_device { struct vfe_line line[VFE_LINE_NUM_MAX]; u32 reg_update; u8 was_streaming; + bool powered_on; const struct vfe_subdev_resources *res; const struct vfe_hw_ops_gen1 *ops_gen1; struct vfe_isr_ops isr_ops; diff --git a/drivers/media/platform/qcom/camss/camss-video.c b/drivers/media/platform/qcom/camss/camss-video.c index 0852eb6f13154..4fb4b978803dd 100644 --- a/drivers/media/platform/qcom/camss/camss-video.c +++ b/drivers/media/platform/qcom/camss/camss-video.c @@ -72,6 +72,10 @@ static struct v4l2_subdev *video_remote_subdev(struct camss_video *video, { struct media_pad *remote; + if (!video->vdev.entity.graph_obj.mdev || + !video->vdev.entity.links.next) + return NULL; + remote = media_pad_remote_pad_first(&video->pad); if (!remote || !is_media_entity_v4l2_subdev(remote->entity)) @@ -180,8 +184,11 @@ static int video_buf_prepare(struct vb2_buffer *vb) { struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue); + struct camss_buffer *buffer = container_of(vbuf, struct camss_buffer, + vb); const struct v4l2_pix_format_mplane *format = &video->active_fmt.fmt.pix_mp; + struct sg_table *sgt; unsigned int i; for (i = 0; i < format->num_planes; i++) { @@ -189,6 +196,10 @@ static int video_buf_prepare(struct vb2_buffer *vb) return -EINVAL; vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage); + + sgt = vb2_dma_sg_plane_desc(vb, i); + if (sgt && sgt->sgl) + buffer->addr[i] = sg_dma_address(sgt->sgl); } vbuf->field = V4L2_FIELD_NONE; @@ -256,7 +267,9 @@ static int video_start_streaming(struct vb2_queue *q, unsigned int count) struct media_entity *entity; struct media_pad *pad; struct v4l2_subdev *subdev; - int ret; + struct v4l2_subdev *started[8]; + int n_started = 0; + int i, ret; ret = video_device_pipeline_alloc_start(vdev); if (ret < 0) { @@ -283,11 +296,18 @@ static int video_start_streaming(struct vb2_queue *q, unsigned int count) ret = v4l2_subdev_call(subdev, video, s_stream, 1); if (ret < 0 && ret != -ENOIOCTLCMD) - goto error; + goto error_stop_subdevs; + + if (n_started < ARRAY_SIZE(started)) + started[n_started++] = subdev; } return 0; +error_stop_subdevs: + for (i = 0; i < n_started; i++) + v4l2_subdev_call(started[i], video, s_stream, 0); + error: video_device_pipeline_stop(vdev); @@ -303,11 +323,12 @@ static void video_stop_streaming(struct vb2_queue *q) struct video_device *vdev = &video->vdev; struct media_entity *entity; struct media_pad *pad; - struct v4l2_subdev *subdev; - int ret; + struct v4l2_subdev *subdevs[8]; + int n_subdevs = 0; + int i, ret; entity = &vdev->entity; - while (1) { + while (n_subdevs < ARRAY_SIZE(subdevs)) { pad = &entity->pads[0]; if (!(pad->flags & MEDIA_PAD_FL_SINK)) break; @@ -317,14 +338,21 @@ static void video_stop_streaming(struct vb2_queue *q) break; entity = pad->entity; - subdev = media_entity_to_v4l2_subdev(entity); - - ret = v4l2_subdev_call(subdev, video, s_stream, 0); + subdevs[n_subdevs++] = media_entity_to_v4l2_subdev(entity); + } - if (ret) { - dev_err(video->camss->dev, "Video pipeline stop failed: %d\n", ret); - return; - } + /* + * Stop subdevices in sink-to-source order (VFE -> CSID -> CSIPHY -> + * sensor) so VFE latches WM_CFG = 0 on the next frame boundary while + * upstream blocks are still streaming, without aborting teardown on + * non-fatal warnings. + */ + for (i = 0; i < n_subdevs; i++) { + ret = v4l2_subdev_call(subdevs[i], video, s_stream, 0); + if (ret && ret != -ENOIOCTLCMD) + dev_warn(video->camss->dev, + "Video pipeline stop subdev %s warning: %d\n", + subdevs[i]->name, ret); } video_device_pipeline_stop(vdev); @@ -466,6 +494,7 @@ static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f) struct v4l2_pix_format_mplane *pix_mp; const struct camss_format_info *fi; struct v4l2_plane_pix_format *p; + struct v4l2_format sd_fmt = { 0 }; u32 bytesperline[3] = { 0 }; u32 sizeimage[3] = { 0 }; u32 width, height; @@ -474,6 +503,15 @@ static int __video_try_fmt(struct camss_video *video, struct v4l2_format *f) pix_mp = &f->fmt.pix_mp; + if (!video->line_based && video->vdev.entity.graph_obj.mdev) { + sd_fmt.fmt.pix_mp.pixelformat = pix_mp->pixelformat; + if (video_get_subdev_format(video, &sd_fmt) == 0) { + pix_mp->pixelformat = sd_fmt.fmt.pix_mp.pixelformat; + pix_mp->width = sd_fmt.fmt.pix_mp.width; + pix_mp->height = sd_fmt.fmt.pix_mp.height; + } + } + if (video->line_based) for (i = 0; i < pix_mp->num_planes && i < 3; i++) { p = &pix_mp->plane_fmt[i]; diff --git a/drivers/media/platform/qcom/camss/camss.c b/drivers/media/platform/qcom/camss/camss.c index 2123f6388e3d7..3a1b4091876ad 100644 --- a/drivers/media/platform/qcom/camss/camss.c +++ b/drivers/media/platform/qcom/camss/camss.c @@ -1950,6 +1950,233 @@ static const struct resources_icc icc_res_sm6350[] = { }, }; +static const struct camss_subdev_resources csiphy_res_sm8150[] = { + /* CSIPHY0 - clocks/regs/supplies owned by PHY driver */ + { + .csiphy = { + .id = 0, + .hw_ops = &csiphy_ops_3ph_1_0, + .formats = &csiphy_formats_sdm845 + } + }, + /* CSIPHY1 - clocks/regs/supplies owned by PHY driver */ + { + .csiphy = { + .id = 1, + .hw_ops = &csiphy_ops_3ph_1_0, + .formats = &csiphy_formats_sdm845 + } + }, + /* CSIPHY2 - clocks/regs/supplies owned by PHY driver */ + { + .csiphy = { + .id = 2, + .hw_ops = &csiphy_ops_3ph_1_0, + .formats = &csiphy_formats_sdm845 + } + }, + /* CSIPHY3 - clocks/regs/supplies owned by PHY driver */ + { + .csiphy = { + .id = 3, + .hw_ops = &csiphy_ops_3ph_1_0, + .formats = &csiphy_formats_sdm845 + } + }, +}; + +static const struct camss_subdev_resources csid_res_sm8150[] = { + /* CSID0 */ + { + .regulators = {}, + .clock = { "vfe0_csid", "vfe0_cphy_rx", "vfe0", "vfe0_axi" }, + .clock_rate = { { 400000000, 480000000, 600000000 }, + { 400000000 }, + { 0 }, + { 0 } }, + .reg = { "csid0" }, + .interrupt = { "csid0" }, + .csid = { + .hw_ops = &csid_ops_gen2, + .parent_dev_ops = &vfe_parent_dev_ops, + .formats = &csid_formats_gen2 + } + }, + /* CSID1 */ + { + .regulators = {}, + .clock = { "vfe1_csid", "vfe1_cphy_rx", "vfe1", "vfe1_axi" }, + .clock_rate = { { 400000000, 480000000, 600000000 }, + { 400000000 }, + { 0 }, + { 0 } }, + .reg = { "csid1" }, + .interrupt = { "csid1" }, + .csid = { + .hw_ops = &csid_ops_gen2, + .parent_dev_ops = &vfe_parent_dev_ops, + .formats = &csid_formats_gen2 + } + }, + /* CSID_LITE0 */ + { + .regulators = {}, + .clock = { "vfe_lite0_csid", "vfe_lite0_cphy_rx", "vfe_lite0" }, + .clock_rate = { { 400000000, 480000000, 600000000 }, + { 400000000 }, + { 0 }, }, + .reg = { "csid_lite0" }, + .interrupt = { "csid_lite0" }, + .csid = { + .is_lite = true, + .hw_ops = &csid_ops_gen2, + .parent_dev_ops = &vfe_parent_dev_ops, + .formats = &csid_formats_gen2 + } + }, + /* CSID_LITE1 */ + { + .regulators = {}, + .clock = { "vfe_lite1_csid", "vfe_lite1_cphy_rx", "vfe_lite1" }, + .clock_rate = { { 400000000, 480000000, 600000000 }, + { 400000000 }, + { 0 }, }, + .reg = { "csid_lite1" }, + .interrupt = { "csid_lite1" }, + .csid = { + .is_lite = true, + .hw_ops = &csid_ops_gen2, + .parent_dev_ops = &vfe_parent_dev_ops, + .formats = &csid_formats_gen2 + } + } +}; + +/* + * cpas_ahb is left at the CAMCC default 80 MHz; it is not matched by + * vfe_match_clock_names(). The VFE/VFE-Lite rates are set from the + * remaining clock-rate tables. + */ +static const struct camss_subdev_resources vfe_res_sm8150[] = { + /* VFE0 */ + { + .regulators = {}, + .clock = { "camnoc_axi", "cpas_ahb", "vfe0", "vfe0_axi", + "gcc_axi_hf", "gcc_axi_sf", "camnoc_dcd_xo", + "vfe0_dsp" }, + .clock_rate = { { 320000000, 400000000, 480000000 }, + { 0 }, + { 400000000, 558000000, 637000000, 760000000 }, + { 0 }, + { 0 }, + { 0 }, + { 0 }, + { 0 }, }, + .reg = { "vfe0" }, + .interrupt = { "vfe0" }, + .vfe = { + .line_num = 4, + .is_lite = false, + .has_pd = true, + .pd_name = "ife0", + .hw_ops = &vfe_ops_170, + .formats_rdi = &vfe_formats_rdi_845, + .formats_pix = &vfe_formats_pix_845 + } + }, + /* VFE1 */ + { + .regulators = {}, + .clock = { "camnoc_axi", "cpas_ahb", "vfe1", "vfe1_axi", + "gcc_axi_hf", "gcc_axi_sf", "camnoc_dcd_xo", + "vfe1_dsp" }, + .clock_rate = { { 320000000, 400000000, 480000000 }, + { 0 }, + { 400000000, 558000000, 637000000, 760000000 }, + { 0 }, + { 0 }, + { 0 }, + { 0 }, + { 0 }, }, + .reg = { "vfe1" }, + .interrupt = { "vfe1" }, + .vfe = { + .line_num = 4, + .is_lite = false, + .has_pd = true, + .pd_name = "ife1", + .hw_ops = &vfe_ops_170, + .formats_rdi = &vfe_formats_rdi_845, + .formats_pix = &vfe_formats_pix_845 + } + }, + /* VFE_LITE_0 */ + { + .regulators = {}, + .clock = { "camnoc_axi", "cpas_ahb", "vfe_lite0", + "gcc_axi_hf", "gcc_axi_sf", "camnoc_dcd_xo" }, + .clock_rate = { { 320000000, 400000000, 480000000 }, + { 0 }, + { 320000000, 400000000, 480000000, 600000000 }, + { 0 }, + { 0 }, + { 0 }, }, + .reg = { "vfe_lite0" }, + .interrupt = { "vfe_lite0" }, + .vfe = { + .is_lite = true, + .line_num = 4, + .hw_ops = &vfe_ops_170, + .formats_rdi = &vfe_formats_rdi_845, + .formats_pix = &vfe_formats_pix_845 + } + }, + /* VFE_LITE_1 */ + { + .regulators = {}, + .clock = { "camnoc_axi", "cpas_ahb", "vfe_lite1", + "gcc_axi_hf", "gcc_axi_sf", "camnoc_dcd_xo" }, + .clock_rate = { { 320000000, 400000000, 480000000 }, + { 0 }, + { 320000000, 400000000, 480000000, 600000000 }, + { 0 }, + { 0 }, + { 0 }, }, + .reg = { "vfe_lite1" }, + .interrupt = { "vfe_lite1" }, + .vfe = { + .is_lite = true, + .line_num = 4, + .hw_ops = &vfe_ops_170, + .formats_rdi = &vfe_formats_rdi_845, + .formats_pix = &vfe_formats_pix_845 + } + } +}; + +static const struct resources_icc icc_res_sm8150[] = { + { + .name = "cam_ahb", + .icc_bw_tbl.avg = 38400, + .icc_bw_tbl.peak = 76800, + }, + { + .name = "cam_hf_0_mnoc", + .icc_bw_tbl.avg = 2097152, + .icc_bw_tbl.peak = 2097152, + }, + { + .name = "cam_hf_1_mnoc", + .icc_bw_tbl.avg = 2097152, + .icc_bw_tbl.peak = 2097152, + }, + { + .name = "cam_sf_0_mnoc", + .icc_bw_tbl.avg = 2097152, + .icc_bw_tbl.peak = 2097152, + }, +}; + static const struct camss_subdev_resources csiphy_res_8250[] = { /* CSIPHY0 */ { @@ -4742,7 +4969,8 @@ static const struct parent_dev_ops vfe_parent_dev_ops = { */ static int camss_parse_endpoint_node(struct device *dev, struct fwnode_handle *ep, - struct camss_async_subdev *csd) + struct camss_async_subdev *csd, + u8 lane_base) { struct csiphy_lanes_cfg *lncfg = &csd->interface.csi2.lane_cfg; struct v4l2_mbus_config_mipi_csi2 *mipi_csi2; @@ -4777,7 +5005,15 @@ static int camss_parse_endpoint_node(struct device *dev, return -ENOMEM; for (i = 0; i < lncfg->num_data; i++) { - lncfg->data[i].pos = mipi_csi2->data_lanes[i]; + u8 lane = mipi_csi2->data_lanes[i]; + + if (lane < lane_base || + lane - lane_base >= MSM_CSIPHY_MAX_DATA_LANE) { + dev_err(dev, "invalid data-lane %u\n", lane); + return -EINVAL; + } + + lncfg->data[i].pos = lane - lane_base; lncfg->data[i].pol = mipi_csi2->lane_polarities[i + 1]; } @@ -4786,7 +5022,7 @@ static int camss_parse_endpoint_node(struct device *dev, /* * camss_parse_ports - Parse ports node - * @dev: CAMSS device + * @camss: CAMSS device * * Return 0 on success or a negative error code on failure */ @@ -4794,19 +5030,51 @@ static int camss_parse_ports(struct camss *camss) { struct device *dev = camss->dev; struct fwnode_handle *fwnode = dev_fwnode(dev), *ep; + u8 lane_base = camss->legacy_phy ? 0 : 1; int ret; fwnode_graph_for_each_endpoint(fwnode, ep) { struct camss_async_subdev *csd; - csd = v4l2_async_nf_add_fwnode_remote(&camss->notifier, ep, - typeof(*csd)); + if (!fwnode_device_is_available(ep)) + continue; + + if (camss->legacy_phy) { + csd = v4l2_async_nf_add_fwnode_remote(&camss->notifier, ep, + typeof(*csd)); + } else { + struct fwnode_handle *phy_out, *phy_node, *phy_in, *sensor_ep; + + phy_out = fwnode_graph_get_remote_endpoint(ep); + if (!phy_out) + continue; + + phy_node = fwnode_graph_get_port_parent(phy_out); + fwnode_handle_put(phy_out); + if (!phy_node) + continue; + + phy_in = fwnode_graph_get_endpoint_by_id(phy_node, 0, 0, 0); + fwnode_handle_put(phy_node); + if (!phy_in) + continue; + + sensor_ep = fwnode_graph_get_remote_endpoint(phy_in); + fwnode_handle_put(phy_in); + if (!sensor_ep) + continue; + + csd = v4l2_async_nf_add_fwnode(&camss->notifier, sensor_ep, + struct camss_async_subdev); + fwnode_handle_put(sensor_ep); + } + if (IS_ERR(csd)) { ret = PTR_ERR(csd); goto err_cleanup; } - ret = camss_parse_endpoint_node(dev, ep, csd); + ret = camss_parse_endpoint_node(dev, ep, csd, lane_base); if (ret < 0) goto err_cleanup; } @@ -4819,6 +5087,29 @@ static int camss_parse_ports(struct camss *camss) return ret; } +static void camss_detect_legacy_phy(struct camss *camss) +{ + struct device_node *remote; + struct device_node *ep; + + camss->legacy_phy = true; + + /* Find first remote-endpoint and determine if it is a generic PHY */ + for_each_endpoint_of_node(camss->dev->of_node, ep) { + remote = of_graph_get_remote_port_parent(ep); + if (!remote) + continue; + + camss->legacy_phy = !of_node_name_eq(remote, "phy"); + of_node_put(remote); + of_node_put(ep); + break; + } + + dev_dbg(camss->dev, "legacy phy mode %s\n", + camss->legacy_phy ? "true" : "false"); +} + /* * camss_init_subdevices - Initialize subdev structures and resources * @camss: CAMSS device @@ -4832,10 +5123,17 @@ static int camss_init_subdevices(struct camss *camss) unsigned int i; int ret; + camss_detect_legacy_phy(camss); + for (i = 0; i < camss->res->csiphy_num; i++) { - ret = msm_csiphy_subdev_init(camss, &camss->csiphy[i], - &res->csiphy_res[i], - res->csiphy_res[i].csiphy.id); + if (!camss->legacy_phy) { + ret = msm_csiphy_subdev_init(camss, i); + } else { + ret = msm_csiphy_subdev_init_legacy(camss, + &camss->csiphy[i], + &res->csiphy_res[i], + res->csiphy_res[i].csiphy.id); + } if (ret < 0) { dev_err(camss->dev, "Failed to init csiphy%d sub-device: %d\n", @@ -4917,6 +5215,11 @@ inline void camss_link_err(struct camss *camss, ret); } +static bool csiphy_enabled(struct camss *camss, struct csiphy_device *csiphy) +{ + return camss->legacy_phy || !IS_ERR_OR_NULL(csiphy->phy); +} + /* * camss_link_entities - Register subdev nodes and create links * @camss: CAMSS device @@ -4929,6 +5232,9 @@ static int camss_link_entities(struct camss *camss) int ret; for (i = 0; i < camss->res->csiphy_num; i++) { + if (!csiphy_enabled(camss, &camss->csiphy[i])) + continue; + for (j = 0; j < camss->res->csid_num; j++) { ret = media_create_pad_link(&camss->csiphy[i].subdev.entity, MSM_CSIPHY_PAD_SRC, @@ -5056,6 +5362,9 @@ static int camss_register_entities(struct camss *camss) int ret; for (i = 0; i < camss->res->csiphy_num; i++) { + if (!csiphy_enabled(camss, &camss->csiphy[i])) + continue; + ret = msm_csiphy_register_entity(&camss->csiphy[i], &camss->v4l2_dev); if (ret < 0) { @@ -5131,8 +5440,10 @@ static int camss_register_entities(struct camss *camss) i = camss->res->csiphy_num; err_reg_csiphy: - for (i--; i >= 0; i--) - msm_csiphy_unregister_entity(&camss->csiphy[i]); + for (i--; i >= 0; i--) { + if (csiphy_enabled(camss, &camss->csiphy[i])) + msm_csiphy_unregister_entity(&camss->csiphy[i]); + } return ret; } @@ -5147,8 +5458,10 @@ static void camss_unregister_entities(struct camss *camss) { unsigned int i; - for (i = 0; i < camss->res->csiphy_num; i++) - msm_csiphy_unregister_entity(&camss->csiphy[i]); + for (i = 0; i < camss->res->csiphy_num; i++) { + if (csiphy_enabled(camss, &camss->csiphy[i])) + msm_csiphy_unregister_entity(&camss->csiphy[i]); + } if (camss->tpg) { for (i = 0; i < camss->res->tpg_num; i++) @@ -5346,6 +5659,33 @@ static void camss_genpd_cleanup(struct camss *camss) dev_pm_domain_detach(camss->genpd, true); } +void camss_set_privacy_led(struct camss *camss, u8 csiphy_id, bool on) +{ + mutex_lock(&camss->privacy_led_lock); + + if (csiphy_id == 0 || csiphy_id == 3) { + if (on) + camss->front_led_refcnt++; + else if (camss->front_led_refcnt > 0) + camss->front_led_refcnt--; + + if (camss->privacy_led_front) + gpiod_set_value_cansleep(camss->privacy_led_front, + camss->front_led_refcnt > 0 ? 1 : 0); + } else if (csiphy_id == 2) { + if (on) + camss->rear_led_refcnt++; + else if (camss->rear_led_refcnt > 0) + camss->rear_led_refcnt--; + + if (camss->privacy_led_rear) + gpiod_set_value_cansleep(camss->privacy_led_rear, + camss->rear_led_refcnt > 0 ? 1 : 0); + } + + mutex_unlock(&camss->privacy_led_lock); +} + /* * camss_probe - Probe CAMSS platform device * @pdev: Pointer to CAMSS platform device @@ -5367,6 +5707,23 @@ static int camss_probe(struct platform_device *pdev) atomic_set(&camss->ref_count, 0); camss->dev = dev; platform_set_drvdata(pdev, camss); + mutex_init(&camss->privacy_led_lock); + + camss->privacy_led_front = + devm_gpiod_get_optional(dev, "privacy-led-front", GPIOD_OUT_LOW); + if (IS_ERR(camss->privacy_led_front)) { + dev_warn(dev, "Failed to get front privacy LED GPIO: %ld\n", + PTR_ERR(camss->privacy_led_front)); + camss->privacy_led_front = NULL; + } + + camss->privacy_led_rear = + devm_gpiod_get_optional(dev, "privacy-led-rear", GPIOD_OUT_LOW); + if (IS_ERR(camss->privacy_led_rear)) { + dev_warn(dev, "Failed to get rear privacy LED GPIO: %ld\n", + PTR_ERR(camss->privacy_led_rear)); + camss->privacy_led_rear = NULL; + } camss->csiphy = devm_kcalloc(dev, camss->res->csiphy_num, sizeof(*camss->csiphy), GFP_KERNEL); @@ -5460,9 +5817,46 @@ static int camss_probe(struct platform_device *pdev) goto err_media_device_unregister; } + if (camss->res->vfe_num && + camss->res->vfe_res[0].vfe.hw_ops == &vfe_ops_170) { + camss->dummy_buf_size = 18 * 1024 * 1024; + camss->dummy_buf = dma_alloc_coherent(camss->dev, + camss->dummy_buf_size, + &camss->dummy_buf_iova, + GFP_KERNEL | __GFP_NOWARN); + if (!camss->dummy_buf) { + camss->dummy_buf_size = 4 * 1024 * 1024; + camss->dummy_buf = dma_alloc_coherent(camss->dev, + camss->dummy_buf_size, + &camss->dummy_buf_iova, + GFP_KERNEL); + } + if (camss->dummy_buf) + dev_dbg(camss->dev, + "Allocated dummy DMA buffer %zu MB at IOVA %pad\n", + camss->dummy_buf_size / (1024 * 1024), + &camss->dummy_buf_iova); + } + + if (camss->res->version == CAMSS_8150) { + ret = pm_runtime_resume_and_get(camss->dev); + if (ret < 0) { + dev_err(dev, "Failed to power up CAMSS parent device: %d\n", + ret); + goto err_media_device_unregister; + } + } + return 0; err_media_device_unregister: + if (camss->dummy_buf) { + dma_free_coherent(camss->dev, + camss->dummy_buf_size, + camss->dummy_buf, + camss->dummy_buf_iova); + camss->dummy_buf = NULL; + } media_device_unregister(&camss->media_dev); err_register_subdevs: camss_unregister_entities(camss); @@ -5480,6 +5874,17 @@ static int camss_probe(struct platform_device *pdev) void camss_delete(struct camss *camss) { + if (camss->res->version == CAMSS_8150) + pm_runtime_put_sync(camss->dev); + + if (camss->dummy_buf) { + dma_free_coherent(camss->dev, + camss->dummy_buf_size, + camss->dummy_buf, + camss->dummy_buf_iova); + camss->dummy_buf = NULL; + } + v4l2_device_unregister(&camss->v4l2_dev); media_device_unregister(&camss->media_dev); media_device_cleanup(&camss->media_dev); @@ -5497,6 +5902,11 @@ static void camss_remove(struct platform_device *pdev) { struct camss *camss = platform_get_drvdata(pdev); + if (camss->privacy_led_front) + gpiod_set_value_cansleep(camss->privacy_led_front, 0); + if (camss->privacy_led_rear) + gpiod_set_value_cansleep(camss->privacy_led_rear, 0); + v4l2_async_nf_unregister(&camss->notifier); v4l2_async_nf_cleanup(&camss->notifier); camss_unregister_entities(camss); @@ -5655,6 +6065,19 @@ static const struct camss_resources sm6350_resources = { .vfe_num = ARRAY_SIZE(vfe_res_sm6350), }; +static const struct camss_resources sm8150_resources = { + .version = CAMSS_8150, + .pd_name = "top", + .csiphy_res = csiphy_res_sm8150, + .csid_res = csid_res_sm8150, + .vfe_res = vfe_res_sm8150, + .icc_res = icc_res_sm8150, + .icc_path_num = ARRAY_SIZE(icc_res_sm8150), + .csiphy_num = ARRAY_SIZE(csiphy_res_sm8150), + .csid_num = ARRAY_SIZE(csid_res_sm8150), + .vfe_num = ARRAY_SIZE(vfe_res_sm8150), +}; + static const struct camss_resources sm8250_resources = { .version = CAMSS_8250, .pd_name = "top", @@ -5748,12 +6171,14 @@ static const struct of_device_id camss_dt_match[] = { { .compatible = "qcom,qcs8300-camss", .data = &qcs8300_resources }, { .compatible = "qcom,sa8775p-camss", .data = &sa8775p_resources }, { .compatible = "qcom,sc7280-camss", .data = &sc7280_resources }, + { .compatible = "qcom,sc8180x-camss", .data = &sm8150_resources }, { .compatible = "qcom,sc8280xp-camss", .data = &sc8280xp_resources }, { .compatible = "qcom,sdm660-camss", .data = &sdm660_resources }, { .compatible = "qcom,sdm670-camss", .data = &sdm670_resources }, { .compatible = "qcom,sdm845-camss", .data = &sdm845_resources }, { .compatible = "qcom,sm6150-camss", .data = &sm6150_resources }, { .compatible = "qcom,sm6350-camss", .data = &sm6350_resources }, + { .compatible = "qcom,sm8150-camss", .data = &sm8150_resources }, { .compatible = "qcom,sm8250-camss", .data = &sm8250_resources }, { .compatible = "qcom,sm8550-camss", .data = &sm8550_resources }, { .compatible = "qcom,sm8650-camss", .data = &sm8650_resources }, @@ -5769,6 +6194,13 @@ static int __maybe_unused camss_runtime_suspend(struct device *dev) int i; int ret; + /* + * On SC8180X / SM8150, dropping camnoc_axi / cam_ahb interconnects + * while TITAN_TOP is active triggers a NoC bus deadlock. + */ + if (camss->res->version == CAMSS_8150) + return 0; + for (i = 0; i < camss->res->icc_path_num; i++) { ret = icc_set_bw(camss->icc_path[i], 0, 0); if (ret) diff --git a/drivers/media/platform/qcom/camss/camss.h b/drivers/media/platform/qcom/camss/camss.h index 93d691c8ac63b..46045917df214 100644 --- a/drivers/media/platform/qcom/camss/camss.h +++ b/drivers/media/platform/qcom/camss/camss.h @@ -11,6 +11,9 @@ #define QC_MSM_CAMSS_H #include +#include +#include +#include #include #include #include @@ -89,6 +92,7 @@ enum camss_version { CAMSS_8x39, CAMSS_8x53, CAMSS_8x96, + CAMSS_8150, CAMSS_8250, CAMSS_8280XP, CAMSS_8300, @@ -138,6 +142,15 @@ struct camss { struct device_link *genpd_link; struct icc_path *icc_path[ICC_SM8250_COUNT]; const struct camss_resources *res; + bool legacy_phy; + void *dummy_buf; + dma_addr_t dummy_buf_iova; + size_t dummy_buf_size; + struct gpio_desc *privacy_led_front; + struct gpio_desc *privacy_led_rear; + struct mutex privacy_led_lock; + int front_led_refcnt; + int rear_led_refcnt; }; struct camss_camera_interface { @@ -179,5 +192,6 @@ void camss_delete(struct camss *camss); void camss_buf_done(struct camss *camss, int hw_id, int port_id); void camss_reg_update(struct camss *camss, int hw_id, int port_id, bool is_clear); +void camss_set_privacy_led(struct camss *camss, u8 csiphy_id, bool on); #endif /* QC_MSM_CAMSS_H */ diff --git a/drivers/phy/qualcomm/Kconfig b/drivers/phy/qualcomm/Kconfig index d910a5d1a1ac3..8ab8d060da465 100644 --- a/drivers/phy/qualcomm/Kconfig +++ b/drivers/phy/qualcomm/Kconfig @@ -242,3 +242,15 @@ config PHY_QCOM_SGMII_ETH help Enable this to support the internal SerDes/SGMII PHY on various Qualcomm chipsets. + +config PHY_QCOM_MIPI_CSI2 + tristate "Qualcomm MIPI CSI-2 D-PHY driver" + depends on OF && (ARCH_QCOM || COMPILE_TEST) + depends on HAS_IOMEM + select GENERIC_PHY + select GENERIC_PHY_MIPI_DPHY + help + Enable this to support the standalone Qualcomm MIPI CSI-2 D-PHY + receiver blocks used with Qualcomm CAMSS on SM8150, SC8180X, and + X1E80100 SoCs. Each CSIPHY instance is exposed through the generic + PHY framework so CAMSS can configure lane routing and power states. diff --git a/drivers/phy/qualcomm/Makefile b/drivers/phy/qualcomm/Makefile index 8bf887d58ee4a..c5262902b2397 100644 --- a/drivers/phy/qualcomm/Makefile +++ b/drivers/phy/qualcomm/Makefile @@ -6,6 +6,9 @@ obj-$(CONFIG_PHY_QCOM_IPQ4019_USB) += phy-qcom-ipq4019-usb.o obj-$(CONFIG_PHY_QCOM_IPQ806X_SATA) += phy-qcom-ipq806x-sata.o obj-$(CONFIG_PHY_QCOM_M31_USB) += phy-qcom-m31.o obj-$(CONFIG_PHY_QCOM_M31_EUSB) += phy-qcom-m31-eusb2.o +obj-$(CONFIG_PHY_QCOM_MIPI_CSI2) += phy-qcom-mipi-csi2.o +phy-qcom-mipi-csi2-y := phy-qcom-mipi-csi2-core.o \ + phy-qcom-mipi-csi2-3ph-dphy.o obj-$(CONFIG_PHY_QCOM_PCIE2) += phy-qcom-pcie2.o obj-$(CONFIG_PHY_QCOM_QMP_COMBO) += phy-qcom-qmp-combo.o phy-qcom-qmp-usbc.o diff --git a/drivers/phy/qualcomm/phy-qcom-mipi-csi2-3ph-dphy.c b/drivers/phy/qualcomm/phy-qcom-mipi-csi2-3ph-dphy.c new file mode 100644 index 0000000000000..01e1a2bf6d35b --- /dev/null +++ b/drivers/phy/qualcomm/phy-qcom-mipi-csi2-3ph-dphy.c @@ -0,0 +1,489 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Qualcomm MSM Camera Subsystem - CSIPHY Module 3phase v1.0 + * + * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. + * Copyright (C) 2016-2026 Linaro Ltd. + */ + +#include +#include +#include +#include +#include + +#include "phy-qcom-mipi-csi2.h" + +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(offset, n) ((offset) + 0x4 * (n)) +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRL0_PHY_SW_RESET BIT(0) +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRL5_CLK_ENABLE BIT(7) +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_COMMON_PWRDN_B BIT(0) +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_SHOW_REV_ID BIT(1) +#define CSIPHY_3PH_CMN_CSI_COMMON_CTRL10_IRQ_CLEAR_CMD BIT(0) +#define CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(offset, n) ((offset) + 0xb0 + 0x4 * (n)) + +#define CSIPHY_2PH_LN_CSI_2PHASE_CTRL9n(n) ((0x200 * (n)) + 0x24) + +/* + * 3 phase CSI has 19 common status regs with only 0-10 being used + * and 11-18 being reserved. + */ +#define CSI_COMMON_STATUS_NUM 11 +/* + * There are a number of common control registers + * The offset to clear the CSIPHY IRQ status starts @ 22 + * So to clear CSI_COMMON_STATUS0 this is CSI_COMMON_CONTROL22, STATUS1 is + * CONTROL23 and so on + */ +#define CSI_CTRL_STATUS_INDEX 22 + +/* + * There are 43 COMMON_CTRL registers with regs after # 33 being reserved + */ +#define CSI_CTRL_MAX 33 + +#define CSIPHY_DEFAULT_PARAMS 0 +#define CSIPHY_SETTLE_CNT_LOWER_BYTE 2 +#define CSIPHY_SKEW_CAL 7 + +/* 7nm 2PH v 1.1 2p5Gbps 4 lane DPHY mode */ +static const struct +mipi_csi2phy_lane_regs lane_regs_sm8150[] = { + {.reg_addr = 0x0004, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x002c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0034, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x001c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0014, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x003c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0000, .reg_data = 0x90, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0008, .reg_data = 0x0e, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0010, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0038, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0060, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0064, .reg_data = 0x7f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0704, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x072c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0734, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x071c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0714, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0728, .reg_data = 0x04, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x073c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0700, .reg_data = 0x80, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0708, .reg_data = 0x0e, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x070c, .reg_data = 0xa5, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0710, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0738, .reg_data = 0x1f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0760, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0764, .reg_data = 0x7f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0204, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x022c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0234, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x021c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0214, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x023c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0200, .reg_data = 0x90, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0208, .reg_data = 0x0e, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0210, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0238, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0260, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0264, .reg_data = 0x7f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0404, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x042c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0434, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x041c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0414, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x043c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0400, .reg_data = 0x90, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0408, .reg_data = 0x0e, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0410, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0438, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0460, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0464, .reg_data = 0x7f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0604, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x062c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0634, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x061c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0614, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x063c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0600, .reg_data = 0x90, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0608, .reg_data = 0x0e, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0610, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0638, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0660, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0664, .reg_data = 0x7f, .param_type = CSIPHY_DEFAULT_PARAMS}, +}; + +/* 4nm 2PH v 2.1.2 2p5Gbps 4 lane DPHY mode */ +static const struct +mipi_csi2phy_lane_regs lane_regs_x1e80100[] = { + /* Power up lanes 2ph mode */ + {.reg_addr = 0x101c, .reg_data = 0x7a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x1018, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + + {.reg_addr = 0x0094, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x00a0, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0090, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0098, .reg_data = 0x08, .param_type = CSIPHY_DEFAULT_PARAMS}, + { .reg_addr = 0x0094, .reg_data = 0x07, .delay_us = 0x01, + .param_type = CSIPHY_DEFAULT_PARAMS }, + {.reg_addr = 0x0030, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0000, .reg_data = 0x8e, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0038, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x002c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0034, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x001c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0014, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x003c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0004, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0020, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0008, .reg_data = 0x10, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0010, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0094, .reg_data = 0xd7, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x005c, .reg_data = 0x00, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0060, .reg_data = 0xbd, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0064, .reg_data = 0x7f, .param_type = CSIPHY_SKEW_CAL}, + + {.reg_addr = 0x0e94, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0ea0, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e90, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e98, .reg_data = 0x08, .param_type = CSIPHY_DEFAULT_PARAMS}, + { .reg_addr = 0x0e94, .reg_data = 0x07, .delay_us = 0x01, + .param_type = CSIPHY_DEFAULT_PARAMS }, + {.reg_addr = 0x0e30, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e28, .reg_data = 0x04, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e00, .reg_data = 0x80, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e0c, .reg_data = 0xff, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e38, .reg_data = 0x1f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e2c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e34, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e1c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e14, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e3c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e04, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e20, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0e08, .reg_data = 0x10, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0e10, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + + {.reg_addr = 0x0494, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x04a0, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0490, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0498, .reg_data = 0x08, .param_type = CSIPHY_DEFAULT_PARAMS}, + { .reg_addr = 0x0494, .reg_data = 0x07, .delay_us = 0x01, + .param_type = CSIPHY_DEFAULT_PARAMS }, + {.reg_addr = 0x0430, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0400, .reg_data = 0x8e, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0438, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x042c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0434, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x041c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0414, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x043c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0404, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0420, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0408, .reg_data = 0x10, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0410, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0494, .reg_data = 0xd7, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x045c, .reg_data = 0x00, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0460, .reg_data = 0xbd, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0464, .reg_data = 0x7f, .param_type = CSIPHY_SKEW_CAL}, + + {.reg_addr = 0x0894, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x08a0, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0890, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0898, .reg_data = 0x08, .param_type = CSIPHY_DEFAULT_PARAMS}, + { .reg_addr = 0x0894, .reg_data = 0x07, .delay_us = 0x01, + .param_type = CSIPHY_DEFAULT_PARAMS }, + {.reg_addr = 0x0830, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0800, .reg_data = 0x8e, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0838, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x082c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0834, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x081c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0814, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x083c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0804, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0820, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0808, .reg_data = 0x10, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0810, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0894, .reg_data = 0xd7, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x085c, .reg_data = 0x00, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0860, .reg_data = 0xbd, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0864, .reg_data = 0x7f, .param_type = CSIPHY_SKEW_CAL}, + + {.reg_addr = 0x0c94, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0ca0, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c90, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c98, .reg_data = 0x08, .param_type = CSIPHY_DEFAULT_PARAMS}, + { .reg_addr = 0x0c94, .reg_data = 0x07, .delay_us = 0x01, + .param_type = CSIPHY_DEFAULT_PARAMS }, + {.reg_addr = 0x0c30, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c00, .reg_data = 0x8e, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c38, .reg_data = 0xfe, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c2c, .reg_data = 0x01, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c34, .reg_data = 0x0f, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c1c, .reg_data = 0x0a, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c14, .reg_data = 0x60, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c3c, .reg_data = 0xb8, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c04, .reg_data = 0x0c, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c20, .reg_data = 0x00, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c08, .reg_data = 0x10, .param_type = CSIPHY_SETTLE_CNT_LOWER_BYTE}, + {.reg_addr = 0x0c10, .reg_data = 0x52, .param_type = CSIPHY_DEFAULT_PARAMS}, + {.reg_addr = 0x0c94, .reg_data = 0xd7, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0c5c, .reg_data = 0x00, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0c60, .reg_data = 0xbd, .param_type = CSIPHY_SKEW_CAL}, + {.reg_addr = 0x0c64, .reg_data = 0x7f, .param_type = CSIPHY_SKEW_CAL}, +}; + +static inline const struct mipi_csi2phy_device_regs * +csi2phy_dev_to_regs(struct mipi_csi2phy_device *csi2phy) +{ + return &csi2phy->soc_cfg->reg_info; +} + +static void phy_qcom_mipi_csi2_hw_version_read(struct mipi_csi2phy_device *csi2phy) +{ + const struct mipi_csi2phy_device_regs *regs = csi2phy_dev_to_regs(csi2phy); + u32 tmp; + + writel(CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_SHOW_REV_ID, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 6)); + + tmp = readl_relaxed(csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(regs->common_regs_offset, 12)); + csi2phy->hw_version = tmp; + + tmp = readl_relaxed(csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(regs->common_regs_offset, 13)); + csi2phy->hw_version |= (tmp << 8) & 0xFF00; + + tmp = readl_relaxed(csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(regs->common_regs_offset, 14)); + csi2phy->hw_version |= (tmp << 16) & 0xFF0000; + + tmp = readl_relaxed(csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(regs->common_regs_offset, 15)); + csi2phy->hw_version |= (tmp << 24) & 0xFF000000; + + dev_dbg_once(csi2phy->dev, "CSIPHY 3PH HW Version = 0x%08x\n", csi2phy->hw_version); +} + +/* + * phy_qcom_mipi_csi2_reset - Perform software reset on CSIPHY module + * @phy_qcom_mipi_csi2: CSIPHY device + */ +static void phy_qcom_mipi_csi2_reset(struct mipi_csi2phy_device *csi2phy) +{ + const struct mipi_csi2phy_device_regs *regs = csi2phy_dev_to_regs(csi2phy); + + writel(CSIPHY_3PH_CMN_CSI_COMMON_CTRL0_PHY_SW_RESET, + csi2phy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 0)); + usleep_range(5000, 8000); + writel(0x0, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 0)); +} + +/* + * phy_qcom_mipi_csi2_settle_cnt_calc - Calculate settle count value + * + * Helper function to calculate settle count value. This is + * based on the CSI2 T_hs_settle parameter which in turn + * is calculated based on the CSI2 transmitter link frequency. + * + * Return settle count. + */ +static u8 phy_qcom_mipi_csi2_settle_cnt_calc(s64 link_freq, u32 timer_clk_rate) +{ + u32 t_hs_prepare_max_ps; + u32 timer_period_ps; + u32 t_hs_settle_ps; + u8 settle_cnt; + u32 ui_ps; + + ui_ps = div64_u64(PSEC_PER_SEC, link_freq); + ui_ps /= 2; + t_hs_prepare_max_ps = 85000 + 6 * ui_ps; + t_hs_settle_ps = t_hs_prepare_max_ps; + + timer_period_ps = div_u64(PSEC_PER_SEC, timer_clk_rate); + + if ((t_hs_settle_ps / timer_period_ps) < 6) + return 0; + + settle_cnt = t_hs_settle_ps / timer_period_ps - 6; + + return settle_cnt; +} + +static void +phy_qcom_mipi_csi2_gen2_config_lanes(struct mipi_csi2phy_device *csi2phy, + u8 settle_cnt) +{ + const struct mipi_csi2phy_device_regs *regs = csi2phy_dev_to_regs(csi2phy); + const struct mipi_csi2phy_lane_regs *r = regs->init_seq; + int i, array_size = regs->lane_array_size; + u32 val; + + for (i = 0; i < array_size; i++, r++) { + switch (r->param_type) { + case CSIPHY_SETTLE_CNT_LOWER_BYTE: + val = settle_cnt & 0xff; + break; + case CSIPHY_SKEW_CAL: + /* TODO: support application of skew from dt flag */ + continue; + default: + val = r->reg_data; + break; + } + writel(val, csi2phy->base + r->reg_addr); + if (r->delay_us) + udelay(r->delay_us); + } +} + +static int phy_qcom_mipi_csi2_lanes_enable(struct mipi_csi2phy_device *csi2phy, + struct mipi_csi2phy_stream_cfg *cfg) +{ + const struct mipi_csi2phy_device_regs *regs = csi2phy_dev_to_regs(csi2phy); + struct mipi_csi2phy_lanes_cfg *lane_cfg = &cfg->lane_cfg; + u8 settle_cnt; + u8 val; + int i; + + if (cfg->link_freq <= 0) + return -EINVAL; + + settle_cnt = phy_qcom_mipi_csi2_settle_cnt_calc(cfg->link_freq, csi2phy->timer_clk_rate); + if (!settle_cnt) + return -ENODEV; + + /* + * CSI_COMMON_CTRL5 is a physical lane power-up bitmap: + * - Bits [0,2,4,6] -> D-PHY data lanes (LN0, LN2, LN4, LN6) + * - Bits [1,3,5] -> C-PHY trio lanes (LN1, LN3, LN5) + * - Bit [7] -> D-PHY clock lane (LNCK) dedicated clock enable + */ + val = CSIPHY_3PH_CMN_CSI_COMMON_CTRL5_CLK_ENABLE; + for (i = 0; i < cfg->num_data_lanes; i++) + val |= BIT(lane_cfg->data[i].pos * 2); + + writel(val, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 5)); + + /* Lane configuration for polarity @ CSIPHY-base + CTRL9 */ + for (i = 0; i < cfg->num_data_lanes; i++) { + if (lane_cfg->data[i].pol) { + u8 pos = lane_cfg->data[i].pos; + + writel(BIT(2), csi2phy->base + CSIPHY_2PH_LN_CSI_2PHASE_CTRL9n(pos * 2)); + } + } + + if (lane_cfg->clk.pol) + writel(BIT(2), csi2phy->base + CSIPHY_2PH_LN_CSI_2PHASE_CTRL9n(lane_cfg->clk.pos)); + + val = CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_COMMON_PWRDN_B; + writel(val, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 6)); + + val = 0x02; + writel(val, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 7)); + + /* Ensure SW_RESET is de-asserted (do NOT pulse reset here or CTRL5/6 are wiped!) */ + val = 0x00; + writel(val, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 0)); + + phy_qcom_mipi_csi2_gen2_config_lanes(csi2phy, settle_cnt); + + /* IRQ_MASK registers - disable all interrupts */ + for (i = CSI_COMMON_STATUS_NUM; i < CSI_CTRL_STATUS_INDEX; i++) { + writel(0, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, i)); + } + + return 0; +} + +static void +phy_qcom_mipi_csi2_lanes_disable(struct mipi_csi2phy_device *csi2phy, + struct mipi_csi2phy_stream_cfg *cfg) +{ + const struct mipi_csi2phy_device_regs *regs = csi2phy_dev_to_regs(csi2phy); + + writel(0, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 5)); + + writel(0, csi2phy->base + + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(regs->common_regs_offset, 6)); +} + +static const struct mipi_csi2phy_hw_ops phy_qcom_mipi_csi2_ops_3ph_1_0 = { + .hw_version_read = phy_qcom_mipi_csi2_hw_version_read, + .reset = phy_qcom_mipi_csi2_reset, + .lanes_enable = phy_qcom_mipi_csi2_lanes_enable, + .lanes_disable = phy_qcom_mipi_csi2_lanes_disable, +}; + +static const char * const sm8150_clks[] = { + "core", + "timer" +}; + +static const char * const sm8150_supplies[] = { + "vdda-phy", + "vdda-pll" +}; + +static struct mipi_csi2_genpd sm8150_genpds[] = { + { .name = "top", .scaled = false }, +}; + +const struct mipi_csi2phy_soc_cfg mipi_csi2_dphy_sm8150 = { + .ops = &phy_qcom_mipi_csi2_ops_3ph_1_0, + .reg_info = { + .init_seq = lane_regs_sm8150, + .lane_array_size = ARRAY_SIZE(lane_regs_sm8150), + .common_regs_offset = 0x800, + }, + .supply_names = sm8150_supplies, + .num_supplies = ARRAY_SIZE(sm8150_supplies), + .clk_names = sm8150_clks, + .num_clk = ARRAY_SIZE(sm8150_clks), + .timer_rate_hz = 300000000, + .genpds = sm8150_genpds, + .num_genpds = ARRAY_SIZE(sm8150_genpds), +}; + +static const char * const x1e_clks[] = { + "core", + "timer", + "ahb" +}; + +static const char * const x1e_supplies[] = { + "vdda-0p9", + "vdda-1p2" +}; + +static struct mipi_csi2_genpd x1e_genpds[] = { + { .name = "top", .scaled = false }, + { .name = "mmcx", .scaled = true }, + { .name = "mx", .scaled = true }, +}; + +const struct mipi_csi2phy_soc_cfg mipi_csi2_dphy_4nm_x1e = { + .ops = &phy_qcom_mipi_csi2_ops_3ph_1_0, + .reg_info = { + .init_seq = lane_regs_x1e80100, + .lane_array_size = ARRAY_SIZE(lane_regs_x1e80100), + .common_regs_offset = 0x1000, + }, + .supply_names = x1e_supplies, + .num_supplies = ARRAY_SIZE(x1e_supplies), + .clk_names = x1e_clks, + .num_clk = ARRAY_SIZE(x1e_clks), + .genpds = x1e_genpds, + .num_genpds = ARRAY_SIZE(x1e_genpds), +}; diff --git a/drivers/phy/qualcomm/phy-qcom-mipi-csi2-core.c b/drivers/phy/qualcomm/phy-qcom-mipi-csi2-core.c new file mode 100644 index 0000000000000..208bfa92418ff --- /dev/null +++ b/drivers/phy/qualcomm/phy-qcom-mipi-csi2-core.c @@ -0,0 +1,489 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2026, Linaro Ltd. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "phy-qcom-mipi-csi2.h" + +static int +phy_qcom_mipi_csi2_set_clock_rates(struct mipi_csi2phy_device *csi2phy, + s64 link_freq) +{ + struct device *dev = csi2phy->dev; + unsigned long opp_rate = link_freq / 4; + struct dev_pm_opp *opp; + long timer_rate; + int i, pstate; + int ret; + + opp = dev_pm_opp_find_freq_ceil(dev, &opp_rate); + if (IS_ERR(opp)) { + struct clk *core_clk = NULL; + + for (i = 0; i < csi2phy->soc_cfg->num_clk; i++) { + if (csi2phy->clks[i].id && + !strcmp(csi2phy->clks[i].id, "core")) { + core_clk = csi2phy->clks[i].clk; + break; + } + } + if (!core_clk) { + dev_err(csi2phy->dev, "Couldn't find ceiling for %lld Hz\n", + link_freq); + return PTR_ERR(opp); + } + opp_rate = clk_round_rate(core_clk, opp_rate); + ret = clk_set_rate(core_clk, opp_rate); + if (ret) + return ret; + i = 0; + goto set_timer_clk; + } + + pstate = 0; + for (i = 0; i < csi2phy->pd_list->num_pds; i++) { + unsigned int perf; + + if (!csi2phy->soc_cfg->genpds[i].scaled) + continue; + + perf = dev_pm_opp_get_required_pstate(opp, pstate); + pstate += 1; + + ret = dev_pm_genpd_set_performance_state(csi2phy->pd_list->pd_devs[i], perf); + if (ret) { + dev_err(csi2phy->dev, "Couldn't set perf state %u\n", + perf); + dev_pm_opp_put(opp); + goto unset_pstate; + } + } + dev_pm_opp_put(opp); + + ret = dev_pm_opp_set_rate(dev, opp_rate); + if (ret) { + dev_err(csi2phy->dev, "dev_pm_opp_set_rate() fail\n"); + goto unset_opp_rate; + } + +set_timer_clk: + + if (csi2phy->soc_cfg->timer_rate_hz) + timer_rate = clk_round_rate(csi2phy->timer_clk, + csi2phy->soc_cfg->timer_rate_hz); + else + timer_rate = clk_round_rate(csi2phy->timer_clk, link_freq / 4); + if (timer_rate <= 0) { + ret = -ENODEV; + goto unset_opp_rate; + } + + ret = clk_set_rate(csi2phy->timer_clk, timer_rate); + if (ret) + goto unset_opp_rate; + + csi2phy->timer_clk_rate = timer_rate; + + return 0; + +unset_opp_rate: + dev_pm_opp_set_rate(dev, 0); + +unset_pstate: + while (i--) { + if (!csi2phy->soc_cfg->genpds[i].scaled) + continue; + + dev_pm_genpd_set_performance_state(csi2phy->pd_list->pd_devs[i], 0); + } + + return ret; +} + +static int phy_qcom_mipi_csi2_configure(struct phy *phy, + union phy_configure_opts *opts) +{ + struct mipi_csi2phy_device *csi2phy = phy_get_drvdata(phy); + struct phy_configure_opts_mipi_dphy *dphy_cfg = &opts->mipi_dphy; + struct mipi_csi2phy_stream_cfg *stream_cfg = &csi2phy->stream_cfg; + int ret; + + ret = phy_mipi_dphy_config_validate(dphy_cfg); + if (ret) + return ret; + + if (dphy_cfg->lanes < 1 || dphy_cfg->lanes > CSI2_MAX_DATA_LANES) + return -EINVAL; + + if (dphy_cfg->lanes != stream_cfg->num_data_lanes) + return -EINVAL; + + stream_cfg->link_freq = dphy_cfg->hs_clk_rate; + + return 0; +} + +static int phy_qcom_mipi_csi2_set_mode(struct phy *phy, + enum phy_mode mode, int submode) +{ + struct mipi_csi2phy_device *csi2phy = phy_get_drvdata(phy); + + if (mode != PHY_MODE_MIPI_DPHY) + return -EOPNOTSUPP; + + csi2phy->phy_mode = mode; + return 0; +} + +static int phy_qcom_mipi_csi2_power_on(struct phy *phy) +{ + struct mipi_csi2phy_device *csi2phy = phy_get_drvdata(phy); + const struct mipi_csi2phy_hw_ops *ops = csi2phy->soc_cfg->ops; + int i, ret; + + if (!csi2phy->powered_on) { + ret = regulator_bulk_enable(csi2phy->soc_cfg->num_supplies, + csi2phy->supplies); + if (ret) + return ret; + + ret = pm_runtime_resume_and_get(csi2phy->dev); + if (ret < 0) + goto disable_regulators; + + ret = phy_qcom_mipi_csi2_set_clock_rates(csi2phy, csi2phy->stream_cfg.link_freq); + if (ret) + goto poweroff_phy; + + ret = clk_bulk_prepare_enable(csi2phy->soc_cfg->num_clk, + csi2phy->clks); + if (ret) { + dev_err(csi2phy->dev, "failed to enable clocks, %d\n", ret); + goto unset_rate; + } + + ops->reset(csi2phy); + + ops->hw_version_read(csi2phy); + csi2phy->powered_on = true; + } else { + ret = phy_qcom_mipi_csi2_set_clock_rates(csi2phy, csi2phy->stream_cfg.link_freq); + if (ret) + return ret; + + ops->reset(csi2phy); + } + + ret = ops->lanes_enable(csi2phy, &csi2phy->stream_cfg); + if (ret) + return ret; + + return 0; + +unset_rate: + for (i = 0; i < csi2phy->pd_list->num_pds; i++) { + if (!csi2phy->soc_cfg->genpds[i].scaled) + continue; + + dev_pm_genpd_set_performance_state(csi2phy->pd_list->pd_devs[i], 0); + } + +poweroff_phy: + pm_runtime_put_sync(csi2phy->dev); + +disable_regulators: + regulator_bulk_disable(csi2phy->soc_cfg->num_supplies, + csi2phy->supplies); + + return ret; +} + +static int phy_qcom_mipi_csi2_power_off(struct phy *phy) +{ + struct mipi_csi2phy_device *csi2phy = phy_get_drvdata(phy); + const struct mipi_csi2phy_hw_ops *ops = csi2phy->soc_cfg->ops; + + ops->lanes_disable(csi2phy, &csi2phy->stream_cfg); + + return 0; +} + +static const struct phy_ops phy_qcom_mipi_csi2_ops = { + .configure = phy_qcom_mipi_csi2_configure, + .set_mode = phy_qcom_mipi_csi2_set_mode, + .power_on = phy_qcom_mipi_csi2_power_on, + .power_off = phy_qcom_mipi_csi2_power_off, + .owner = THIS_MODULE, +}; + +static int phy_qcom_mipi_csi2_attach_pm_domains(struct mipi_csi2phy_device *csi2phy) +{ + struct dev_pm_domain_attach_data pd_data = { 0 }; + const char **pd_names; + int i; + + /* + * platform_probe() auto-attaches a single power domain during probe. + * Detach it first so devm_pm_domain_attach_list() can attach all + * domains by name (otherwise it returns -EEXIST for single-domain + * SoCs such as SM8150). + */ + if (csi2phy->dev->pm_domain) + dev_pm_domain_detach(csi2phy->dev, false); + + pd_names = devm_kzalloc(csi2phy->dev, + sizeof(char *) * csi2phy->soc_cfg->num_genpds, + GFP_KERNEL); + if (!pd_names) + return -ENOMEM; + + for (i = 0; i < csi2phy->soc_cfg->num_genpds; i++) + pd_names[i] = csi2phy->soc_cfg->genpds[i].name; + + pd_data.pd_names = pd_names; + pd_data.num_pd_names = csi2phy->soc_cfg->num_genpds; + + return devm_pm_domain_attach_list(csi2phy->dev, &pd_data, + &csi2phy->pd_list); +} + +static int phy_qcom_mipi_csi2_parse_routing(struct mipi_csi2phy_device *csi2phy) +{ + struct mipi_csi2phy_stream_cfg *stream_cfg = &csi2phy->stream_cfg; + u32 lane_polarities[CSI2_MAX_DATA_LANES + 1]; + u32 data_lanes[CSI2_MAX_DATA_LANES]; + struct device *dev = csi2phy->dev; + struct fwnode_handle *ep; + int num_polarities; + int num_data_lanes; + u32 bus_type; + int i, ret; + + ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 1, 0); + if (ep) { + fwnode_handle_put(ep); + dev_err(dev, "DPHY split mode is not supported\n"); + return -EOPNOTSUPP; + } + + ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev), 0, 0, 0); + if (!ep) { + dev_info(dev, "No active sensor on port@0; defaulting to 1-lane passive mode\n"); + stream_cfg->num_data_lanes = 1; + stream_cfg->lane_cfg.clk.pos = CSI2_DEFAULT_CLK_LANE; + stream_cfg->lane_cfg.clk.pol = 0; + stream_cfg->lane_cfg.data[0].pos = 0; + stream_cfg->lane_cfg.data[0].pol = 0; + return 0; + } + + /* bus-type is optional, D-PHY is the default */ + bus_type = MEDIA_BUS_TYPE_CSI2_DPHY; + fwnode_property_read_u32(ep, "bus-type", &bus_type); + + if (bus_type != MEDIA_BUS_TYPE_CSI2_DPHY) { + ret = -EOPNOTSUPP; + dev_err(dev, "Unsupported bus-type %u\n", bus_type); + goto out_put; + } + + num_data_lanes = fwnode_property_count_u32(ep, "data-lanes"); + if (num_data_lanes < 1 || num_data_lanes > CSI2_MAX_DATA_LANES) { + ret = -EINVAL; + dev_err(dev, "Invalid data-lanes count: %d\n", num_data_lanes); + goto out_put; + } + stream_cfg->num_data_lanes = num_data_lanes; + + ret = fwnode_property_read_u32_array(ep, "data-lanes", data_lanes, + stream_cfg->num_data_lanes); + if (ret) { + dev_err(dev, "Failed to read data-lanes: %d\n", ret); + goto out_put; + } + + /* lane-polarities: optional, up to num_data_lanes + 1 entries */ + memset(lane_polarities, 0x00, sizeof(lane_polarities)); + num_polarities = fwnode_property_count_u32(ep, "lane-polarities"); + if (num_polarities > 0) { + if (num_polarities != stream_cfg->num_data_lanes + 1) { + ret = -EINVAL; + dev_err(dev, "clock+data-lane %d/polarities %d mismatch\n", + stream_cfg->num_data_lanes + 1, num_polarities); + goto out_put; + } + + ret = fwnode_property_read_u32_array(ep, "lane-polarities", lane_polarities, + num_polarities); + if (ret) { + dev_err(dev, "Failed to read lane-polarities: %d\n", ret); + goto out_put; + } + } + + csi2phy->stream_cfg.lane_cfg.clk.pos = CSI2_DEFAULT_CLK_LANE; + csi2phy->stream_cfg.lane_cfg.clk.pol = lane_polarities[0]; + + for (i = 0; i < csi2phy->stream_cfg.num_data_lanes; i++) { + if (data_lanes[i] < 1 || data_lanes[i] > CSI2_MAX_DATA_LANES) { + dev_err(dev, "Invalid lane %d\n", data_lanes[i]); + ret = -EINVAL; + goto out_put; + } + + /* Convert data-lanes = <1 2 3 4> to bit positions */ + csi2phy->stream_cfg.lane_cfg.data[i].pos = data_lanes[i] - 1; + csi2phy->stream_cfg.lane_cfg.data[i].pol = lane_polarities[i + 1]; + } + + ret = 0; + +out_put: + fwnode_handle_put(ep); + + return ret; +} + +static int phy_qcom_mipi_csi2_probe(struct platform_device *pdev) +{ + unsigned int i, num_clk, num_supplies; + struct mipi_csi2phy_device *csi2phy; + struct phy_provider *phy_provider; + struct device *dev = &pdev->dev; + struct phy *generic_phy; + int ret; + + csi2phy = devm_kzalloc(dev, sizeof(*csi2phy), GFP_KERNEL); + if (!csi2phy) + return -ENOMEM; + + csi2phy->dev = dev; + dev_set_drvdata(dev, csi2phy); + + csi2phy->soc_cfg = device_get_match_data(&pdev->dev); + + if (!csi2phy->soc_cfg) + return -EINVAL; + + num_clk = csi2phy->soc_cfg->num_clk; + + ret = phy_qcom_mipi_csi2_parse_routing(csi2phy); + if (ret) + return ret; + + ret = phy_qcom_mipi_csi2_attach_pm_domains(csi2phy); + if (ret < 0 || !csi2phy->pd_list) { + if (ret == 0) + ret = -ENODEV; + return dev_err_probe(dev, ret, "Failed to attach power-domain list\n"); + } + + pm_runtime_set_autosuspend_delay(dev, 2000); + pm_runtime_use_autosuspend(dev); + ret = devm_pm_runtime_enable(dev); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to enable pm runtime\n"); + + ret = devm_clk_bulk_get_all(dev, &csi2phy->clks); + if (ret < 0) + return dev_err_probe(dev, ret, "Failed to get clocks\n"); + + if (num_clk != ret) { + return dev_err_probe(dev, -ENODEV, "clock count %d expected %d\n", + ret, num_clk); + } + + for (i = 0; i < num_clk; i++) { + if (!csi2phy->clks[i].id) + return dev_err_probe(dev, -EINVAL, "Missing clock-names\n"); + + if (!strcmp(csi2phy->clks[i].id, "timer")) { + csi2phy->timer_clk = csi2phy->clks[i].clk; + break; + } + } + if (!csi2phy->timer_clk) + return dev_err_probe(dev, -ENODEV, "no timer clock\n"); + + ret = devm_pm_opp_set_clkname(dev, "core"); + if (ret) + return dev_err_probe(dev, ret, "Failed to set opp clkname\n"); + + ret = devm_pm_opp_of_add_table(dev); + if (ret && ret != -ENODEV) + return dev_err_probe(dev, ret, "invalid OPP table in device tree\n"); + + num_supplies = csi2phy->soc_cfg->num_supplies; + csi2phy->supplies = devm_kzalloc(dev, sizeof(*csi2phy->supplies) * num_supplies, + GFP_KERNEL); + if (!csi2phy->supplies) + return -ENOMEM; + + for (i = 0; i < num_supplies; i++) + csi2phy->supplies[i].supply = csi2phy->soc_cfg->supply_names[i]; + + ret = devm_regulator_bulk_get(dev, num_supplies, csi2phy->supplies); + if (ret) + return dev_err_probe(dev, ret, + "failed to get regulator supplies\n"); + + csi2phy->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(csi2phy->base)) + return PTR_ERR(csi2phy->base); + + generic_phy = devm_phy_create(dev, NULL, &phy_qcom_mipi_csi2_ops); + if (IS_ERR(generic_phy)) { + ret = PTR_ERR(generic_phy); + return dev_err_probe(dev, ret, "failed to create phy\n"); + } + csi2phy->phy = generic_phy; + + phy_set_drvdata(generic_phy, csi2phy); + + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); + if (!IS_ERR(phy_provider)) + dev_dbg(dev, "Registered MIPI CSI2 PHY device\n"); + + return PTR_ERR_OR_ZERO(phy_provider); +} + +static const struct of_device_id phy_qcom_mipi_csi2_of_match_table[] = { + { .compatible = "qcom,sc8180x-csi2-phy", .data = &mipi_csi2_dphy_sm8150 }, + { .compatible = "qcom,sm8150-csi2-phy", .data = &mipi_csi2_dphy_sm8150 }, + { .compatible = "qcom,x1e80100-csi2-phy", .data = &mipi_csi2_dphy_4nm_x1e }, + { } +}; +MODULE_DEVICE_TABLE(of, phy_qcom_mipi_csi2_of_match_table); + +static struct platform_driver phy_qcom_mipi_csi2_driver = { + .probe = phy_qcom_mipi_csi2_probe, + .driver = { + .name = "qcom-mipi-csi2-phy", + .of_match_table = phy_qcom_mipi_csi2_of_match_table, + }, +}; + +module_platform_driver(phy_qcom_mipi_csi2_driver); + +MODULE_DESCRIPTION("Qualcomm MIPI CSI2 PHY driver"); +MODULE_AUTHOR("Bryan O'Donoghue "); +MODULE_LICENSE("GPL"); diff --git a/drivers/phy/qualcomm/phy-qcom-mipi-csi2.h b/drivers/phy/qualcomm/phy-qcom-mipi-csi2.h new file mode 100644 index 0000000000000..f220ca054159c --- /dev/null +++ b/drivers/phy/qualcomm/phy-qcom-mipi-csi2.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * + * Qualcomm MIPI CSI2 CPHY/DPHY driver + * + * Copyright (C) 2025 Linaro Ltd. + */ +#ifndef __PHY_QCOM_MIPI_CSI2_H__ +#define __PHY_QCOM_MIPI_CSI2_H__ + +#include + +#define CSI2_MAX_DATA_LANES 4 +#define CSI2_DEFAULT_CLK_LANE 7 + +struct mipi_csi2phy_lane { + u8 pos; + u8 pol; +}; + +struct mipi_csi2phy_lanes_cfg { + struct mipi_csi2phy_lane data[CSI2_MAX_DATA_LANES]; + struct mipi_csi2phy_lane clk; +}; + +struct mipi_csi2phy_stream_cfg { + s64 link_freq; + u8 num_data_lanes; + struct mipi_csi2phy_lanes_cfg lane_cfg; +}; + +struct mipi_csi2phy_device; + +struct mipi_csi2phy_hw_ops { + void (*hw_version_read)(struct mipi_csi2phy_device *csi2phy_dev); + void (*reset)(struct mipi_csi2phy_device *csi2phy_dev); + int (*lanes_enable)(struct mipi_csi2phy_device *csi2phy_dev, + struct mipi_csi2phy_stream_cfg *cfg); + void (*lanes_disable)(struct mipi_csi2phy_device *csi2phy_dev, + struct mipi_csi2phy_stream_cfg *cfg); +}; + +struct mipi_csi2phy_lane_regs { + const s32 reg_addr; + const s32 reg_data; + const u32 delay_us; + const u32 param_type; +}; + +struct mipi_csi2phy_device_regs { + const struct mipi_csi2phy_lane_regs *init_seq; + const int lane_array_size; + const u32 common_regs_offset; +}; + +struct mipi_csi2_genpd { + const char *name; + bool scaled; +}; + +struct mipi_csi2phy_soc_cfg { + const struct mipi_csi2phy_hw_ops *ops; + const struct mipi_csi2phy_device_regs reg_info; + + const char * const *supply_names; + const unsigned int num_supplies; + + const char * const *clk_names; + const unsigned int num_clk; + + /* 0 = derive timer rate from link_freq/4; non-zero = fixed rate */ + unsigned long timer_rate_hz; + + const struct mipi_csi2_genpd *genpds; + const unsigned int num_genpds; +}; + +struct mipi_csi2phy_device { + struct device *dev; + u8 phy_mode; + + struct phy *phy; + void __iomem *base; + + struct clk_bulk_data *clks; + struct clk *timer_clk; + u32 timer_clk_rate; + + struct regulator_bulk_data *supplies; + struct dev_pm_domain_list *pd_list; + + const struct mipi_csi2phy_soc_cfg *soc_cfg; + struct mipi_csi2phy_stream_cfg stream_cfg; + + u32 hw_version; + bool powered_on; +}; + +extern const struct mipi_csi2phy_soc_cfg mipi_csi2_dphy_sm8150; +extern const struct mipi_csi2phy_soc_cfg mipi_csi2_dphy_4nm_x1e; + +#endif /* __PHY_QCOM_MIPI_CSI2_H__ */