[PATCH RFC v2 2/4] phy: Add MIPI C-PHY configuration helpers

From: George Neacsu via B4 Relay

Date: Fri Oct 16 2026 - 06:58:02 EST


From: George Neacsu <geocausa@xxxxxxxxx>

A CSI-2 receiver knows the link frequency of its transmitter, and by the
V4L2 convention a C-PHY link frequency is half the symbol rate on each
trio. Add helpers so that the receiver and the PHY do not each open-code
that conversion, as phy_mipi_dphy_get_default_config_for_hsclk() does
for D-PHY:

- phy_mipi_cphy_get_default_config_for_link_freq() fills a
configuration from a link frequency and a trio count;
- phy_mipi_cphy_config_validate() checks for a symbol rate and one to
three trios;
- phy_mipi_cphy_link_freq() gives the link frequency of a configuration.

They are small enough to be inline, so no new Kconfig symbol is needed.

Suggested-by: Bryan O'Donoghue <bod@xxxxxxxxxx>
Suggested-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: George Neacsu <geocausa@xxxxxxxxx>
---
include/linux/phy/phy-mipi-cphy.h | 61 +++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+)

diff --git a/include/linux/phy/phy-mipi-cphy.h b/include/linux/phy/phy-mipi-cphy.h
index ef6af61de..f6746dcb0 100644
--- a/include/linux/phy/phy-mipi-cphy.h
+++ b/include/linux/phy/phy-mipi-cphy.h
@@ -2,6 +2,10 @@
#ifndef __PHY_MIPI_CPHY_H_
#define __PHY_MIPI_CPHY_H_

+#include <linux/errno.h>
+
+#define PHY_MIPI_CPHY_MAX_TRIOS 3
+
/**
* struct phy_configure_opts_mipi_cphy - MIPI C-PHY configuration set
*
@@ -24,4 +28,61 @@ struct phy_configure_opts_mipi_cphy {
unsigned char trios;
};

+/**
+ * phy_mipi_cphy_config_validate() - Validate a MIPI C-PHY configuration
+ * @cfg: configuration to check
+ *
+ * Return: 0 if @cfg has a symbol rate and one to three trios, -EINVAL
+ * otherwise.
+ */
+static inline int
+phy_mipi_cphy_config_validate(const struct phy_configure_opts_mipi_cphy *cfg)
+{
+ if (!cfg->symbol_rate)
+ return -EINVAL;
+
+ if (cfg->trios < 1 || cfg->trios > PHY_MIPI_CPHY_MAX_TRIOS)
+ return -EINVAL;
+
+ return 0;
+}
+
+/**
+ * phy_mipi_cphy_get_default_config_for_link_freq() - C-PHY configuration
+ * from a link frequency
+ * @link_freq: link frequency in Hz
+ * @trios: number of active trios
+ * @cfg: configuration to fill
+ *
+ * A C-PHY link frequency is half the symbol rate on each trio, the
+ * convention V4L2 uses for the link frequency of a C-PHY transmitter.
+ *
+ * Return: 0 on success, -EINVAL if the resulting configuration is invalid.
+ */
+static inline int
+phy_mipi_cphy_get_default_config_for_link_freq(unsigned long long link_freq,
+ unsigned int trios,
+ struct phy_configure_opts_mipi_cphy *cfg)
+{
+ if (trios > PHY_MIPI_CPHY_MAX_TRIOS)
+ return -EINVAL;
+
+ cfg->symbol_rate = link_freq * 2;
+ cfg->trios = trios;
+
+ return phy_mipi_cphy_config_validate(cfg);
+}
+
+/**
+ * phy_mipi_cphy_link_freq() - Link frequency of a MIPI C-PHY configuration
+ * @cfg: configuration
+ *
+ * Return: the link frequency in Hz, half the symbol rate on each trio.
+ */
+static inline unsigned long long
+phy_mipi_cphy_link_freq(const struct phy_configure_opts_mipi_cphy *cfg)
+{
+ return cfg->symbol_rate / 2;
+}
+
#endif /* __PHY_MIPI_CPHY_H_ */

--
2.53.0