1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (c) 2020 Rockchip Electronics Co., Ltd. */ 3 #ifndef _RKCIF_MIPI_CSI2_H_ 4 #define _RKCIF_MIPI_CSI2_H_ 5 6 #include <linux/notifier.h> 7 #include <media/v4l2-device.h> 8 #include <media/v4l2-fwnode.h> 9 #include <media/v4l2-subdev.h> 10 #include <media/v4l2-event.h> 11 12 #define CSI2_ERR_FSFE_MASK (0xff << 8) 13 #define CSI2_ERR_COUNT_ALL_MASK (0xff) 14 15 /* 16 * there must be 5 pads: 1 input pad from sensor, and 17 * the 4 virtual channel output pads 18 */ 19 #define CSI2_SINK_PAD 0 20 #define CSI2_NUM_SINK_PADS 4 21 #define CSI2_NUM_SRC_PADS 8 22 #define CSI2_NUM_PADS 5 23 #define CSI2_NUM_PADS_MAX 9 24 #define CSI2_NUM_PADS_SINGLE_LINK 2 25 #define MAX_CSI2_SENSORS 2 26 27 #define RKCIF_DEFAULT_WIDTH 640 28 #define RKCIF_DEFAULT_HEIGHT 480 29 30 /* 31 * The default maximum bit-rate per lane in Mbps, if the 32 * source subdev does not provide V4L2_CID_LINK_FREQ. 33 */ 34 #define CSI2_DEFAULT_MAX_MBPS 849 35 36 #define IMX_MEDIA_GRP_ID_CSI2 BIT(8) 37 #define CSIHOST_MAX_ERRINT_COUNT 10 38 39 #define DEVICE_NAME "rockchip-mipi-csi2" 40 41 /* CSI Host Registers Define */ 42 #define CSIHOST_N_LANES 0x04 43 #define CSIHOST_DPHY_SHUTDOWNZ 0x08 44 #define CSIHOST_PHY_RSTZ 0x0c 45 #define CSIHOST_RESETN 0x10 46 #define CSIHOST_PHY_STATE 0x14 47 #define CSIHOST_ERR1 0x20 48 #define CSIHOST_ERR2 0x24 49 #define CSIHOST_MSK1 0x28 50 #define CSIHOST_MSK2 0x2c 51 #define CSIHOST_CONTROL 0x40 52 53 #define CSIHOST_ERR1_PHYERR_SPTSYNCHS 0x0000000f 54 #define CSIHOST_ERR1_ERR_BNDRY_MATCH 0x000000f0 55 #define CSIHOST_ERR1_ERR_SEQ 0x00000f00 56 #define CSIHOST_ERR1_ERR_FRM_DATA 0x0000f000 57 #define CSIHOST_ERR1_ERR_CRC 0x1f000000 58 59 #define CSIHOST_ERR2_PHYERR_ESC 0x0000000f 60 #define CSIHOST_ERR2_PHYERR_SOTHS 0x000000f0 61 #define CSIHOST_ERR2_ECC_CORRECTED 0x00000f00 62 #define CSIHOST_ERR2_ERR_ID 0x0000f000 63 #define CSIHOST_ERR2_PHYERR_CODEHS 0x01000000 64 65 #define SW_CPHY_EN(x) ((x) << 0) 66 #define SW_DSI_EN(x) ((x) << 4) 67 #define SW_DATATYPE_FS(x) ((x) << 8) 68 #define SW_DATATYPE_FE(x) ((x) << 14) 69 #define SW_DATATYPE_LS(x) ((x) << 20) 70 #define SW_DATATYPE_LE(x) ((x) << 26) 71 72 /* 73 * add new chip id in tail in time order 74 * by increasing to distinguish csi2 host version 75 */ 76 enum rkcsi2_chip_id { 77 CHIP_PX30_CSI2, 78 CHIP_RK1808_CSI2, 79 CHIP_RK3128_CSI2, 80 CHIP_RK3288_CSI2, 81 CHIP_RV1126_CSI2, 82 CHIP_RK3568_CSI2, 83 CHIP_RK3588_CSI2, 84 }; 85 86 enum csi2_pads { 87 RK_CSI2_PAD_SINK = 0, 88 RK_CSI2X_PAD_SOURCE0, 89 RK_CSI2X_PAD_SOURCE1, 90 RK_CSI2X_PAD_SOURCE2, 91 RK_CSI2X_PAD_SOURCE3 92 }; 93 94 enum csi2_err { 95 RK_CSI2_ERR_SOTSYN = 0x0, 96 RK_CSI2_ERR_FS_FE_MIS, 97 RK_CSI2_ERR_FRM_SEQ_ERR, 98 RK_CSI2_ERR_CRC_ONCE, 99 RK_CSI2_ERR_CRC, 100 RK_CSI2_ERR_ALL, 101 RK_CSI2_ERR_MAX 102 }; 103 104 enum host_type_t { 105 RK_CSI_RXHOST, 106 RK_DSI_RXHOST 107 }; 108 109 struct csi2_match_data { 110 int chip_id; 111 int num_pads; 112 }; 113 114 struct csi2_sensor { 115 struct v4l2_subdev *sd; 116 struct v4l2_mbus_config mbus; 117 int lanes; 118 }; 119 120 struct csi2_err_stats { 121 unsigned int cnt; 122 }; 123 124 struct csi2_dev { 125 struct device *dev; 126 struct v4l2_subdev sd; 127 struct media_pad pad[CSI2_NUM_PADS_MAX]; 128 struct clk_bulk_data *clks_bulk; 129 int clks_num; 130 struct reset_control *rsts_bulk; 131 132 void __iomem *base; 133 struct v4l2_async_notifier notifier; 134 struct v4l2_fwnode_bus_mipi_csi2 bus; 135 136 /* lock to protect all members below */ 137 struct mutex lock; 138 139 struct v4l2_mbus_framefmt format_mbus; 140 struct v4l2_rect crop; 141 int stream_count; 142 struct v4l2_subdev *src_sd; 143 bool sink_linked[CSI2_NUM_SRC_PADS]; 144 struct csi2_sensor sensors[MAX_CSI2_SENSORS]; 145 const struct csi2_match_data *match_data; 146 int num_sensors; 147 atomic_t frm_sync_seq; 148 struct csi2_err_stats err_list[RK_CSI2_ERR_MAX]; 149 }; 150 151 u32 rkcif_csi2_get_sof(struct csi2_dev *csi2_dev); 152 void rkcif_csi2_set_sof(struct csi2_dev *csi2_dev, u32 seq); 153 void rkcif_csi2_event_inc_sof(struct csi2_dev *csi2_dev); 154 int __init rkcif_csi2_plat_drv_init(void); 155 void __exit rkcif_csi2_plat_drv_exit(void); 156 int rkcif_csi2_register_notifier(struct notifier_block *nb); 157 int rkcif_csi2_unregister_notifier(struct notifier_block *nb); 158 159 #endif 160