blob: 934f6e17e06d17be63a23e924f5fc425bfd3b4e6 [file] [log] [blame]
Mats Randgaardd32d9862015-07-09 05:45:47 -03001/*
2 * tc358743 - Toshiba HDMI to CSI-2 bridge
3 *
4 * Copyright 2015 Cisco Systems, Inc. and/or its affiliates. All rights
5 * reserved.
6 *
7 * This program is free software; you may redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
12 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
13 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
15 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
16 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 * SOFTWARE.
19 *
20 */
21
22/*
23 * References (c = chapter, p = page):
24 * REF_01 - Toshiba, TC358743XBG (H2C), Functional Specification, Rev 0.60
25 * REF_02 - Toshiba, TC358743XBG_HDMI-CSI_Tv11p_nm.xls
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/slab.h>
31#include <linux/i2c.h>
Philipp Zabel25614822015-07-17 11:02:55 -030032#include <linux/clk.h>
Mats Randgaardd32d9862015-07-09 05:45:47 -030033#include <linux/delay.h>
Philipp Zabel25614822015-07-17 11:02:55 -030034#include <linux/gpio/consumer.h>
Philipp Zabeld747b802015-07-17 11:02:56 -030035#include <linux/interrupt.h>
Mats Randgaardd32d9862015-07-09 05:45:47 -030036#include <linux/videodev2.h>
37#include <linux/workqueue.h>
38#include <linux/v4l2-dv-timings.h>
39#include <linux/hdmi.h>
40#include <media/v4l2-dv-timings.h>
41#include <media/v4l2-device.h>
42#include <media/v4l2-ctrls.h>
Philipp Zabel25614822015-07-17 11:02:55 -030043#include <media/v4l2-of.h>
Mats Randgaardd32d9862015-07-09 05:45:47 -030044#include <media/tc358743.h>
45
46#include "tc358743_regs.h"
47
48static int debug;
49module_param(debug, int, 0644);
50MODULE_PARM_DESC(debug, "debug level (0-3)");
51
52MODULE_DESCRIPTION("Toshiba TC358743 HDMI to CSI-2 bridge driver");
53MODULE_AUTHOR("Ramakrishnan Muthukrishnan <ram@rkrishnan.org>");
54MODULE_AUTHOR("Mikhail Khelik <mkhelik@cisco.com>");
55MODULE_AUTHOR("Mats Randgaard <matrandg@cisco.com>");
56MODULE_LICENSE("GPL");
57
58#define EDID_NUM_BLOCKS_MAX 8
59#define EDID_BLOCK_SIZE 128
60
61static const struct v4l2_dv_timings_cap tc358743_timings_cap = {
62 .type = V4L2_DV_BT_656_1120,
63 /* keep this initialization for compatibility with GCC < 4.4.6 */
64 .reserved = { 0 },
65 /* Pixel clock from REF_01 p. 20. Min/max height/width are unknown */
66 V4L2_INIT_BT_TIMINGS(1, 10000, 1, 10000, 0, 165000000,
67 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
68 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
69 V4L2_DV_BT_CAP_PROGRESSIVE |
70 V4L2_DV_BT_CAP_REDUCED_BLANKING |
71 V4L2_DV_BT_CAP_CUSTOM)
72};
73
74struct tc358743_state {
75 struct tc358743_platform_data pdata;
Philipp Zabel25614822015-07-17 11:02:55 -030076 struct v4l2_of_bus_mipi_csi2 bus;
Mats Randgaardd32d9862015-07-09 05:45:47 -030077 struct v4l2_subdev sd;
78 struct media_pad pad;
79 struct v4l2_ctrl_handler hdl;
80 struct i2c_client *i2c_client;
81 /* CONFCTL is modified in ops and tc358743_hdmi_sys_int_handler */
82 struct mutex confctl_mutex;
83
84 /* controls */
85 struct v4l2_ctrl *detect_tx_5v_ctrl;
86 struct v4l2_ctrl *audio_sampling_rate_ctrl;
87 struct v4l2_ctrl *audio_present_ctrl;
88
89 /* work queues */
90 struct workqueue_struct *work_queues;
91 struct delayed_work delayed_work_enable_hotplug;
92
93 /* edid */
94 u8 edid_blocks_written;
95
96 struct v4l2_dv_timings timings;
97 u32 mbus_fmt_code;
Philipp Zabel25614822015-07-17 11:02:55 -030098
99 struct gpio_desc *reset_gpio;
Mats Randgaardd32d9862015-07-09 05:45:47 -0300100};
101
102static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
103 bool cable_connected);
104static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd);
105
106static inline struct tc358743_state *to_state(struct v4l2_subdev *sd)
107{
108 return container_of(sd, struct tc358743_state, sd);
109}
110
111/* --------------- I2C --------------- */
112
113static void i2c_rd(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
114{
115 struct tc358743_state *state = to_state(sd);
116 struct i2c_client *client = state->i2c_client;
117 int err;
118 u8 buf[2] = { reg >> 8, reg & 0xff };
119 struct i2c_msg msgs[] = {
120 {
121 .addr = client->addr,
122 .flags = 0,
123 .len = 2,
124 .buf = buf,
125 },
126 {
127 .addr = client->addr,
128 .flags = I2C_M_RD,
129 .len = n,
130 .buf = values,
131 },
132 };
133
134 err = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
135 if (err != ARRAY_SIZE(msgs)) {
136 v4l2_err(sd, "%s: reading register 0x%x from 0x%x failed\n",
137 __func__, reg, client->addr);
138 }
139}
140
141static void i2c_wr(struct v4l2_subdev *sd, u16 reg, u8 *values, u32 n)
142{
143 struct tc358743_state *state = to_state(sd);
144 struct i2c_client *client = state->i2c_client;
145 int err, i;
146 struct i2c_msg msg;
147 u8 data[2 + n];
148
149 msg.addr = client->addr;
150 msg.buf = data;
151 msg.len = 2 + n;
152 msg.flags = 0;
153
154 data[0] = reg >> 8;
155 data[1] = reg & 0xff;
156
157 for (i = 0; i < n; i++)
158 data[2 + i] = values[i];
159
160 err = i2c_transfer(client->adapter, &msg, 1);
161 if (err != 1) {
162 v4l2_err(sd, "%s: writing register 0x%x from 0x%x failed\n",
163 __func__, reg, client->addr);
164 return;
165 }
166
167 if (debug < 3)
168 return;
169
170 switch (n) {
171 case 1:
172 v4l2_info(sd, "I2C write 0x%04x = 0x%02x",
173 reg, data[2]);
174 break;
175 case 2:
176 v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x",
177 reg, data[3], data[2]);
178 break;
179 case 4:
180 v4l2_info(sd, "I2C write 0x%04x = 0x%02x%02x%02x%02x",
181 reg, data[5], data[4], data[3], data[2]);
182 break;
183 default:
184 v4l2_info(sd, "I2C write %d bytes from address 0x%04x\n",
185 n, reg);
186 }
187}
188
189static u8 i2c_rd8(struct v4l2_subdev *sd, u16 reg)
190{
191 u8 val;
192
193 i2c_rd(sd, reg, &val, 1);
194
195 return val;
196}
197
198static void i2c_wr8(struct v4l2_subdev *sd, u16 reg, u8 val)
199{
200 i2c_wr(sd, reg, &val, 1);
201}
202
203static void i2c_wr8_and_or(struct v4l2_subdev *sd, u16 reg,
204 u8 mask, u8 val)
205{
206 i2c_wr8(sd, reg, (i2c_rd8(sd, reg) & mask) | val);
207}
208
209static u16 i2c_rd16(struct v4l2_subdev *sd, u16 reg)
210{
211 u16 val;
212
213 i2c_rd(sd, reg, (u8 *)&val, 2);
214
215 return val;
216}
217
218static void i2c_wr16(struct v4l2_subdev *sd, u16 reg, u16 val)
219{
220 i2c_wr(sd, reg, (u8 *)&val, 2);
221}
222
223static void i2c_wr16_and_or(struct v4l2_subdev *sd, u16 reg, u16 mask, u16 val)
224{
225 i2c_wr16(sd, reg, (i2c_rd16(sd, reg) & mask) | val);
226}
227
228static u32 i2c_rd32(struct v4l2_subdev *sd, u16 reg)
229{
230 u32 val;
231
232 i2c_rd(sd, reg, (u8 *)&val, 4);
233
234 return val;
235}
236
237static void i2c_wr32(struct v4l2_subdev *sd, u16 reg, u32 val)
238{
239 i2c_wr(sd, reg, (u8 *)&val, 4);
240}
241
242/* --------------- STATUS --------------- */
243
244static inline bool is_hdmi(struct v4l2_subdev *sd)
245{
246 return i2c_rd8(sd, SYS_STATUS) & MASK_S_HDMI;
247}
248
249static inline bool tx_5v_power_present(struct v4l2_subdev *sd)
250{
251 return i2c_rd8(sd, SYS_STATUS) & MASK_S_DDC5V;
252}
253
254static inline bool no_signal(struct v4l2_subdev *sd)
255{
256 return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_TMDS);
257}
258
259static inline bool no_sync(struct v4l2_subdev *sd)
260{
261 return !(i2c_rd8(sd, SYS_STATUS) & MASK_S_SYNC);
262}
263
264static inline bool audio_present(struct v4l2_subdev *sd)
265{
266 return i2c_rd8(sd, AU_STATUS0) & MASK_S_A_SAMPLE;
267}
268
269static int get_audio_sampling_rate(struct v4l2_subdev *sd)
270{
271 static const int code_to_rate[] = {
272 44100, 0, 48000, 32000, 22050, 384000, 24000, 352800,
273 88200, 768000, 96000, 705600, 176400, 0, 192000, 0
274 };
275
276 /* Register FS_SET is not cleared when the cable is disconnected */
277 if (no_signal(sd))
278 return 0;
279
280 return code_to_rate[i2c_rd8(sd, FS_SET) & MASK_FS];
281}
282
283static unsigned tc358743_num_csi_lanes_in_use(struct v4l2_subdev *sd)
284{
285 return ((i2c_rd32(sd, CSI_CONTROL) & MASK_NOL) >> 1) + 1;
286}
287
288/* --------------- TIMINGS --------------- */
289
290static inline unsigned fps(const struct v4l2_bt_timings *t)
291{
292 if (!V4L2_DV_BT_FRAME_HEIGHT(t) || !V4L2_DV_BT_FRAME_WIDTH(t))
293 return 0;
294
295 return DIV_ROUND_CLOSEST((unsigned)t->pixelclock,
296 V4L2_DV_BT_FRAME_HEIGHT(t) * V4L2_DV_BT_FRAME_WIDTH(t));
297}
298
299static int tc358743_get_detected_timings(struct v4l2_subdev *sd,
300 struct v4l2_dv_timings *timings)
301{
302 struct v4l2_bt_timings *bt = &timings->bt;
303 unsigned width, height, frame_width, frame_height, frame_interval, fps;
304
305 memset(timings, 0, sizeof(struct v4l2_dv_timings));
306
307 if (no_signal(sd)) {
308 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
309 return -ENOLINK;
310 }
311 if (no_sync(sd)) {
312 v4l2_dbg(1, debug, sd, "%s: no sync on signal\n", __func__);
313 return -ENOLCK;
314 }
315
316 timings->type = V4L2_DV_BT_656_1120;
317 bt->interlaced = i2c_rd8(sd, VI_STATUS1) & MASK_S_V_INTERLACE ?
318 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
319
320 width = ((i2c_rd8(sd, DE_WIDTH_H_HI) & 0x1f) << 8) +
321 i2c_rd8(sd, DE_WIDTH_H_LO);
322 height = ((i2c_rd8(sd, DE_WIDTH_V_HI) & 0x1f) << 8) +
323 i2c_rd8(sd, DE_WIDTH_V_LO);
324 frame_width = ((i2c_rd8(sd, H_SIZE_HI) & 0x1f) << 8) +
325 i2c_rd8(sd, H_SIZE_LO);
326 frame_height = (((i2c_rd8(sd, V_SIZE_HI) & 0x3f) << 8) +
327 i2c_rd8(sd, V_SIZE_LO)) / 2;
328 /* frame interval in milliseconds * 10
329 * Require SYS_FREQ0 and SYS_FREQ1 are precisely set */
330 frame_interval = ((i2c_rd8(sd, FV_CNT_HI) & 0x3) << 8) +
331 i2c_rd8(sd, FV_CNT_LO);
332 fps = (frame_interval > 0) ?
333 DIV_ROUND_CLOSEST(10000, frame_interval) : 0;
334
335 bt->width = width;
336 bt->height = height;
337 bt->vsync = frame_height - height;
338 bt->hsync = frame_width - width;
339 bt->pixelclock = frame_width * frame_height * fps;
340 if (bt->interlaced == V4L2_DV_INTERLACED) {
341 bt->height *= 2;
342 bt->il_vsync = bt->vsync + 1;
343 bt->pixelclock /= 2;
344 }
345
346 return 0;
347}
348
349/* --------------- HOTPLUG / HDCP / EDID --------------- */
350
351static void tc358743_delayed_work_enable_hotplug(struct work_struct *work)
352{
353 struct delayed_work *dwork = to_delayed_work(work);
354 struct tc358743_state *state = container_of(dwork,
355 struct tc358743_state, delayed_work_enable_hotplug);
356 struct v4l2_subdev *sd = &state->sd;
357
358 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
359
360 i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, MASK_HPD_OUT0);
361}
362
363static void tc358743_set_hdmi_hdcp(struct v4l2_subdev *sd, bool enable)
364{
365 v4l2_dbg(2, debug, sd, "%s: %s\n", __func__, enable ?
366 "enable" : "disable");
367
368 i2c_wr8_and_or(sd, HDCP_REG1,
369 ~(MASK_AUTH_UNAUTH_SEL | MASK_AUTH_UNAUTH),
370 MASK_AUTH_UNAUTH_SEL_16_FRAMES | MASK_AUTH_UNAUTH_AUTO);
371
372 i2c_wr8_and_or(sd, HDCP_REG2, ~MASK_AUTO_P3_RESET,
373 SET_AUTO_P3_RESET_FRAMES(0x0f));
374
375 /* HDCP is disabled by configuring the receiver as HDCP repeater. The
376 * repeater mode require software support to work, so HDCP
377 * authentication will fail.
378 */
379 i2c_wr8_and_or(sd, HDCP_REG3, ~KEY_RD_CMD, enable ? KEY_RD_CMD : 0);
380 i2c_wr8_and_or(sd, HDCP_MODE, ~(MASK_AUTO_CLR | MASK_MODE_RST_TN),
381 enable ? (MASK_AUTO_CLR | MASK_MODE_RST_TN) : 0);
382
383 /* Apple MacBook Pro gen.8 has a bug that makes it freeze every fifth
384 * second when HDCP is disabled, but the MAX_EXCED bit is handled
385 * correctly and HDCP is disabled on the HDMI output.
386 */
387 i2c_wr8_and_or(sd, BSTATUS1, ~MASK_MAX_EXCED,
388 enable ? 0 : MASK_MAX_EXCED);
389 i2c_wr8_and_or(sd, BCAPS, ~(MASK_REPEATER | MASK_READY),
390 enable ? 0 : MASK_REPEATER | MASK_READY);
391}
392
393static void tc358743_disable_edid(struct v4l2_subdev *sd)
394{
395 struct tc358743_state *state = to_state(sd);
396
397 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
398
399 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
400
401 /* DDC access to EDID is also disabled when hotplug is disabled. See
402 * register DDC_CTL */
403 i2c_wr8_and_or(sd, HPD_CTL, ~MASK_HPD_OUT0, 0x0);
404}
405
406static void tc358743_enable_edid(struct v4l2_subdev *sd)
407{
408 struct tc358743_state *state = to_state(sd);
409
410 if (state->edid_blocks_written == 0) {
411 v4l2_dbg(2, debug, sd, "%s: no EDID -> no hotplug\n", __func__);
412 return;
413 }
414
415 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
416
417 /* Enable hotplug after 100 ms. DDC access to EDID is also enabled when
418 * hotplug is enabled. See register DDC_CTL */
419 queue_delayed_work(state->work_queues,
420 &state->delayed_work_enable_hotplug, HZ / 10);
421
422 tc358743_enable_interrupts(sd, true);
423 tc358743_s_ctrl_detect_tx_5v(sd);
424}
425
426static void tc358743_erase_bksv(struct v4l2_subdev *sd)
427{
428 int i;
429
430 for (i = 0; i < 5; i++)
431 i2c_wr8(sd, BKSV + i, 0);
432}
433
434/* --------------- AVI infoframe --------------- */
435
436static void print_avi_infoframe(struct v4l2_subdev *sd)
437{
438 struct i2c_client *client = v4l2_get_subdevdata(sd);
439 struct device *dev = &client->dev;
440 union hdmi_infoframe frame;
441 u8 buffer[HDMI_INFOFRAME_SIZE(AVI)];
442
443 if (!is_hdmi(sd)) {
444 v4l2_info(sd, "DVI-D signal - AVI infoframe not supported\n");
445 return;
446 }
447
448 i2c_rd(sd, PK_AVI_0HEAD, buffer, HDMI_INFOFRAME_SIZE(AVI));
449
450 if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
451 v4l2_err(sd, "%s: unpack of AVI infoframe failed\n", __func__);
452 return;
453 }
454
455 hdmi_infoframe_log(KERN_INFO, dev, &frame);
456}
457
458/* --------------- CTRLS --------------- */
459
460static int tc358743_s_ctrl_detect_tx_5v(struct v4l2_subdev *sd)
461{
462 struct tc358743_state *state = to_state(sd);
463
464 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
465 tx_5v_power_present(sd));
466}
467
468static int tc358743_s_ctrl_audio_sampling_rate(struct v4l2_subdev *sd)
469{
470 struct tc358743_state *state = to_state(sd);
471
472 return v4l2_ctrl_s_ctrl(state->audio_sampling_rate_ctrl,
473 get_audio_sampling_rate(sd));
474}
475
476static int tc358743_s_ctrl_audio_present(struct v4l2_subdev *sd)
477{
478 struct tc358743_state *state = to_state(sd);
479
480 return v4l2_ctrl_s_ctrl(state->audio_present_ctrl,
481 audio_present(sd));
482}
483
484static int tc358743_update_controls(struct v4l2_subdev *sd)
485{
486 int ret = 0;
487
488 ret |= tc358743_s_ctrl_detect_tx_5v(sd);
489 ret |= tc358743_s_ctrl_audio_sampling_rate(sd);
490 ret |= tc358743_s_ctrl_audio_present(sd);
491
492 return ret;
493}
494
495/* --------------- INIT --------------- */
496
497static void tc358743_reset_phy(struct v4l2_subdev *sd)
498{
499 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
500
501 i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, 0);
502 i2c_wr8_and_or(sd, PHY_RST, ~MASK_RESET_CTRL, MASK_RESET_CTRL);
503}
504
505static void tc358743_reset(struct v4l2_subdev *sd, uint16_t mask)
506{
507 u16 sysctl = i2c_rd16(sd, SYSCTL);
508
509 i2c_wr16(sd, SYSCTL, sysctl | mask);
510 i2c_wr16(sd, SYSCTL, sysctl & ~mask);
511}
512
513static inline void tc358743_sleep_mode(struct v4l2_subdev *sd, bool enable)
514{
515 i2c_wr16_and_or(sd, SYSCTL, ~MASK_SLEEP,
516 enable ? MASK_SLEEP : 0);
517}
518
519static inline void enable_stream(struct v4l2_subdev *sd, bool enable)
520{
521 struct tc358743_state *state = to_state(sd);
522
523 v4l2_dbg(3, debug, sd, "%s: %sable\n",
524 __func__, enable ? "en" : "dis");
525
526 if (enable) {
527 /* It is critical for CSI receiver to see lane transition
528 * LP11->HS. Set to non-continuous mode to enable clock lane
529 * LP11 state. */
530 i2c_wr32(sd, TXOPTIONCNTRL, 0);
531 /* Set to continuous mode to trigger LP11->HS transition */
532 i2c_wr32(sd, TXOPTIONCNTRL, MASK_CONTCLKMODE);
533 /* Unmute video */
534 i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE);
535 } else {
536 /* Mute video so that all data lanes go to LSP11 state.
537 * No data is output to CSI Tx block. */
538 i2c_wr8(sd, VI_MUTE, MASK_AUTO_MUTE | MASK_VI_MUTE);
539 }
540
541 mutex_lock(&state->confctl_mutex);
542 i2c_wr16_and_or(sd, CONFCTL, ~(MASK_VBUFEN | MASK_ABUFEN),
543 enable ? (MASK_VBUFEN | MASK_ABUFEN) : 0x0);
544 mutex_unlock(&state->confctl_mutex);
545}
546
547static void tc358743_set_pll(struct v4l2_subdev *sd)
548{
549 struct tc358743_state *state = to_state(sd);
550 struct tc358743_platform_data *pdata = &state->pdata;
551 u16 pllctl0 = i2c_rd16(sd, PLLCTL0);
552 u16 pllctl1 = i2c_rd16(sd, PLLCTL1);
553 u16 pllctl0_new = SET_PLL_PRD(pdata->pll_prd) |
554 SET_PLL_FBD(pdata->pll_fbd);
555 u32 hsck = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
556
557 v4l2_dbg(2, debug, sd, "%s:\n", __func__);
558
559 /* Only rewrite when needed (new value or disabled), since rewriting
560 * triggers another format change event. */
561 if ((pllctl0 != pllctl0_new) || ((pllctl1 & MASK_PLL_EN) == 0)) {
562 u16 pll_frs;
563
564 if (hsck > 500000000)
565 pll_frs = 0x0;
566 else if (hsck > 250000000)
567 pll_frs = 0x1;
568 else if (hsck > 125000000)
569 pll_frs = 0x2;
570 else
571 pll_frs = 0x3;
572
573 v4l2_dbg(1, debug, sd, "%s: updating PLL clock\n", __func__);
574 tc358743_sleep_mode(sd, true);
575 i2c_wr16(sd, PLLCTL0, pllctl0_new);
576 i2c_wr16_and_or(sd, PLLCTL1,
577 ~(MASK_PLL_FRS | MASK_RESETB | MASK_PLL_EN),
578 (SET_PLL_FRS(pll_frs) | MASK_RESETB |
579 MASK_PLL_EN));
580 udelay(10); /* REF_02, Sheet "Source HDMI" */
581 i2c_wr16_and_or(sd, PLLCTL1, ~MASK_CKEN, MASK_CKEN);
582 tc358743_sleep_mode(sd, false);
583 }
584}
585
586static void tc358743_set_ref_clk(struct v4l2_subdev *sd)
587{
588 struct tc358743_state *state = to_state(sd);
589 struct tc358743_platform_data *pdata = &state->pdata;
590 u32 sys_freq;
591 u32 lockdet_ref;
592 u16 fh_min;
593 u16 fh_max;
594
595 BUG_ON(!(pdata->refclk_hz == 26000000 ||
596 pdata->refclk_hz == 27000000 ||
597 pdata->refclk_hz == 42000000));
598
599 sys_freq = pdata->refclk_hz / 10000;
600 i2c_wr8(sd, SYS_FREQ0, sys_freq & 0x00ff);
601 i2c_wr8(sd, SYS_FREQ1, (sys_freq & 0xff00) >> 8);
602
603 i2c_wr8_and_or(sd, PHY_CTL0, ~MASK_PHY_SYSCLK_IND,
604 (pdata->refclk_hz == 42000000) ?
605 MASK_PHY_SYSCLK_IND : 0x0);
606
607 fh_min = pdata->refclk_hz / 100000;
608 i2c_wr8(sd, FH_MIN0, fh_min & 0x00ff);
609 i2c_wr8(sd, FH_MIN1, (fh_min & 0xff00) >> 8);
610
611 fh_max = (fh_min * 66) / 10;
612 i2c_wr8(sd, FH_MAX0, fh_max & 0x00ff);
613 i2c_wr8(sd, FH_MAX1, (fh_max & 0xff00) >> 8);
614
615 lockdet_ref = pdata->refclk_hz / 100;
616 i2c_wr8(sd, LOCKDET_REF0, lockdet_ref & 0x0000ff);
617 i2c_wr8(sd, LOCKDET_REF1, (lockdet_ref & 0x00ff00) >> 8);
618 i2c_wr8(sd, LOCKDET_REF2, (lockdet_ref & 0x0f0000) >> 16);
619
620 i2c_wr8_and_or(sd, NCO_F0_MOD, ~MASK_NCO_F0_MOD,
621 (pdata->refclk_hz == 27000000) ?
622 MASK_NCO_F0_MOD_27MHZ : 0x0);
623}
624
625static void tc358743_set_csi_color_space(struct v4l2_subdev *sd)
626{
627 struct tc358743_state *state = to_state(sd);
628
629 switch (state->mbus_fmt_code) {
630 case MEDIA_BUS_FMT_UYVY8_1X16:
631 v4l2_dbg(2, debug, sd, "%s: YCbCr 422 16-bit\n", __func__);
632 i2c_wr8_and_or(sd, VOUT_SET2,
633 ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
634 MASK_SEL422 | MASK_VOUT_422FIL_100);
635 i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
636 MASK_VOUT_COLOR_601_YCBCR_LIMITED);
637 mutex_lock(&state->confctl_mutex);
638 i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT,
639 MASK_YCBCRFMT_422_8_BIT);
640 mutex_unlock(&state->confctl_mutex);
641 break;
642 case MEDIA_BUS_FMT_RGB888_1X24:
643 v4l2_dbg(2, debug, sd, "%s: RGB 888 24-bit\n", __func__);
644 i2c_wr8_and_or(sd, VOUT_SET2,
645 ~(MASK_SEL422 | MASK_VOUT_422FIL_100) & 0xff,
646 0x00);
647 i2c_wr8_and_or(sd, VI_REP, ~MASK_VOUT_COLOR_SEL & 0xff,
648 MASK_VOUT_COLOR_RGB_FULL);
649 mutex_lock(&state->confctl_mutex);
650 i2c_wr16_and_or(sd, CONFCTL, ~MASK_YCBCRFMT, 0);
651 mutex_unlock(&state->confctl_mutex);
652 break;
653 default:
654 v4l2_dbg(2, debug, sd, "%s: Unsupported format code 0x%x\n",
655 __func__, state->mbus_fmt_code);
656 }
657}
658
659static unsigned tc358743_num_csi_lanes_needed(struct v4l2_subdev *sd)
660{
661 struct tc358743_state *state = to_state(sd);
662 struct v4l2_bt_timings *bt = &state->timings.bt;
663 struct tc358743_platform_data *pdata = &state->pdata;
664 u32 bits_pr_pixel =
665 (state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16) ? 16 : 24;
666 u32 bps = bt->width * bt->height * fps(bt) * bits_pr_pixel;
667 u32 bps_pr_lane = (pdata->refclk_hz / pdata->pll_prd) * pdata->pll_fbd;
668
669 return DIV_ROUND_UP(bps, bps_pr_lane);
670}
671
672static void tc358743_set_csi(struct v4l2_subdev *sd)
673{
674 struct tc358743_state *state = to_state(sd);
675 struct tc358743_platform_data *pdata = &state->pdata;
676 unsigned lanes = tc358743_num_csi_lanes_needed(sd);
677
678 v4l2_dbg(3, debug, sd, "%s:\n", __func__);
679
680 tc358743_reset(sd, MASK_CTXRST);
681
682 if (lanes < 1)
683 i2c_wr32(sd, CLW_CNTRL, MASK_CLW_LANEDISABLE);
684 if (lanes < 1)
685 i2c_wr32(sd, D0W_CNTRL, MASK_D0W_LANEDISABLE);
686 if (lanes < 2)
687 i2c_wr32(sd, D1W_CNTRL, MASK_D1W_LANEDISABLE);
688 if (lanes < 3)
689 i2c_wr32(sd, D2W_CNTRL, MASK_D2W_LANEDISABLE);
690 if (lanes < 4)
691 i2c_wr32(sd, D3W_CNTRL, MASK_D3W_LANEDISABLE);
692
693 i2c_wr32(sd, LINEINITCNT, pdata->lineinitcnt);
694 i2c_wr32(sd, LPTXTIMECNT, pdata->lptxtimecnt);
695 i2c_wr32(sd, TCLK_HEADERCNT, pdata->tclk_headercnt);
696 i2c_wr32(sd, TCLK_TRAILCNT, pdata->tclk_trailcnt);
697 i2c_wr32(sd, THS_HEADERCNT, pdata->ths_headercnt);
698 i2c_wr32(sd, TWAKEUP, pdata->twakeup);
699 i2c_wr32(sd, TCLK_POSTCNT, pdata->tclk_postcnt);
700 i2c_wr32(sd, THS_TRAILCNT, pdata->ths_trailcnt);
701 i2c_wr32(sd, HSTXVREGCNT, pdata->hstxvregcnt);
702
703 i2c_wr32(sd, HSTXVREGEN,
704 ((lanes > 0) ? MASK_CLM_HSTXVREGEN : 0x0) |
705 ((lanes > 0) ? MASK_D0M_HSTXVREGEN : 0x0) |
706 ((lanes > 1) ? MASK_D1M_HSTXVREGEN : 0x0) |
707 ((lanes > 2) ? MASK_D2M_HSTXVREGEN : 0x0) |
708 ((lanes > 3) ? MASK_D3M_HSTXVREGEN : 0x0));
709
Philipp Zabel25614822015-07-17 11:02:55 -0300710 i2c_wr32(sd, TXOPTIONCNTRL, (state->bus.flags &
711 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK) ? MASK_CONTCLKMODE : 0);
Mats Randgaardd32d9862015-07-09 05:45:47 -0300712 i2c_wr32(sd, STARTCNTRL, MASK_START);
713 i2c_wr32(sd, CSI_START, MASK_STRT);
714
715 i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
716 MASK_ADDRESS_CSI_CONTROL |
717 MASK_CSI_MODE |
718 MASK_TXHSMD |
719 ((lanes == 4) ? MASK_NOL_4 :
720 (lanes == 3) ? MASK_NOL_3 :
721 (lanes == 2) ? MASK_NOL_2 : MASK_NOL_1));
722
723 i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
724 MASK_ADDRESS_CSI_ERR_INTENA | MASK_TXBRK | MASK_QUNK |
725 MASK_WCER | MASK_INER);
726
727 i2c_wr32(sd, CSI_CONFW, MASK_MODE_CLEAR |
728 MASK_ADDRESS_CSI_ERR_HALT | MASK_TXBRK | MASK_QUNK);
729
730 i2c_wr32(sd, CSI_CONFW, MASK_MODE_SET |
731 MASK_ADDRESS_CSI_INT_ENA | MASK_INTER);
732}
733
734static void tc358743_set_hdmi_phy(struct v4l2_subdev *sd)
735{
736 struct tc358743_state *state = to_state(sd);
737 struct tc358743_platform_data *pdata = &state->pdata;
738
739 /* Default settings from REF_02, sheet "Source HDMI"
740 * and custom settings as platform data */
741 i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, 0x0);
742 i2c_wr8(sd, PHY_CTL1, SET_PHY_AUTO_RST1_US(1600) |
743 SET_FREQ_RANGE_MODE_CYCLES(1));
744 i2c_wr8_and_or(sd, PHY_CTL2, ~MASK_PHY_AUTO_RSTn,
745 (pdata->hdmi_phy_auto_reset_tmds_detected ?
746 MASK_PHY_AUTO_RST2 : 0) |
747 (pdata->hdmi_phy_auto_reset_tmds_in_range ?
748 MASK_PHY_AUTO_RST3 : 0) |
749 (pdata->hdmi_phy_auto_reset_tmds_valid ?
750 MASK_PHY_AUTO_RST4 : 0));
751 i2c_wr8(sd, PHY_BIAS, 0x40);
752 i2c_wr8(sd, PHY_CSQ, SET_CSQ_CNT_LEVEL(0x0a));
753 i2c_wr8(sd, AVM_CTL, 45);
754 i2c_wr8_and_or(sd, HDMI_DET, ~MASK_HDMI_DET_V,
755 pdata->hdmi_detection_delay << 4);
756 i2c_wr8_and_or(sd, HV_RST, ~(MASK_H_PI_RST | MASK_V_PI_RST),
757 (pdata->hdmi_phy_auto_reset_hsync_out_of_range ?
758 MASK_H_PI_RST : 0) |
759 (pdata->hdmi_phy_auto_reset_vsync_out_of_range ?
760 MASK_V_PI_RST : 0));
761 i2c_wr8_and_or(sd, PHY_EN, ~MASK_ENABLE_PHY, MASK_ENABLE_PHY);
762}
763
764static void tc358743_set_hdmi_audio(struct v4l2_subdev *sd)
765{
766 struct tc358743_state *state = to_state(sd);
767
768 /* Default settings from REF_02, sheet "Source HDMI" */
769 i2c_wr8(sd, FORCE_MUTE, 0x00);
770 i2c_wr8(sd, AUTO_CMD0, MASK_AUTO_MUTE7 | MASK_AUTO_MUTE6 |
771 MASK_AUTO_MUTE5 | MASK_AUTO_MUTE4 |
772 MASK_AUTO_MUTE1 | MASK_AUTO_MUTE0);
773 i2c_wr8(sd, AUTO_CMD1, MASK_AUTO_MUTE9);
774 i2c_wr8(sd, AUTO_CMD2, MASK_AUTO_PLAY3 | MASK_AUTO_PLAY2);
775 i2c_wr8(sd, BUFINIT_START, SET_BUFINIT_START_MS(500));
776 i2c_wr8(sd, FS_MUTE, 0x00);
777 i2c_wr8(sd, FS_IMODE, MASK_NLPCM_SMODE | MASK_FS_SMODE);
778 i2c_wr8(sd, ACR_MODE, MASK_CTS_MODE);
779 i2c_wr8(sd, ACR_MDF0, MASK_ACR_L2MDF_1976_PPM | MASK_ACR_L1MDF_976_PPM);
780 i2c_wr8(sd, ACR_MDF1, MASK_ACR_L3MDF_3906_PPM);
781 i2c_wr8(sd, SDO_MODE1, MASK_SDO_FMT_I2S);
782 i2c_wr8(sd, DIV_MODE, SET_DIV_DLY_MS(100));
783
784 mutex_lock(&state->confctl_mutex);
785 i2c_wr16_and_or(sd, CONFCTL, 0xffff, MASK_AUDCHNUM_2 |
786 MASK_AUDOUTSEL_I2S | MASK_AUTOINDEX);
787 mutex_unlock(&state->confctl_mutex);
788}
789
790static void tc358743_set_hdmi_info_frame_mode(struct v4l2_subdev *sd)
791{
792 /* Default settings from REF_02, sheet "Source HDMI" */
793 i2c_wr8(sd, PK_INT_MODE, MASK_ISRC2_INT_MODE | MASK_ISRC_INT_MODE |
794 MASK_ACP_INT_MODE | MASK_VS_INT_MODE |
795 MASK_SPD_INT_MODE | MASK_MS_INT_MODE |
796 MASK_AUD_INT_MODE | MASK_AVI_INT_MODE);
797 i2c_wr8(sd, NO_PKT_LIMIT, 0x2c);
798 i2c_wr8(sd, NO_PKT_CLR, 0x53);
799 i2c_wr8(sd, ERR_PK_LIMIT, 0x01);
800 i2c_wr8(sd, NO_PKT_LIMIT2, 0x30);
801 i2c_wr8(sd, NO_GDB_LIMIT, 0x10);
802}
803
804static void tc358743_initial_setup(struct v4l2_subdev *sd)
805{
806 struct tc358743_state *state = to_state(sd);
807 struct tc358743_platform_data *pdata = &state->pdata;
808
809 /* CEC and IR are not supported by this driver */
810 i2c_wr16_and_or(sd, SYSCTL, ~(MASK_CECRST | MASK_IRRST),
811 (MASK_CECRST | MASK_IRRST));
812
813 tc358743_reset(sd, MASK_CTXRST | MASK_HDMIRST);
814 tc358743_sleep_mode(sd, false);
815
816 i2c_wr16(sd, FIFOCTL, pdata->fifo_level);
817
818 tc358743_set_ref_clk(sd);
819
820 i2c_wr8_and_or(sd, DDC_CTL, ~MASK_DDC5V_MODE,
821 pdata->ddc5v_delay & MASK_DDC5V_MODE);
822 i2c_wr8_and_or(sd, EDID_MODE, ~MASK_EDID_MODE, MASK_EDID_MODE_E_DDC);
823
824 tc358743_set_hdmi_phy(sd);
825 tc358743_set_hdmi_hdcp(sd, pdata->enable_hdcp);
826 tc358743_set_hdmi_audio(sd);
827 tc358743_set_hdmi_info_frame_mode(sd);
828
829 /* All CE and IT formats are detected as RGB full range in DVI mode */
830 i2c_wr8_and_or(sd, VI_MODE, ~MASK_RGB_DVI, 0);
831
832 i2c_wr8_and_or(sd, VOUT_SET2, ~MASK_VOUTCOLORMODE,
833 MASK_VOUTCOLORMODE_AUTO);
834 i2c_wr8(sd, VOUT_SET3, MASK_VOUT_EXTCNT);
835}
836
837/* --------------- IRQ --------------- */
838
839static void tc358743_format_change(struct v4l2_subdev *sd)
840{
841 struct tc358743_state *state = to_state(sd);
842 struct v4l2_dv_timings timings;
843 const struct v4l2_event tc358743_ev_fmt = {
844 .type = V4L2_EVENT_SOURCE_CHANGE,
845 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
846 };
847
848 if (tc358743_get_detected_timings(sd, &timings)) {
849 enable_stream(sd, false);
850
851 v4l2_dbg(1, debug, sd, "%s: Format changed. No signal\n",
852 __func__);
853 } else {
854 if (!v4l2_match_dv_timings(&state->timings, &timings, 0))
855 enable_stream(sd, false);
856
857 v4l2_print_dv_timings(sd->name,
858 "tc358743_format_change: Format changed. New format: ",
859 &timings, false);
860 }
861
862 v4l2_subdev_notify(sd, V4L2_DEVICE_NOTIFY_EVENT,
863 (void *)&tc358743_ev_fmt);
864}
865
866static void tc358743_init_interrupts(struct v4l2_subdev *sd)
867{
868 u16 i;
869
870 /* clear interrupt status registers */
871 for (i = SYS_INT; i <= KEY_INT; i++)
872 i2c_wr8(sd, i, 0xff);
873
874 i2c_wr16(sd, INTSTATUS, 0xffff);
875}
876
877static void tc358743_enable_interrupts(struct v4l2_subdev *sd,
878 bool cable_connected)
879{
880 v4l2_dbg(2, debug, sd, "%s: cable connected = %d\n", __func__,
881 cable_connected);
882
883 if (cable_connected) {
884 i2c_wr8(sd, SYS_INTM, ~(MASK_M_DDC | MASK_M_DVI_DET |
885 MASK_M_HDMI_DET) & 0xff);
886 i2c_wr8(sd, CLK_INTM, ~MASK_M_IN_DE_CHG);
887 i2c_wr8(sd, CBIT_INTM, ~(MASK_M_CBIT_FS | MASK_M_AF_LOCK |
888 MASK_M_AF_UNLOCK) & 0xff);
889 i2c_wr8(sd, AUDIO_INTM, ~MASK_M_BUFINIT_END);
890 i2c_wr8(sd, MISC_INTM, ~MASK_M_SYNC_CHG);
891 } else {
892 i2c_wr8(sd, SYS_INTM, ~MASK_M_DDC & 0xff);
893 i2c_wr8(sd, CLK_INTM, 0xff);
894 i2c_wr8(sd, CBIT_INTM, 0xff);
895 i2c_wr8(sd, AUDIO_INTM, 0xff);
896 i2c_wr8(sd, MISC_INTM, 0xff);
897 }
898}
899
900static void tc358743_hdmi_audio_int_handler(struct v4l2_subdev *sd,
901 bool *handled)
902{
903 u8 audio_int_mask = i2c_rd8(sd, AUDIO_INTM);
904 u8 audio_int = i2c_rd8(sd, AUDIO_INT) & ~audio_int_mask;
905
906 i2c_wr8(sd, AUDIO_INT, audio_int);
907
908 v4l2_dbg(3, debug, sd, "%s: AUDIO_INT = 0x%02x\n", __func__, audio_int);
909
910 tc358743_s_ctrl_audio_sampling_rate(sd);
911 tc358743_s_ctrl_audio_present(sd);
912}
913
914static void tc358743_csi_err_int_handler(struct v4l2_subdev *sd, bool *handled)
915{
916 v4l2_err(sd, "%s: CSI_ERR = 0x%x\n", __func__, i2c_rd32(sd, CSI_ERR));
917
918 i2c_wr32(sd, CSI_INT_CLR, MASK_ICRER);
919}
920
921static void tc358743_hdmi_misc_int_handler(struct v4l2_subdev *sd,
922 bool *handled)
923{
924 u8 misc_int_mask = i2c_rd8(sd, MISC_INTM);
925 u8 misc_int = i2c_rd8(sd, MISC_INT) & ~misc_int_mask;
926
927 i2c_wr8(sd, MISC_INT, misc_int);
928
929 v4l2_dbg(3, debug, sd, "%s: MISC_INT = 0x%02x\n", __func__, misc_int);
930
931 if (misc_int & MASK_I_SYNC_CHG) {
932 /* Reset the HDMI PHY to try to trigger proper lock on the
933 * incoming video format. Erase BKSV to prevent that old keys
934 * are used when a new source is connected. */
935 if (no_sync(sd) || no_signal(sd)) {
936 tc358743_reset_phy(sd);
937 tc358743_erase_bksv(sd);
938 }
939
940 tc358743_format_change(sd);
941
942 misc_int &= ~MASK_I_SYNC_CHG;
943 if (handled)
944 *handled = true;
945 }
946
947 if (misc_int) {
948 v4l2_err(sd, "%s: Unhandled MISC_INT interrupts: 0x%02x\n",
949 __func__, misc_int);
950 }
951}
952
953static void tc358743_hdmi_cbit_int_handler(struct v4l2_subdev *sd,
954 bool *handled)
955{
956 u8 cbit_int_mask = i2c_rd8(sd, CBIT_INTM);
957 u8 cbit_int = i2c_rd8(sd, CBIT_INT) & ~cbit_int_mask;
958
959 i2c_wr8(sd, CBIT_INT, cbit_int);
960
961 v4l2_dbg(3, debug, sd, "%s: CBIT_INT = 0x%02x\n", __func__, cbit_int);
962
963 if (cbit_int & MASK_I_CBIT_FS) {
964
965 v4l2_dbg(1, debug, sd, "%s: Audio sample rate changed\n",
966 __func__);
967 tc358743_s_ctrl_audio_sampling_rate(sd);
968
969 cbit_int &= ~MASK_I_CBIT_FS;
970 if (handled)
971 *handled = true;
972 }
973
974 if (cbit_int & (MASK_I_AF_LOCK | MASK_I_AF_UNLOCK)) {
975
976 v4l2_dbg(1, debug, sd, "%s: Audio present changed\n",
977 __func__);
978 tc358743_s_ctrl_audio_present(sd);
979
980 cbit_int &= ~(MASK_I_AF_LOCK | MASK_I_AF_UNLOCK);
981 if (handled)
982 *handled = true;
983 }
984
985 if (cbit_int) {
986 v4l2_err(sd, "%s: Unhandled CBIT_INT interrupts: 0x%02x\n",
987 __func__, cbit_int);
988 }
989}
990
991static void tc358743_hdmi_clk_int_handler(struct v4l2_subdev *sd, bool *handled)
992{
993 u8 clk_int_mask = i2c_rd8(sd, CLK_INTM);
994 u8 clk_int = i2c_rd8(sd, CLK_INT) & ~clk_int_mask;
995
996 /* Bit 7 and bit 6 are set even when they are masked */
997 i2c_wr8(sd, CLK_INT, clk_int | 0x80 | MASK_I_OUT_H_CHG);
998
999 v4l2_dbg(3, debug, sd, "%s: CLK_INT = 0x%02x\n", __func__, clk_int);
1000
1001 if (clk_int & (MASK_I_IN_DE_CHG)) {
1002
1003 v4l2_dbg(1, debug, sd, "%s: DE size or position has changed\n",
1004 __func__);
1005
1006 /* If the source switch to a new resolution with the same pixel
1007 * frequency as the existing (e.g. 1080p25 -> 720p50), the
1008 * I_SYNC_CHG interrupt is not always triggered, while the
1009 * I_IN_DE_CHG interrupt seems to work fine. Format change
1010 * notifications are only sent when the signal is stable to
1011 * reduce the number of notifications. */
1012 if (!no_signal(sd) && !no_sync(sd))
1013 tc358743_format_change(sd);
1014
1015 clk_int &= ~(MASK_I_IN_DE_CHG);
1016 if (handled)
1017 *handled = true;
1018 }
1019
1020 if (clk_int) {
1021 v4l2_err(sd, "%s: Unhandled CLK_INT interrupts: 0x%02x\n",
1022 __func__, clk_int);
1023 }
1024}
1025
1026static void tc358743_hdmi_sys_int_handler(struct v4l2_subdev *sd, bool *handled)
1027{
1028 struct tc358743_state *state = to_state(sd);
1029 u8 sys_int_mask = i2c_rd8(sd, SYS_INTM);
1030 u8 sys_int = i2c_rd8(sd, SYS_INT) & ~sys_int_mask;
1031
1032 i2c_wr8(sd, SYS_INT, sys_int);
1033
1034 v4l2_dbg(3, debug, sd, "%s: SYS_INT = 0x%02x\n", __func__, sys_int);
1035
1036 if (sys_int & MASK_I_DDC) {
1037 bool tx_5v = tx_5v_power_present(sd);
1038
1039 v4l2_dbg(1, debug, sd, "%s: Tx 5V power present: %s\n",
1040 __func__, tx_5v ? "yes" : "no");
1041
1042 if (tx_5v) {
1043 tc358743_enable_edid(sd);
1044 } else {
1045 tc358743_enable_interrupts(sd, false);
1046 tc358743_disable_edid(sd);
1047 memset(&state->timings, 0, sizeof(state->timings));
1048 tc358743_erase_bksv(sd);
1049 tc358743_update_controls(sd);
1050 }
1051
1052 sys_int &= ~MASK_I_DDC;
1053 if (handled)
1054 *handled = true;
1055 }
1056
1057 if (sys_int & MASK_I_DVI) {
1058 v4l2_dbg(1, debug, sd, "%s: HDMI->DVI change detected\n",
1059 __func__);
1060
1061 /* Reset the HDMI PHY to try to trigger proper lock on the
1062 * incoming video format. Erase BKSV to prevent that old keys
1063 * are used when a new source is connected. */
1064 if (no_sync(sd) || no_signal(sd)) {
1065 tc358743_reset_phy(sd);
1066 tc358743_erase_bksv(sd);
1067 }
1068
1069 sys_int &= ~MASK_I_DVI;
1070 if (handled)
1071 *handled = true;
1072 }
1073
1074 if (sys_int & MASK_I_HDMI) {
1075 v4l2_dbg(1, debug, sd, "%s: DVI->HDMI change detected\n",
1076 __func__);
1077
1078 /* Register is reset in DVI mode (REF_01, c. 6.6.41) */
1079 i2c_wr8(sd, ANA_CTL, MASK_APPL_PCSX_NORMAL | MASK_ANALOG_ON);
1080
1081 sys_int &= ~MASK_I_HDMI;
1082 if (handled)
1083 *handled = true;
1084 }
1085
1086 if (sys_int) {
1087 v4l2_err(sd, "%s: Unhandled SYS_INT interrupts: 0x%02x\n",
1088 __func__, sys_int);
1089 }
1090}
1091
1092/* --------------- CORE OPS --------------- */
1093
1094static int tc358743_log_status(struct v4l2_subdev *sd)
1095{
1096 struct tc358743_state *state = to_state(sd);
1097 struct v4l2_dv_timings timings;
1098 uint8_t hdmi_sys_status = i2c_rd8(sd, SYS_STATUS);
1099 uint16_t sysctl = i2c_rd16(sd, SYSCTL);
1100 u8 vi_status3 = i2c_rd8(sd, VI_STATUS3);
1101 const int deep_color_mode[4] = { 8, 10, 12, 16 };
1102 static const char * const input_color_space[] = {
1103 "RGB", "YCbCr 601", "Adobe RGB", "YCbCr 709", "NA (4)",
1104 "xvYCC 601", "NA(6)", "xvYCC 709", "NA(8)", "sYCC601",
1105 "NA(10)", "NA(11)", "NA(12)", "Adobe YCC 601"};
1106
1107 v4l2_info(sd, "-----Chip status-----\n");
1108 v4l2_info(sd, "Chip ID: 0x%02x\n",
1109 (i2c_rd16(sd, CHIPID) & MASK_CHIPID) >> 8);
1110 v4l2_info(sd, "Chip revision: 0x%02x\n",
1111 i2c_rd16(sd, CHIPID) & MASK_REVID);
1112 v4l2_info(sd, "Reset: IR: %d, CEC: %d, CSI TX: %d, HDMI: %d\n",
1113 !!(sysctl & MASK_IRRST),
1114 !!(sysctl & MASK_CECRST),
1115 !!(sysctl & MASK_CTXRST),
1116 !!(sysctl & MASK_HDMIRST));
1117 v4l2_info(sd, "Sleep mode: %s\n", sysctl & MASK_SLEEP ? "on" : "off");
1118 v4l2_info(sd, "Cable detected (+5V power): %s\n",
1119 hdmi_sys_status & MASK_S_DDC5V ? "yes" : "no");
1120 v4l2_info(sd, "DDC lines enabled: %s\n",
1121 (i2c_rd8(sd, EDID_MODE) & MASK_EDID_MODE_E_DDC) ?
1122 "yes" : "no");
1123 v4l2_info(sd, "Hotplug enabled: %s\n",
1124 (i2c_rd8(sd, HPD_CTL) & MASK_HPD_OUT0) ?
1125 "yes" : "no");
1126 v4l2_info(sd, "CEC enabled: %s\n",
1127 (i2c_rd16(sd, CECEN) & MASK_CECEN) ? "yes" : "no");
1128 v4l2_info(sd, "-----Signal status-----\n");
1129 v4l2_info(sd, "TMDS signal detected: %s\n",
1130 hdmi_sys_status & MASK_S_TMDS ? "yes" : "no");
1131 v4l2_info(sd, "Stable sync signal: %s\n",
1132 hdmi_sys_status & MASK_S_SYNC ? "yes" : "no");
1133 v4l2_info(sd, "PHY PLL locked: %s\n",
1134 hdmi_sys_status & MASK_S_PHY_PLL ? "yes" : "no");
1135 v4l2_info(sd, "PHY DE detected: %s\n",
1136 hdmi_sys_status & MASK_S_PHY_SCDT ? "yes" : "no");
1137
1138 if (tc358743_get_detected_timings(sd, &timings)) {
1139 v4l2_info(sd, "No video detected\n");
1140 } else {
1141 v4l2_print_dv_timings(sd->name, "Detected format: ", &timings,
1142 true);
1143 }
1144 v4l2_print_dv_timings(sd->name, "Configured format: ", &state->timings,
1145 true);
1146
1147 v4l2_info(sd, "-----CSI-TX status-----\n");
1148 v4l2_info(sd, "Lanes needed: %d\n",
1149 tc358743_num_csi_lanes_needed(sd));
1150 v4l2_info(sd, "Lanes in use: %d\n",
1151 tc358743_num_csi_lanes_in_use(sd));
1152 v4l2_info(sd, "Waiting for particular sync signal: %s\n",
1153 (i2c_rd16(sd, CSI_STATUS) & MASK_S_WSYNC) ?
1154 "yes" : "no");
1155 v4l2_info(sd, "Transmit mode: %s\n",
1156 (i2c_rd16(sd, CSI_STATUS) & MASK_S_TXACT) ?
1157 "yes" : "no");
1158 v4l2_info(sd, "Receive mode: %s\n",
1159 (i2c_rd16(sd, CSI_STATUS) & MASK_S_RXACT) ?
1160 "yes" : "no");
1161 v4l2_info(sd, "Stopped: %s\n",
1162 (i2c_rd16(sd, CSI_STATUS) & MASK_S_HLT) ?
1163 "yes" : "no");
1164 v4l2_info(sd, "Color space: %s\n",
1165 state->mbus_fmt_code == MEDIA_BUS_FMT_UYVY8_1X16 ?
1166 "YCbCr 422 16-bit" :
1167 state->mbus_fmt_code == MEDIA_BUS_FMT_RGB888_1X24 ?
1168 "RGB 888 24-bit" : "Unsupported");
1169
1170 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
1171 v4l2_info(sd, "HDCP encrypted content: %s\n",
1172 hdmi_sys_status & MASK_S_HDCP ? "yes" : "no");
1173 v4l2_info(sd, "Input color space: %s %s range\n",
1174 input_color_space[(vi_status3 & MASK_S_V_COLOR) >> 1],
1175 (vi_status3 & MASK_LIMITED) ? "limited" : "full");
1176 if (!is_hdmi(sd))
1177 return 0;
1178 v4l2_info(sd, "AV Mute: %s\n", hdmi_sys_status & MASK_S_AVMUTE ? "on" :
1179 "off");
1180 v4l2_info(sd, "Deep color mode: %d-bits per channel\n",
1181 deep_color_mode[(i2c_rd8(sd, VI_STATUS1) &
1182 MASK_S_DEEPCOLOR) >> 2]);
1183 print_avi_infoframe(sd);
1184
1185 return 0;
1186}
1187
1188#ifdef CONFIG_VIDEO_ADV_DEBUG
1189static void tc358743_print_register_map(struct v4l2_subdev *sd)
1190{
1191 v4l2_info(sd, "0x0000–0x00FF: Global Control Register\n");
1192 v4l2_info(sd, "0x0100–0x01FF: CSI2-TX PHY Register\n");
1193 v4l2_info(sd, "0x0200–0x03FF: CSI2-TX PPI Register\n");
1194 v4l2_info(sd, "0x0400–0x05FF: Reserved\n");
1195 v4l2_info(sd, "0x0600–0x06FF: CEC Register\n");
1196 v4l2_info(sd, "0x0700–0x84FF: Reserved\n");
1197 v4l2_info(sd, "0x8500–0x85FF: HDMIRX System Control Register\n");
1198 v4l2_info(sd, "0x8600–0x86FF: HDMIRX Audio Control Register\n");
1199 v4l2_info(sd, "0x8700–0x87FF: HDMIRX InfoFrame packet data Register\n");
1200 v4l2_info(sd, "0x8800–0x88FF: HDMIRX HDCP Port Register\n");
1201 v4l2_info(sd, "0x8900–0x89FF: HDMIRX Video Output Port & 3D Register\n");
1202 v4l2_info(sd, "0x8A00–0x8BFF: Reserved\n");
1203 v4l2_info(sd, "0x8C00–0x8FFF: HDMIRX EDID-RAM (1024bytes)\n");
1204 v4l2_info(sd, "0x9000–0x90FF: HDMIRX GBD Extraction Control\n");
1205 v4l2_info(sd, "0x9100–0x92FF: HDMIRX GBD RAM read\n");
1206 v4l2_info(sd, "0x9300- : Reserved\n");
1207}
1208
1209static int tc358743_get_reg_size(u16 address)
1210{
1211 /* REF_01 p. 66-72 */
1212 if (address <= 0x00ff)
1213 return 2;
1214 else if ((address >= 0x0100) && (address <= 0x06FF))
1215 return 4;
1216 else if ((address >= 0x0700) && (address <= 0x84ff))
1217 return 2;
1218 else
1219 return 1;
1220}
1221
1222static int tc358743_g_register(struct v4l2_subdev *sd,
1223 struct v4l2_dbg_register *reg)
1224{
1225 if (reg->reg > 0xffff) {
1226 tc358743_print_register_map(sd);
1227 return -EINVAL;
1228 }
1229
1230 reg->size = tc358743_get_reg_size(reg->reg);
1231
1232 i2c_rd(sd, reg->reg, (u8 *)&reg->val, reg->size);
1233
1234 return 0;
1235}
1236
1237static int tc358743_s_register(struct v4l2_subdev *sd,
1238 const struct v4l2_dbg_register *reg)
1239{
1240 if (reg->reg > 0xffff) {
1241 tc358743_print_register_map(sd);
1242 return -EINVAL;
1243 }
1244
1245 /* It should not be possible for the user to enable HDCP with a simple
1246 * v4l2-dbg command.
1247 *
1248 * DO NOT REMOVE THIS unless all other issues with HDCP have been
1249 * resolved.
1250 */
1251 if (reg->reg == HDCP_MODE ||
1252 reg->reg == HDCP_REG1 ||
1253 reg->reg == HDCP_REG2 ||
1254 reg->reg == HDCP_REG3 ||
1255 reg->reg == BCAPS)
1256 return 0;
1257
1258 i2c_wr(sd, (u16)reg->reg, (u8 *)&reg->val,
1259 tc358743_get_reg_size(reg->reg));
1260
1261 return 0;
1262}
1263#endif
1264
1265static int tc358743_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1266{
1267 u16 intstatus = i2c_rd16(sd, INTSTATUS);
1268
1269 v4l2_dbg(1, debug, sd, "%s: IntStatus = 0x%04x\n", __func__, intstatus);
1270
1271 if (intstatus & MASK_HDMI_INT) {
1272 u8 hdmi_int0 = i2c_rd8(sd, HDMI_INT0);
1273 u8 hdmi_int1 = i2c_rd8(sd, HDMI_INT1);
1274
1275 if (hdmi_int0 & MASK_I_MISC)
1276 tc358743_hdmi_misc_int_handler(sd, handled);
1277 if (hdmi_int1 & MASK_I_CBIT)
1278 tc358743_hdmi_cbit_int_handler(sd, handled);
1279 if (hdmi_int1 & MASK_I_CLK)
1280 tc358743_hdmi_clk_int_handler(sd, handled);
1281 if (hdmi_int1 & MASK_I_SYS)
1282 tc358743_hdmi_sys_int_handler(sd, handled);
1283 if (hdmi_int1 & MASK_I_AUD)
1284 tc358743_hdmi_audio_int_handler(sd, handled);
1285
1286 i2c_wr16(sd, INTSTATUS, MASK_HDMI_INT);
1287 intstatus &= ~MASK_HDMI_INT;
1288 }
1289
1290 if (intstatus & MASK_CSI_INT) {
1291 u32 csi_int = i2c_rd32(sd, CSI_INT);
1292
1293 if (csi_int & MASK_INTER)
1294 tc358743_csi_err_int_handler(sd, handled);
1295
1296 i2c_wr16(sd, INTSTATUS, MASK_CSI_INT);
1297 intstatus &= ~MASK_CSI_INT;
1298 }
1299
1300 intstatus = i2c_rd16(sd, INTSTATUS);
1301 if (intstatus) {
1302 v4l2_dbg(1, debug, sd,
1303 "%s: Unhandled IntStatus interrupts: 0x%02x\n",
1304 __func__, intstatus);
1305 }
1306
1307 return 0;
1308}
1309
Philipp Zabeld747b802015-07-17 11:02:56 -03001310static irqreturn_t tc358743_irq_handler(int irq, void *dev_id)
1311{
1312 struct tc358743_state *state = dev_id;
1313 bool handled;
1314
1315 tc358743_isr(&state->sd, 0, &handled);
1316
1317 return handled ? IRQ_HANDLED : IRQ_NONE;
1318}
1319
Mats Randgaardd32d9862015-07-09 05:45:47 -03001320/* --------------- VIDEO OPS --------------- */
1321
1322static int tc358743_g_input_status(struct v4l2_subdev *sd, u32 *status)
1323{
1324 *status = 0;
1325 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
1326 *status |= no_sync(sd) ? V4L2_IN_ST_NO_SYNC : 0;
1327
1328 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
1329
1330 return 0;
1331}
1332
1333static int tc358743_s_dv_timings(struct v4l2_subdev *sd,
1334 struct v4l2_dv_timings *timings)
1335{
1336 struct tc358743_state *state = to_state(sd);
1337 struct v4l2_bt_timings *bt;
1338
1339 if (!timings)
1340 return -EINVAL;
1341
1342 if (debug)
1343 v4l2_print_dv_timings(sd->name, "tc358743_s_dv_timings: ",
1344 timings, false);
1345
1346 if (v4l2_match_dv_timings(&state->timings, timings, 0)) {
1347 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1348 return 0;
1349 }
1350
1351 bt = &timings->bt;
1352
1353 if (!v4l2_valid_dv_timings(timings,
1354 &tc358743_timings_cap, NULL, NULL)) {
1355 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1356 return -ERANGE;
1357 }
1358
1359 state->timings = *timings;
1360
1361 enable_stream(sd, false);
1362 tc358743_set_pll(sd);
1363 tc358743_set_csi(sd);
1364
1365 return 0;
1366}
1367
1368static int tc358743_g_dv_timings(struct v4l2_subdev *sd,
1369 struct v4l2_dv_timings *timings)
1370{
1371 struct tc358743_state *state = to_state(sd);
1372
1373 *timings = state->timings;
1374
1375 return 0;
1376}
1377
1378static int tc358743_enum_dv_timings(struct v4l2_subdev *sd,
1379 struct v4l2_enum_dv_timings *timings)
1380{
1381 if (timings->pad != 0)
1382 return -EINVAL;
1383
1384 return v4l2_enum_dv_timings_cap(timings,
1385 &tc358743_timings_cap, NULL, NULL);
1386}
1387
1388static int tc358743_query_dv_timings(struct v4l2_subdev *sd,
1389 struct v4l2_dv_timings *timings)
1390{
1391 int ret;
1392
1393 ret = tc358743_get_detected_timings(sd, timings);
1394 if (ret)
1395 return ret;
1396
1397 if (debug)
1398 v4l2_print_dv_timings(sd->name, "tc358743_query_dv_timings: ",
1399 timings, false);
1400
1401 if (!v4l2_valid_dv_timings(timings,
1402 &tc358743_timings_cap, NULL, NULL)) {
1403 v4l2_dbg(1, debug, sd, "%s: timings out of range\n", __func__);
1404 return -ERANGE;
1405 }
1406
1407 return 0;
1408}
1409
1410static int tc358743_dv_timings_cap(struct v4l2_subdev *sd,
1411 struct v4l2_dv_timings_cap *cap)
1412{
1413 if (cap->pad != 0)
1414 return -EINVAL;
1415
1416 *cap = tc358743_timings_cap;
1417
1418 return 0;
1419}
1420
1421static int tc358743_g_mbus_config(struct v4l2_subdev *sd,
1422 struct v4l2_mbus_config *cfg)
1423{
1424 cfg->type = V4L2_MBUS_CSI2;
1425
1426 /* Support for non-continuous CSI-2 clock is missing in the driver */
1427 cfg->flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
1428
1429 switch (tc358743_num_csi_lanes_in_use(sd)) {
1430 case 1:
1431 cfg->flags |= V4L2_MBUS_CSI2_1_LANE;
1432 break;
1433 case 2:
1434 cfg->flags |= V4L2_MBUS_CSI2_2_LANE;
1435 break;
1436 case 3:
1437 cfg->flags |= V4L2_MBUS_CSI2_3_LANE;
1438 break;
1439 case 4:
1440 cfg->flags |= V4L2_MBUS_CSI2_4_LANE;
1441 break;
1442 default:
1443 return -EINVAL;
1444 }
1445
1446 return 0;
1447}
1448
1449static int tc358743_s_stream(struct v4l2_subdev *sd, int enable)
1450{
1451 enable_stream(sd, enable);
1452
1453 return 0;
1454}
1455
1456/* --------------- PAD OPS --------------- */
1457
1458static int tc358743_get_fmt(struct v4l2_subdev *sd,
1459 struct v4l2_subdev_pad_config *cfg,
1460 struct v4l2_subdev_format *format)
1461{
1462 struct tc358743_state *state = to_state(sd);
1463 u8 vi_rep = i2c_rd8(sd, VI_REP);
1464
1465 if (format->pad != 0)
1466 return -EINVAL;
1467
1468 format->format.code = state->mbus_fmt_code;
1469 format->format.width = state->timings.bt.width;
1470 format->format.height = state->timings.bt.height;
1471 format->format.field = V4L2_FIELD_NONE;
1472
1473 switch (vi_rep & MASK_VOUT_COLOR_SEL) {
1474 case MASK_VOUT_COLOR_RGB_FULL:
1475 case MASK_VOUT_COLOR_RGB_LIMITED:
1476 format->format.colorspace = V4L2_COLORSPACE_SRGB;
1477 break;
1478 case MASK_VOUT_COLOR_601_YCBCR_LIMITED:
1479 case MASK_VOUT_COLOR_601_YCBCR_FULL:
1480 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
1481 break;
1482 case MASK_VOUT_COLOR_709_YCBCR_FULL:
1483 case MASK_VOUT_COLOR_709_YCBCR_LIMITED:
1484 format->format.colorspace = V4L2_COLORSPACE_REC709;
1485 break;
1486 default:
1487 format->format.colorspace = 0;
1488 break;
1489 }
1490
1491 return 0;
1492}
1493
1494static int tc358743_set_fmt(struct v4l2_subdev *sd,
1495 struct v4l2_subdev_pad_config *cfg,
1496 struct v4l2_subdev_format *format)
1497{
1498 struct tc358743_state *state = to_state(sd);
1499
1500 u32 code = format->format.code; /* is overwritten by get_fmt */
1501 int ret = tc358743_get_fmt(sd, cfg, format);
1502
1503 format->format.code = code;
1504
1505 if (ret)
1506 return ret;
1507
1508 switch (code) {
1509 case MEDIA_BUS_FMT_RGB888_1X24:
1510 case MEDIA_BUS_FMT_UYVY8_1X16:
1511 break;
1512 default:
1513 return -EINVAL;
1514 }
1515
1516 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1517 return 0;
1518
1519 state->mbus_fmt_code = format->format.code;
1520
1521 enable_stream(sd, false);
1522 tc358743_set_pll(sd);
1523 tc358743_set_csi(sd);
1524 tc358743_set_csi_color_space(sd);
1525
1526 return 0;
1527}
1528
1529static int tc358743_g_edid(struct v4l2_subdev *sd,
1530 struct v4l2_subdev_edid *edid)
1531{
1532 struct tc358743_state *state = to_state(sd);
1533
1534 if (edid->pad != 0)
1535 return -EINVAL;
1536
1537 if (edid->start_block == 0 && edid->blocks == 0) {
1538 edid->blocks = state->edid_blocks_written;
1539 return 0;
1540 }
1541
1542 if (state->edid_blocks_written == 0)
1543 return -ENODATA;
1544
1545 if (edid->start_block >= state->edid_blocks_written ||
1546 edid->blocks == 0)
1547 return -EINVAL;
1548
1549 if (edid->start_block + edid->blocks > state->edid_blocks_written)
1550 edid->blocks = state->edid_blocks_written - edid->start_block;
1551
1552 i2c_rd(sd, EDID_RAM + (edid->start_block * EDID_BLOCK_SIZE), edid->edid,
1553 edid->blocks * EDID_BLOCK_SIZE);
1554
1555 return 0;
1556}
1557
1558static int tc358743_s_edid(struct v4l2_subdev *sd,
1559 struct v4l2_subdev_edid *edid)
1560{
1561 struct tc358743_state *state = to_state(sd);
1562 u16 edid_len = edid->blocks * EDID_BLOCK_SIZE;
1563
1564 v4l2_dbg(2, debug, sd, "%s, pad %d, start block %d, blocks %d\n",
1565 __func__, edid->pad, edid->start_block, edid->blocks);
1566
1567 if (edid->pad != 0)
1568 return -EINVAL;
1569
1570 if (edid->start_block != 0)
1571 return -EINVAL;
1572
1573 if (edid->blocks > EDID_NUM_BLOCKS_MAX) {
1574 edid->blocks = EDID_NUM_BLOCKS_MAX;
1575 return -E2BIG;
1576 }
1577
1578 tc358743_disable_edid(sd);
1579
1580 i2c_wr8(sd, EDID_LEN1, edid_len & 0xff);
1581 i2c_wr8(sd, EDID_LEN2, edid_len >> 8);
1582
1583 if (edid->blocks == 0) {
1584 state->edid_blocks_written = 0;
1585 return 0;
1586 }
1587
1588 i2c_wr(sd, EDID_RAM, edid->edid, edid_len);
1589
1590 state->edid_blocks_written = edid->blocks;
1591
1592 if (tx_5v_power_present(sd))
1593 tc358743_enable_edid(sd);
1594
1595 return 0;
1596}
1597
1598/* -------------------------------------------------------------------------- */
1599
1600static const struct v4l2_subdev_core_ops tc358743_core_ops = {
1601 .log_status = tc358743_log_status,
1602#ifdef CONFIG_VIDEO_ADV_DEBUG
1603 .g_register = tc358743_g_register,
1604 .s_register = tc358743_s_register,
1605#endif
1606 .interrupt_service_routine = tc358743_isr,
1607};
1608
1609static const struct v4l2_subdev_video_ops tc358743_video_ops = {
1610 .g_input_status = tc358743_g_input_status,
1611 .s_dv_timings = tc358743_s_dv_timings,
1612 .g_dv_timings = tc358743_g_dv_timings,
1613 .query_dv_timings = tc358743_query_dv_timings,
1614 .g_mbus_config = tc358743_g_mbus_config,
1615 .s_stream = tc358743_s_stream,
1616};
1617
1618static const struct v4l2_subdev_pad_ops tc358743_pad_ops = {
1619 .set_fmt = tc358743_set_fmt,
1620 .get_fmt = tc358743_get_fmt,
1621 .get_edid = tc358743_g_edid,
1622 .set_edid = tc358743_s_edid,
1623 .enum_dv_timings = tc358743_enum_dv_timings,
1624 .dv_timings_cap = tc358743_dv_timings_cap,
1625};
1626
1627static const struct v4l2_subdev_ops tc358743_ops = {
1628 .core = &tc358743_core_ops,
1629 .video = &tc358743_video_ops,
1630 .pad = &tc358743_pad_ops,
1631};
1632
1633/* --------------- CUSTOM CTRLS --------------- */
1634
1635static const struct v4l2_ctrl_config tc358743_ctrl_audio_sampling_rate = {
1636 .id = TC358743_CID_AUDIO_SAMPLING_RATE,
1637 .name = "Audio sampling rate",
1638 .type = V4L2_CTRL_TYPE_INTEGER,
1639 .min = 0,
1640 .max = 768000,
1641 .step = 1,
1642 .def = 0,
1643 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1644};
1645
1646static const struct v4l2_ctrl_config tc358743_ctrl_audio_present = {
1647 .id = TC358743_CID_AUDIO_PRESENT,
1648 .name = "Audio present",
1649 .type = V4L2_CTRL_TYPE_BOOLEAN,
1650 .min = 0,
1651 .max = 1,
1652 .step = 1,
1653 .def = 0,
1654 .flags = V4L2_CTRL_FLAG_READ_ONLY,
1655};
1656
1657/* --------------- PROBE / REMOVE --------------- */
1658
Philipp Zabel25614822015-07-17 11:02:55 -03001659#ifdef CONFIG_OF
1660static void tc358743_gpio_reset(struct tc358743_state *state)
1661{
1662 gpiod_set_value(state->reset_gpio, 0);
1663 usleep_range(5000, 10000);
1664 gpiod_set_value(state->reset_gpio, 1);
1665 usleep_range(1000, 2000);
1666 gpiod_set_value(state->reset_gpio, 0);
1667 msleep(20);
1668}
1669
1670static int tc358743_probe_of(struct tc358743_state *state)
1671{
1672 struct device *dev = &state->i2c_client->dev;
1673 struct v4l2_of_endpoint *endpoint;
1674 struct device_node *ep;
1675 struct clk *refclk;
1676 u32 bps_pr_lane;
1677 int ret = -EINVAL;
1678
1679 refclk = devm_clk_get(dev, "refclk");
1680 if (IS_ERR(refclk)) {
1681 if (PTR_ERR(refclk) != -EPROBE_DEFER)
1682 dev_err(dev, "failed to get refclk: %ld\n",
1683 PTR_ERR(refclk));
1684 return PTR_ERR(refclk);
1685 }
1686
1687 ep = of_graph_get_next_endpoint(dev->of_node, NULL);
1688 if (!ep) {
1689 dev_err(dev, "missing endpoint node\n");
1690 return -EINVAL;
1691 }
1692
1693 endpoint = v4l2_of_alloc_parse_endpoint(ep);
1694 if (IS_ERR(endpoint)) {
1695 dev_err(dev, "failed to parse endpoint\n");
1696 return PTR_ERR(endpoint);
1697 }
1698
1699 if (endpoint->bus_type != V4L2_MBUS_CSI2 ||
1700 endpoint->bus.mipi_csi2.num_data_lanes == 0 ||
1701 endpoint->nr_of_link_frequencies == 0) {
1702 dev_err(dev, "missing CSI-2 properties in endpoint\n");
1703 goto free_endpoint;
1704 }
1705
1706 state->bus = endpoint->bus.mipi_csi2;
1707
1708 clk_prepare_enable(refclk);
1709
1710 state->pdata.refclk_hz = clk_get_rate(refclk);
1711 state->pdata.ddc5v_delay = DDC5V_DELAY_100_MS;
1712 state->pdata.enable_hdcp = false;
1713 /* A FIFO level of 16 should be enough for 2-lane 720p60 at 594 MHz. */
1714 state->pdata.fifo_level = 16;
1715 /*
1716 * The PLL input clock is obtained by dividing refclk by pll_prd.
1717 * It must be between 6 MHz and 40 MHz, lower frequency is better.
1718 */
1719 switch (state->pdata.refclk_hz) {
1720 case 26000000:
1721 case 27000000:
1722 case 42000000:
1723 state->pdata.pll_prd = state->pdata.refclk_hz / 6000000;
1724 break;
1725 default:
1726 dev_err(dev, "unsupported refclk rate: %u Hz\n",
1727 state->pdata.refclk_hz);
1728 goto disable_clk;
1729 }
1730
1731 /*
1732 * The CSI bps per lane must be between 62.5 Mbps and 1 Gbps.
1733 * The default is 594 Mbps for 4-lane 1080p60 or 2-lane 720p60.
1734 */
1735 bps_pr_lane = 2 * endpoint->link_frequencies[0];
1736 if (bps_pr_lane < 62500000U || bps_pr_lane > 1000000000U) {
1737 dev_err(dev, "unsupported bps per lane: %u bps\n", bps_pr_lane);
1738 goto disable_clk;
1739 }
1740
1741 /* The CSI speed per lane is refclk / pll_prd * pll_fbd */
1742 state->pdata.pll_fbd = bps_pr_lane /
1743 state->pdata.refclk_hz * state->pdata.pll_prd;
1744
1745 /*
1746 * FIXME: These timings are from REF_02 for 594 Mbps per lane (297 MHz
1747 * link frequency). In principle it should be possible to calculate
1748 * them based on link frequency and resolution.
1749 */
1750 if (bps_pr_lane != 594000000U)
1751 dev_warn(dev, "untested bps per lane: %u bps\n", bps_pr_lane);
1752 state->pdata.lineinitcnt = 0xe80;
1753 state->pdata.lptxtimecnt = 0x003;
1754 /* tclk-preparecnt: 3, tclk-zerocnt: 20 */
1755 state->pdata.tclk_headercnt = 0x1403;
1756 state->pdata.tclk_trailcnt = 0x00;
1757 /* ths-preparecnt: 3, ths-zerocnt: 1 */
1758 state->pdata.ths_headercnt = 0x0103;
1759 state->pdata.twakeup = 0x4882;
1760 state->pdata.tclk_postcnt = 0x008;
1761 state->pdata.ths_trailcnt = 0x2;
1762 state->pdata.hstxvregcnt = 0;
1763
1764 state->reset_gpio = devm_gpiod_get(dev, "reset");
1765 if (IS_ERR(state->reset_gpio)) {
1766 dev_err(dev, "failed to get reset gpio\n");
1767 ret = PTR_ERR(state->reset_gpio);
1768 goto disable_clk;
1769 }
1770
1771 tc358743_gpio_reset(state);
1772
1773 ret = 0;
1774 goto free_endpoint;
1775
1776disable_clk:
1777 clk_disable_unprepare(refclk);
1778free_endpoint:
1779 v4l2_of_free_endpoint(endpoint);
1780 return ret;
1781}
1782#else
1783static inline int tc358743_probe_of(struct tc358743_state *state)
1784{
1785 return -ENODEV;
1786}
1787#endif
1788
Mats Randgaardd32d9862015-07-09 05:45:47 -03001789static int tc358743_probe(struct i2c_client *client,
1790 const struct i2c_device_id *id)
1791{
1792 static struct v4l2_dv_timings default_timing =
1793 V4L2_DV_BT_CEA_640X480P59_94;
1794 struct tc358743_state *state;
1795 struct tc358743_platform_data *pdata = client->dev.platform_data;
1796 struct v4l2_subdev *sd;
1797 int err;
1798
1799 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1800 return -EIO;
1801 v4l_dbg(1, debug, client, "chip found @ 0x%x (%s)\n",
1802 client->addr << 1, client->adapter->name);
1803
1804 state = devm_kzalloc(&client->dev, sizeof(struct tc358743_state),
1805 GFP_KERNEL);
1806 if (!state)
1807 return -ENOMEM;
1808
Mats Randgaardd32d9862015-07-09 05:45:47 -03001809 state->i2c_client = client;
Philipp Zabel25614822015-07-17 11:02:55 -03001810
1811 /* platform data */
1812 if (pdata) {
1813 state->pdata = *pdata;
1814 state->bus.flags = V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
1815 } else {
1816 err = tc358743_probe_of(state);
1817 if (err == -ENODEV)
1818 v4l_err(client, "No platform data!\n");
1819 if (err)
1820 return err;
1821 }
1822
Mats Randgaardd32d9862015-07-09 05:45:47 -03001823 sd = &state->sd;
1824 v4l2_i2c_subdev_init(sd, client, &tc358743_ops);
Philipp Zabel8ec23da2015-07-17 11:02:54 -03001825 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
Mats Randgaardd32d9862015-07-09 05:45:47 -03001826
1827 /* i2c access */
1828 if ((i2c_rd16(sd, CHIPID) & MASK_CHIPID) != 0) {
1829 v4l2_info(sd, "not a TC358743 on address 0x%x\n",
1830 client->addr << 1);
1831 return -ENODEV;
1832 }
1833
1834 /* control handlers */
1835 v4l2_ctrl_handler_init(&state->hdl, 3);
1836
1837 /* private controls */
1838 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(&state->hdl, NULL,
1839 V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
1840
1841 /* custom controls */
1842 state->audio_sampling_rate_ctrl = v4l2_ctrl_new_custom(&state->hdl,
1843 &tc358743_ctrl_audio_sampling_rate, NULL);
1844
1845 state->audio_present_ctrl = v4l2_ctrl_new_custom(&state->hdl,
1846 &tc358743_ctrl_audio_present, NULL);
1847
1848 sd->ctrl_handler = &state->hdl;
1849 if (state->hdl.error) {
1850 err = state->hdl.error;
1851 goto err_hdl;
1852 }
1853
1854 if (tc358743_update_controls(sd)) {
1855 err = -ENODEV;
1856 goto err_hdl;
1857 }
1858
1859 /* work queues */
1860 state->work_queues = create_singlethread_workqueue(client->name);
1861 if (!state->work_queues) {
1862 v4l2_err(sd, "Could not create work queue\n");
1863 err = -ENOMEM;
1864 goto err_hdl;
1865 }
1866
Philipp Zabel4c5211a2015-07-17 11:02:53 -03001867 state->pad.flags = MEDIA_PAD_FL_SOURCE;
1868 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
1869 if (err < 0)
1870 goto err_hdl;
1871
1872 sd->dev = &client->dev;
1873 err = v4l2_async_register_subdev(sd);
1874 if (err < 0)
1875 goto err_hdl;
1876
Mats Randgaardd32d9862015-07-09 05:45:47 -03001877 mutex_init(&state->confctl_mutex);
1878
1879 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
1880 tc358743_delayed_work_enable_hotplug);
1881
1882 tc358743_initial_setup(sd);
1883
1884 tc358743_s_dv_timings(sd, &default_timing);
1885
1886 state->mbus_fmt_code = MEDIA_BUS_FMT_RGB888_1X24;
1887 tc358743_set_csi_color_space(sd);
1888
1889 tc358743_init_interrupts(sd);
Philipp Zabeld747b802015-07-17 11:02:56 -03001890
1891 if (state->i2c_client->irq) {
1892 err = devm_request_threaded_irq(&client->dev,
1893 state->i2c_client->irq,
1894 NULL, tc358743_irq_handler,
1895 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
1896 "tc358743", state);
1897 if (err)
1898 goto err_work_queues;
1899 }
1900
Mats Randgaardd32d9862015-07-09 05:45:47 -03001901 tc358743_enable_interrupts(sd, tx_5v_power_present(sd));
1902 i2c_wr16(sd, INTMASK, ~(MASK_HDMI_MSK | MASK_CSI_MSK) & 0xffff);
1903
1904 err = v4l2_ctrl_handler_setup(sd->ctrl_handler);
1905 if (err)
1906 goto err_work_queues;
1907
1908 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1909 client->addr << 1, client->adapter->name);
1910
1911 return 0;
1912
1913err_work_queues:
1914 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1915 destroy_workqueue(state->work_queues);
1916 mutex_destroy(&state->confctl_mutex);
1917err_hdl:
Philipp Zabel4c5211a2015-07-17 11:02:53 -03001918 media_entity_cleanup(&sd->entity);
Mats Randgaardd32d9862015-07-09 05:45:47 -03001919 v4l2_ctrl_handler_free(&state->hdl);
1920 return err;
1921}
1922
1923static int tc358743_remove(struct i2c_client *client)
1924{
1925 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1926 struct tc358743_state *state = to_state(sd);
1927
1928 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1929 destroy_workqueue(state->work_queues);
Philipp Zabel4c5211a2015-07-17 11:02:53 -03001930 v4l2_async_unregister_subdev(sd);
Mats Randgaardd32d9862015-07-09 05:45:47 -03001931 v4l2_device_unregister_subdev(sd);
1932 mutex_destroy(&state->confctl_mutex);
Philipp Zabel4c5211a2015-07-17 11:02:53 -03001933 media_entity_cleanup(&sd->entity);
Mats Randgaardd32d9862015-07-09 05:45:47 -03001934 v4l2_ctrl_handler_free(&state->hdl);
1935
1936 return 0;
1937}
1938
1939static struct i2c_device_id tc358743_id[] = {
1940 {"tc358743", 0},
1941 {}
1942};
1943
1944MODULE_DEVICE_TABLE(i2c, tc358743_id);
1945
1946static struct i2c_driver tc358743_driver = {
1947 .driver = {
1948 .owner = THIS_MODULE,
1949 .name = "tc358743",
1950 },
1951 .probe = tc358743_probe,
1952 .remove = tc358743_remove,
1953 .id_table = tc358743_id,
1954};
1955
1956module_i2c_driver(tc358743_driver);