2 * tc358840.c - Toshiba UH2C/D HDMI-CSI bridge driver
4 * Copyright (c) 2015, Armin Weiss <weii@zhaw.ch>
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
7 * This program is based on the tc358840 - Toshiba HDMI to CSI-2 bridge driver
8 * from Cisco Systems, Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/module.h>
26 #include <linux/i2c.h>
27 #include <linux/regmap.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/of_gpio.h>
30 #include <linux/of_irq.h>
31 #include <linux/interrupt.h>
32 #include <linux/videodev2.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <linux/hdmi.h>
36 #include <linux/v4l2-dv-timings.h>
38 #include <media/v4l2-dv-timings.h>
39 #include <media/v4l2-ctrls.h>
40 #include <media/v4l2-event.h>
41 #include <media/v4l2-device.h>
42 #include <media/v4l2-subdev.h>
43 #include <media/v4l2-of.h>
44 #include <media/camera_common.h>
46 #include <media/i2c/tc358840.h>
47 #include "tc358840_regs.h"
51 module_param(debug, int, 0644);
52 MODULE_PARM_DESC(debug, "debug level (0-3)");
54 /* TODO: Implement Colorbar TPG */
56 #define EDID_NUM_BLOCKS_MAX 8
57 #define EDID_BLOCK_SIZE 128
60 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
61 0x52, 0x62, 0x88, 0x88, 0x00, 0x88, 0x88, 0x88,
62 0x1C, 0x15, 0x01, 0x03, 0x80, 0x00, 0x00, 0x78,
63 0x0A, 0x0D, 0xC9, 0xA0, 0x57, 0x47, 0x98, 0x27,
64 0x12, 0x48, 0x4C, 0x00, 0x00, 0x00, 0x01, 0x01,
65 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
66 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xEC, 0x68,
67 0x00, 0xA0, 0xF0, 0x70, 0x37, 0x80, 0x30, 0x20,
68 0x3A, 0x00, 0x00, 0x70, 0xF8, 0x00, 0x00, 0x1C,
69 0x02, 0x3A, 0x80, 0x18, 0x71, 0x38, 0x2D, 0x40,
70 0x58, 0x2C, 0x45, 0x00, 0xC4, 0x8E, 0x21, 0x00,
71 0x00, 0x1E, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x54,
72 0x6F, 0x73, 0x68, 0x69, 0x62, 0x61, 0x2D, 0x55,
73 0x48, 0x32, 0x44, 0x0A, 0x00, 0x00, 0x00, 0xFD,
74 0x00, 0x17, 0x3D, 0x0F, 0x8C, 0x17, 0x00, 0x0A,
75 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01, 0xAF,
76 0x02, 0x03, 0x1A, 0x74, 0x47, 0x32, 0x10, 0x04,
77 0x32, 0x32, 0x32, 0x32, 0x23, 0x09, 0x07, 0x01,
78 0x83, 0x01, 0x00, 0x00, 0x65, 0x03, 0x0C, 0x00,
79 0x10, 0x00, 0x01, 0x1D, 0x00, 0x72, 0x51, 0xD0,
80 0x1E, 0x20, 0x6E, 0x28, 0x55, 0x00, 0xC4, 0x8E,
81 0x21, 0x00, 0x00, 0x1E, 0xEC, 0x68, 0x00, 0xA0,
82 0xF0, 0x70, 0x37, 0x80, 0x30, 0x20, 0x3A, 0x00,
83 0x00, 0x70, 0xF8, 0x00, 0x00, 0x1C, 0xEC, 0x68,
84 0x00, 0xA0, 0xF0, 0x70, 0x37, 0x80, 0x30, 0x20,
85 0x3A, 0x00, 0x00, 0x70, 0xF8, 0x00, 0x00, 0x1C,
86 0xEC, 0x68, 0x00, 0xA0, 0xF0, 0x70, 0x37, 0x80,
87 0x30, 0x20, 0x3A, 0x00, 0x00, 0x70, 0xF8, 0x00,
88 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26,
94 static const struct v4l2_dv_timings_cap tc358840_timings_cap = {
95 .type = V4L2_DV_BT_656_1120,
96 /* keep this initialization for compatibility with GCC < 4.4.6 */
98 /* Pixel clock from REF_01 p. 20. Min/max height/width are unknown */
100 1, 10000, 1, 10000, 0, 297000000,
101 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
102 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
103 V4L2_DV_BT_CAP_PROGRESSIVE |
104 V4L2_DV_BT_CAP_REDUCED_BLANKING |
105 V4L2_DV_BT_CAP_CUSTOM)
108 struct tc358840_state {
109 struct tc358840_platform_data pdata;
110 struct v4l2_subdev sd;
111 struct media_pad pad[2];
112 struct v4l2_ctrl_handler hdl;
113 struct i2c_client *i2c_client;
118 struct v4l2_ctrl *detect_tx_5v_ctrl;
119 struct v4l2_ctrl *audio_sampling_rate_ctrl;
120 struct v4l2_ctrl *audio_present_ctrl;
121 struct v4l2_ctrl *splitter_width_ctrl;
124 struct workqueue_struct *work_queues;
125 struct delayed_work delayed_work_enable_hotplug;
126 struct work_struct process_isr;
127 struct mutex isr_lock;
130 u8 edid_blocks_written;
133 struct v4l2_dv_timings timings;
137 static inline struct tc358840_state *to_state(struct v4l2_subdev *sd)
139 return container_of(sd, struct tc358840_state, sd);
143 static void tc358840_enable_interrupts(
144 struct v4l2_subdev *sd, bool cable_connected);
145 static int tc358840_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
146 static void tc358840_init_interrupts(struct v4l2_subdev *sd);
147 static int tc358840_s_dv_timings(
148 struct v4l2_subdev *sd, struct v4l2_dv_timings *timings);
149 static void tc358840_set_csi(struct v4l2_subdev *sd);
150 static void tc358840_set_splitter(struct v4l2_subdev *sd);
152 /* --------------- I2C --------------- */
154 static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
156 struct tc358840_state *state = to_state(sd);
157 struct i2c_client *client = state->i2c_client;
159 u8 buf[2] = { reg >> 8, reg & 0xff };
160 struct i2c_msg msgs[] = {
162 .addr = client->addr,
168 .addr = client->addr,
175 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
176 if (err != ARRAY_SIZE(msgs)) {
177 v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
178 __func__, reg, client->addr);
186 v4l2_info(sd, "I2C read 0x%04X = 0x%02X\n", reg, values[0]);
189 v4l2_info(sd, "I2C read 0x%04X = 0x%02X%02X\n",
190 reg, values[1], values[0]);
193 v4l2_info(sd, "I2C read 0x%04X = 0x%02X%02X%02X%02X\n",
194 reg, values[3], values[2], values[1], values[0]);
197 v4l2_info(sd, "I2C read %d bytes from address 0x%04X\n",
203 static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
205 struct tc358840_state *state = to_state(sd);
206 struct i2c_client *client = state->i2c_client;
211 msg.addr = client->addr;
217 data[1] = reg & 0xff;
219 for (i = 0; i < n; i++)
220 data[2 + i] = values[i];
222 err = i2c_transfer(client->adapter, &msg, 1);
224 v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed\n",
225 __func__, reg, client->addr);
234 v4l2_info(sd, "I2C write 0x%04X = 0x%02X\n", reg, data[2]);
237 v4l2_info(sd, "I2C write 0x%04X = 0x%02X%02X\n",
238 reg, data[3], data[2]);
241 v4l2_info(sd, "I2C write 0x%04X = 0x%02X%02X%02X%02X\n",
242 reg, data[5], data[4], data[3], data[2]);
245 v4l2_info(sd, "I2C write %d bytes from address 0x%04X\n",
250 static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
254 i2c_rd(sd, reg, &val, 1);
259 static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
261 i2c_wr(sd, reg, &val, 1);
264 static void i2c_wr8_and_or(struct v4l2_subdev *sd, u16 reg,
267 i2c_wr8(sd, reg, (i2c_rd8(sd, reg) & mask) | val);
270 static u16 i2c_rd16(struct v4l2_subdev *sd, u16 reg)
274 i2c_rd(sd, reg, (u8 *)&val, 2);
279 static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
281 i2c_wr(sd, reg, (u8 *)&val, 2);
284 static void i2c_wr16_and_or(struct v4l2_subdev *sd, u16 reg, u16 mask, u16 val)
286 i2c_wr16(sd, reg, (i2c_rd16(sd, reg) & mask) | val);
289 static u32 i2c_rd32(struct v4l2_subdev *sd, u16 reg)
293 i2c_rd(sd, reg, (u8 *)&val, 4);
298 static void i2c_wr32(struct v4l2_subdev *sd, u16 reg, u32 val)
300 i2c_wr(sd, reg, (u8 *)&val, 4);
303 static void i2c_wr32_and_or(struct v4l2_subdev *sd, u32 reg, u32 mask, u32 val)
305 i2c_wr32(sd, reg, (i2c_rd32(sd, reg) & mask) | val);
308 /* --------------- STATUS --------------- */
310 static inline bool is_hdmi(struct v4l2_subdev *sd)
312 return i2c_rd8(sd, SYS_STATUS) & MASK_S_HDMI;
315 static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
317 return i2c_rd8(sd, SYS_STATUS) & MASK_S_DDC5V;
320 static inline bool no_signal(struct v4l2_subdev *sd)
322 return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_TMDS);
325 static inline bool no_sync(struct v4l2_subdev *sd)
327 return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_SYNC);
330 static inline bool audio_present(struct v4l2_subdev *sd)
332 return i2c_rd8(sd, AU_STATUS0) & MASK_S_A_SAMPLE;
335 static int get_audio_sampling_rate(struct v4l2_subdev *sd)
337 static const int code_to_rate[] = {
338 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
339 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
342 /* Register FS_SET is not cleared when the cable is disconnected */
346 return code_to_rate[i2c_rd8(sd, FS_SET) & MASK_FS];
349 static unsigned tc358840_num_csi_lanes_in_use(struct v4l2_subdev *sd)
351 /* FIXME: Read # of lanes from both, TX0 and TX1 */
352 return i2c_rd32(sd, CSITX0_BASE_ADDR+LANEEN) & MASK_LANES;
355 /* --------------- TIMINGS --------------- */
357 static inline unsigned fps(const struct v4l2_bt_timings *t)
359 if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
362 return DIV_ROUND_CLOSEST((unsigned)t->pixelclock,
363 V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
366 static int tc358840_get_detected_timings(struct v4l2_subdev *sd,
367 struct v4l2_dv_timings *timings)
369 struct v4l2_bt_timings *bt = &timings->bt;
370 unsigned width, height, frame_width, frame_height, frame_interval, fps;
372 memset(timings, 0, sizeof(struct v4l2_dv_timings));
375 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
379 v4l2_dbg(1, debug, sd, "%s: no sync on signal\n", __func__);
383 timings->type = V4L2_DV_BT_656_1120;
385 bt->interlaced = i2c_rd8(sd, VI_STATUS1) &
386 MASK_S_V_INTERLACE ? V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
388 width = ((i2c_rd8(sd, DE_HSIZE_HI) & 0x1f) << 8) +
389 i2c_rd8(sd, DE_HSIZE_LO);
390 height = ((i2c_rd8(sd, DE_VSIZE_HI) & 0x1f) << 8) +
391 i2c_rd8(sd, DE_VSIZE_LO);
392 frame_width = ((i2c_rd8(sd, IN_HSIZE_HI) & 0x1f) << 8) +
393 i2c_rd8(sd, IN_HSIZE_LO);
394 frame_height = (((i2c_rd8(sd, IN_VSIZE_HI) & 0x3f) << 8) +
395 i2c_rd8(sd, IN_VSIZE_LO)) / 2;
397 /* TODO: Check if frame_interval is correct
398 * since the register is not in the datasheet rev. 1.5 */
400 /* frame interval in milliseconds * 10
401 * Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
402 frame_interval = ((i2c_rd8(sd, FV_CNT_HI) & 0x3) << 8) +
403 i2c_rd8(sd, FV_CNT_LO);
404 fps = (frame_interval > 0) ?
405 DIV_ROUND_CLOSEST(10000, frame_interval) : 0;
409 bt->vsync = frame_height - height;
410 bt->hsync = frame_width - width;
411 bt->pixelclock = frame_width * frame_height * fps;
412 if (bt->interlaced == V4L2_DV_INTERLACED) {
414 bt->il_vsync = bt->vsync + 1;
421 /* --------------- HOTPLUG / HDCP / EDID --------------- */
423 static void tc358840_delayed_work_enable_hotplug(struct work_struct *work)
425 struct delayed_work *dwork = to_delayed_work(work);
426 struct tc358840_state *state = container_of(dwork,
427 struct tc358840_state, delayed_work_enable_hotplug);
428 struct v4l2_subdev *sd = &state->sd;
430 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
432 i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, MASK_HPD_OUT0);
435 static void tc358840_set_hdmi_hdcp(struct v4l2_subdev *sd, bool enable)
437 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ?
438 "enable" : "disable");
440 i2c_wr8_and_or(sd, HDCP_REG1,
441 ~(MASK_AUTH_UNAUTH_SEL | MASK_AUTH_UNAUTH),
442 MASK_AUTH_UNAUTH_SEL_16_FRAMES | MASK_AUTH_UNAUTH_AUTO);
444 i2c_wr8_and_or(sd, HDCP_REG2, ~MASK_AUTO_P3_RESET,
445 SET_AUTO_P3_RESET_FRAMES(0x0f));
447 /* HDCP is disabled by configuring the receiver as HDCP repeater. The
448 * repeater mode require software support to work, so HDCP
449 * authentication will fail.
451 i2c_wr8_and_or(sd, HDCP_REG3, ~KEY_RD_CMD, enable ? KEY_RD_CMD : 0);
452 i2c_wr8_and_or(sd, HDCP_MODE, ~(MASK_AUTO_CLR | MASK_MODE_RST_TN),
453 enable ? (MASK_AUTO_CLR | MASK_MODE_RST_TN) : 0);
455 /* Apple MacBook Pro gen.8 has a bug that makes it freeze every fifth
456 * second when HDCP is disabled, but the MAX_EXCED bit is handled
457 * correctly and HDCP is disabled on the HDMI output.
459 i2c_wr8_and_or(sd, BSTATUS1, ~MASK_MAX_EXCED,
460 enable ? 0 : MASK_MAX_EXCED);
461 i2c_wr8_and_or(sd, BCAPS, ~(MASK_REPEATER | MASK_READY),
462 enable ? 0 : MASK_REPEATER | MASK_READY);
465 static void tc358840_disable_edid(struct v4l2_subdev *sd)
467 struct tc358840_state *state = to_state(sd);
469 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
471 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
473 /* DDC access to EDID is also disabled when hotplug is disabled. See
474 * register DDC_CTL */
475 i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, 0x0);
478 static void tc358840_enable_edid(struct v4l2_subdev *sd)
480 struct tc358840_state *state = to_state(sd);
482 if (state->edid_blocks_written == 0) {
483 v4l2_dbg(2, debug, sd, "%s: no EDID -> no hotplug\n", __func__);
487 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
489 /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when
490 * hotplug is enabled. See register DDC_CTL */
491 queue_delayed_work(state->work_queues,
492 &state->delayed_work_enable_hotplug, HZ / 10);
494 tc358840_enable_interrupts(sd, true);
495 tc358840_s_ctrl_detect_tx_5v(sd);
498 static void tc358840_erase_bksv(struct v4l2_subdev *sd)
502 for (i = 0; i < 5; i++)
503 i2c_wr8(sd, BKSV + i, 0);
506 /* --------------- AVI infoframe --------------- */
508 static void print_avi_infoframe(struct v4l2_subdev *sd)
510 struct i2c_client *client = v4l2_get_subdevdata(sd);
511 struct device *dev = &client->dev;
512 union hdmi_infoframe frame;
513 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
516 v4l2_info(sd, "DVI-D signal - AVI infoframe not supported\n");
520 i2c_rd(sd, PK_AVI_0HEAD, buffer, HDMI_INFOFRAME_SIZE(AVI));
522 if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
523 v4l2_err(sd, "%s: unpack of AVI infoframe failed\n", __func__);
527 hdmi_infoframe_log(KERN_INFO, dev, &frame);
530 /* --------------- CTRLS --------------- */
532 static int tc358840_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
534 struct tc358840_state *state = to_state(sd);
536 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
537 tx_5v_power_present(sd));
540 static int tc358840_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
542 struct tc358840_state *state = to_state(sd);
544 return v4l2_ctrl_s_ctrl(state->audio_sampling_rate_ctrl,
545 get_audio_sampling_rate(sd));
548 static int tc358840_s_ctrl_audio_present(struct v4l2_subdev *sd)
550 struct tc358840_state *state = to_state(sd);
552 return v4l2_ctrl_s_ctrl(state->audio_present_ctrl,
556 static int tc358840_update_controls(struct v4l2_subdev *sd)
560 ret |= tc358840_s_ctrl_detect_tx_5v(sd);
561 ret |= tc358840_s_ctrl_audio_sampling_rate(sd);
562 ret |= tc358840_s_ctrl_audio_present(sd);
567 /* --------------- INIT --------------- */
569 static void tc358840_reset_phy(struct v4l2_subdev *sd)
571 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
573 i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, 0);
574 i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, MASK_RESET_CTRL);
577 static void tc358840_reset(struct v4l2_subdev *sd, u16 mask)
579 u16 sysctl = i2c_rd16(sd, SYSCTL);
581 i2c_wr16(sd, SYSCTL, sysctl | mask);
582 i2c_wr16(sd, SYSCTL, sysctl & ~mask);
585 static inline void tc358840_sleep_mode(struct v4l2_subdev *sd, bool enable)
587 v4l2_dbg(1, debug, sd, "%s(): %s\n", __func__,
588 enable ? "enable" : "disable");
590 i2c_wr16_and_or(sd, SYSCTL, ~MASK_SLEEP, enable ? MASK_SLEEP : 0);
593 static int enable_stream(struct v4l2_subdev *sd, bool enable)
595 struct tc358840_state *state = to_state(sd);
596 struct tc358840_platform_data *pdata = &state->pdata;
598 u32 sync_timeout_ctr;
600 v4l2_dbg(2, debug, sd, "%s: %sable\n", __func__, enable ? "en" : "dis");
602 if (enable == state->enabled)
606 #if 0 /* Wait until we can use the clock-noncontinuous property */
607 if (pdata->endpoint.bus.mipi_csi2.flags &
608 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK) {
609 i2c_wr32_and_or(sd, FUNCMODE, ~(MASK_CONTCLKMODE),
612 /* It is critical for CSI receiver to see lane transition
613 * LP11->HS. Set to non-continuous mode to enable clock lane
615 i2c_wr32_and_or(sd, FUNCMODE, ~(MASK_CONTCLKMODE), 0);
616 /* Set to continuous mode to trigger LP11->HS transition */
617 i2c_wr32_and_or(sd, FUNCMODE, 0, MASK_CONTCLKMODE);
620 i2c_wr32_and_or(sd, FUNCMODE, ~(MASK_CONTCLKMODE),
624 i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE);
625 /* Signal end of initialization */
626 i2c_wr8(sd, INIT_END, MASK_INIT_END);
628 /* Enable Registers to be initialized */
629 i2c_wr8_and_or(sd, INIT_END, ~(MASK_INIT_END), 0x00);
631 /* Mute video so that all data lanes go to LSP11 state.
632 * No data is output to CSI Tx block. */
634 i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE | MASK_VI_MUTE);
635 tc358840_set_csi(sd);
636 tc358840_set_splitter(sd);
639 /* Wait for HDMI input to become stable */
641 sync_timeout_ctr = 100;
642 while (no_sync(sd) && sync_timeout_ctr)
645 if (sync_timeout_ctr == 0) {
646 /* Disable stream again. Probably no cable inserted.. */
647 v4l2_err(sd, "%s: Timeout: HDMI input sync failed.\n",
649 enable_stream(sd, false);
653 v4l2_dbg(2, debug, sd,
654 "%s: Stream enabled! Remaining timeout attempts: %d\n",
655 __func__, sync_timeout_ctr);
658 i2c_wr16_and_or(sd, CONFCTL0,
659 ~(MASK_VTX0EN | MASK_VTX1EN | MASK_ABUFEN),
660 enable ? ((pdata->csi_port & (MASK_VTX0EN | MASK_VTX1EN)) |
661 MASK_ABUFEN | MASK_TX_MSEL | MASK_AUTOINDEX) :
662 (MASK_TX_MSEL | MASK_AUTOINDEX));
663 state->enabled = enable;
668 static void tc358840_set_splitter(struct v4l2_subdev *sd)
670 struct tc358840_state *state = to_state(sd);
672 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
674 if (state->timings.bt.width <= 1920) {
675 i2c_wr16_and_or(sd, SPLITTX0_CTRL, ~(MASK_IFEN | MASK_LCD_CSEL),
677 i2c_wr16_and_or(sd, SPLITTX1_CTRL, ~(MASK_IFEN | MASK_LCD_CSEL),
680 i2c_wr16_and_or(sd, SPLITTX0_SPLIT, (u16)~(MASK_TX1SEL | MASK_EHW), 0);
682 i2c_wr16_and_or(sd, SPLITTX0_CTRL, ~(MASK_IFEN | MASK_LCD_CSEL | MASK_SPBP), 0);
683 i2c_wr16_and_or(sd, SPLITTX1_CTRL, ~(MASK_IFEN | MASK_LCD_CSEL | MASK_SPBP), 0);
685 i2c_wr16_and_or(sd, SPLITTX0_SPLIT, ~(MASK_TX1SEL), MASK_EHW);
689 static void tc358840_set_pll(struct v4l2_subdev *sd,
690 enum tc358840_csi_port port)
692 struct tc358840_state *state = to_state(sd);
693 struct tc358840_platform_data *pdata = &state->pdata;
700 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
702 BUG_ON((pdata->csi_port <= CSI_TX_NONE) ||
703 (pdata->csi_port > CSI_TX_BOTH));
705 if (pdata->csi_port == CSI_TX_NONE) {
706 v4l2_err(sd, "%s: No CSI port defined!\n", __func__);
710 base_addr = (port == CSI_TX_0) ? CSITX0_BASE_ADDR :
712 pllconf = i2c_rd32(sd, base_addr+PLLCONF);
713 pllconf_new = SET_PLL_PRD(pdata->pll_prd) |
714 SET_PLL_FBD(pdata->pll_fbd);
716 hsck = (pdata->refclk_hz / pdata->pll_prd) *
719 if (hsck > 500000000)
721 else if (hsck > 250000000)
723 else if (hsck > 125000000)
728 v4l2_dbg(1, debug, sd, "%s: Updating PLL clock of CSI TX%d\n",
731 i2c_wr32(sd, base_addr+PLLCONF,
732 pllconf_new | SET_PLL_FRS(pll_frs));
735 static void tc358840_set_ref_clk(struct v4l2_subdev *sd)
737 struct tc358840_state *state = to_state(sd);
738 struct tc358840_platform_data *pdata = &state->pdata;
745 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
747 BUG_ON((pdata->refclk_hz < 40000000) || (pdata->refclk_hz > 50000000));
749 /* System Frequency */
750 sys_freq = pdata->refclk_hz / 10000;
751 i2c_wr8(sd, SYS_FREQ0, sys_freq & 0x00FF);
752 i2c_wr8(sd, SYS_FREQ1, (sys_freq & 0xFF00) >> 8);
754 /* Audio System Frequency */
755 lock_ref_freq = pdata->refclk_hz / 100;
756 i2c_wr8(sd, LOCK_REF_FREQA, lock_ref_freq & 0xFF);
757 i2c_wr8(sd, LOCK_REF_FREQB, (lock_ref_freq >> 8) & 0xFF);
758 i2c_wr8(sd, LOCK_REF_FREQC, (lock_ref_freq >> 16) & 0x0F);
761 i2c_wr8(sd, NCO_F0_MOD, MASK_NCO_F0_MOD_REG);
762 /* 6.144 * 2^28 = 1649267442 */
763 nco = (1649267442 / (pdata->refclk_hz / 1000000));
764 i2c_wr8(sd, NCO_48F0A, nco & 0xFF);
765 i2c_wr8(sd, NCO_48F0B, (nco >> 8) & 0xFF);
766 i2c_wr8(sd, NCO_48F0C, (nco >> 16) & 0xFF);
767 i2c_wr8(sd, NCO_48F0D, (nco >> 24) & 0xFF);
769 /* Color Space Conversion */
770 csc = pdata->refclk_hz / 10000;
771 i2c_wr8(sd, SCLK_CSC0, csc & 0xFF);
772 i2c_wr8(sd, SCLK_CSC1, (csc >> 8) & 0xFF);
775 static void tc358840_set_csi_mbus_config(struct v4l2_subdev *sd)
777 struct tc358840_state *state = to_state(sd);
779 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
781 switch (state->mbus_fmt_code) {
782 case V4L2_MBUS_FMT_UYVY8_1X16:
783 v4l2_dbg(2, debug, sd, "%s: YCbCr 422 16-bit\n", __func__);
785 i2c_wr8(sd, VOUT_FMT, MASK_OUTFMT_422 | MASK_422FMT_NORMAL);
786 i2c_wr8(sd, VOUT_FIL, MASK_422FIL_3_TAP_444 |
788 i2c_wr8(sd, VOUT_SYNC0, MASK_MODE_2);
789 i2c_wr8(sd, VOUT_CSC, MASK_CSC_MODE_BUILTIN |
790 MASK_COLOR_601_YCBCR_LIMITED);
791 i2c_wr16_and_or(sd, CONFCTL0, ~(MASK_YCBCRFMT),
792 MASK_YCBCRFMT_YCBCR422_8);
793 i2c_wr16(sd, CONFCTL1, 0x0);
796 case V4L2_MBUS_FMT_RGB888_1X24:
797 v4l2_dbg(2, debug, sd, "%s: RGB 888 24-bit\n", __func__);
799 i2c_wr8(sd, VOUT_FMT, MASK_OUTFMT_444_RGB);
800 i2c_wr8(sd, VOUT_FIL, MASK_422FIL_3_TAP_444 |
802 i2c_wr8(sd, VOUT_SYNC0, MASK_MODE_2);
803 i2c_wr8(sd, VOUT_CSC, MASK_CSC_MODE_BUILTIN |
804 MASK_COLOR_RGB_LIMITED);
805 i2c_wr16_and_or(sd, CONFCTL0, ~(MASK_YCBCRFMT), 0x0);
806 i2c_wr16_and_or(sd, CONFCTL1, 0x0, MASK_TX_OUT_FMT_RGB888);
810 v4l2_dbg(2, debug, sd, "%s: Unsupported format code 0x%x\n",
811 __func__, state->mbus_fmt_code);
816 static unsigned tc358840_num_csi_lanes_needed(struct v4l2_subdev *sd)
819 /* TODO: Check if this can be useful */
821 struct tc358840_state *state = to_state(sd);
822 struct v4l2_bt_timings *bt = &state->timings.bt;
823 struct tc358840_platform_data *pdata = &state->pdata;
825 (state->mbus_fmt_code == V4L2_MBUS_FMT_UYVY8_1X16) ? 16 : 24;
826 u32 bps = bt->width * bt->height * fps(bt) * bits_pr_pixel;
827 u32 bps_pr_lane = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
829 return DIV_ROUND_UP(bps, bps_pr_lane);
832 /* Always use 4 lanes for one CSI */
836 static void tc358840_set_csi(struct v4l2_subdev *sd)
838 struct tc358840_state *state = to_state(sd);
839 struct tc358840_platform_data *pdata = &state->pdata;
841 struct v4l2_bt_timings *bt = &state->timings.bt;
843 unsigned lanes = tc358840_num_csi_lanes_needed(sd);
845 enum tc358840_csi_port port;
848 v4l2_dbg(3, debug, sd, "%s:\n", __func__);
850 tc358840_reset(sd, MASK_CTXRST);
852 for (port = CSI_TX_0; port <= CSI_TX_1; port++) {
853 base_addr = (port == CSI_TX_0) ? CSITX0_BASE_ADDR :
856 if (pdata->csi_port != CSI_TX_BOTH &&
857 pdata->csi_port != port) {
858 v4l2_dbg(1, debug, sd,
859 "%s: Disabling CSI TX%d\n", __func__, port-1);
861 /* Disable CSI lanes (High Z)*/
862 i2c_wr32_and_or(sd, base_addr+LANEEN,
867 v4l2_dbg(1, debug, sd,
868 "%s: Enabling CSI TX%d\n", __func__, port-1);
871 i2c_wr32(sd, base_addr+CSITX_CLKEN, MASK_CSITX_EN);
873 * PLL has to be enabled between CSITX_CLKEN and
876 tc358840_set_pll(sd, port);
878 i2c_wr32_and_or(sd, base_addr+MIPICLKEN,
879 ~(MASK_MP_CKEN), MASK_MP_ENABLE);
880 usleep_range(10000, 11000);
882 i2c_wr32(sd, base_addr+MIPICLKEN,
883 MASK_MP_CKEN | MASK_MP_ENABLE);
885 i2c_wr32(sd, base_addr+PPICLKEN, MASK_HSTXCLKEN);
887 i2c_wr32(sd, base_addr+LANEEN,
888 (lanes & MASK_LANES) | MASK_CLANEEN);
891 i2c_wr32(sd, base_addr+LINEINITCNT, pdata->lineinitcnt);
894 * TODO: Check if this is the correct register
897 i2c_rd32(sd, base_addr+FUNCMODE);
899 i2c_wr32(sd, base_addr+LPTXTIMECNT, pdata->lptxtimecnt);
901 i2c_wr32(sd, base_addr+TCLK_HEADERCNT,
902 pdata->tclk_headercnt);
904 i2c_wr32(sd, base_addr+TCLK_TRAILCNT,
905 pdata->tclk_trailcnt);
907 i2c_wr32(sd, base_addr+THS_HEADERCNT,
908 pdata->ths_headercnt);
910 i2c_wr32(sd, base_addr+TWAKEUP, pdata->twakeup);
912 i2c_wr32(sd, base_addr+TCLK_POSTCNT,
913 pdata->tclk_postcnt);
915 i2c_wr32(sd, base_addr+THS_TRAILCNT,
916 pdata->ths_trailcnt);
919 i2c_wr32(sd, base_addr+HSTXVREGCNT, pdata->hstxvregcnt);
922 i2c_wr32(sd, base_addr+HSTXVREGEN,
923 ((lanes > 0) ? MASK_CLM_HSTXVREGEN : 0x0) |
924 ((lanes > 0) ? MASK_D0M_HSTXVREGEN : 0x0) |
925 ((lanes > 1) ? MASK_D1M_HSTXVREGEN : 0x0) |
926 ((lanes > 2) ? MASK_D2M_HSTXVREGEN : 0x0) |
927 ((lanes > 3) ? MASK_D3M_HSTXVREGEN : 0x0));
930 * Finishing configuration by setting CSITX to start
933 i2c_wr32(sd, base_addr+CSITX_START, 0x00000001);
935 i2c_rd32(sd, base_addr+CSITX_INTERNAL_STAT);
939 static void tc358840_set_hdmi_phy(struct v4l2_subdev *sd)
941 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
944 tc358840_reset_phy(sd);
946 /* Set PHY to manual */
947 i2c_wr8(sd, PHY_CTL, MASK_48_MHZ);
950 i2c_wr8_and_or(sd, PHY_ENB, ~MASK_ENABLE_PHY, 0x0);
951 i2c_wr8_and_or(sd, PHY_ENB, ~MASK_ENABLE_PHY, MASK_ENABLE_PHY);
953 /* Enable Audio PLL */
954 i2c_wr8(sd, APPL_CTL, MASK_APLL_CPCTL_NORMAL | MASK_APLL_ON);
957 i2c_wr8(sd, DDCIO_CTL, MASK_DDC_PWR_ON);
960 static void tc358840_set_hdmi_audio(struct v4l2_subdev *sd)
962 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
964 i2c_wr8(sd, FORCE_MUTE, 0x00);
965 i2c_wr8(sd, AUTO_CMD0, MASK_AUTO_MUTE7 | MASK_AUTO_MUTE6 |
966 MASK_AUTO_MUTE5 | MASK_AUTO_MUTE4 |
967 MASK_AUTO_MUTE1 | MASK_AUTO_MUTE0);
968 i2c_wr8(sd, AUTO_CMD1, MASK_AUTO_MUTE9);
969 i2c_wr8(sd, AUTO_CMD2, MASK_AUTO_PLAY3 | MASK_AUTO_PLAY2);
970 i2c_wr8(sd, BUFINIT_START, SET_BUFINIT_START_MS(500));
971 i2c_wr8(sd, FS_MUTE, 0x00);
972 i2c_wr8(sd, FS_IMODE, MASK_NLPCM_SMODE | MASK_FS_SMODE);
973 i2c_wr8(sd, ACR_MODE, MASK_CTS_MODE);
974 i2c_wr8(sd, ACR_MDF0, MASK_ACR_L2MDF_1976_PPM | MASK_ACR_L1MDF_976_PPM);
975 i2c_wr8(sd, ACR_MDF1, MASK_ACR_L3MDF_3906_PPM);
977 * TODO: Set output data bit length (currently 16 bit, 8 bit discarded)
979 i2c_wr8(sd, SDO_MODE1, MASK_SDO_FMT_I2S);
980 i2c_wr8(sd, DIV_MODE, SET_DIV_DLY_MS(100));
981 i2c_wr16_and_or(sd, CONFCTL0, 0xFFFF, MASK_AUDCHNUM_2 |
982 MASK_AUDOUTSEL_I2S | MASK_AUTOINDEX);
985 static void tc358840_set_hdmi_info_frame_mode(struct v4l2_subdev *sd)
987 v4l2_dbg(3, debug, sd, "%s(): DUMMY\n", __func__);
989 /* TODO: Check which registers are needed/available */
991 i2c_wr8(sd, PK_INT_MODE, MASK_ISRC2_INT_MODE | MASK_ISRC_INT_MODE |
992 MASK_ACP_INT_MODE | MASK_VS_INT_MODE |
993 MASK_SPD_INT_MODE | MASK_MS_INT_MODE |
994 MASK_AUD_INT_MODE | MASK_AVI_INT_MODE);
995 i2c_wr8(sd, NO_PKT_LIMIT, 0x2c);
996 i2c_wr8(sd, NO_PKT_CLR, 0x53);
997 i2c_wr8(sd, ERR_PK_LIMIT, 0x01);
998 i2c_wr8(sd, NO_PKT_LIMIT2, 0x30);
999 i2c_wr8(sd, NO_GDB_LIMIT, 0x10);
1003 static void tc358840_initial_setup(struct v4l2_subdev *sd)
1005 static struct v4l2_dv_timings default_timing = V4L2_DV_BT_CEA_1920X1080P60;
1006 struct tc358840_state *state = to_state(sd);
1007 struct tc358840_platform_data *pdata = &state->pdata;
1009 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1012 enable_stream(sd, false);
1014 tc358840_sleep_mode(sd, false);
1015 tc358840_reset(sd, MASK_RESET_ALL);
1017 tc358840_init_interrupts(sd);
1019 /* *** Init CSI *** */
1020 tc358840_s_dv_timings(sd, &default_timing);
1022 tc358840_set_ref_clk(sd);
1024 i2c_wr8_and_or(sd, DDC_CTL, ~MASK_DDC5V_MODE,
1025 pdata->ddc5v_delay & MASK_DDC5V_MODE);
1027 i2c_wr8_and_or(sd, EDID_MODE, ~MASK_EDID_MODE_ALL, MASK_RAM_EDDC);
1029 i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_CTL0, 0);
1031 tc358840_set_hdmi_phy(sd);
1033 tc358840_set_hdmi_hdcp(sd, pdata->enable_hdcp);
1034 tc358840_set_hdmi_audio(sd);
1035 tc358840_set_hdmi_info_frame_mode(sd);
1037 /* All CE and IT formats are detected as RGB full range in DVI mode */
1038 i2c_wr8_and_or(sd, VI_MODE, ~MASK_RGB_DVI, 0);
1041 /* --------------- IRQ --------------- */
1043 static void tc358840_format_change(struct v4l2_subdev *sd)
1045 struct tc358840_state *state = to_state(sd);
1046 struct v4l2_dv_timings timings;
1047 static const struct v4l2_event tc358840_ev_fmt = {
1048 .type = V4L2_EVENT_SOURCE_CHANGE,
1049 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1052 if (tc358840_get_detected_timings(sd, &timings)) {
1053 enable_stream(sd, false);
1055 v4l2_info(sd, "%s: No Signal\n", __func__);
1057 if (!v4l2_match_dv_timings(&state->timings, &timings, 0))
1058 enable_stream(sd, false);
1060 v4l2_print_dv_timings(sd->name,
1061 "tc358840_format_change: New format: ",
1065 /* Application gets notified after CSI Tx's are reset */
1067 v4l2_subdev_notify_event(sd, &tc358840_ev_fmt);
1070 static void tc358840_init_interrupts(struct v4l2_subdev *sd)
1074 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1076 /* clear interrupt status registers */
1077 for (i = SYS_INT; i <= MISC_INT; i++) {
1078 /* No interrupt register at Address 0x850A */
1080 i2c_wr8(sd, i, 0xFF);
1083 /* Clear and disable all interrupts */
1084 i2c_wr16(sd, INTSTATUS, MASK_INT_STATUS_MASK_ALL);
1085 i2c_wr16(sd, INTSTATUS, 0x0);
1087 i2c_wr16(sd, INTMASK, MASK_INT_STATUS_MASK_ALL);
1090 static void tc358840_enable_interrupts(struct v4l2_subdev *sd,
1091 bool cable_connected)
1093 v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
1096 if (cable_connected) {
1097 i2c_wr8(sd, SYS_INTM, ~(MASK_DDC | MASK_DVI |
1099 i2c_wr8(sd, SYS_INTM, ~(MASK_DDC) & 0xFF);
1100 i2c_wr8(sd, CLK_INTM, ~MASK_IN_DE_CHG);
1101 i2c_wr8(sd, CBIT_INTM, ~(MASK_CBIT_FS | MASK_AF_LOCK |
1102 MASK_AF_UNLOCK) & 0xFF);
1103 i2c_wr8(sd, AUDIO_INTM, ~MASK_BUFINIT_END);
1104 i2c_wr8(sd, MISC_INTM, ~MASK_SYNC_CHG);
1106 i2c_wr8(sd, SYS_INTM, ~MASK_DDC & 0xFF);
1107 i2c_wr8(sd, CLK_INTM, 0xFF);
1108 i2c_wr8(sd, CBIT_INTM, 0xFF);
1109 i2c_wr8(sd, AUDIO_INTM, 0xFF);
1110 i2c_wr8(sd, MISC_INTM, 0xFF);
1114 static void tc358840_hdmi_audio_int_handler(struct v4l2_subdev *sd,
1117 u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
1118 u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
1120 i2c_wr8(sd, AUDIO_INT, audio_int);
1122 v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
1124 tc358840_s_ctrl_audio_sampling_rate(sd);
1125 tc358840_s_ctrl_audio_present(sd);
1128 static void tc358840_hdmi_misc_int_handler(struct v4l2_subdev *sd,
1131 struct tc358840_state *state = to_state(sd);
1132 u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
1133 u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
1135 i2c_wr8(sd, MISC_INT, misc_int);
1137 v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
1139 if (misc_int & MASK_SYNC_CHG) {
1140 /* Reset the HDMI PHY to try to trigger proper lock on the
1141 * incoming video format. Erase BKSV to prevent that old keys
1142 * are used when a new source is connected. */
1144 state->format_changed = true;
1146 misc_int &= ~MASK_SYNC_CHG;
1152 v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
1153 __func__, misc_int);
1157 static void tc358840_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
1160 u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
1161 u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
1163 i2c_wr8(sd, CBIT_INT, cbit_int);
1165 v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
1167 if (cbit_int & MASK_CBIT_FS) {
1169 v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
1171 tc358840_s_ctrl_audio_sampling_rate(sd);
1173 cbit_int &= ~MASK_CBIT_FS;
1178 if (cbit_int & (MASK_AF_LOCK | MASK_AF_UNLOCK)) {
1180 v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
1182 tc358840_s_ctrl_audio_present(sd);
1184 cbit_int &= ~(MASK_AF_LOCK | MASK_AF_UNLOCK);
1190 v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
1191 __func__, cbit_int);
1195 static void tc358840_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
1197 struct tc358840_state *state = to_state(sd);
1198 u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
1199 u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
1201 /* Bit 7 and bit 6 are set even when they are masked */
1202 i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_OUT_H_CHG);
1204 v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
1206 if (clk_int & (MASK_IN_DE_CHG)) {
1208 v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
1211 /* TODO: Check if also true for tc358840 */
1212 /* If the source switch to a new resolution with the same pixel
1213 * frequency as the existing (e.g. 1080p25 -> 720p50), the
1214 * I_SYNC_CHG interrupt is not always triggered, while the
1215 * I_IN_DE_CHG interrupt seems to work fine. FMT_CHANGE
1216 * notifications are only sent when the signal is stable to
1217 * reduce the number of notifications. */
1218 if (!no_signal(sd) && !no_sync(sd))
1219 state->format_changed = true;
1221 clk_int &= ~MASK_IN_DE_CHG;
1227 v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
1232 static void tc358840_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
1234 struct tc358840_state *state = to_state(sd);
1235 u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
1236 u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
1238 i2c_wr8(sd, SYS_INT, sys_int);
1240 v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
1242 if (sys_int & MASK_DDC) {
1243 bool tx_5v = tx_5v_power_present(sd);
1245 v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
1246 __func__, tx_5v ? "yes" : "no");
1249 tc358840_enable_edid(sd);
1251 tc358840_enable_interrupts(sd, false);
1252 tc358840_disable_edid(sd);
1253 memset(&state->timings, 0, sizeof(state->timings));
1254 tc358840_erase_bksv(sd);
1255 tc358840_update_controls(sd);
1258 sys_int &= ~MASK_DDC;
1263 if (sys_int & MASK_DVI) {
1264 v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
1267 /* Reset the HDMI PHY to try to trigger proper lock on the
1268 * incoming video format. Erase BKSV to prevent that old keys
1269 * are used when a new source is connected. */
1270 if (no_sync(sd) || no_signal(sd))
1271 state->format_changed = true;
1273 sys_int &= ~MASK_DVI;
1278 if (sys_int & MASK_HDMI) {
1279 v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
1282 /* TODO: Check if reg is required. Reg not found in Rev. 1.5 */
1284 i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
1286 sys_int &= ~MASK_HDMI;
1292 v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
1297 /* --------------- CORE OPS --------------- */
1299 static int tc358840_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1301 struct tc358840_state *state = to_state(sd);
1303 unsigned retry = 10;
1305 //disable_irq_nosync(state->pdata.interrupt);
1306 intstatus = i2c_rd16(sd, INTSTATUS);
1308 v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04X\n", __func__, intstatus);
1311 * Need to figure out why these msleeps are needed, and which of these
1312 * are needed. Without msleeps the interrupts just stop.
1314 usleep_range(500, 1000);
1315 state->format_changed = false;
1316 if (intstatus & MASK_HDMI_INT) {
1321 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
1322 usleep_range(500, 1000);
1323 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
1324 usleep_range(500, 1000);
1326 if (hdmi_int0 & MASK_MISC)
1327 tc358840_hdmi_misc_int_handler(sd, handled);
1328 if (hdmi_int1 & MASK_ACBIT)
1329 tc358840_hdmi_cbit_int_handler(sd, handled);
1330 if (hdmi_int1 & MASK_CLK)
1331 tc358840_hdmi_clk_int_handler(sd, handled);
1332 if (hdmi_int1 & MASK_SYS)
1333 tc358840_hdmi_sys_int_handler(sd, handled);
1334 if (hdmi_int1 & MASK_AUD)
1335 tc358840_hdmi_audio_int_handler(sd, handled);
1337 usleep_range(500, 1000);
1338 i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
1339 intstatus &= ~MASK_HDMI_INT;
1340 usleep_range(500, 1000);
1342 /* Display unhandled HDMI interrupts */
1343 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
1345 v4l2_dbg(1, debug, sd,
1346 "%s: Unhandled HDMI_INT0 interrupts: 0x%02X\n",
1347 __func__, hdmi_int0);
1351 usleep_range(500, 1000);
1352 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
1354 v4l2_dbg(1, debug, sd,
1355 "%s: Unhandled HDMI_INT1 interrupts: 0x%02X\n",
1356 __func__, hdmi_int1);
1362 if (handled && state->format_changed) {
1363 tc358840_format_change(sd);
1364 if (no_sync(sd) || no_signal(sd)) {
1365 tc358840_reset_phy(sd);
1366 tc358840_erase_bksv(sd);
1370 if (intstatus & MASK_CSITX0_INT) {
1371 v4l2_dbg(3, debug, sd, "%s: MASK_CSITX0_INT\n", __func__);
1373 i2c_wr16(sd, INTSTATUS, MASK_CSITX0_INT);
1374 intstatus &= ~MASK_CSITX0_INT;
1377 if (intstatus & MASK_CSITX1_INT) {
1378 v4l2_dbg(3, debug, sd, "%s: MASK_CSITX1_INT\n", __func__);
1380 i2c_wr16(sd, INTSTATUS, MASK_CSITX1_INT);
1381 intstatus &= ~MASK_CSITX1_INT;
1385 v4l2_dbg(1, debug, sd,
1386 "%s: Unhandled IntStatus interrupts: 0x%04x\n",
1387 __func__, intstatus);
1389 //enable_irq(state->pdata.interrupt);
1393 static void tc358840_process_isr(struct work_struct *work)
1395 struct tc358840_state *state = container_of(work,
1396 struct tc358840_state, process_isr);
1397 struct v4l2_subdev *sd = &state->sd;
1400 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
1402 mutex_lock(&state->isr_lock);
1403 tc358840_isr(sd, 0, &handled);
1404 mutex_unlock(&state->isr_lock);
1407 static irqreturn_t tc358840_irq_handler(int irq, void *dev_id)
1409 struct v4l2_subdev *sd = dev_id;
1410 struct tc358840_state *state = to_state(sd);
1412 queue_work(state->work_queues, &state->process_isr);
1417 /* --------------- PAD OPS --------------- */
1419 static int tc358840_get_fmt(struct v4l2_subdev *sd,
1420 struct v4l2_subdev_fh *fh,
1421 struct v4l2_subdev_format *format)
1423 struct tc358840_state *state = to_state(sd);
1424 u8 vout_csc = i2c_rd8(sd, VOUT_CSC);
1425 struct v4l2_mbus_framefmt *fmt;
1427 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1429 if (format->pad != 0)
1432 format->format.code = state->mbus_fmt_code;
1433 format->format.width = state->timings.bt.width;
1434 format->format.height = state->timings.bt.height;
1435 format->format.field = V4L2_FIELD_NONE;
1437 switch (vout_csc & MASK_COLOR) {
1438 case MASK_COLOR_RGB_FULL:
1439 case MASK_COLOR_RGB_LIMITED:
1440 format->format.colorspace = V4L2_COLORSPACE_SRGB;
1442 case MASK_COLOR_601_YCBCR_FULL:
1443 case MASK_COLOR_601_YCBCR_LIMITED:
1444 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1446 case MASK_COLOR_709_YCBCR_FULL:
1447 case MASK_COLOR_709_YCBCR_LIMITED:
1448 format->format.colorspace = V4L2_COLORSPACE_REC709;
1451 format->format.colorspace = 0;
1455 fmt = &format->format;
1456 v4l2_dbg(3, debug, sd,
1457 "%s(): width=%d, height=%d, code=0x%08X, field=%d\n",
1458 __func__, fmt->width, fmt->height, fmt->code, fmt->field);
1463 static int tc358840_set_fmt(struct v4l2_subdev *sd,
1464 struct v4l2_subdev_fh *fh,
1465 struct v4l2_subdev_format *format)
1467 struct tc358840_state *state = to_state(sd);
1468 u32 code = format->format.code; /* is overwritten by get_fmt */
1469 int ret = tc358840_get_fmt(sd, fh, format);
1471 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1473 format->format.code = code;
1479 case V4L2_MBUS_FMT_RGB888_1X24:
1480 case V4L2_MBUS_FMT_UYVY8_1X16:
1486 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1489 v4l2_dbg(3, debug, sd, "%s(): format->which=%d\n",
1490 __func__, format->which);
1492 state->mbus_fmt_code = format->format.code;
1493 enable_stream(sd, false);
1494 tc358840_set_csi(sd);
1495 tc358840_set_csi_mbus_config(sd);
1500 static int tc358840_g_edid(struct v4l2_subdev *sd,
1501 struct v4l2_subdev_edid *edid)
1503 struct tc358840_state *state = to_state(sd);
1505 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1507 memset(edid->reserved, 0, sizeof(edid->reserved));
1512 if (edid->start_block == 0 && edid->blocks == 0) {
1513 edid->blocks = state->edid_blocks_written;
1517 if (state->edid_blocks_written == 0)
1520 if (edid->start_block >= state->edid_blocks_written ||
1524 if (edid->start_block + edid->blocks > state->edid_blocks_written)
1525 edid->blocks = state->edid_blocks_written - edid->start_block;
1527 i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
1528 edid->blocks * EDID_BLOCK_SIZE);
1533 static int tc358840_s_edid(struct v4l2_subdev *sd,
1534 struct v4l2_subdev_edid *edid)
1536 struct tc358840_state *state = to_state(sd);
1537 u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
1540 v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
1541 __func__, edid->pad, edid->start_block, edid->blocks);
1543 memset(edid->reserved, 0, sizeof(edid->reserved));
1548 if (edid->start_block != 0)
1551 if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
1552 edid->blocks = EDID_NUM_BLOCKS_MAX;
1556 tc358840_disable_edid(sd);
1558 i2c_wr8(sd, EDID_LEN1, edid_len & 0xFF);
1559 i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
1561 if (edid->blocks == 0) {
1562 state->edid_blocks_written = 0;
1566 for (i = 0; i < 256; i++) {
1568 val = edid->edid[i];
1569 i2c_wr8(sd, EDID_RAM + i, val);
1572 state->edid_blocks_written = edid->blocks;
1574 if (tx_5v_power_present(sd))
1575 tc358840_enable_edid(sd);
1580 static int tc358840_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
1581 struct v4l2_event_subscription *sub)
1583 switch (sub->type) {
1584 case V4L2_EVENT_SOURCE_CHANGE:
1585 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
1586 case V4L2_EVENT_CTRL:
1587 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
1593 /* --------------- VIDEO OPS --------------- */
1595 static int tc358840_g_input_status(struct v4l2_subdev *sd, u32 *status)
1597 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1600 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1601 *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
1603 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1608 static int tc358840_s_dv_timings(struct v4l2_subdev *sd,
1609 struct v4l2_dv_timings *timings)
1611 struct tc358840_state *state = to_state(sd);
1613 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1619 v4l2_print_dv_timings(sd->name, "tc358840_s_dv_timings: ",
1622 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1623 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1627 if (!v4l2_valid_dv_timings(timings, &tc358840_timings_cap, NULL, NULL)) {
1628 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1632 state->timings = *timings;
1634 enable_stream(sd, false);
1635 tc358840_set_csi(sd);
1636 tc358840_set_splitter(sd);
1641 static int tc358840_g_dv_timings(struct v4l2_subdev *sd,
1642 struct v4l2_dv_timings *timings)
1644 struct tc358840_state *state = to_state(sd);
1646 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1648 *timings = state->timings;
1653 static int tc358840_enum_dv_timings(struct v4l2_subdev *sd,
1654 struct v4l2_enum_dv_timings *timings)
1656 v4l2_dbg(3, debug, sd, "%s(): DUMMY\n", __func__);
1658 if (timings->pad != 0)
1661 return v4l2_enum_dv_timings_cap(timings,
1662 &tc358840_timings_cap, NULL, NULL);
1665 static int tc358840_query_dv_timings(struct v4l2_subdev *sd,
1666 struct v4l2_dv_timings *timings)
1670 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1672 ret = tc358840_get_detected_timings(sd, timings);
1677 v4l2_print_dv_timings(sd->name, "tc358840_query_dv_timings: ",
1679 if (!v4l2_valid_dv_timings(timings, &tc358840_timings_cap, NULL, NULL)) {
1680 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1687 static int tc358840_dv_timings_cap(struct v4l2_subdev *sd,
1688 struct v4l2_dv_timings_cap *cap)
1690 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1695 *cap = tc358840_timings_cap;
1700 static int tc358840_g_mbus_config(struct v4l2_subdev *sd,
1701 struct v4l2_mbus_config *cfg)
1703 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1705 cfg->type = V4L2_MBUS_CSI2;
1707 /* Support for non-continuous CSI-2 clock is missing in the driver */
1708 cfg->flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK | V4L2_MBUS_CSI2_CHANNEL_0;
1710 switch (tc358840_num_csi_lanes_in_use(sd)) {
1712 cfg->flags |= V4L2_MBUS_CSI2_1_LANE;
1715 cfg->flags |= V4L2_MBUS_CSI2_2_LANE;
1718 cfg->flags |= V4L2_MBUS_CSI2_3_LANE;
1721 cfg->flags |= V4L2_MBUS_CSI2_4_LANE;
1727 v4l2_dbg(2, debug, sd, "%s: Lanes: 0x%02X\n",
1728 __func__, cfg->flags & 0x0F);
1733 #ifdef CONFIG_VIDEO_ADV_DEBUG
1734 static int tc358840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1736 switch (reg->size) {
1738 reg->val = i2c_rd8(sd, reg->reg);
1741 reg->val = i2c_rd16(sd, reg->reg);
1745 reg->val = i2c_rd32(sd, reg->reg);
1751 static int tc358840_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
1753 switch (reg->size) {
1755 i2c_wr8(sd, reg->reg, reg->val);
1758 i2c_wr16(sd, reg->reg, reg->val);
1762 i2c_wr32(sd, reg->reg, reg->val);
1769 static int tc358840_log_status(struct v4l2_subdev *sd)
1771 struct tc358840_state *state = to_state(sd);
1772 struct v4l2_dv_timings timings;
1773 u8 hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
1774 u16 sysctl = i2c_rd16(sd, SYSCTL);
1775 u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
1776 const int deep_color_mode[4] = { 8, 10, 12, 16 };
1777 static const char * const input_color_space[] = {
1778 "RGB", "YCbCr 601", "Adobe RGB", "YCbCr 709", "NA (4)",
1779 "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
1780 "NA(10)", "NA(11)", "NA(12)", "Adobe YCC 601"};
1782 v4l2_ctrl_subdev_log_status(sd);
1783 v4l2_info(sd, "-----Chip status-----\n");
1784 v4l2_info(sd, "Chip ID: 0x%02x\n",
1785 (i2c_rd16(sd, CHIPID_ADDR) & MASK_CHIPID) >> 8);
1786 v4l2_info(sd, "Chip revision: 0x%02x\n",
1787 i2c_rd16(sd, CHIPID_ADDR) & MASK_REVID);
1788 v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
1789 !!(sysctl & MASK_IRRST),
1790 !!(sysctl & MASK_CECRST),
1791 !!(sysctl & MASK_CTXRST),
1792 !!(sysctl & MASK_HDMIRST));
1793 v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
1794 v4l2_info(sd, "Cable detected (+5V power): %s\n",
1795 hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
1796 v4l2_info(sd, "DDC lines enabled: %s\n",
1797 (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_ALL) ?
1799 v4l2_info(sd, "Hotplug enabled: %s\n",
1800 (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
1802 v4l2_info(sd, "CEC enabled: %s\n",
1803 (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
1804 v4l2_info(sd, "-----Signal status-----\n");
1805 v4l2_info(sd, "TMDS signal detected: %s\n",
1806 hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
1807 v4l2_info(sd, "Stable sync signal: %s\n",
1808 hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
1809 v4l2_info(sd, "PHY PLL locked: %s\n",
1810 hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
1811 v4l2_info(sd, "PHY DE detected: %s\n",
1812 hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
1814 if (tc358840_get_detected_timings(sd, &timings)) {
1815 v4l2_info(sd, "No video detected\n");
1817 v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
1820 v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
1823 v4l2_info(sd, "-----CSI-TX status-----\n");
1824 v4l2_info(sd, "Lanes needed: %d\n",
1825 tc358840_num_csi_lanes_needed(sd));
1826 v4l2_info(sd, "Lanes in use: %d\n",
1827 tc358840_num_csi_lanes_in_use(sd));
1828 v4l2_info(sd, "Splitter %sabled\n",
1829 (i2c_rd16(sd, SPLITTX0_CTRL) & MASK_SPBP) ? "dis" : "en");
1831 v4l2_info(sd, "Waiting for particular sync signal: %s\n",
1832 (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
1834 v4l2_info(sd, "Transmit mode: %s\n",
1835 (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
1837 v4l2_info(sd, "Receive mode: %s\n",
1838 (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
1840 v4l2_info(sd, "Stopped: %s\n",
1841 (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
1843 v4l2_info(sd, "Color space: %s\n",
1844 state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
1845 "YCbCr 422 16-bit" :
1846 state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
1847 "RGB 888 24-bit" : "Unsupported");
1849 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
1850 v4l2_info(sd, "HDCP encrypted content: %s\n",
1851 hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
1852 v4l2_info(sd, "Input color space: %s %s range\n",
1853 input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
1854 (vi_status3 & MASK_LIMITED) ? "limited" : "full");
1857 v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
1859 v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
1860 deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
1861 MASK_S_DEEPCOLOR) >> 2]);
1862 print_avi_infoframe(sd);
1867 static int tc358840_s_stream(struct v4l2_subdev *sd, int enable)
1869 v4l2_dbg(3, debug, sd, "%s():\n", __func__);
1871 return enable_stream(sd, enable);
1874 static int tc358840_s_mbus_fmt(struct v4l2_subdev *sd,
1875 struct v4l2_mbus_framefmt *fmt)
1877 struct v4l2_subdev_format format;
1879 v4l2_dbg(2, debug, sd, "%s()\n", __func__);
1881 memcpy(&format.format, fmt, sizeof(struct v4l2_mbus_framefmt));
1882 format.which = V4L2_SUBDEV_FORMAT_ACTIVE;
1885 return tc358840_set_fmt(sd, NULL, &format);
1888 static int tc358840_g_mbus_fmt(struct v4l2_subdev *sd,
1889 struct v4l2_mbus_framefmt *fmt)
1891 struct v4l2_subdev_format format;
1894 v4l2_dbg(2, debug, sd, "%s()\n", __func__);
1897 ret = tc358840_get_fmt(sd, NULL, &format);
1901 *fmt = format.format;
1906 static int tc358840_try_mbus_fmt(struct v4l2_subdev *sd,
1907 struct v4l2_mbus_framefmt *fmt)
1909 struct v4l2_subdev_format format;
1911 v4l2_dbg(2, debug, sd,
1912 "%s(): width=%d, height=%d, code=0x%04X, field=%d\n",
1913 __func__, fmt->width, fmt->height, fmt->code, fmt->field);
1915 memcpy(&format.format, fmt, sizeof(struct v4l2_mbus_framefmt));
1916 format.which = V4L2_SUBDEV_FORMAT_TRY;
1919 return tc358840_set_fmt(sd, NULL, &format);
1922 static int tc358840_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh,
1923 struct v4l2_subdev_mbus_code_enum *code)
1925 v4l2_dbg(2, debug, sd, "%s()\n", __func__);
1927 if (code->index >= 2)
1930 switch (code->index) {
1932 code->code = V4L2_MBUS_FMT_UYVY8_1X16;
1935 code->code = V4L2_MBUS_FMT_RGB888_1X24;
1941 static int tc358840_enum_framesizes(struct v4l2_subdev *sd,
1942 struct v4l2_frmsizeenum *fsizes)
1944 const struct camera_common_frmfmt *frmfmt = tc358840_frmfmt;
1945 int num_frmfmt = ARRAY_SIZE(tc358840_frmfmt);
1947 v4l2_dbg(2, debug, sd, "%s()\n", __func__);
1949 if (fsizes->pixel_format != V4L2_PIX_FMT_UYVY &&
1950 fsizes->pixel_format != V4L2_PIX_FMT_ABGR32)
1953 if (fsizes->index >= num_frmfmt)
1956 fsizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1957 fsizes->discrete = frmfmt[fsizes->index].size;
1962 static int tc358840_enum_frameintervals(struct v4l2_subdev *sd,
1963 struct v4l2_frmivalenum *fintervals)
1965 const struct camera_common_frmfmt *frmfmt = tc358840_frmfmt;
1966 int num_frmfmt = ARRAY_SIZE(tc358840_frmfmt);
1969 v4l2_dbg(2, debug, sd, "%s()\n", __func__);
1971 if (fintervals->pixel_format != V4L2_PIX_FMT_UYVY &&
1972 fintervals->pixel_format != V4L2_PIX_FMT_ABGR32)
1975 for (i = 0; i < num_frmfmt; i++) {
1976 if (frmfmt[i].size.width == fintervals->width &&
1977 frmfmt[i].size.height == fintervals->height)
1980 if (i >= num_frmfmt)
1983 if (fintervals->index >= frmfmt[i].num_framerates)
1986 fintervals->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1987 fintervals->discrete.numerator = 1;
1988 fintervals->discrete.denominator =
1989 frmfmt[i].framerates[fintervals->index];
1994 static int tc358840_s_power(struct v4l2_subdev *sd, int on)
1999 static struct v4l2_subdev_video_ops tc358840_subdev_video_ops = {
2000 .g_input_status = tc358840_g_input_status,
2001 .s_dv_timings = tc358840_s_dv_timings,
2002 .g_dv_timings = tc358840_g_dv_timings,
2003 .query_dv_timings = tc358840_query_dv_timings,
2004 .g_mbus_config = tc358840_g_mbus_config,
2005 .s_stream = tc358840_s_stream,
2007 .s_mbus_fmt = tc358840_s_mbus_fmt,
2008 .g_mbus_fmt = tc358840_g_mbus_fmt,
2009 .try_mbus_fmt = tc358840_try_mbus_fmt,
2011 .enum_framesizes = tc358840_enum_framesizes,
2012 .enum_frameintervals = tc358840_enum_frameintervals,
2015 static struct v4l2_subdev_core_ops tc358840_subdev_core_ops = {
2016 .s_power = tc358840_s_power,
2017 .log_status = tc358840_log_status,
2018 .interrupt_service_routine = tc358840_isr,
2019 .subscribe_event = tc358840_subscribe_event,
2020 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2021 #ifdef CONFIG_VIDEO_ADV_DEBUG
2022 .g_register = tc358840_g_register,
2023 .s_register = tc358840_s_register,
2027 static const struct v4l2_subdev_pad_ops tc358840_pad_ops = {
2028 .set_fmt = tc358840_set_fmt,
2029 .get_fmt = tc358840_get_fmt,
2030 .enum_mbus_code = tc358840_enum_mbus_code,
2031 .get_edid = tc358840_g_edid,
2032 .set_edid = tc358840_s_edid,
2033 .dv_timings_cap = tc358840_dv_timings_cap,
2034 .enum_dv_timings = tc358840_enum_dv_timings,
2037 static struct v4l2_subdev_ops tc358840_ops = {
2038 .core = &tc358840_subdev_core_ops,
2039 .video = &tc358840_subdev_video_ops,
2040 .pad = &tc358840_pad_ops,
2044 /* --------------- CUSTOM CTRLS --------------- */
2046 static const struct v4l2_ctrl_config tc358840_ctrl_audio_sampling_rate = {
2047 .id = TC358840_CID_AUDIO_SAMPLING_RATE,
2048 .name = "Audio sampling rate",
2049 .type = V4L2_CTRL_TYPE_INTEGER,
2054 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2057 static const struct v4l2_ctrl_config tc358840_ctrl_audio_present = {
2058 .id = TC358840_CID_AUDIO_PRESENT,
2059 .name = "Audio present",
2060 .type = V4L2_CTRL_TYPE_BOOLEAN,
2065 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2068 static const struct v4l2_ctrl_config tc358840_ctrl_splitter_width = {
2069 .id = TC358840_CID_SPLITTER_WIDTH,
2070 .name = "Splitter Width",
2071 .type = V4L2_CTRL_TYPE_INTEGER,
2076 .flags = V4L2_CTRL_FLAG_READ_ONLY,
2079 /* --------------- PROBE / REMOVE --------------- */
2083 static bool tc358840_parse_dt(struct tc358840_platform_data *pdata,
2084 struct i2c_client *client)
2086 struct device_node *node = client->dev.of_node;
2087 const u32 *property;
2089 v4l_dbg(1, debug, client, "Device Tree Parameters:\n");
2091 pdata->reset_gpio = of_get_named_gpio(node, "reset-gpios", 0);
2092 if (pdata->reset_gpio == 0)
2094 v4l_dbg(1, debug, client, "reset_gpio = %d\n", pdata->reset_gpio);
2096 // if (v4l2_of_parse_endpoint(node, &pdata->endpoint))
2099 property = of_get_property(node, "refclk_hz", NULL);
2100 if (property == NULL)
2102 pdata->refclk_hz = be32_to_cpup(property);
2103 v4l_dbg(1, debug, client, "refclk_hz = %d\n", be32_to_cpup(property));
2105 property = of_get_property(node, "ddc5v_delay", NULL);
2106 if (property == NULL)
2108 pdata->ddc5v_delay = be32_to_cpup(property);
2109 if (pdata->ddc5v_delay > DDC5V_DELAY_MAX)
2110 pdata->ddc5v_delay = DDC5V_DELAY_MAX;
2111 v4l_dbg(1, debug, client, "ddc5v_delay = %d ms\n",
2112 50 * pdata->ddc5v_delay);
2114 property = of_get_property(node, "enable_hdcp", NULL);
2115 if (property == NULL)
2117 pdata->enable_hdcp = be32_to_cpup(property);
2118 v4l_dbg(1, debug, client, "enable_hdcp = %d\n", be32_to_cpup(property));
2120 property = of_get_property(node, "csi_port", NULL);
2121 if (property == NULL)
2123 pdata->csi_port = be32_to_cpup(property);
2124 v4l_dbg(1, debug, client, "csi_port = %d\n", be32_to_cpup(property));
2126 property = of_get_property(node, "lineinitcnt", NULL);
2127 if (property == NULL)
2129 pdata->lineinitcnt = be32_to_cpup(property);
2130 v4l_dbg(1, debug, client, "lineinitcnt = %d\n", be32_to_cpup(property));
2132 property = of_get_property(node, "lptxtimecnt", NULL);
2133 if (property == NULL)
2135 pdata->lptxtimecnt = be32_to_cpup(property);
2136 v4l_dbg(1, debug, client, "lptxtimecnt = %d\n", be32_to_cpup(property));
2138 property = of_get_property(node, "tclk_headercnt", NULL);
2139 if (property == NULL)
2141 pdata->tclk_headercnt = be32_to_cpup(property);
2142 v4l_dbg(1, debug, client, "tclk_headercnt = %d\n",
2143 be32_to_cpup(property));
2145 property = of_get_property(node, "tclk_trailcnt", NULL);
2146 if (property == NULL)
2148 pdata->tclk_trailcnt = be32_to_cpup(property);
2149 v4l_dbg(1, debug, client, "tclk_trailcnt = %d\n",
2150 be32_to_cpup(property));
2152 property = of_get_property(node, "ths_headercnt", NULL);
2153 if (property == NULL)
2155 pdata->ths_headercnt = be32_to_cpup(property);
2156 v4l_dbg(1, debug, client, "ths_headercnt = %d\n",
2157 be32_to_cpup(property));
2159 property = of_get_property(node, "twakeup", NULL);
2160 if (property == NULL)
2162 pdata->twakeup = be32_to_cpup(property);
2163 v4l_dbg(1, debug, client, "twakeup = %d\n", be32_to_cpup(property));
2165 property = of_get_property(node, "tclk_postcnt", NULL);
2166 if (property == NULL)
2168 pdata->tclk_postcnt = be32_to_cpup(property);
2169 v4l_dbg(1, debug, client, "tclk_postcnt = %d\n",
2170 be32_to_cpup(property));
2172 property = of_get_property(node, "ths_trailcnt", NULL);
2173 if (property == NULL)
2175 pdata->ths_trailcnt = be32_to_cpup(property);
2176 v4l_dbg(1, debug, client, "ths_trailcnt = %d\n",
2177 be32_to_cpup(property));
2179 property = of_get_property(node, "hstxvregcnt", NULL);
2180 if (property == NULL)
2182 pdata->hstxvregcnt = be32_to_cpup(property);
2183 v4l_dbg(1, debug, client, "hstxvregcnt = %d\n", be32_to_cpup(property));
2185 property = of_get_property(node, "pll_prd", NULL);
2186 if (property == NULL)
2188 pdata->pll_prd = be32_to_cpup(property);
2189 v4l_dbg(1, debug, client, "pll_prd = %d\n", be32_to_cpup(property));
2191 property = of_get_property(node, "pll_fbd", NULL);
2192 if (property == NULL)
2194 pdata->pll_fbd = be32_to_cpup(property);
2195 v4l_dbg(1, debug, client, "pll_fbd = %d\n", be32_to_cpup(property));
2201 static int tc358840_pwr_init(struct tc358840_platform_data *pdata,
2202 struct i2c_client *client)
2204 struct device_node *node = client->dev.of_node;
2207 struct regulator *dvdd;
2208 struct regulator *iovdd;
2210 err = camera_common_regulator_get(client, &iovdd, "vif");
2212 dev_err(&client->dev, "cannot get regulator vif %d\n", err);
2216 err = camera_common_regulator_get(client, &dvdd, "vdig");
2218 dev_err(&client->dev, "cannot get regulator vdig %d\n", err);
2223 cam2_rst = of_get_named_gpio(node, "cam2_rst", 0);
2226 err = gpio_request(cam2_rst, "cam2-rst");
2228 dev_err(&client->dev,
2229 "cam2 rst gpio request failed %d\n", err);
2231 gpio_direction_output(cam2_rst, 1);
2234 err = regulator_enable(dvdd);
2236 dev_err(&client->dev, "cannot enable regulator vif %d\n", err);
2241 err = regulator_enable(iovdd);
2243 dev_err(&client->dev, "cannot enable regulator vdig %d\n", err);
2247 gpio_direction_output(cam2_rst, 1);
2252 static int tc358840_verify_chipid(struct v4l2_subdev *sd)
2256 cid = i2c_rd16(sd, CHIPID_ADDR);
2257 if (cid != TC358840_CHIPID) {
2258 v4l2_err(sd, "Invalid chip ID 0x%04X\n", cid);
2262 v4l2_dbg(1, debug, sd, "TC358840 ChipID 0x%02x, Revision 0x%02x\n",
2263 (cid & MASK_CHIPID) >> 8, cid & MASK_REVID);
2268 static int tc358840_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
2270 struct i2c_client *client = v4l2_get_subdevdata(sd);
2272 dev_dbg(&client->dev, "%s:\n", __func__);
2276 static const struct v4l2_subdev_internal_ops tc358840_subdev_internal_ops = {
2277 .open = tc358840_open,
2280 static const struct media_entity_operations tc358840_media_ops = {
2281 #ifdef CONFIG_MEDIA_CONTROLLER
2282 .link_validate = v4l2_subdev_link_validate,
2286 static int tc358840_probe(struct i2c_client *client, const struct i2c_device_id *id)
2288 struct v4l2_subdev_edid sd_edid = {
2292 struct tc358840_state *state;
2293 struct v4l2_subdev *sd;
2296 state = devm_kzalloc(&client->dev, sizeof(struct tc358840_state), GFP_KERNEL);
2300 if (client->dev.of_node) {
2301 if (!tc358840_parse_dt(&state->pdata, client)) {
2302 v4l_err(client, "Couldn't parse device tree\n");
2305 if (!tc358840_pwr_init(&state->pdata, client)) {
2306 v4l_err(client, "Couldn't power init\n");
2310 if (!client->dev.platform_data) {
2311 v4l_err(client, "No platform data!\n");
2314 state->pdata = *(struct tc358840_platform_data *)client->dev.platform_data;
2317 state->i2c_client = client;
2320 i2c_set_clientdata(client, state);
2322 v4l2_i2c_subdev_init(sd, client, &tc358840_ops);
2324 /* Release System Reset (pin K8) */
2325 v4l2_info(sd, "Releasing System Reset (gpio 0x%04X)\n",
2326 state->pdata.reset_gpio);
2327 if (!gpio_is_valid(state->pdata.reset_gpio)) {
2328 v4l_err(client, "Reset GPIO is invalid!\n");
2329 return state->pdata.reset_gpio;
2331 err = devm_gpio_request_one(&client->dev, state->pdata.reset_gpio,
2332 GPIOF_OUT_INIT_HIGH, "tc358840-reset");
2334 dev_err(&client->dev,
2335 "Failed to request Reset GPIO 0x%04X: %d\n",
2336 state->pdata.reset_gpio, err);
2341 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2343 v4l_info(client, "Chip found @ 7h%02X (%s)\n",
2344 client->addr, client->adapter->name);
2347 /* Verify chip ID */
2348 err = tc358840_verify_chipid(sd);
2352 /* Control Handlers */
2353 v4l2_ctrl_handler_init(&state->hdl, 4);
2355 /* Custom controls */
2356 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
2357 V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
2359 state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
2360 &tc358840_ctrl_audio_sampling_rate, NULL);
2362 state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
2363 &tc358840_ctrl_audio_present, NULL);
2365 state->splitter_width_ctrl = v4l2_ctrl_new_custom(&state->hdl,
2366 &tc358840_ctrl_splitter_width, NULL);
2368 if (state->hdl.error) {
2369 err = state->hdl.error;
2373 sd->ctrl_handler = &state->hdl;
2375 if (tc358840_update_controls(sd)) {
2381 state->work_queues = create_singlethread_workqueue(client->name);
2382 if (!state->work_queues) {
2383 v4l2_err(sd, "Could not create work queue!\n");
2387 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2388 tc358840_delayed_work_enable_hotplug);
2389 INIT_WORK(&state->process_isr, tc358840_process_isr);
2390 mutex_init(&state->isr_lock);
2393 state->mbus_fmt_code = V4L2_MBUS_FMT_UYVY8_1X16;
2394 tc358840_initial_setup(sd);
2396 tc358840_set_csi_mbus_config(sd);
2400 err = devm_request_threaded_irq(&state->i2c_client->dev,
2401 client->irq, NULL, tc358840_irq_handler,
2402 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
2403 sd->name, (void *)sd);
2405 v4l2_err(sd, "Could not request interrupt %d!\n",
2411 tc358840_enable_interrupts(sd, tx_5v_power_present(sd));
2414 * FIXME: Don't know what MASK_CSITX0_INT and MASK_CSITX1_INT do.
2415 * Thus, disable them for now...
2418 i2c_wr16(sd, INTMASK, ~(MASK_HDMI_INT | MASK_CSITX0_INT |
2419 MASK_CSITX1_INT) & 0xFFFF);
2421 i2c_wr16(sd, INTMASK, ~(MASK_HDMI_INT) & 0xFFFF);
2423 v4l2_ctrl_handler_setup(sd->ctrl_handler);
2425 v4l2_info(sd, "%s found @ 7h%02X (%s)\n", client->name,
2426 client->addr, client->adapter->name);
2428 sd->dev = &client->dev;
2429 sd->internal_ops = &tc358840_subdev_internal_ops;
2430 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2431 #if defined(CONFIG_MEDIA_CONTROLLER)
2432 state->pad[0].flags = MEDIA_PAD_FL_SOURCE;
2433 state->pad[1].flags = MEDIA_PAD_FL_SOURCE;
2434 sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
2435 sd->entity.ops = &tc358840_media_ops;
2436 err = media_entity_init(&sd->entity, 2, state->pad, 0);
2438 dev_err(&client->dev, "unable to init media entity\n");
2443 /* Temporary EDID. Should be set by userspace */
2444 tc358840_s_edid(sd, &sd_edid);
2446 err = v4l2_async_register_subdev(sd);
2451 v4l2_ctrl_handler_free(&state->hdl);
2455 static int tc358840_remove(struct i2c_client *client)
2457 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2459 v4l_dbg(1, debug, client, "%s()\n", __func__);
2461 #if defined(CONFIG_MEDIA_CONTROLLER)
2462 media_entity_cleanup(&sd->entity);
2467 static const struct i2c_device_id tc358840_id[] = {
2472 MODULE_DEVICE_TABLE(i2c, tc358840_id);
2475 static const struct of_device_id tc358840_of_table[] = {
2476 { .compatible = "toshiba,tc358840" },
2479 MODULE_DEVICE_TABLE(of, tc358840_of_table);
2482 static struct i2c_driver tc358840_driver = {
2484 .of_match_table = of_match_ptr(tc358840_of_table),
2486 .owner = THIS_MODULE,
2488 .probe = tc358840_probe,
2489 .remove = tc358840_remove,
2490 .id_table = tc358840_id,
2492 module_i2c_driver(tc358840_driver);
2494 MODULE_DESCRIPTION("Driver for Toshiba TC358840 HDMI to CSI-2 Bridge");
2495 MODULE_AUTHOR("Armin Weiss (weii@zhaw.ch)");
2496 MODULE_LICENSE("GPL v2");