1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
// SPDX-License-Identifier: GPL-2.0
/*
* Qualcomm MSM Camera Subsystem - CSID (CSI Decoder) Module
*
* Copyright (c) 2024 Qualcomm Technologies, Inc.
*/
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/of.h>
#include "camss.h"
#include "camss-csid.h"
#include "camss-csid-780.h"
#define CSID_IO_PATH_CFG0(csid) (0x4 * (csid))
#define OUTPUT_IFE_EN 0x100
#define INTERNAL_CSID 1
#define CSID_RST_CFG 0xC
#define RST_MODE BIT(0)
#define RST_LOCATION BIT(4)
#define CSID_RST_CMD 0x10
#define SELECT_HW_RST BIT(0)
#define SELECT_IRQ_RST BIT(2)
#define CSID_IRQ_CMD 0x14
#define IRQ_CMD_CLEAR BIT(0)
#define CSID_RUP_AUP_CMD 0x18
#define CSID_RUP_AUP_RDI(rdi) ((BIT(4) | BIT(20)) << (rdi))
#define CSID_TOP_IRQ_STATUS 0x7C
#define TOP_IRQ_STATUS_RESET_DONE BIT(0)
#define CSID_TOP_IRQ_MASK 0x80
#define CSID_TOP_IRQ_CLEAR 0x84
#define CSID_TOP_IRQ_SET 0x88
#define CSID_CSI2_RX_IRQ_STATUS 0x9C
#define CSID_CSI2_RX_IRQ_MASK 0xA0
#define CSID_CSI2_RX_IRQ_CLEAR 0xA4
#define CSID_CSI2_RX_IRQ_SET 0xA8
#define CSID_BUF_DONE_IRQ_STATUS 0x8C
#define BUF_DONE_IRQ_STATUS_RDI_OFFSET (csid_is_lite(csid) ? 1 : 14)
#define CSID_BUF_DONE_IRQ_MASK 0x90
#define CSID_BUF_DONE_IRQ_CLEAR 0x94
#define CSID_BUF_DONE_IRQ_SET 0x98
#define CSID_CSI2_RDIN_IRQ_STATUS(rdi) (0xEC + 0x10 * (rdi))
#define RUP_DONE_IRQ_STATUS BIT(23)
#define CSID_CSI2_RDIN_IRQ_CLEAR(rdi) (0xF4 + 0x10 * (rdi))
#define CSID_CSI2_RDIN_IRQ_SET(rdi) (0xF8 + 0x10 * (rdi))
#define CSID_CSI2_RX_CFG0 0x200
#define CSI2_RX_CFG0_NUM_ACTIVE_LANES 0
#define CSI2_RX_CFG0_DL0_INPUT_SEL 4
#define CSI2_RX_CFG0_PHY_NUM_SEL 20
#define CSID_CSI2_RX_CFG1 0x204
#define CSI2_RX_CFG1_ECC_CORRECTION_EN BIT(0)
#define CSI2_RX_CFG1_VC_MODE BIT(2)
#define CSID_RDI_CFG0(rdi) (0x500 + 0x100 * (rdi))
#define RDI_CFG0_TIMESTAMP_EN BIT(6)
#define RDI_CFG0_TIMESTAMP_STB_SEL BIT(8)
#define RDI_CFG0_DECODE_FORMAT 12
#define RDI_CFG0_DT 16
#define RDI_CFG0_VC 22
#define RDI_CFG0_DT_ID 27
#define RDI_CFG0_EN BIT(31)
#define CSID_RDI_CTRL(rdi) (0x504 + 0x100 * (rdi))
#define RDI_CTRL_START_CMD BIT(0)
#define CSID_RDI_CFG1(rdi) (0x510 + 0x100 * (rdi))
#define RDI_CFG1_DROP_H_EN BIT(5)
#define RDI_CFG1_DROP_V_EN BIT(6)
#define RDI_CFG1_CROP_H_EN BIT(7)
#define RDI_CFG1_CROP_V_EN BIT(8)
#define RDI_CFG1_PIX_STORE BIT(10)
#define RDI_CFG1_PACKING_FORMAT_MIPI BIT(15)
#define CSID_RDI_IRQ_SUBSAMPLE_PATTERN(rdi) (0x548 + 0x100 * (rdi))
#define CSID_RDI_IRQ_SUBSAMPLE_PERIOD(rdi) (0x54C + 0x100 * (rdi))
#define CSI2_RX_CFG0_PHY_SEL_BASE_IDX 1
static void __csid_configure_rx(struct csid_device *csid,
struct csid_phy_config *phy, int vc)
{
int val;
val = (phy->lane_cnt - 1) << CSI2_RX_CFG0_NUM_ACTIVE_LANES;
val |= phy->lane_assign << CSI2_RX_CFG0_DL0_INPUT_SEL;
val |= (phy->csiphy_id + CSI2_RX_CFG0_PHY_SEL_BASE_IDX) << CSI2_RX_CFG0_PHY_NUM_SEL;
writel(val, csid->base + CSID_CSI2_RX_CFG0);
val = CSI2_RX_CFG1_ECC_CORRECTION_EN;
if (vc > 3)
val |= CSI2_RX_CFG1_VC_MODE;
writel(val, csid->base + CSID_CSI2_RX_CFG1);
}
static void __csid_ctrl_rdi(struct csid_device *csid, int enable, u8 rdi)
{
int val = 0;
if (enable)
val = RDI_CTRL_START_CMD;
writel(val, csid->base + CSID_RDI_CTRL(rdi));
}
static void __csid_configure_wrapper(struct csid_device *csid)
{
u32 val;
/* csid lite doesn't need to configure top register */
if (csid->res->is_lite)
return;
val = OUTPUT_IFE_EN | INTERNAL_CSID;
writel(val, csid->camss->csid_wrapper_base + CSID_IO_PATH_CFG0(csid->id));
}
static void __csid_configure_rdi_stream(struct csid_device *csid, u8 enable, u8 vc)
{
u32 val;
u8 lane_cnt = csid->phy.lane_cnt;
/* Source pads matching RDI channels on hardware. Pad 1 -> RDI0, Pad 2 -> RDI1, etc. */
struct v4l2_mbus_framefmt *input_format = &csid->fmt[MSM_CSID_PAD_FIRST_SRC + vc];
const struct csid_format_info *format = csid_get_fmt_entry(csid->res->formats->formats,
csid->res->formats->nformats,
input_format->code);
if (!lane_cnt)
lane_cnt = 4;
/*
* DT_ID is a two bit bitfield that is concatenated with
* the four least significant bits of the five bit VC
* bitfield to generate an internal CID value.
*
* CSID_RDI_CFG0(vc)
* DT_ID : 28:27
* VC : 26:22
* DT : 21:16
*
* CID : VC 3:0 << 2 | DT_ID 1:0
*/
u8 dt_id = vc & 0x03;
val = RDI_CFG0_TIMESTAMP_EN;
val |= RDI_CFG0_TIMESTAMP_STB_SEL;
/* note: for non-RDI path, this should be format->decode_format */
val |= DECODE_FORMAT_PAYLOAD_ONLY << RDI_CFG0_DECODE_FORMAT;
val |= vc << RDI_CFG0_VC;
val |= format->data_type << RDI_CFG0_DT;
val |= dt_id << RDI_CFG0_DT_ID;
writel(val, csid->base + CSID_RDI_CFG0(vc));
val = RDI_CFG1_PACKING_FORMAT_MIPI;
val |= RDI_CFG1_PIX_STORE;
val |= RDI_CFG1_DROP_H_EN;
val |= RDI_CFG1_DROP_V_EN;
val |= RDI_CFG1_CROP_H_EN;
val |= RDI_CFG1_CROP_V_EN;
writel(val, csid->base + CSID_RDI_CFG1(vc));
val = 0;
writel(val, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PERIOD(vc));
val = 1;
writel(val, csid->base + CSID_RDI_IRQ_SUBSAMPLE_PATTERN(vc));
val = 0;
writel(val, csid->base + CSID_RDI_CTRL(vc));
val = readl(csid->base + CSID_RDI_CFG0(vc));
if (enable)
val |= RDI_CFG0_EN;
writel(val, csid->base + CSID_RDI_CFG0(vc));
}
static void csid_configure_stream(struct csid_device *csid, u8 enable)
{
u8 i;
__csid_configure_wrapper(csid);
/* Loop through all enabled VCs and configure stream for each */
for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++)
if (csid->phy.en_vc & BIT(i)) {
__csid_configure_rdi_stream(csid, enable, i);
__csid_configure_rx(csid, &csid->phy, i);
__csid_ctrl_rdi(csid, enable, i);
}
}
static int csid_configure_testgen_pattern(struct csid_device *csid, s32 val)
{
return 0;
}
static void csid_subdev_reg_update(struct csid_device *csid, int port_id, bool clear)
{
if (clear) {
csid->reg_update &= ~CSID_RUP_AUP_RDI(port_id);
} else {
csid->reg_update |= CSID_RUP_AUP_RDI(port_id);
writel(csid->reg_update, csid->base + CSID_RUP_AUP_CMD);
}
}
/*
* csid_isr - CSID module interrupt service routine
* @irq: Interrupt line
* @dev: CSID device
*
* Return IRQ_HANDLED on success
*/
static irqreturn_t csid_isr(int irq, void *dev)
{
struct csid_device *csid = dev;
u32 val, buf_done_val;
u8 reset_done;
int i;
val = readl(csid->base + CSID_TOP_IRQ_STATUS);
writel(val, csid->base + CSID_TOP_IRQ_CLEAR);
reset_done = val & TOP_IRQ_STATUS_RESET_DONE;
val = readl(csid->base + CSID_CSI2_RX_IRQ_STATUS);
writel(val, csid->base + CSID_CSI2_RX_IRQ_CLEAR);
buf_done_val = readl(csid->base + CSID_BUF_DONE_IRQ_STATUS);
writel(buf_done_val, csid->base + CSID_BUF_DONE_IRQ_CLEAR);
/* Read and clear IRQ status for each enabled RDI channel */
for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++)
if (csid->phy.en_vc & BIT(i)) {
val = readl(csid->base + CSID_CSI2_RDIN_IRQ_STATUS(i));
writel(val, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(i));
if (val & RUP_DONE_IRQ_STATUS)
/* clear the reg update bit */
csid_subdev_reg_update(csid, i, true);
if (buf_done_val & BIT(BUF_DONE_IRQ_STATUS_RDI_OFFSET + i)) {
/*
* For Titan 780, bus done and RUP IRQ have been moved to
* CSID from VFE. Once CSID received bus done, need notify
* VFE of this event. Trigger VFE to handle bus done process.
*/
camss_buf_done(csid->camss, csid->id, i);
}
}
val = IRQ_CMD_CLEAR;
writel(val, csid->base + CSID_IRQ_CMD);
if (reset_done)
complete(&csid->reset_complete);
return IRQ_HANDLED;
}
/*
* csid_reset - Trigger reset on CSID module and wait to complete
* @csid: CSID device
*
* Return 0 on success or a negative error code otherwise
*/
static int csid_reset(struct csid_device *csid)
{
unsigned long time;
u32 val;
int i;
reinit_completion(&csid->reset_complete);
writel(1, csid->base + CSID_TOP_IRQ_CLEAR);
writel(1, csid->base + CSID_IRQ_CMD);
writel(1, csid->base + CSID_TOP_IRQ_MASK);
for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++)
if (csid->phy.en_vc & BIT(i)) {
writel(BIT(BUF_DONE_IRQ_STATUS_RDI_OFFSET + i),
csid->base + CSID_BUF_DONE_IRQ_CLEAR);
writel(IRQ_CMD_CLEAR, csid->base + CSID_IRQ_CMD);
writel(BIT(BUF_DONE_IRQ_STATUS_RDI_OFFSET + i),
csid->base + CSID_BUF_DONE_IRQ_MASK);
}
/* preserve registers */
val = RST_LOCATION | RST_MODE;
writel(val, csid->base + CSID_RST_CFG);
val = SELECT_HW_RST | SELECT_IRQ_RST;
writel(val, csid->base + CSID_RST_CMD);
time = wait_for_completion_timeout(&csid->reset_complete,
msecs_to_jiffies(CSID_RESET_TIMEOUT_MS));
if (!time) {
dev_err(csid->camss->dev, "CSID reset timeout\n");
return -EIO;
}
return 0;
}
static void csid_subdev_init(struct csid_device *csid)
{
csid->testgen.nmodes = CSID_PAYLOAD_MODE_DISABLED;
}
const struct csid_hw_ops csid_ops_780 = {
.configure_stream = csid_configure_stream,
.configure_testgen_pattern = csid_configure_testgen_pattern,
.hw_version = csid_hw_version,
.isr = csid_isr,
.reset = csid_reset,
.src_pad_code = csid_src_pad_code,
.subdev_init = csid_subdev_init,
.reg_update = csid_subdev_reg_update,
};
|