Debian Codegraph @

drivers/media/platform/synopsys/hdmirx/snps_hdmirx.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2025 Collabora, Ltd.
4 * Author: Shreeya Patel <[email protected]>
5 * Author: Dmitry Osipenko <[email protected]>
6 *
7 * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
8 * Author: Dingxian Wen <[email protected]>
9 */
10
11 #include <linux/clk.h>
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/hdmi.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/math64.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/of_reserved_mem.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/platform_device.h>
27 #include <linux/property.h>
28 #include <linux/regmap.h>
29 #include <linux/reset.h>
30 #include <linux/v4l2-dv-timings.h>
31 #include <linux/workqueue.h>
32
33 #include <media/cec.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ctrls.h>
36 #include <media/v4l2-device.h>
37 #include <media/v4l2-dv-timings.h>
38 #include <media/v4l2-event.h>
39 #include <media/v4l2-fh.h>
40 #include <media/v4l2-ioctl.h>
41 #include <media/videobuf2-dma-contig.h>
42 #include <media/videobuf2-v4l2.h>
43
44 #include "snps_hdmirx.h"
45 #include "snps_hdmirx_cec.h"
46
47 #define EDID_NUM_BLOCKS_MAX 4
48 #define EDID_BLOCK_SIZE 128
49 #define HDMIRX_PLANE_Y 0
50 #define HDMIRX_PLANE_CBCR 1
51 #define FILTER_FRAME_CNT 6
52
53 static int debug;
54 module_param(debug, int, 0644);
55 MODULE_PARM_DESC(debug, "debug level (0-3)");
56
57 enum hdmirx_pix_fmt {
58 HDMIRX_RGB888 = 0,
59 HDMIRX_YUV422 = 1,
60 HDMIRX_YUV444 = 2,
61 HDMIRX_YUV420 = 3,
62 };
63
64 enum ddr_store_fmt {
65 STORE_RGB888 = 0,
66 STORE_RGBA_ARGB,
67 STORE_YUV420_8BIT,
68 STORE_YUV420_10BIT,
69 STORE_YUV422_8BIT,
70 STORE_YUV422_10BIT,
71 STORE_YUV444_8BIT,
72 STORE_YUV420_16BIT = 8,
73 STORE_YUV422_16BIT = 9,
74 };
75
76 enum hdmirx_reg_attr {
77 HDMIRX_ATTR_RW = 0,
78 HDMIRX_ATTR_RO = 1,
79 HDMIRX_ATTR_WO = 2,
80 HDMIRX_ATTR_RE = 3,
81 };
82
83 enum {
84 HDMIRX_RST_A,
85 HDMIRX_RST_P,
86 HDMIRX_RST_REF,
87 HDMIRX_RST_BIU,
88 HDMIRX_NUM_RST,
89 };
90
91 static const char *const pix_fmt_str[] = {
92 "RGB888",
93 "YUV422",
94 "YUV444",
95 "YUV420",
96 };
97
98 struct hdmirx_buffer {
99 struct vb2_v4l2_buffer vb;
100 struct list_head queue;
101 u32 buff_addr[VIDEO_MAX_PLANES];
102 };
103
104 struct hdmirx_stream {
105 struct snps_hdmirx_dev *hdmirx_dev;
106 struct video_device vdev;
107 struct vb2_queue buf_queue;
108 struct list_head buf_head;
109 struct hdmirx_buffer *curr_buf;
110 struct hdmirx_buffer *next_buf;
111 struct v4l2_pix_format_mplane pixm;
112 const struct v4l2_format_info *out_finfo;
113 struct mutex vlock; /* to lock resources associated with video buffer and video device */
114 spinlock_t vbq_lock; /* to lock video buffer queue */
115 bool stopping;
116 wait_queue_head_t wq_stopped;
117 u32 sequence;
118 u32 line_flag_int_cnt;
119 u32 irq_stat;
120 };
121
122 struct snps_hdmirx_dev {
123 struct device *dev;
124 struct hdmirx_stream stream;
125 struct v4l2_device v4l2_dev;
126 struct v4l2_ctrl_handler hdl;
127 struct v4l2_ctrl *detect_tx_5v_ctrl;
128 struct v4l2_ctrl *rgb_range;
129 struct v4l2_ctrl *content_type;
130 struct v4l2_dv_timings timings;
131 struct gpio_desc *detect_5v_gpio;
132 struct delayed_work delayed_work_hotplug;
133 struct delayed_work delayed_work_res_change;
134 struct hdmirx_cec *cec;
135 struct mutex phy_rw_lock; /* to protect phy r/w configuration */
136 struct mutex stream_lock; /* to lock video stream capture */
137 struct mutex work_lock; /* to lock the critical section of hotplug event */
138 struct reset_control_bulk_data resets[HDMIRX_NUM_RST];
139 struct clk_bulk_data *clks;
140 struct regmap *grf;
141 struct regmap *vo1_grf;
142 struct completion cr_read_done;
143 struct completion cr_write_done;
144 struct completion timer_base_lock;
145 struct completion avi_pkt_rcv;
146 struct dentry *debugfs_dir;
147 struct v4l2_debugfs_if *infoframes;
148 enum hdmirx_pix_fmt pix_fmt;
149 void __iomem *regs;
150 int hdmi_irq;
151 int dma_irq;
152 int det_irq;
153 bool hpd_trigger_level_high;
154 bool tmds_clk_ratio;
155 bool plugged;
156 int num_clks;
157 u32 edid_blocks_written;
158 u32 cur_fmt_fourcc;
159 u32 color_depth;
160 spinlock_t rst_lock; /* to lock register access */
161 u8 edid[EDID_NUM_BLOCKS_MAX * EDID_BLOCK_SIZE];
162 };
163
164 static const struct v4l2_dv_timings cea640x480 = V4L2_DV_BT_CEA_640X480P59_94;
165
166 static const struct v4l2_dv_timings_cap hdmirx_timings_cap = {
167 .type = V4L2_DV_BT_656_1120,
168 .reserved = { 0 },
169 V4L2_INIT_BT_TIMINGS(640, 4096, /* min/max width */
170 480, 2160, /* min/max height */
171 20000000, 600000000, /* min/max pixelclock */
172 /* standards */
173 V4L2_DV_BT_STD_CEA861,
174 /* capabilities */
175 V4L2_DV_BT_CAP_PROGRESSIVE |
176 V4L2_DV_BT_CAP_INTERLACED)
177 };
178
179 static void hdmirx_writel(struct snps_hdmirx_dev *hdmirx_dev, int reg, u32 val)
180 {
181 guard(spinlock_irqsave)(&hdmirx_dev->rst_lock);
182
183 writel(val, hdmirx_dev->regs + reg);
184 }
185
186 static u32 hdmirx_readl(struct snps_hdmirx_dev *hdmirx_dev, int reg)
187 {
188 guard(spinlock_irqsave)(&hdmirx_dev->rst_lock);
189
190 return readl(hdmirx_dev->regs + reg);
191 }
192
193 static void hdmirx_reset_dma(struct snps_hdmirx_dev *hdmirx_dev)
194 {
195 guard(spinlock_irqsave)(&hdmirx_dev->rst_lock);
196
197 reset_control_reset(hdmirx_dev->resets[0].rstc);
198 }
199
200 static void hdmirx_update_bits(struct snps_hdmirx_dev *hdmirx_dev, int reg,
201 u32 mask, u32 data)
202 {
203 u32 val;
204
205 guard(spinlock_irqsave)(&hdmirx_dev->rst_lock);
206
207 val = readl(hdmirx_dev->regs + reg) & ~mask;
208 val |= (data & mask);
209 writel(val, hdmirx_dev->regs + reg);
210 }
211
212 static int hdmirx_subscribe_event(struct v4l2_fh *fh,
213 const struct v4l2_event_subscription *sub)
214 {
215 switch (sub->type) {
216 case V4L2_EVENT_SOURCE_CHANGE:
217 return v4l2_src_change_event_subscribe(fh, sub);
218 case V4L2_EVENT_CTRL:
219 return v4l2_ctrl_subscribe_event(fh, sub);
220 default:
221 break;
222 }
223
224 return -EINVAL;
225 }
226
227 static bool tx_5v_power_present(struct snps_hdmirx_dev *hdmirx_dev)
228 {
229 const unsigned int detection_threshold = 7;
230 int val, i, cnt = 0;
231 bool ret;
232
233 for (i = 0; i < 10; i++) {
234 usleep_range(1000, 1100);
235 val = gpiod_get_value_cansleep(hdmirx_dev->detect_5v_gpio);
236 if (val > 0)
237 cnt++;
238 if (cnt >= detection_threshold)
239 break;
240 }
241
242 ret = cnt >= detection_threshold;
243 v4l2_dbg(3, debug, &hdmirx_dev->v4l2_dev, "%s: %d\n", __func__, ret);
244
245 return ret;
246 }
247
248 static bool signal_not_lock(struct snps_hdmirx_dev *hdmirx_dev)
249 {
250 u32 mu_status, dma_st10, cmu_st;
251
252 mu_status = hdmirx_readl(hdmirx_dev, MAINUNIT_STATUS);
253 dma_st10 = hdmirx_readl(hdmirx_dev, DMA_STATUS10);
254 cmu_st = hdmirx_readl(hdmirx_dev, CMU_STATUS);
255
256 if ((mu_status & TMDSVALID_STABLE_ST) &&
257 (dma_st10 & HDMIRX_LOCK) &&
258 (cmu_st & TMDSQPCLK_LOCKED_ST))
259 return false;
260
261 return true;
262 }
263
264 static void hdmirx_get_timings(struct snps_hdmirx_dev *hdmirx_dev,
265 struct v4l2_bt_timings *bt)
266 {
267 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
268 u32 hact, vact, htotal, vtotal, fps;
269 u32 hfp, hs, hbp, vfp, vs, vbp;
270 u32 val;
271
272 val = hdmirx_readl(hdmirx_dev, DMA_STATUS2);
273 hact = (val >> 16) & 0xffff;
274 vact = val & 0xffff;
275
276 val = hdmirx_readl(hdmirx_dev, DMA_STATUS3);
277 htotal = (val >> 16) & 0xffff;
278 vtotal = val & 0xffff;
279
280 val = hdmirx_readl(hdmirx_dev, DMA_STATUS4);
281 hs = (val >> 16) & 0xffff;
282 vs = val & 0xffff;
283
284 val = hdmirx_readl(hdmirx_dev, DMA_STATUS5);
285 hbp = (val >> 16) & 0xffff;
286 vbp = val & 0xffff;
287
288 if (hdmirx_dev->pix_fmt == HDMIRX_YUV420) {
289 htotal *= 2;
290 hbp *= 2;
291 hs *= 2;
292 }
293
294 hfp = htotal - hact - hs - hbp;
295 vfp = vtotal - vact - vs - vbp;
296
297 fps = div_u64(bt->pixelclock + (htotal * vtotal) / 2, htotal * vtotal);
298 bt->width = hact;
299 bt->height = vact;
300 bt->hfrontporch = hfp;
301 bt->hsync = hs;
302 bt->hbackporch = hbp;
303 bt->vfrontporch = vfp;
304 bt->vsync = vs;
305 bt->vbackporch = vbp;
306
307 v4l2_dbg(1, debug, v4l2_dev, "get timings from dma\n");
308 v4l2_dbg(1, debug, v4l2_dev,
309 "act:%ux%u%s, total:%ux%u, fps:%u, pixclk:%llu\n",
310 bt->width, bt->height, bt->interlaced ? "i" : "p",
311 htotal, vtotal, fps, bt->pixelclock);
312
313 v4l2_dbg(2, debug, v4l2_dev,
314 "hfp:%u, hact:%u, hs:%u, hbp:%u, vfp:%u, vact:%u, vs:%u, vbp:%u\n",
315 bt->hfrontporch, hact, bt->hsync, bt->hbackporch,
316 bt->vfrontporch, vact, bt->vsync, bt->vbackporch);
317
318 if (bt->interlaced == V4L2_DV_INTERLACED) {
319 bt->height *= 2;
320 bt->il_vfrontporch = bt->vfrontporch;
321 bt->il_vsync = bt->vsync + 1;
322 bt->il_vbackporch = bt->vbackporch;
323 }
324 }
325
326 static bool hdmirx_check_timing_valid(struct v4l2_bt_timings *bt)
327 {
328 /*
329 * Sanity-check timing values. Some of the values will be outside
330 * of a valid range till hardware becomes ready to perform capture.
331 */
332 if (bt->width < 100 || bt->width > 5000 ||
333 bt->height < 100 || bt->height > 5000)
334 return false;
335
336 if (!bt->hsync || bt->hsync > 200 ||
337 !bt->vsync || bt->vsync > 100)
338 return false;
339
340 /*
341 * According to the CEA-861, 1280x720p25 Hblank timing is up to 2680,
342 * and all standard video format timings are less than 3000.
343 */
344 if (!bt->hbackporch || bt->hbackporch > 3000 ||
345 !bt->vbackporch || bt->vbackporch > 3000)
346 return false;
347
348 if (!bt->hfrontporch || bt->hfrontporch > 3000 ||
349 !bt->vfrontporch || bt->vfrontporch > 3000)
350 return false;
351
352 return true;
353 }
354
355 static void hdmirx_toggle_polarity(struct snps_hdmirx_dev *hdmirx_dev)
356 {
357 u32 val = hdmirx_readl(hdmirx_dev, DMA_CONFIG6);
358
359 if (!(val & (VSYNC_TOGGLE_EN | HSYNC_TOGGLE_EN))) {
360 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6,
361 VSYNC_TOGGLE_EN | HSYNC_TOGGLE_EN,
362 VSYNC_TOGGLE_EN | HSYNC_TOGGLE_EN);
363 hdmirx_update_bits(hdmirx_dev, VIDEO_CONFIG2,
364 VPROC_VSYNC_POL_OVR_VALUE |
365 VPROC_VSYNC_POL_OVR_EN |
366 VPROC_HSYNC_POL_OVR_VALUE |
367 VPROC_HSYNC_POL_OVR_EN,
368 VPROC_VSYNC_POL_OVR_EN |
369 VPROC_HSYNC_POL_OVR_EN);
370 return;
371 }
372
373 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6,
374 VSYNC_TOGGLE_EN | HSYNC_TOGGLE_EN, 0);
375
376 hdmirx_update_bits(hdmirx_dev, VIDEO_CONFIG2,
377 VPROC_VSYNC_POL_OVR_VALUE |
378 VPROC_VSYNC_POL_OVR_EN |
379 VPROC_HSYNC_POL_OVR_VALUE |
380 VPROC_HSYNC_POL_OVR_EN, 0);
381 }
382
383 /*
384 * When querying DV timings during preview, if the DMA's timing is stable,
385 * we retrieve the timings directly from the DMA. However, if the current
386 * resolution is negative, obtaining the timing from CTRL may require a
387 * change in the sync polarity, potentially leading to DMA errors.
388 */
389 static int hdmirx_get_detected_timings(struct snps_hdmirx_dev *hdmirx_dev,
390 struct v4l2_dv_timings *timings)
391 {
392 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
393 struct v4l2_bt_timings *bt = &timings->bt;
394 u32 val, tmdsqpclk_freq, pix_clk;
395 unsigned int num_retries = 0;
396 u32 field_type, deframer_st;
397 u64 tmp_data, tmds_clk;
398 bool is_dvi_mode;
399 int ret;
400
401 mutex_lock(&hdmirx_dev->work_lock);
402 retry:
403 memset(timings, 0, sizeof(struct v4l2_dv_timings));
404 timings->type = V4L2_DV_BT_656_1120;
405
406 val = hdmirx_readl(hdmirx_dev, DMA_STATUS11);
407 field_type = (val & HDMIRX_TYPE_MASK) >> 7;
408
409 if (field_type & BIT(0))
410 bt->interlaced = V4L2_DV_INTERLACED;
411 else
412 bt->interlaced = V4L2_DV_PROGRESSIVE;
413
414 deframer_st = hdmirx_readl(hdmirx_dev, DEFRAMER_STATUS);
415 is_dvi_mode = !(deframer_st & OPMODE_STS_MASK);
416
417 tmdsqpclk_freq = hdmirx_readl(hdmirx_dev, CMU_TMDSQPCLK_FREQ);
418 tmds_clk = tmdsqpclk_freq * 4 * 1000;
419 tmp_data = tmds_clk * 24;
420 do_div(tmp_data, hdmirx_dev->color_depth);
421 pix_clk = tmp_data;
422 bt->pixelclock = pix_clk;
423
424 if (hdmirx_dev->pix_fmt == HDMIRX_YUV420)
425 bt->pixelclock *= 2;
426
427 hdmirx_get_timings(hdmirx_dev, bt);
428
429 v4l2_dbg(2, debug, v4l2_dev, "tmds_clk:%llu, pix_clk:%d\n", tmds_clk, pix_clk);
430 v4l2_dbg(1, debug, v4l2_dev, "interlace:%d, fmt:%d, color:%d, mode:%s\n",
431 bt->interlaced, hdmirx_dev->pix_fmt,
432 hdmirx_dev->color_depth,
433 is_dvi_mode ? "dvi" : "hdmi");
434 v4l2_dbg(2, debug, v4l2_dev, "deframer_st:%#x\n", deframer_st);
435
436 /*
437 * Timing will be invalid until it's latched by HW or if signal's
438 * polarity doesn't match.
439 */
440 if (!hdmirx_check_timing_valid(bt)) {
441 if (num_retries++ < 20) {
442 if (num_retries == 10)
443 hdmirx_toggle_polarity(hdmirx_dev);
444
445 usleep_range(10 * 1000, 10 * 1100);
446 goto retry;
447 }
448
449 ret = -ERANGE;
450 } else {
451 ret = 0;
452 }
453
454 mutex_unlock(&hdmirx_dev->work_lock);
455
456 return ret;
457 }
458
459 static bool port_no_link(struct snps_hdmirx_dev *hdmirx_dev)
460 {
461 return !tx_5v_power_present(hdmirx_dev);
462 }
463
464 static int hdmirx_query_dv_timings(struct file *file, void *priv,
465 struct v4l2_dv_timings *timings)
466 {
467 struct hdmirx_stream *stream = video_drvdata(file);
468 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
469 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
470 int ret;
471
472 if (port_no_link(hdmirx_dev)) {
473 v4l2_err(v4l2_dev, "%s: port has no link\n", __func__);
474 return -ENOLINK;
475 }
476
477 if (signal_not_lock(hdmirx_dev)) {
478 v4l2_err(v4l2_dev, "%s: signal is not locked\n", __func__);
479 return -ENOLCK;
480 }
481
482 ret = hdmirx_get_detected_timings(hdmirx_dev, timings);
483 if (ret)
484 return ret;
485
486 if (debug)
487 v4l2_print_dv_timings(hdmirx_dev->v4l2_dev.name,
488 "query_dv_timings: ", timings, false);
489
490 if (!v4l2_valid_dv_timings(timings, &hdmirx_timings_cap, NULL, NULL)) {
491 v4l2_dbg(1, debug, v4l2_dev, "%s: timings out of range\n", __func__);
492 return -ERANGE;
493 }
494
495 return 0;
496 }
497
498 static void hdmirx_hpd_ctrl(struct snps_hdmirx_dev *hdmirx_dev, bool en)
499 {
500 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
501
502 v4l2_dbg(1, debug, v4l2_dev, "%s: %sable, hpd_trigger_level_high:%d\n",
503 __func__, en ? "en" : "dis", hdmirx_dev->hpd_trigger_level_high);
504
505 hdmirx_update_bits(hdmirx_dev, SCDC_CONFIG, HPDLOW, en ? 0 : HPDLOW);
506 hdmirx_writel(hdmirx_dev, CORE_CONFIG,
507 hdmirx_dev->hpd_trigger_level_high ? en : !en);
508
509 /* 100ms delay as per HDMI spec + extra 50ms to cover internal delay */
510 if (!en)
511 msleep(100 + 50);
512 }
513
514 static void hdmirx_write_edid_data(struct snps_hdmirx_dev *hdmirx_dev,
515 u8 *edid, unsigned int num_blocks)
516 {
517 static u8 data[EDID_NUM_BLOCKS_MAX * EDID_BLOCK_SIZE];
518 unsigned int edid_len = num_blocks * EDID_BLOCK_SIZE;
519 unsigned int i;
520
521 cec_s_phys_addr_from_edid(hdmirx_dev->cec->adap,
522 (const struct edid *)edid);
523
524 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG11,
525 EDID_READ_EN_MASK |
526 EDID_WRITE_EN_MASK |
527 EDID_SLAVE_ADDR_MASK,
528 EDID_READ_EN(0) |
529 EDID_WRITE_EN(1) |
530 EDID_SLAVE_ADDR(0x50));
531 for (i = 0; i < edid_len; i++)
532 hdmirx_writel(hdmirx_dev, DMA_CONFIG10, edid[i]);
533
534 /* read out for debug */
535 if (debug >= 2) {
536 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG11,
537 EDID_READ_EN_MASK |
538 EDID_WRITE_EN_MASK,
539 EDID_READ_EN(1) |
540 EDID_WRITE_EN(0));
541
542 for (i = 0; i < edid_len; i++)
543 data[i] = hdmirx_readl(hdmirx_dev, DMA_STATUS14);
544
545 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, data,
546 edid_len, false);
547 }
548
549 /*
550 * Must set EDID_READ_EN & EDID_WRITE_EN bit to 0,
551 * when the read/write edid operation is completed. Otherwise, it
552 * will affect the reading and writing of other registers.
553 */
554 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG11,
555 EDID_READ_EN_MASK | EDID_WRITE_EN_MASK,
556 EDID_READ_EN(0) | EDID_WRITE_EN(0));
557 }
558
559 static void hdmirx_write_edid(struct snps_hdmirx_dev *hdmirx_dev,
560 struct v4l2_edid *edid)
561 {
562 memset(edid->reserved, 0, sizeof(edid->reserved));
563 memset(hdmirx_dev->edid, 0, sizeof(hdmirx_dev->edid));
564
565 hdmirx_write_edid_data(hdmirx_dev, edid->edid, edid->blocks);
566
567 hdmirx_dev->edid_blocks_written = edid->blocks;
568 memcpy(hdmirx_dev->edid, edid->edid, edid->blocks * EDID_BLOCK_SIZE);
569 }
570
571 /*
572 * Before clearing interrupt, we need to read the interrupt status.
573 */
574 static inline void hdmirx_clear_interrupt(struct snps_hdmirx_dev *hdmirx_dev,
575 u32 reg, u32 val)
576 {
577 /* (interrupt status register) = (interrupt clear register) - 0x8 */
578 hdmirx_readl(hdmirx_dev, reg - 0x8);
579 hdmirx_writel(hdmirx_dev, reg, val);
580 }
581
582 static void hdmirx_interrupts_setup(struct snps_hdmirx_dev *hdmirx_dev, bool en)
583 {
584 v4l2_dbg(1, debug, &hdmirx_dev->v4l2_dev, "%s: %sable\n",
585 __func__, en ? "en" : "dis");
586
587 disable_irq(hdmirx_dev->hdmi_irq);
588
589 /* Note: In DVI mode, it needs to be written twice to take effect. */
590 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_0_INT_CLEAR, 0xffffffff);
591 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
592 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_0_INT_CLEAR, 0xffffffff);
593 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
594 hdmirx_clear_interrupt(hdmirx_dev, AVPUNIT_0_INT_CLEAR, 0xffffffff);
595
596 if (en) {
597 hdmirx_update_bits(hdmirx_dev, MAINUNIT_0_INT_MASK_N,
598 TMDSQPCLK_OFF_CHG | TMDSQPCLK_LOCKED_CHG,
599 TMDSQPCLK_OFF_CHG | TMDSQPCLK_LOCKED_CHG);
600 hdmirx_update_bits(hdmirx_dev, MAINUNIT_2_INT_MASK_N,
601 TMDSVALID_STABLE_CHG, TMDSVALID_STABLE_CHG);
602 hdmirx_update_bits(hdmirx_dev, AVPUNIT_0_INT_MASK_N,
603 CED_DYN_CNT_CH2_IRQ |
604 CED_DYN_CNT_CH1_IRQ |
605 CED_DYN_CNT_CH0_IRQ,
606 CED_DYN_CNT_CH2_IRQ |
607 CED_DYN_CNT_CH1_IRQ |
608 CED_DYN_CNT_CH0_IRQ);
609 } else {
610 hdmirx_writel(hdmirx_dev, MAINUNIT_0_INT_MASK_N, 0);
611 hdmirx_writel(hdmirx_dev, MAINUNIT_2_INT_MASK_N, 0);
612 hdmirx_writel(hdmirx_dev, AVPUNIT_0_INT_MASK_N, 0);
613 }
614
615 enable_irq(hdmirx_dev->hdmi_irq);
616 }
617
618 static void hdmirx_plugout(struct snps_hdmirx_dev *hdmirx_dev)
619 {
620 if (!hdmirx_dev->plugged)
621 return;
622
623 hdmirx_update_bits(hdmirx_dev, SCDC_CONFIG, POWERPROVIDED, 0);
624 hdmirx_interrupts_setup(hdmirx_dev, false);
625 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, HDMIRX_DMA_EN, 0);
626 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG4,
627 LINE_FLAG_INT_EN |
628 HDMIRX_DMA_IDLE_INT |
629 HDMIRX_LOCK_DISABLE_INT |
630 LAST_FRAME_AXI_UNFINISH_INT_EN |
631 FIFO_OVERFLOW_INT_EN |
632 FIFO_UNDERFLOW_INT_EN |
633 HDMIRX_AXI_ERROR_INT_EN, 0);
634 hdmirx_reset_dma(hdmirx_dev);
635 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, HDMI_DISABLE | PHY_RESET |
636 PHY_PDDQ, HDMI_DISABLE);
637 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG0, 0x0);
638 cancel_delayed_work(&hdmirx_dev->delayed_work_res_change);
639
640 /* will be NULL on driver removal */
641 if (hdmirx_dev->rgb_range)
642 v4l2_ctrl_s_ctrl(hdmirx_dev->rgb_range, V4L2_DV_RGB_RANGE_AUTO);
643
644 if (hdmirx_dev->content_type)
645 v4l2_ctrl_s_ctrl(hdmirx_dev->content_type,
646 V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
647
648 hdmirx_dev->plugged = false;
649 }
650
651 static int hdmirx_set_edid(struct file *file, void *fh, struct v4l2_edid *edid)
652 {
653 struct hdmirx_stream *stream = video_drvdata(file);
654 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
655 u16 phys_addr;
656 int err;
657
658 if (edid->pad)
659 return -EINVAL;
660
661 if (edid->start_block)
662 return -EINVAL;
663
664 if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
665 edid->blocks = EDID_NUM_BLOCKS_MAX;
666 return -E2BIG;
667 }
668
669 if (edid->blocks) {
670 phys_addr = cec_get_edid_phys_addr(edid->edid,
671 edid->blocks * EDID_BLOCK_SIZE,
672 NULL);
673
674 err = v4l2_phys_addr_validate(phys_addr, &phys_addr, NULL);
675 if (err)
676 return err;
677 }
678
679 /*
680 * Touching HW registers in parallel with plugin/out handlers
681 * will bring hardware into a bad state.
682 */
683 mutex_lock(&hdmirx_dev->work_lock);
684
685 hdmirx_hpd_ctrl(hdmirx_dev, false);
686
687 if (edid->blocks) {
688 hdmirx_write_edid(hdmirx_dev, edid);
689 hdmirx_hpd_ctrl(hdmirx_dev, true);
690 } else {
691 cec_phys_addr_invalidate(hdmirx_dev->cec->adap);
692 hdmirx_dev->edid_blocks_written = 0;
693 }
694
695 mutex_unlock(&hdmirx_dev->work_lock);
696
697 return 0;
698 }
699
700 static int hdmirx_get_edid(struct file *file, void *fh, struct v4l2_edid *edid)
701 {
702 struct hdmirx_stream *stream = video_drvdata(file);
703 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
704 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
705
706 memset(edid->reserved, 0, sizeof(edid->reserved));
707
708 if (edid->pad)
709 return -EINVAL;
710
711 if (!edid->start_block && !edid->blocks) {
712 edid->blocks = hdmirx_dev->edid_blocks_written;
713 return 0;
714 }
715
716 if (!hdmirx_dev->edid_blocks_written)
717 return -ENODATA;
718
719 if (edid->start_block >= hdmirx_dev->edid_blocks_written || !edid->blocks)
720 return -EINVAL;
721
722 if (edid->start_block + edid->blocks > hdmirx_dev->edid_blocks_written)
723 edid->blocks = hdmirx_dev->edid_blocks_written - edid->start_block;
724
725 memcpy(edid->edid, hdmirx_dev->edid, edid->blocks * EDID_BLOCK_SIZE);
726
727 v4l2_dbg(1, debug, v4l2_dev, "%s: read EDID:\n", __func__);
728 if (debug > 0)
729 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
730 edid->edid, edid->blocks * EDID_BLOCK_SIZE, false);
731
732 return 0;
733 }
734
735 static int hdmirx_g_parm(struct file *file, void *priv,
736 struct v4l2_streamparm *parm)
737 {
738 struct hdmirx_stream *stream = video_drvdata(file);
739 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
740
741 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
742 return -EINVAL;
743
744 parm->parm.capture.timeperframe = v4l2_calc_timeperframe(&hdmirx_dev->timings);
745
746 return 0;
747 }
748
749 static int hdmirx_dv_timings_cap(struct file *file, void *fh,
750 struct v4l2_dv_timings_cap *cap)
751 {
752 *cap = hdmirx_timings_cap;
753 return 0;
754 }
755
756 static int hdmirx_enum_dv_timings(struct file *file, void *priv,
757 struct v4l2_enum_dv_timings *timings)
758 {
759 return v4l2_enum_dv_timings_cap(timings, &hdmirx_timings_cap, NULL, NULL);
760 }
761
762 static void hdmirx_scdc_init(struct snps_hdmirx_dev *hdmirx_dev)
763 {
764 hdmirx_update_bits(hdmirx_dev, I2C_SLAVE_CONFIG1,
765 I2C_SDA_OUT_HOLD_VALUE_QST_MASK |
766 I2C_SDA_IN_HOLD_VALUE_QST_MASK,
767 I2C_SDA_OUT_HOLD_VALUE_QST(0x80) |
768 I2C_SDA_IN_HOLD_VALUE_QST(0x15));
769 hdmirx_update_bits(hdmirx_dev, SCDC_REGBANK_CONFIG0,
770 SCDC_SINKVERSION_QST_MASK,
771 SCDC_SINKVERSION_QST(1));
772 }
773
774 static int wait_reg_bit_status(struct snps_hdmirx_dev *hdmirx_dev, u32 reg,
775 u32 bit_mask, u32 expect_val, bool is_grf,
776 u32 ms)
777 {
778 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
779 u32 i, val;
780
781 for (i = 0; i < ms; i++) {
782 if (is_grf)
783 regmap_read(hdmirx_dev->grf, reg, &val);
784 else
785 val = hdmirx_readl(hdmirx_dev, reg);
786
787 if ((val & bit_mask) == expect_val) {
788 v4l2_dbg(2, debug, v4l2_dev,
789 "%s: i:%d, time: %dms\n", __func__, i, ms);
790 break;
791 }
792 usleep_range(1000, 1010);
793 }
794
795 if (i == ms)
796 return -1;
797
798 return 0;
799 }
800
801 static int hdmirx_phy_register_read(struct snps_hdmirx_dev *hdmirx_dev,
802 u32 phy_reg, u32 *val)
803 {
804 struct device *dev = hdmirx_dev->dev;
805 u32 status;
806
807 guard(mutex)(&hdmirx_dev->phy_rw_lock);
808
809 reinit_completion(&hdmirx_dev->cr_read_done);
810 /* clear irq status */
811 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
812 /* en irq */
813 hdmirx_update_bits(hdmirx_dev, MAINUNIT_2_INT_MASK_N,
814 PHYCREG_CR_READ_DONE, PHYCREG_CR_READ_DONE);
815 /* write phy reg addr */
816 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG1, phy_reg);
817 /* config read enable */
818 hdmirx_writel(hdmirx_dev, PHYCREG_CONTROL, PHYCREG_CR_PARA_READ_P);
819
820 if (!wait_for_completion_timeout(&hdmirx_dev->cr_read_done,
821 msecs_to_jiffies(20))) {
822 dev_err(dev, "%s wait cr read done failed\n", __func__);
823 return -ETIMEDOUT;
824 }
825
826 /* read phy reg value */
827 status = hdmirx_readl(hdmirx_dev, PHYCREG_STATUS);
828 if (!(status & PHYCREG_CR_PARA_DATAVALID)) {
829 dev_err(dev, "%s cr read failed\n", __func__);
830 return -EINVAL;
831 }
832
833 *val = status & PHYCREG_CR_PARA_RD_DATA_MASK;
834
835 return 0;
836 }
837
838 static int hdmirx_phy_register_write(struct snps_hdmirx_dev *hdmirx_dev,
839 u32 phy_reg, u32 val)
840 {
841 struct device *dev = hdmirx_dev->dev;
842
843 guard(mutex)(&hdmirx_dev->phy_rw_lock);
844
845 reinit_completion(&hdmirx_dev->cr_write_done);
846 /* clear irq status */
847 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
848 /* en irq */
849 hdmirx_update_bits(hdmirx_dev, MAINUNIT_2_INT_MASK_N,
850 PHYCREG_CR_WRITE_DONE, PHYCREG_CR_WRITE_DONE);
851 /* write phy reg addr */
852 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG1, phy_reg);
853 /* write phy reg val */
854 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG2, val);
855 /* config write enable */
856 hdmirx_writel(hdmirx_dev, PHYCREG_CONTROL, PHYCREG_CR_PARA_WRITE_P);
857
858 if (!wait_for_completion_timeout(&hdmirx_dev->cr_write_done,
859 msecs_to_jiffies(20))) {
860 dev_err(dev, "%s wait cr write done failed\n", __func__);
861 return -1;
862 }
863
864 return 0;
865 }
866
867 static void hdmirx_tmds_clk_ratio_config(struct snps_hdmirx_dev *hdmirx_dev)
868 {
869 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
870 u32 val;
871
872 val = hdmirx_readl(hdmirx_dev, SCDC_REGBANK_STATUS1);
873 v4l2_dbg(3, debug, v4l2_dev, "%s: scdc_regbank_st:%#x\n", __func__, val);
874 hdmirx_dev->tmds_clk_ratio = (val & SCDC_TMDSBITCLKRATIO) > 0;
875
876 if (hdmirx_dev->tmds_clk_ratio) {
877 v4l2_dbg(3, debug, v4l2_dev, "%s: HDMITX greater than 3.4Gbps\n", __func__);
878 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG,
879 TMDS_CLOCK_RATIO, TMDS_CLOCK_RATIO);
880 } else {
881 v4l2_dbg(3, debug, v4l2_dev, "%s: HDMITX less than 3.4Gbps\n", __func__);
882 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG,
883 TMDS_CLOCK_RATIO, 0);
884 }
885 }
886
887 static void hdmirx_phy_config(struct snps_hdmirx_dev *hdmirx_dev)
888 {
889 struct device *dev = hdmirx_dev->dev;
890
891 hdmirx_clear_interrupt(hdmirx_dev, SCDC_INT_CLEAR, 0xffffffff);
892 hdmirx_update_bits(hdmirx_dev, SCDC_INT_MASK_N, SCDCTMDSCCFG_CHG,
893 SCDCTMDSCCFG_CHG);
894 /* cr_para_clk 24M */
895 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, REFFREQ_SEL_MASK, REFFREQ_SEL(0));
896 /* rx data width 40bit valid */
897 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, RXDATA_WIDTH, RXDATA_WIDTH);
898 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, PHY_RESET, PHY_RESET);
899 usleep_range(100, 110);
900 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, PHY_RESET, 0);
901 usleep_range(100, 110);
902 /* select cr para interface */
903 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG0, 0x3);
904
905 if (wait_reg_bit_status(hdmirx_dev, SYS_GRF_SOC_STATUS1,
906 HDMIRXPHY_SRAM_INIT_DONE,
907 HDMIRXPHY_SRAM_INIT_DONE, true, 10))
908 dev_err(dev, "%s: phy SRAM init failed\n", __func__);
909
910 regmap_write(hdmirx_dev->grf, SYS_GRF_SOC_CON1,
911 (HDMIRXPHY_SRAM_EXT_LD_DONE << 16) |
912 HDMIRXPHY_SRAM_EXT_LD_DONE);
913 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 2);
914 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 3);
915 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 2);
916 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 2);
917 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 3);
918 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 2);
919 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 0);
920 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 1);
921 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 0);
922 hdmirx_phy_register_write(hdmirx_dev, SUP_DIG_ANA_CREGS_SUP_ANA_NC, 0);
923
924 hdmirx_phy_register_write(hdmirx_dev,
925 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_RATE_CALC_HDMI14_CDR_SETTING_3_REG,
926 CDR_SETTING_BOUNDARY_3_DEFAULT);
927 hdmirx_phy_register_write(hdmirx_dev,
928 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_RATE_CALC_HDMI14_CDR_SETTING_4_REG,
929 CDR_SETTING_BOUNDARY_4_DEFAULT);
930 hdmirx_phy_register_write(hdmirx_dev,
931 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_RATE_CALC_HDMI14_CDR_SETTING_5_REG,
932 CDR_SETTING_BOUNDARY_5_DEFAULT);
933 hdmirx_phy_register_write(hdmirx_dev,
934 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_RATE_CALC_HDMI14_CDR_SETTING_6_REG,
935 CDR_SETTING_BOUNDARY_6_DEFAULT);
936 hdmirx_phy_register_write(hdmirx_dev,
937 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_RATE_CALC_HDMI14_CDR_SETTING_7_REG,
938 CDR_SETTING_BOUNDARY_7_DEFAULT);
939
940 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, PHY_PDDQ, 0);
941 if (wait_reg_bit_status(hdmirx_dev, PHY_STATUS, PDDQ_ACK, 0, false, 10))
942 dev_err(dev, "%s: wait pddq ack failed\n", __func__);
943
944 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, HDMI_DISABLE, 0);
945 if (wait_reg_bit_status(hdmirx_dev, PHY_STATUS, HDMI_DISABLE_ACK, 0,
946 false, 50))
947 dev_err(dev, "%s: wait hdmi disable ack failed\n", __func__);
948
949 hdmirx_tmds_clk_ratio_config(hdmirx_dev);
950 }
951
952 static void hdmirx_controller_init(struct snps_hdmirx_dev *hdmirx_dev)
953 {
954 const unsigned long iref_clk_freq_hz = 428571429;
955 struct device *dev = hdmirx_dev->dev;
956
957 reinit_completion(&hdmirx_dev->timer_base_lock);
958 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_0_INT_CLEAR, 0xffffffff);
959 /* en irq */
960 hdmirx_update_bits(hdmirx_dev, MAINUNIT_0_INT_MASK_N,
961 TIMER_BASE_LOCKED_IRQ, TIMER_BASE_LOCKED_IRQ);
962 /* write irefclk freq */
963 hdmirx_writel(hdmirx_dev, GLOBAL_TIMER_REF_BASE, iref_clk_freq_hz);
964
965 if (!wait_for_completion_timeout(&hdmirx_dev->timer_base_lock,
966 msecs_to_jiffies(20)))
967 dev_err(dev, "%s wait timer base lock failed\n", __func__);
968
969 hdmirx_update_bits(hdmirx_dev, CMU_CONFIG0,
970 TMDSQPCLK_STABLE_FREQ_MARGIN_MASK |
971 AUDCLK_STABLE_FREQ_MARGIN_MASK,
972 TMDSQPCLK_STABLE_FREQ_MARGIN(2) |
973 AUDCLK_STABLE_FREQ_MARGIN(1));
974 hdmirx_update_bits(hdmirx_dev, DESCRAND_EN_CONTROL,
975 SCRAMB_EN_SEL_QST_MASK, SCRAMB_EN_SEL_QST(1));
976 hdmirx_update_bits(hdmirx_dev, CED_CONFIG,
977 CED_VIDDATACHECKEN_QST |
978 CED_DATAISCHECKEN_QST |
979 CED_GBCHECKEN_QST |
980 CED_CTRLCHECKEN_QST |
981 CED_CHLOCKMAXER_QST_MASK,
982 CED_VIDDATACHECKEN_QST |
983 CED_GBCHECKEN_QST |
984 CED_CTRLCHECKEN_QST |
985 CED_CHLOCKMAXER_QST(0x10));
986 hdmirx_update_bits(hdmirx_dev, DEFRAMER_CONFIG0,
987 VS_REMAPFILTER_EN_QST | VS_FILTER_ORDER_QST_MASK,
988 VS_REMAPFILTER_EN_QST | VS_FILTER_ORDER_QST(0x3));
989 }
990
991 static void hdmirx_get_colordepth(struct snps_hdmirx_dev *hdmirx_dev)
992 {
993 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
994 u32 val, color_depth_reg;
995
996 val = hdmirx_readl(hdmirx_dev, DMA_STATUS11);
997 color_depth_reg = (val & HDMIRX_COLOR_DEPTH_MASK) >> 3;
998
999 switch (color_depth_reg) {
1000 case 0x4:
1001 hdmirx_dev->color_depth = 24;
1002 break;
1003 case 0x5:
1004 hdmirx_dev->color_depth = 30;
1005 break;
1006 case 0x6:
1007 hdmirx_dev->color_depth = 36;
1008 break;
1009 case 0x7:
1010 hdmirx_dev->color_depth = 48;
1011 break;
1012 default:
1013 hdmirx_dev->color_depth = 24;
1014 break;
1015 }
1016
1017 v4l2_dbg(1, debug, v4l2_dev, "%s: color_depth: %d, reg_val:%d\n",
1018 __func__, hdmirx_dev->color_depth, color_depth_reg);
1019 }
1020
1021 static void hdmirx_get_pix_fmt(struct snps_hdmirx_dev *hdmirx_dev)
1022 {
1023 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1024 u32 val;
1025
1026 val = hdmirx_readl(hdmirx_dev, DMA_STATUS11);
1027 hdmirx_dev->pix_fmt = val & HDMIRX_FORMAT_MASK;
1028
1029 switch (hdmirx_dev->pix_fmt) {
1030 case HDMIRX_RGB888:
1031 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_BGR24;
1032 break;
1033 case HDMIRX_YUV422:
1034 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_NV16;
1035 break;
1036 case HDMIRX_YUV444:
1037 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_NV24;
1038 break;
1039 case HDMIRX_YUV420:
1040 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_NV12;
1041 break;
1042 default:
1043 v4l2_err(v4l2_dev,
1044 "%s: err pix_fmt: %d, set RGB888 as default\n",
1045 __func__, hdmirx_dev->pix_fmt);
1046 hdmirx_dev->pix_fmt = HDMIRX_RGB888;
1047 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_BGR24;
1048 break;
1049 }
1050
1051 v4l2_dbg(1, debug, v4l2_dev, "%s: pix_fmt: %s\n", __func__,
1052 pix_fmt_str[hdmirx_dev->pix_fmt]);
1053 }
1054
1055 static void hdmirx_read_avi_infoframe(struct snps_hdmirx_dev *hdmirx_dev,
1056 u8 *aviif)
1057 {
1058 unsigned int i, b, itr = 0;
1059 u32 val;
1060
1061 aviif[itr++] = HDMI_INFOFRAME_TYPE_AVI;
1062 val = hdmirx_readl(hdmirx_dev, PKTDEC_AVIIF_PH2_1);
1063 aviif[itr++] = val & 0xff;
1064 aviif[itr++] = (val >> 8) & 0xff;
1065
1066 for (i = 0; i < 7; i++) {
1067 val = hdmirx_readl(hdmirx_dev, PKTDEC_AVIIF_PB3_0 + 4 * i);
1068
1069 for (b = 0; b < 4; b++)
1070 aviif[itr++] = (val >> (8 * b)) & 0xff;
1071 }
1072 }
1073
1074 static void hdmirx_get_avi_infoframe(struct snps_hdmirx_dev *hdmirx_dev)
1075 {
1076 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1077 union hdmi_infoframe frame = {};
1078 u8 aviif[3 + 7 * 4];
1079 int err;
1080
1081 hdmirx_read_avi_infoframe(hdmirx_dev, aviif);
1082
1083 err = hdmi_infoframe_unpack(&frame, aviif, sizeof(aviif));
1084 if (err) {
1085 v4l2_err(v4l2_dev, "failed to unpack AVI infoframe\n");
1086 return;
1087 }
1088
1089 v4l2_ctrl_s_ctrl(hdmirx_dev->rgb_range, frame.avi.quantization_range);
1090
1091 if (frame.avi.itc)
1092 v4l2_ctrl_s_ctrl(hdmirx_dev->content_type,
1093 frame.avi.content_type);
1094 else
1095 v4l2_ctrl_s_ctrl(hdmirx_dev->content_type,
1096 V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
1097 }
1098
1099 static ssize_t
1100 hdmirx_debugfs_if_read(u32 type, void *priv, struct file *filp,
1101 char __user *ubuf, size_t count, loff_t *ppos)
1102 {
1103 struct snps_hdmirx_dev *hdmirx_dev = priv;
1104 u8 aviif[V4L2_DEBUGFS_IF_MAX_LEN] = {};
1105 int len;
1106
1107 if (type != V4L2_DEBUGFS_IF_AVI)
1108 return 0;
1109
1110 hdmirx_read_avi_infoframe(hdmirx_dev, aviif);
1111
1112 len = aviif[2] + 4;
1113 if (len > V4L2_DEBUGFS_IF_MAX_LEN)
1114 len = -ENOENT;
1115 else
1116 len = simple_read_from_buffer(ubuf, count, ppos, aviif, len);
1117
1118 return len < 0 ? 0 : len;
1119 }
1120
1121 static void hdmirx_format_change(struct snps_hdmirx_dev *hdmirx_dev)
1122 {
1123 struct hdmirx_stream *stream = &hdmirx_dev->stream;
1124 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1125 static const struct v4l2_event ev_src_chg = {
1126 .type = V4L2_EVENT_SOURCE_CHANGE,
1127 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1128 };
1129
1130 hdmirx_get_pix_fmt(hdmirx_dev);
1131 hdmirx_get_colordepth(hdmirx_dev);
1132 hdmirx_get_avi_infoframe(hdmirx_dev);
1133
1134 v4l2_dbg(1, debug, v4l2_dev, "%s: queue res_chg_event\n", __func__);
1135 v4l2_event_queue(&stream->vdev, &ev_src_chg);
1136 }
1137
1138 static void hdmirx_set_ddr_store_fmt(struct snps_hdmirx_dev *hdmirx_dev)
1139 {
1140 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1141 enum ddr_store_fmt store_fmt;
1142 u32 dma_cfg1;
1143
1144 switch (hdmirx_dev->pix_fmt) {
1145 case HDMIRX_RGB888:
1146 store_fmt = STORE_RGB888;
1147 break;
1148 case HDMIRX_YUV444:
1149 store_fmt = STORE_YUV444_8BIT;
1150 break;
1151 case HDMIRX_YUV422:
1152 store_fmt = STORE_YUV422_8BIT;
1153 break;
1154 case HDMIRX_YUV420:
1155 store_fmt = STORE_YUV420_8BIT;
1156 break;
1157 default:
1158 store_fmt = STORE_RGB888;
1159 break;
1160 }
1161
1162 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG1,
1163 DDR_STORE_FORMAT_MASK, DDR_STORE_FORMAT(store_fmt));
1164 dma_cfg1 = hdmirx_readl(hdmirx_dev, DMA_CONFIG1);
1165 v4l2_dbg(1, debug, v4l2_dev, "%s: pix_fmt: %s, DMA_CONFIG1:%#x\n",
1166 __func__, pix_fmt_str[hdmirx_dev->pix_fmt], dma_cfg1);
1167 }
1168
1169 static void hdmirx_dma_config(struct snps_hdmirx_dev *hdmirx_dev)
1170 {
1171 hdmirx_set_ddr_store_fmt(hdmirx_dev);
1172
1173 /* Note: uv_swap, rb can not swap, doc err */
1174 if (hdmirx_dev->cur_fmt_fourcc != V4L2_PIX_FMT_NV16)
1175 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, RB_SWAP_EN, RB_SWAP_EN);
1176 else
1177 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, RB_SWAP_EN, 0);
1178
1179 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG7,
1180 LOCK_FRAME_NUM_MASK,
1181 LOCK_FRAME_NUM(2));
1182 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG1,
1183 UV_WID_MASK | Y_WID_MASK | ABANDON_EN,
1184 UV_WID(1) | Y_WID(2) | ABANDON_EN);
1185 }
1186
1187 static void hdmirx_submodule_init(struct snps_hdmirx_dev *hdmirx_dev)
1188 {
1189 /* Note: if not config HDCP2_CONFIG, there will be some errors; */
1190 hdmirx_update_bits(hdmirx_dev, HDCP2_CONFIG,
1191 HDCP2_SWITCH_OVR_VALUE |
1192 HDCP2_SWITCH_OVR_EN,
1193 HDCP2_SWITCH_OVR_EN);
1194 hdmirx_scdc_init(hdmirx_dev);
1195 hdmirx_controller_init(hdmirx_dev);
1196 }
1197
1198 static int hdmirx_enum_input(struct file *file, void *priv,
1199 struct v4l2_input *input)
1200 {
1201 if (input->index > 0)
1202 return -EINVAL;
1203
1204 input->type = V4L2_INPUT_TYPE_CAMERA;
1205 input->std = 0;
1206 strscpy(input->name, "HDMI IN", sizeof(input->name));
1207 input->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1208
1209 return 0;
1210 }
1211
1212 static int hdmirx_get_input(struct file *file, void *priv, unsigned int *i)
1213 {
1214 *i = 0;
1215 return 0;
1216 }
1217
1218 static int hdmirx_set_input(struct file *file, void *priv, unsigned int i)
1219 {
1220 if (i)
1221 return -EINVAL;
1222 return 0;
1223 }
1224
1225 static void hdmirx_set_fmt(struct hdmirx_stream *stream,
1226 struct v4l2_pix_format_mplane *pixm, bool try)
1227 {
1228 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1229 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1230 struct v4l2_bt_timings *bt = &hdmirx_dev->timings.bt;
1231 const struct v4l2_format_info *finfo;
1232 unsigned int imagesize = 0;
1233 unsigned int i;
1234
1235 memset(&pixm->plane_fmt[0], 0, sizeof(struct v4l2_plane_pix_format));
1236 finfo = v4l2_format_info(pixm->pixelformat);
1237 if (!finfo) {
1238 finfo = v4l2_format_info(V4L2_PIX_FMT_BGR24);
1239 v4l2_dbg(1, debug, v4l2_dev,
1240 "%s: set_fmt:%#x not supported, use def_fmt:%x\n",
1241 __func__, pixm->pixelformat, finfo->format);
1242 }
1243
1244 if (!bt->width || !bt->height)
1245 v4l2_dbg(1, debug, v4l2_dev, "%s: invalid resolution:%#xx%#x\n",
1246 __func__, bt->width, bt->height);
1247
1248 pixm->pixelformat = finfo->format;
1249 pixm->width = bt->width;
1250 pixm->height = bt->height;
1251 pixm->num_planes = finfo->mem_planes;
1252 pixm->quantization = V4L2_QUANTIZATION_DEFAULT;
1253 pixm->colorspace = V4L2_COLORSPACE_SRGB;
1254 pixm->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1255
1256 if (bt->interlaced == V4L2_DV_INTERLACED)
1257 pixm->field = V4L2_FIELD_INTERLACED_TB;
1258 else
1259 pixm->field = V4L2_FIELD_NONE;
1260
1261 memset(pixm->reserved, 0, sizeof(pixm->reserved));
1262
1263 v4l2_fill_pixfmt_mp(pixm, finfo->format, pixm->width, pixm->height);
1264
1265 for (i = 0; i < finfo->comp_planes; i++) {
1266 struct v4l2_plane_pix_format *plane_fmt;
1267 int width, height, bpl, size, bpp = 0;
1268 const unsigned int hw_align = 64;
1269
1270 if (!i) {
1271 width = pixm->width;
1272 height = pixm->height;
1273 } else {
1274 width = pixm->width / finfo->hdiv;
1275 height = pixm->height / finfo->vdiv;
1276 }
1277
1278 switch (finfo->format) {
1279 case V4L2_PIX_FMT_NV24:
1280 case V4L2_PIX_FMT_NV16:
1281 case V4L2_PIX_FMT_NV12:
1282 case V4L2_PIX_FMT_BGR24:
1283 bpp = finfo->bpp[i];
1284 break;
1285 default:
1286 v4l2_dbg(1, debug, v4l2_dev,
1287 "fourcc: %#x is not supported\n",
1288 finfo->format);
1289 break;
1290 }
1291
1292 bpl = ALIGN(width * bpp, hw_align);
1293 size = bpl * height;
1294 imagesize += size;
1295
1296 if (finfo->mem_planes > i) {
1297 /* Set bpl and size for each mplane */
1298 plane_fmt = pixm->plane_fmt + i;
1299 plane_fmt->bytesperline = bpl;
1300 plane_fmt->sizeimage = size;
1301 }
1302
1303 v4l2_dbg(1, debug, v4l2_dev,
1304 "C-Plane %u size: %d, Total imagesize: %d\n",
1305 i, size, imagesize);
1306 }
1307
1308 /* Convert to non-MPLANE format as we want to unify non-MPLANE and MPLANE */
1309 if (finfo->mem_planes == 1)
1310 pixm->plane_fmt[0].sizeimage = imagesize;
1311
1312 if (!try) {
1313 stream->out_finfo = finfo;
1314 stream->pixm = *pixm;
1315 v4l2_dbg(1, debug, v4l2_dev,
1316 "%s: req(%d, %d), out(%d, %d), fmt:%#x\n", __func__,
1317 pixm->width, pixm->height, stream->pixm.width,
1318 stream->pixm.height, finfo->format);
1319 }
1320 }
1321
1322 static int hdmirx_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
1323 struct v4l2_fmtdesc *f)
1324 {
1325 struct hdmirx_stream *stream = video_drvdata(file);
1326 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1327
1328 if (f->index >= 1)
1329 return -EINVAL;
1330
1331 f->pixelformat = hdmirx_dev->cur_fmt_fourcc;
1332
1333 return 0;
1334 }
1335
1336 static int hdmirx_s_fmt_vid_cap_mplane(struct file *file,
1337 void *priv, struct v4l2_format *f)
1338 {
1339 struct hdmirx_stream *stream = video_drvdata(file);
1340 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1341 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1342
1343 if (vb2_is_busy(&stream->buf_queue)) {
1344 v4l2_err(v4l2_dev, "%s: queue busy\n", __func__);
1345 return -EBUSY;
1346 }
1347
1348 hdmirx_set_fmt(stream, &f->fmt.pix_mp, false);
1349
1350 return 0;
1351 }
1352
1353 static int hdmirx_g_fmt_vid_cap_mplane(struct file *file, void *fh,
1354 struct v4l2_format *f)
1355 {
1356 struct hdmirx_stream *stream = video_drvdata(file);
1357 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1358 struct v4l2_pix_format_mplane pixm = {};
1359
1360 pixm.pixelformat = hdmirx_dev->cur_fmt_fourcc;
1361 hdmirx_set_fmt(stream, &pixm, true);
1362 f->fmt.pix_mp = pixm;
1363
1364 return 0;
1365 }
1366
1367 static int hdmirx_g_dv_timings(struct file *file, void *priv,
1368 struct v4l2_dv_timings *timings)
1369 {
1370 struct hdmirx_stream *stream = video_drvdata(file);
1371 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1372 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1373 u32 dma_cfg1;
1374
1375 *timings = hdmirx_dev->timings;
1376 dma_cfg1 = hdmirx_readl(hdmirx_dev, DMA_CONFIG1);
1377 v4l2_dbg(1, debug, v4l2_dev, "%s: pix_fmt: %s, DMA_CONFIG1:%#x\n",
1378 __func__, pix_fmt_str[hdmirx_dev->pix_fmt], dma_cfg1);
1379
1380 return 0;
1381 }
1382
1383 static int hdmirx_s_dv_timings(struct file *file, void *priv,
1384 struct v4l2_dv_timings *timings)
1385 {
1386 struct hdmirx_stream *stream = video_drvdata(file);
1387 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1388 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1389
1390 if (!timings)
1391 return -EINVAL;
1392
1393 if (debug)
1394 v4l2_print_dv_timings(hdmirx_dev->v4l2_dev.name,
1395 "s_dv_timings: ", timings, false);
1396
1397 if (!v4l2_valid_dv_timings(timings, &hdmirx_timings_cap, NULL, NULL)) {
1398 v4l2_dbg(1, debug, v4l2_dev,
1399 "%s: timings out of range\n", __func__);
1400 return -ERANGE;
1401 }
1402
1403 /* Check if the timings are part of the CEA-861 timings. */
1404 v4l2_find_dv_timings_cap(timings, &hdmirx_timings_cap, 0, NULL, NULL);
1405
1406 if (v4l2_match_dv_timings(&hdmirx_dev->timings, timings, 0, false)) {
1407 v4l2_dbg(1, debug, v4l2_dev, "%s: no change\n", __func__);
1408 return 0;
1409 }
1410
1411 /*
1412 * Changing the timings implies a format change, which is not allowed
1413 * while buffers for use with streaming have already been allocated.
1414 */
1415 if (vb2_is_busy(&stream->buf_queue))
1416 return -EBUSY;
1417
1418 hdmirx_dev->timings = *timings;
1419 /* Update the internal format */
1420 hdmirx_set_fmt(stream, &stream->pixm, false);
1421
1422 return 0;
1423 }
1424
1425 static int hdmirx_querycap(struct file *file, void *priv,
1426 struct v4l2_capability *cap)
1427 {
1428 struct hdmirx_stream *stream = video_drvdata(file);
1429 struct device *dev = stream->hdmirx_dev->dev;
1430
1431 strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));
1432 strscpy(cap->card, dev->driver->name, sizeof(cap->card));
1433
1434 return 0;
1435 }
1436
1437 static int hdmirx_queue_setup(struct vb2_queue *queue,
1438 unsigned int *num_buffers,
1439 unsigned int *num_planes,
1440 unsigned int sizes[],
1441 struct device *alloc_ctxs[])
1442 {
1443 struct hdmirx_stream *stream = vb2_get_drv_priv(queue);
1444 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1445 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1446 const struct v4l2_pix_format_mplane *pixm = NULL;
1447 const struct v4l2_format_info *out_finfo;
1448 u32 i;
1449
1450 pixm = &stream->pixm;
1451 out_finfo = stream->out_finfo;
1452
1453 if (!out_finfo) {
1454 v4l2_err(v4l2_dev, "%s: out_fmt not set\n", __func__);
1455 return -EINVAL;
1456 }
1457
1458 if (*num_planes) {
1459 if (*num_planes != pixm->num_planes)
1460 return -EINVAL;
1461
1462 for (i = 0; i < *num_planes; i++)
1463 if (sizes[i] < pixm->plane_fmt[i].sizeimage)
1464 return -EINVAL;
1465 return 0;
1466 }
1467
1468 *num_planes = out_finfo->mem_planes;
1469
1470 for (i = 0; i < out_finfo->mem_planes; i++)
1471 sizes[i] = pixm->plane_fmt[i].sizeimage;
1472
1473 v4l2_dbg(1, debug, v4l2_dev, "%s: count %d, size %d\n",
1474 v4l2_type_names[queue->type], *num_buffers, sizes[0]);
1475
1476 return 0;
1477 }
1478
1479 /*
1480 * The vb2_buffer are stored in hdmirx_buffer, in order to unify
1481 * mplane buffer and none-mplane buffer.
1482 */
1483 static void hdmirx_buf_queue(struct vb2_buffer *vb)
1484 {
1485 const struct v4l2_pix_format_mplane *pixm;
1486 const struct v4l2_format_info *out_finfo;
1487 struct hdmirx_buffer *hdmirx_buf;
1488 struct vb2_v4l2_buffer *vbuf;
1489 struct hdmirx_stream *stream;
1490 struct vb2_queue *queue;
1491 unsigned long flags;
1492 unsigned int i;
1493
1494 vbuf = to_vb2_v4l2_buffer(vb);
1495 hdmirx_buf = container_of(vbuf, struct hdmirx_buffer, vb);
1496 queue = vb->vb2_queue;
1497 stream = vb2_get_drv_priv(queue);
1498 pixm = &stream->pixm;
1499 out_finfo = stream->out_finfo;
1500
1501 memset(hdmirx_buf->buff_addr, 0, sizeof(hdmirx_buf->buff_addr));
1502
1503 /*
1504 * If mplanes > 1, every c-plane has its own m-plane,
1505 * otherwise, multiple c-planes are in the same m-plane
1506 */
1507 for (i = 0; i < out_finfo->mem_planes; i++)
1508 hdmirx_buf->buff_addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
1509
1510 if (out_finfo->mem_planes == 1) {
1511 if (out_finfo->comp_planes == 1) {
1512 hdmirx_buf->buff_addr[HDMIRX_PLANE_CBCR] =
1513 hdmirx_buf->buff_addr[HDMIRX_PLANE_Y];
1514 } else {
1515 for (i = 0; i < out_finfo->comp_planes - 1; i++)
1516 hdmirx_buf->buff_addr[i + 1] =
1517 hdmirx_buf->buff_addr[i] +
1518 pixm->plane_fmt[i].bytesperline *
1519 pixm->height;
1520 }
1521 }
1522
1523 spin_lock_irqsave(&stream->vbq_lock, flags);
1524 list_add_tail(&hdmirx_buf->queue, &stream->buf_head);
1525 spin_unlock_irqrestore(&stream->vbq_lock, flags);
1526 }
1527
1528 static void return_all_buffers(struct hdmirx_stream *stream,
1529 enum vb2_buffer_state state)
1530 {
1531 struct hdmirx_buffer *buf, *tmp;
1532 unsigned long flags;
1533
1534 spin_lock_irqsave(&stream->vbq_lock, flags);
1535 if (stream->curr_buf)
1536 list_add_tail(&stream->curr_buf->queue, &stream->buf_head);
1537 if (stream->next_buf && stream->next_buf != stream->curr_buf)
1538 list_add_tail(&stream->next_buf->queue, &stream->buf_head);
1539 stream->curr_buf = NULL;
1540 stream->next_buf = NULL;
1541
1542 list_for_each_entry_safe(buf, tmp, &stream->buf_head, queue) {
1543 list_del(&buf->queue);
1544 vb2_buffer_done(&buf->vb.vb2_buf, state);
1545 }
1546 spin_unlock_irqrestore(&stream->vbq_lock, flags);
1547 }
1548
1549 static void hdmirx_stop_streaming(struct vb2_queue *queue)
1550 {
1551 struct hdmirx_stream *stream = vb2_get_drv_priv(queue);
1552 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1553 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1554 int ret;
1555
1556 v4l2_dbg(1, debug, v4l2_dev, "stream start stopping\n");
1557 mutex_lock(&hdmirx_dev->stream_lock);
1558 WRITE_ONCE(stream->stopping, true);
1559
1560 /* wait last irq to return the buffer */
1561 ret = wait_event_timeout(stream->wq_stopped, !stream->stopping,
1562 msecs_to_jiffies(500));
1563 if (!ret)
1564 v4l2_dbg(1, debug, v4l2_dev, "%s: timeout waiting last irq\n",
1565 __func__);
1566
1567 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, HDMIRX_DMA_EN, 0);
1568 return_all_buffers(stream, VB2_BUF_STATE_ERROR);
1569 mutex_unlock(&hdmirx_dev->stream_lock);
1570 v4l2_dbg(1, debug, v4l2_dev, "stream stopping finished\n");
1571 }
1572
1573 static int hdmirx_start_streaming(struct vb2_queue *queue, unsigned int count)
1574 {
1575 struct hdmirx_stream *stream = vb2_get_drv_priv(queue);
1576 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1577 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1578 struct v4l2_dv_timings timings = hdmirx_dev->timings;
1579 struct v4l2_bt_timings *bt = &timings.bt;
1580 unsigned long lock_flags = 0;
1581 int line_flag;
1582
1583 mutex_lock(&hdmirx_dev->stream_lock);
1584 stream->sequence = 0;
1585 stream->line_flag_int_cnt = 0;
1586 stream->curr_buf = NULL;
1587 stream->next_buf = NULL;
1588 stream->irq_stat = 0;
1589
1590 WRITE_ONCE(stream->stopping, false);
1591
1592 spin_lock_irqsave(&stream->vbq_lock, lock_flags);
1593 if (!stream->curr_buf) {
1594 if (!list_empty(&stream->buf_head)) {
1595 stream->curr_buf = list_first_entry(&stream->buf_head,
1596 struct hdmirx_buffer,
1597 queue);
1598 list_del(&stream->curr_buf->queue);
1599 } else {
1600 stream->curr_buf = NULL;
1601 }
1602 }
1603 spin_unlock_irqrestore(&stream->vbq_lock, lock_flags);
1604
1605 if (!stream->curr_buf) {
1606 mutex_unlock(&hdmirx_dev->stream_lock);
1607 return -ENOMEM;
1608 }
1609
1610 v4l2_dbg(2, debug, v4l2_dev,
1611 "%s: start_stream cur_buf y_addr:%#x, uv_addr:%#x\n",
1612 __func__, stream->curr_buf->buff_addr[HDMIRX_PLANE_Y],
1613 stream->curr_buf->buff_addr[HDMIRX_PLANE_CBCR]);
1614 hdmirx_writel(hdmirx_dev, DMA_CONFIG2,
1615 stream->curr_buf->buff_addr[HDMIRX_PLANE_Y]);
1616 hdmirx_writel(hdmirx_dev, DMA_CONFIG3,
1617 stream->curr_buf->buff_addr[HDMIRX_PLANE_CBCR]);
1618
1619 if (bt->height) {
1620 if (bt->interlaced == V4L2_DV_INTERLACED)
1621 line_flag = bt->height / 4;
1622 else
1623 line_flag = bt->height / 2;
1624 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG7,
1625 LINE_FLAG_NUM_MASK,
1626 LINE_FLAG_NUM(line_flag));
1627 } else {
1628 v4l2_err(v4l2_dev, "invalid BT timing height=%d\n", bt->height);
1629 }
1630
1631 hdmirx_writel(hdmirx_dev, DMA_CONFIG5, 0xffffffff);
1632 hdmirx_writel(hdmirx_dev, CED_DYN_CONTROL, 0x1);
1633 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG4,
1634 LINE_FLAG_INT_EN |
1635 HDMIRX_DMA_IDLE_INT |
1636 HDMIRX_LOCK_DISABLE_INT |
1637 LAST_FRAME_AXI_UNFINISH_INT_EN |
1638 FIFO_OVERFLOW_INT_EN |
1639 FIFO_UNDERFLOW_INT_EN |
1640 HDMIRX_AXI_ERROR_INT_EN,
1641 LINE_FLAG_INT_EN |
1642 HDMIRX_DMA_IDLE_INT |
1643 HDMIRX_LOCK_DISABLE_INT |
1644 LAST_FRAME_AXI_UNFINISH_INT_EN |
1645 FIFO_OVERFLOW_INT_EN |
1646 FIFO_UNDERFLOW_INT_EN |
1647 HDMIRX_AXI_ERROR_INT_EN);
1648 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, HDMIRX_DMA_EN, HDMIRX_DMA_EN);
1649 v4l2_dbg(1, debug, v4l2_dev, "%s: enable dma", __func__);
1650 mutex_unlock(&hdmirx_dev->stream_lock);
1651
1652 return 0;
1653 }
1654
1655 /* vb2 queue */
1656 static const struct vb2_ops hdmirx_vb2_ops = {
1657 .queue_setup = hdmirx_queue_setup,
1658 .buf_queue = hdmirx_buf_queue,
1659 .stop_streaming = hdmirx_stop_streaming,
1660 .start_streaming = hdmirx_start_streaming,
1661 };
1662
1663 static int hdmirx_init_vb2_queue(struct vb2_queue *q,
1664 struct hdmirx_stream *stream,
1665 enum v4l2_buf_type buf_type)
1666 {
1667 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1668
1669 q->type = buf_type;
1670 q->io_modes = VB2_MMAP | VB2_DMABUF;
1671 q->drv_priv = stream;
1672 q->ops = &hdmirx_vb2_ops;
1673 q->mem_ops = &vb2_dma_contig_memops;
1674 q->buf_struct_size = sizeof(struct hdmirx_buffer);
1675 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1676 q->lock = &stream->vlock;
1677 q->dev = hdmirx_dev->dev;
1678 q->min_queued_buffers = 1;
1679
1680 return vb2_queue_init(q);
1681 }
1682
1683 /* video device */
1684 static const struct v4l2_ioctl_ops hdmirx_v4l2_ioctl_ops = {
1685 .vidioc_querycap = hdmirx_querycap,
1686 .vidioc_try_fmt_vid_cap_mplane = hdmirx_g_fmt_vid_cap_mplane,
1687 .vidioc_s_fmt_vid_cap_mplane = hdmirx_s_fmt_vid_cap_mplane,
1688 .vidioc_g_fmt_vid_cap_mplane = hdmirx_g_fmt_vid_cap_mplane,
1689 .vidioc_enum_fmt_vid_cap = hdmirx_enum_fmt_vid_cap_mplane,
1690
1691 .vidioc_s_dv_timings = hdmirx_s_dv_timings,
1692 .vidioc_g_dv_timings = hdmirx_g_dv_timings,
1693 .vidioc_enum_dv_timings = hdmirx_enum_dv_timings,
1694 .vidioc_query_dv_timings = hdmirx_query_dv_timings,
1695 .vidioc_dv_timings_cap = hdmirx_dv_timings_cap,
1696 .vidioc_enum_input = hdmirx_enum_input,
1697 .vidioc_g_input = hdmirx_get_input,
1698 .vidioc_s_input = hdmirx_set_input,
1699 .vidioc_g_edid = hdmirx_get_edid,
1700 .vidioc_s_edid = hdmirx_set_edid,
1701 .vidioc_g_parm = hdmirx_g_parm,
1702
1703 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1704 .vidioc_querybuf = vb2_ioctl_querybuf,
1705 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1706 .vidioc_qbuf = vb2_ioctl_qbuf,
1707 .vidioc_expbuf = vb2_ioctl_expbuf,
1708 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1709 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1710 .vidioc_streamon = vb2_ioctl_streamon,
1711 .vidioc_streamoff = vb2_ioctl_streamoff,
1712
1713 .vidioc_log_status = v4l2_ctrl_log_status,
1714 .vidioc_subscribe_event = hdmirx_subscribe_event,
1715 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1716 };
1717
1718 static const struct v4l2_file_operations hdmirx_fops = {
1719 .owner = THIS_MODULE,
1720 .open = v4l2_fh_open,
1721 .release = vb2_fop_release,
1722 .unlocked_ioctl = video_ioctl2,
1723 .poll = vb2_fop_poll,
1724 .mmap = vb2_fop_mmap,
1725 };
1726
1727 static int hdmirx_register_stream_vdev(struct hdmirx_stream *stream)
1728 {
1729 struct snps_hdmirx_dev *hdmirx_dev = stream->hdmirx_dev;
1730 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1731 struct video_device *vdev = &stream->vdev;
1732 int ret;
1733
1734 strscpy(vdev->name, "stream_hdmirx", sizeof(vdev->name));
1735 INIT_LIST_HEAD(&stream->buf_head);
1736 spin_lock_init(&stream->vbq_lock);
1737 mutex_init(&stream->vlock);
1738 init_waitqueue_head(&stream->wq_stopped);
1739 stream->curr_buf = NULL;
1740 stream->next_buf = NULL;
1741
1742 vdev->ioctl_ops = &hdmirx_v4l2_ioctl_ops;
1743 vdev->release = video_device_release_empty;
1744 vdev->fops = &hdmirx_fops;
1745 vdev->minor = -1;
1746 vdev->v4l2_dev = v4l2_dev;
1747 vdev->lock = &stream->vlock;
1748 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
1749 V4L2_CAP_STREAMING;
1750 vdev->vfl_dir = VFL_DIR_RX;
1751
1752 video_set_drvdata(vdev, stream);
1753
1754 hdmirx_init_vb2_queue(&stream->buf_queue, stream,
1755 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1756 vdev->queue = &stream->buf_queue;
1757
1758 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1759 if (ret < 0) {
1760 v4l2_err(v4l2_dev, "video_register_device failed: %d\n", ret);
1761 return ret;
1762 }
1763
1764 return 0;
1765 }
1766
1767 static void process_signal_change(struct snps_hdmirx_dev *hdmirx_dev)
1768 {
1769 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG6, HDMIRX_DMA_EN, 0);
1770 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG4,
1771 LINE_FLAG_INT_EN |
1772 HDMIRX_DMA_IDLE_INT |
1773 HDMIRX_LOCK_DISABLE_INT |
1774 LAST_FRAME_AXI_UNFINISH_INT_EN |
1775 FIFO_OVERFLOW_INT_EN |
1776 FIFO_UNDERFLOW_INT_EN |
1777 HDMIRX_AXI_ERROR_INT_EN, 0);
1778 hdmirx_reset_dma(hdmirx_dev);
1779 queue_delayed_work(system_unbound_wq,
1780 &hdmirx_dev->delayed_work_res_change,
1781 msecs_to_jiffies(50));
1782 }
1783
1784 static void avpunit_0_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1785 int status, bool *handled)
1786 {
1787 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1788
1789 if (status & (CED_DYN_CNT_CH2_IRQ |
1790 CED_DYN_CNT_CH1_IRQ |
1791 CED_DYN_CNT_CH0_IRQ)) {
1792 process_signal_change(hdmirx_dev);
1793 v4l2_dbg(2, debug, v4l2_dev, "%s: avp0_st:%#x\n",
1794 __func__, status);
1795 *handled = true;
1796 }
1797
1798 hdmirx_clear_interrupt(hdmirx_dev, AVPUNIT_0_INT_CLEAR, 0xffffffff);
1799 hdmirx_writel(hdmirx_dev, AVPUNIT_0_INT_FORCE, 0x0);
1800 }
1801
1802 static void avpunit_1_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1803 int status, bool *handled)
1804 {
1805 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1806
1807 if (status & DEFRAMER_VSYNC_THR_REACHED_IRQ) {
1808 v4l2_dbg(2, debug, v4l2_dev,
1809 "Vertical Sync threshold reached interrupt %#x", status);
1810 hdmirx_update_bits(hdmirx_dev, AVPUNIT_1_INT_MASK_N,
1811 DEFRAMER_VSYNC_THR_REACHED_MASK_N, 0);
1812 *handled = true;
1813 }
1814 }
1815
1816 static void mainunit_0_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1817 int status, bool *handled)
1818 {
1819 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1820
1821 v4l2_dbg(2, debug, v4l2_dev, "mu0_st:%#x\n", status);
1822 if (status & TIMER_BASE_LOCKED_IRQ) {
1823 hdmirx_update_bits(hdmirx_dev, MAINUNIT_0_INT_MASK_N,
1824 TIMER_BASE_LOCKED_IRQ, 0);
1825 complete(&hdmirx_dev->timer_base_lock);
1826 *handled = true;
1827 }
1828
1829 if (status & TMDSQPCLK_OFF_CHG) {
1830 process_signal_change(hdmirx_dev);
1831 v4l2_dbg(2, debug, v4l2_dev, "%s: TMDSQPCLK_OFF_CHG\n", __func__);
1832 *handled = true;
1833 }
1834
1835 if (status & TMDSQPCLK_LOCKED_CHG) {
1836 process_signal_change(hdmirx_dev);
1837 v4l2_dbg(2, debug, v4l2_dev, "%s: TMDSQPCLK_LOCKED_CHG\n", __func__);
1838 *handled = true;
1839 }
1840
1841 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_0_INT_CLEAR, 0xffffffff);
1842 hdmirx_writel(hdmirx_dev, MAINUNIT_0_INT_FORCE, 0x0);
1843 }
1844
1845 static void mainunit_2_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1846 int status, bool *handled)
1847 {
1848 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1849
1850 v4l2_dbg(2, debug, v4l2_dev, "mu2_st:%#x\n", status);
1851 if (status & PHYCREG_CR_WRITE_DONE) {
1852 hdmirx_update_bits(hdmirx_dev, MAINUNIT_2_INT_MASK_N,
1853 PHYCREG_CR_WRITE_DONE, 0);
1854 complete(&hdmirx_dev->cr_write_done);
1855 *handled = true;
1856 }
1857
1858 if (status & PHYCREG_CR_READ_DONE) {
1859 hdmirx_update_bits(hdmirx_dev, MAINUNIT_2_INT_MASK_N,
1860 PHYCREG_CR_READ_DONE, 0);
1861 complete(&hdmirx_dev->cr_read_done);
1862 *handled = true;
1863 }
1864
1865 if (status & TMDSVALID_STABLE_CHG) {
1866 process_signal_change(hdmirx_dev);
1867 v4l2_dbg(2, debug, v4l2_dev, "%s: TMDSVALID_STABLE_CHG\n", __func__);
1868 *handled = true;
1869 }
1870
1871 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
1872 hdmirx_writel(hdmirx_dev, MAINUNIT_2_INT_FORCE, 0x0);
1873 }
1874
1875 static void pkt_2_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1876 int status, bool *handled)
1877 {
1878 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1879
1880 v4l2_dbg(2, debug, v4l2_dev, "%s: pk2_st:%#x\n", __func__, status);
1881 if (status & PKTDEC_AVIIF_RCV_IRQ) {
1882 hdmirx_update_bits(hdmirx_dev, PKT_2_INT_MASK_N,
1883 PKTDEC_AVIIF_RCV_IRQ, 0);
1884 complete(&hdmirx_dev->avi_pkt_rcv);
1885 v4l2_dbg(2, debug, v4l2_dev, "%s: AVIIF_RCV_IRQ\n", __func__);
1886 *handled = true;
1887 }
1888
1889 hdmirx_clear_interrupt(hdmirx_dev, PKT_2_INT_CLEAR, 0xffffffff);
1890 }
1891
1892 static void scdc_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1893 int status, bool *handled)
1894 {
1895 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1896
1897 v4l2_dbg(2, debug, v4l2_dev, "%s: scdc_st:%#x\n", __func__, status);
1898 if (status & SCDCTMDSCCFG_CHG) {
1899 hdmirx_tmds_clk_ratio_config(hdmirx_dev);
1900 *handled = true;
1901 }
1902
1903 hdmirx_clear_interrupt(hdmirx_dev, SCDC_INT_CLEAR, 0xffffffff);
1904 }
1905
1906 static irqreturn_t hdmirx_hdmi_irq_handler(int irq, void *dev_id)
1907 {
1908 struct snps_hdmirx_dev *hdmirx_dev = dev_id;
1909 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1910 u32 mu0_st, mu2_st, pk2_st, scdc_st, avp1_st, avp0_st;
1911 u32 mu0_mask, mu2_mask, pk2_mask, scdc_mask, avp1_msk, avp0_msk;
1912 bool handled = false;
1913
1914 mu0_mask = hdmirx_readl(hdmirx_dev, MAINUNIT_0_INT_MASK_N);
1915 mu2_mask = hdmirx_readl(hdmirx_dev, MAINUNIT_2_INT_MASK_N);
1916 pk2_mask = hdmirx_readl(hdmirx_dev, PKT_2_INT_MASK_N);
1917 scdc_mask = hdmirx_readl(hdmirx_dev, SCDC_INT_MASK_N);
1918 mu0_st = hdmirx_readl(hdmirx_dev, MAINUNIT_0_INT_STATUS);
1919 mu2_st = hdmirx_readl(hdmirx_dev, MAINUNIT_2_INT_STATUS);
1920 pk2_st = hdmirx_readl(hdmirx_dev, PKT_2_INT_STATUS);
1921 scdc_st = hdmirx_readl(hdmirx_dev, SCDC_INT_STATUS);
1922 avp0_st = hdmirx_readl(hdmirx_dev, AVPUNIT_0_INT_STATUS);
1923 avp1_st = hdmirx_readl(hdmirx_dev, AVPUNIT_1_INT_STATUS);
1924 avp0_msk = hdmirx_readl(hdmirx_dev, AVPUNIT_0_INT_MASK_N);
1925 avp1_msk = hdmirx_readl(hdmirx_dev, AVPUNIT_1_INT_MASK_N);
1926 mu0_st &= mu0_mask;
1927 mu2_st &= mu2_mask;
1928 pk2_st &= pk2_mask;
1929 avp1_st &= avp1_msk;
1930 avp0_st &= avp0_msk;
1931 scdc_st &= scdc_mask;
1932
1933 if (avp0_st)
1934 avpunit_0_int_handler(hdmirx_dev, avp0_st, &handled);
1935 if (avp1_st)
1936 avpunit_1_int_handler(hdmirx_dev, avp1_st, &handled);
1937 if (mu0_st)
1938 mainunit_0_int_handler(hdmirx_dev, mu0_st, &handled);
1939 if (mu2_st)
1940 mainunit_2_int_handler(hdmirx_dev, mu2_st, &handled);
1941 if (pk2_st)
1942 pkt_2_int_handler(hdmirx_dev, pk2_st, &handled);
1943 if (scdc_st)
1944 scdc_int_handler(hdmirx_dev, scdc_st, &handled);
1945
1946 if (!handled) {
1947 v4l2_dbg(2, debug, v4l2_dev, "%s: hdmi irq not handled", __func__);
1948 v4l2_dbg(2, debug, v4l2_dev,
1949 "avp0:%#x, avp1:%#x, mu0:%#x, mu2:%#x, pk2:%#x, scdc:%#x\n",
1950 avp0_st, avp1_st, mu0_st, mu2_st, pk2_st, scdc_st);
1951 }
1952
1953 v4l2_dbg(2, debug, v4l2_dev, "%s: en_fiq", __func__);
1954
1955 return handled ? IRQ_HANDLED : IRQ_NONE;
1956 }
1957
1958 static void hdmirx_vb_done(struct hdmirx_stream *stream,
1959 struct vb2_v4l2_buffer *vb_done)
1960 {
1961 const struct v4l2_format_info *finfo = stream->out_finfo;
1962 u32 i;
1963
1964 /* Dequeue a filled buffer */
1965 for (i = 0; i < finfo->mem_planes; i++) {
1966 vb2_set_plane_payload(&vb_done->vb2_buf, i,
1967 stream->pixm.plane_fmt[i].sizeimage);
1968 }
1969
1970 vb_done->vb2_buf.timestamp = ktime_get_ns();
1971 vb2_buffer_done(&vb_done->vb2_buf, VB2_BUF_STATE_DONE);
1972 }
1973
1974 static void dma_idle_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
1975 bool *handled)
1976 {
1977 struct hdmirx_stream *stream = &hdmirx_dev->stream;
1978 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
1979 struct v4l2_dv_timings timings = hdmirx_dev->timings;
1980 struct v4l2_bt_timings *bt = &timings.bt;
1981 struct vb2_v4l2_buffer *vb_done = NULL;
1982
1983 if (!(stream->irq_stat) && !(stream->irq_stat & LINE_FLAG_INT_EN))
1984 v4l2_dbg(1, debug, v4l2_dev,
1985 "%s: last time have no line_flag_irq\n", __func__);
1986
1987 /* skip first frames that are expected to come out zeroed from DMA */
1988 if (stream->line_flag_int_cnt <= FILTER_FRAME_CNT)
1989 goto DMA_IDLE_OUT;
1990
1991 if (bt->interlaced != V4L2_DV_INTERLACED ||
1992 !(stream->line_flag_int_cnt % 2)) {
1993 if (stream->next_buf) {
1994 if (stream->curr_buf)
1995 vb_done = &stream->curr_buf->vb;
1996
1997 if (vb_done) {
1998 vb_done->vb2_buf.timestamp = ktime_get_ns();
1999 vb_done->sequence = stream->sequence;
2000
2001 if (bt->interlaced)
2002 vb_done->field = V4L2_FIELD_INTERLACED_TB;
2003 else
2004 vb_done->field = V4L2_FIELD_NONE;
2005
2006 hdmirx_vb_done(stream, vb_done);
2007 }
2008
2009 stream->curr_buf = NULL;
2010 if (stream->next_buf) {
2011 stream->curr_buf = stream->next_buf;
2012 stream->next_buf = NULL;
2013 }
2014 } else {
2015 v4l2_dbg(3, debug, v4l2_dev,
2016 "%s: next_buf NULL, skip vb_done\n", __func__);
2017 }
2018
2019 stream->sequence++;
2020 if (stream->sequence == 30)
2021 v4l2_dbg(1, debug, v4l2_dev, "rcv frames\n");
2022 }
2023
2024 DMA_IDLE_OUT:
2025 *handled = true;
2026 }
2027
2028 static void line_flag_int_handler(struct snps_hdmirx_dev *hdmirx_dev,
2029 bool *handled)
2030 {
2031 struct hdmirx_stream *stream = &hdmirx_dev->stream;
2032 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
2033 struct v4l2_dv_timings timings = hdmirx_dev->timings;
2034 struct v4l2_bt_timings *bt = &timings.bt;
2035 u32 dma_cfg6;
2036
2037 stream->line_flag_int_cnt++;
2038 if (!(stream->irq_stat) && !(stream->irq_stat & HDMIRX_DMA_IDLE_INT))
2039 v4l2_dbg(1, debug, v4l2_dev,
2040 "%s: last have no dma_idle_irq\n", __func__);
2041 dma_cfg6 = hdmirx_readl(hdmirx_dev, DMA_CONFIG6);
2042 if (!(dma_cfg6 & HDMIRX_DMA_EN)) {
2043 v4l2_dbg(2, debug, v4l2_dev, "%s: dma not on\n", __func__);
2044 goto LINE_FLAG_OUT;
2045 }
2046
2047 if (stream->line_flag_int_cnt <= FILTER_FRAME_CNT)
2048 goto LINE_FLAG_OUT;
2049
2050 if (bt->interlaced != V4L2_DV_INTERLACED ||
2051 !(stream->line_flag_int_cnt % 2)) {
2052 if (!stream->next_buf) {
2053 spin_lock(&stream->vbq_lock);
2054 if (!list_empty(&stream->buf_head)) {
2055 stream->next_buf = list_first_entry(&stream->buf_head,
2056 struct hdmirx_buffer,
2057 queue);
2058 list_del(&stream->next_buf->queue);
2059 } else {
2060 stream->next_buf = NULL;
2061 }
2062 spin_unlock(&stream->vbq_lock);
2063
2064 if (stream->next_buf) {
2065 hdmirx_writel(hdmirx_dev, DMA_CONFIG2,
2066 stream->next_buf->buff_addr[HDMIRX_PLANE_Y]);
2067 hdmirx_writel(hdmirx_dev, DMA_CONFIG3,
2068 stream->next_buf->buff_addr[HDMIRX_PLANE_CBCR]);
2069 } else {
2070 v4l2_dbg(3, debug, v4l2_dev,
2071 "%s: no buffer is available\n", __func__);
2072 }
2073 }
2074 } else {
2075 v4l2_dbg(3, debug, v4l2_dev, "%s: interlace:%d, line_flag_int_cnt:%d\n",
2076 __func__, bt->interlaced, stream->line_flag_int_cnt);
2077 }
2078
2079 LINE_FLAG_OUT:
2080 *handled = true;
2081 }
2082
2083 static irqreturn_t hdmirx_dma_irq_handler(int irq, void *dev_id)
2084 {
2085 struct snps_hdmirx_dev *hdmirx_dev = dev_id;
2086 struct hdmirx_stream *stream = &hdmirx_dev->stream;
2087 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
2088 u32 dma_stat1, dma_stat13;
2089 bool handled = false;
2090
2091 dma_stat1 = hdmirx_readl(hdmirx_dev, DMA_STATUS1);
2092 dma_stat13 = hdmirx_readl(hdmirx_dev, DMA_STATUS13);
2093 v4l2_dbg(3, debug, v4l2_dev, "dma_irq st1:%#x, st13:%d\n",
2094 dma_stat1, dma_stat13);
2095
2096 if (READ_ONCE(stream->stopping)) {
2097 v4l2_dbg(1, debug, v4l2_dev, "%s: stop stream\n", __func__);
2098 hdmirx_writel(hdmirx_dev, DMA_CONFIG5, 0xffffffff);
2099 hdmirx_update_bits(hdmirx_dev, DMA_CONFIG4,
2100 LINE_FLAG_INT_EN |
2101 HDMIRX_DMA_IDLE_INT |
2102 HDMIRX_LOCK_DISABLE_INT |
2103 LAST_FRAME_AXI_UNFINISH_INT_EN |
2104 FIFO_OVERFLOW_INT_EN |
2105 FIFO_UNDERFLOW_INT_EN |
2106 HDMIRX_AXI_ERROR_INT_EN, 0);
2107 WRITE_ONCE(stream->stopping, false);
2108 wake_up(&stream->wq_stopped);
2109 return IRQ_HANDLED;
2110 }
2111
2112 if (dma_stat1 & HDMIRX_DMA_IDLE_INT)
2113 dma_idle_int_handler(hdmirx_dev, &handled);
2114
2115 if (dma_stat1 & LINE_FLAG_INT_EN)
2116 line_flag_int_handler(hdmirx_dev, &handled);
2117
2118 if (!handled)
2119 v4l2_dbg(3, debug, v4l2_dev,
2120 "%s: dma irq not handled, dma_stat1:%#x\n",
2121 __func__, dma_stat1);
2122
2123 stream->irq_stat = dma_stat1;
2124 hdmirx_writel(hdmirx_dev, DMA_CONFIG5, 0xffffffff);
2125
2126 return IRQ_HANDLED;
2127 }
2128
2129 static int hdmirx_wait_signal_lock(struct snps_hdmirx_dev *hdmirx_dev)
2130 {
2131 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
2132 u32 mu_status, scdc_status, dma_st10, cmu_st;
2133 u32 i;
2134
2135 for (i = 0; i < 300; i++) {
2136 mu_status = hdmirx_readl(hdmirx_dev, MAINUNIT_STATUS);
2137 scdc_status = hdmirx_readl(hdmirx_dev, SCDC_REGBANK_STATUS3);
2138 dma_st10 = hdmirx_readl(hdmirx_dev, DMA_STATUS10);
2139 cmu_st = hdmirx_readl(hdmirx_dev, CMU_STATUS);
2140
2141 if ((mu_status & TMDSVALID_STABLE_ST) &&
2142 (dma_st10 & HDMIRX_LOCK) &&
2143 (cmu_st & TMDSQPCLK_LOCKED_ST))
2144 break;
2145
2146 if (!tx_5v_power_present(hdmirx_dev)) {
2147 v4l2_dbg(1, debug, v4l2_dev,
2148 "%s: HDMI pull out, return\n", __func__);
2149 return -1;
2150 }
2151
2152 hdmirx_tmds_clk_ratio_config(hdmirx_dev);
2153 }
2154
2155 if (i == 300) {
2156 v4l2_err(v4l2_dev, "%s: signal not lock, tmds_clk_ratio:%d\n",
2157 __func__, hdmirx_dev->tmds_clk_ratio);
2158 v4l2_err(v4l2_dev, "%s: mu_st:%#x, scdc_st:%#x, dma_st10:%#x\n",
2159 __func__, mu_status, scdc_status, dma_st10);
2160 return -1;
2161 }
2162
2163 v4l2_dbg(1, debug, v4l2_dev, "%s: signal lock ok, i:%d\n", __func__, i);
2164 hdmirx_writel(hdmirx_dev, GLOBAL_SWRESET_REQUEST, DATAPATH_SWRESETREQ);
2165
2166 reinit_completion(&hdmirx_dev->avi_pkt_rcv);
2167 hdmirx_clear_interrupt(hdmirx_dev, PKT_2_INT_CLEAR, 0xffffffff);
2168 hdmirx_update_bits(hdmirx_dev, PKT_2_INT_MASK_N,
2169 PKTDEC_AVIIF_RCV_IRQ, PKTDEC_AVIIF_RCV_IRQ);
2170
2171 if (!wait_for_completion_timeout(&hdmirx_dev->avi_pkt_rcv,
2172 msecs_to_jiffies(300))) {
2173 v4l2_err(v4l2_dev, "%s wait avi_pkt_rcv failed\n", __func__);
2174 hdmirx_update_bits(hdmirx_dev, PKT_2_INT_MASK_N,
2175 PKTDEC_AVIIF_RCV_IRQ, 0);
2176 }
2177
2178 msleep(50);
2179 hdmirx_format_change(hdmirx_dev);
2180
2181 return 0;
2182 }
2183
2184 static void hdmirx_plugin(struct snps_hdmirx_dev *hdmirx_dev)
2185 {
2186 if (hdmirx_dev->plugged)
2187 return;
2188
2189 hdmirx_submodule_init(hdmirx_dev);
2190 hdmirx_update_bits(hdmirx_dev, SCDC_CONFIG, POWERPROVIDED,
2191 POWERPROVIDED);
2192 hdmirx_phy_config(hdmirx_dev);
2193 hdmirx_interrupts_setup(hdmirx_dev, true);
2194
2195 hdmirx_dev->plugged = true;
2196 }
2197
2198 static void hdmirx_delayed_work_hotplug(struct work_struct *work)
2199 {
2200 struct snps_hdmirx_dev *hdmirx_dev;
2201 bool plugin;
2202
2203 hdmirx_dev = container_of(work, struct snps_hdmirx_dev,
2204 delayed_work_hotplug.work);
2205
2206 mutex_lock(&hdmirx_dev->work_lock);
2207 plugin = tx_5v_power_present(hdmirx_dev);
2208 v4l2_ctrl_s_ctrl(hdmirx_dev->detect_tx_5v_ctrl, plugin);
2209 v4l2_dbg(1, debug, &hdmirx_dev->v4l2_dev, "%s: plugin:%d\n",
2210 __func__, plugin);
2211
2212 hdmirx_plugout(hdmirx_dev);
2213
2214 if (plugin)
2215 hdmirx_plugin(hdmirx_dev);
2216
2217 mutex_unlock(&hdmirx_dev->work_lock);
2218 }
2219
2220 static void hdmirx_delayed_work_res_change(struct work_struct *work)
2221 {
2222 struct snps_hdmirx_dev *hdmirx_dev;
2223 bool plugin;
2224
2225 hdmirx_dev = container_of(work, struct snps_hdmirx_dev,
2226 delayed_work_res_change.work);
2227
2228 mutex_lock(&hdmirx_dev->work_lock);
2229 plugin = tx_5v_power_present(hdmirx_dev);
2230 v4l2_dbg(1, debug, &hdmirx_dev->v4l2_dev, "%s: plugin:%d\n",
2231 __func__, plugin);
2232 if (plugin) {
2233 hdmirx_interrupts_setup(hdmirx_dev, false);
2234 hdmirx_submodule_init(hdmirx_dev);
2235 hdmirx_update_bits(hdmirx_dev, SCDC_CONFIG, POWERPROVIDED,
2236 POWERPROVIDED);
2237 hdmirx_phy_config(hdmirx_dev);
2238
2239 if (hdmirx_wait_signal_lock(hdmirx_dev)) {
2240 hdmirx_plugout(hdmirx_dev);
2241 queue_delayed_work(system_unbound_wq,
2242 &hdmirx_dev->delayed_work_hotplug,
2243 msecs_to_jiffies(200));
2244 } else {
2245 hdmirx_dma_config(hdmirx_dev);
2246 hdmirx_interrupts_setup(hdmirx_dev, true);
2247 }
2248 }
2249 mutex_unlock(&hdmirx_dev->work_lock);
2250 }
2251
2252 static irqreturn_t hdmirx_5v_det_irq_handler(int irq, void *dev_id)
2253 {
2254 struct snps_hdmirx_dev *hdmirx_dev = dev_id;
2255
2256 queue_delayed_work(system_unbound_wq,
2257 &hdmirx_dev->delayed_work_hotplug,
2258 msecs_to_jiffies(10));
2259
2260 return IRQ_HANDLED;
2261 }
2262
2263 static const struct hdmirx_cec_ops hdmirx_cec_ops = {
2264 .write = hdmirx_writel,
2265 .read = hdmirx_readl,
2266 };
2267
2268 static void devm_hdmirx_of_reserved_mem_device_release(void *dev)
2269 {
2270 of_reserved_mem_device_release(dev);
2271 }
2272
2273 static int hdmirx_parse_dt(struct snps_hdmirx_dev *hdmirx_dev)
2274 {
2275 struct device *dev = hdmirx_dev->dev;
2276 int ret;
2277
2278 hdmirx_dev->num_clks = devm_clk_bulk_get_all(dev, &hdmirx_dev->clks);
2279 if (hdmirx_dev->num_clks < 1)
2280 return -ENODEV;
2281
2282 hdmirx_dev->resets[HDMIRX_RST_A].id = "axi";
2283 hdmirx_dev->resets[HDMIRX_RST_P].id = "apb";
2284 hdmirx_dev->resets[HDMIRX_RST_REF].id = "ref";
2285 hdmirx_dev->resets[HDMIRX_RST_BIU].id = "biu";
2286
2287 ret = devm_reset_control_bulk_get_exclusive(dev, HDMIRX_NUM_RST,
2288 hdmirx_dev->resets);
2289 if (ret < 0) {
2290 dev_err(dev, "failed to get reset controls\n");
2291 return ret;
2292 }
2293
2294 hdmirx_dev->detect_5v_gpio =
2295 devm_gpiod_get_optional(dev, "hpd", GPIOD_IN);
2296
2297 if (IS_ERR(hdmirx_dev->detect_5v_gpio)) {
2298 dev_err(dev, "failed to get hdmirx hot plug detection gpio\n");
2299 return PTR_ERR(hdmirx_dev->detect_5v_gpio);
2300 }
2301
2302 hdmirx_dev->grf = syscon_regmap_lookup_by_phandle(dev->of_node,
2303 "rockchip,grf");
2304 if (IS_ERR(hdmirx_dev->grf)) {
2305 dev_err(dev, "failed to get rockchip,grf\n");
2306 return PTR_ERR(hdmirx_dev->grf);
2307 }
2308
2309 hdmirx_dev->vo1_grf = syscon_regmap_lookup_by_phandle(dev->of_node,
2310 "rockchip,vo1-grf");
2311 if (IS_ERR(hdmirx_dev->vo1_grf)) {
2312 dev_err(dev, "failed to get rockchip,vo1-grf\n");
2313 return PTR_ERR(hdmirx_dev->vo1_grf);
2314 }
2315
2316 if (!device_property_read_bool(dev, "hpd-is-active-low"))
2317 hdmirx_dev->hpd_trigger_level_high = true;
2318
2319 ret = of_reserved_mem_device_init(dev);
2320 if (ret) {
2321 dev_warn(dev, "no reserved memory for HDMIRX, use default CMA\n");
2322 } else {
2323 ret = devm_add_action_or_reset(dev,
2324 devm_hdmirx_of_reserved_mem_device_release,
2325 dev);
2326 if (ret)
2327 return ret;
2328 }
2329
2330 return 0;
2331 }
2332
2333 static void hdmirx_disable_all_interrupts(struct snps_hdmirx_dev *hdmirx_dev)
2334 {
2335 hdmirx_writel(hdmirx_dev, MAINUNIT_0_INT_MASK_N, 0);
2336 hdmirx_writel(hdmirx_dev, MAINUNIT_1_INT_MASK_N, 0);
2337 hdmirx_writel(hdmirx_dev, MAINUNIT_2_INT_MASK_N, 0);
2338 hdmirx_writel(hdmirx_dev, AVPUNIT_0_INT_MASK_N, 0);
2339 hdmirx_writel(hdmirx_dev, AVPUNIT_1_INT_MASK_N, 0);
2340 hdmirx_writel(hdmirx_dev, PKT_0_INT_MASK_N, 0);
2341 hdmirx_writel(hdmirx_dev, PKT_1_INT_MASK_N, 0);
2342 hdmirx_writel(hdmirx_dev, PKT_2_INT_MASK_N, 0);
2343 hdmirx_writel(hdmirx_dev, SCDC_INT_MASK_N, 0);
2344 hdmirx_writel(hdmirx_dev, CEC_INT_MASK_N, 0);
2345
2346 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_0_INT_CLEAR, 0xffffffff);
2347 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_1_INT_CLEAR, 0xffffffff);
2348 hdmirx_clear_interrupt(hdmirx_dev, MAINUNIT_2_INT_CLEAR, 0xffffffff);
2349 hdmirx_clear_interrupt(hdmirx_dev, AVPUNIT_0_INT_CLEAR, 0xffffffff);
2350 hdmirx_clear_interrupt(hdmirx_dev, AVPUNIT_1_INT_CLEAR, 0xffffffff);
2351 hdmirx_clear_interrupt(hdmirx_dev, PKT_0_INT_CLEAR, 0xffffffff);
2352 hdmirx_clear_interrupt(hdmirx_dev, PKT_1_INT_CLEAR, 0xffffffff);
2353 hdmirx_clear_interrupt(hdmirx_dev, PKT_2_INT_CLEAR, 0xffffffff);
2354 hdmirx_clear_interrupt(hdmirx_dev, SCDC_INT_CLEAR, 0xffffffff);
2355 hdmirx_clear_interrupt(hdmirx_dev, HDCP_INT_CLEAR, 0xffffffff);
2356 hdmirx_clear_interrupt(hdmirx_dev, HDCP_1_INT_CLEAR, 0xffffffff);
2357 hdmirx_clear_interrupt(hdmirx_dev, CEC_INT_CLEAR, 0xffffffff);
2358 }
2359
2360 static int hdmirx_detect_broken_interrupt(struct snps_hdmirx_dev *hdmirx_dev)
2361 {
2362 int ret;
2363 u32 val;
2364
2365 enable_irq(hdmirx_dev->hdmi_irq);
2366
2367 hdmirx_writel(hdmirx_dev, PHYCREG_CONFIG0, 0x3);
2368
2369 ret = hdmirx_phy_register_read(hdmirx_dev,
2370 HDMIPCS_DIG_CTRL_PATH_MAIN_FSM_FSM_CONFIG,
2371 &val);
2372
2373 disable_irq(hdmirx_dev->hdmi_irq);
2374
2375 return ret;
2376 }
2377
2378 static int hdmirx_init(struct snps_hdmirx_dev *hdmirx_dev)
2379 {
2380 int ret;
2381
2382 hdmirx_update_bits(hdmirx_dev, PHY_CONFIG, PHY_RESET | PHY_PDDQ, 0);
2383
2384 regmap_write(hdmirx_dev->vo1_grf, VO1_GRF_VO1_CON2,
2385 (HDMIRX_SDAIN_MSK | HDMIRX_SCLIN_MSK) |
2386 ((HDMIRX_SDAIN_MSK | HDMIRX_SCLIN_MSK) << 16));
2387
2388 /*
2389 * RK3588 downstream version of TF-A remaps HDMIRX interrupt and
2390 * requires use of a vendor-specific FW API that we don't support
2391 * in this driver.
2392 */
2393 ret = hdmirx_detect_broken_interrupt(hdmirx_dev);
2394 if (ret)
2395 dev_err_probe(hdmirx_dev->dev, ret,
2396 "interrupt not functioning, open-source TF-A is required by this driver\n");
2397
2398 /*
2399 * Some interrupts are enabled by default, so we disable
2400 * all interrupts and clear interrupts status first.
2401 */
2402 hdmirx_disable_all_interrupts(hdmirx_dev);
2403
2404 return ret;
2405 }
2406
2407 /* hdmi-4k-300mhz EDID produced by v4l2-ctl tool */
2408 static u8 __maybe_unused edid_default[] = {
2409 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
2410 0x31, 0xd8, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
2411 0x22, 0x1a, 0x01, 0x03, 0x80, 0x60, 0x36, 0x78,
2412 0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
2413 0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
2414 0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
2415 0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x04, 0x74,
2416 0x00, 0x30, 0xf2, 0x70, 0x5a, 0x80, 0xb0, 0x58,
2417 0x8a, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00, 0x1e,
2418 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
2419 0x87, 0x1e, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
2420 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x68,
2421 0x64, 0x6d, 0x69, 0x2d, 0x34, 0x6b, 0x2d, 0x33,
2422 0x30, 0x30, 0x0a, 0x20, 0x00, 0x00, 0x00, 0x10,
2423 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2424 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xc5,
2425
2426 0x02, 0x03, 0x40, 0xf1, 0x4f, 0x5f, 0x5e, 0x5d,
2427 0x10, 0x1f, 0x04, 0x13, 0x22, 0x21, 0x20, 0x05,
2428 0x14, 0x02, 0x11, 0x01, 0x23, 0x09, 0x07, 0x07,
2429 0x83, 0x01, 0x00, 0x00, 0x6d, 0x03, 0x0c, 0x00,
2430 0x10, 0x00, 0x00, 0x3c, 0x21, 0x00, 0x60, 0x01,
2431 0x02, 0x03, 0x67, 0xd8, 0x5d, 0xc4, 0x01, 0x00,
2432 0x00, 0x00, 0xe2, 0x00, 0xca, 0xe3, 0x05, 0x00,
2433 0x00, 0xe3, 0x06, 0x01, 0x00, 0xe2, 0x0d, 0x5f,
2434 0xa3, 0x66, 0x00, 0xa0, 0xf0, 0x70, 0x1f, 0x80,
2435 0x30, 0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32, 0x00,
2436 0x00, 0x1e, 0x1a, 0x36, 0x80, 0xa0, 0x70, 0x38,
2437 0x1f, 0x40, 0x30, 0x20, 0x35, 0x00, 0xc0, 0x1c,
2438 0x32, 0x00, 0x00, 0x1a, 0x1a, 0x1d, 0x00, 0x80,
2439 0x51, 0xd0, 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00,
2440 0xc0, 0x1c, 0x32, 0x00, 0x00, 0x1c, 0x00, 0x00,
2441 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa1,
2442 };
2443
2444 static void hdmirx_load_default_edid(struct snps_hdmirx_dev *hdmirx_dev)
2445 {
2446 struct v4l2_edid def_edid = {};
2447
2448 hdmirx_hpd_ctrl(hdmirx_dev, false);
2449
2450 if (!IS_ENABLED(CONFIG_VIDEO_SYNOPSYS_HDMIRX_LOAD_DEFAULT_EDID))
2451 return;
2452
2453 /* disable hpd and write edid */
2454 def_edid.blocks = sizeof(edid_default) / EDID_BLOCK_SIZE;
2455 def_edid.edid = edid_default;
2456
2457 hdmirx_write_edid(hdmirx_dev, &def_edid);
2458 hdmirx_hpd_ctrl(hdmirx_dev, true);
2459 }
2460
2461 static int hdmirx_disable(struct device *dev)
2462 {
2463 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2464 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
2465
2466 hdmirx_plugout(hdmirx_dev);
2467 hdmirx_hpd_ctrl(hdmirx_dev, false);
2468
2469 clk_bulk_disable_unprepare(hdmirx_dev->num_clks, hdmirx_dev->clks);
2470
2471 v4l2_dbg(2, debug, v4l2_dev, "%s: suspend\n", __func__);
2472
2473 return pinctrl_pm_select_sleep_state(dev);
2474 }
2475
2476 static int hdmirx_enable(struct device *dev)
2477 {
2478 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2479 struct v4l2_device *v4l2_dev = &hdmirx_dev->v4l2_dev;
2480 int ret;
2481
2482 v4l2_dbg(2, debug, v4l2_dev, "%s: resume\n", __func__);
2483 ret = pinctrl_pm_select_default_state(dev);
2484 if (ret < 0)
2485 return ret;
2486
2487 ret = clk_bulk_prepare_enable(hdmirx_dev->num_clks, hdmirx_dev->clks);
2488 if (ret) {
2489 dev_err(dev, "failed to enable hdmirx bulk clks: %d\n", ret);
2490 return ret;
2491 }
2492
2493 reset_control_bulk_assert(HDMIRX_NUM_RST, hdmirx_dev->resets);
2494 usleep_range(150, 160);
2495 reset_control_bulk_deassert(HDMIRX_NUM_RST, hdmirx_dev->resets);
2496 usleep_range(150, 160);
2497
2498 return 0;
2499 }
2500
2501 static void hdmirx_disable_irq(struct device *dev)
2502 {
2503 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2504
2505 disable_irq(hdmirx_dev->det_irq);
2506 disable_irq(hdmirx_dev->dma_irq);
2507 disable_irq(hdmirx_dev->hdmi_irq);
2508
2509 cancel_delayed_work_sync(&hdmirx_dev->delayed_work_hotplug);
2510 cancel_delayed_work_sync(&hdmirx_dev->delayed_work_res_change);
2511 }
2512
2513 static void hdmirx_enable_irq(struct device *dev)
2514 {
2515 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2516
2517 enable_irq(hdmirx_dev->hdmi_irq);
2518 enable_irq(hdmirx_dev->dma_irq);
2519 enable_irq(hdmirx_dev->det_irq);
2520
2521 queue_delayed_work(system_unbound_wq,
2522 &hdmirx_dev->delayed_work_hotplug,
2523 msecs_to_jiffies(110));
2524 }
2525
2526 static __maybe_unused int hdmirx_suspend(struct device *dev)
2527 {
2528 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2529
2530 hdmirx_disable_irq(dev);
2531
2532 /* TODO store CEC HW state */
2533 disable_irq(hdmirx_dev->cec->irq);
2534
2535 return hdmirx_disable(dev);
2536 }
2537
2538 static __maybe_unused int hdmirx_resume(struct device *dev)
2539 {
2540 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2541 int ret = hdmirx_enable(dev);
2542
2543 if (ret)
2544 return ret;
2545
2546 if (hdmirx_dev->edid_blocks_written) {
2547 hdmirx_write_edid_data(hdmirx_dev, hdmirx_dev->edid,
2548 hdmirx_dev->edid_blocks_written);
2549 hdmirx_hpd_ctrl(hdmirx_dev, true);
2550 }
2551
2552 /* TODO restore CEC HW state */
2553 enable_irq(hdmirx_dev->cec->irq);
2554
2555 hdmirx_enable_irq(dev);
2556
2557 return 0;
2558 }
2559
2560 static const struct dev_pm_ops snps_hdmirx_pm_ops = {
2561 SET_SYSTEM_SLEEP_PM_OPS(hdmirx_suspend, hdmirx_resume)
2562 };
2563
2564 static int hdmirx_setup_irq(struct snps_hdmirx_dev *hdmirx_dev,
2565 struct platform_device *pdev)
2566 {
2567 struct device *dev = hdmirx_dev->dev;
2568 int ret, irq;
2569
2570 irq = platform_get_irq_byname(pdev, "hdmi");
2571 if (irq < 0) {
2572 dev_err_probe(dev, irq, "failed to get hdmi irq\n");
2573 return irq;
2574 }
2575
2576 irq_set_status_flags(irq, IRQ_NOAUTOEN);
2577
2578 hdmirx_dev->hdmi_irq = irq;
2579 ret = devm_request_irq(dev, irq, hdmirx_hdmi_irq_handler, 0,
2580 "rk_hdmirx-hdmi", hdmirx_dev);
2581 if (ret) {
2582 dev_err_probe(dev, ret, "failed to request hdmi irq\n");
2583 return ret;
2584 }
2585
2586 irq = platform_get_irq_byname(pdev, "dma");
2587 if (irq < 0) {
2588 dev_err_probe(dev, irq, "failed to get dma irq\n");
2589 return irq;
2590 }
2591
2592 irq_set_status_flags(irq, IRQ_NOAUTOEN);
2593
2594 hdmirx_dev->dma_irq = irq;
2595 ret = devm_request_threaded_irq(dev, irq, NULL, hdmirx_dma_irq_handler,
2596 IRQF_ONESHOT, "rk_hdmirx-dma",
2597 hdmirx_dev);
2598 if (ret) {
2599 dev_err_probe(dev, ret, "failed to request dma irq\n");
2600 return ret;
2601 }
2602
2603 irq = gpiod_to_irq(hdmirx_dev->detect_5v_gpio);
2604 if (irq < 0) {
2605 dev_err_probe(dev, irq, "failed to get hdmirx-5v irq\n");
2606 return irq;
2607 }
2608
2609 irq_set_status_flags(irq, IRQ_NOAUTOEN);
2610
2611 hdmirx_dev->det_irq = irq;
2612 ret = devm_request_irq(dev, irq, hdmirx_5v_det_irq_handler,
2613 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
2614 "rk_hdmirx-5v", hdmirx_dev);
2615 if (ret) {
2616 dev_err_probe(dev, ret, "failed to request hdmirx-5v irq\n");
2617 return ret;
2618 }
2619
2620 return 0;
2621 }
2622
2623 static int hdmirx_register_cec(struct snps_hdmirx_dev *hdmirx_dev,
2624 struct platform_device *pdev)
2625 {
2626 struct device *dev = hdmirx_dev->dev;
2627 struct hdmirx_cec_data cec_data;
2628 int irq;
2629
2630 irq = platform_get_irq_byname(pdev, "cec");
2631 if (irq < 0) {
2632 dev_err_probe(dev, irq, "failed to get cec irq\n");
2633 return irq;
2634 }
2635
2636 cec_data.hdmirx = hdmirx_dev;
2637 cec_data.dev = hdmirx_dev->dev;
2638 cec_data.ops = &hdmirx_cec_ops;
2639 cec_data.irq = irq;
2640
2641 hdmirx_dev->cec = snps_hdmirx_cec_register(&cec_data);
2642 if (IS_ERR(hdmirx_dev->cec))
2643 return dev_err_probe(dev, PTR_ERR(hdmirx_dev->cec),
2644 "failed to register cec\n");
2645
2646 return 0;
2647 }
2648
2649 static int hdmirx_probe(struct platform_device *pdev)
2650 {
2651 struct snps_hdmirx_dev *hdmirx_dev;
2652 struct device *dev = &pdev->dev;
2653 struct v4l2_ctrl_handler *hdl;
2654 struct hdmirx_stream *stream;
2655 struct v4l2_device *v4l2_dev;
2656 int ret;
2657
2658 hdmirx_dev = devm_kzalloc(dev, sizeof(*hdmirx_dev), GFP_KERNEL);
2659 if (!hdmirx_dev)
2660 return -ENOMEM;
2661
2662 /*
2663 * RK3588 HDMIRX SoC integration doesn't use IOMMU and can
2664 * address only first 32bit of the physical address space.
2665 */
2666 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2667 if (ret)
2668 return ret;
2669
2670 hdmirx_dev->dev = dev;
2671 dev_set_drvdata(dev, hdmirx_dev);
2672
2673 ret = hdmirx_parse_dt(hdmirx_dev);
2674 if (ret)
2675 return ret;
2676
2677 ret = hdmirx_setup_irq(hdmirx_dev, pdev);
2678 if (ret)
2679 return ret;
2680
2681 hdmirx_dev->regs = devm_platform_ioremap_resource(pdev, 0);
2682 if (IS_ERR(hdmirx_dev->regs))
2683 return dev_err_probe(dev, PTR_ERR(hdmirx_dev->regs),
2684 "failed to remap regs resource\n");
2685
2686 mutex_init(&hdmirx_dev->phy_rw_lock);
2687 mutex_init(&hdmirx_dev->stream_lock);
2688 mutex_init(&hdmirx_dev->work_lock);
2689 spin_lock_init(&hdmirx_dev->rst_lock);
2690
2691 init_completion(&hdmirx_dev->cr_read_done);
2692 init_completion(&hdmirx_dev->cr_write_done);
2693 init_completion(&hdmirx_dev->timer_base_lock);
2694 init_completion(&hdmirx_dev->avi_pkt_rcv);
2695
2696 INIT_DELAYED_WORK(&hdmirx_dev->delayed_work_hotplug,
2697 hdmirx_delayed_work_hotplug);
2698 INIT_DELAYED_WORK(&hdmirx_dev->delayed_work_res_change,
2699 hdmirx_delayed_work_res_change);
2700
2701 hdmirx_dev->cur_fmt_fourcc = V4L2_PIX_FMT_BGR24;
2702 hdmirx_dev->timings = cea640x480;
2703
2704 hdmirx_enable(dev);
2705
2706 ret = hdmirx_init(hdmirx_dev);
2707 if (ret)
2708 goto err_pm;
2709
2710 v4l2_dev = &hdmirx_dev->v4l2_dev;
2711 strscpy(v4l2_dev->name, dev_name(dev), sizeof(v4l2_dev->name));
2712
2713 hdl = &hdmirx_dev->hdl;
2714 v4l2_ctrl_handler_init(hdl, 3);
2715
2716 hdmirx_dev->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
2717 V4L2_CID_DV_RX_POWER_PRESENT,
2718 0, 1, 0, 0);
2719
2720 hdmirx_dev->rgb_range = v4l2_ctrl_new_std_menu(hdl, NULL,
2721 V4L2_CID_DV_RX_RGB_RANGE,
2722 V4L2_DV_RGB_RANGE_FULL, 0,
2723 V4L2_DV_RGB_RANGE_AUTO);
2724
2725 hdmirx_dev->rgb_range->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2726
2727 hdmirx_dev->content_type =
2728 v4l2_ctrl_new_std_menu(hdl, NULL, V4L2_CID_DV_RX_IT_CONTENT_TYPE,
2729 V4L2_DV_IT_CONTENT_TYPE_NO_ITC, 0,
2730 V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
2731
2732 if (hdl->error) {
2733 ret = hdl->error;
2734 dev_err_probe(dev, ret, "v4l2 ctrl handler init failed\n");
2735 goto err_pm;
2736 }
2737 hdmirx_dev->v4l2_dev.ctrl_handler = hdl;
2738
2739 ret = v4l2_device_register(dev, &hdmirx_dev->v4l2_dev);
2740 if (ret < 0) {
2741 dev_err_probe(dev, ret, "v4l2 device registration failed\n");
2742 goto err_hdl;
2743 }
2744
2745 stream = &hdmirx_dev->stream;
2746 stream->hdmirx_dev = hdmirx_dev;
2747 ret = hdmirx_register_stream_vdev(stream);
2748 if (ret < 0) {
2749 dev_err_probe(dev, ret, "video device registration failed\n");
2750 goto err_unreg_v4l2_dev;
2751 }
2752
2753 ret = hdmirx_register_cec(hdmirx_dev, pdev);
2754 if (ret)
2755 goto err_unreg_video_dev;
2756
2757 hdmirx_load_default_edid(hdmirx_dev);
2758
2759 hdmirx_enable_irq(dev);
2760
2761 hdmirx_dev->debugfs_dir = debugfs_create_dir(hdmirx_dev->v4l2_dev.name,
2762 v4l2_debugfs_root());
2763
2764 hdmirx_dev->infoframes = v4l2_debugfs_if_alloc(hdmirx_dev->debugfs_dir,
2765 V4L2_DEBUGFS_IF_AVI, hdmirx_dev,
2766 hdmirx_debugfs_if_read);
2767
2768 return 0;
2769
2770 err_unreg_video_dev:
2771 vb2_video_unregister_device(&hdmirx_dev->stream.vdev);
2772 err_unreg_v4l2_dev:
2773 v4l2_device_unregister(&hdmirx_dev->v4l2_dev);
2774 err_hdl:
2775 v4l2_ctrl_handler_free(&hdmirx_dev->hdl);
2776 err_pm:
2777 hdmirx_disable(dev);
2778
2779 return ret;
2780 }
2781
2782 static void hdmirx_remove(struct platform_device *pdev)
2783 {
2784 struct device *dev = &pdev->dev;
2785 struct snps_hdmirx_dev *hdmirx_dev = dev_get_drvdata(dev);
2786
2787 v4l2_debugfs_if_free(hdmirx_dev->infoframes);
2788 debugfs_remove_recursive(hdmirx_dev->debugfs_dir);
2789
2790 snps_hdmirx_cec_unregister(hdmirx_dev->cec);
2791
2792 hdmirx_disable_irq(dev);
2793
2794 vb2_video_unregister_device(&hdmirx_dev->stream.vdev);
2795 v4l2_ctrl_handler_free(&hdmirx_dev->hdl);
2796 v4l2_device_unregister(&hdmirx_dev->v4l2_dev);
2797
2798 /* touched by hdmirx_disable()->hdmirx_plugout() */
2799 hdmirx_dev->rgb_range = NULL;
2800 hdmirx_dev->content_type = NULL;
2801
2802 hdmirx_disable(dev);
2803
2804 reset_control_bulk_assert(HDMIRX_NUM_RST, hdmirx_dev->resets);
2805 }
2806
2807 static const struct of_device_id hdmirx_id[] = {
2808 { .compatible = "rockchip,rk3588-hdmirx-ctrler" },
2809 { }
2810 };
2811 MODULE_DEVICE_TABLE(of, hdmirx_id);
2812
2813 static struct platform_driver hdmirx_driver = {
2814 .probe = hdmirx_probe,
2815 .remove = hdmirx_remove,
2816 .driver = {
2817 .name = "snps_hdmirx",
2818 .of_match_table = hdmirx_id,
2819 .pm = &snps_hdmirx_pm_ops,
2820 }
2821 };
2822 module_platform_driver(hdmirx_driver);
2823
2824 MODULE_DESCRIPTION("Synopsys HDMI Receiver Driver");
2825 MODULE_AUTHOR("Dingxian Wen <[email protected]>");
2826 MODULE_AUTHOR("Shreeya Patel <[email protected]>");
2827 MODULE_AUTHOR("Dmitry Osipenko <[email protected]>");
2828 MODULE_LICENSE("GPL");