155e5927eSHans Verkuil // SPDX-License-Identifier: GPL-2.0-only
2d32d9864SMats Randgaard /*
3d32d9864SMats Randgaard * tc358743 - Toshiba HDMI to CSI-2 bridge
4d32d9864SMats Randgaard *
5d32d9864SMats Randgaard * Copyright 2015 Cisco Systems, Inc. and/or its affiliates. All rights
6d32d9864SMats Randgaard * reserved.
7d32d9864SMats Randgaard */
8d32d9864SMats Randgaard
9d32d9864SMats Randgaard /*
10d32d9864SMats Randgaard * References (c = chapter, p = page):
11d32d9864SMats Randgaard * REF_01 - Toshiba, TC358743XBG (H2C), Functional Specification, Rev 0.60
12d32d9864SMats Randgaard * REF_02 - Toshiba, TC358743XBG_HDMI-CSI_Tv11p_nm.xls
13d32d9864SMats Randgaard */
14d32d9864SMats Randgaard
15d32d9864SMats Randgaard #include <linux/kernel.h>
16d32d9864SMats Randgaard #include <linux/module.h>
17d32d9864SMats Randgaard #include <linux/slab.h>
18d32d9864SMats Randgaard #include <linux/i2c.h>
1925614824SPhilipp Zabel #include <linux/clk.h>
20d32d9864SMats Randgaard #include <linux/delay.h>
2125614824SPhilipp Zabel #include <linux/gpio/consumer.h>
22d747b806SPhilipp Zabel #include <linux/interrupt.h>
234e66a52aSDave Stevenson #include <linux/timer.h>
24859969b3SSakari Ailus #include <linux/of_graph.h>
25d32d9864SMats Randgaard #include <linux/videodev2.h>
26d32d9864SMats Randgaard #include <linux/workqueue.h>
27d32d9864SMats Randgaard #include <linux/v4l2-dv-timings.h>
28d32d9864SMats Randgaard #include <linux/hdmi.h>
29a0ec8d1dSHans Verkuil #include <media/cec.h>
30d32d9864SMats Randgaard #include <media/v4l2-dv-timings.h>
31d32d9864SMats Randgaard #include <media/v4l2-device.h>
32d32d9864SMats Randgaard #include <media/v4l2-ctrls.h>
331140f919SPhilipp Zabel #include <media/v4l2-event.h>
34859969b3SSakari Ailus #include <media/v4l2-fwnode.h>
35b5dcee22SMauro Carvalho Chehab #include <media/i2c/tc358743.h>
36d32d9864SMats Randgaard
37d32d9864SMats Randgaard #include "tc358743_regs.h"
38d32d9864SMats Randgaard
39d32d9864SMats Randgaard static int debug;
40d32d9864SMats Randgaard module_param(debug, int, 0644);
41d32d9864SMats Randgaard MODULE_PARM_DESC(debug, "debug level (0-3)");
42d32d9864SMats Randgaard
43d32d9864SMats Randgaard MODULE_DESCRIPTION("Toshiba TC358743 HDMI to CSI-2 bridge driver");
44d32d9864SMats Randgaard MODULE_AUTHOR("Ramakrishnan Muthukrishnan <ram@rkrishnan.org>");
45d32d9864SMats Randgaard MODULE_AUTHOR("Mikhail Khelik <mkhelik@cisco.com>");
46d32d9864SMats Randgaard MODULE_AUTHOR("Mats Randgaard <matrandg@cisco.com>");
47d32d9864SMats Randgaard MODULE_LICENSE("GPL");
48d32d9864SMats Randgaard
49d32d9864SMats Randgaard #define EDID_NUM_BLOCKS_MAX 8
50d32d9864SMats Randgaard #define EDID_BLOCK_SIZE 128
51d32d9864SMats Randgaard
52fcae73faSMats Randgaard #define I2C_MAX_XFER_SIZE (EDID_BLOCK_SIZE + 2)
531d88f831SMauro Carvalho Chehab
54a0ec8d1dSHans Verkuil #define POLL_INTERVAL_CEC_MS 10
554e66a52aSDave Stevenson #define POLL_INTERVAL_MS 1000
564e66a52aSDave Stevenson
57d32d9864SMats Randgaard static const struct v4l2_dv_timings_cap tc358743_timings_cap = {
58d32d9864SMats Randgaard .type = V4L2_DV_BT_656_1120,
59d32d9864SMats Randgaard /* keep this initialization for compatibility with GCC < 4.4.6 */
60d32d9864SMats Randgaard .reserved = { 0 },
61d32d9864SMats Randgaard /* Pixel clock from REF_01 p. 20. Min/max height/width are unknown */
622912289aSHans Verkuil V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 13000000, 165000000,
63d32d9864SMats Randgaard V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
64d32d9864SMats Randgaard V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
65d32d9864SMats Randgaard V4L2_DV_BT_CAP_PROGRESSIVE |
66d32d9864SMats Randgaard V4L2_DV_BT_CAP_REDUCED_BLANKING |
67d32d9864SMats Randgaard V4L2_DV_BT_CAP_CUSTOM)
68d32d9864SMats Randgaard };
69d32d9864SMats Randgaard
70d32d9864SMats Randgaard struct tc358743_state {
71d32d9864SMats Randgaard struct tc358743_platform_data pdata;
7294d964e5SLaurent Pinchart struct v4l2_mbus_config_mipi_csi2 bus;
73d32d9864SMats Randgaard struct v4l2_subdev sd;
74d32d9864SMats Randgaard struct media_pad pad;
75d32d9864SMats Randgaard struct v4l2_ctrl_handler hdl;
76d32d9864SMats Randgaard struct i2c_client *i2c_client;
77d32d9864SMats Randgaard /* CONFCTL is modified in ops and tc358743_hdmi_sys_int_handler */
78d32d9864SMats Randgaard struct mutex confctl_mutex;
79d32d9864SMats Randgaard
80d32d9864SMats Randgaard /* controls */
81d32d9864SMats Randgaard struct v4l2_ctrl *detect_tx_5v_ctrl;
82d32d9864SMats Randgaard struct v4l2_ctrl *audio_sampling_rate_ctrl;
83d32d9864SMats Randgaard struct v4l2_ctrl *audio_present_ctrl;
84d32d9864SMats Randgaard
85d32d9864SMats Randgaard struct delayed_work delayed_work_enable_hotplug;
86d32d9864SMats Randgaard
874e66a52aSDave Stevenson struct timer_list timer;
884e66a52aSDave Stevenson struct work_struct work_i2c_poll;
894e66a52aSDave Stevenson
90d32d9864SMats Randgaard /* edid */
91d32d9864SMats Randgaard u8 edid_blocks_written;
92d32d9864SMats Randgaard
93d32d9864SMats Randgaard struct v4l2_dv_timings timings;
94d32d9864SMats Randgaard u32 mbus_fmt_code;
9559e34ba8SMats Randgaard u8 csi_lanes_in_use;
9625614824SPhilipp Zabel
9725614824SPhilipp Zabel struct gpio_desc *reset_gpio;
98a0ec8d1dSHans Verkuil
99a0ec8d1dSHans Verkuil struct cec_adapter *cec_adap;
100d32d9864SMats Randgaard };
101d32d9864SMats Randgaard
102d32d9864SMats Randgaard static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
103d32d9864SMats Randgaard bool cable_connected);
104d32d9864SMats Randgaard static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
105d32d9864SMats Randgaard
to_state(struct v4l2_subdev * sd)106d32d9864SMats Randgaard static inline struct tc358743_state *to_state(struct v4l2_subdev *sd)
107d32d9864SMats Randgaard {
108d32d9864SMats Randgaard return container_of(sd, struct tc358743_state, sd);
109d32d9864SMats Randgaard }
110d32d9864SMats Randgaard
111d32d9864SMats Randgaard /* --------------- I2C --------------- */
112d32d9864SMats Randgaard
i2c_rd(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)113d32d9864SMats Randgaard static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
114d32d9864SMats Randgaard {
115d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
116d32d9864SMats Randgaard struct i2c_client *client = state->i2c_client;
117d32d9864SMats Randgaard int err;
118d32d9864SMats Randgaard u8 buf[2] = { reg >> 8, reg & 0xff };
119d32d9864SMats Randgaard struct i2c_msg msgs[] = {
120d32d9864SMats Randgaard {
121d32d9864SMats Randgaard .addr = client->addr,
122d32d9864SMats Randgaard .flags = 0,
123d32d9864SMats Randgaard .len = 2,
124d32d9864SMats Randgaard .buf = buf,
125d32d9864SMats Randgaard },
126d32d9864SMats Randgaard {
127d32d9864SMats Randgaard .addr = client->addr,
128d32d9864SMats Randgaard .flags = I2C_M_RD,
129d32d9864SMats Randgaard .len = n,
130d32d9864SMats Randgaard .buf = values,
131d32d9864SMats Randgaard },
132d32d9864SMats Randgaard };
133d32d9864SMats Randgaard
134d32d9864SMats Randgaard err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
135d32d9864SMats Randgaard if (err != ARRAY_SIZE(msgs)) {
136fae4280eSAlexander Stein v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed: %d\n",
137fae4280eSAlexander Stein __func__, reg, client->addr, err);
138d32d9864SMats Randgaard }
139d32d9864SMats Randgaard }
140d32d9864SMats Randgaard
i2c_wr(struct v4l2_subdev * sd,u16 reg,u8 * values,u32 n)141d32d9864SMats Randgaard static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
142d32d9864SMats Randgaard {
143d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
144d32d9864SMats Randgaard struct i2c_client *client = state->i2c_client;
145d32d9864SMats Randgaard int err, i;
146d32d9864SMats Randgaard struct i2c_msg msg;
147fcae73faSMats Randgaard u8 data[I2C_MAX_XFER_SIZE];
1481d88f831SMauro Carvalho Chehab
149fcae73faSMats Randgaard if ((2 + n) > I2C_MAX_XFER_SIZE) {
150fcae73faSMats Randgaard n = I2C_MAX_XFER_SIZE - 2;
1511d88f831SMauro Carvalho Chehab v4l2_warn(sd, "i2c wr reg=%04x: len=%d is too big!\n",
1521d88f831SMauro Carvalho Chehab reg, 2 + n);
153fcae73faSMats Randgaard }
154d32d9864SMats Randgaard
155d32d9864SMats Randgaard msg.addr = client->addr;
156d32d9864SMats Randgaard msg.buf = data;
157d32d9864SMats Randgaard msg.len = 2 + n;
158d32d9864SMats Randgaard msg.flags = 0;
159d32d9864SMats Randgaard
160d32d9864SMats Randgaard data[0] = reg >> 8;
161d32d9864SMats Randgaard data[1] = reg & 0xff;
162d32d9864SMats Randgaard
163d32d9864SMats Randgaard for (i = 0; i < n; i++)
164d32d9864SMats Randgaard data[2 + i] = values[i];
165d32d9864SMats Randgaard
166d32d9864SMats Randgaard err = i2c_transfer(client->adapter, &msg, 1);
167d32d9864SMats Randgaard if (err != 1) {
168fae4280eSAlexander Stein v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed: %d\n",
169fae4280eSAlexander Stein __func__, reg, client->addr, err);
170d32d9864SMats Randgaard return;
171d32d9864SMats Randgaard }
172d32d9864SMats Randgaard
173d32d9864SMats Randgaard if (debug < 3)
174d32d9864SMats Randgaard return;
175d32d9864SMats Randgaard
176d32d9864SMats Randgaard switch (n) {
177d32d9864SMats Randgaard case 1:
178d32d9864SMats Randgaard v4l2_info(sd, "I2C write 0x%04x = 0x%02x",
179d32d9864SMats Randgaard reg, data[2]);
180d32d9864SMats Randgaard break;
181d32d9864SMats Randgaard case 2:
182d32d9864SMats Randgaard v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x",
183d32d9864SMats Randgaard reg, data[3], data[2]);
184d32d9864SMats Randgaard break;
185d32d9864SMats Randgaard case 4:
186d32d9864SMats Randgaard v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x%02x%02x",
187d32d9864SMats Randgaard reg, data[5], data[4], data[3], data[2]);
188d32d9864SMats Randgaard break;
189d32d9864SMats Randgaard default:
190d32d9864SMats Randgaard v4l2_info(sd, "I2C write %d bytes from address 0x%04x\n",
191d32d9864SMats Randgaard n, reg);
192d32d9864SMats Randgaard }
193d32d9864SMats Randgaard }
194d32d9864SMats Randgaard
i2c_rdreg(struct v4l2_subdev * sd,u16 reg,u32 n)1953538aa6eSArnd Bergmann static noinline u32 i2c_rdreg(struct v4l2_subdev *sd, u16 reg, u32 n)
1963538aa6eSArnd Bergmann {
1973538aa6eSArnd Bergmann __le32 val = 0;
1983538aa6eSArnd Bergmann
1993538aa6eSArnd Bergmann i2c_rd(sd, reg, (u8 __force *)&val, n);
2003538aa6eSArnd Bergmann
2013538aa6eSArnd Bergmann return le32_to_cpu(val);
2023538aa6eSArnd Bergmann }
2033538aa6eSArnd Bergmann
i2c_wrreg(struct v4l2_subdev * sd,u16 reg,u32 val,u32 n)2043538aa6eSArnd Bergmann static noinline void i2c_wrreg(struct v4l2_subdev *sd, u16 reg, u32 val, u32 n)
2053538aa6eSArnd Bergmann {
2063538aa6eSArnd Bergmann __le32 raw = cpu_to_le32(val);
2073538aa6eSArnd Bergmann
2083538aa6eSArnd Bergmann i2c_wr(sd, reg, (u8 __force *)&raw, n);
2093538aa6eSArnd Bergmann }
2103538aa6eSArnd Bergmann
i2c_rd8(struct v4l2_subdev * sd,u16 reg)211d32d9864SMats Randgaard static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
212d32d9864SMats Randgaard {
2133538aa6eSArnd Bergmann return i2c_rdreg(sd, reg, 1);
214d32d9864SMats Randgaard }
215d32d9864SMats Randgaard
i2c_wr8(struct v4l2_subdev * sd,u16 reg,u8 val)216d32d9864SMats Randgaard static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
217d32d9864SMats Randgaard {
2183538aa6eSArnd Bergmann i2c_wrreg(sd, reg, val, 1);
219d32d9864SMats Randgaard }
220d32d9864SMats Randgaard
i2c_wr8_and_or(struct v4l2_subdev * sd,u16 reg,u8 mask,u8 val)221d32d9864SMats Randgaard static void i2c_wr8_and_or(struct v4l2_subdev *sd, u16 reg,
222d32d9864SMats Randgaard u8 mask, u8 val)
223d32d9864SMats Randgaard {
224f2c61f98SPhilipp Zabel i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 1) & mask) | val, 1);
225d32d9864SMats Randgaard }
226d32d9864SMats Randgaard
i2c_rd16(struct v4l2_subdev * sd,u16 reg)227d32d9864SMats Randgaard static u16 i2c_rd16(struct v4l2_subdev *sd, u16 reg)
228d32d9864SMats Randgaard {
2293538aa6eSArnd Bergmann return i2c_rdreg(sd, reg, 2);
230d32d9864SMats Randgaard }
231d32d9864SMats Randgaard
i2c_wr16(struct v4l2_subdev * sd,u16 reg,u16 val)232d32d9864SMats Randgaard static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
233d32d9864SMats Randgaard {
2343538aa6eSArnd Bergmann i2c_wrreg(sd, reg, val, 2);
235d32d9864SMats Randgaard }
236d32d9864SMats Randgaard
i2c_wr16_and_or(struct v4l2_subdev * sd,u16 reg,u16 mask,u16 val)237d32d9864SMats Randgaard static void i2c_wr16_and_or(struct v4l2_subdev *sd, u16 reg, u16 mask, u16 val)
238d32d9864SMats Randgaard {
2393538aa6eSArnd Bergmann i2c_wrreg(sd, reg, (i2c_rdreg(sd, reg, 2) & mask) | val, 2);
240d32d9864SMats Randgaard }
241d32d9864SMats Randgaard
i2c_rd32(struct v4l2_subdev * sd,u16 reg)242d32d9864SMats Randgaard static u32 i2c_rd32(struct v4l2_subdev *sd, u16 reg)
243d32d9864SMats Randgaard {
2443538aa6eSArnd Bergmann return i2c_rdreg(sd, reg, 4);
245d32d9864SMats Randgaard }
246d32d9864SMats Randgaard
i2c_wr32(struct v4l2_subdev * sd,u16 reg,u32 val)247d32d9864SMats Randgaard static void i2c_wr32(struct v4l2_subdev *sd, u16 reg, u32 val)
248d32d9864SMats Randgaard {
2493538aa6eSArnd Bergmann i2c_wrreg(sd, reg, val, 4);
250d32d9864SMats Randgaard }
251d32d9864SMats Randgaard
252d32d9864SMats Randgaard /* --------------- STATUS --------------- */
253d32d9864SMats Randgaard
is_hdmi(struct v4l2_subdev * sd)254d32d9864SMats Randgaard static inline bool is_hdmi(struct v4l2_subdev *sd)
255d32d9864SMats Randgaard {
256d32d9864SMats Randgaard return i2c_rd8(sd, SYS_STATUS) & MASK_S_HDMI;
257d32d9864SMats Randgaard }
258d32d9864SMats Randgaard
tx_5v_power_present(struct v4l2_subdev * sd)259d32d9864SMats Randgaard static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
260d32d9864SMats Randgaard {
261d32d9864SMats Randgaard return i2c_rd8(sd, SYS_STATUS) & MASK_S_DDC5V;
262d32d9864SMats Randgaard }
263d32d9864SMats Randgaard
no_signal(struct v4l2_subdev * sd)264d32d9864SMats Randgaard static inline bool no_signal(struct v4l2_subdev *sd)
265d32d9864SMats Randgaard {
266d32d9864SMats Randgaard return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_TMDS);
267d32d9864SMats Randgaard }
268d32d9864SMats Randgaard
no_sync(struct v4l2_subdev * sd)269d32d9864SMats Randgaard static inline bool no_sync(struct v4l2_subdev *sd)
270d32d9864SMats Randgaard {
271d32d9864SMats Randgaard return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_SYNC);
272d32d9864SMats Randgaard }
273d32d9864SMats Randgaard
audio_present(struct v4l2_subdev * sd)274d32d9864SMats Randgaard static inline bool audio_present(struct v4l2_subdev *sd)
275d32d9864SMats Randgaard {
276d32d9864SMats Randgaard return i2c_rd8(sd, AU_STATUS0) & MASK_S_A_SAMPLE;
277d32d9864SMats Randgaard }
278d32d9864SMats Randgaard
get_audio_sampling_rate(struct v4l2_subdev * sd)279d32d9864SMats Randgaard static int get_audio_sampling_rate(struct v4l2_subdev *sd)
280d32d9864SMats Randgaard {
281d32d9864SMats Randgaard static const int code_to_rate[] = {
282d32d9864SMats Randgaard 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
283d32d9864SMats Randgaard 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
284d32d9864SMats Randgaard };
285d32d9864SMats Randgaard
286d32d9864SMats Randgaard /* Register FS_SET is not cleared when the cable is disconnected */
287d32d9864SMats Randgaard if (no_signal(sd))
288d32d9864SMats Randgaard return 0;
289d32d9864SMats Randgaard
290d32d9864SMats Randgaard return code_to_rate[i2c_rd8(sd, FS_SET) & MASK_FS];
291d32d9864SMats Randgaard }
292d32d9864SMats Randgaard
293d32d9864SMats Randgaard /* --------------- TIMINGS --------------- */
294d32d9864SMats Randgaard
fps(const struct v4l2_bt_timings * t)295d32d9864SMats Randgaard static inline unsigned fps(const struct v4l2_bt_timings *t)
296d32d9864SMats Randgaard {
297d32d9864SMats Randgaard if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
298d32d9864SMats Randgaard return 0;
299d32d9864SMats Randgaard
300d32d9864SMats Randgaard return DIV_ROUND_CLOSEST((unsigned)t->pixelclock,
301d32d9864SMats Randgaard V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
302d32d9864SMats Randgaard }
303d32d9864SMats Randgaard
tc358743_get_detected_timings(struct v4l2_subdev * sd,struct v4l2_dv_timings * timings)304d32d9864SMats Randgaard static int tc358743_get_detected_timings(struct v4l2_subdev *sd,
305d32d9864SMats Randgaard struct v4l2_dv_timings *timings)
306d32d9864SMats Randgaard {
307d32d9864SMats Randgaard struct v4l2_bt_timings *bt = &timings->bt;
308d32d9864SMats Randgaard unsigned width, height, frame_width, frame_height, frame_interval, fps;
309d32d9864SMats Randgaard
310d32d9864SMats Randgaard memset(timings, 0, sizeof(struct v4l2_dv_timings));
311d32d9864SMats Randgaard
312d32d9864SMats Randgaard if (no_signal(sd)) {
313d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
314d32d9864SMats Randgaard return -ENOLINK;
315d32d9864SMats Randgaard }
316d32d9864SMats Randgaard if (no_sync(sd)) {
317d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: no sync on signal\n", __func__);
318d32d9864SMats Randgaard return -ENOLCK;
319d32d9864SMats Randgaard }
320d32d9864SMats Randgaard
321d32d9864SMats Randgaard timings->type = V4L2_DV_BT_656_1120;
322d32d9864SMats Randgaard bt->interlaced = i2c_rd8(sd, VI_STATUS1) & MASK_S_V_INTERLACE ?
323d32d9864SMats Randgaard V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
324d32d9864SMats Randgaard
325d32d9864SMats Randgaard width = ((i2c_rd8(sd, DE_WIDTH_H_HI) & 0x1f) << 8) +
326d32d9864SMats Randgaard i2c_rd8(sd, DE_WIDTH_H_LO);
327d32d9864SMats Randgaard height = ((i2c_rd8(sd, DE_WIDTH_V_HI) & 0x1f) << 8) +
328d32d9864SMats Randgaard i2c_rd8(sd, DE_WIDTH_V_LO);
329d32d9864SMats Randgaard frame_width = ((i2c_rd8(sd, H_SIZE_HI) & 0x1f) << 8) +
330d32d9864SMats Randgaard i2c_rd8(sd, H_SIZE_LO);
331d32d9864SMats Randgaard frame_height = (((i2c_rd8(sd, V_SIZE_HI) & 0x3f) << 8) +
332d32d9864SMats Randgaard i2c_rd8(sd, V_SIZE_LO)) / 2;
333d32d9864SMats Randgaard /* frame interval in milliseconds * 10
334d32d9864SMats Randgaard * Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
335d32d9864SMats Randgaard frame_interval = ((i2c_rd8(sd, FV_CNT_HI) & 0x3) << 8) +
336d32d9864SMats Randgaard i2c_rd8(sd, FV_CNT_LO);
337d32d9864SMats Randgaard fps = (frame_interval > 0) ?
338d32d9864SMats Randgaard DIV_ROUND_CLOSEST(10000, frame_interval) : 0;
339d32d9864SMats Randgaard
340d32d9864SMats Randgaard bt->width = width;
341d32d9864SMats Randgaard bt->height = height;
342d32d9864SMats Randgaard bt->vsync = frame_height - height;
343d32d9864SMats Randgaard bt->hsync = frame_width - width;
344d32d9864SMats Randgaard bt->pixelclock = frame_width * frame_height * fps;
345d32d9864SMats Randgaard if (bt->interlaced == V4L2_DV_INTERLACED) {
346d32d9864SMats Randgaard bt->height *= 2;
347d32d9864SMats Randgaard bt->il_vsync = bt->vsync + 1;
348d32d9864SMats Randgaard bt->pixelclock /= 2;
349d32d9864SMats Randgaard }
350d32d9864SMats Randgaard
351d32d9864SMats Randgaard return 0;
352d32d9864SMats Randgaard }
353d32d9864SMats Randgaard
354d32d9864SMats Randgaard /* --------------- HOTPLUG / HDCP / EDID --------------- */
355d32d9864SMats Randgaard
tc358743_delayed_work_enable_hotplug(struct work_struct * work)356d32d9864SMats Randgaard static void tc358743_delayed_work_enable_hotplug(struct work_struct *work)
357d32d9864SMats Randgaard {
358d32d9864SMats Randgaard struct delayed_work *dwork = to_delayed_work(work);
359d32d9864SMats Randgaard struct tc358743_state *state = container_of(dwork,
360d32d9864SMats Randgaard struct tc358743_state, delayed_work_enable_hotplug);
361d32d9864SMats Randgaard struct v4l2_subdev *sd = &state->sd;
362d32d9864SMats Randgaard
363d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s:\n", __func__);
364d32d9864SMats Randgaard
365d32d9864SMats Randgaard i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, MASK_HPD_OUT0);
366d32d9864SMats Randgaard }
367d32d9864SMats Randgaard
tc358743_set_hdmi_hdcp(struct v4l2_subdev * sd,bool enable)368d32d9864SMats Randgaard static void tc358743_set_hdmi_hdcp(struct v4l2_subdev *sd, bool enable)
369d32d9864SMats Randgaard {
370d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ?
371d32d9864SMats Randgaard "enable" : "disable");
372d32d9864SMats Randgaard
3730a1f0850SMats Randgaard if (enable) {
3740a1f0850SMats Randgaard i2c_wr8_and_or(sd, HDCP_REG3, ~KEY_RD_CMD, KEY_RD_CMD);
3750a1f0850SMats Randgaard
3760a1f0850SMats Randgaard i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION, 0);
3770a1f0850SMats Randgaard
3780a1f0850SMats Randgaard i2c_wr8_and_or(sd, HDCP_REG1, 0xff,
3790a1f0850SMats Randgaard MASK_AUTH_UNAUTH_SEL_16_FRAMES |
3800a1f0850SMats Randgaard MASK_AUTH_UNAUTH_AUTO);
381d32d9864SMats Randgaard
382d32d9864SMats Randgaard i2c_wr8_and_or(sd, HDCP_REG2, ~MASK_AUTO_P3_RESET,
383d32d9864SMats Randgaard SET_AUTO_P3_RESET_FRAMES(0x0f));
3840a1f0850SMats Randgaard } else {
3850a1f0850SMats Randgaard i2c_wr8_and_or(sd, HDCP_MODE, ~MASK_MANUAL_AUTHENTICATION,
3860a1f0850SMats Randgaard MASK_MANUAL_AUTHENTICATION);
3870a1f0850SMats Randgaard }
388d32d9864SMats Randgaard }
389d32d9864SMats Randgaard
tc358743_disable_edid(struct v4l2_subdev * sd)390d32d9864SMats Randgaard static void tc358743_disable_edid(struct v4l2_subdev *sd)
391d32d9864SMats Randgaard {
392d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
393d32d9864SMats Randgaard
394d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s:\n", __func__);
395d32d9864SMats Randgaard
396d32d9864SMats Randgaard cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
397d32d9864SMats Randgaard
398d32d9864SMats Randgaard /* DDC access to EDID is also disabled when hotplug is disabled. See
399d32d9864SMats Randgaard * register DDC_CTL */
400d32d9864SMats Randgaard i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, 0x0);
401d32d9864SMats Randgaard }
402d32d9864SMats Randgaard
tc358743_enable_edid(struct v4l2_subdev * sd)403d32d9864SMats Randgaard static void tc358743_enable_edid(struct v4l2_subdev *sd)
404d32d9864SMats Randgaard {
405d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
406d32d9864SMats Randgaard
407d32d9864SMats Randgaard if (state->edid_blocks_written == 0) {
408d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: no EDID -> no hotplug\n", __func__);
409676c002aSMats Randgaard tc358743_s_ctrl_detect_tx_5v(sd);
410d32d9864SMats Randgaard return;
411d32d9864SMats Randgaard }
412d32d9864SMats Randgaard
413d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s:\n", __func__);
414d32d9864SMats Randgaard
415d32d9864SMats Randgaard /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when
416d32d9864SMats Randgaard * hotplug is enabled. See register DDC_CTL */
4171ce39546SBhaktipriya Shridhar schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 10);
418d32d9864SMats Randgaard
419d32d9864SMats Randgaard tc358743_enable_interrupts(sd, true);
420d32d9864SMats Randgaard tc358743_s_ctrl_detect_tx_5v(sd);
421d32d9864SMats Randgaard }
422d32d9864SMats Randgaard
tc358743_erase_bksv(struct v4l2_subdev * sd)423d32d9864SMats Randgaard static void tc358743_erase_bksv(struct v4l2_subdev *sd)
424d32d9864SMats Randgaard {
425d32d9864SMats Randgaard int i;
426d32d9864SMats Randgaard
427d32d9864SMats Randgaard for (i = 0; i < 5; i++)
428d32d9864SMats Randgaard i2c_wr8(sd, BKSV + i, 0);
429d32d9864SMats Randgaard }
430d32d9864SMats Randgaard
431d32d9864SMats Randgaard /* --------------- AVI infoframe --------------- */
432d32d9864SMats Randgaard
print_avi_infoframe(struct v4l2_subdev * sd)433d32d9864SMats Randgaard static void print_avi_infoframe(struct v4l2_subdev *sd)
434d32d9864SMats Randgaard {
435d32d9864SMats Randgaard struct i2c_client *client = v4l2_get_subdevdata(sd);
436d32d9864SMats Randgaard struct device *dev = &client->dev;
437d32d9864SMats Randgaard union hdmi_infoframe frame;
438d32d9864SMats Randgaard u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
439d32d9864SMats Randgaard
440d32d9864SMats Randgaard if (!is_hdmi(sd)) {
441d32d9864SMats Randgaard v4l2_info(sd, "DVI-D signal - AVI infoframe not supported\n");
442d32d9864SMats Randgaard return;
443d32d9864SMats Randgaard }
444d32d9864SMats Randgaard
445d32d9864SMats Randgaard i2c_rd(sd, PK_AVI_0HEAD, buffer, HDMI_INFOFRAME_SIZE(AVI));
446d32d9864SMats Randgaard
447480b8b3eSVille Syrjälä if (hdmi_infoframe_unpack(&frame, buffer, sizeof(buffer)) < 0) {
448d32d9864SMats Randgaard v4l2_err(sd, "%s: unpack of AVI infoframe failed\n", __func__);
449d32d9864SMats Randgaard return;
450d32d9864SMats Randgaard }
451d32d9864SMats Randgaard
452d32d9864SMats Randgaard hdmi_infoframe_log(KERN_INFO, dev, &frame);
453d32d9864SMats Randgaard }
454d32d9864SMats Randgaard
455d32d9864SMats Randgaard /* --------------- CTRLS --------------- */
456d32d9864SMats Randgaard
tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev * sd)457d32d9864SMats Randgaard static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
458d32d9864SMats Randgaard {
459d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
460d32d9864SMats Randgaard
461d32d9864SMats Randgaard return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
462d32d9864SMats Randgaard tx_5v_power_present(sd));
463d32d9864SMats Randgaard }
464d32d9864SMats Randgaard
tc358743_s_ctrl_audio_sampling_rate(struct v4l2_subdev * sd)465d32d9864SMats Randgaard static int tc358743_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
466d32d9864SMats Randgaard {
467d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
468d32d9864SMats Randgaard
469d32d9864SMats Randgaard return v4l2_ctrl_s_ctrl(state->audio_sampling_rate_ctrl,
470d32d9864SMats Randgaard get_audio_sampling_rate(sd));
471d32d9864SMats Randgaard }
472d32d9864SMats Randgaard
tc358743_s_ctrl_audio_present(struct v4l2_subdev * sd)473d32d9864SMats Randgaard static int tc358743_s_ctrl_audio_present(struct v4l2_subdev *sd)
474d32d9864SMats Randgaard {
475d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
476d32d9864SMats Randgaard
477d32d9864SMats Randgaard return v4l2_ctrl_s_ctrl(state->audio_present_ctrl,
478d32d9864SMats Randgaard audio_present(sd));
479d32d9864SMats Randgaard }
480d32d9864SMats Randgaard
tc358743_update_controls(struct v4l2_subdev * sd)481d32d9864SMats Randgaard static int tc358743_update_controls(struct v4l2_subdev *sd)
482d32d9864SMats Randgaard {
483d32d9864SMats Randgaard int ret = 0;
484d32d9864SMats Randgaard
485d32d9864SMats Randgaard ret |= tc358743_s_ctrl_detect_tx_5v(sd);
486d32d9864SMats Randgaard ret |= tc358743_s_ctrl_audio_sampling_rate(sd);
487d32d9864SMats Randgaard ret |= tc358743_s_ctrl_audio_present(sd);
488d32d9864SMats Randgaard
489d32d9864SMats Randgaard return ret;
490d32d9864SMats Randgaard }
491d32d9864SMats Randgaard
492d32d9864SMats Randgaard /* --------------- INIT --------------- */
493d32d9864SMats Randgaard
tc358743_reset_phy(struct v4l2_subdev * sd)494d32d9864SMats Randgaard static void tc358743_reset_phy(struct v4l2_subdev *sd)
495d32d9864SMats Randgaard {
496d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s:\n", __func__);
497d32d9864SMats Randgaard
498d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, 0);
499d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, MASK_RESET_CTRL);
500d32d9864SMats Randgaard }
501d32d9864SMats Randgaard
tc358743_reset(struct v4l2_subdev * sd,uint16_t mask)502d32d9864SMats Randgaard static void tc358743_reset(struct v4l2_subdev *sd, uint16_t mask)
503d32d9864SMats Randgaard {
504d32d9864SMats Randgaard u16 sysctl = i2c_rd16(sd, SYSCTL);
505d32d9864SMats Randgaard
506d32d9864SMats Randgaard i2c_wr16(sd, SYSCTL, sysctl | mask);
507d32d9864SMats Randgaard i2c_wr16(sd, SYSCTL, sysctl & ~mask);
508d32d9864SMats Randgaard }
509d32d9864SMats Randgaard
tc358743_sleep_mode(struct v4l2_subdev * sd,bool enable)510d32d9864SMats Randgaard static inline void tc358743_sleep_mode(struct v4l2_subdev *sd, bool enable)
511d32d9864SMats Randgaard {
512d32d9864SMats Randgaard i2c_wr16_and_or(sd, SYSCTL, ~MASK_SLEEP,
513d32d9864SMats Randgaard enable ? MASK_SLEEP : 0);
514d32d9864SMats Randgaard }
515d32d9864SMats Randgaard
enable_stream(struct v4l2_subdev * sd,bool enable)516d32d9864SMats Randgaard static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
517d32d9864SMats Randgaard {
518d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
519d32d9864SMats Randgaard
520d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: %sable\n",
521d32d9864SMats Randgaard __func__, enable ? "en" : "dis");
522d32d9864SMats Randgaard
523d32d9864SMats Randgaard if (enable) {
524d32d9864SMats Randgaard /* It is critical for CSI receiver to see lane transition
525d32d9864SMats Randgaard * LP11->HS. Set to non-continuous mode to enable clock lane
526d32d9864SMats Randgaard * LP11 state. */
527d32d9864SMats Randgaard i2c_wr32(sd, TXOPTIONCNTRL, 0);
528d32d9864SMats Randgaard /* Set to continuous mode to trigger LP11->HS transition */
529d32d9864SMats Randgaard i2c_wr32(sd, TXOPTIONCNTRL, MASK_CONTCLKMODE);
530d32d9864SMats Randgaard /* Unmute video */
531d32d9864SMats Randgaard i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE);
532d32d9864SMats Randgaard } else {
533d32d9864SMats Randgaard /* Mute video so that all data lanes go to LSP11 state.
534d32d9864SMats Randgaard * No data is output to CSI Tx block. */
535d32d9864SMats Randgaard i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE | MASK_VI_MUTE);
536d32d9864SMats Randgaard }
537d32d9864SMats Randgaard
538d32d9864SMats Randgaard mutex_lock(&state->confctl_mutex);
539d32d9864SMats Randgaard i2c_wr16_and_or(sd, CONFCTL, ~(MASK_VBUFEN | MASK_ABUFEN),
540d32d9864SMats Randgaard enable ? (MASK_VBUFEN | MASK_ABUFEN) : 0x0);
541d32d9864SMats Randgaard mutex_unlock(&state->confctl_mutex);
542d32d9864SMats Randgaard }
543d32d9864SMats Randgaard
tc358743_set_pll(struct v4l2_subdev * sd)544d32d9864SMats Randgaard static void tc358743_set_pll(struct v4l2_subdev *sd)
545d32d9864SMats Randgaard {
546d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
547d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
548d32d9864SMats Randgaard u16 pllctl0 = i2c_rd16(sd, PLLCTL0);
549d32d9864SMats Randgaard u16 pllctl1 = i2c_rd16(sd, PLLCTL1);
550d32d9864SMats Randgaard u16 pllctl0_new = SET_PLL_PRD(pdata->pll_prd) |
551d32d9864SMats Randgaard SET_PLL_FBD(pdata->pll_fbd);
552d32d9864SMats Randgaard u32 hsck = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
553d32d9864SMats Randgaard
554d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s:\n", __func__);
555d32d9864SMats Randgaard
556d32d9864SMats Randgaard /* Only rewrite when needed (new value or disabled), since rewriting
557d32d9864SMats Randgaard * triggers another format change event. */
558d32d9864SMats Randgaard if ((pllctl0 != pllctl0_new) || ((pllctl1 & MASK_PLL_EN) == 0)) {
559d32d9864SMats Randgaard u16 pll_frs;
560d32d9864SMats Randgaard
561d32d9864SMats Randgaard if (hsck > 500000000)
562d32d9864SMats Randgaard pll_frs = 0x0;
563d32d9864SMats Randgaard else if (hsck > 250000000)
564d32d9864SMats Randgaard pll_frs = 0x1;
565d32d9864SMats Randgaard else if (hsck > 125000000)
566d32d9864SMats Randgaard pll_frs = 0x2;
567d32d9864SMats Randgaard else
568d32d9864SMats Randgaard pll_frs = 0x3;
569d32d9864SMats Randgaard
570d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: updating PLL clock\n", __func__);
571d32d9864SMats Randgaard tc358743_sleep_mode(sd, true);
572d32d9864SMats Randgaard i2c_wr16(sd, PLLCTL0, pllctl0_new);
573d32d9864SMats Randgaard i2c_wr16_and_or(sd, PLLCTL1,
574d32d9864SMats Randgaard ~(MASK_PLL_FRS | MASK_RESETB | MASK_PLL_EN),
575d32d9864SMats Randgaard (SET_PLL_FRS(pll_frs) | MASK_RESETB |
576d32d9864SMats Randgaard MASK_PLL_EN));
577d32d9864SMats Randgaard udelay(10); /* REF_02, Sheet "Source HDMI" */
578d32d9864SMats Randgaard i2c_wr16_and_or(sd, PLLCTL1, ~MASK_CKEN, MASK_CKEN);
579d32d9864SMats Randgaard tc358743_sleep_mode(sd, false);
580d32d9864SMats Randgaard }
581d32d9864SMats Randgaard }
582d32d9864SMats Randgaard
tc358743_set_ref_clk(struct v4l2_subdev * sd)583d32d9864SMats Randgaard static void tc358743_set_ref_clk(struct v4l2_subdev *sd)
584d32d9864SMats Randgaard {
585d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
586d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
587d32d9864SMats Randgaard u32 sys_freq;
588d32d9864SMats Randgaard u32 lockdet_ref;
589a0ec8d1dSHans Verkuil u32 cec_freq;
590d32d9864SMats Randgaard u16 fh_min;
591d32d9864SMats Randgaard u16 fh_max;
592d32d9864SMats Randgaard
593d32d9864SMats Randgaard BUG_ON(!(pdata->refclk_hz == 26000000 ||
594d32d9864SMats Randgaard pdata->refclk_hz == 27000000 ||
595d32d9864SMats Randgaard pdata->refclk_hz == 42000000));
596d32d9864SMats Randgaard
597d32d9864SMats Randgaard sys_freq = pdata->refclk_hz / 10000;
598d32d9864SMats Randgaard i2c_wr8(sd, SYS_FREQ0, sys_freq & 0x00ff);
599d32d9864SMats Randgaard i2c_wr8(sd, SYS_FREQ1, (sys_freq & 0xff00) >> 8);
600d32d9864SMats Randgaard
601d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_CTL0, ~MASK_PHY_SYSCLK_IND,
602d32d9864SMats Randgaard (pdata->refclk_hz == 42000000) ?
603d32d9864SMats Randgaard MASK_PHY_SYSCLK_IND : 0x0);
604d32d9864SMats Randgaard
605d32d9864SMats Randgaard fh_min = pdata->refclk_hz / 100000;
606d32d9864SMats Randgaard i2c_wr8(sd, FH_MIN0, fh_min & 0x00ff);
607d32d9864SMats Randgaard i2c_wr8(sd, FH_MIN1, (fh_min & 0xff00) >> 8);
608d32d9864SMats Randgaard
609d32d9864SMats Randgaard fh_max = (fh_min * 66) / 10;
610d32d9864SMats Randgaard i2c_wr8(sd, FH_MAX0, fh_max & 0x00ff);
611d32d9864SMats Randgaard i2c_wr8(sd, FH_MAX1, (fh_max & 0xff00) >> 8);
612d32d9864SMats Randgaard
613d32d9864SMats Randgaard lockdet_ref = pdata->refclk_hz / 100;
614d32d9864SMats Randgaard i2c_wr8(sd, LOCKDET_REF0, lockdet_ref & 0x0000ff);
615d32d9864SMats Randgaard i2c_wr8(sd, LOCKDET_REF1, (lockdet_ref & 0x00ff00) >> 8);
616d32d9864SMats Randgaard i2c_wr8(sd, LOCKDET_REF2, (lockdet_ref & 0x0f0000) >> 16);
617d32d9864SMats Randgaard
618d32d9864SMats Randgaard i2c_wr8_and_or(sd, NCO_F0_MOD, ~MASK_NCO_F0_MOD,
619d32d9864SMats Randgaard (pdata->refclk_hz == 27000000) ?
620d32d9864SMats Randgaard MASK_NCO_F0_MOD_27MHZ : 0x0);
621a0ec8d1dSHans Verkuil
622a0ec8d1dSHans Verkuil /*
623a0ec8d1dSHans Verkuil * Trial and error suggests that the default register value
624a0ec8d1dSHans Verkuil * of 656 is for a 42 MHz reference clock. Use that to derive
625a0ec8d1dSHans Verkuil * a new value based on the actual reference clock.
626a0ec8d1dSHans Verkuil */
627a0ec8d1dSHans Verkuil cec_freq = (656 * sys_freq) / 4200;
628a0ec8d1dSHans Verkuil i2c_wr16(sd, CECHCLK, cec_freq);
629a0ec8d1dSHans Verkuil i2c_wr16(sd, CECLCLK, cec_freq);
630d32d9864SMats Randgaard }
631d32d9864SMats Randgaard
tc358743_set_csi_color_space(struct v4l2_subdev * sd)632d32d9864SMats Randgaard static void tc358743_set_csi_color_space(struct v4l2_subdev *sd)
633d32d9864SMats Randgaard {
634d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
635d32d9864SMats Randgaard
636d32d9864SMats Randgaard switch (state->mbus_fmt_code) {
637d32d9864SMats Randgaard case MEDIA_BUS_FMT_UYVY8_1X16:
638d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: YCbCr 422 16-bit\n", __func__);
639d32d9864SMats Randgaard i2c_wr8_and_or(sd, VOUT_SET2,
640d32d9864SMats Randgaard ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
641d32d9864SMats Randgaard MASK_SEL422 | MASK_VOUT_422FIL_100);
642d32d9864SMats Randgaard i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
643d32d9864SMats Randgaard MASK_VOUT_COLOR_601_YCBCR_LIMITED);
644d32d9864SMats Randgaard mutex_lock(&state->confctl_mutex);
645d32d9864SMats Randgaard i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT,
646d32d9864SMats Randgaard MASK_YCBCRFMT_422_8_BIT);
647d32d9864SMats Randgaard mutex_unlock(&state->confctl_mutex);
648d32d9864SMats Randgaard break;
649d32d9864SMats Randgaard case MEDIA_BUS_FMT_RGB888_1X24:
650d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: RGB 888 24-bit\n", __func__);
651d32d9864SMats Randgaard i2c_wr8_and_or(sd, VOUT_SET2,
652d32d9864SMats Randgaard ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
653d32d9864SMats Randgaard 0x00);
654d32d9864SMats Randgaard i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
655d32d9864SMats Randgaard MASK_VOUT_COLOR_RGB_FULL);
656d32d9864SMats Randgaard mutex_lock(&state->confctl_mutex);
657d32d9864SMats Randgaard i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT, 0);
658d32d9864SMats Randgaard mutex_unlock(&state->confctl_mutex);
659d32d9864SMats Randgaard break;
660d32d9864SMats Randgaard default:
661d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: Unsupported format code 0x%x\n",
662d32d9864SMats Randgaard __func__, state->mbus_fmt_code);
663d32d9864SMats Randgaard }
664d32d9864SMats Randgaard }
665d32d9864SMats Randgaard
tc358743_num_csi_lanes_needed(struct v4l2_subdev * sd)666d32d9864SMats Randgaard static unsigned tc358743_num_csi_lanes_needed(struct v4l2_subdev *sd)
667d32d9864SMats Randgaard {
668d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
669d32d9864SMats Randgaard struct v4l2_bt_timings *bt = &state->timings.bt;
670d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
671d32d9864SMats Randgaard u32 bits_pr_pixel =
672d32d9864SMats Randgaard (state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16) ? 16 : 24;
673d32d9864SMats Randgaard u32 bps = bt->width * bt->height * fps(bt) * bits_pr_pixel;
674d32d9864SMats Randgaard u32 bps_pr_lane = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
675d32d9864SMats Randgaard
676d32d9864SMats Randgaard return DIV_ROUND_UP(bps, bps_pr_lane);
677d32d9864SMats Randgaard }
678d32d9864SMats Randgaard
tc358743_set_csi(struct v4l2_subdev * sd)679d32d9864SMats Randgaard static void tc358743_set_csi(struct v4l2_subdev *sd)
680d32d9864SMats Randgaard {
681d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
682d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
683d32d9864SMats Randgaard unsigned lanes = tc358743_num_csi_lanes_needed(sd);
684d32d9864SMats Randgaard
685d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s:\n", __func__);
686d32d9864SMats Randgaard
68759e34ba8SMats Randgaard state->csi_lanes_in_use = lanes;
68859e34ba8SMats Randgaard
689d32d9864SMats Randgaard tc358743_reset(sd, MASK_CTXRST);
690d32d9864SMats Randgaard
691d32d9864SMats Randgaard if (lanes < 1)
692d32d9864SMats Randgaard i2c_wr32(sd, CLW_CNTRL, MASK_CLW_LANEDISABLE);
693d32d9864SMats Randgaard if (lanes < 1)
694d32d9864SMats Randgaard i2c_wr32(sd, D0W_CNTRL, MASK_D0W_LANEDISABLE);
695d32d9864SMats Randgaard if (lanes < 2)
696d32d9864SMats Randgaard i2c_wr32(sd, D1W_CNTRL, MASK_D1W_LANEDISABLE);
697d32d9864SMats Randgaard if (lanes < 3)
698d32d9864SMats Randgaard i2c_wr32(sd, D2W_CNTRL, MASK_D2W_LANEDISABLE);
699d32d9864SMats Randgaard if (lanes < 4)
700d32d9864SMats Randgaard i2c_wr32(sd, D3W_CNTRL, MASK_D3W_LANEDISABLE);
701d32d9864SMats Randgaard
702d32d9864SMats Randgaard i2c_wr32(sd, LINEINITCNT, pdata->lineinitcnt);
703d32d9864SMats Randgaard i2c_wr32(sd, LPTXTIMECNT, pdata->lptxtimecnt);
704d32d9864SMats Randgaard i2c_wr32(sd, TCLK_HEADERCNT, pdata->tclk_headercnt);
705d32d9864SMats Randgaard i2c_wr32(sd, TCLK_TRAILCNT, pdata->tclk_trailcnt);
706d32d9864SMats Randgaard i2c_wr32(sd, THS_HEADERCNT, pdata->ths_headercnt);
707d32d9864SMats Randgaard i2c_wr32(sd, TWAKEUP, pdata->twakeup);
708d32d9864SMats Randgaard i2c_wr32(sd, TCLK_POSTCNT, pdata->tclk_postcnt);
709d32d9864SMats Randgaard i2c_wr32(sd, THS_TRAILCNT, pdata->ths_trailcnt);
710d32d9864SMats Randgaard i2c_wr32(sd, HSTXVREGCNT, pdata->hstxvregcnt);
711d32d9864SMats Randgaard
712d32d9864SMats Randgaard i2c_wr32(sd, HSTXVREGEN,
713d32d9864SMats Randgaard ((lanes > 0) ? MASK_CLM_HSTXVREGEN : 0x0) |
714d32d9864SMats Randgaard ((lanes > 0) ? MASK_D0M_HSTXVREGEN : 0x0) |
715d32d9864SMats Randgaard ((lanes > 1) ? MASK_D1M_HSTXVREGEN : 0x0) |
716d32d9864SMats Randgaard ((lanes > 2) ? MASK_D2M_HSTXVREGEN : 0x0) |
717d32d9864SMats Randgaard ((lanes > 3) ? MASK_D3M_HSTXVREGEN : 0x0));
718d32d9864SMats Randgaard
71925614824SPhilipp Zabel i2c_wr32(sd, TXOPTIONCNTRL, (state->bus.flags &
720b9f7caa7SLaurent Pinchart V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK) ? 0 : MASK_CONTCLKMODE);
721d32d9864SMats Randgaard i2c_wr32(sd, STARTCNTRL, MASK_START);
722d32d9864SMats Randgaard i2c_wr32(sd, CSI_START, MASK_STRT);
723d32d9864SMats Randgaard
724d32d9864SMats Randgaard i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
725d32d9864SMats Randgaard MASK_ADDRESS_CSI_CONTROL |
726d32d9864SMats Randgaard MASK_CSI_MODE |
727d32d9864SMats Randgaard MASK_TXHSMD |
728d32d9864SMats Randgaard ((lanes == 4) ? MASK_NOL_4 :
729d32d9864SMats Randgaard (lanes == 3) ? MASK_NOL_3 :
730d32d9864SMats Randgaard (lanes == 2) ? MASK_NOL_2 : MASK_NOL_1));
731d32d9864SMats Randgaard
732d32d9864SMats Randgaard i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
733d32d9864SMats Randgaard MASK_ADDRESS_CSI_ERR_INTENA | MASK_TXBRK | MASK_QUNK |
734d32d9864SMats Randgaard MASK_WCER | MASK_INER);
735d32d9864SMats Randgaard
736d32d9864SMats Randgaard i2c_wr32(sd, CSI_CONFW, MASK_MODE_CLEAR |
737d32d9864SMats Randgaard MASK_ADDRESS_CSI_ERR_HALT | MASK_TXBRK | MASK_QUNK);
738d32d9864SMats Randgaard
739d32d9864SMats Randgaard i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
740d32d9864SMats Randgaard MASK_ADDRESS_CSI_INT_ENA | MASK_INTER);
741d32d9864SMats Randgaard }
742d32d9864SMats Randgaard
tc358743_set_hdmi_phy(struct v4l2_subdev * sd)743d32d9864SMats Randgaard static void tc358743_set_hdmi_phy(struct v4l2_subdev *sd)
744d32d9864SMats Randgaard {
745d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
746d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
747d32d9864SMats Randgaard
748d32d9864SMats Randgaard /* Default settings from REF_02, sheet "Source HDMI"
749d32d9864SMats Randgaard * and custom settings as platform data */
750d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, 0x0);
751d32d9864SMats Randgaard i2c_wr8(sd, PHY_CTL1, SET_PHY_AUTO_RST1_US(1600) |
752d32d9864SMats Randgaard SET_FREQ_RANGE_MODE_CYCLES(1));
753d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_CTL2, ~MASK_PHY_AUTO_RSTn,
754d32d9864SMats Randgaard (pdata->hdmi_phy_auto_reset_tmds_detected ?
755d32d9864SMats Randgaard MASK_PHY_AUTO_RST2 : 0) |
756d32d9864SMats Randgaard (pdata->hdmi_phy_auto_reset_tmds_in_range ?
757d32d9864SMats Randgaard MASK_PHY_AUTO_RST3 : 0) |
758d32d9864SMats Randgaard (pdata->hdmi_phy_auto_reset_tmds_valid ?
759d32d9864SMats Randgaard MASK_PHY_AUTO_RST4 : 0));
760d32d9864SMats Randgaard i2c_wr8(sd, PHY_BIAS, 0x40);
761d32d9864SMats Randgaard i2c_wr8(sd, PHY_CSQ, SET_CSQ_CNT_LEVEL(0x0a));
762d32d9864SMats Randgaard i2c_wr8(sd, AVM_CTL, 45);
763d32d9864SMats Randgaard i2c_wr8_and_or(sd, HDMI_DET, ~MASK_HDMI_DET_V,
764d32d9864SMats Randgaard pdata->hdmi_detection_delay << 4);
765d32d9864SMats Randgaard i2c_wr8_and_or(sd, HV_RST, ~(MASK_H_PI_RST | MASK_V_PI_RST),
766d32d9864SMats Randgaard (pdata->hdmi_phy_auto_reset_hsync_out_of_range ?
767d32d9864SMats Randgaard MASK_H_PI_RST : 0) |
768d32d9864SMats Randgaard (pdata->hdmi_phy_auto_reset_vsync_out_of_range ?
769d32d9864SMats Randgaard MASK_V_PI_RST : 0));
770d32d9864SMats Randgaard i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, MASK_ENABLE_PHY);
771d32d9864SMats Randgaard }
772d32d9864SMats Randgaard
tc358743_set_hdmi_audio(struct v4l2_subdev * sd)773d32d9864SMats Randgaard static void tc358743_set_hdmi_audio(struct v4l2_subdev *sd)
774d32d9864SMats Randgaard {
775d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
776d32d9864SMats Randgaard
777d32d9864SMats Randgaard /* Default settings from REF_02, sheet "Source HDMI" */
778d32d9864SMats Randgaard i2c_wr8(sd, FORCE_MUTE, 0x00);
779d32d9864SMats Randgaard i2c_wr8(sd, AUTO_CMD0, MASK_AUTO_MUTE7 | MASK_AUTO_MUTE6 |
780d32d9864SMats Randgaard MASK_AUTO_MUTE5 | MASK_AUTO_MUTE4 |
781d32d9864SMats Randgaard MASK_AUTO_MUTE1 | MASK_AUTO_MUTE0);
782d32d9864SMats Randgaard i2c_wr8(sd, AUTO_CMD1, MASK_AUTO_MUTE9);
783d32d9864SMats Randgaard i2c_wr8(sd, AUTO_CMD2, MASK_AUTO_PLAY3 | MASK_AUTO_PLAY2);
784d32d9864SMats Randgaard i2c_wr8(sd, BUFINIT_START, SET_BUFINIT_START_MS(500));
785d32d9864SMats Randgaard i2c_wr8(sd, FS_MUTE, 0x00);
786d32d9864SMats Randgaard i2c_wr8(sd, FS_IMODE, MASK_NLPCM_SMODE | MASK_FS_SMODE);
787d32d9864SMats Randgaard i2c_wr8(sd, ACR_MODE, MASK_CTS_MODE);
788d32d9864SMats Randgaard i2c_wr8(sd, ACR_MDF0, MASK_ACR_L2MDF_1976_PPM | MASK_ACR_L1MDF_976_PPM);
789d32d9864SMats Randgaard i2c_wr8(sd, ACR_MDF1, MASK_ACR_L3MDF_3906_PPM);
790d32d9864SMats Randgaard i2c_wr8(sd, SDO_MODE1, MASK_SDO_FMT_I2S);
791d32d9864SMats Randgaard i2c_wr8(sd, DIV_MODE, SET_DIV_DLY_MS(100));
792d32d9864SMats Randgaard
793d32d9864SMats Randgaard mutex_lock(&state->confctl_mutex);
794d32d9864SMats Randgaard i2c_wr16_and_or(sd, CONFCTL, 0xffff, MASK_AUDCHNUM_2 |
795d32d9864SMats Randgaard MASK_AUDOUTSEL_I2S | MASK_AUTOINDEX);
796d32d9864SMats Randgaard mutex_unlock(&state->confctl_mutex);
797d32d9864SMats Randgaard }
798d32d9864SMats Randgaard
tc358743_set_hdmi_info_frame_mode(struct v4l2_subdev * sd)799d32d9864SMats Randgaard static void tc358743_set_hdmi_info_frame_mode(struct v4l2_subdev *sd)
800d32d9864SMats Randgaard {
801d32d9864SMats Randgaard /* Default settings from REF_02, sheet "Source HDMI" */
802d32d9864SMats Randgaard i2c_wr8(sd, PK_INT_MODE, MASK_ISRC2_INT_MODE | MASK_ISRC_INT_MODE |
803d32d9864SMats Randgaard MASK_ACP_INT_MODE | MASK_VS_INT_MODE |
804d32d9864SMats Randgaard MASK_SPD_INT_MODE | MASK_MS_INT_MODE |
805d32d9864SMats Randgaard MASK_AUD_INT_MODE | MASK_AVI_INT_MODE);
806d32d9864SMats Randgaard i2c_wr8(sd, NO_PKT_LIMIT, 0x2c);
807d32d9864SMats Randgaard i2c_wr8(sd, NO_PKT_CLR, 0x53);
808d32d9864SMats Randgaard i2c_wr8(sd, ERR_PK_LIMIT, 0x01);
809d32d9864SMats Randgaard i2c_wr8(sd, NO_PKT_LIMIT2, 0x30);
810d32d9864SMats Randgaard i2c_wr8(sd, NO_GDB_LIMIT, 0x10);
811d32d9864SMats Randgaard }
812d32d9864SMats Randgaard
tc358743_initial_setup(struct v4l2_subdev * sd)813d32d9864SMats Randgaard static void tc358743_initial_setup(struct v4l2_subdev *sd)
814d32d9864SMats Randgaard {
815d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
816d32d9864SMats Randgaard struct tc358743_platform_data *pdata = &state->pdata;
817d32d9864SMats Randgaard
818a0ec8d1dSHans Verkuil /*
819a0ec8d1dSHans Verkuil * IR is not supported by this driver.
820a0ec8d1dSHans Verkuil * CEC is only enabled if needed.
821a0ec8d1dSHans Verkuil */
822a0ec8d1dSHans Verkuil i2c_wr16_and_or(sd, SYSCTL, ~(MASK_IRRST | MASK_CECRST),
823a0ec8d1dSHans Verkuil (MASK_IRRST | MASK_CECRST));
824d32d9864SMats Randgaard
825d32d9864SMats Randgaard tc358743_reset(sd, MASK_CTXRST | MASK_HDMIRST);
826a0ec8d1dSHans Verkuil #ifdef CONFIG_VIDEO_TC358743_CEC
827a0ec8d1dSHans Verkuil tc358743_reset(sd, MASK_CECRST);
828a0ec8d1dSHans Verkuil #endif
829d32d9864SMats Randgaard tc358743_sleep_mode(sd, false);
830d32d9864SMats Randgaard
831d32d9864SMats Randgaard i2c_wr16(sd, FIFOCTL, pdata->fifo_level);
832d32d9864SMats Randgaard
833d32d9864SMats Randgaard tc358743_set_ref_clk(sd);
834d32d9864SMats Randgaard
835d32d9864SMats Randgaard i2c_wr8_and_or(sd, DDC_CTL, ~MASK_DDC5V_MODE,
836d32d9864SMats Randgaard pdata->ddc5v_delay & MASK_DDC5V_MODE);
837d32d9864SMats Randgaard i2c_wr8_and_or(sd, EDID_MODE, ~MASK_EDID_MODE, MASK_EDID_MODE_E_DDC);
838d32d9864SMats Randgaard
839d32d9864SMats Randgaard tc358743_set_hdmi_phy(sd);
840d32d9864SMats Randgaard tc358743_set_hdmi_hdcp(sd, pdata->enable_hdcp);
841d32d9864SMats Randgaard tc358743_set_hdmi_audio(sd);
842d32d9864SMats Randgaard tc358743_set_hdmi_info_frame_mode(sd);
843d32d9864SMats Randgaard
844d32d9864SMats Randgaard /* All CE and IT formats are detected as RGB full range in DVI mode */
845d32d9864SMats Randgaard i2c_wr8_and_or(sd, VI_MODE, ~MASK_RGB_DVI, 0);
846d32d9864SMats Randgaard
847d32d9864SMats Randgaard i2c_wr8_and_or(sd, VOUT_SET2, ~MASK_VOUTCOLORMODE,
848d32d9864SMats Randgaard MASK_VOUTCOLORMODE_AUTO);
849d32d9864SMats Randgaard i2c_wr8(sd, VOUT_SET3, MASK_VOUT_EXTCNT);
850d32d9864SMats Randgaard }
851d32d9864SMats Randgaard
852a0ec8d1dSHans Verkuil /* --------------- CEC --------------- */
853a0ec8d1dSHans Verkuil
854a0ec8d1dSHans Verkuil #ifdef CONFIG_VIDEO_TC358743_CEC
tc358743_cec_adap_enable(struct cec_adapter * adap,bool enable)855a0ec8d1dSHans Verkuil static int tc358743_cec_adap_enable(struct cec_adapter *adap, bool enable)
856a0ec8d1dSHans Verkuil {
857a0ec8d1dSHans Verkuil struct tc358743_state *state = adap->priv;
858a0ec8d1dSHans Verkuil struct v4l2_subdev *sd = &state->sd;
859a0ec8d1dSHans Verkuil
860a0ec8d1dSHans Verkuil i2c_wr32(sd, CECIMSK, enable ? MASK_CECTIM | MASK_CECRIM : 0);
861a0ec8d1dSHans Verkuil i2c_wr32(sd, CECICLR, MASK_CECTICLR | MASK_CECRICLR);
862a0ec8d1dSHans Verkuil i2c_wr32(sd, CECEN, enable);
863a0ec8d1dSHans Verkuil if (enable)
864a0ec8d1dSHans Verkuil i2c_wr32(sd, CECREN, MASK_CECREN);
865a0ec8d1dSHans Verkuil return 0;
866a0ec8d1dSHans Verkuil }
867a0ec8d1dSHans Verkuil
tc358743_cec_adap_monitor_all_enable(struct cec_adapter * adap,bool enable)868a0ec8d1dSHans Verkuil static int tc358743_cec_adap_monitor_all_enable(struct cec_adapter *adap,
869a0ec8d1dSHans Verkuil bool enable)
870a0ec8d1dSHans Verkuil {
871a0ec8d1dSHans Verkuil struct tc358743_state *state = adap->priv;
872a0ec8d1dSHans Verkuil struct v4l2_subdev *sd = &state->sd;
873a0ec8d1dSHans Verkuil u32 reg;
874a0ec8d1dSHans Verkuil
875a0ec8d1dSHans Verkuil reg = i2c_rd32(sd, CECRCTL1);
876a0ec8d1dSHans Verkuil if (enable)
877a0ec8d1dSHans Verkuil reg |= MASK_CECOTH;
878a0ec8d1dSHans Verkuil else
879a0ec8d1dSHans Verkuil reg &= ~MASK_CECOTH;
880a0ec8d1dSHans Verkuil i2c_wr32(sd, CECRCTL1, reg);
881a0ec8d1dSHans Verkuil return 0;
882a0ec8d1dSHans Verkuil }
883a0ec8d1dSHans Verkuil
tc358743_cec_adap_log_addr(struct cec_adapter * adap,u8 log_addr)884a0ec8d1dSHans Verkuil static int tc358743_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
885a0ec8d1dSHans Verkuil {
886a0ec8d1dSHans Verkuil struct tc358743_state *state = adap->priv;
887a0ec8d1dSHans Verkuil struct v4l2_subdev *sd = &state->sd;
888a0ec8d1dSHans Verkuil unsigned int la = 0;
889a0ec8d1dSHans Verkuil
890a0ec8d1dSHans Verkuil if (log_addr != CEC_LOG_ADDR_INVALID) {
891a0ec8d1dSHans Verkuil la = i2c_rd32(sd, CECADD);
892a0ec8d1dSHans Verkuil la |= 1 << log_addr;
893a0ec8d1dSHans Verkuil }
894a0ec8d1dSHans Verkuil i2c_wr32(sd, CECADD, la);
895a0ec8d1dSHans Verkuil return 0;
896a0ec8d1dSHans Verkuil }
897a0ec8d1dSHans Verkuil
tc358743_cec_adap_transmit(struct cec_adapter * adap,u8 attempts,u32 signal_free_time,struct cec_msg * msg)898a0ec8d1dSHans Verkuil static int tc358743_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
899a0ec8d1dSHans Verkuil u32 signal_free_time, struct cec_msg *msg)
900a0ec8d1dSHans Verkuil {
901a0ec8d1dSHans Verkuil struct tc358743_state *state = adap->priv;
902a0ec8d1dSHans Verkuil struct v4l2_subdev *sd = &state->sd;
903a0ec8d1dSHans Verkuil unsigned int i;
904a0ec8d1dSHans Verkuil
905a0ec8d1dSHans Verkuil i2c_wr32(sd, CECTCTL,
906a0ec8d1dSHans Verkuil (cec_msg_is_broadcast(msg) ? MASK_CECBRD : 0) |
907a0ec8d1dSHans Verkuil (signal_free_time - 1));
908a0ec8d1dSHans Verkuil for (i = 0; i < msg->len; i++)
909a0ec8d1dSHans Verkuil i2c_wr32(sd, CECTBUF1 + i * 4,
910a0ec8d1dSHans Verkuil msg->msg[i] | ((i == msg->len - 1) ? MASK_CECTEOM : 0));
911a0ec8d1dSHans Verkuil i2c_wr32(sd, CECTEN, MASK_CECTEN);
912a0ec8d1dSHans Verkuil return 0;
913a0ec8d1dSHans Verkuil }
914a0ec8d1dSHans Verkuil
915a0ec8d1dSHans Verkuil static const struct cec_adap_ops tc358743_cec_adap_ops = {
916a0ec8d1dSHans Verkuil .adap_enable = tc358743_cec_adap_enable,
917a0ec8d1dSHans Verkuil .adap_log_addr = tc358743_cec_adap_log_addr,
918a0ec8d1dSHans Verkuil .adap_transmit = tc358743_cec_adap_transmit,
919a0ec8d1dSHans Verkuil .adap_monitor_all_enable = tc358743_cec_adap_monitor_all_enable,
920a0ec8d1dSHans Verkuil };
921a0ec8d1dSHans Verkuil
tc358743_cec_handler(struct v4l2_subdev * sd,u16 intstatus,bool * handled)922877cb8a4STom Rix static void tc358743_cec_handler(struct v4l2_subdev *sd, u16 intstatus,
923a0ec8d1dSHans Verkuil bool *handled)
924a0ec8d1dSHans Verkuil {
925a0ec8d1dSHans Verkuil struct tc358743_state *state = to_state(sd);
926a0ec8d1dSHans Verkuil unsigned int cec_rxint, cec_txint;
927a0ec8d1dSHans Verkuil unsigned int clr = 0;
928a0ec8d1dSHans Verkuil
929a0ec8d1dSHans Verkuil cec_rxint = i2c_rd32(sd, CECRSTAT);
930a0ec8d1dSHans Verkuil cec_txint = i2c_rd32(sd, CECTSTAT);
931a0ec8d1dSHans Verkuil
932a0ec8d1dSHans Verkuil if (intstatus & MASK_CEC_RINT)
933a0ec8d1dSHans Verkuil clr |= MASK_CECRICLR;
934a0ec8d1dSHans Verkuil if (intstatus & MASK_CEC_TINT)
935a0ec8d1dSHans Verkuil clr |= MASK_CECTICLR;
936a0ec8d1dSHans Verkuil i2c_wr32(sd, CECICLR, clr);
937a0ec8d1dSHans Verkuil
938a0ec8d1dSHans Verkuil if ((intstatus & MASK_CEC_TINT) && cec_txint) {
939a0ec8d1dSHans Verkuil if (cec_txint & MASK_CECTIEND)
940a0ec8d1dSHans Verkuil cec_transmit_attempt_done(state->cec_adap,
941a0ec8d1dSHans Verkuil CEC_TX_STATUS_OK);
942a0ec8d1dSHans Verkuil else if (cec_txint & MASK_CECTIAL)
943a0ec8d1dSHans Verkuil cec_transmit_attempt_done(state->cec_adap,
944a0ec8d1dSHans Verkuil CEC_TX_STATUS_ARB_LOST);
945a0ec8d1dSHans Verkuil else if (cec_txint & MASK_CECTIACK)
946a0ec8d1dSHans Verkuil cec_transmit_attempt_done(state->cec_adap,
947a0ec8d1dSHans Verkuil CEC_TX_STATUS_NACK);
948a0ec8d1dSHans Verkuil else if (cec_txint & MASK_CECTIUR) {
949a0ec8d1dSHans Verkuil /*
950a0ec8d1dSHans Verkuil * Not sure when this bit is set. Treat
951a0ec8d1dSHans Verkuil * it as an error for now.
952a0ec8d1dSHans Verkuil */
953a0ec8d1dSHans Verkuil cec_transmit_attempt_done(state->cec_adap,
954a0ec8d1dSHans Verkuil CEC_TX_STATUS_ERROR);
955a0ec8d1dSHans Verkuil }
956877cb8a4STom Rix if (handled)
957a0ec8d1dSHans Verkuil *handled = true;
958a0ec8d1dSHans Verkuil }
959a0ec8d1dSHans Verkuil if ((intstatus & MASK_CEC_RINT) &&
960a0ec8d1dSHans Verkuil (cec_rxint & MASK_CECRIEND)) {
961a0ec8d1dSHans Verkuil struct cec_msg msg = {};
962a0ec8d1dSHans Verkuil unsigned int i;
963a0ec8d1dSHans Verkuil unsigned int v;
964a0ec8d1dSHans Verkuil
965a0ec8d1dSHans Verkuil v = i2c_rd32(sd, CECRCTR);
966a0ec8d1dSHans Verkuil msg.len = v & 0x1f;
9673028fb90SHans Verkuil if (msg.len > CEC_MAX_MSG_SIZE)
9683028fb90SHans Verkuil msg.len = CEC_MAX_MSG_SIZE;
969a0ec8d1dSHans Verkuil for (i = 0; i < msg.len; i++) {
970a0ec8d1dSHans Verkuil v = i2c_rd32(sd, CECRBUF1 + i * 4);
971a0ec8d1dSHans Verkuil msg.msg[i] = v & 0xff;
972a0ec8d1dSHans Verkuil }
973a0ec8d1dSHans Verkuil cec_received_msg(state->cec_adap, &msg);
974877cb8a4STom Rix if (handled)
975a0ec8d1dSHans Verkuil *handled = true;
976a0ec8d1dSHans Verkuil }
977a0ec8d1dSHans Verkuil i2c_wr16(sd, INTSTATUS,
978a0ec8d1dSHans Verkuil intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
979a0ec8d1dSHans Verkuil }
980a0ec8d1dSHans Verkuil
981a0ec8d1dSHans Verkuil #endif
982a0ec8d1dSHans Verkuil
983d32d9864SMats Randgaard /* --------------- IRQ --------------- */
984d32d9864SMats Randgaard
tc358743_format_change(struct v4l2_subdev * sd)985d32d9864SMats Randgaard static void tc358743_format_change(struct v4l2_subdev *sd)
986d32d9864SMats Randgaard {
987d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
988d32d9864SMats Randgaard struct v4l2_dv_timings timings;
989d32d9864SMats Randgaard const struct v4l2_event tc358743_ev_fmt = {
990d32d9864SMats Randgaard .type = V4L2_EVENT_SOURCE_CHANGE,
991d32d9864SMats Randgaard .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
992d32d9864SMats Randgaard };
993d32d9864SMats Randgaard
994d32d9864SMats Randgaard if (tc358743_get_detected_timings(sd, &timings)) {
995d32d9864SMats Randgaard enable_stream(sd, false);
996d32d9864SMats Randgaard
9972874bf3eSMats Randgaard v4l2_dbg(1, debug, sd, "%s: No signal\n",
998d32d9864SMats Randgaard __func__);
999d32d9864SMats Randgaard } else {
100085f9e06cSHans Verkuil if (!v4l2_match_dv_timings(&state->timings, &timings, 0, false))
1001d32d9864SMats Randgaard enable_stream(sd, false);
1002d32d9864SMats Randgaard
10032874bf3eSMats Randgaard if (debug)
1004d32d9864SMats Randgaard v4l2_print_dv_timings(sd->name,
10052874bf3eSMats Randgaard "tc358743_format_change: New format: ",
1006d32d9864SMats Randgaard &timings, false);
1007d32d9864SMats Randgaard }
1008d32d9864SMats Randgaard
1009abeaca0fSPhilipp Zabel if (sd->devnode)
10101140f919SPhilipp Zabel v4l2_subdev_notify_event(sd, &tc358743_ev_fmt);
1011d32d9864SMats Randgaard }
1012d32d9864SMats Randgaard
tc358743_init_interrupts(struct v4l2_subdev * sd)1013d32d9864SMats Randgaard static void tc358743_init_interrupts(struct v4l2_subdev *sd)
1014d32d9864SMats Randgaard {
1015d32d9864SMats Randgaard u16 i;
1016d32d9864SMats Randgaard
1017d32d9864SMats Randgaard /* clear interrupt status registers */
1018d32d9864SMats Randgaard for (i = SYS_INT; i <= KEY_INT; i++)
1019d32d9864SMats Randgaard i2c_wr8(sd, i, 0xff);
1020d32d9864SMats Randgaard
1021d32d9864SMats Randgaard i2c_wr16(sd, INTSTATUS, 0xffff);
1022d32d9864SMats Randgaard }
1023d32d9864SMats Randgaard
tc358743_enable_interrupts(struct v4l2_subdev * sd,bool cable_connected)1024d32d9864SMats Randgaard static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
1025d32d9864SMats Randgaard bool cable_connected)
1026d32d9864SMats Randgaard {
1027d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
1028d32d9864SMats Randgaard cable_connected);
1029d32d9864SMats Randgaard
1030d32d9864SMats Randgaard if (cable_connected) {
1031d32d9864SMats Randgaard i2c_wr8(sd, SYS_INTM, ~(MASK_M_DDC | MASK_M_DVI_DET |
1032d32d9864SMats Randgaard MASK_M_HDMI_DET) & 0xff);
1033d32d9864SMats Randgaard i2c_wr8(sd, CLK_INTM, ~MASK_M_IN_DE_CHG);
1034d32d9864SMats Randgaard i2c_wr8(sd, CBIT_INTM, ~(MASK_M_CBIT_FS | MASK_M_AF_LOCK |
1035d32d9864SMats Randgaard MASK_M_AF_UNLOCK) & 0xff);
1036d32d9864SMats Randgaard i2c_wr8(sd, AUDIO_INTM, ~MASK_M_BUFINIT_END);
1037d32d9864SMats Randgaard i2c_wr8(sd, MISC_INTM, ~MASK_M_SYNC_CHG);
1038d32d9864SMats Randgaard } else {
1039d32d9864SMats Randgaard i2c_wr8(sd, SYS_INTM, ~MASK_M_DDC & 0xff);
1040d32d9864SMats Randgaard i2c_wr8(sd, CLK_INTM, 0xff);
1041d32d9864SMats Randgaard i2c_wr8(sd, CBIT_INTM, 0xff);
1042d32d9864SMats Randgaard i2c_wr8(sd, AUDIO_INTM, 0xff);
1043d32d9864SMats Randgaard i2c_wr8(sd, MISC_INTM, 0xff);
1044d32d9864SMats Randgaard }
1045d32d9864SMats Randgaard }
1046d32d9864SMats Randgaard
tc358743_hdmi_audio_int_handler(struct v4l2_subdev * sd,bool * handled)1047d32d9864SMats Randgaard static void tc358743_hdmi_audio_int_handler(struct v4l2_subdev *sd,
1048d32d9864SMats Randgaard bool *handled)
1049d32d9864SMats Randgaard {
1050d32d9864SMats Randgaard u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
1051d32d9864SMats Randgaard u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
1052d32d9864SMats Randgaard
1053d32d9864SMats Randgaard i2c_wr8(sd, AUDIO_INT, audio_int);
1054d32d9864SMats Randgaard
1055d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
1056d32d9864SMats Randgaard
1057d32d9864SMats Randgaard tc358743_s_ctrl_audio_sampling_rate(sd);
1058d32d9864SMats Randgaard tc358743_s_ctrl_audio_present(sd);
1059d32d9864SMats Randgaard }
1060d32d9864SMats Randgaard
tc358743_csi_err_int_handler(struct v4l2_subdev * sd,bool * handled)1061d32d9864SMats Randgaard static void tc358743_csi_err_int_handler(struct v4l2_subdev *sd, bool *handled)
1062d32d9864SMats Randgaard {
1063d32d9864SMats Randgaard v4l2_err(sd, "%s: CSI_ERR = 0x%x\n", __func__, i2c_rd32(sd, CSI_ERR));
1064d32d9864SMats Randgaard
1065d32d9864SMats Randgaard i2c_wr32(sd, CSI_INT_CLR, MASK_ICRER);
1066d32d9864SMats Randgaard }
1067d32d9864SMats Randgaard
tc358743_hdmi_misc_int_handler(struct v4l2_subdev * sd,bool * handled)1068d32d9864SMats Randgaard static void tc358743_hdmi_misc_int_handler(struct v4l2_subdev *sd,
1069d32d9864SMats Randgaard bool *handled)
1070d32d9864SMats Randgaard {
1071d32d9864SMats Randgaard u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
1072d32d9864SMats Randgaard u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
1073d32d9864SMats Randgaard
1074d32d9864SMats Randgaard i2c_wr8(sd, MISC_INT, misc_int);
1075d32d9864SMats Randgaard
1076d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
1077d32d9864SMats Randgaard
1078d32d9864SMats Randgaard if (misc_int & MASK_I_SYNC_CHG) {
1079d32d9864SMats Randgaard /* Reset the HDMI PHY to try to trigger proper lock on the
1080d32d9864SMats Randgaard * incoming video format. Erase BKSV to prevent that old keys
1081d32d9864SMats Randgaard * are used when a new source is connected. */
1082d32d9864SMats Randgaard if (no_sync(sd) || no_signal(sd)) {
1083d32d9864SMats Randgaard tc358743_reset_phy(sd);
1084d32d9864SMats Randgaard tc358743_erase_bksv(sd);
1085d32d9864SMats Randgaard }
1086d32d9864SMats Randgaard
1087d32d9864SMats Randgaard tc358743_format_change(sd);
1088d32d9864SMats Randgaard
1089d32d9864SMats Randgaard misc_int &= ~MASK_I_SYNC_CHG;
1090d32d9864SMats Randgaard if (handled)
1091d32d9864SMats Randgaard *handled = true;
1092d32d9864SMats Randgaard }
1093d32d9864SMats Randgaard
1094d32d9864SMats Randgaard if (misc_int) {
1095d32d9864SMats Randgaard v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
1096d32d9864SMats Randgaard __func__, misc_int);
1097d32d9864SMats Randgaard }
1098d32d9864SMats Randgaard }
1099d32d9864SMats Randgaard
tc358743_hdmi_cbit_int_handler(struct v4l2_subdev * sd,bool * handled)1100d32d9864SMats Randgaard static void tc358743_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
1101d32d9864SMats Randgaard bool *handled)
1102d32d9864SMats Randgaard {
1103d32d9864SMats Randgaard u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
1104d32d9864SMats Randgaard u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
1105d32d9864SMats Randgaard
1106d32d9864SMats Randgaard i2c_wr8(sd, CBIT_INT, cbit_int);
1107d32d9864SMats Randgaard
1108d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
1109d32d9864SMats Randgaard
1110d32d9864SMats Randgaard if (cbit_int & MASK_I_CBIT_FS) {
1111d32d9864SMats Randgaard
1112d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
1113d32d9864SMats Randgaard __func__);
1114d32d9864SMats Randgaard tc358743_s_ctrl_audio_sampling_rate(sd);
1115d32d9864SMats Randgaard
1116d32d9864SMats Randgaard cbit_int &= ~MASK_I_CBIT_FS;
1117d32d9864SMats Randgaard if (handled)
1118d32d9864SMats Randgaard *handled = true;
1119d32d9864SMats Randgaard }
1120d32d9864SMats Randgaard
1121d32d9864SMats Randgaard if (cbit_int & (MASK_I_AF_LOCK | MASK_I_AF_UNLOCK)) {
1122d32d9864SMats Randgaard
1123d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
1124d32d9864SMats Randgaard __func__);
1125d32d9864SMats Randgaard tc358743_s_ctrl_audio_present(sd);
1126d32d9864SMats Randgaard
1127d32d9864SMats Randgaard cbit_int &= ~(MASK_I_AF_LOCK | MASK_I_AF_UNLOCK);
1128d32d9864SMats Randgaard if (handled)
1129d32d9864SMats Randgaard *handled = true;
1130d32d9864SMats Randgaard }
1131d32d9864SMats Randgaard
1132d32d9864SMats Randgaard if (cbit_int) {
1133d32d9864SMats Randgaard v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
1134d32d9864SMats Randgaard __func__, cbit_int);
1135d32d9864SMats Randgaard }
1136d32d9864SMats Randgaard }
1137d32d9864SMats Randgaard
tc358743_hdmi_clk_int_handler(struct v4l2_subdev * sd,bool * handled)1138d32d9864SMats Randgaard static void tc358743_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
1139d32d9864SMats Randgaard {
1140d32d9864SMats Randgaard u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
1141d32d9864SMats Randgaard u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
1142d32d9864SMats Randgaard
1143d32d9864SMats Randgaard /* Bit 7 and bit 6 are set even when they are masked */
1144d32d9864SMats Randgaard i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_I_OUT_H_CHG);
1145d32d9864SMats Randgaard
1146d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
1147d32d9864SMats Randgaard
1148d32d9864SMats Randgaard if (clk_int & (MASK_I_IN_DE_CHG)) {
1149d32d9864SMats Randgaard
1150d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
1151d32d9864SMats Randgaard __func__);
1152d32d9864SMats Randgaard
1153d32d9864SMats Randgaard /* If the source switch to a new resolution with the same pixel
1154d32d9864SMats Randgaard * frequency as the existing (e.g. 1080p25 -> 720p50), the
1155d32d9864SMats Randgaard * I_SYNC_CHG interrupt is not always triggered, while the
1156d32d9864SMats Randgaard * I_IN_DE_CHG interrupt seems to work fine. Format change
1157d32d9864SMats Randgaard * notifications are only sent when the signal is stable to
1158d32d9864SMats Randgaard * reduce the number of notifications. */
1159d32d9864SMats Randgaard if (!no_signal(sd) && !no_sync(sd))
1160d32d9864SMats Randgaard tc358743_format_change(sd);
1161d32d9864SMats Randgaard
1162d32d9864SMats Randgaard clk_int &= ~(MASK_I_IN_DE_CHG);
1163d32d9864SMats Randgaard if (handled)
1164d32d9864SMats Randgaard *handled = true;
1165d32d9864SMats Randgaard }
1166d32d9864SMats Randgaard
1167d32d9864SMats Randgaard if (clk_int) {
1168d32d9864SMats Randgaard v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
1169d32d9864SMats Randgaard __func__, clk_int);
1170d32d9864SMats Randgaard }
1171d32d9864SMats Randgaard }
1172d32d9864SMats Randgaard
tc358743_hdmi_sys_int_handler(struct v4l2_subdev * sd,bool * handled)1173d32d9864SMats Randgaard static void tc358743_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
1174d32d9864SMats Randgaard {
1175d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1176d32d9864SMats Randgaard u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
1177d32d9864SMats Randgaard u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
1178d32d9864SMats Randgaard
1179d32d9864SMats Randgaard i2c_wr8(sd, SYS_INT, sys_int);
1180d32d9864SMats Randgaard
1181d32d9864SMats Randgaard v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
1182d32d9864SMats Randgaard
1183d32d9864SMats Randgaard if (sys_int & MASK_I_DDC) {
1184d32d9864SMats Randgaard bool tx_5v = tx_5v_power_present(sd);
1185d32d9864SMats Randgaard
1186d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
1187d32d9864SMats Randgaard __func__, tx_5v ? "yes" : "no");
1188d32d9864SMats Randgaard
1189d32d9864SMats Randgaard if (tx_5v) {
1190d32d9864SMats Randgaard tc358743_enable_edid(sd);
1191d32d9864SMats Randgaard } else {
1192d32d9864SMats Randgaard tc358743_enable_interrupts(sd, false);
1193d32d9864SMats Randgaard tc358743_disable_edid(sd);
1194d32d9864SMats Randgaard memset(&state->timings, 0, sizeof(state->timings));
1195d32d9864SMats Randgaard tc358743_erase_bksv(sd);
1196d32d9864SMats Randgaard tc358743_update_controls(sd);
1197d32d9864SMats Randgaard }
1198d32d9864SMats Randgaard
1199d32d9864SMats Randgaard sys_int &= ~MASK_I_DDC;
1200d32d9864SMats Randgaard if (handled)
1201d32d9864SMats Randgaard *handled = true;
1202d32d9864SMats Randgaard }
1203d32d9864SMats Randgaard
1204d32d9864SMats Randgaard if (sys_int & MASK_I_DVI) {
1205d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
1206d32d9864SMats Randgaard __func__);
1207d32d9864SMats Randgaard
1208d32d9864SMats Randgaard /* Reset the HDMI PHY to try to trigger proper lock on the
1209d32d9864SMats Randgaard * incoming video format. Erase BKSV to prevent that old keys
1210d32d9864SMats Randgaard * are used when a new source is connected. */
1211d32d9864SMats Randgaard if (no_sync(sd) || no_signal(sd)) {
1212d32d9864SMats Randgaard tc358743_reset_phy(sd);
1213d32d9864SMats Randgaard tc358743_erase_bksv(sd);
1214d32d9864SMats Randgaard }
1215d32d9864SMats Randgaard
1216d32d9864SMats Randgaard sys_int &= ~MASK_I_DVI;
1217d32d9864SMats Randgaard if (handled)
1218d32d9864SMats Randgaard *handled = true;
1219d32d9864SMats Randgaard }
1220d32d9864SMats Randgaard
1221d32d9864SMats Randgaard if (sys_int & MASK_I_HDMI) {
1222d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
1223d32d9864SMats Randgaard __func__);
1224d32d9864SMats Randgaard
1225d32d9864SMats Randgaard /* Register is reset in DVI mode (REF_01, c. 6.6.41) */
1226d32d9864SMats Randgaard i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
1227d32d9864SMats Randgaard
1228d32d9864SMats Randgaard sys_int &= ~MASK_I_HDMI;
1229d32d9864SMats Randgaard if (handled)
1230d32d9864SMats Randgaard *handled = true;
1231d32d9864SMats Randgaard }
1232d32d9864SMats Randgaard
1233d32d9864SMats Randgaard if (sys_int) {
1234d32d9864SMats Randgaard v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
1235d32d9864SMats Randgaard __func__, sys_int);
1236d32d9864SMats Randgaard }
1237d32d9864SMats Randgaard }
1238d32d9864SMats Randgaard
1239d32d9864SMats Randgaard /* --------------- CORE OPS --------------- */
1240d32d9864SMats Randgaard
tc358743_log_status(struct v4l2_subdev * sd)1241d32d9864SMats Randgaard static int tc358743_log_status(struct v4l2_subdev *sd)
1242d32d9864SMats Randgaard {
1243d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1244d32d9864SMats Randgaard struct v4l2_dv_timings timings;
1245d32d9864SMats Randgaard uint8_t hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
1246d32d9864SMats Randgaard uint16_t sysctl = i2c_rd16(sd, SYSCTL);
1247d32d9864SMats Randgaard u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
1248d32d9864SMats Randgaard const int deep_color_mode[4] = { 8, 10, 12, 16 };
1249d32d9864SMats Randgaard static const char * const input_color_space[] = {
1250db034018SHans Verkuil "RGB", "YCbCr 601", "opRGB", "YCbCr 709", "NA (4)",
1251d32d9864SMats Randgaard "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
1252db034018SHans Verkuil "NA(10)", "NA(11)", "NA(12)", "opYCC 601"};
1253d32d9864SMats Randgaard
1254d32d9864SMats Randgaard v4l2_info(sd, "-----Chip status-----\n");
1255d32d9864SMats Randgaard v4l2_info(sd, "Chip ID: 0x%02x\n",
1256d32d9864SMats Randgaard (i2c_rd16(sd, CHIPID) & MASK_CHIPID) >> 8);
1257d32d9864SMats Randgaard v4l2_info(sd, "Chip revision: 0x%02x\n",
1258d32d9864SMats Randgaard i2c_rd16(sd, CHIPID) & MASK_REVID);
1259d32d9864SMats Randgaard v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
1260d32d9864SMats Randgaard !!(sysctl & MASK_IRRST),
1261d32d9864SMats Randgaard !!(sysctl & MASK_CECRST),
1262d32d9864SMats Randgaard !!(sysctl & MASK_CTXRST),
1263d32d9864SMats Randgaard !!(sysctl & MASK_HDMIRST));
1264d32d9864SMats Randgaard v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
1265d32d9864SMats Randgaard v4l2_info(sd, "Cable detected (+5V power): %s\n",
1266d32d9864SMats Randgaard hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
1267d32d9864SMats Randgaard v4l2_info(sd, "DDC lines enabled: %s\n",
1268d32d9864SMats Randgaard (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_E_DDC) ?
1269d32d9864SMats Randgaard "yes" : "no");
1270d32d9864SMats Randgaard v4l2_info(sd, "Hotplug enabled: %s\n",
1271d32d9864SMats Randgaard (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
1272d32d9864SMats Randgaard "yes" : "no");
1273d32d9864SMats Randgaard v4l2_info(sd, "CEC enabled: %s\n",
1274d32d9864SMats Randgaard (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
1275d32d9864SMats Randgaard v4l2_info(sd, "-----Signal status-----\n");
1276d32d9864SMats Randgaard v4l2_info(sd, "TMDS signal detected: %s\n",
1277d32d9864SMats Randgaard hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
1278d32d9864SMats Randgaard v4l2_info(sd, "Stable sync signal: %s\n",
1279d32d9864SMats Randgaard hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
1280d32d9864SMats Randgaard v4l2_info(sd, "PHY PLL locked: %s\n",
1281d32d9864SMats Randgaard hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
1282d32d9864SMats Randgaard v4l2_info(sd, "PHY DE detected: %s\n",
1283d32d9864SMats Randgaard hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
1284d32d9864SMats Randgaard
1285d32d9864SMats Randgaard if (tc358743_get_detected_timings(sd, &timings)) {
1286d32d9864SMats Randgaard v4l2_info(sd, "No video detected\n");
1287d32d9864SMats Randgaard } else {
1288d32d9864SMats Randgaard v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
1289d32d9864SMats Randgaard true);
1290d32d9864SMats Randgaard }
1291d32d9864SMats Randgaard v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
1292d32d9864SMats Randgaard true);
1293d32d9864SMats Randgaard
1294d32d9864SMats Randgaard v4l2_info(sd, "-----CSI-TX status-----\n");
1295d32d9864SMats Randgaard v4l2_info(sd, "Lanes needed: %d\n",
1296d32d9864SMats Randgaard tc358743_num_csi_lanes_needed(sd));
1297d32d9864SMats Randgaard v4l2_info(sd, "Lanes in use: %d\n",
129859e34ba8SMats Randgaard state->csi_lanes_in_use);
1299d32d9864SMats Randgaard v4l2_info(sd, "Waiting for particular sync signal: %s\n",
1300d32d9864SMats Randgaard (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
1301d32d9864SMats Randgaard "yes" : "no");
1302d32d9864SMats Randgaard v4l2_info(sd, "Transmit mode: %s\n",
1303d32d9864SMats Randgaard (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
1304d32d9864SMats Randgaard "yes" : "no");
1305d32d9864SMats Randgaard v4l2_info(sd, "Receive mode: %s\n",
1306d32d9864SMats Randgaard (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
1307d32d9864SMats Randgaard "yes" : "no");
1308d32d9864SMats Randgaard v4l2_info(sd, "Stopped: %s\n",
1309d32d9864SMats Randgaard (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
1310d32d9864SMats Randgaard "yes" : "no");
1311d32d9864SMats Randgaard v4l2_info(sd, "Color space: %s\n",
1312d32d9864SMats Randgaard state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
1313d32d9864SMats Randgaard "YCbCr 422 16-bit" :
1314d32d9864SMats Randgaard state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
1315d32d9864SMats Randgaard "RGB 888 24-bit" : "Unsupported");
1316d32d9864SMats Randgaard
1317d32d9864SMats Randgaard v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
1318d32d9864SMats Randgaard v4l2_info(sd, "HDCP encrypted content: %s\n",
1319d32d9864SMats Randgaard hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
1320d32d9864SMats Randgaard v4l2_info(sd, "Input color space: %s %s range\n",
1321d32d9864SMats Randgaard input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
1322d32d9864SMats Randgaard (vi_status3 & MASK_LIMITED) ? "limited" : "full");
1323d32d9864SMats Randgaard if (!is_hdmi(sd))
1324d32d9864SMats Randgaard return 0;
1325d32d9864SMats Randgaard v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
1326d32d9864SMats Randgaard "off");
1327d32d9864SMats Randgaard v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
1328d32d9864SMats Randgaard deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
1329d32d9864SMats Randgaard MASK_S_DEEPCOLOR) >> 2]);
1330d32d9864SMats Randgaard print_avi_infoframe(sd);
1331d32d9864SMats Randgaard
1332d32d9864SMats Randgaard return 0;
1333d32d9864SMats Randgaard }
1334d32d9864SMats Randgaard
1335d32d9864SMats Randgaard #ifdef CONFIG_VIDEO_ADV_DEBUG
tc358743_print_register_map(struct v4l2_subdev * sd)1336d32d9864SMats Randgaard static void tc358743_print_register_map(struct v4l2_subdev *sd)
1337d32d9864SMats Randgaard {
133885e510a1SHans Verkuil v4l2_info(sd, "0x0000-0x00FF: Global Control Register\n");
133985e510a1SHans Verkuil v4l2_info(sd, "0x0100-0x01FF: CSI2-TX PHY Register\n");
134085e510a1SHans Verkuil v4l2_info(sd, "0x0200-0x03FF: CSI2-TX PPI Register\n");
134185e510a1SHans Verkuil v4l2_info(sd, "0x0400-0x05FF: Reserved\n");
134285e510a1SHans Verkuil v4l2_info(sd, "0x0600-0x06FF: CEC Register\n");
134385e510a1SHans Verkuil v4l2_info(sd, "0x0700-0x84FF: Reserved\n");
134485e510a1SHans Verkuil v4l2_info(sd, "0x8500-0x85FF: HDMIRX System Control Register\n");
134585e510a1SHans Verkuil v4l2_info(sd, "0x8600-0x86FF: HDMIRX Audio Control Register\n");
134685e510a1SHans Verkuil v4l2_info(sd, "0x8700-0x87FF: HDMIRX InfoFrame packet data Register\n");
134785e510a1SHans Verkuil v4l2_info(sd, "0x8800-0x88FF: HDMIRX HDCP Port Register\n");
134885e510a1SHans Verkuil v4l2_info(sd, "0x8900-0x89FF: HDMIRX Video Output Port & 3D Register\n");
134985e510a1SHans Verkuil v4l2_info(sd, "0x8A00-0x8BFF: Reserved\n");
135085e510a1SHans Verkuil v4l2_info(sd, "0x8C00-0x8FFF: HDMIRX EDID-RAM (1024bytes)\n");
135185e510a1SHans Verkuil v4l2_info(sd, "0x9000-0x90FF: HDMIRX GBD Extraction Control\n");
135285e510a1SHans Verkuil v4l2_info(sd, "0x9100-0x92FF: HDMIRX GBD RAM read\n");
1353d32d9864SMats Randgaard v4l2_info(sd, "0x9300- : Reserved\n");
1354d32d9864SMats Randgaard }
1355d32d9864SMats Randgaard
tc358743_get_reg_size(u16 address)1356d32d9864SMats Randgaard static int tc358743_get_reg_size(u16 address)
1357d32d9864SMats Randgaard {
1358d32d9864SMats Randgaard /* REF_01 p. 66-72 */
1359d32d9864SMats Randgaard if (address <= 0x00ff)
1360d32d9864SMats Randgaard return 2;
1361d32d9864SMats Randgaard else if ((address >= 0x0100) && (address <= 0x06FF))
1362d32d9864SMats Randgaard return 4;
1363d32d9864SMats Randgaard else if ((address >= 0x0700) && (address <= 0x84ff))
1364d32d9864SMats Randgaard return 2;
1365d32d9864SMats Randgaard else
1366d32d9864SMats Randgaard return 1;
1367d32d9864SMats Randgaard }
1368d32d9864SMats Randgaard
tc358743_g_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)1369d32d9864SMats Randgaard static int tc358743_g_register(struct v4l2_subdev *sd,
1370d32d9864SMats Randgaard struct v4l2_dbg_register *reg)
1371d32d9864SMats Randgaard {
1372d32d9864SMats Randgaard if (reg->reg > 0xffff) {
1373d32d9864SMats Randgaard tc358743_print_register_map(sd);
1374d32d9864SMats Randgaard return -EINVAL;
1375d32d9864SMats Randgaard }
1376d32d9864SMats Randgaard
1377d32d9864SMats Randgaard reg->size = tc358743_get_reg_size(reg->reg);
1378d32d9864SMats Randgaard
13793538aa6eSArnd Bergmann reg->val = i2c_rdreg(sd, reg->reg, reg->size);
1380d32d9864SMats Randgaard
1381d32d9864SMats Randgaard return 0;
1382d32d9864SMats Randgaard }
1383d32d9864SMats Randgaard
tc358743_s_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)1384d32d9864SMats Randgaard static int tc358743_s_register(struct v4l2_subdev *sd,
1385d32d9864SMats Randgaard const struct v4l2_dbg_register *reg)
1386d32d9864SMats Randgaard {
1387d32d9864SMats Randgaard if (reg->reg > 0xffff) {
1388d32d9864SMats Randgaard tc358743_print_register_map(sd);
1389d32d9864SMats Randgaard return -EINVAL;
1390d32d9864SMats Randgaard }
1391d32d9864SMats Randgaard
1392d32d9864SMats Randgaard /* It should not be possible for the user to enable HDCP with a simple
1393d32d9864SMats Randgaard * v4l2-dbg command.
1394d32d9864SMats Randgaard *
1395d32d9864SMats Randgaard * DO NOT REMOVE THIS unless all other issues with HDCP have been
1396d32d9864SMats Randgaard * resolved.
1397d32d9864SMats Randgaard */
1398d32d9864SMats Randgaard if (reg->reg == HDCP_MODE ||
1399d32d9864SMats Randgaard reg->reg == HDCP_REG1 ||
1400d32d9864SMats Randgaard reg->reg == HDCP_REG2 ||
1401d32d9864SMats Randgaard reg->reg == HDCP_REG3 ||
1402d32d9864SMats Randgaard reg->reg == BCAPS)
1403d32d9864SMats Randgaard return 0;
1404d32d9864SMats Randgaard
14053538aa6eSArnd Bergmann i2c_wrreg(sd, (u16)reg->reg, reg->val,
1406d32d9864SMats Randgaard tc358743_get_reg_size(reg->reg));
1407d32d9864SMats Randgaard
1408d32d9864SMats Randgaard return 0;
1409d32d9864SMats Randgaard }
1410d32d9864SMats Randgaard #endif
1411d32d9864SMats Randgaard
tc358743_isr(struct v4l2_subdev * sd,u32 status,bool * handled)1412d32d9864SMats Randgaard static int tc358743_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1413d32d9864SMats Randgaard {
1414d32d9864SMats Randgaard u16 intstatus = i2c_rd16(sd, INTSTATUS);
1415d32d9864SMats Randgaard
1416d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04x\n", __func__, intstatus);
1417d32d9864SMats Randgaard
1418d32d9864SMats Randgaard if (intstatus & MASK_HDMI_INT) {
1419d32d9864SMats Randgaard u8 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
1420d32d9864SMats Randgaard u8 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
1421d32d9864SMats Randgaard
1422d32d9864SMats Randgaard if (hdmi_int0 & MASK_I_MISC)
1423d32d9864SMats Randgaard tc358743_hdmi_misc_int_handler(sd, handled);
1424d32d9864SMats Randgaard if (hdmi_int1 & MASK_I_CBIT)
1425d32d9864SMats Randgaard tc358743_hdmi_cbit_int_handler(sd, handled);
1426d32d9864SMats Randgaard if (hdmi_int1 & MASK_I_CLK)
1427d32d9864SMats Randgaard tc358743_hdmi_clk_int_handler(sd, handled);
1428d32d9864SMats Randgaard if (hdmi_int1 & MASK_I_SYS)
1429d32d9864SMats Randgaard tc358743_hdmi_sys_int_handler(sd, handled);
1430d32d9864SMats Randgaard if (hdmi_int1 & MASK_I_AUD)
1431d32d9864SMats Randgaard tc358743_hdmi_audio_int_handler(sd, handled);
1432d32d9864SMats Randgaard
1433d32d9864SMats Randgaard i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
1434d32d9864SMats Randgaard intstatus &= ~MASK_HDMI_INT;
1435d32d9864SMats Randgaard }
1436d32d9864SMats Randgaard
1437a0ec8d1dSHans Verkuil #ifdef CONFIG_VIDEO_TC358743_CEC
1438a0ec8d1dSHans Verkuil if (intstatus & (MASK_CEC_RINT | MASK_CEC_TINT)) {
1439877cb8a4STom Rix tc358743_cec_handler(sd, intstatus, handled);
1440a0ec8d1dSHans Verkuil i2c_wr16(sd, INTSTATUS,
1441a0ec8d1dSHans Verkuil intstatus & (MASK_CEC_RINT | MASK_CEC_TINT));
1442a0ec8d1dSHans Verkuil intstatus &= ~(MASK_CEC_RINT | MASK_CEC_TINT);
1443a0ec8d1dSHans Verkuil }
1444a0ec8d1dSHans Verkuil #endif
1445a0ec8d1dSHans Verkuil
1446d32d9864SMats Randgaard if (intstatus & MASK_CSI_INT) {
1447d32d9864SMats Randgaard u32 csi_int = i2c_rd32(sd, CSI_INT);
1448d32d9864SMats Randgaard
1449d32d9864SMats Randgaard if (csi_int & MASK_INTER)
1450d32d9864SMats Randgaard tc358743_csi_err_int_handler(sd, handled);
1451d32d9864SMats Randgaard
1452d32d9864SMats Randgaard i2c_wr16(sd, INTSTATUS, MASK_CSI_INT);
1453d32d9864SMats Randgaard }
1454d32d9864SMats Randgaard
1455d32d9864SMats Randgaard intstatus = i2c_rd16(sd, INTSTATUS);
1456d32d9864SMats Randgaard if (intstatus) {
1457d32d9864SMats Randgaard v4l2_dbg(1, debug, sd,
1458d32d9864SMats Randgaard "%s: Unhandled IntStatus interrupts: 0x%02x\n",
1459d32d9864SMats Randgaard __func__, intstatus);
1460d32d9864SMats Randgaard }
1461d32d9864SMats Randgaard
1462d32d9864SMats Randgaard return 0;
1463d32d9864SMats Randgaard }
1464d32d9864SMats Randgaard
tc358743_irq_handler(int irq,void * dev_id)1465d747b806SPhilipp Zabel static irqreturn_t tc358743_irq_handler(int irq, void *dev_id)
1466d747b806SPhilipp Zabel {
1467d747b806SPhilipp Zabel struct tc358743_state *state = dev_id;
1468274cf92dSTom Rix bool handled = false;
1469d747b806SPhilipp Zabel
1470d747b806SPhilipp Zabel tc358743_isr(&state->sd, 0, &handled);
1471d747b806SPhilipp Zabel
1472d747b806SPhilipp Zabel return handled ? IRQ_HANDLED : IRQ_NONE;
1473d747b806SPhilipp Zabel }
1474d747b806SPhilipp Zabel
tc358743_irq_poll_timer(struct timer_list * t)1475a88cd60fSKees Cook static void tc358743_irq_poll_timer(struct timer_list *t)
14764e66a52aSDave Stevenson {
1477a88cd60fSKees Cook struct tc358743_state *state = from_timer(state, t, timer);
1478a0ec8d1dSHans Verkuil unsigned int msecs;
14794e66a52aSDave Stevenson
14804e66a52aSDave Stevenson schedule_work(&state->work_i2c_poll);
1481a0ec8d1dSHans Verkuil /*
1482a0ec8d1dSHans Verkuil * If CEC is present, then we need to poll more frequently,
1483a0ec8d1dSHans Verkuil * otherwise we will miss CEC messages.
1484a0ec8d1dSHans Verkuil */
1485a0ec8d1dSHans Verkuil msecs = state->cec_adap ? POLL_INTERVAL_CEC_MS : POLL_INTERVAL_MS;
1486a0ec8d1dSHans Verkuil mod_timer(&state->timer, jiffies + msecs_to_jiffies(msecs));
14874e66a52aSDave Stevenson }
14884e66a52aSDave Stevenson
tc358743_work_i2c_poll(struct work_struct * work)14894e66a52aSDave Stevenson static void tc358743_work_i2c_poll(struct work_struct *work)
14904e66a52aSDave Stevenson {
14914e66a52aSDave Stevenson struct tc358743_state *state = container_of(work,
14924e66a52aSDave Stevenson struct tc358743_state, work_i2c_poll);
14934e66a52aSDave Stevenson bool handled;
14944e66a52aSDave Stevenson
14954e66a52aSDave Stevenson tc358743_isr(&state->sd, 0, &handled);
14964e66a52aSDave Stevenson }
14974e66a52aSDave Stevenson
tc358743_subscribe_event(struct v4l2_subdev * sd,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)14981140f919SPhilipp Zabel static int tc358743_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
14991140f919SPhilipp Zabel struct v4l2_event_subscription *sub)
15001140f919SPhilipp Zabel {
15011140f919SPhilipp Zabel switch (sub->type) {
15021140f919SPhilipp Zabel case V4L2_EVENT_SOURCE_CHANGE:
15031140f919SPhilipp Zabel return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
15041140f919SPhilipp Zabel case V4L2_EVENT_CTRL:
15051140f919SPhilipp Zabel return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
15061140f919SPhilipp Zabel default:
15071140f919SPhilipp Zabel return -EINVAL;
15081140f919SPhilipp Zabel }
15091140f919SPhilipp Zabel }
15101140f919SPhilipp Zabel
1511d32d9864SMats Randgaard /* --------------- VIDEO OPS --------------- */
1512d32d9864SMats Randgaard
tc358743_g_input_status(struct v4l2_subdev * sd,u32 * status)1513d32d9864SMats Randgaard static int tc358743_g_input_status(struct v4l2_subdev *sd, u32 *status)
1514d32d9864SMats Randgaard {
1515d32d9864SMats Randgaard *status = 0;
1516d32d9864SMats Randgaard *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1517d32d9864SMats Randgaard *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
1518d32d9864SMats Randgaard
1519d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1520d32d9864SMats Randgaard
1521d32d9864SMats Randgaard return 0;
1522d32d9864SMats Randgaard }
1523d32d9864SMats Randgaard
tc358743_s_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)152438b317caSPaweł Anikiel static int tc358743_s_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1525d32d9864SMats Randgaard struct v4l2_dv_timings *timings)
1526d32d9864SMats Randgaard {
1527d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1528d32d9864SMats Randgaard
152938b317caSPaweł Anikiel if (pad != 0)
153038b317caSPaweł Anikiel return -EINVAL;
153138b317caSPaweł Anikiel
1532d32d9864SMats Randgaard if (!timings)
1533d32d9864SMats Randgaard return -EINVAL;
1534d32d9864SMats Randgaard
1535d32d9864SMats Randgaard if (debug)
1536d32d9864SMats Randgaard v4l2_print_dv_timings(sd->name, "tc358743_s_dv_timings: ",
1537d32d9864SMats Randgaard timings, false);
1538d32d9864SMats Randgaard
153985f9e06cSHans Verkuil if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
1540d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1541d32d9864SMats Randgaard return 0;
1542d32d9864SMats Randgaard }
1543d32d9864SMats Randgaard
1544d32d9864SMats Randgaard if (!v4l2_valid_dv_timings(timings,
1545d32d9864SMats Randgaard &tc358743_timings_cap, NULL, NULL)) {
1546d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1547d32d9864SMats Randgaard return -ERANGE;
1548d32d9864SMats Randgaard }
1549d32d9864SMats Randgaard
1550d32d9864SMats Randgaard state->timings = *timings;
1551d32d9864SMats Randgaard
1552d32d9864SMats Randgaard enable_stream(sd, false);
1553d32d9864SMats Randgaard tc358743_set_pll(sd);
1554d32d9864SMats Randgaard tc358743_set_csi(sd);
1555d32d9864SMats Randgaard
1556d32d9864SMats Randgaard return 0;
1557d32d9864SMats Randgaard }
1558d32d9864SMats Randgaard
tc358743_g_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)155938b317caSPaweł Anikiel static int tc358743_g_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1560d32d9864SMats Randgaard struct v4l2_dv_timings *timings)
1561d32d9864SMats Randgaard {
1562d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1563d32d9864SMats Randgaard
156438b317caSPaweł Anikiel if (pad != 0)
156538b317caSPaweł Anikiel return -EINVAL;
156638b317caSPaweł Anikiel
1567d32d9864SMats Randgaard *timings = state->timings;
1568d32d9864SMats Randgaard
1569d32d9864SMats Randgaard return 0;
1570d32d9864SMats Randgaard }
1571d32d9864SMats Randgaard
tc358743_enum_dv_timings(struct v4l2_subdev * sd,struct v4l2_enum_dv_timings * timings)1572d32d9864SMats Randgaard static int tc358743_enum_dv_timings(struct v4l2_subdev *sd,
1573d32d9864SMats Randgaard struct v4l2_enum_dv_timings *timings)
1574d32d9864SMats Randgaard {
1575d32d9864SMats Randgaard if (timings->pad != 0)
1576d32d9864SMats Randgaard return -EINVAL;
1577d32d9864SMats Randgaard
1578d32d9864SMats Randgaard return v4l2_enum_dv_timings_cap(timings,
1579d32d9864SMats Randgaard &tc358743_timings_cap, NULL, NULL);
1580d32d9864SMats Randgaard }
1581d32d9864SMats Randgaard
tc358743_query_dv_timings(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_dv_timings * timings)158238b317caSPaweł Anikiel static int tc358743_query_dv_timings(struct v4l2_subdev *sd, unsigned int pad,
1583d32d9864SMats Randgaard struct v4l2_dv_timings *timings)
1584d32d9864SMats Randgaard {
1585d32d9864SMats Randgaard int ret;
1586d32d9864SMats Randgaard
158738b317caSPaweł Anikiel if (pad != 0)
158838b317caSPaweł Anikiel return -EINVAL;
158938b317caSPaweł Anikiel
1590d32d9864SMats Randgaard ret = tc358743_get_detected_timings(sd, timings);
1591d32d9864SMats Randgaard if (ret)
1592d32d9864SMats Randgaard return ret;
1593d32d9864SMats Randgaard
1594d32d9864SMats Randgaard if (debug)
1595d32d9864SMats Randgaard v4l2_print_dv_timings(sd->name, "tc358743_query_dv_timings: ",
1596d32d9864SMats Randgaard timings, false);
1597d32d9864SMats Randgaard
1598d32d9864SMats Randgaard if (!v4l2_valid_dv_timings(timings,
1599d32d9864SMats Randgaard &tc358743_timings_cap, NULL, NULL)) {
1600d32d9864SMats Randgaard v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1601d32d9864SMats Randgaard return -ERANGE;
1602d32d9864SMats Randgaard }
1603d32d9864SMats Randgaard
1604d32d9864SMats Randgaard return 0;
1605d32d9864SMats Randgaard }
1606d32d9864SMats Randgaard
tc358743_dv_timings_cap(struct v4l2_subdev * sd,struct v4l2_dv_timings_cap * cap)1607d32d9864SMats Randgaard static int tc358743_dv_timings_cap(struct v4l2_subdev *sd,
1608d32d9864SMats Randgaard struct v4l2_dv_timings_cap *cap)
1609d32d9864SMats Randgaard {
1610d32d9864SMats Randgaard if (cap->pad != 0)
1611d32d9864SMats Randgaard return -EINVAL;
1612d32d9864SMats Randgaard
1613d32d9864SMats Randgaard *cap = tc358743_timings_cap;
1614d32d9864SMats Randgaard
1615d32d9864SMats Randgaard return 0;
1616d32d9864SMats Randgaard }
1617d32d9864SMats Randgaard
tc358743_get_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)16180c3da525SJacopo Mondi static int tc358743_get_mbus_config(struct v4l2_subdev *sd,
16190c3da525SJacopo Mondi unsigned int pad,
1620d32d9864SMats Randgaard struct v4l2_mbus_config *cfg)
1621d32d9864SMats Randgaard {
162259e34ba8SMats Randgaard struct tc358743_state *state = to_state(sd);
162359e34ba8SMats Randgaard
16242d95e7edSSakari Ailus cfg->type = V4L2_MBUS_CSI2_DPHY;
1625d32d9864SMats Randgaard
1626d32d9864SMats Randgaard /* Support for non-continuous CSI-2 clock is missing in the driver */
1627b9f7caa7SLaurent Pinchart cfg->bus.mipi_csi2.flags = 0;
16286a7bdd89SLaurent Pinchart cfg->bus.mipi_csi2.num_data_lanes = state->csi_lanes_in_use;
1629d32d9864SMats Randgaard
1630d32d9864SMats Randgaard return 0;
1631d32d9864SMats Randgaard }
1632d32d9864SMats Randgaard
tc358743_s_stream(struct v4l2_subdev * sd,int enable)1633d32d9864SMats Randgaard static int tc358743_s_stream(struct v4l2_subdev *sd, int enable)
1634d32d9864SMats Randgaard {
1635d32d9864SMats Randgaard enable_stream(sd, enable);
1636fbd4676cSPhilipp Zabel if (!enable) {
16373863d4bbSJacob Chen /* Put all lanes in LP-11 state (STOPSTATE) */
1638fbd4676cSPhilipp Zabel tc358743_set_csi(sd);
1639fbd4676cSPhilipp Zabel }
1640d32d9864SMats Randgaard
1641d32d9864SMats Randgaard return 0;
1642d32d9864SMats Randgaard }
1643d32d9864SMats Randgaard
1644d32d9864SMats Randgaard /* --------------- PAD OPS --------------- */
1645d32d9864SMats Randgaard
tc358743_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)16463cb0fe6fSDave Stevenson static int tc358743_enum_mbus_code(struct v4l2_subdev *sd,
16470d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
16483cb0fe6fSDave Stevenson struct v4l2_subdev_mbus_code_enum *code)
16493cb0fe6fSDave Stevenson {
16503cb0fe6fSDave Stevenson switch (code->index) {
16513cb0fe6fSDave Stevenson case 0:
16523cb0fe6fSDave Stevenson code->code = MEDIA_BUS_FMT_RGB888_1X24;
16533cb0fe6fSDave Stevenson break;
16543cb0fe6fSDave Stevenson case 1:
16553cb0fe6fSDave Stevenson code->code = MEDIA_BUS_FMT_UYVY8_1X16;
16563cb0fe6fSDave Stevenson break;
16573cb0fe6fSDave Stevenson default:
16583cb0fe6fSDave Stevenson return -EINVAL;
16593cb0fe6fSDave Stevenson }
16603cb0fe6fSDave Stevenson return 0;
16613cb0fe6fSDave Stevenson }
16623cb0fe6fSDave Stevenson
tc358743_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1663d32d9864SMats Randgaard static int tc358743_get_fmt(struct v4l2_subdev *sd,
16640d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1665d32d9864SMats Randgaard struct v4l2_subdev_format *format)
1666d32d9864SMats Randgaard {
1667d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1668d32d9864SMats Randgaard u8 vi_rep = i2c_rd8(sd, VI_REP);
1669d32d9864SMats Randgaard
1670d32d9864SMats Randgaard if (format->pad != 0)
1671d32d9864SMats Randgaard return -EINVAL;
1672d32d9864SMats Randgaard
1673d32d9864SMats Randgaard format->format.code = state->mbus_fmt_code;
1674d32d9864SMats Randgaard format->format.width = state->timings.bt.width;
1675d32d9864SMats Randgaard format->format.height = state->timings.bt.height;
1676d32d9864SMats Randgaard format->format.field = V4L2_FIELD_NONE;
1677d32d9864SMats Randgaard
1678d32d9864SMats Randgaard switch (vi_rep & MASK_VOUT_COLOR_SEL) {
1679d32d9864SMats Randgaard case MASK_VOUT_COLOR_RGB_FULL:
1680d32d9864SMats Randgaard case MASK_VOUT_COLOR_RGB_LIMITED:
1681d32d9864SMats Randgaard format->format.colorspace = V4L2_COLORSPACE_SRGB;
1682d32d9864SMats Randgaard break;
1683d32d9864SMats Randgaard case MASK_VOUT_COLOR_601_YCBCR_LIMITED:
1684d32d9864SMats Randgaard case MASK_VOUT_COLOR_601_YCBCR_FULL:
1685d32d9864SMats Randgaard format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1686d32d9864SMats Randgaard break;
1687d32d9864SMats Randgaard case MASK_VOUT_COLOR_709_YCBCR_FULL:
1688d32d9864SMats Randgaard case MASK_VOUT_COLOR_709_YCBCR_LIMITED:
1689d32d9864SMats Randgaard format->format.colorspace = V4L2_COLORSPACE_REC709;
1690d32d9864SMats Randgaard break;
1691d32d9864SMats Randgaard default:
1692d32d9864SMats Randgaard format->format.colorspace = 0;
1693d32d9864SMats Randgaard break;
1694d32d9864SMats Randgaard }
1695d32d9864SMats Randgaard
1696d32d9864SMats Randgaard return 0;
1697d32d9864SMats Randgaard }
1698d32d9864SMats Randgaard
tc358743_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1699d32d9864SMats Randgaard static int tc358743_set_fmt(struct v4l2_subdev *sd,
17000d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
1701d32d9864SMats Randgaard struct v4l2_subdev_format *format)
1702d32d9864SMats Randgaard {
1703d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1704d32d9864SMats Randgaard
1705d32d9864SMats Randgaard u32 code = format->format.code; /* is overwritten by get_fmt */
17060d346d2aSTomi Valkeinen int ret = tc358743_get_fmt(sd, sd_state, format);
1707d32d9864SMats Randgaard
1708d32d9864SMats Randgaard format->format.code = code;
1709d32d9864SMats Randgaard
1710d32d9864SMats Randgaard if (ret)
1711d32d9864SMats Randgaard return ret;
1712d32d9864SMats Randgaard
1713d32d9864SMats Randgaard switch (code) {
1714d32d9864SMats Randgaard case MEDIA_BUS_FMT_RGB888_1X24:
1715d32d9864SMats Randgaard case MEDIA_BUS_FMT_UYVY8_1X16:
1716d32d9864SMats Randgaard break;
1717d32d9864SMats Randgaard default:
1718d32d9864SMats Randgaard return -EINVAL;
1719d32d9864SMats Randgaard }
1720d32d9864SMats Randgaard
1721d32d9864SMats Randgaard if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1722d32d9864SMats Randgaard return 0;
1723d32d9864SMats Randgaard
1724d32d9864SMats Randgaard state->mbus_fmt_code = format->format.code;
1725d32d9864SMats Randgaard
1726d32d9864SMats Randgaard enable_stream(sd, false);
1727d32d9864SMats Randgaard tc358743_set_pll(sd);
1728d32d9864SMats Randgaard tc358743_set_csi(sd);
1729d32d9864SMats Randgaard tc358743_set_csi_color_space(sd);
1730d32d9864SMats Randgaard
1731d32d9864SMats Randgaard return 0;
1732d32d9864SMats Randgaard }
1733d32d9864SMats Randgaard
tc358743_g_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)1734d32d9864SMats Randgaard static int tc358743_g_edid(struct v4l2_subdev *sd,
1735d32d9864SMats Randgaard struct v4l2_subdev_edid *edid)
1736d32d9864SMats Randgaard {
1737d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1738d32d9864SMats Randgaard
173972777724SHans Verkuil memset(edid->reserved, 0, sizeof(edid->reserved));
174072777724SHans Verkuil
1741d32d9864SMats Randgaard if (edid->pad != 0)
1742d32d9864SMats Randgaard return -EINVAL;
1743d32d9864SMats Randgaard
1744d32d9864SMats Randgaard if (edid->start_block == 0 && edid->blocks == 0) {
1745d32d9864SMats Randgaard edid->blocks = state->edid_blocks_written;
1746d32d9864SMats Randgaard return 0;
1747d32d9864SMats Randgaard }
1748d32d9864SMats Randgaard
1749d32d9864SMats Randgaard if (state->edid_blocks_written == 0)
1750d32d9864SMats Randgaard return -ENODATA;
1751d32d9864SMats Randgaard
1752d32d9864SMats Randgaard if (edid->start_block >= state->edid_blocks_written ||
1753d32d9864SMats Randgaard edid->blocks == 0)
1754d32d9864SMats Randgaard return -EINVAL;
1755d32d9864SMats Randgaard
1756d32d9864SMats Randgaard if (edid->start_block + edid->blocks > state->edid_blocks_written)
1757d32d9864SMats Randgaard edid->blocks = state->edid_blocks_written - edid->start_block;
1758d32d9864SMats Randgaard
1759d32d9864SMats Randgaard i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
1760d32d9864SMats Randgaard edid->blocks * EDID_BLOCK_SIZE);
1761d32d9864SMats Randgaard
1762d32d9864SMats Randgaard return 0;
1763d32d9864SMats Randgaard }
1764d32d9864SMats Randgaard
tc358743_s_edid(struct v4l2_subdev * sd,struct v4l2_subdev_edid * edid)1765d32d9864SMats Randgaard static int tc358743_s_edid(struct v4l2_subdev *sd,
1766d32d9864SMats Randgaard struct v4l2_subdev_edid *edid)
1767d32d9864SMats Randgaard {
1768d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
1769d32d9864SMats Randgaard u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
1770a0ec8d1dSHans Verkuil u16 pa;
1771a0ec8d1dSHans Verkuil int err;
1772fcae73faSMats Randgaard int i;
1773d32d9864SMats Randgaard
1774d32d9864SMats Randgaard v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
1775d32d9864SMats Randgaard __func__, edid->pad, edid->start_block, edid->blocks);
1776d32d9864SMats Randgaard
177772777724SHans Verkuil memset(edid->reserved, 0, sizeof(edid->reserved));
177872777724SHans Verkuil
1779d32d9864SMats Randgaard if (edid->pad != 0)
1780d32d9864SMats Randgaard return -EINVAL;
1781d32d9864SMats Randgaard
1782d32d9864SMats Randgaard if (edid->start_block != 0)
1783d32d9864SMats Randgaard return -EINVAL;
1784d32d9864SMats Randgaard
1785d32d9864SMats Randgaard if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
1786d32d9864SMats Randgaard edid->blocks = EDID_NUM_BLOCKS_MAX;
1787d32d9864SMats Randgaard return -E2BIG;
1788d32d9864SMats Randgaard }
1789a0ec8d1dSHans Verkuil pa = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
17909cfd2753SHans Verkuil err = v4l2_phys_addr_validate(pa, &pa, NULL);
1791a0ec8d1dSHans Verkuil if (err)
1792a0ec8d1dSHans Verkuil return err;
1793a0ec8d1dSHans Verkuil
1794a0ec8d1dSHans Verkuil cec_phys_addr_invalidate(state->cec_adap);
1795d32d9864SMats Randgaard
1796d32d9864SMats Randgaard tc358743_disable_edid(sd);
1797d32d9864SMats Randgaard
1798d32d9864SMats Randgaard i2c_wr8(sd, EDID_LEN1, edid_len & 0xff);
1799d32d9864SMats Randgaard i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
1800d32d9864SMats Randgaard
1801d32d9864SMats Randgaard if (edid->blocks == 0) {
1802d32d9864SMats Randgaard state->edid_blocks_written = 0;
1803d32d9864SMats Randgaard return 0;
1804d32d9864SMats Randgaard }
1805d32d9864SMats Randgaard
1806fcae73faSMats Randgaard for (i = 0; i < edid_len; i += EDID_BLOCK_SIZE)
1807fcae73faSMats Randgaard i2c_wr(sd, EDID_RAM + i, edid->edid + i, EDID_BLOCK_SIZE);
1808d32d9864SMats Randgaard
1809d32d9864SMats Randgaard state->edid_blocks_written = edid->blocks;
1810d32d9864SMats Randgaard
1811a0ec8d1dSHans Verkuil cec_s_phys_addr(state->cec_adap, pa, false);
1812a0ec8d1dSHans Verkuil
1813d32d9864SMats Randgaard if (tx_5v_power_present(sd))
1814d32d9864SMats Randgaard tc358743_enable_edid(sd);
1815d32d9864SMats Randgaard
1816d32d9864SMats Randgaard return 0;
1817d32d9864SMats Randgaard }
1818d32d9864SMats Randgaard
1819d32d9864SMats Randgaard /* -------------------------------------------------------------------------- */
1820d32d9864SMats Randgaard
1821d32d9864SMats Randgaard static const struct v4l2_subdev_core_ops tc358743_core_ops = {
1822d32d9864SMats Randgaard .log_status = tc358743_log_status,
1823d32d9864SMats Randgaard #ifdef CONFIG_VIDEO_ADV_DEBUG
1824d32d9864SMats Randgaard .g_register = tc358743_g_register,
1825d32d9864SMats Randgaard .s_register = tc358743_s_register,
1826d32d9864SMats Randgaard #endif
1827d32d9864SMats Randgaard .interrupt_service_routine = tc358743_isr,
18281140f919SPhilipp Zabel .subscribe_event = tc358743_subscribe_event,
18291140f919SPhilipp Zabel .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1830d32d9864SMats Randgaard };
1831d32d9864SMats Randgaard
1832d32d9864SMats Randgaard static const struct v4l2_subdev_video_ops tc358743_video_ops = {
1833d32d9864SMats Randgaard .g_input_status = tc358743_g_input_status,
1834d32d9864SMats Randgaard .s_stream = tc358743_s_stream,
1835d32d9864SMats Randgaard };
1836d32d9864SMats Randgaard
1837d32d9864SMats Randgaard static const struct v4l2_subdev_pad_ops tc358743_pad_ops = {
18383cb0fe6fSDave Stevenson .enum_mbus_code = tc358743_enum_mbus_code,
1839d32d9864SMats Randgaard .set_fmt = tc358743_set_fmt,
1840d32d9864SMats Randgaard .get_fmt = tc358743_get_fmt,
1841d32d9864SMats Randgaard .get_edid = tc358743_g_edid,
1842d32d9864SMats Randgaard .set_edid = tc358743_s_edid,
184338b317caSPaweł Anikiel .s_dv_timings = tc358743_s_dv_timings,
184438b317caSPaweł Anikiel .g_dv_timings = tc358743_g_dv_timings,
184538b317caSPaweł Anikiel .query_dv_timings = tc358743_query_dv_timings,
1846d32d9864SMats Randgaard .enum_dv_timings = tc358743_enum_dv_timings,
1847d32d9864SMats Randgaard .dv_timings_cap = tc358743_dv_timings_cap,
18480c3da525SJacopo Mondi .get_mbus_config = tc358743_get_mbus_config,
1849d32d9864SMats Randgaard };
1850d32d9864SMats Randgaard
1851d32d9864SMats Randgaard static const struct v4l2_subdev_ops tc358743_ops = {
1852d32d9864SMats Randgaard .core = &tc358743_core_ops,
1853d32d9864SMats Randgaard .video = &tc358743_video_ops,
1854d32d9864SMats Randgaard .pad = &tc358743_pad_ops,
1855d32d9864SMats Randgaard };
1856d32d9864SMats Randgaard
1857d32d9864SMats Randgaard /* --------------- CUSTOM CTRLS --------------- */
1858d32d9864SMats Randgaard
1859d32d9864SMats Randgaard static const struct v4l2_ctrl_config tc358743_ctrl_audio_sampling_rate = {
1860d32d9864SMats Randgaard .id = TC358743_CID_AUDIO_SAMPLING_RATE,
1861d32d9864SMats Randgaard .name = "Audio sampling rate",
1862d32d9864SMats Randgaard .type = V4L2_CTRL_TYPE_INTEGER,
1863d32d9864SMats Randgaard .min = 0,
1864d32d9864SMats Randgaard .max = 768000,
1865d32d9864SMats Randgaard .step = 1,
1866d32d9864SMats Randgaard .def = 0,
1867d32d9864SMats Randgaard .flags = V4L2_CTRL_FLAG_READ_ONLY,
1868d32d9864SMats Randgaard };
1869d32d9864SMats Randgaard
1870d32d9864SMats Randgaard static const struct v4l2_ctrl_config tc358743_ctrl_audio_present = {
1871d32d9864SMats Randgaard .id = TC358743_CID_AUDIO_PRESENT,
1872d32d9864SMats Randgaard .name = "Audio present",
1873d32d9864SMats Randgaard .type = V4L2_CTRL_TYPE_BOOLEAN,
1874d32d9864SMats Randgaard .min = 0,
1875d32d9864SMats Randgaard .max = 1,
1876d32d9864SMats Randgaard .step = 1,
1877d32d9864SMats Randgaard .def = 0,
1878d32d9864SMats Randgaard .flags = V4L2_CTRL_FLAG_READ_ONLY,
1879d32d9864SMats Randgaard };
1880d32d9864SMats Randgaard
1881d32d9864SMats Randgaard /* --------------- PROBE / REMOVE --------------- */
1882d32d9864SMats Randgaard
188325614824SPhilipp Zabel #ifdef CONFIG_OF
tc358743_gpio_reset(struct tc358743_state * state)188425614824SPhilipp Zabel static void tc358743_gpio_reset(struct tc358743_state *state)
188525614824SPhilipp Zabel {
188625614824SPhilipp Zabel usleep_range(5000, 10000);
188725614824SPhilipp Zabel gpiod_set_value(state->reset_gpio, 1);
188825614824SPhilipp Zabel usleep_range(1000, 2000);
188925614824SPhilipp Zabel gpiod_set_value(state->reset_gpio, 0);
189025614824SPhilipp Zabel msleep(20);
189125614824SPhilipp Zabel }
189225614824SPhilipp Zabel
tc358743_probe_of(struct tc358743_state * state)189325614824SPhilipp Zabel static int tc358743_probe_of(struct tc358743_state *state)
189425614824SPhilipp Zabel {
189525614824SPhilipp Zabel struct device *dev = &state->i2c_client->dev;
18966970d37cSSakari Ailus struct v4l2_fwnode_endpoint endpoint = { .bus_type = 0 };
189725614824SPhilipp Zabel struct device_node *ep;
189825614824SPhilipp Zabel struct clk *refclk;
189925614824SPhilipp Zabel u32 bps_pr_lane;
19006970d37cSSakari Ailus int ret;
190125614824SPhilipp Zabel
190225614824SPhilipp Zabel refclk = devm_clk_get(dev, "refclk");
19036cb7d1b3SYang Yingliang if (IS_ERR(refclk))
19046cb7d1b3SYang Yingliang return dev_err_probe(dev, PTR_ERR(refclk),
19056cb7d1b3SYang Yingliang "failed to get refclk\n");
190625614824SPhilipp Zabel
1907c188df3dSKuninori Morimoto ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
190825614824SPhilipp Zabel if (!ep) {
190925614824SPhilipp Zabel dev_err(dev, "missing endpoint node\n");
191025614824SPhilipp Zabel return -EINVAL;
191125614824SPhilipp Zabel }
191225614824SPhilipp Zabel
19136970d37cSSakari Ailus ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(ep), &endpoint);
19146970d37cSSakari Ailus if (ret) {
191525614824SPhilipp Zabel dev_err(dev, "failed to parse endpoint\n");
191664bac691SAlexey Khoroshilov goto put_node;
191725614824SPhilipp Zabel }
191825614824SPhilipp Zabel
19196970d37cSSakari Ailus if (endpoint.bus_type != V4L2_MBUS_CSI2_DPHY ||
19206970d37cSSakari Ailus endpoint.bus.mipi_csi2.num_data_lanes == 0 ||
19216970d37cSSakari Ailus endpoint.nr_of_link_frequencies == 0) {
192225614824SPhilipp Zabel dev_err(dev, "missing CSI-2 properties in endpoint\n");
19236970d37cSSakari Ailus ret = -EINVAL;
192425614824SPhilipp Zabel goto free_endpoint;
192525614824SPhilipp Zabel }
192625614824SPhilipp Zabel
19276970d37cSSakari Ailus if (endpoint.bus.mipi_csi2.num_data_lanes > 4) {
19289d39e46bSPhilipp Zabel dev_err(dev, "invalid number of lanes\n");
19296970d37cSSakari Ailus ret = -EINVAL;
19309d39e46bSPhilipp Zabel goto free_endpoint;
19319d39e46bSPhilipp Zabel }
19329d39e46bSPhilipp Zabel
19336970d37cSSakari Ailus state->bus = endpoint.bus.mipi_csi2;
193425614824SPhilipp Zabel
193543b288baSArvind Yadav ret = clk_prepare_enable(refclk);
193643b288baSArvind Yadav if (ret) {
193743b288baSArvind Yadav dev_err(dev, "Failed! to enable clock\n");
193843b288baSArvind Yadav goto free_endpoint;
193943b288baSArvind Yadav }
194025614824SPhilipp Zabel
194125614824SPhilipp Zabel state->pdata.refclk_hz = clk_get_rate(refclk);
194225614824SPhilipp Zabel state->pdata.ddc5v_delay = DDC5V_DELAY_100_MS;
194325614824SPhilipp Zabel state->pdata.enable_hdcp = false;
194425614824SPhilipp Zabel /* A FIFO level of 16 should be enough for 2-lane 720p60 at 594 MHz. */
194525614824SPhilipp Zabel state->pdata.fifo_level = 16;
194625614824SPhilipp Zabel /*
194725614824SPhilipp Zabel * The PLL input clock is obtained by dividing refclk by pll_prd.
194825614824SPhilipp Zabel * It must be between 6 MHz and 40 MHz, lower frequency is better.
194925614824SPhilipp Zabel */
195025614824SPhilipp Zabel switch (state->pdata.refclk_hz) {
195125614824SPhilipp Zabel case 26000000:
195225614824SPhilipp Zabel case 27000000:
195325614824SPhilipp Zabel case 42000000:
195425614824SPhilipp Zabel state->pdata.pll_prd = state->pdata.refclk_hz / 6000000;
195525614824SPhilipp Zabel break;
195625614824SPhilipp Zabel default:
195725614824SPhilipp Zabel dev_err(dev, "unsupported refclk rate: %u Hz\n",
195825614824SPhilipp Zabel state->pdata.refclk_hz);
195925614824SPhilipp Zabel goto disable_clk;
196025614824SPhilipp Zabel }
196125614824SPhilipp Zabel
196225614824SPhilipp Zabel /*
196325614824SPhilipp Zabel * The CSI bps per lane must be between 62.5 Mbps and 1 Gbps.
196425614824SPhilipp Zabel * The default is 594 Mbps for 4-lane 1080p60 or 2-lane 720p60.
196525614824SPhilipp Zabel */
19666970d37cSSakari Ailus bps_pr_lane = 2 * endpoint.link_frequencies[0];
196725614824SPhilipp Zabel if (bps_pr_lane < 62500000U || bps_pr_lane > 1000000000U) {
196825614824SPhilipp Zabel dev_err(dev, "unsupported bps per lane: %u bps\n", bps_pr_lane);
1969a6b1e709SZhen Lei ret = -EINVAL;
197025614824SPhilipp Zabel goto disable_clk;
197125614824SPhilipp Zabel }
197225614824SPhilipp Zabel
197325614824SPhilipp Zabel /* The CSI speed per lane is refclk / pll_prd * pll_fbd */
197425614824SPhilipp Zabel state->pdata.pll_fbd = bps_pr_lane /
197525614824SPhilipp Zabel state->pdata.refclk_hz * state->pdata.pll_prd;
197625614824SPhilipp Zabel
197725614824SPhilipp Zabel /*
197825614824SPhilipp Zabel * FIXME: These timings are from REF_02 for 594 Mbps per lane (297 MHz
197925614824SPhilipp Zabel * link frequency). In principle it should be possible to calculate
198025614824SPhilipp Zabel * them based on link frequency and resolution.
198125614824SPhilipp Zabel */
198225614824SPhilipp Zabel if (bps_pr_lane != 594000000U)
198325614824SPhilipp Zabel dev_warn(dev, "untested bps per lane: %u bps\n", bps_pr_lane);
198425614824SPhilipp Zabel state->pdata.lineinitcnt = 0xe80;
198525614824SPhilipp Zabel state->pdata.lptxtimecnt = 0x003;
198625614824SPhilipp Zabel /* tclk-preparecnt: 3, tclk-zerocnt: 20 */
198725614824SPhilipp Zabel state->pdata.tclk_headercnt = 0x1403;
198825614824SPhilipp Zabel state->pdata.tclk_trailcnt = 0x00;
198925614824SPhilipp Zabel /* ths-preparecnt: 3, ths-zerocnt: 1 */
199025614824SPhilipp Zabel state->pdata.ths_headercnt = 0x0103;
199125614824SPhilipp Zabel state->pdata.twakeup = 0x4882;
199225614824SPhilipp Zabel state->pdata.tclk_postcnt = 0x008;
199325614824SPhilipp Zabel state->pdata.ths_trailcnt = 0x2;
199425614824SPhilipp Zabel state->pdata.hstxvregcnt = 0;
199525614824SPhilipp Zabel
19961e137d92SUwe Kleine-König state->reset_gpio = devm_gpiod_get_optional(dev, "reset",
19971e137d92SUwe Kleine-König GPIOD_OUT_LOW);
199825614824SPhilipp Zabel if (IS_ERR(state->reset_gpio)) {
199925614824SPhilipp Zabel dev_err(dev, "failed to get reset gpio\n");
200025614824SPhilipp Zabel ret = PTR_ERR(state->reset_gpio);
200125614824SPhilipp Zabel goto disable_clk;
200225614824SPhilipp Zabel }
200325614824SPhilipp Zabel
20041e137d92SUwe Kleine-König if (state->reset_gpio)
200525614824SPhilipp Zabel tc358743_gpio_reset(state);
200625614824SPhilipp Zabel
200725614824SPhilipp Zabel ret = 0;
200825614824SPhilipp Zabel goto free_endpoint;
200925614824SPhilipp Zabel
201025614824SPhilipp Zabel disable_clk:
201125614824SPhilipp Zabel clk_disable_unprepare(refclk);
201225614824SPhilipp Zabel free_endpoint:
20136970d37cSSakari Ailus v4l2_fwnode_endpoint_free(&endpoint);
201464bac691SAlexey Khoroshilov put_node:
201564bac691SAlexey Khoroshilov of_node_put(ep);
201625614824SPhilipp Zabel return ret;
201725614824SPhilipp Zabel }
201825614824SPhilipp Zabel #else
tc358743_probe_of(struct tc358743_state * state)201925614824SPhilipp Zabel static inline int tc358743_probe_of(struct tc358743_state *state)
202025614824SPhilipp Zabel {
202125614824SPhilipp Zabel return -ENODEV;
202225614824SPhilipp Zabel }
202325614824SPhilipp Zabel #endif
202425614824SPhilipp Zabel
tc358743_probe(struct i2c_client * client)2025e6714993SKieran Bingham static int tc358743_probe(struct i2c_client *client)
2026d32d9864SMats Randgaard {
2027d32d9864SMats Randgaard static struct v4l2_dv_timings default_timing =
2028d32d9864SMats Randgaard V4L2_DV_BT_CEA_640X480P59_94;
2029d32d9864SMats Randgaard struct tc358743_state *state;
2030d32d9864SMats Randgaard struct tc358743_platform_data *pdata = client->dev.platform_data;
2031d32d9864SMats Randgaard struct v4l2_subdev *sd;
2032a0ec8d1dSHans Verkuil u16 irq_mask = MASK_HDMI_MSK | MASK_CSI_MSK;
2033d32d9864SMats Randgaard int err;
2034d32d9864SMats Randgaard
2035d32d9864SMats Randgaard if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2036d32d9864SMats Randgaard return -EIO;
2037d32d9864SMats Randgaard v4l_dbg(1, debug, client, "chip found @ 0x%x (%s)\n",
2038d32d9864SMats Randgaard client->addr << 1, client->adapter->name);
2039d32d9864SMats Randgaard
2040d32d9864SMats Randgaard state = devm_kzalloc(&client->dev, sizeof(struct tc358743_state),
2041d32d9864SMats Randgaard GFP_KERNEL);
2042d32d9864SMats Randgaard if (!state)
2043d32d9864SMats Randgaard return -ENOMEM;
2044d32d9864SMats Randgaard
2045d32d9864SMats Randgaard state->i2c_client = client;
204625614824SPhilipp Zabel
204725614824SPhilipp Zabel /* platform data */
204825614824SPhilipp Zabel if (pdata) {
204925614824SPhilipp Zabel state->pdata = *pdata;
2050b9f7caa7SLaurent Pinchart state->bus.flags = 0;
205125614824SPhilipp Zabel } else {
205225614824SPhilipp Zabel err = tc358743_probe_of(state);
205325614824SPhilipp Zabel if (err == -ENODEV)
205425614824SPhilipp Zabel v4l_err(client, "No platform data!\n");
205525614824SPhilipp Zabel if (err)
205625614824SPhilipp Zabel return err;
205725614824SPhilipp Zabel }
205825614824SPhilipp Zabel
2059d32d9864SMats Randgaard sd = &state->sd;
2060d32d9864SMats Randgaard v4l2_i2c_subdev_init(sd, client, &tc358743_ops);
20618ec23da7SPhilipp Zabel sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2062d32d9864SMats Randgaard
2063d32d9864SMats Randgaard /* i2c access */
2064d32d9864SMats Randgaard if ((i2c_rd16(sd, CHIPID) & MASK_CHIPID) != 0) {
2065d32d9864SMats Randgaard v4l2_info(sd, "not a TC358743 on address 0x%x\n",
2066d32d9864SMats Randgaard client->addr << 1);
2067d32d9864SMats Randgaard return -ENODEV;
2068d32d9864SMats Randgaard }
2069d32d9864SMats Randgaard
2070d32d9864SMats Randgaard /* control handlers */
2071d32d9864SMats Randgaard v4l2_ctrl_handler_init(&state->hdl, 3);
2072d32d9864SMats Randgaard
2073d32d9864SMats Randgaard state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
2074d32d9864SMats Randgaard V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
2075d32d9864SMats Randgaard
2076d32d9864SMats Randgaard /* custom controls */
2077d32d9864SMats Randgaard state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
2078d32d9864SMats Randgaard &tc358743_ctrl_audio_sampling_rate, NULL);
2079d32d9864SMats Randgaard
2080d32d9864SMats Randgaard state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
2081d32d9864SMats Randgaard &tc358743_ctrl_audio_present, NULL);
2082d32d9864SMats Randgaard
2083d32d9864SMats Randgaard sd->ctrl_handler = &state->hdl;
2084d32d9864SMats Randgaard if (state->hdl.error) {
2085d32d9864SMats Randgaard err = state->hdl.error;
2086d32d9864SMats Randgaard goto err_hdl;
2087d32d9864SMats Randgaard }
2088d32d9864SMats Randgaard
2089d32d9864SMats Randgaard if (tc358743_update_controls(sd)) {
2090d32d9864SMats Randgaard err = -ENODEV;
2091d32d9864SMats Randgaard goto err_hdl;
2092d32d9864SMats Randgaard }
2093d32d9864SMats Randgaard
20944c5211a1SPhilipp Zabel state->pad.flags = MEDIA_PAD_FL_SOURCE;
2095f7480ad0SPhilipp Zabel sd->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
2096ab22e77cSMauro Carvalho Chehab err = media_entity_pads_init(&sd->entity, 1, &state->pad);
20974c5211a1SPhilipp Zabel if (err < 0)
20984c5211a1SPhilipp Zabel goto err_hdl;
20994c5211a1SPhilipp Zabel
21002da2391cSDave Stevenson state->mbus_fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
21012da2391cSDave Stevenson
21024c5211a1SPhilipp Zabel sd->dev = &client->dev;
21034c5211a1SPhilipp Zabel
2104d32d9864SMats Randgaard mutex_init(&state->confctl_mutex);
2105d32d9864SMats Randgaard
2106d32d9864SMats Randgaard INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
2107d32d9864SMats Randgaard tc358743_delayed_work_enable_hotplug);
2108d32d9864SMats Randgaard
2109a0ec8d1dSHans Verkuil #ifdef CONFIG_VIDEO_TC358743_CEC
2110a0ec8d1dSHans Verkuil state->cec_adap = cec_allocate_adapter(&tc358743_cec_adap_ops,
2111a0ec8d1dSHans Verkuil state, dev_name(&client->dev),
2112a0ec8d1dSHans Verkuil CEC_CAP_DEFAULTS | CEC_CAP_MONITOR_ALL, CEC_MAX_LOG_ADDRS);
2113a0ec8d1dSHans Verkuil if (IS_ERR(state->cec_adap)) {
21147996e5c4SDan Carpenter err = PTR_ERR(state->cec_adap);
2115a0ec8d1dSHans Verkuil goto err_hdl;
2116a0ec8d1dSHans Verkuil }
2117a0ec8d1dSHans Verkuil irq_mask |= MASK_CEC_RMSK | MASK_CEC_TMSK;
2118a0ec8d1dSHans Verkuil #endif
2119a0ec8d1dSHans Verkuil
2120d32d9864SMats Randgaard tc358743_initial_setup(sd);
2121d32d9864SMats Randgaard
212238b317caSPaweł Anikiel tc358743_s_dv_timings(sd, 0, &default_timing);
2123d32d9864SMats Randgaard
2124d32d9864SMats Randgaard tc358743_set_csi_color_space(sd);
2125d32d9864SMats Randgaard
2126d32d9864SMats Randgaard tc358743_init_interrupts(sd);
2127d747b806SPhilipp Zabel
2128d747b806SPhilipp Zabel if (state->i2c_client->irq) {
2129d747b806SPhilipp Zabel err = devm_request_threaded_irq(&client->dev,
2130d747b806SPhilipp Zabel state->i2c_client->irq,
2131d747b806SPhilipp Zabel NULL, tc358743_irq_handler,
2132d747b806SPhilipp Zabel IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
2133d747b806SPhilipp Zabel "tc358743", state);
2134d747b806SPhilipp Zabel if (err)
2135d747b806SPhilipp Zabel goto err_work_queues;
21364e66a52aSDave Stevenson } else {
21374e66a52aSDave Stevenson INIT_WORK(&state->work_i2c_poll,
21384e66a52aSDave Stevenson tc358743_work_i2c_poll);
2139a88cd60fSKees Cook timer_setup(&state->timer, tc358743_irq_poll_timer, 0);
21404e66a52aSDave Stevenson state->timer.expires = jiffies +
21414e66a52aSDave Stevenson msecs_to_jiffies(POLL_INTERVAL_MS);
21424e66a52aSDave Stevenson add_timer(&state->timer);
2143d747b806SPhilipp Zabel }
2144d747b806SPhilipp Zabel
2145a0ec8d1dSHans Verkuil err = cec_register_adapter(state->cec_adap, &client->dev);
2146a0ec8d1dSHans Verkuil if (err < 0) {
2147a0ec8d1dSHans Verkuil pr_err("%s: failed to register the cec device\n", __func__);
2148a0ec8d1dSHans Verkuil cec_delete_adapter(state->cec_adap);
2149a0ec8d1dSHans Verkuil state->cec_adap = NULL;
2150a0ec8d1dSHans Verkuil goto err_work_queues;
2151a0ec8d1dSHans Verkuil }
2152a0ec8d1dSHans Verkuil
2153d32d9864SMats Randgaard tc358743_enable_interrupts(sd, tx_5v_power_present(sd));
2154a0ec8d1dSHans Verkuil i2c_wr16(sd, INTMASK, ~irq_mask);
2155d32d9864SMats Randgaard
2156d32d9864SMats Randgaard err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
2157d32d9864SMats Randgaard if (err)
2158d32d9864SMats Randgaard goto err_work_queues;
2159d32d9864SMats Randgaard
216024b41af1SAlexander Stein err = v4l2_async_register_subdev(sd);
216124b41af1SAlexander Stein if (err < 0)
216224b41af1SAlexander Stein goto err_work_queues;
216324b41af1SAlexander Stein
2164d32d9864SMats Randgaard v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
2165d32d9864SMats Randgaard client->addr << 1, client->adapter->name);
2166d32d9864SMats Randgaard
2167d32d9864SMats Randgaard return 0;
2168d32d9864SMats Randgaard
2169d32d9864SMats Randgaard err_work_queues:
2170a0ec8d1dSHans Verkuil cec_unregister_adapter(state->cec_adap);
21714e66a52aSDave Stevenson if (!state->i2c_client->irq)
21724e66a52aSDave Stevenson flush_work(&state->work_i2c_poll);
2173d32d9864SMats Randgaard cancel_delayed_work(&state->delayed_work_enable_hotplug);
2174d32d9864SMats Randgaard mutex_destroy(&state->confctl_mutex);
2175d32d9864SMats Randgaard err_hdl:
21764c5211a1SPhilipp Zabel media_entity_cleanup(&sd->entity);
2177d32d9864SMats Randgaard v4l2_ctrl_handler_free(&state->hdl);
2178d32d9864SMats Randgaard return err;
2179d32d9864SMats Randgaard }
2180d32d9864SMats Randgaard
tc358743_remove(struct i2c_client * client)2181ed5c2f5fSUwe Kleine-König static void tc358743_remove(struct i2c_client *client)
2182d32d9864SMats Randgaard {
2183d32d9864SMats Randgaard struct v4l2_subdev *sd = i2c_get_clientdata(client);
2184d32d9864SMats Randgaard struct tc358743_state *state = to_state(sd);
2185d32d9864SMats Randgaard
21864e66a52aSDave Stevenson if (!state->i2c_client->irq) {
21874e66a52aSDave Stevenson del_timer_sync(&state->timer);
21884e66a52aSDave Stevenson flush_work(&state->work_i2c_poll);
21894e66a52aSDave Stevenson }
21906107a4fdSYang Yingliang cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
2191a0ec8d1dSHans Verkuil cec_unregister_adapter(state->cec_adap);
21924c5211a1SPhilipp Zabel v4l2_async_unregister_subdev(sd);
2193d32d9864SMats Randgaard v4l2_device_unregister_subdev(sd);
2194d32d9864SMats Randgaard mutex_destroy(&state->confctl_mutex);
21954c5211a1SPhilipp Zabel media_entity_cleanup(&sd->entity);
2196d32d9864SMats Randgaard v4l2_ctrl_handler_free(&state->hdl);
2197d32d9864SMats Randgaard }
2198d32d9864SMats Randgaard
21992f8dfed8SArvind Yadav static const struct i2c_device_id tc358743_id[] = {
2200*cc4cbd4bSUwe Kleine-König { "tc358743" },
2201d32d9864SMats Randgaard {}
2202d32d9864SMats Randgaard };
2203d32d9864SMats Randgaard
2204d32d9864SMats Randgaard MODULE_DEVICE_TABLE(i2c, tc358743_id);
2205d32d9864SMats Randgaard
2206c0746c1aSJavier Martinez Canillas #if IS_ENABLED(CONFIG_OF)
2207c0746c1aSJavier Martinez Canillas static const struct of_device_id tc358743_of_match[] = {
2208c0746c1aSJavier Martinez Canillas { .compatible = "toshiba,tc358743" },
2209c0746c1aSJavier Martinez Canillas {},
2210c0746c1aSJavier Martinez Canillas };
2211c0746c1aSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, tc358743_of_match);
2212c0746c1aSJavier Martinez Canillas #endif
2213c0746c1aSJavier Martinez Canillas
2214d32d9864SMats Randgaard static struct i2c_driver tc358743_driver = {
2215d32d9864SMats Randgaard .driver = {
2216d32d9864SMats Randgaard .name = "tc358743",
2217c0746c1aSJavier Martinez Canillas .of_match_table = of_match_ptr(tc358743_of_match),
2218d32d9864SMats Randgaard },
2219aaeb31c0SUwe Kleine-König .probe = tc358743_probe,
2220d32d9864SMats Randgaard .remove = tc358743_remove,
2221d32d9864SMats Randgaard .id_table = tc358743_id,
2222d32d9864SMats Randgaard };
2223d32d9864SMats Randgaard
2224d32d9864SMats Randgaard module_i2c_driver(tc358743_driver);
2225