Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Samsung CSIS MIPI CSI-2 receiver driver.
0004  *
0005  * The Samsung CSIS IP is a MIPI CSI-2 receiver found in various NXP i.MX7 and
0006  * i.MX8 SoCs. The i.MX7 features version 3.3 of the IP, while i.MX8 features
0007  * version 3.6.3.
0008  *
0009  * Copyright (C) 2019 Linaro Ltd
0010  * Copyright (C) 2015-2016 Freescale Semiconductor, Inc. All Rights Reserved.
0011  * Copyright (C) 2011 - 2013 Samsung Electronics Co., Ltd.
0012  *
0013  */
0014 
0015 #include <linux/clk.h>
0016 #include <linux/debugfs.h>
0017 #include <linux/delay.h>
0018 #include <linux/errno.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/io.h>
0021 #include <linux/kernel.h>
0022 #include <linux/module.h>
0023 #include <linux/mutex.h>
0024 #include <linux/of.h>
0025 #include <linux/of_device.h>
0026 #include <linux/platform_device.h>
0027 #include <linux/pm_runtime.h>
0028 #include <linux/regulator/consumer.h>
0029 #include <linux/reset.h>
0030 #include <linux/spinlock.h>
0031 
0032 #include <media/v4l2-common.h>
0033 #include <media/v4l2-device.h>
0034 #include <media/v4l2-fwnode.h>
0035 #include <media/v4l2-mc.h>
0036 #include <media/v4l2-subdev.h>
0037 
0038 #define CSIS_DRIVER_NAME            "imx-mipi-csis"
0039 
0040 #define CSIS_PAD_SINK               0
0041 #define CSIS_PAD_SOURCE             1
0042 #define CSIS_PADS_NUM               2
0043 
0044 #define MIPI_CSIS_DEF_PIX_WIDTH         640
0045 #define MIPI_CSIS_DEF_PIX_HEIGHT        480
0046 
0047 /* Register map definition */
0048 
0049 /* CSIS version */
0050 #define MIPI_CSIS_VERSION           0x00
0051 #define MIPI_CSIS_VERSION_IMX7D         0x03030505
0052 #define MIPI_CSIS_VERSION_IMX8MP        0x03060301
0053 
0054 /* CSIS common control */
0055 #define MIPI_CSIS_CMN_CTRL          0x04
0056 #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW    BIT(16)
0057 #define MIPI_CSIS_CMN_CTRL_INTER_MODE       BIT(10)
0058 #define MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL   BIT(2)
0059 #define MIPI_CSIS_CMN_CTRL_RESET        BIT(1)
0060 #define MIPI_CSIS_CMN_CTRL_ENABLE       BIT(0)
0061 
0062 #define MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET   8
0063 #define MIPI_CSIS_CMN_CTRL_LANE_NR_MASK     (3 << 8)
0064 
0065 /* CSIS clock control */
0066 #define MIPI_CSIS_CLK_CTRL          0x08
0067 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH3(x) ((x) << 28)
0068 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH2(x) ((x) << 24)
0069 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH1(x) ((x) << 20)
0070 #define MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(x) ((x) << 16)
0071 #define MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK   (0xf << 4)
0072 #define MIPI_CSIS_CLK_CTRL_WCLK_SRC     BIT(0)
0073 
0074 /* CSIS Interrupt mask */
0075 #define MIPI_CSIS_INT_MSK           0x10
0076 #define MIPI_CSIS_INT_MSK_EVEN_BEFORE       BIT(31)
0077 #define MIPI_CSIS_INT_MSK_EVEN_AFTER        BIT(30)
0078 #define MIPI_CSIS_INT_MSK_ODD_BEFORE        BIT(29)
0079 #define MIPI_CSIS_INT_MSK_ODD_AFTER     BIT(28)
0080 #define MIPI_CSIS_INT_MSK_FRAME_START       BIT(24)
0081 #define MIPI_CSIS_INT_MSK_FRAME_END     BIT(20)
0082 #define MIPI_CSIS_INT_MSK_ERR_SOT_HS        BIT(16)
0083 #define MIPI_CSIS_INT_MSK_ERR_LOST_FS       BIT(12)
0084 #define MIPI_CSIS_INT_MSK_ERR_LOST_FE       BIT(8)
0085 #define MIPI_CSIS_INT_MSK_ERR_OVER      BIT(4)
0086 #define MIPI_CSIS_INT_MSK_ERR_WRONG_CFG     BIT(3)
0087 #define MIPI_CSIS_INT_MSK_ERR_ECC       BIT(2)
0088 #define MIPI_CSIS_INT_MSK_ERR_CRC       BIT(1)
0089 #define MIPI_CSIS_INT_MSK_ERR_UNKNOWN       BIT(0)
0090 
0091 /* CSIS Interrupt source */
0092 #define MIPI_CSIS_INT_SRC           0x14
0093 #define MIPI_CSIS_INT_SRC_EVEN_BEFORE       BIT(31)
0094 #define MIPI_CSIS_INT_SRC_EVEN_AFTER        BIT(30)
0095 #define MIPI_CSIS_INT_SRC_EVEN          BIT(30)
0096 #define MIPI_CSIS_INT_SRC_ODD_BEFORE        BIT(29)
0097 #define MIPI_CSIS_INT_SRC_ODD_AFTER     BIT(28)
0098 #define MIPI_CSIS_INT_SRC_ODD           (0x3 << 28)
0099 #define MIPI_CSIS_INT_SRC_NON_IMAGE_DATA    (0xf << 28)
0100 #define MIPI_CSIS_INT_SRC_FRAME_START       BIT(24)
0101 #define MIPI_CSIS_INT_SRC_FRAME_END     BIT(20)
0102 #define MIPI_CSIS_INT_SRC_ERR_SOT_HS        BIT(16)
0103 #define MIPI_CSIS_INT_SRC_ERR_LOST_FS       BIT(12)
0104 #define MIPI_CSIS_INT_SRC_ERR_LOST_FE       BIT(8)
0105 #define MIPI_CSIS_INT_SRC_ERR_OVER      BIT(4)
0106 #define MIPI_CSIS_INT_SRC_ERR_WRONG_CFG     BIT(3)
0107 #define MIPI_CSIS_INT_SRC_ERR_ECC       BIT(2)
0108 #define MIPI_CSIS_INT_SRC_ERR_CRC       BIT(1)
0109 #define MIPI_CSIS_INT_SRC_ERR_UNKNOWN       BIT(0)
0110 #define MIPI_CSIS_INT_SRC_ERRORS        0xfffff
0111 
0112 /* D-PHY status control */
0113 #define MIPI_CSIS_DPHY_STATUS           0x20
0114 #define MIPI_CSIS_DPHY_STATUS_ULPS_DAT      BIT(8)
0115 #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_DAT BIT(4)
0116 #define MIPI_CSIS_DPHY_STATUS_ULPS_CLK      BIT(1)
0117 #define MIPI_CSIS_DPHY_STATUS_STOPSTATE_CLK BIT(0)
0118 
0119 /* D-PHY common control */
0120 #define MIPI_CSIS_DPHY_CMN_CTRL         0x24
0121 #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(n) ((n) << 24)
0122 #define MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE_MASK   GENMASK(31, 24)
0123 #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(n)    ((n) << 22)
0124 #define MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE_MASK  GENMASK(23, 22)
0125 #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_CLK   BIT(6)
0126 #define MIPI_CSIS_DPHY_CMN_CTRL_DPDN_SWAP_DAT   BIT(5)
0127 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_DAT  BIT(1)
0128 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE_CLK  BIT(0)
0129 #define MIPI_CSIS_DPHY_CMN_CTRL_ENABLE      (0x1f << 0)
0130 
0131 /* D-PHY Master and Slave Control register Low */
0132 #define MIPI_CSIS_DPHY_BCTRL_L          0x30
0133 #define MIPI_CSIS_DPHY_BCTRL_L_USER_DATA_PATTERN_LOW(n)     (((n) & 3U) << 30)
0134 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV      (0 << 28)
0135 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_724MV      (1 << 28)
0136 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_733MV      (2 << 28)
0137 #define MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_706MV      (3 << 28)
0138 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ        (0 << 27)
0139 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_1_5MHZ      (1 << 27)
0140 #define MIPI_CSIS_DPHY_BCTRL_L_VREG12_EXTPWR_EN_CTL     BIT(26)
0141 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V     (0 << 24)
0142 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_23V        (1 << 24)
0143 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_17V        (2 << 24)
0144 #define MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_26V        (3 << 24)
0145 #define MIPI_CSIS_DPHY_BCTRL_L_REG_1P2_LVL_SEL          BIT(23)
0146 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV       (0 << 21)
0147 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_100MV      (1 << 21)
0148 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_120MV      (2 << 21)
0149 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_140MV      (3 << 21)
0150 #define MIPI_CSIS_DPHY_BCTRL_L_VREF_SRC_SEL         BIT(20)
0151 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV     (0 << 18)
0152 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_743MV     (1 << 18)
0153 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_650MV     (2 << 18)
0154 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_682MV     (3 << 18)
0155 #define MIPI_CSIS_DPHY_BCTRL_L_LP_RX_PULSE_REJECT       BIT(17)
0156 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_0  (0 << 15)
0157 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_15P    (1 << 15)
0158 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_DOWN_30P    (3 << 15)
0159 #define MIPI_CSIS_DPHY_BCTRL_L_MSTRCLK_LP_SLEW_RATE_UP      BIT(14)
0160 #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV           (0 << 13)
0161 #define MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_70MV           (1 << 13)
0162 #define MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_EN           BIT(12)
0163 #define MIPI_CSIS_DPHY_BCTRL_L_ERRCONTENTION_LP_EN      BIT(11)
0164 #define MIPI_CSIS_DPHY_BCTRL_L_TXTRIGGER_CLK_EN         BIT(10)
0165 #define MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(n)            (((n) * 25 / 1000000) << 0)
0166 
0167 /* D-PHY Master and Slave Control register High */
0168 #define MIPI_CSIS_DPHY_BCTRL_H          0x34
0169 /* D-PHY Slave Control register Low */
0170 #define MIPI_CSIS_DPHY_SCTRL_L          0x38
0171 /* D-PHY Slave Control register High */
0172 #define MIPI_CSIS_DPHY_SCTRL_H          0x3c
0173 
0174 /* ISP Configuration register */
0175 #define MIPI_CSIS_ISP_CONFIG_CH(n)      (0x40 + (n) * 0x10)
0176 #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP_MSK   (0xff << 24)
0177 #define MIPI_CSIS_ISPCFG_MEM_FULL_GAP(x)    ((x) << 24)
0178 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_SINGLE  (0 << 12)
0179 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL    (1 << 12)
0180 #define MIPI_CSIS_ISPCFG_PIXEL_MODE_QUAD    (2 << 12)   /* i.MX8M[MNP] only */
0181 #define MIPI_CSIS_ISPCFG_PIXEL_MASK     (3 << 12)
0182 #define MIPI_CSIS_ISPCFG_ALIGN_32BIT        BIT(11)
0183 #define MIPI_CSIS_ISPCFG_FMT(fmt)       ((fmt) << 2)
0184 #define MIPI_CSIS_ISPCFG_FMT_MASK       (0x3f << 2)
0185 
0186 /* ISP Image Resolution register */
0187 #define MIPI_CSIS_ISP_RESOL_CH(n)       (0x44 + (n) * 0x10)
0188 #define CSIS_MAX_PIX_WIDTH          0xffff
0189 #define CSIS_MAX_PIX_HEIGHT         0xffff
0190 
0191 /* ISP SYNC register */
0192 #define MIPI_CSIS_ISP_SYNC_CH(n)        (0x48 + (n) * 0x10)
0193 #define MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET   18
0194 #define MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET   12
0195 #define MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET   0
0196 
0197 /* ISP shadow registers */
0198 #define MIPI_CSIS_SDW_CONFIG_CH(n)      (0x80 + (n) * 0x10)
0199 #define MIPI_CSIS_SDW_RESOL_CH(n)       (0x84 + (n) * 0x10)
0200 #define MIPI_CSIS_SDW_SYNC_CH(n)        (0x88 + (n) * 0x10)
0201 
0202 /* Debug control register */
0203 #define MIPI_CSIS_DBG_CTRL          0xc0
0204 #define MIPI_CSIS_DBG_INTR_MSK          0xc4
0205 #define MIPI_CSIS_DBG_INTR_MSK_DT_NOT_SUPPORT   BIT(25)
0206 #define MIPI_CSIS_DBG_INTR_MSK_DT_IGNORE    BIT(24)
0207 #define MIPI_CSIS_DBG_INTR_MSK_ERR_FRAME_SIZE   BIT(20)
0208 #define MIPI_CSIS_DBG_INTR_MSK_TRUNCATED_FRAME  BIT(16)
0209 #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FE     BIT(12)
0210 #define MIPI_CSIS_DBG_INTR_MSK_EARLY_FS     BIT(8)
0211 #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_FALL   BIT(4)
0212 #define MIPI_CSIS_DBG_INTR_MSK_CAM_VSYNC_RISE   BIT(0)
0213 #define MIPI_CSIS_DBG_INTR_SRC          0xc8
0214 #define MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT   BIT(25)
0215 #define MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE    BIT(24)
0216 #define MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE   BIT(20)
0217 #define MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME  BIT(16)
0218 #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FE     BIT(12)
0219 #define MIPI_CSIS_DBG_INTR_SRC_EARLY_FS     BIT(8)
0220 #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL   BIT(4)
0221 #define MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE   BIT(0)
0222 
0223 #define MIPI_CSIS_FRAME_COUNTER_CH(n)       (0x0100 + (n) * 4)
0224 
0225 /* Non-image packet data buffers */
0226 #define MIPI_CSIS_PKTDATA_ODD           0x2000
0227 #define MIPI_CSIS_PKTDATA_EVEN          0x3000
0228 #define MIPI_CSIS_PKTDATA_SIZE          SZ_4K
0229 
0230 #define DEFAULT_SCLK_CSIS_FREQ          166000000UL
0231 
0232 /* MIPI CSI-2 Data Types */
0233 #define MIPI_CSI2_DATA_TYPE_YUV420_8        0x18
0234 #define MIPI_CSI2_DATA_TYPE_YUV420_10       0x19
0235 #define MIPI_CSI2_DATA_TYPE_LE_YUV420_8     0x1a
0236 #define MIPI_CSI2_DATA_TYPE_CS_YUV420_8     0x1c
0237 #define MIPI_CSI2_DATA_TYPE_CS_YUV420_10    0x1d
0238 #define MIPI_CSI2_DATA_TYPE_YUV422_8        0x1e
0239 #define MIPI_CSI2_DATA_TYPE_YUV422_10       0x1f
0240 #define MIPI_CSI2_DATA_TYPE_RGB565      0x22
0241 #define MIPI_CSI2_DATA_TYPE_RGB666      0x23
0242 #define MIPI_CSI2_DATA_TYPE_RGB888      0x24
0243 #define MIPI_CSI2_DATA_TYPE_RAW6        0x28
0244 #define MIPI_CSI2_DATA_TYPE_RAW7        0x29
0245 #define MIPI_CSI2_DATA_TYPE_RAW8        0x2a
0246 #define MIPI_CSI2_DATA_TYPE_RAW10       0x2b
0247 #define MIPI_CSI2_DATA_TYPE_RAW12       0x2c
0248 #define MIPI_CSI2_DATA_TYPE_RAW14       0x2d
0249 #define MIPI_CSI2_DATA_TYPE_USER(x)     (0x30 + (x))
0250 
0251 struct mipi_csis_event {
0252     bool debug;
0253     u32 mask;
0254     const char * const name;
0255     unsigned int counter;
0256 };
0257 
0258 static const struct mipi_csis_event mipi_csis_events[] = {
0259     /* Errors */
0260     { false, MIPI_CSIS_INT_SRC_ERR_SOT_HS,      "SOT Error" },
0261     { false, MIPI_CSIS_INT_SRC_ERR_LOST_FS,     "Lost Frame Start Error" },
0262     { false, MIPI_CSIS_INT_SRC_ERR_LOST_FE,     "Lost Frame End Error" },
0263     { false, MIPI_CSIS_INT_SRC_ERR_OVER,        "FIFO Overflow Error" },
0264     { false, MIPI_CSIS_INT_SRC_ERR_WRONG_CFG,   "Wrong Configuration Error" },
0265     { false, MIPI_CSIS_INT_SRC_ERR_ECC,     "ECC Error" },
0266     { false, MIPI_CSIS_INT_SRC_ERR_CRC,     "CRC Error" },
0267     { false, MIPI_CSIS_INT_SRC_ERR_UNKNOWN,     "Unknown Error" },
0268     { true, MIPI_CSIS_DBG_INTR_SRC_DT_NOT_SUPPORT,  "Data Type Not Supported" },
0269     { true, MIPI_CSIS_DBG_INTR_SRC_DT_IGNORE,   "Data Type Ignored" },
0270     { true, MIPI_CSIS_DBG_INTR_SRC_ERR_FRAME_SIZE,  "Frame Size Error" },
0271     { true, MIPI_CSIS_DBG_INTR_SRC_TRUNCATED_FRAME, "Truncated Frame" },
0272     { true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FE,    "Early Frame End" },
0273     { true, MIPI_CSIS_DBG_INTR_SRC_EARLY_FS,    "Early Frame Start" },
0274     /* Non-image data receive events */
0275     { false, MIPI_CSIS_INT_SRC_EVEN_BEFORE,     "Non-image data before even frame" },
0276     { false, MIPI_CSIS_INT_SRC_EVEN_AFTER,      "Non-image data after even frame" },
0277     { false, MIPI_CSIS_INT_SRC_ODD_BEFORE,      "Non-image data before odd frame" },
0278     { false, MIPI_CSIS_INT_SRC_ODD_AFTER,       "Non-image data after odd frame" },
0279     /* Frame start/end */
0280     { false, MIPI_CSIS_INT_SRC_FRAME_START,     "Frame Start" },
0281     { false, MIPI_CSIS_INT_SRC_FRAME_END,       "Frame End" },
0282     { true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_FALL,  "VSYNC Falling Edge" },
0283     { true, MIPI_CSIS_DBG_INTR_SRC_CAM_VSYNC_RISE,  "VSYNC Rising Edge" },
0284 };
0285 
0286 #define MIPI_CSIS_NUM_EVENTS ARRAY_SIZE(mipi_csis_events)
0287 
0288 enum mipi_csis_clk {
0289     MIPI_CSIS_CLK_PCLK,
0290     MIPI_CSIS_CLK_WRAP,
0291     MIPI_CSIS_CLK_PHY,
0292     MIPI_CSIS_CLK_AXI,
0293 };
0294 
0295 static const char * const mipi_csis_clk_id[] = {
0296     "pclk",
0297     "wrap",
0298     "phy",
0299     "axi",
0300 };
0301 
0302 enum mipi_csis_version {
0303     MIPI_CSIS_V3_3,
0304     MIPI_CSIS_V3_6_3,
0305 };
0306 
0307 struct mipi_csis_info {
0308     enum mipi_csis_version version;
0309     unsigned int num_clocks;
0310 };
0311 
0312 struct mipi_csis_device {
0313     struct device *dev;
0314     void __iomem *regs;
0315     struct clk_bulk_data *clks;
0316     struct reset_control *mrst;
0317     struct regulator *mipi_phy_regulator;
0318     const struct mipi_csis_info *info;
0319 
0320     struct v4l2_subdev sd;
0321     struct media_pad pads[CSIS_PADS_NUM];
0322     struct v4l2_async_notifier notifier;
0323     struct v4l2_subdev *src_sd;
0324 
0325     struct v4l2_mbus_config_mipi_csi2 bus;
0326     u32 clk_frequency;
0327     u32 hs_settle;
0328     u32 clk_settle;
0329 
0330     struct mutex lock;  /* Protect csis_fmt and format_mbus */
0331     const struct csis_pix_format *csis_fmt;
0332     struct v4l2_mbus_framefmt format_mbus[CSIS_PADS_NUM];
0333 
0334     spinlock_t slock;   /* Protect events */
0335     struct mipi_csis_event events[MIPI_CSIS_NUM_EVENTS];
0336     struct dentry *debugfs_root;
0337     struct {
0338         bool enable;
0339         u32 hs_settle;
0340         u32 clk_settle;
0341     } debug;
0342 };
0343 
0344 /* -----------------------------------------------------------------------------
0345  * Format helpers
0346  */
0347 
0348 struct csis_pix_format {
0349     u32 code;
0350     u32 output;
0351     u32 data_type;
0352     u8 width;
0353 };
0354 
0355 static const struct csis_pix_format mipi_csis_formats[] = {
0356     /* YUV formats. */
0357     {
0358         .code = MEDIA_BUS_FMT_UYVY8_1X16,
0359         .output = MEDIA_BUS_FMT_UYVY8_1X16,
0360         .data_type = MIPI_CSI2_DATA_TYPE_YUV422_8,
0361         .width = 16,
0362     },
0363     /* RGB formats. */
0364     {
0365         .code = MEDIA_BUS_FMT_RGB565_1X16,
0366         .output = MEDIA_BUS_FMT_RGB565_1X16,
0367         .data_type = MIPI_CSI2_DATA_TYPE_RGB565,
0368         .width = 16,
0369     }, {
0370         .code = MEDIA_BUS_FMT_BGR888_1X24,
0371         .output = MEDIA_BUS_FMT_RGB888_1X24,
0372         .data_type = MIPI_CSI2_DATA_TYPE_RGB888,
0373         .width = 24,
0374     },
0375     /* RAW (Bayer and greyscale) formats. */
0376     {
0377         .code = MEDIA_BUS_FMT_SBGGR8_1X8,
0378         .output = MEDIA_BUS_FMT_SBGGR8_1X8,
0379         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0380         .width = 8,
0381     }, {
0382         .code = MEDIA_BUS_FMT_SGBRG8_1X8,
0383         .output = MEDIA_BUS_FMT_SGBRG8_1X8,
0384         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0385         .width = 8,
0386     }, {
0387         .code = MEDIA_BUS_FMT_SGRBG8_1X8,
0388         .output = MEDIA_BUS_FMT_SGRBG8_1X8,
0389         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0390         .width = 8,
0391     }, {
0392         .code = MEDIA_BUS_FMT_SRGGB8_1X8,
0393         .output = MEDIA_BUS_FMT_SRGGB8_1X8,
0394         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0395         .width = 8,
0396     }, {
0397         .code = MEDIA_BUS_FMT_Y8_1X8,
0398         .output = MEDIA_BUS_FMT_Y8_1X8,
0399         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0400         .width = 8,
0401     }, {
0402         .code = MEDIA_BUS_FMT_SBGGR10_1X10,
0403         .output = MEDIA_BUS_FMT_SBGGR10_1X10,
0404         .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
0405         .width = 10,
0406     }, {
0407         .code = MEDIA_BUS_FMT_SGBRG10_1X10,
0408         .output = MEDIA_BUS_FMT_SGBRG10_1X10,
0409         .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
0410         .width = 10,
0411     }, {
0412         .code = MEDIA_BUS_FMT_SGRBG10_1X10,
0413         .output = MEDIA_BUS_FMT_SGRBG10_1X10,
0414         .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
0415         .width = 10,
0416     }, {
0417         .code = MEDIA_BUS_FMT_SRGGB10_1X10,
0418         .output = MEDIA_BUS_FMT_SRGGB10_1X10,
0419         .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
0420         .width = 10,
0421     }, {
0422         .code = MEDIA_BUS_FMT_Y10_1X10,
0423         .output = MEDIA_BUS_FMT_Y10_1X10,
0424         .data_type = MIPI_CSI2_DATA_TYPE_RAW10,
0425         .width = 10,
0426     }, {
0427         .code = MEDIA_BUS_FMT_SBGGR12_1X12,
0428         .output = MEDIA_BUS_FMT_SBGGR12_1X12,
0429         .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
0430         .width = 12,
0431     }, {
0432         .code = MEDIA_BUS_FMT_SGBRG12_1X12,
0433         .output = MEDIA_BUS_FMT_SGBRG12_1X12,
0434         .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
0435         .width = 12,
0436     }, {
0437         .code = MEDIA_BUS_FMT_SGRBG12_1X12,
0438         .output = MEDIA_BUS_FMT_SGRBG12_1X12,
0439         .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
0440         .width = 12,
0441     }, {
0442         .code = MEDIA_BUS_FMT_SRGGB12_1X12,
0443         .output = MEDIA_BUS_FMT_SRGGB12_1X12,
0444         .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
0445         .width = 12,
0446     }, {
0447         .code = MEDIA_BUS_FMT_Y12_1X12,
0448         .output = MEDIA_BUS_FMT_Y12_1X12,
0449         .data_type = MIPI_CSI2_DATA_TYPE_RAW12,
0450         .width = 12,
0451     }, {
0452         .code = MEDIA_BUS_FMT_SBGGR14_1X14,
0453         .output = MEDIA_BUS_FMT_SBGGR14_1X14,
0454         .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
0455         .width = 14,
0456     }, {
0457         .code = MEDIA_BUS_FMT_SGBRG14_1X14,
0458         .output = MEDIA_BUS_FMT_SGBRG14_1X14,
0459         .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
0460         .width = 14,
0461     }, {
0462         .code = MEDIA_BUS_FMT_SGRBG14_1X14,
0463         .output = MEDIA_BUS_FMT_SGRBG14_1X14,
0464         .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
0465         .width = 14,
0466     }, {
0467         .code = MEDIA_BUS_FMT_SRGGB14_1X14,
0468         .output = MEDIA_BUS_FMT_SRGGB14_1X14,
0469         .data_type = MIPI_CSI2_DATA_TYPE_RAW14,
0470         .width = 14,
0471     },
0472     /* JPEG */
0473     {
0474         .code = MEDIA_BUS_FMT_JPEG_1X8,
0475         .output = MEDIA_BUS_FMT_JPEG_1X8,
0476         /*
0477          * Map JPEG_1X8 to the RAW8 datatype.
0478          *
0479          * The CSI-2 specification suggests in Annex A "JPEG8 Data
0480          * Format (informative)" to transmit JPEG data using one of the
0481          * Data Types aimed to represent arbitrary data, such as the
0482          * "User Defined Data Type 1" (0x30).
0483          *
0484          * However, when configured with a User Defined Data Type, the
0485          * CSIS outputs data in quad pixel mode regardless of the mode
0486          * selected in the MIPI_CSIS_ISP_CONFIG_CH register. Neither of
0487          * the IP cores connected to the CSIS in i.MX SoCs (CSI bridge
0488          * or ISI) support quad pixel mode, so this will never work in
0489          * practice.
0490          *
0491          * Some sensors (such as the OV5640) send JPEG data using the
0492          * RAW8 data type. This is usable and works, so map the JPEG
0493          * format to RAW8. If the CSIS ends up being integrated in an
0494          * SoC that can support quad pixel mode, this will have to be
0495          * revisited.
0496          */
0497         .data_type = MIPI_CSI2_DATA_TYPE_RAW8,
0498         .width = 8,
0499     }
0500 };
0501 
0502 static const struct csis_pix_format *find_csis_format(u32 code)
0503 {
0504     unsigned int i;
0505 
0506     for (i = 0; i < ARRAY_SIZE(mipi_csis_formats); i++)
0507         if (code == mipi_csis_formats[i].code)
0508             return &mipi_csis_formats[i];
0509     return NULL;
0510 }
0511 
0512 /* -----------------------------------------------------------------------------
0513  * Hardware configuration
0514  */
0515 
0516 static inline u32 mipi_csis_read(struct mipi_csis_device *csis, u32 reg)
0517 {
0518     return readl(csis->regs + reg);
0519 }
0520 
0521 static inline void mipi_csis_write(struct mipi_csis_device *csis, u32 reg,
0522                    u32 val)
0523 {
0524     writel(val, csis->regs + reg);
0525 }
0526 
0527 static void mipi_csis_enable_interrupts(struct mipi_csis_device *csis, bool on)
0528 {
0529     mipi_csis_write(csis, MIPI_CSIS_INT_MSK, on ? 0xffffffff : 0);
0530     mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_MSK, on ? 0xffffffff : 0);
0531 }
0532 
0533 static void mipi_csis_sw_reset(struct mipi_csis_device *csis)
0534 {
0535     u32 val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
0536 
0537     mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
0538             val | MIPI_CSIS_CMN_CTRL_RESET);
0539     usleep_range(10, 20);
0540 }
0541 
0542 static void mipi_csis_system_enable(struct mipi_csis_device *csis, int on)
0543 {
0544     u32 val, mask;
0545 
0546     val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
0547     if (on)
0548         val |= MIPI_CSIS_CMN_CTRL_ENABLE;
0549     else
0550         val &= ~MIPI_CSIS_CMN_CTRL_ENABLE;
0551     mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
0552 
0553     val = mipi_csis_read(csis, MIPI_CSIS_DPHY_CMN_CTRL);
0554     val &= ~MIPI_CSIS_DPHY_CMN_CTRL_ENABLE;
0555     if (on) {
0556         mask = (1 << (csis->bus.num_data_lanes + 1)) - 1;
0557         val |= (mask & MIPI_CSIS_DPHY_CMN_CTRL_ENABLE);
0558     }
0559     mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL, val);
0560 }
0561 
0562 /* Called with the csis.lock mutex held */
0563 static void __mipi_csis_set_format(struct mipi_csis_device *csis)
0564 {
0565     struct v4l2_mbus_framefmt *mf = &csis->format_mbus[CSIS_PAD_SINK];
0566     u32 val;
0567 
0568     /* Color format */
0569     val = mipi_csis_read(csis, MIPI_CSIS_ISP_CONFIG_CH(0));
0570     val &= ~(MIPI_CSIS_ISPCFG_ALIGN_32BIT | MIPI_CSIS_ISPCFG_FMT_MASK
0571         | MIPI_CSIS_ISPCFG_PIXEL_MASK);
0572 
0573     /*
0574      * YUV 4:2:2 can be transferred with 8 or 16 bits per clock sample
0575      * (referred to in the documentation as single and dual pixel modes
0576      * respectively, although the 8-bit mode transfers half a pixel per
0577      * clock sample and the 16-bit mode one pixel). While both mode work
0578      * when the CSIS is connected to a receiver that supports either option,
0579      * single pixel mode requires clock rates twice as high. As all SoCs
0580      * that integrate the CSIS can operate in 16-bit bit mode, and some do
0581      * not support 8-bit mode (this is the case of the i.MX8MP), use dual
0582      * pixel mode unconditionally.
0583      *
0584      * TODO: Verify which other formats require DUAL (or QUAD) modes.
0585      */
0586     if (csis->csis_fmt->data_type == MIPI_CSI2_DATA_TYPE_YUV422_8)
0587         val |= MIPI_CSIS_ISPCFG_PIXEL_MODE_DUAL;
0588 
0589     val |= MIPI_CSIS_ISPCFG_FMT(csis->csis_fmt->data_type);
0590     mipi_csis_write(csis, MIPI_CSIS_ISP_CONFIG_CH(0), val);
0591 
0592     /* Pixel resolution */
0593     val = mf->width | (mf->height << 16);
0594     mipi_csis_write(csis, MIPI_CSIS_ISP_RESOL_CH(0), val);
0595 }
0596 
0597 static int mipi_csis_calculate_params(struct mipi_csis_device *csis)
0598 {
0599     s64 link_freq;
0600     u32 lane_rate;
0601 
0602     /* Calculate the line rate from the pixel rate. */
0603     link_freq = v4l2_get_link_freq(csis->src_sd->ctrl_handler,
0604                        csis->csis_fmt->width,
0605                        csis->bus.num_data_lanes * 2);
0606     if (link_freq < 0) {
0607         dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
0608             (int)link_freq);
0609         return link_freq;
0610     }
0611 
0612     lane_rate = link_freq * 2;
0613 
0614     if (lane_rate < 80000000 || lane_rate > 1500000000) {
0615         dev_dbg(csis->dev, "Out-of-bound lane rate %u\n", lane_rate);
0616         return -EINVAL;
0617     }
0618 
0619     /*
0620      * The HSSETTLE counter value is document in a table, but can also
0621      * easily be calculated. Hardcode the CLKSETTLE value to 0 for now
0622      * (which is documented as corresponding to CSI-2 v0.87 to v1.00) until
0623      * we figure out how to compute it correctly.
0624      */
0625     csis->hs_settle = (lane_rate - 5000000) / 45000000;
0626     csis->clk_settle = 0;
0627 
0628     dev_dbg(csis->dev, "lane rate %u, Tclk_settle %u, Ths_settle %u\n",
0629         lane_rate, csis->clk_settle, csis->hs_settle);
0630 
0631     if (csis->debug.hs_settle < 0xff) {
0632         dev_dbg(csis->dev, "overriding Ths_settle with %u\n",
0633             csis->debug.hs_settle);
0634         csis->hs_settle = csis->debug.hs_settle;
0635     }
0636 
0637     if (csis->debug.clk_settle < 4) {
0638         dev_dbg(csis->dev, "overriding Tclk_settle with %u\n",
0639             csis->debug.clk_settle);
0640         csis->clk_settle = csis->debug.clk_settle;
0641     }
0642 
0643     return 0;
0644 }
0645 
0646 static void mipi_csis_set_params(struct mipi_csis_device *csis)
0647 {
0648     int lanes = csis->bus.num_data_lanes;
0649     u32 val;
0650 
0651     val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
0652     val &= ~MIPI_CSIS_CMN_CTRL_LANE_NR_MASK;
0653     val |= (lanes - 1) << MIPI_CSIS_CMN_CTRL_LANE_NR_OFFSET;
0654     if (csis->info->version == MIPI_CSIS_V3_3)
0655         val |= MIPI_CSIS_CMN_CTRL_INTER_MODE;
0656     mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL, val);
0657 
0658     __mipi_csis_set_format(csis);
0659 
0660     mipi_csis_write(csis, MIPI_CSIS_DPHY_CMN_CTRL,
0661             MIPI_CSIS_DPHY_CMN_CTRL_HSSETTLE(csis->hs_settle) |
0662             MIPI_CSIS_DPHY_CMN_CTRL_CLKSETTLE(csis->clk_settle));
0663 
0664     val = (0 << MIPI_CSIS_ISP_SYNC_HSYNC_LINTV_OFFSET)
0665         | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_SINTV_OFFSET)
0666         | (0 << MIPI_CSIS_ISP_SYNC_VSYNC_EINTV_OFFSET);
0667     mipi_csis_write(csis, MIPI_CSIS_ISP_SYNC_CH(0), val);
0668 
0669     val = mipi_csis_read(csis, MIPI_CSIS_CLK_CTRL);
0670     val |= MIPI_CSIS_CLK_CTRL_WCLK_SRC;
0671     val |= MIPI_CSIS_CLK_CTRL_CLKGATE_TRAIL_CH0(15);
0672     val &= ~MIPI_CSIS_CLK_CTRL_CLKGATE_EN_MSK;
0673     mipi_csis_write(csis, MIPI_CSIS_CLK_CTRL, val);
0674 
0675     mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_L,
0676             MIPI_CSIS_DPHY_BCTRL_L_BIAS_REF_VOLT_715MV |
0677             MIPI_CSIS_DPHY_BCTRL_L_BGR_CHOPPER_FREQ_3MHZ |
0678             MIPI_CSIS_DPHY_BCTRL_L_REG_12P_LVL_CTL_1_2V |
0679             MIPI_CSIS_DPHY_BCTRL_L_LP_RX_HYS_LVL_80MV |
0680             MIPI_CSIS_DPHY_BCTRL_L_LP_RX_VREF_LVL_715MV |
0681             MIPI_CSIS_DPHY_BCTRL_L_LP_CD_HYS_60MV |
0682             MIPI_CSIS_DPHY_BCTRL_L_B_DPHYCTRL(20000000));
0683     mipi_csis_write(csis, MIPI_CSIS_DPHY_BCTRL_H, 0);
0684 
0685     /* Update the shadow register. */
0686     val = mipi_csis_read(csis, MIPI_CSIS_CMN_CTRL);
0687     mipi_csis_write(csis, MIPI_CSIS_CMN_CTRL,
0688             val | MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW |
0689             MIPI_CSIS_CMN_CTRL_UPDATE_SHADOW_CTRL);
0690 }
0691 
0692 static int mipi_csis_clk_enable(struct mipi_csis_device *csis)
0693 {
0694     return clk_bulk_prepare_enable(csis->info->num_clocks, csis->clks);
0695 }
0696 
0697 static void mipi_csis_clk_disable(struct mipi_csis_device *csis)
0698 {
0699     clk_bulk_disable_unprepare(csis->info->num_clocks, csis->clks);
0700 }
0701 
0702 static int mipi_csis_clk_get(struct mipi_csis_device *csis)
0703 {
0704     unsigned int i;
0705     int ret;
0706 
0707     csis->clks = devm_kcalloc(csis->dev, csis->info->num_clocks,
0708                   sizeof(*csis->clks), GFP_KERNEL);
0709 
0710     if (!csis->clks)
0711         return -ENOMEM;
0712 
0713     for (i = 0; i < csis->info->num_clocks; i++)
0714         csis->clks[i].id = mipi_csis_clk_id[i];
0715 
0716     ret = devm_clk_bulk_get(csis->dev, csis->info->num_clocks,
0717                 csis->clks);
0718     if (ret < 0)
0719         return ret;
0720 
0721     /* Set clock rate */
0722     ret = clk_set_rate(csis->clks[MIPI_CSIS_CLK_WRAP].clk,
0723                csis->clk_frequency);
0724     if (ret < 0)
0725         dev_err(csis->dev, "set rate=%d failed: %d\n",
0726             csis->clk_frequency, ret);
0727 
0728     return ret;
0729 }
0730 
0731 static void mipi_csis_start_stream(struct mipi_csis_device *csis)
0732 {
0733     mipi_csis_sw_reset(csis);
0734     mipi_csis_set_params(csis);
0735     mipi_csis_system_enable(csis, true);
0736     mipi_csis_enable_interrupts(csis, true);
0737 }
0738 
0739 static void mipi_csis_stop_stream(struct mipi_csis_device *csis)
0740 {
0741     mipi_csis_enable_interrupts(csis, false);
0742     mipi_csis_system_enable(csis, false);
0743 }
0744 
0745 static irqreturn_t mipi_csis_irq_handler(int irq, void *dev_id)
0746 {
0747     struct mipi_csis_device *csis = dev_id;
0748     unsigned long flags;
0749     unsigned int i;
0750     u32 status;
0751     u32 dbg_status;
0752 
0753     status = mipi_csis_read(csis, MIPI_CSIS_INT_SRC);
0754     dbg_status = mipi_csis_read(csis, MIPI_CSIS_DBG_INTR_SRC);
0755 
0756     spin_lock_irqsave(&csis->slock, flags);
0757 
0758     /* Update the event/error counters */
0759     if ((status & MIPI_CSIS_INT_SRC_ERRORS) || csis->debug.enable) {
0760         for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++) {
0761             struct mipi_csis_event *event = &csis->events[i];
0762 
0763             if ((!event->debug && (status & event->mask)) ||
0764                 (event->debug && (dbg_status & event->mask)))
0765                 event->counter++;
0766         }
0767     }
0768     spin_unlock_irqrestore(&csis->slock, flags);
0769 
0770     mipi_csis_write(csis, MIPI_CSIS_INT_SRC, status);
0771     mipi_csis_write(csis, MIPI_CSIS_DBG_INTR_SRC, dbg_status);
0772 
0773     return IRQ_HANDLED;
0774 }
0775 
0776 /* -----------------------------------------------------------------------------
0777  * PHY regulator and reset
0778  */
0779 
0780 static int mipi_csis_phy_enable(struct mipi_csis_device *csis)
0781 {
0782     if (csis->info->version != MIPI_CSIS_V3_3)
0783         return 0;
0784 
0785     return regulator_enable(csis->mipi_phy_regulator);
0786 }
0787 
0788 static int mipi_csis_phy_disable(struct mipi_csis_device *csis)
0789 {
0790     if (csis->info->version != MIPI_CSIS_V3_3)
0791         return 0;
0792 
0793     return regulator_disable(csis->mipi_phy_regulator);
0794 }
0795 
0796 static void mipi_csis_phy_reset(struct mipi_csis_device *csis)
0797 {
0798     if (csis->info->version != MIPI_CSIS_V3_3)
0799         return;
0800 
0801     reset_control_assert(csis->mrst);
0802     msleep(20);
0803     reset_control_deassert(csis->mrst);
0804 }
0805 
0806 static int mipi_csis_phy_init(struct mipi_csis_device *csis)
0807 {
0808     if (csis->info->version != MIPI_CSIS_V3_3)
0809         return 0;
0810 
0811     /* Get MIPI PHY reset and regulator. */
0812     csis->mrst = devm_reset_control_get_exclusive(csis->dev, NULL);
0813     if (IS_ERR(csis->mrst))
0814         return PTR_ERR(csis->mrst);
0815 
0816     csis->mipi_phy_regulator = devm_regulator_get(csis->dev, "phy");
0817     if (IS_ERR(csis->mipi_phy_regulator))
0818         return PTR_ERR(csis->mipi_phy_regulator);
0819 
0820     return regulator_set_voltage(csis->mipi_phy_regulator, 1000000,
0821                      1000000);
0822 }
0823 
0824 /* -----------------------------------------------------------------------------
0825  * Debug
0826  */
0827 
0828 static void mipi_csis_clear_counters(struct mipi_csis_device *csis)
0829 {
0830     unsigned long flags;
0831     unsigned int i;
0832 
0833     spin_lock_irqsave(&csis->slock, flags);
0834     for (i = 0; i < MIPI_CSIS_NUM_EVENTS; i++)
0835         csis->events[i].counter = 0;
0836     spin_unlock_irqrestore(&csis->slock, flags);
0837 }
0838 
0839 static void mipi_csis_log_counters(struct mipi_csis_device *csis, bool non_errors)
0840 {
0841     unsigned int num_events = non_errors ? MIPI_CSIS_NUM_EVENTS
0842                 : MIPI_CSIS_NUM_EVENTS - 8;
0843     unsigned long flags;
0844     unsigned int i;
0845 
0846     spin_lock_irqsave(&csis->slock, flags);
0847 
0848     for (i = 0; i < num_events; ++i) {
0849         if (csis->events[i].counter > 0 || csis->debug.enable)
0850             dev_info(csis->dev, "%s events: %d\n",
0851                  csis->events[i].name,
0852                  csis->events[i].counter);
0853     }
0854     spin_unlock_irqrestore(&csis->slock, flags);
0855 }
0856 
0857 static int mipi_csis_dump_regs(struct mipi_csis_device *csis)
0858 {
0859     static const struct {
0860         u32 offset;
0861         const char * const name;
0862     } registers[] = {
0863         { MIPI_CSIS_CMN_CTRL, "CMN_CTRL" },
0864         { MIPI_CSIS_CLK_CTRL, "CLK_CTRL" },
0865         { MIPI_CSIS_INT_MSK, "INT_MSK" },
0866         { MIPI_CSIS_DPHY_STATUS, "DPHY_STATUS" },
0867         { MIPI_CSIS_DPHY_CMN_CTRL, "DPHY_CMN_CTRL" },
0868         { MIPI_CSIS_DPHY_SCTRL_L, "DPHY_SCTRL_L" },
0869         { MIPI_CSIS_DPHY_SCTRL_H, "DPHY_SCTRL_H" },
0870         { MIPI_CSIS_ISP_CONFIG_CH(0), "ISP_CONFIG_CH0" },
0871         { MIPI_CSIS_ISP_RESOL_CH(0), "ISP_RESOL_CH0" },
0872         { MIPI_CSIS_SDW_CONFIG_CH(0), "SDW_CONFIG_CH0" },
0873         { MIPI_CSIS_SDW_RESOL_CH(0), "SDW_RESOL_CH0" },
0874         { MIPI_CSIS_DBG_CTRL, "DBG_CTRL" },
0875         { MIPI_CSIS_FRAME_COUNTER_CH(0), "FRAME_COUNTER_CH0" },
0876     };
0877 
0878     unsigned int i;
0879     u32 cfg;
0880 
0881     if (!pm_runtime_get_if_in_use(csis->dev))
0882         return 0;
0883 
0884     dev_info(csis->dev, "--- REGISTERS ---\n");
0885 
0886     for (i = 0; i < ARRAY_SIZE(registers); i++) {
0887         cfg = mipi_csis_read(csis, registers[i].offset);
0888         dev_info(csis->dev, "%14s: 0x%08x\n", registers[i].name, cfg);
0889     }
0890 
0891     pm_runtime_put(csis->dev);
0892 
0893     return 0;
0894 }
0895 
0896 static int mipi_csis_dump_regs_show(struct seq_file *m, void *private)
0897 {
0898     struct mipi_csis_device *csis = m->private;
0899 
0900     return mipi_csis_dump_regs(csis);
0901 }
0902 DEFINE_SHOW_ATTRIBUTE(mipi_csis_dump_regs);
0903 
0904 static void mipi_csis_debugfs_init(struct mipi_csis_device *csis)
0905 {
0906     csis->debug.hs_settle = UINT_MAX;
0907     csis->debug.clk_settle = UINT_MAX;
0908 
0909     csis->debugfs_root = debugfs_create_dir(dev_name(csis->dev), NULL);
0910 
0911     debugfs_create_bool("debug_enable", 0600, csis->debugfs_root,
0912                 &csis->debug.enable);
0913     debugfs_create_file("dump_regs", 0600, csis->debugfs_root, csis,
0914                 &mipi_csis_dump_regs_fops);
0915     debugfs_create_u32("tclk_settle", 0600, csis->debugfs_root,
0916                &csis->debug.clk_settle);
0917     debugfs_create_u32("ths_settle", 0600, csis->debugfs_root,
0918                &csis->debug.hs_settle);
0919 }
0920 
0921 static void mipi_csis_debugfs_exit(struct mipi_csis_device *csis)
0922 {
0923     debugfs_remove_recursive(csis->debugfs_root);
0924 }
0925 
0926 /* -----------------------------------------------------------------------------
0927  * V4L2 subdev operations
0928  */
0929 
0930 static struct mipi_csis_device *sd_to_mipi_csis_device(struct v4l2_subdev *sdev)
0931 {
0932     return container_of(sdev, struct mipi_csis_device, sd);
0933 }
0934 
0935 static int mipi_csis_s_stream(struct v4l2_subdev *sd, int enable)
0936 {
0937     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
0938     int ret;
0939 
0940     if (!enable) {
0941         mutex_lock(&csis->lock);
0942 
0943         v4l2_subdev_call(csis->src_sd, video, s_stream, 0);
0944 
0945         mipi_csis_stop_stream(csis);
0946         if (csis->debug.enable)
0947             mipi_csis_log_counters(csis, true);
0948 
0949         mutex_unlock(&csis->lock);
0950 
0951         pm_runtime_put(csis->dev);
0952 
0953         return 0;
0954     }
0955 
0956     ret = mipi_csis_calculate_params(csis);
0957     if (ret < 0)
0958         return ret;
0959 
0960     mipi_csis_clear_counters(csis);
0961 
0962     ret = pm_runtime_resume_and_get(csis->dev);
0963     if (ret < 0)
0964         return ret;
0965 
0966     mutex_lock(&csis->lock);
0967 
0968     mipi_csis_start_stream(csis);
0969     ret = v4l2_subdev_call(csis->src_sd, video, s_stream, 1);
0970     if (ret < 0)
0971         goto error;
0972 
0973     mipi_csis_log_counters(csis, true);
0974 
0975     mutex_unlock(&csis->lock);
0976 
0977     return 0;
0978 
0979 error:
0980     mipi_csis_stop_stream(csis);
0981     mutex_unlock(&csis->lock);
0982     pm_runtime_put(csis->dev);
0983 
0984     return ret;
0985 }
0986 
0987 static struct v4l2_mbus_framefmt *
0988 mipi_csis_get_format(struct mipi_csis_device *csis,
0989              struct v4l2_subdev_state *sd_state,
0990              enum v4l2_subdev_format_whence which,
0991              unsigned int pad)
0992 {
0993     if (which == V4L2_SUBDEV_FORMAT_TRY)
0994         return v4l2_subdev_get_try_format(&csis->sd, sd_state, pad);
0995 
0996     return &csis->format_mbus[pad];
0997 }
0998 
0999 static int mipi_csis_init_cfg(struct v4l2_subdev *sd,
1000                   struct v4l2_subdev_state *sd_state)
1001 {
1002     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1003     struct v4l2_mbus_framefmt *fmt_sink;
1004     struct v4l2_mbus_framefmt *fmt_source;
1005     enum v4l2_subdev_format_whence which;
1006 
1007     which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1008     fmt_sink = mipi_csis_get_format(csis, sd_state, which, CSIS_PAD_SINK);
1009 
1010     fmt_sink->code = MEDIA_BUS_FMT_UYVY8_1X16;
1011     fmt_sink->width = MIPI_CSIS_DEF_PIX_WIDTH;
1012     fmt_sink->height = MIPI_CSIS_DEF_PIX_HEIGHT;
1013     fmt_sink->field = V4L2_FIELD_NONE;
1014 
1015     fmt_sink->colorspace = V4L2_COLORSPACE_SMPTE170M;
1016     fmt_sink->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt_sink->colorspace);
1017     fmt_sink->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt_sink->colorspace);
1018     fmt_sink->quantization =
1019         V4L2_MAP_QUANTIZATION_DEFAULT(false, fmt_sink->colorspace,
1020                           fmt_sink->ycbcr_enc);
1021 
1022     fmt_source = mipi_csis_get_format(csis, sd_state, which,
1023                       CSIS_PAD_SOURCE);
1024     *fmt_source = *fmt_sink;
1025 
1026     return 0;
1027 }
1028 
1029 static int mipi_csis_get_fmt(struct v4l2_subdev *sd,
1030                  struct v4l2_subdev_state *sd_state,
1031                  struct v4l2_subdev_format *sdformat)
1032 {
1033     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1034     struct v4l2_mbus_framefmt *fmt;
1035 
1036     fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
1037                    sdformat->pad);
1038 
1039     mutex_lock(&csis->lock);
1040     sdformat->format = *fmt;
1041     mutex_unlock(&csis->lock);
1042 
1043     return 0;
1044 }
1045 
1046 static int mipi_csis_enum_mbus_code(struct v4l2_subdev *sd,
1047                     struct v4l2_subdev_state *sd_state,
1048                     struct v4l2_subdev_mbus_code_enum *code)
1049 {
1050     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1051 
1052     /*
1053      * The CSIS can't transcode in any way, the source format is identical
1054      * to the sink format.
1055      */
1056     if (code->pad == CSIS_PAD_SOURCE) {
1057         struct v4l2_mbus_framefmt *fmt;
1058 
1059         if (code->index > 0)
1060             return -EINVAL;
1061 
1062         fmt = mipi_csis_get_format(csis, sd_state, code->which,
1063                        code->pad);
1064         code->code = fmt->code;
1065         return 0;
1066     }
1067 
1068     if (code->pad != CSIS_PAD_SINK)
1069         return -EINVAL;
1070 
1071     if (code->index >= ARRAY_SIZE(mipi_csis_formats))
1072         return -EINVAL;
1073 
1074     code->code = mipi_csis_formats[code->index].code;
1075 
1076     return 0;
1077 }
1078 
1079 static int mipi_csis_set_fmt(struct v4l2_subdev *sd,
1080                  struct v4l2_subdev_state *sd_state,
1081                  struct v4l2_subdev_format *sdformat)
1082 {
1083     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1084     struct csis_pix_format const *csis_fmt;
1085     struct v4l2_mbus_framefmt *fmt;
1086     unsigned int align;
1087 
1088     /*
1089      * The CSIS can't transcode in any way, the source format can't be
1090      * modified.
1091      */
1092     if (sdformat->pad == CSIS_PAD_SOURCE)
1093         return mipi_csis_get_fmt(sd, sd_state, sdformat);
1094 
1095     if (sdformat->pad != CSIS_PAD_SINK)
1096         return -EINVAL;
1097 
1098     /*
1099      * Validate the media bus code and clamp and align the size.
1100      *
1101      * The total number of bits per line must be a multiple of 8. We thus
1102      * need to align the width for formats that are not multiples of 8
1103      * bits.
1104      */
1105     csis_fmt = find_csis_format(sdformat->format.code);
1106     if (!csis_fmt)
1107         csis_fmt = &mipi_csis_formats[0];
1108 
1109     switch (csis_fmt->width % 8) {
1110     case 0:
1111         align = 0;
1112         break;
1113     case 4:
1114         align = 1;
1115         break;
1116     case 2:
1117     case 6:
1118         align = 2;
1119         break;
1120     default:
1121         /* 1, 3, 5, 7 */
1122         align = 3;
1123         break;
1124     }
1125 
1126     v4l_bound_align_image(&sdformat->format.width, 1,
1127                   CSIS_MAX_PIX_WIDTH, align,
1128                   &sdformat->format.height, 1,
1129                   CSIS_MAX_PIX_HEIGHT, 0, 0);
1130 
1131     fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
1132                    sdformat->pad);
1133 
1134     mutex_lock(&csis->lock);
1135 
1136     fmt->code = csis_fmt->code;
1137     fmt->width = sdformat->format.width;
1138     fmt->height = sdformat->format.height;
1139     fmt->colorspace = sdformat->format.colorspace;
1140     fmt->quantization = sdformat->format.quantization;
1141     fmt->xfer_func = sdformat->format.xfer_func;
1142     fmt->ycbcr_enc = sdformat->format.ycbcr_enc;
1143 
1144     sdformat->format = *fmt;
1145 
1146     /* Propagate the format from sink to source. */
1147     fmt = mipi_csis_get_format(csis, sd_state, sdformat->which,
1148                    CSIS_PAD_SOURCE);
1149     *fmt = sdformat->format;
1150 
1151     /* The format on the source pad might change due to unpacking. */
1152     fmt->code = csis_fmt->output;
1153 
1154     /* Store the CSIS format descriptor for active formats. */
1155     if (sdformat->which == V4L2_SUBDEV_FORMAT_ACTIVE)
1156         csis->csis_fmt = csis_fmt;
1157 
1158     mutex_unlock(&csis->lock);
1159 
1160     return 0;
1161 }
1162 
1163 static int mipi_csis_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
1164                     struct v4l2_mbus_frame_desc *fd)
1165 {
1166     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1167     struct v4l2_mbus_frame_desc_entry *entry = &fd->entry[0];
1168 
1169     if (pad != CSIS_PAD_SOURCE)
1170         return -EINVAL;
1171 
1172     fd->type = V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL;
1173     fd->num_entries = 1;
1174 
1175     memset(entry, 0, sizeof(*entry));
1176 
1177     mutex_lock(&csis->lock);
1178 
1179     entry->flags = 0;
1180     entry->pixelcode = csis->csis_fmt->code;
1181     entry->bus.csi2.vc = 0;
1182     entry->bus.csi2.dt = csis->csis_fmt->data_type;
1183 
1184     mutex_unlock(&csis->lock);
1185 
1186     return 0;
1187 }
1188 
1189 static int mipi_csis_log_status(struct v4l2_subdev *sd)
1190 {
1191     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1192 
1193     mipi_csis_log_counters(csis, true);
1194     if (csis->debug.enable)
1195         mipi_csis_dump_regs(csis);
1196 
1197     return 0;
1198 }
1199 
1200 static const struct v4l2_subdev_core_ops mipi_csis_core_ops = {
1201     .log_status = mipi_csis_log_status,
1202 };
1203 
1204 static const struct v4l2_subdev_video_ops mipi_csis_video_ops = {
1205     .s_stream   = mipi_csis_s_stream,
1206 };
1207 
1208 static const struct v4l2_subdev_pad_ops mipi_csis_pad_ops = {
1209     .init_cfg       = mipi_csis_init_cfg,
1210     .enum_mbus_code     = mipi_csis_enum_mbus_code,
1211     .get_fmt        = mipi_csis_get_fmt,
1212     .set_fmt        = mipi_csis_set_fmt,
1213     .get_frame_desc     = mipi_csis_get_frame_desc,
1214 };
1215 
1216 static const struct v4l2_subdev_ops mipi_csis_subdev_ops = {
1217     .core   = &mipi_csis_core_ops,
1218     .video  = &mipi_csis_video_ops,
1219     .pad    = &mipi_csis_pad_ops,
1220 };
1221 
1222 /* -----------------------------------------------------------------------------
1223  * Media entity operations
1224  */
1225 
1226 static int mipi_csis_link_setup(struct media_entity *entity,
1227                 const struct media_pad *local_pad,
1228                 const struct media_pad *remote_pad, u32 flags)
1229 {
1230     struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1231     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1232     struct v4l2_subdev *remote_sd;
1233 
1234     dev_dbg(csis->dev, "link setup %s -> %s", remote_pad->entity->name,
1235         local_pad->entity->name);
1236 
1237     /* We only care about the link to the source. */
1238     if (!(local_pad->flags & MEDIA_PAD_FL_SINK))
1239         return 0;
1240 
1241     remote_sd = media_entity_to_v4l2_subdev(remote_pad->entity);
1242 
1243     if (flags & MEDIA_LNK_FL_ENABLED) {
1244         if (csis->src_sd)
1245             return -EBUSY;
1246 
1247         csis->src_sd = remote_sd;
1248     } else {
1249         csis->src_sd = NULL;
1250     }
1251 
1252     return 0;
1253 }
1254 
1255 static const struct media_entity_operations mipi_csis_entity_ops = {
1256     .link_setup = mipi_csis_link_setup,
1257     .link_validate  = v4l2_subdev_link_validate,
1258     .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
1259 };
1260 
1261 /* -----------------------------------------------------------------------------
1262  * Async subdev notifier
1263  */
1264 
1265 static struct mipi_csis_device *
1266 mipi_notifier_to_csis_state(struct v4l2_async_notifier *n)
1267 {
1268     return container_of(n, struct mipi_csis_device, notifier);
1269 }
1270 
1271 static int mipi_csis_notify_bound(struct v4l2_async_notifier *notifier,
1272                   struct v4l2_subdev *sd,
1273                   struct v4l2_async_subdev *asd)
1274 {
1275     struct mipi_csis_device *csis = mipi_notifier_to_csis_state(notifier);
1276     struct media_pad *sink = &csis->sd.entity.pads[CSIS_PAD_SINK];
1277 
1278     return v4l2_create_fwnode_links_to_pad(sd, sink, 0);
1279 }
1280 
1281 static const struct v4l2_async_notifier_operations mipi_csis_notify_ops = {
1282     .bound = mipi_csis_notify_bound,
1283 };
1284 
1285 static int mipi_csis_async_register(struct mipi_csis_device *csis)
1286 {
1287     struct v4l2_fwnode_endpoint vep = {
1288         .bus_type = V4L2_MBUS_CSI2_DPHY,
1289     };
1290     struct v4l2_async_subdev *asd;
1291     struct fwnode_handle *ep;
1292     unsigned int i;
1293     int ret;
1294 
1295     v4l2_async_nf_init(&csis->notifier);
1296 
1297     ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev), 0, 0,
1298                          FWNODE_GRAPH_ENDPOINT_NEXT);
1299     if (!ep)
1300         return -ENOTCONN;
1301 
1302     ret = v4l2_fwnode_endpoint_parse(ep, &vep);
1303     if (ret)
1304         goto err_parse;
1305 
1306     for (i = 0; i < vep.bus.mipi_csi2.num_data_lanes; ++i) {
1307         if (vep.bus.mipi_csi2.data_lanes[i] != i + 1) {
1308             dev_err(csis->dev,
1309                 "data lanes reordering is not supported");
1310             ret = -EINVAL;
1311             goto err_parse;
1312         }
1313     }
1314 
1315     csis->bus = vep.bus.mipi_csi2;
1316 
1317     dev_dbg(csis->dev, "data lanes: %d\n", csis->bus.num_data_lanes);
1318     dev_dbg(csis->dev, "flags: 0x%08x\n", csis->bus.flags);
1319 
1320     asd = v4l2_async_nf_add_fwnode_remote(&csis->notifier, ep,
1321                           struct v4l2_async_subdev);
1322     if (IS_ERR(asd)) {
1323         ret = PTR_ERR(asd);
1324         goto err_parse;
1325     }
1326 
1327     fwnode_handle_put(ep);
1328 
1329     csis->notifier.ops = &mipi_csis_notify_ops;
1330 
1331     ret = v4l2_async_subdev_nf_register(&csis->sd, &csis->notifier);
1332     if (ret)
1333         return ret;
1334 
1335     return v4l2_async_register_subdev(&csis->sd);
1336 
1337 err_parse:
1338     fwnode_handle_put(ep);
1339 
1340     return ret;
1341 }
1342 
1343 /* -----------------------------------------------------------------------------
1344  * Suspend/resume
1345  */
1346 
1347 static int __maybe_unused mipi_csis_runtime_suspend(struct device *dev)
1348 {
1349     struct v4l2_subdev *sd = dev_get_drvdata(dev);
1350     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1351     int ret = 0;
1352 
1353     mutex_lock(&csis->lock);
1354 
1355     ret = mipi_csis_phy_disable(csis);
1356     if (ret)
1357         goto unlock;
1358 
1359     mipi_csis_clk_disable(csis);
1360 
1361 unlock:
1362     mutex_unlock(&csis->lock);
1363 
1364     return ret ? -EAGAIN : 0;
1365 }
1366 
1367 static int __maybe_unused mipi_csis_runtime_resume(struct device *dev)
1368 {
1369     struct v4l2_subdev *sd = dev_get_drvdata(dev);
1370     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1371     int ret = 0;
1372 
1373     mutex_lock(&csis->lock);
1374 
1375     ret = mipi_csis_phy_enable(csis);
1376     if (ret)
1377         goto unlock;
1378 
1379     mipi_csis_clk_enable(csis);
1380 
1381 unlock:
1382     mutex_unlock(&csis->lock);
1383 
1384     return ret ? -EAGAIN : 0;
1385 }
1386 
1387 static const struct dev_pm_ops mipi_csis_pm_ops = {
1388     SET_RUNTIME_PM_OPS(mipi_csis_runtime_suspend, mipi_csis_runtime_resume,
1389                NULL)
1390 };
1391 
1392 /* -----------------------------------------------------------------------------
1393  * Probe/remove & platform driver
1394  */
1395 
1396 static int mipi_csis_subdev_init(struct mipi_csis_device *csis)
1397 {
1398     struct v4l2_subdev *sd = &csis->sd;
1399 
1400     v4l2_subdev_init(sd, &mipi_csis_subdev_ops);
1401     sd->owner = THIS_MODULE;
1402     snprintf(sd->name, sizeof(sd->name), "csis-%s",
1403          dev_name(csis->dev));
1404 
1405     sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1406     sd->ctrl_handler = NULL;
1407 
1408     sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
1409     sd->entity.ops = &mipi_csis_entity_ops;
1410 
1411     sd->dev = csis->dev;
1412 
1413     sd->fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(csis->dev),
1414                              1, 0, 0);
1415     if (!sd->fwnode) {
1416         dev_err(csis->dev, "Unable to retrieve endpoint for port@1\n");
1417         return -ENOENT;
1418     }
1419 
1420     csis->csis_fmt = &mipi_csis_formats[0];
1421     mipi_csis_init_cfg(sd, NULL);
1422 
1423     csis->pads[CSIS_PAD_SINK].flags = MEDIA_PAD_FL_SINK
1424                      | MEDIA_PAD_FL_MUST_CONNECT;
1425     csis->pads[CSIS_PAD_SOURCE].flags = MEDIA_PAD_FL_SOURCE
1426                        | MEDIA_PAD_FL_MUST_CONNECT;
1427     return media_entity_pads_init(&sd->entity, CSIS_PADS_NUM,
1428                       csis->pads);
1429 }
1430 
1431 static int mipi_csis_parse_dt(struct mipi_csis_device *csis)
1432 {
1433     struct device_node *node = csis->dev->of_node;
1434 
1435     if (of_property_read_u32(node, "clock-frequency",
1436                  &csis->clk_frequency))
1437         csis->clk_frequency = DEFAULT_SCLK_CSIS_FREQ;
1438 
1439     return 0;
1440 }
1441 
1442 static int mipi_csis_probe(struct platform_device *pdev)
1443 {
1444     struct device *dev = &pdev->dev;
1445     struct mipi_csis_device *csis;
1446     int irq;
1447     int ret;
1448 
1449     csis = devm_kzalloc(dev, sizeof(*csis), GFP_KERNEL);
1450     if (!csis)
1451         return -ENOMEM;
1452 
1453     mutex_init(&csis->lock);
1454     spin_lock_init(&csis->slock);
1455 
1456     csis->dev = dev;
1457     csis->info = of_device_get_match_data(dev);
1458 
1459     memcpy(csis->events, mipi_csis_events, sizeof(csis->events));
1460 
1461     /* Parse DT properties. */
1462     ret = mipi_csis_parse_dt(csis);
1463     if (ret < 0) {
1464         dev_err(dev, "Failed to parse device tree: %d\n", ret);
1465         return ret;
1466     }
1467 
1468     /* Acquire resources. */
1469     csis->regs = devm_platform_ioremap_resource(pdev, 0);
1470     if (IS_ERR(csis->regs))
1471         return PTR_ERR(csis->regs);
1472 
1473     irq = platform_get_irq(pdev, 0);
1474     if (irq < 0)
1475         return irq;
1476 
1477     ret = mipi_csis_phy_init(csis);
1478     if (ret < 0)
1479         return ret;
1480 
1481     ret = mipi_csis_clk_get(csis);
1482     if (ret < 0)
1483         return ret;
1484 
1485     /* Reset PHY and enable the clocks. */
1486     mipi_csis_phy_reset(csis);
1487 
1488     ret = mipi_csis_clk_enable(csis);
1489     if (ret < 0) {
1490         dev_err(csis->dev, "failed to enable clocks: %d\n", ret);
1491         return ret;
1492     }
1493 
1494     /* Now that the hardware is initialized, request the interrupt. */
1495     ret = devm_request_irq(dev, irq, mipi_csis_irq_handler, 0,
1496                    dev_name(dev), csis);
1497     if (ret) {
1498         dev_err(dev, "Interrupt request failed\n");
1499         goto disable_clock;
1500     }
1501 
1502     /* Initialize and register the subdev. */
1503     ret = mipi_csis_subdev_init(csis);
1504     if (ret < 0)
1505         goto disable_clock;
1506 
1507     platform_set_drvdata(pdev, &csis->sd);
1508 
1509     ret = mipi_csis_async_register(csis);
1510     if (ret < 0) {
1511         dev_err(dev, "async register failed: %d\n", ret);
1512         goto cleanup;
1513     }
1514 
1515     /* Initialize debugfs. */
1516     mipi_csis_debugfs_init(csis);
1517 
1518     /* Enable runtime PM. */
1519     pm_runtime_enable(dev);
1520     if (!pm_runtime_enabled(dev)) {
1521         ret = mipi_csis_runtime_resume(dev);
1522         if (ret < 0)
1523             goto unregister_all;
1524     }
1525 
1526     dev_info(dev, "lanes: %d, freq: %u\n",
1527          csis->bus.num_data_lanes, csis->clk_frequency);
1528 
1529     return 0;
1530 
1531 unregister_all:
1532     mipi_csis_debugfs_exit(csis);
1533 cleanup:
1534     media_entity_cleanup(&csis->sd.entity);
1535     v4l2_async_nf_unregister(&csis->notifier);
1536     v4l2_async_nf_cleanup(&csis->notifier);
1537     v4l2_async_unregister_subdev(&csis->sd);
1538 disable_clock:
1539     mipi_csis_clk_disable(csis);
1540     fwnode_handle_put(csis->sd.fwnode);
1541     mutex_destroy(&csis->lock);
1542 
1543     return ret;
1544 }
1545 
1546 static int mipi_csis_remove(struct platform_device *pdev)
1547 {
1548     struct v4l2_subdev *sd = platform_get_drvdata(pdev);
1549     struct mipi_csis_device *csis = sd_to_mipi_csis_device(sd);
1550 
1551     mipi_csis_debugfs_exit(csis);
1552     v4l2_async_nf_unregister(&csis->notifier);
1553     v4l2_async_nf_cleanup(&csis->notifier);
1554     v4l2_async_unregister_subdev(&csis->sd);
1555 
1556     pm_runtime_disable(&pdev->dev);
1557     mipi_csis_runtime_suspend(&pdev->dev);
1558     mipi_csis_clk_disable(csis);
1559     media_entity_cleanup(&csis->sd.entity);
1560     fwnode_handle_put(csis->sd.fwnode);
1561     mutex_destroy(&csis->lock);
1562     pm_runtime_set_suspended(&pdev->dev);
1563 
1564     return 0;
1565 }
1566 
1567 static const struct of_device_id mipi_csis_of_match[] = {
1568     {
1569         .compatible = "fsl,imx7-mipi-csi2",
1570         .data = &(const struct mipi_csis_info){
1571             .version = MIPI_CSIS_V3_3,
1572             .num_clocks = 3,
1573         },
1574     }, {
1575         .compatible = "fsl,imx8mm-mipi-csi2",
1576         .data = &(const struct mipi_csis_info){
1577             .version = MIPI_CSIS_V3_6_3,
1578             .num_clocks = 4,
1579         },
1580     },
1581     { /* sentinel */ },
1582 };
1583 MODULE_DEVICE_TABLE(of, mipi_csis_of_match);
1584 
1585 static struct platform_driver mipi_csis_driver = {
1586     .probe      = mipi_csis_probe,
1587     .remove     = mipi_csis_remove,
1588     .driver     = {
1589         .of_match_table = mipi_csis_of_match,
1590         .name       = CSIS_DRIVER_NAME,
1591         .pm     = &mipi_csis_pm_ops,
1592     },
1593 };
1594 
1595 module_platform_driver(mipi_csis_driver);
1596 
1597 MODULE_DESCRIPTION("i.MX7 & i.MX8 MIPI CSI-2 receiver driver");
1598 MODULE_LICENSE("GPL v2");
1599 MODULE_ALIAS("platform:imx-mipi-csi2");