1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * camss-csid.h
4  *
5  * Qualcomm MSM Camera Subsystem - CSID (CSI Decoder) Module
6  *
7  * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
8  * Copyright (C) 2015-2018 Linaro Ltd.
9  */
10 #ifndef QC_MSM_CAMSS_CSID_H
11 #define QC_MSM_CAMSS_CSID_H
12 
13 #include <linux/clk.h>
14 #include <linux/interrupt.h>
15 #include <media/media-entity.h>
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-mediabus.h>
19 #include <media/v4l2-subdev.h>
20 
21 #define MSM_CSID_PAD_SINK 0
22 #define MSM_CSID_PAD_SRC 1
23 #define MSM_CSID_PADS_NUM 2
24 
25 #define DATA_TYPE_EMBEDDED_DATA_8BIT	0x12
26 #define DATA_TYPE_YUV420_8BIT		0x18
27 #define DATA_TYPE_YUV420_10BIT		0x19
28 #define DATA_TYPE_YUV420_8BIT_LEGACY	0x1a
29 #define DATA_TYPE_YUV420_8BIT_SHIFTED	0x1c /* Chroma Shifted Pixel Sampling */
30 #define DATA_TYPE_YUV420_10BIT_SHIFTED	0x1d /* Chroma Shifted Pixel Sampling */
31 #define DATA_TYPE_YUV422_8BIT		0x1e
32 #define DATA_TYPE_YUV422_10BIT		0x1f
33 #define DATA_TYPE_RGB444		0x20
34 #define DATA_TYPE_RGB555		0x21
35 #define DATA_TYPE_RGB565		0x22
36 #define DATA_TYPE_RGB666		0x23
37 #define DATA_TYPE_RGB888		0x24
38 #define DATA_TYPE_RAW_24BIT		0x27
39 #define DATA_TYPE_RAW_6BIT		0x28
40 #define DATA_TYPE_RAW_7BIT		0x29
41 #define DATA_TYPE_RAW_8BIT		0x2a
42 #define DATA_TYPE_RAW_10BIT		0x2b
43 #define DATA_TYPE_RAW_12BIT		0x2c
44 #define DATA_TYPE_RAW_14BIT		0x2d
45 #define DATA_TYPE_RAW_16BIT		0x2e
46 #define DATA_TYPE_RAW_20BIT		0x2f
47 
48 #define CSID_RESET_TIMEOUT_MS 500
49 
50 enum csid_testgen_mode {
51 	CSID_PAYLOAD_MODE_DISABLED = 0,
52 	CSID_PAYLOAD_MODE_INCREMENTING = 1,
53 	CSID_PAYLOAD_MODE_ALTERNATING_55_AA = 2,
54 	CSID_PAYLOAD_MODE_ALL_ZEROES = 3,
55 	CSID_PAYLOAD_MODE_ALL_ONES = 4,
56 	CSID_PAYLOAD_MODE_RANDOM = 5,
57 	CSID_PAYLOAD_MODE_USER_SPECIFIED = 6,
58 	CSID_PAYLOAD_MODE_NUM_SUPPORTED_GEN1 = 6, /* excluding disabled */
59 	CSID_PAYLOAD_MODE_COMPLEX_PATTERN = 7,
60 	CSID_PAYLOAD_MODE_COLOR_BOX = 8,
61 	CSID_PAYLOAD_MODE_COLOR_BARS = 9,
62 	CSID_PAYLOAD_MODE_NUM_SUPPORTED_GEN2 = 9, /* excluding disabled */
63 };
64 
65 struct csid_format {
66 	u32 code;
67 	u8 data_type;
68 	u8 decode_format;
69 	u8 bpp;
70 	u8 spp; /* bus samples per pixel */
71 };
72 
73 struct csid_testgen_config {
74 	enum csid_testgen_mode mode;
75 	const char * const*modes;
76 	u8 nmodes;
77 	u8 enabled;
78 };
79 
80 struct csid_phy_config {
81 	u8 csiphy_id;
82 	u8 lane_cnt;
83 	u32 lane_assign;
84 };
85 
86 struct csid_device;
87 
88 struct csid_hw_ops {
89 	/*
90 	 * configure_stream - Configures and starts CSID input stream
91 	 * @csid: CSID device
92 	 */
93 	void (*configure_stream)(struct csid_device *csid, u8 enable);
94 
95 	/*
96 	 * configure_testgen_pattern - Validates and configures output pattern mode
97 	 * of test pattern generator
98 	 * @csid: CSID device
99 	 */
100 	int (*configure_testgen_pattern)(struct csid_device *csid, s32 val);
101 
102 	/*
103 	 * hw_version - Read hardware version register from hardware
104 	 * @csid: CSID device
105 	 */
106 	u32 (*hw_version)(struct csid_device *csid);
107 
108 	/*
109 	 * isr - CSID module interrupt service routine
110 	 * @irq: Interrupt line
111 	 * @dev: CSID device
112 	 *
113 	 * Return IRQ_HANDLED on success
114 	 */
115 	irqreturn_t (*isr)(int irq, void *dev);
116 
117 	/*
118 	 * reset - Trigger reset on CSID module and wait to complete
119 	 * @csid: CSID device
120 	 *
121 	 * Return 0 on success or a negative error code otherwise
122 	 */
123 	int (*reset)(struct csid_device *csid);
124 
125 	/*
126 	 * src_pad_code - Pick an output/src format based on the input/sink format
127 	 * @csid: CSID device
128 	 * @sink_code: The sink format of the input
129 	 * @match_format_idx: Request preferred index, as defined by subdevice csid_format.
130 	 *	Set @match_code to 0 if used.
131 	 * @match_code: Request preferred code, set @match_format_idx to 0 if used
132 	 *
133 	 * Return 0 on failure or src format code otherwise
134 	 */
135 	u32 (*src_pad_code)(struct csid_device *csid, u32 sink_code,
136 			    unsigned int match_format_idx, u32 match_code);
137 
138 	/*
139 	 * subdev_init - Initialize CSID device according for hardware revision
140 	 * @csid: CSID device
141 	 */
142 	void (*subdev_init)(struct csid_device *csid);
143 };
144 
145 struct csid_device {
146 	struct camss *camss;
147 	u8 id;
148 	struct v4l2_subdev subdev;
149 	struct media_pad pads[MSM_CSID_PADS_NUM];
150 	void __iomem *base;
151 	u32 irq;
152 	char irq_name[30];
153 	struct camss_clock *clock;
154 	int nclocks;
155 	struct regulator *vdda;
156 	struct completion reset_complete;
157 	struct csid_testgen_config testgen;
158 	struct csid_phy_config phy;
159 	struct v4l2_mbus_framefmt fmt[MSM_CSID_PADS_NUM];
160 	struct v4l2_ctrl_handler ctrls;
161 	struct v4l2_ctrl *testgen_mode;
162 	const struct csid_format *formats;
163 	unsigned int nformats;
164 	const struct csid_hw_ops *ops;
165 };
166 
167 struct resources;
168 
169 /*
170  * csid_find_code - Find a format code in an array using array index or format code
171  * @codes: Array of format codes
172  * @ncodes: Length of @code array
173  * @req_format_idx: Request preferred index, as defined by subdevice csid_format.
174  *	Set @match_code to 0 if used.
175  * @match_code: Request preferred code, set @req_format_idx to 0 if used
176  *
177  * Return 0 on failure or format code otherwise
178  */
179 u32 csid_find_code(u32 *codes, unsigned int ncode,
180 		   unsigned int match_format_idx, u32 match_code);
181 
182 /*
183  * csid_get_fmt_entry - Find csid_format entry with matching format code
184  * @formats: Array of format csid_format entries
185  * @nformats: Length of @nformats array
186  * @code: Desired format code
187  *
188  * Return formats[0] on failure to find code
189  */
190 const struct csid_format *csid_get_fmt_entry(const struct csid_format *formats,
191 					     unsigned int nformats,
192 					     u32 code);
193 
194 int msm_csid_subdev_init(struct camss *camss, struct csid_device *csid,
195 			 const struct resources *res, u8 id);
196 
197 int msm_csid_register_entity(struct csid_device *csid,
198 			     struct v4l2_device *v4l2_dev);
199 
200 void msm_csid_unregister_entity(struct csid_device *csid);
201 
202 void msm_csid_get_csid_id(struct media_entity *entity, u8 *id);
203 
204 extern const char * const csid_testgen_modes[];
205 
206 extern const struct csid_hw_ops csid_ops_4_1;
207 extern const struct csid_hw_ops csid_ops_4_7;
208 extern const struct csid_hw_ops csid_ops_170;
209 
210 
211 #endif /* QC_MSM_CAMSS_CSID_H */
212