blob: c28bf94a74099a80cf1de4e28b0cd929c9723226 [file] [log] [blame]
Hans Verkuila89bcd42013-08-22 06:14:22 -03001/*
2 * adv7842 - Analog Devices ADV7842 video decoder driver
3 *
4 * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 *
19 */
20
21/*
22 * References (c = chapter, p = page):
Mats Randgaard5b64b202013-12-05 12:08:45 -030023 * REF_01 - Analog devices, ADV7842,
24 * Register Settings Recommendations, Rev. 1.9, April 2011
Mats Randgaard7de6fab2013-12-10 11:24:35 -030025 * REF_02 - Analog devices, Software User Guide, UG-206,
26 * ADV7842 I2C Register Maps, Rev. 0, November 2010
Mats Randgaard5b64b202013-12-05 12:08:45 -030027 * REF_03 - Analog devices, Hardware User Guide, UG-214,
28 * ADV7842 Fast Switching 2:1 HDMI 1.4 Receiver with 3D-Comb
29 * Decoder and Digitizer , Rev. 0, January 2011
Hans Verkuila89bcd42013-08-22 06:14:22 -030030 */
31
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/i2c.h>
37#include <linux/delay.h>
38#include <linux/videodev2.h>
39#include <linux/workqueue.h>
40#include <linux/v4l2-dv-timings.h>
Martin Bugge09f90c52014-12-19 09:14:23 -030041#include <linux/hdmi.h>
Hans Verkuil25c84fb2015-09-07 08:13:26 -030042#include <media/cec.h>
Hans Verkuila89bcd42013-08-22 06:14:22 -030043#include <media/v4l2-device.h>
Lars-Peter Clausenaef51592015-06-24 13:50:28 -030044#include <media/v4l2-event.h>
Hans Verkuila89bcd42013-08-22 06:14:22 -030045#include <media/v4l2-ctrls.h>
46#include <media/v4l2-dv-timings.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020047#include <media/i2c/adv7842.h>
Hans Verkuila89bcd42013-08-22 06:14:22 -030048
49static int debug;
50module_param(debug, int, 0644);
51MODULE_PARM_DESC(debug, "debug level (0-2)");
52
53MODULE_DESCRIPTION("Analog Devices ADV7842 video decoder driver");
54MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
55MODULE_AUTHOR("Martin Bugge <marbugge@cisco.com>");
56MODULE_LICENSE("GPL");
57
58/* ADV7842 system clock frequency */
59#define ADV7842_fsc (28636360)
60
Hans Verkuilf888ae72015-05-01 11:31:30 -030061#define ADV7842_RGB_OUT (1 << 1)
62
63#define ADV7842_OP_FORMAT_SEL_8BIT (0 << 0)
64#define ADV7842_OP_FORMAT_SEL_10BIT (1 << 0)
65#define ADV7842_OP_FORMAT_SEL_12BIT (2 << 0)
66
67#define ADV7842_OP_MODE_SEL_SDR_422 (0 << 5)
68#define ADV7842_OP_MODE_SEL_DDR_422 (1 << 5)
69#define ADV7842_OP_MODE_SEL_SDR_444 (2 << 5)
70#define ADV7842_OP_MODE_SEL_DDR_444 (3 << 5)
71#define ADV7842_OP_MODE_SEL_SDR_422_2X (4 << 5)
72#define ADV7842_OP_MODE_SEL_ADI_CM (5 << 5)
73
74#define ADV7842_OP_CH_SEL_GBR (0 << 5)
75#define ADV7842_OP_CH_SEL_GRB (1 << 5)
76#define ADV7842_OP_CH_SEL_BGR (2 << 5)
77#define ADV7842_OP_CH_SEL_RGB (3 << 5)
78#define ADV7842_OP_CH_SEL_BRG (4 << 5)
79#define ADV7842_OP_CH_SEL_RBG (5 << 5)
80
81#define ADV7842_OP_SWAP_CB_CR (1 << 0)
82
Hans Verkuil25c84fb2015-09-07 08:13:26 -030083#define ADV7842_MAX_ADDRS (3)
84
Hans Verkuila89bcd42013-08-22 06:14:22 -030085/*
86**********************************************************************
87*
88* Arrays with configuration parameters for the ADV7842
89*
90**********************************************************************
91*/
92
Hans Verkuilf888ae72015-05-01 11:31:30 -030093struct adv7842_format_info {
94 u32 code;
95 u8 op_ch_sel;
96 bool rgb_out;
97 bool swap_cb_cr;
98 u8 op_format_sel;
99};
100
Hans Verkuila89bcd42013-08-22 06:14:22 -0300101struct adv7842_state {
Martin Bugge7de5be42013-12-05 11:39:37 -0300102 struct adv7842_platform_data pdata;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300103 struct v4l2_subdev sd;
104 struct media_pad pad;
105 struct v4l2_ctrl_handler hdl;
106 enum adv7842_mode mode;
107 struct v4l2_dv_timings timings;
108 enum adv7842_vid_std_select vid_std_select;
Hans Verkuilf888ae72015-05-01 11:31:30 -0300109
110 const struct adv7842_format_info *format;
111
Hans Verkuila89bcd42013-08-22 06:14:22 -0300112 v4l2_std_id norm;
113 struct {
114 u8 edid[256];
115 u32 present;
116 } hdmi_edid;
117 struct {
118 u8 edid[256];
119 u32 present;
120 } vga_edid;
121 struct v4l2_fract aspect_ratio;
122 u32 rgb_quantization_range;
123 bool is_cea_format;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300124 struct delayed_work delayed_work_enable_hotplug;
Martin Bugge6e9071f2013-12-10 12:00:06 -0300125 bool restart_stdi_once;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300126 bool hdmi_port_a;
127
128 /* i2c clients */
129 struct i2c_client *i2c_sdp_io;
130 struct i2c_client *i2c_sdp;
131 struct i2c_client *i2c_cp;
132 struct i2c_client *i2c_vdp;
133 struct i2c_client *i2c_afe;
134 struct i2c_client *i2c_hdmi;
135 struct i2c_client *i2c_repeater;
136 struct i2c_client *i2c_edid;
137 struct i2c_client *i2c_infoframe;
138 struct i2c_client *i2c_cec;
139 struct i2c_client *i2c_avlink;
140
141 /* controls */
142 struct v4l2_ctrl *detect_tx_5v_ctrl;
143 struct v4l2_ctrl *analog_sampling_phase_ctrl;
144 struct v4l2_ctrl *free_run_color_ctrl_manual;
145 struct v4l2_ctrl *free_run_color_ctrl;
146 struct v4l2_ctrl *rgb_quantization_range_ctrl;
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300147
148 struct cec_adapter *cec_adap;
149 u8 cec_addr[ADV7842_MAX_ADDRS];
150 u8 cec_valid_addrs;
151 bool cec_enabled_adap;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300152};
153
154/* Unsupported timings. This device cannot support 720p30. */
155static const struct v4l2_dv_timings adv7842_timings_exceptions[] = {
156 V4L2_DV_BT_CEA_1280X720P30,
157 { }
158};
159
160static bool adv7842_check_dv_timings(const struct v4l2_dv_timings *t, void *hdl)
161{
162 int i;
163
164 for (i = 0; adv7842_timings_exceptions[i].bt.width; i++)
Hans Verkuil85f9e062015-11-13 09:46:26 -0200165 if (v4l2_match_dv_timings(t, adv7842_timings_exceptions + i, 0, false))
Hans Verkuila89bcd42013-08-22 06:14:22 -0300166 return false;
167 return true;
168}
169
170struct adv7842_video_standards {
171 struct v4l2_dv_timings timings;
172 u8 vid_std;
173 u8 v_freq;
174};
175
176/* sorted by number of lines */
177static const struct adv7842_video_standards adv7842_prim_mode_comp[] = {
178 /* { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 }, TODO flickering */
179 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
180 { V4L2_DV_BT_CEA_1280X720P50, 0x19, 0x01 },
181 { V4L2_DV_BT_CEA_1280X720P60, 0x19, 0x00 },
182 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
183 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
184 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
185 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
186 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
187 /* TODO add 1920x1080P60_RB (CVT timing) */
188 { },
189};
190
191/* sorted by number of lines */
192static const struct adv7842_video_standards adv7842_prim_mode_gr[] = {
193 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
194 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
195 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
196 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
197 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
198 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
199 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
200 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
201 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
202 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
203 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
204 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
205 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
206 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
207 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
208 { V4L2_DV_BT_DMT_1360X768P60, 0x12, 0x00 },
209 { V4L2_DV_BT_DMT_1366X768P60, 0x13, 0x00 },
210 { V4L2_DV_BT_DMT_1400X1050P60, 0x14, 0x00 },
211 { V4L2_DV_BT_DMT_1400X1050P75, 0x15, 0x00 },
212 { V4L2_DV_BT_DMT_1600X1200P60, 0x16, 0x00 }, /* TODO not tested */
213 /* TODO add 1600X1200P60_RB (not a DMT timing) */
214 { V4L2_DV_BT_DMT_1680X1050P60, 0x18, 0x00 },
215 { V4L2_DV_BT_DMT_1920X1200P60_RB, 0x19, 0x00 }, /* TODO not tested */
216 { },
217};
218
219/* sorted by number of lines */
220static const struct adv7842_video_standards adv7842_prim_mode_hdmi_comp[] = {
221 { V4L2_DV_BT_CEA_720X480P59_94, 0x0a, 0x00 },
222 { V4L2_DV_BT_CEA_720X576P50, 0x0b, 0x00 },
223 { V4L2_DV_BT_CEA_1280X720P50, 0x13, 0x01 },
224 { V4L2_DV_BT_CEA_1280X720P60, 0x13, 0x00 },
225 { V4L2_DV_BT_CEA_1920X1080P24, 0x1e, 0x04 },
226 { V4L2_DV_BT_CEA_1920X1080P25, 0x1e, 0x03 },
227 { V4L2_DV_BT_CEA_1920X1080P30, 0x1e, 0x02 },
228 { V4L2_DV_BT_CEA_1920X1080P50, 0x1e, 0x01 },
229 { V4L2_DV_BT_CEA_1920X1080P60, 0x1e, 0x00 },
230 { },
231};
232
233/* sorted by number of lines */
234static const struct adv7842_video_standards adv7842_prim_mode_hdmi_gr[] = {
235 { V4L2_DV_BT_DMT_640X480P60, 0x08, 0x00 },
236 { V4L2_DV_BT_DMT_640X480P72, 0x09, 0x00 },
237 { V4L2_DV_BT_DMT_640X480P75, 0x0a, 0x00 },
238 { V4L2_DV_BT_DMT_640X480P85, 0x0b, 0x00 },
239 { V4L2_DV_BT_DMT_800X600P56, 0x00, 0x00 },
240 { V4L2_DV_BT_DMT_800X600P60, 0x01, 0x00 },
241 { V4L2_DV_BT_DMT_800X600P72, 0x02, 0x00 },
242 { V4L2_DV_BT_DMT_800X600P75, 0x03, 0x00 },
243 { V4L2_DV_BT_DMT_800X600P85, 0x04, 0x00 },
244 { V4L2_DV_BT_DMT_1024X768P60, 0x0c, 0x00 },
245 { V4L2_DV_BT_DMT_1024X768P70, 0x0d, 0x00 },
246 { V4L2_DV_BT_DMT_1024X768P75, 0x0e, 0x00 },
247 { V4L2_DV_BT_DMT_1024X768P85, 0x0f, 0x00 },
248 { V4L2_DV_BT_DMT_1280X1024P60, 0x05, 0x00 },
249 { V4L2_DV_BT_DMT_1280X1024P75, 0x06, 0x00 },
250 { },
251};
252
Hans Verkuil48519832015-05-07 10:37:57 -0300253static const struct v4l2_event adv7842_ev_fmt = {
254 .type = V4L2_EVENT_SOURCE_CHANGE,
255 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
256};
257
Hans Verkuila89bcd42013-08-22 06:14:22 -0300258/* ----------------------------------------------------------------------- */
259
260static inline struct adv7842_state *to_state(struct v4l2_subdev *sd)
261{
262 return container_of(sd, struct adv7842_state, sd);
263}
264
265static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
266{
267 return &container_of(ctrl->handler, struct adv7842_state, hdl)->sd;
268}
269
Hans Verkuilf888ae72015-05-01 11:31:30 -0300270static inline unsigned hblanking(const struct v4l2_bt_timings *t)
271{
272 return V4L2_DV_BT_BLANKING_WIDTH(t);
273}
274
Hans Verkuila89bcd42013-08-22 06:14:22 -0300275static inline unsigned htotal(const struct v4l2_bt_timings *t)
276{
277 return V4L2_DV_BT_FRAME_WIDTH(t);
278}
279
Hans Verkuilf888ae72015-05-01 11:31:30 -0300280static inline unsigned vblanking(const struct v4l2_bt_timings *t)
281{
282 return V4L2_DV_BT_BLANKING_HEIGHT(t);
283}
284
Hans Verkuila89bcd42013-08-22 06:14:22 -0300285static inline unsigned vtotal(const struct v4l2_bt_timings *t)
286{
287 return V4L2_DV_BT_FRAME_HEIGHT(t);
288}
289
290
291/* ----------------------------------------------------------------------- */
292
293static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
294 u8 command, bool check)
295{
296 union i2c_smbus_data data;
297
298 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
299 I2C_SMBUS_READ, command,
300 I2C_SMBUS_BYTE_DATA, &data))
301 return data.byte;
302 if (check)
303 v4l_err(client, "error reading %02x, %02x\n",
304 client->addr, command);
305 return -EIO;
306}
307
308static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
309{
310 int i;
311
312 for (i = 0; i < 3; i++) {
313 int ret = adv_smbus_read_byte_data_check(client, command, true);
314
315 if (ret >= 0) {
316 if (i)
317 v4l_err(client, "read ok after %d retries\n", i);
318 return ret;
319 }
320 }
321 v4l_err(client, "read failed\n");
322 return -EIO;
323}
324
325static s32 adv_smbus_write_byte_data(struct i2c_client *client,
326 u8 command, u8 value)
327{
328 union i2c_smbus_data data;
329 int err;
330 int i;
331
332 data.byte = value;
333 for (i = 0; i < 3; i++) {
334 err = i2c_smbus_xfer(client->adapter, client->addr,
335 client->flags,
336 I2C_SMBUS_WRITE, command,
337 I2C_SMBUS_BYTE_DATA, &data);
338 if (!err)
339 break;
340 }
341 if (err < 0)
342 v4l_err(client, "error writing %02x, %02x, %02x\n",
343 client->addr, command, value);
344 return err;
345}
346
347static void adv_smbus_write_byte_no_check(struct i2c_client *client,
348 u8 command, u8 value)
349{
350 union i2c_smbus_data data;
351 data.byte = value;
352
353 i2c_smbus_xfer(client->adapter, client->addr,
354 client->flags,
355 I2C_SMBUS_WRITE, command,
356 I2C_SMBUS_BYTE_DATA, &data);
357}
358
359static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
360 u8 command, unsigned length, const u8 *values)
361{
362 union i2c_smbus_data data;
363
364 if (length > I2C_SMBUS_BLOCK_MAX)
365 length = I2C_SMBUS_BLOCK_MAX;
366 data.block[0] = length;
367 memcpy(data.block + 1, values, length);
368 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
369 I2C_SMBUS_WRITE, command,
370 I2C_SMBUS_I2C_BLOCK_DATA, &data);
371}
372
373/* ----------------------------------------------------------------------- */
374
375static inline int io_read(struct v4l2_subdev *sd, u8 reg)
376{
377 struct i2c_client *client = v4l2_get_subdevdata(sd);
378
379 return adv_smbus_read_byte_data(client, reg);
380}
381
382static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
383{
384 struct i2c_client *client = v4l2_get_subdevdata(sd);
385
386 return adv_smbus_write_byte_data(client, reg, val);
387}
388
389static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
390{
391 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
392}
393
Hans Verkuilf888ae72015-05-01 11:31:30 -0300394static inline int io_write_clr_set(struct v4l2_subdev *sd,
395 u8 reg, u8 mask, u8 val)
396{
397 return io_write(sd, reg, (io_read(sd, reg) & ~mask) | val);
398}
399
Hans Verkuila89bcd42013-08-22 06:14:22 -0300400static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
401{
402 struct adv7842_state *state = to_state(sd);
403
404 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
405}
406
407static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
408{
409 struct adv7842_state *state = to_state(sd);
410
411 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
412}
413
414static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
415{
416 struct adv7842_state *state = to_state(sd);
417
418 return adv_smbus_read_byte_data(state->i2c_cec, reg);
419}
420
421static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
422{
423 struct adv7842_state *state = to_state(sd);
424
425 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
426}
427
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300428static inline int cec_write_clr_set(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
Hans Verkuila89bcd42013-08-22 06:14:22 -0300429{
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300430 return cec_write(sd, reg, (cec_read(sd, reg) & ~mask) | val);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300431}
432
433static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
434{
435 struct adv7842_state *state = to_state(sd);
436
437 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
438}
439
440static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
441{
442 struct adv7842_state *state = to_state(sd);
443
444 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
445}
446
447static inline int sdp_io_read(struct v4l2_subdev *sd, u8 reg)
448{
449 struct adv7842_state *state = to_state(sd);
450
451 return adv_smbus_read_byte_data(state->i2c_sdp_io, reg);
452}
453
454static inline int sdp_io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
455{
456 struct adv7842_state *state = to_state(sd);
457
458 return adv_smbus_write_byte_data(state->i2c_sdp_io, reg, val);
459}
460
461static inline int sdp_io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
462{
463 return sdp_io_write(sd, reg, (sdp_io_read(sd, reg) & mask) | val);
464}
465
466static inline int sdp_read(struct v4l2_subdev *sd, u8 reg)
467{
468 struct adv7842_state *state = to_state(sd);
469
470 return adv_smbus_read_byte_data(state->i2c_sdp, reg);
471}
472
473static inline int sdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
474{
475 struct adv7842_state *state = to_state(sd);
476
477 return adv_smbus_write_byte_data(state->i2c_sdp, reg, val);
478}
479
480static inline int sdp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
481{
482 return sdp_write(sd, reg, (sdp_read(sd, reg) & mask) | val);
483}
484
485static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
486{
487 struct adv7842_state *state = to_state(sd);
488
489 return adv_smbus_read_byte_data(state->i2c_afe, reg);
490}
491
492static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
493{
494 struct adv7842_state *state = to_state(sd);
495
496 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
497}
498
499static inline int afe_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
500{
501 return afe_write(sd, reg, (afe_read(sd, reg) & mask) | val);
502}
503
504static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
505{
506 struct adv7842_state *state = to_state(sd);
507
508 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
509}
510
511static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
512{
513 struct adv7842_state *state = to_state(sd);
514
515 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
516}
517
518static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
519{
520 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
521}
522
523static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
524{
525 struct adv7842_state *state = to_state(sd);
526
527 return adv_smbus_read_byte_data(state->i2c_edid, reg);
528}
529
530static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
531{
532 struct adv7842_state *state = to_state(sd);
533
534 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
535}
536
537static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
538{
539 struct adv7842_state *state = to_state(sd);
540
541 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
542}
543
544static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
545{
546 struct adv7842_state *state = to_state(sd);
547
548 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
549}
550
Mats Randgaard5b64b202013-12-05 12:08:45 -0300551static inline int hdmi_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
552{
553 return hdmi_write(sd, reg, (hdmi_read(sd, reg) & mask) | val);
554}
555
Hans Verkuila89bcd42013-08-22 06:14:22 -0300556static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
557{
558 struct adv7842_state *state = to_state(sd);
559
560 return adv_smbus_read_byte_data(state->i2c_cp, reg);
561}
562
563static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
564{
565 struct adv7842_state *state = to_state(sd);
566
567 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
568}
569
570static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
571{
572 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
573}
574
575static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
576{
577 struct adv7842_state *state = to_state(sd);
578
579 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
580}
581
582static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
583{
584 struct adv7842_state *state = to_state(sd);
585
586 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
587}
588
589static void main_reset(struct v4l2_subdev *sd)
590{
591 struct i2c_client *client = v4l2_get_subdevdata(sd);
592
593 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
594
595 adv_smbus_write_byte_no_check(client, 0xff, 0x80);
596
Martin Bugge84aeed52013-12-05 11:56:32 -0300597 mdelay(5);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300598}
599
Hans Verkuilf888ae72015-05-01 11:31:30 -0300600/* -----------------------------------------------------------------------------
601 * Format helpers
602 */
603
604static const struct adv7842_format_info adv7842_formats[] = {
605 { MEDIA_BUS_FMT_RGB888_1X24, ADV7842_OP_CH_SEL_RGB, true, false,
606 ADV7842_OP_MODE_SEL_SDR_444 | ADV7842_OP_FORMAT_SEL_8BIT },
607 { MEDIA_BUS_FMT_YUYV8_2X8, ADV7842_OP_CH_SEL_RGB, false, false,
608 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
609 { MEDIA_BUS_FMT_YVYU8_2X8, ADV7842_OP_CH_SEL_RGB, false, true,
610 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_8BIT },
611 { MEDIA_BUS_FMT_YUYV10_2X10, ADV7842_OP_CH_SEL_RGB, false, false,
612 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
613 { MEDIA_BUS_FMT_YVYU10_2X10, ADV7842_OP_CH_SEL_RGB, false, true,
614 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_10BIT },
615 { MEDIA_BUS_FMT_YUYV12_2X12, ADV7842_OP_CH_SEL_RGB, false, false,
616 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
617 { MEDIA_BUS_FMT_YVYU12_2X12, ADV7842_OP_CH_SEL_RGB, false, true,
618 ADV7842_OP_MODE_SEL_SDR_422 | ADV7842_OP_FORMAT_SEL_12BIT },
619 { MEDIA_BUS_FMT_UYVY8_1X16, ADV7842_OP_CH_SEL_RBG, false, false,
620 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
621 { MEDIA_BUS_FMT_VYUY8_1X16, ADV7842_OP_CH_SEL_RBG, false, true,
622 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
623 { MEDIA_BUS_FMT_YUYV8_1X16, ADV7842_OP_CH_SEL_RGB, false, false,
624 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
625 { MEDIA_BUS_FMT_YVYU8_1X16, ADV7842_OP_CH_SEL_RGB, false, true,
626 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_8BIT },
627 { MEDIA_BUS_FMT_UYVY10_1X20, ADV7842_OP_CH_SEL_RBG, false, false,
628 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
629 { MEDIA_BUS_FMT_VYUY10_1X20, ADV7842_OP_CH_SEL_RBG, false, true,
630 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
631 { MEDIA_BUS_FMT_YUYV10_1X20, ADV7842_OP_CH_SEL_RGB, false, false,
632 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
633 { MEDIA_BUS_FMT_YVYU10_1X20, ADV7842_OP_CH_SEL_RGB, false, true,
634 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_10BIT },
635 { MEDIA_BUS_FMT_UYVY12_1X24, ADV7842_OP_CH_SEL_RBG, false, false,
636 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
637 { MEDIA_BUS_FMT_VYUY12_1X24, ADV7842_OP_CH_SEL_RBG, false, true,
638 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
639 { MEDIA_BUS_FMT_YUYV12_1X24, ADV7842_OP_CH_SEL_RGB, false, false,
640 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
641 { MEDIA_BUS_FMT_YVYU12_1X24, ADV7842_OP_CH_SEL_RGB, false, true,
642 ADV7842_OP_MODE_SEL_SDR_422_2X | ADV7842_OP_FORMAT_SEL_12BIT },
643};
644
645static const struct adv7842_format_info *
646adv7842_format_info(struct adv7842_state *state, u32 code)
647{
648 unsigned int i;
649
650 for (i = 0; i < ARRAY_SIZE(adv7842_formats); ++i) {
651 if (adv7842_formats[i].code == code)
652 return &adv7842_formats[i];
653 }
654
655 return NULL;
656}
657
Hans Verkuila89bcd42013-08-22 06:14:22 -0300658/* ----------------------------------------------------------------------- */
659
Martin Bugge933913d2014-01-24 10:50:03 -0300660static inline bool is_analog_input(struct v4l2_subdev *sd)
661{
662 struct adv7842_state *state = to_state(sd);
663
664 return ((state->mode == ADV7842_MODE_RGB) ||
665 (state->mode == ADV7842_MODE_COMP));
666}
667
Hans Verkuila89bcd42013-08-22 06:14:22 -0300668static inline bool is_digital_input(struct v4l2_subdev *sd)
669{
670 struct adv7842_state *state = to_state(sd);
671
672 return state->mode == ADV7842_MODE_HDMI;
673}
674
675static const struct v4l2_dv_timings_cap adv7842_timings_cap_analog = {
676 .type = V4L2_DV_BT_656_1120,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300677 /* keep this initialization for compatibility with GCC < 4.4.6 */
678 .reserved = { 0 },
Hans Verkuilcdde22f2018-11-08 04:51:51 -0500679 V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 170000000,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300680 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
Hans Verkuila89bcd42013-08-22 06:14:22 -0300681 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300682 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
683 V4L2_DV_BT_CAP_CUSTOM)
Hans Verkuila89bcd42013-08-22 06:14:22 -0300684};
685
686static const struct v4l2_dv_timings_cap adv7842_timings_cap_digital = {
687 .type = V4L2_DV_BT_656_1120,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300688 /* keep this initialization for compatibility with GCC < 4.4.6 */
689 .reserved = { 0 },
Hans Verkuilcdde22f2018-11-08 04:51:51 -0500690 V4L2_INIT_BT_TIMINGS(640, 1920, 350, 1200, 25000000, 225000000,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300691 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
Hans Verkuila89bcd42013-08-22 06:14:22 -0300692 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT,
Gianluca Gennari9b51f172013-08-30 08:29:22 -0300693 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_REDUCED_BLANKING |
694 V4L2_DV_BT_CAP_CUSTOM)
Hans Verkuila89bcd42013-08-22 06:14:22 -0300695};
696
697static inline const struct v4l2_dv_timings_cap *
698adv7842_get_dv_timings_cap(struct v4l2_subdev *sd)
699{
700 return is_digital_input(sd) ? &adv7842_timings_cap_digital :
701 &adv7842_timings_cap_analog;
702}
703
704/* ----------------------------------------------------------------------- */
705
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300706static u16 adv7842_read_cable_det(struct v4l2_subdev *sd)
707{
708 u8 reg = io_read(sd, 0x6f);
709 u16 val = 0;
710
711 if (reg & 0x02)
712 val |= 1; /* port A */
713 if (reg & 0x01)
714 val |= 2; /* port B */
715 return val;
716}
717
Hans Verkuila89bcd42013-08-22 06:14:22 -0300718static void adv7842_delayed_work_enable_hotplug(struct work_struct *work)
719{
720 struct delayed_work *dwork = to_delayed_work(work);
721 struct adv7842_state *state = container_of(dwork,
722 struct adv7842_state, delayed_work_enable_hotplug);
723 struct v4l2_subdev *sd = &state->sd;
724 int present = state->hdmi_edid.present;
725 u8 mask = 0;
726
727 v4l2_dbg(2, debug, sd, "%s: enable hotplug on ports: 0x%x\n",
728 __func__, present);
729
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300730 if (present & (0x04 << ADV7842_EDID_PORT_A))
731 mask |= 0x20;
732 if (present & (0x04 << ADV7842_EDID_PORT_B))
733 mask |= 0x10;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300734 io_write_and_or(sd, 0x20, 0xcf, mask);
735}
736
737static int edid_write_vga_segment(struct v4l2_subdev *sd)
738{
739 struct i2c_client *client = v4l2_get_subdevdata(sd);
740 struct adv7842_state *state = to_state(sd);
741 const u8 *val = state->vga_edid.edid;
742 int err = 0;
743 int i;
744
745 v4l2_dbg(2, debug, sd, "%s: write EDID on VGA port\n", __func__);
746
747 /* HPA disable on port A and B */
748 io_write_and_or(sd, 0x20, 0xcf, 0x00);
749
750 /* Disable I2C access to internal EDID ram from VGA DDC port */
751 rep_write_and_or(sd, 0x7f, 0x7f, 0x00);
752
753 /* edid segment pointer '1' for VGA port */
754 rep_write_and_or(sd, 0x77, 0xef, 0x10);
755
756 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
757 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
758 I2C_SMBUS_BLOCK_MAX, val + i);
759 if (err)
760 return err;
761
762 /* Calculates the checksums and enables I2C access
763 * to internal EDID ram from VGA DDC port.
764 */
765 rep_write_and_or(sd, 0x7f, 0x7f, 0x80);
766
767 for (i = 0; i < 1000; i++) {
768 if (rep_read(sd, 0x79) & 0x20)
769 break;
770 mdelay(1);
771 }
772 if (i == 1000) {
773 v4l_err(client, "error enabling edid on VGA port\n");
774 return -EIO;
775 }
776
777 /* enable hotplug after 200 ms */
Bhaktipriya Shridhar1d3e1542016-07-02 07:37:22 -0300778 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300779
780 return 0;
781}
782
Hans Verkuila89bcd42013-08-22 06:14:22 -0300783static int edid_write_hdmi_segment(struct v4l2_subdev *sd, u8 port)
784{
785 struct i2c_client *client = v4l2_get_subdevdata(sd);
786 struct adv7842_state *state = to_state(sd);
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300787 const u8 *edid = state->hdmi_edid.edid;
788 int spa_loc;
789 u16 pa;
Hans Verkuila89bcd42013-08-22 06:14:22 -0300790 int err = 0;
791 int i;
792
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300793 v4l2_dbg(2, debug, sd, "%s: write EDID on port %c\n",
794 __func__, (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
Hans Verkuila89bcd42013-08-22 06:14:22 -0300795
796 /* HPA disable on port A and B */
797 io_write_and_or(sd, 0x20, 0xcf, 0x00);
798
799 /* Disable I2C access to internal EDID ram from HDMI DDC ports */
800 rep_write_and_or(sd, 0x77, 0xf3, 0x00);
801
Hans Verkuil2c0ffc92018-10-04 03:58:34 -0400802 if (!state->hdmi_edid.present) {
803 cec_phys_addr_invalidate(state->cec_adap);
Martin Buggefc2e9912013-12-05 12:09:51 -0300804 return 0;
Hans Verkuil2c0ffc92018-10-04 03:58:34 -0400805 }
Martin Buggefc2e9912013-12-05 12:09:51 -0300806
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300807 pa = cec_get_edid_phys_addr(edid, 256, &spa_loc);
808 err = cec_phys_addr_validate(pa, &pa, NULL);
809 if (err)
810 return err;
811
812 /*
813 * Return an error if no location of the source physical address
814 * was found.
815 */
816 if (spa_loc == 0)
817 return -EINVAL;
818
Hans Verkuila89bcd42013-08-22 06:14:22 -0300819 /* edid segment pointer '0' for HDMI ports */
820 rep_write_and_or(sd, 0x77, 0xef, 0x00);
821
822 for (i = 0; !err && i < 256; i += I2C_SMBUS_BLOCK_MAX)
823 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300824 I2C_SMBUS_BLOCK_MAX, edid + i);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300825 if (err)
826 return err;
827
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300828 if (port == ADV7842_EDID_PORT_A) {
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300829 rep_write(sd, 0x72, edid[spa_loc]);
830 rep_write(sd, 0x73, edid[spa_loc + 1]);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300831 } else {
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300832 rep_write(sd, 0x74, edid[spa_loc]);
833 rep_write(sd, 0x75, edid[spa_loc + 1]);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300834 }
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300835 rep_write(sd, 0x76, spa_loc & 0xff);
836 rep_write_and_or(sd, 0x77, 0xbf, (spa_loc >> 2) & 0x40);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300837
838 /* Calculates the checksums and enables I2C access to internal
839 * EDID ram from HDMI DDC ports
840 */
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300841 rep_write_and_or(sd, 0x77, 0xf3, state->hdmi_edid.present);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300842
843 for (i = 0; i < 1000; i++) {
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300844 if (rep_read(sd, 0x7d) & state->hdmi_edid.present)
Hans Verkuila89bcd42013-08-22 06:14:22 -0300845 break;
846 mdelay(1);
847 }
848 if (i == 1000) {
Mats Randgaard7de6fab2013-12-10 11:24:35 -0300849 v4l_err(client, "error enabling edid on port %c\n",
850 (port == ADV7842_EDID_PORT_A) ? 'A' : 'B');
Hans Verkuila89bcd42013-08-22 06:14:22 -0300851 return -EIO;
852 }
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300853 cec_s_phys_addr(state->cec_adap, pa, false);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300854
855 /* enable hotplug after 200 ms */
Bhaktipriya Shridhar1d3e1542016-07-02 07:37:22 -0300856 schedule_delayed_work(&state->delayed_work_enable_hotplug, HZ / 5);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300857
858 return 0;
859}
860
861/* ----------------------------------------------------------------------- */
862
863#ifdef CONFIG_VIDEO_ADV_DEBUG
864static void adv7842_inv_register(struct v4l2_subdev *sd)
865{
866 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
867 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
868 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
869 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
870 v4l2_info(sd, "0x400-0x4ff: SDP_IO Map\n");
871 v4l2_info(sd, "0x500-0x5ff: SDP Map\n");
872 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
873 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
874 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
875 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
876 v4l2_info(sd, "0xa00-0xaff: CP Map\n");
877 v4l2_info(sd, "0xb00-0xbff: VDP Map\n");
878}
879
880static int adv7842_g_register(struct v4l2_subdev *sd,
881 struct v4l2_dbg_register *reg)
882{
883 reg->size = 1;
884 switch (reg->reg >> 8) {
885 case 0:
886 reg->val = io_read(sd, reg->reg & 0xff);
887 break;
888 case 1:
889 reg->val = avlink_read(sd, reg->reg & 0xff);
890 break;
891 case 2:
892 reg->val = cec_read(sd, reg->reg & 0xff);
893 break;
894 case 3:
895 reg->val = infoframe_read(sd, reg->reg & 0xff);
896 break;
897 case 4:
898 reg->val = sdp_io_read(sd, reg->reg & 0xff);
899 break;
900 case 5:
901 reg->val = sdp_read(sd, reg->reg & 0xff);
902 break;
903 case 6:
904 reg->val = afe_read(sd, reg->reg & 0xff);
905 break;
906 case 7:
907 reg->val = rep_read(sd, reg->reg & 0xff);
908 break;
909 case 8:
910 reg->val = edid_read(sd, reg->reg & 0xff);
911 break;
912 case 9:
913 reg->val = hdmi_read(sd, reg->reg & 0xff);
914 break;
915 case 0xa:
916 reg->val = cp_read(sd, reg->reg & 0xff);
917 break;
918 case 0xb:
919 reg->val = vdp_read(sd, reg->reg & 0xff);
920 break;
921 default:
922 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
923 adv7842_inv_register(sd);
924 break;
925 }
926 return 0;
927}
928
929static int adv7842_s_register(struct v4l2_subdev *sd,
930 const struct v4l2_dbg_register *reg)
931{
932 u8 val = reg->val & 0xff;
933
934 switch (reg->reg >> 8) {
935 case 0:
936 io_write(sd, reg->reg & 0xff, val);
937 break;
938 case 1:
939 avlink_write(sd, reg->reg & 0xff, val);
940 break;
941 case 2:
942 cec_write(sd, reg->reg & 0xff, val);
943 break;
944 case 3:
945 infoframe_write(sd, reg->reg & 0xff, val);
946 break;
947 case 4:
948 sdp_io_write(sd, reg->reg & 0xff, val);
949 break;
950 case 5:
951 sdp_write(sd, reg->reg & 0xff, val);
952 break;
953 case 6:
954 afe_write(sd, reg->reg & 0xff, val);
955 break;
956 case 7:
957 rep_write(sd, reg->reg & 0xff, val);
958 break;
959 case 8:
960 edid_write(sd, reg->reg & 0xff, val);
961 break;
962 case 9:
963 hdmi_write(sd, reg->reg & 0xff, val);
964 break;
965 case 0xa:
966 cp_write(sd, reg->reg & 0xff, val);
967 break;
968 case 0xb:
969 vdp_write(sd, reg->reg & 0xff, val);
970 break;
971 default:
972 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
973 adv7842_inv_register(sd);
974 break;
975 }
976 return 0;
977}
978#endif
979
980static int adv7842_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
981{
982 struct adv7842_state *state = to_state(sd);
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300983 u16 cable_det = adv7842_read_cable_det(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300984
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300985 v4l2_dbg(1, debug, sd, "%s: 0x%x\n", __func__, cable_det);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300986
Hans Verkuil25c84fb2015-09-07 08:13:26 -0300987 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl, cable_det);
Hans Verkuila89bcd42013-08-22 06:14:22 -0300988}
989
990static int find_and_set_predefined_video_timings(struct v4l2_subdev *sd,
991 u8 prim_mode,
992 const struct adv7842_video_standards *predef_vid_timings,
993 const struct v4l2_dv_timings *timings)
994{
995 int i;
996
997 for (i = 0; predef_vid_timings[i].timings.bt.width; i++) {
998 if (!v4l2_match_dv_timings(timings, &predef_vid_timings[i].timings,
Hans Verkuil85f9e062015-11-13 09:46:26 -0200999 is_digital_input(sd) ? 250000 : 1000000, false))
Hans Verkuila89bcd42013-08-22 06:14:22 -03001000 continue;
1001 /* video std */
1002 io_write(sd, 0x00, predef_vid_timings[i].vid_std);
1003 /* v_freq and prim mode */
1004 io_write(sd, 0x01, (predef_vid_timings[i].v_freq << 4) + prim_mode);
1005 return 0;
1006 }
1007
1008 return -1;
1009}
1010
1011static int configure_predefined_video_timings(struct v4l2_subdev *sd,
1012 struct v4l2_dv_timings *timings)
1013{
1014 struct adv7842_state *state = to_state(sd);
1015 int err;
1016
1017 v4l2_dbg(1, debug, sd, "%s\n", __func__);
1018
1019 /* reset to default values */
1020 io_write(sd, 0x16, 0x43);
1021 io_write(sd, 0x17, 0x5a);
1022 /* disable embedded syncs for auto graphics mode */
1023 cp_write_and_or(sd, 0x81, 0xef, 0x00);
1024 cp_write(sd, 0x26, 0x00);
1025 cp_write(sd, 0x27, 0x00);
1026 cp_write(sd, 0x28, 0x00);
1027 cp_write(sd, 0x29, 0x00);
Martin Bugge6251e652013-12-10 11:01:00 -03001028 cp_write(sd, 0x8f, 0x40);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001029 cp_write(sd, 0x90, 0x00);
1030 cp_write(sd, 0xa5, 0x00);
1031 cp_write(sd, 0xa6, 0x00);
1032 cp_write(sd, 0xa7, 0x00);
1033 cp_write(sd, 0xab, 0x00);
1034 cp_write(sd, 0xac, 0x00);
1035
1036 switch (state->mode) {
1037 case ADV7842_MODE_COMP:
1038 case ADV7842_MODE_RGB:
1039 err = find_and_set_predefined_video_timings(sd,
1040 0x01, adv7842_prim_mode_comp, timings);
1041 if (err)
1042 err = find_and_set_predefined_video_timings(sd,
1043 0x02, adv7842_prim_mode_gr, timings);
1044 break;
1045 case ADV7842_MODE_HDMI:
1046 err = find_and_set_predefined_video_timings(sd,
1047 0x05, adv7842_prim_mode_hdmi_comp, timings);
1048 if (err)
1049 err = find_and_set_predefined_video_timings(sd,
1050 0x06, adv7842_prim_mode_hdmi_gr, timings);
1051 break;
1052 default:
1053 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1054 __func__, state->mode);
1055 err = -1;
1056 break;
1057 }
1058
1059
1060 return err;
1061}
1062
1063static void configure_custom_video_timings(struct v4l2_subdev *sd,
1064 const struct v4l2_bt_timings *bt)
1065{
1066 struct adv7842_state *state = to_state(sd);
1067 struct i2c_client *client = v4l2_get_subdevdata(sd);
1068 u32 width = htotal(bt);
1069 u32 height = vtotal(bt);
1070 u16 cp_start_sav = bt->hsync + bt->hbackporch - 4;
1071 u16 cp_start_eav = width - bt->hfrontporch;
1072 u16 cp_start_vbi = height - bt->vfrontporch + 1;
1073 u16 cp_end_vbi = bt->vsync + bt->vbackporch + 1;
1074 u16 ch1_fr_ll = (((u32)bt->pixelclock / 100) > 0) ?
1075 ((width * (ADV7842_fsc / 100)) / ((u32)bt->pixelclock / 100)) : 0;
1076 const u8 pll[2] = {
1077 0xc0 | ((width >> 8) & 0x1f),
1078 width & 0xff
1079 };
1080
1081 v4l2_dbg(2, debug, sd, "%s\n", __func__);
1082
1083 switch (state->mode) {
1084 case ADV7842_MODE_COMP:
1085 case ADV7842_MODE_RGB:
1086 /* auto graphics */
1087 io_write(sd, 0x00, 0x07); /* video std */
1088 io_write(sd, 0x01, 0x02); /* prim mode */
1089 /* enable embedded syncs for auto graphics mode */
1090 cp_write_and_or(sd, 0x81, 0xef, 0x10);
1091
1092 /* Should only be set in auto-graphics mode [REF_02, p. 91-92] */
1093 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
1094 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
1095 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
1096 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
1097 break;
1098 }
1099
1100 /* active video - horizontal timing */
1101 cp_write(sd, 0x26, (cp_start_sav >> 8) & 0xf);
1102 cp_write(sd, 0x27, (cp_start_sav & 0xff));
1103 cp_write(sd, 0x28, (cp_start_eav >> 8) & 0xf);
1104 cp_write(sd, 0x29, (cp_start_eav & 0xff));
1105
1106 /* active video - vertical timing */
1107 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
1108 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) |
1109 ((cp_end_vbi >> 8) & 0xf));
1110 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
1111 break;
1112 case ADV7842_MODE_HDMI:
1113 /* set default prim_mode/vid_std for HDMI
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -03001114 according to [REF_03, c. 4.2] */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001115 io_write(sd, 0x00, 0x02); /* video std */
1116 io_write(sd, 0x01, 0x06); /* prim mode */
1117 break;
1118 default:
1119 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1120 __func__, state->mode);
1121 break;
1122 }
1123
1124 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7);
1125 cp_write(sd, 0x90, ch1_fr_ll & 0xff);
1126 cp_write(sd, 0xab, (height >> 4) & 0xff);
1127 cp_write(sd, 0xac, (height & 0x0f) << 4);
1128}
1129
Martin Bugge933913d2014-01-24 10:50:03 -03001130static void adv7842_set_offset(struct v4l2_subdev *sd, bool auto_offset, u16 offset_a, u16 offset_b, u16 offset_c)
1131{
1132 struct adv7842_state *state = to_state(sd);
1133 u8 offset_buf[4];
1134
1135 if (auto_offset) {
1136 offset_a = 0x3ff;
1137 offset_b = 0x3ff;
1138 offset_c = 0x3ff;
1139 }
1140
1141 v4l2_dbg(2, debug, sd, "%s: %s offset: a = 0x%x, b = 0x%x, c = 0x%x\n",
1142 __func__, auto_offset ? "Auto" : "Manual",
1143 offset_a, offset_b, offset_c);
1144
1145 offset_buf[0]= (cp_read(sd, 0x77) & 0xc0) | ((offset_a & 0x3f0) >> 4);
1146 offset_buf[1] = ((offset_a & 0x00f) << 4) | ((offset_b & 0x3c0) >> 6);
1147 offset_buf[2] = ((offset_b & 0x03f) << 2) | ((offset_c & 0x300) >> 8);
1148 offset_buf[3] = offset_c & 0x0ff;
1149
1150 /* Registers must be written in this order with no i2c access in between */
1151 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x77, 4, offset_buf))
1152 v4l2_err(sd, "%s: i2c error writing to CP reg 0x77, 0x78, 0x79, 0x7a\n", __func__);
1153}
1154
1155static void adv7842_set_gain(struct v4l2_subdev *sd, bool auto_gain, u16 gain_a, u16 gain_b, u16 gain_c)
1156{
1157 struct adv7842_state *state = to_state(sd);
1158 u8 gain_buf[4];
1159 u8 gain_man = 1;
1160 u8 agc_mode_man = 1;
1161
1162 if (auto_gain) {
1163 gain_man = 0;
1164 agc_mode_man = 0;
1165 gain_a = 0x100;
1166 gain_b = 0x100;
1167 gain_c = 0x100;
1168 }
1169
1170 v4l2_dbg(2, debug, sd, "%s: %s gain: a = 0x%x, b = 0x%x, c = 0x%x\n",
1171 __func__, auto_gain ? "Auto" : "Manual",
1172 gain_a, gain_b, gain_c);
1173
1174 gain_buf[0] = ((gain_man << 7) | (agc_mode_man << 6) | ((gain_a & 0x3f0) >> 4));
1175 gain_buf[1] = (((gain_a & 0x00f) << 4) | ((gain_b & 0x3c0) >> 6));
1176 gain_buf[2] = (((gain_b & 0x03f) << 2) | ((gain_c & 0x300) >> 8));
1177 gain_buf[3] = ((gain_c & 0x0ff));
1178
1179 /* Registers must be written in this order with no i2c access in between */
1180 if (adv_smbus_write_i2c_block_data(state->i2c_cp, 0x73, 4, gain_buf))
1181 v4l2_err(sd, "%s: i2c error writing to CP reg 0x73, 0x74, 0x75, 0x76\n", __func__);
1182}
1183
Hans Verkuila89bcd42013-08-22 06:14:22 -03001184static void set_rgb_quantization_range(struct v4l2_subdev *sd)
1185{
1186 struct adv7842_state *state = to_state(sd);
Martin Bugge933913d2014-01-24 10:50:03 -03001187 bool rgb_output = io_read(sd, 0x02) & 0x02;
1188 bool hdmi_signal = hdmi_read(sd, 0x05) & 0x80;
Hans Verkuilfd742462016-06-28 11:43:01 -03001189 u8 y = HDMI_COLORSPACE_RGB;
1190
1191 if (hdmi_signal && (io_read(sd, 0x60) & 1))
1192 y = infoframe_read(sd, 0x01) >> 5;
Hans Verkuila89bcd42013-08-22 06:14:22 -03001193
Martin Bugge933913d2014-01-24 10:50:03 -03001194 v4l2_dbg(2, debug, sd, "%s: RGB quantization range: %d, RGB out: %d, HDMI: %d\n",
1195 __func__, state->rgb_quantization_range,
1196 rgb_output, hdmi_signal);
1197
1198 adv7842_set_gain(sd, true, 0x0, 0x0, 0x0);
1199 adv7842_set_offset(sd, true, 0x0, 0x0, 0x0);
Hans Verkuilfd742462016-06-28 11:43:01 -03001200 io_write_clr_set(sd, 0x02, 0x04, rgb_output ? 0 : 4);
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001201
Hans Verkuila89bcd42013-08-22 06:14:22 -03001202 switch (state->rgb_quantization_range) {
1203 case V4L2_DV_RGB_RANGE_AUTO:
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001204 if (state->mode == ADV7842_MODE_RGB) {
1205 /* Receiving analog RGB signal
1206 * Set RGB full range (0-255) */
1207 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1208 break;
1209 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03001210
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001211 if (state->mode == ADV7842_MODE_COMP) {
1212 /* Receiving analog YPbPr signal
1213 * Set automode */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001214 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001215 break;
1216 }
1217
Martin Bugge933913d2014-01-24 10:50:03 -03001218 if (hdmi_signal) {
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001219 /* Receiving HDMI signal
1220 * Set automode */
1221 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1222 break;
1223 }
1224
1225 /* Receiving DVI-D signal
1226 * ADV7842 selects RGB limited range regardless of
1227 * input format (CE/IT) in automatic mode */
Hans Verkuil680fee02015-03-20 14:05:05 -03001228 if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) {
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001229 /* RGB limited range (16-235) */
1230 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1231 } else {
1232 /* RGB full range (0-255) */
1233 io_write_and_or(sd, 0x02, 0x0f, 0x10);
Martin Bugge933913d2014-01-24 10:50:03 -03001234
1235 if (is_digital_input(sd) && rgb_output) {
1236 adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
1237 } else {
1238 adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1239 adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
1240 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03001241 }
1242 break;
1243 case V4L2_DV_RGB_RANGE_LIMITED:
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001244 if (state->mode == ADV7842_MODE_COMP) {
1245 /* YCrCb limited range (16-235) */
1246 io_write_and_or(sd, 0x02, 0x0f, 0x20);
Martin Bugge933913d2014-01-24 10:50:03 -03001247 break;
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001248 }
Martin Bugge933913d2014-01-24 10:50:03 -03001249
Hans Verkuilfd742462016-06-28 11:43:01 -03001250 if (y != HDMI_COLORSPACE_RGB)
1251 break;
1252
Martin Bugge933913d2014-01-24 10:50:03 -03001253 /* RGB limited range (16-235) */
1254 io_write_and_or(sd, 0x02, 0x0f, 0x00);
1255
Hans Verkuila89bcd42013-08-22 06:14:22 -03001256 break;
1257 case V4L2_DV_RGB_RANGE_FULL:
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001258 if (state->mode == ADV7842_MODE_COMP) {
1259 /* YCrCb full range (0-255) */
1260 io_write_and_or(sd, 0x02, 0x0f, 0x60);
Martin Bugge933913d2014-01-24 10:50:03 -03001261 break;
1262 }
1263
Hans Verkuilfd742462016-06-28 11:43:01 -03001264 if (y != HDMI_COLORSPACE_RGB)
1265 break;
1266
Martin Bugge933913d2014-01-24 10:50:03 -03001267 /* RGB full range (0-255) */
1268 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1269
1270 if (is_analog_input(sd) || hdmi_signal)
1271 break;
1272
1273 /* Adjust gain/offset for DVI-D signals only */
1274 if (rgb_output) {
1275 adv7842_set_offset(sd, false, 0x40, 0x40, 0x40);
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001276 } else {
Martin Bugge933913d2014-01-24 10:50:03 -03001277 adv7842_set_gain(sd, false, 0xe0, 0xe0, 0xe0);
1278 adv7842_set_offset(sd, false, 0x70, 0x70, 0x70);
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001279 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03001280 break;
1281 }
1282}
1283
1284static int adv7842_s_ctrl(struct v4l2_ctrl *ctrl)
1285{
1286 struct v4l2_subdev *sd = to_sd(ctrl);
1287 struct adv7842_state *state = to_state(sd);
1288
1289 /* TODO SDP ctrls
1290 contrast/brightness/hue/free run is acting a bit strange,
1291 not sure if sdp csc is correct.
1292 */
1293 switch (ctrl->id) {
1294 /* standard ctrls */
1295 case V4L2_CID_BRIGHTNESS:
1296 cp_write(sd, 0x3c, ctrl->val);
1297 sdp_write(sd, 0x14, ctrl->val);
1298 /* ignore lsb sdp 0x17[3:2] */
1299 return 0;
1300 case V4L2_CID_CONTRAST:
1301 cp_write(sd, 0x3a, ctrl->val);
1302 sdp_write(sd, 0x13, ctrl->val);
1303 /* ignore lsb sdp 0x17[1:0] */
1304 return 0;
1305 case V4L2_CID_SATURATION:
1306 cp_write(sd, 0x3b, ctrl->val);
1307 sdp_write(sd, 0x15, ctrl->val);
1308 /* ignore lsb sdp 0x17[5:4] */
1309 return 0;
1310 case V4L2_CID_HUE:
1311 cp_write(sd, 0x3d, ctrl->val);
1312 sdp_write(sd, 0x16, ctrl->val);
1313 /* ignore lsb sdp 0x17[7:6] */
1314 return 0;
1315 /* custom ctrls */
1316 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
1317 afe_write(sd, 0xc8, ctrl->val);
1318 return 0;
1319 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
1320 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
1321 sdp_write_and_or(sd, 0xdd, ~0x04, (ctrl->val << 2));
1322 return 0;
1323 case V4L2_CID_ADV_RX_FREE_RUN_COLOR: {
1324 u8 R = (ctrl->val & 0xff0000) >> 16;
1325 u8 G = (ctrl->val & 0x00ff00) >> 8;
1326 u8 B = (ctrl->val & 0x0000ff);
1327 /* RGB -> YUV, numerical approximation */
1328 int Y = 66 * R + 129 * G + 25 * B;
1329 int U = -38 * R - 74 * G + 112 * B;
1330 int V = 112 * R - 94 * G - 18 * B;
1331
1332 /* Scale down to 8 bits with rounding */
1333 Y = (Y + 128) >> 8;
1334 U = (U + 128) >> 8;
1335 V = (V + 128) >> 8;
1336 /* make U,V positive */
1337 Y += 16;
1338 U += 128;
1339 V += 128;
1340
1341 v4l2_dbg(1, debug, sd, "R %x, G %x, B %x\n", R, G, B);
1342 v4l2_dbg(1, debug, sd, "Y %x, U %x, V %x\n", Y, U, V);
1343
1344 /* CP */
1345 cp_write(sd, 0xc1, R);
1346 cp_write(sd, 0xc0, G);
1347 cp_write(sd, 0xc2, B);
1348 /* SDP */
1349 sdp_write(sd, 0xde, Y);
1350 sdp_write(sd, 0xdf, (V & 0xf0) | ((U >> 4) & 0x0f));
1351 return 0;
1352 }
1353 case V4L2_CID_DV_RX_RGB_RANGE:
1354 state->rgb_quantization_range = ctrl->val;
1355 set_rgb_quantization_range(sd);
1356 return 0;
1357 }
1358 return -EINVAL;
1359}
1360
Hans Verkuile8979272016-01-27 11:31:42 -02001361static int adv7842_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1362{
1363 struct v4l2_subdev *sd = to_sd(ctrl);
1364
1365 if (ctrl->id == V4L2_CID_DV_RX_IT_CONTENT_TYPE) {
1366 ctrl->val = V4L2_DV_IT_CONTENT_TYPE_NO_ITC;
1367 if ((io_read(sd, 0x60) & 1) && (infoframe_read(sd, 0x03) & 0x80))
1368 ctrl->val = (infoframe_read(sd, 0x05) >> 4) & 3;
1369 return 0;
1370 }
1371 return -EINVAL;
1372}
1373
Hans Verkuila89bcd42013-08-22 06:14:22 -03001374static inline bool no_power(struct v4l2_subdev *sd)
1375{
1376 return io_read(sd, 0x0c) & 0x24;
1377}
1378
1379static inline bool no_cp_signal(struct v4l2_subdev *sd)
1380{
1381 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0) || !(cp_read(sd, 0xb1) & 0x80);
1382}
1383
1384static inline bool is_hdmi(struct v4l2_subdev *sd)
1385{
1386 return hdmi_read(sd, 0x05) & 0x80;
1387}
1388
1389static int adv7842_g_input_status(struct v4l2_subdev *sd, u32 *status)
1390{
1391 struct adv7842_state *state = to_state(sd);
1392
1393 *status = 0;
1394
1395 if (io_read(sd, 0x0c) & 0x24)
1396 *status |= V4L2_IN_ST_NO_POWER;
1397
1398 if (state->mode == ADV7842_MODE_SDP) {
1399 /* status from SDP block */
1400 if (!(sdp_read(sd, 0x5A) & 0x01))
1401 *status |= V4L2_IN_ST_NO_SIGNAL;
1402
1403 v4l2_dbg(1, debug, sd, "%s: SDP status = 0x%x\n",
1404 __func__, *status);
1405 return 0;
1406 }
1407 /* status from CP block */
1408 if ((cp_read(sd, 0xb5) & 0xd0) != 0xd0 ||
1409 !(cp_read(sd, 0xb1) & 0x80))
1410 /* TODO channel 2 */
1411 *status |= V4L2_IN_ST_NO_SIGNAL;
1412
1413 if (is_digital_input(sd) && ((io_read(sd, 0x74) & 0x03) != 0x03))
1414 *status |= V4L2_IN_ST_NO_SIGNAL;
1415
1416 v4l2_dbg(1, debug, sd, "%s: CP status = 0x%x\n",
1417 __func__, *status);
1418
1419 return 0;
1420}
1421
1422struct stdi_readback {
1423 u16 bl, lcf, lcvs;
1424 u8 hs_pol, vs_pol;
1425 bool interlaced;
1426};
1427
1428static int stdi2dv_timings(struct v4l2_subdev *sd,
1429 struct stdi_readback *stdi,
1430 struct v4l2_dv_timings *timings)
1431{
1432 struct adv7842_state *state = to_state(sd);
1433 u32 hfreq = (ADV7842_fsc * 8) / stdi->bl;
1434 u32 pix_clk;
1435 int i;
1436
1437 for (i = 0; v4l2_dv_timings_presets[i].bt.width; i++) {
1438 const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1439
1440 if (!v4l2_valid_dv_timings(&v4l2_dv_timings_presets[i],
1441 adv7842_get_dv_timings_cap(sd),
1442 adv7842_check_dv_timings, NULL))
1443 continue;
1444 if (vtotal(bt) != stdi->lcf + 1)
1445 continue;
1446 if (bt->vsync != stdi->lcvs)
1447 continue;
1448
1449 pix_clk = hfreq * htotal(bt);
1450
1451 if ((pix_clk < bt->pixelclock + 1000000) &&
1452 (pix_clk > bt->pixelclock - 1000000)) {
1453 *timings = v4l2_dv_timings_presets[i];
1454 return 0;
1455 }
1456 }
1457
Prashant Laddha5fea1bb2015-06-10 13:51:42 -03001458 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs, 0,
Hans Verkuila89bcd42013-08-22 06:14:22 -03001459 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1460 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
Prashant Laddha061ddda2015-05-22 02:27:34 -03001461 false, timings))
Hans Verkuila89bcd42013-08-22 06:14:22 -03001462 return 0;
1463 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
1464 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
1465 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
Prashant Laddha061ddda2015-05-22 02:27:34 -03001466 false, state->aspect_ratio, timings))
Hans Verkuila89bcd42013-08-22 06:14:22 -03001467 return 0;
1468
1469 v4l2_dbg(2, debug, sd,
1470 "%s: No format candidate found for lcvs = %d, lcf=%d, bl = %d, %chsync, %cvsync\n",
1471 __func__, stdi->lcvs, stdi->lcf, stdi->bl,
1472 stdi->hs_pol, stdi->vs_pol);
1473 return -1;
1474}
1475
1476static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
1477{
1478 u32 status;
1479
1480 adv7842_g_input_status(sd, &status);
1481 if (status & V4L2_IN_ST_NO_SIGNAL) {
1482 v4l2_dbg(2, debug, sd, "%s: no signal\n", __func__);
1483 return -ENOLINK;
1484 }
1485
1486 stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
1487 stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
1488 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
1489
1490 if ((cp_read(sd, 0xb5) & 0x80) && ((cp_read(sd, 0xb5) & 0x03) == 0x01)) {
1491 stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
1492 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
1493 stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
1494 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
1495 } else {
1496 stdi->hs_pol = 'x';
1497 stdi->vs_pol = 'x';
1498 }
1499 stdi->interlaced = (cp_read(sd, 0xb1) & 0x40) ? true : false;
1500
1501 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1502 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1503 return -ENOLINK;
1504 }
1505
1506 v4l2_dbg(2, debug, sd,
1507 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1508 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1509 stdi->hs_pol, stdi->vs_pol,
1510 stdi->interlaced ? "interlaced" : "progressive");
1511
1512 return 0;
1513}
1514
1515static int adv7842_enum_dv_timings(struct v4l2_subdev *sd,
1516 struct v4l2_enum_dv_timings *timings)
1517{
Laurent Pinchartc9161942014-01-31 08:51:18 -03001518 if (timings->pad != 0)
1519 return -EINVAL;
1520
Hans Verkuila89bcd42013-08-22 06:14:22 -03001521 return v4l2_enum_dv_timings_cap(timings,
1522 adv7842_get_dv_timings_cap(sd), adv7842_check_dv_timings, NULL);
1523}
1524
1525static int adv7842_dv_timings_cap(struct v4l2_subdev *sd,
1526 struct v4l2_dv_timings_cap *cap)
1527{
Laurent Pinchartc9161942014-01-31 08:51:18 -03001528 if (cap->pad != 0)
1529 return -EINVAL;
1530
Hans Verkuila89bcd42013-08-22 06:14:22 -03001531 *cap = *adv7842_get_dv_timings_cap(sd);
1532 return 0;
1533}
1534
1535/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001536 if the format is listed in adv7842_timings[] */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001537static void adv7842_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1538 struct v4l2_dv_timings *timings)
1539{
1540 v4l2_find_dv_timings_cap(timings, adv7842_get_dv_timings_cap(sd),
1541 is_digital_input(sd) ? 250000 : 1000000,
1542 adv7842_check_dv_timings, NULL);
1543}
1544
1545static int adv7842_query_dv_timings(struct v4l2_subdev *sd,
1546 struct v4l2_dv_timings *timings)
1547{
1548 struct adv7842_state *state = to_state(sd);
1549 struct v4l2_bt_timings *bt = &timings->bt;
1550 struct stdi_readback stdi = { 0 };
1551
Martin Buggee78d8342013-12-10 10:57:03 -03001552 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1553
Hans Verkuilf8789e62014-09-20 07:36:39 -03001554 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1555
Hans Verkuila89bcd42013-08-22 06:14:22 -03001556 /* SDP block */
1557 if (state->mode == ADV7842_MODE_SDP)
1558 return -ENODATA;
1559
1560 /* read STDI */
1561 if (read_stdi(sd, &stdi)) {
Martin Bugge6e9071f2013-12-10 12:00:06 -03001562 state->restart_stdi_once = true;
Hans Verkuila89bcd42013-08-22 06:14:22 -03001563 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1564 return -ENOLINK;
1565 }
1566 bt->interlaced = stdi.interlaced ?
1567 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
Hans Verkuilf888ae72015-05-01 11:31:30 -03001568 bt->standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1569 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
Hans Verkuila89bcd42013-08-22 06:14:22 -03001570
1571 if (is_digital_input(sd)) {
Hans Verkuil28a769f2015-06-07 07:32:31 -03001572 u32 freq;
Martin Buggee78d8342013-12-10 10:57:03 -03001573
1574 timings->type = V4L2_DV_BT_656_1120;
Martin Bugge6e9071f2013-12-10 12:00:06 -03001575
Martin Buggee78d8342013-12-10 10:57:03 -03001576 bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
1577 bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
Martin Bugge81ba0a42014-01-24 10:50:04 -03001578 freq = ((hdmi_read(sd, 0x51) << 1) + (hdmi_read(sd, 0x52) >> 7)) * 1000000;
1579 freq += ((hdmi_read(sd, 0x52) & 0x7f) * 7813);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001580 if (is_hdmi(sd)) {
1581 /* adjust for deep color mode */
Martin Bugge81ba0a42014-01-24 10:50:04 -03001582 freq = freq * 8 / (((hdmi_read(sd, 0x0b) & 0xc0) >> 6) * 2 + 8);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001583 }
Martin Buggee78d8342013-12-10 10:57:03 -03001584 bt->pixelclock = freq;
1585 bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
Hans Verkuila89bcd42013-08-22 06:14:22 -03001586 hdmi_read(sd, 0x21);
Martin Buggee78d8342013-12-10 10:57:03 -03001587 bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
Hans Verkuila89bcd42013-08-22 06:14:22 -03001588 hdmi_read(sd, 0x23);
Martin Buggee78d8342013-12-10 10:57:03 -03001589 bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
Hans Verkuila89bcd42013-08-22 06:14:22 -03001590 hdmi_read(sd, 0x25);
Martin Buggee78d8342013-12-10 10:57:03 -03001591 bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1592 hdmi_read(sd, 0x2b)) / 2;
1593 bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1594 hdmi_read(sd, 0x2f)) / 2;
1595 bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1596 hdmi_read(sd, 0x33)) / 2;
1597 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1598 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1599 if (bt->interlaced == V4L2_DV_INTERLACED) {
1600 bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
1601 hdmi_read(sd, 0x0c);
1602 bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
1603 hdmi_read(sd, 0x2d)) / 2;
1604 bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1605 hdmi_read(sd, 0x31)) / 2;
Hans Verkuilf8789e62014-09-20 07:36:39 -03001606 bt->il_vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
Martin Buggee78d8342013-12-10 10:57:03 -03001607 hdmi_read(sd, 0x35)) / 2;
Hans Verkuilf888ae72015-05-01 11:31:30 -03001608 } else {
1609 bt->il_vfrontporch = 0;
1610 bt->il_vsync = 0;
1611 bt->il_vbackporch = 0;
Martin Buggee78d8342013-12-10 10:57:03 -03001612 }
1613 adv7842_fill_optional_dv_timings_fields(sd, timings);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001614 } else {
Martin Bugge6e9071f2013-12-10 12:00:06 -03001615 /* find format
1616 * Since LCVS values are inaccurate [REF_03, p. 339-340],
1617 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1618 */
1619 if (!stdi2dv_timings(sd, &stdi, timings))
1620 goto found;
1621 stdi.lcvs += 1;
1622 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1623 if (!stdi2dv_timings(sd, &stdi, timings))
1624 goto found;
1625 stdi.lcvs -= 2;
1626 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001627 if (stdi2dv_timings(sd, &stdi, timings)) {
Martin Bugge6e9071f2013-12-10 12:00:06 -03001628 /*
1629 * The STDI block may measure wrong values, especially
1630 * for lcvs and lcf. If the driver can not find any
1631 * valid timing, the STDI block is restarted to measure
1632 * the video timings again. The function will return an
1633 * error, but the restart of STDI will generate a new
1634 * STDI interrupt and the format detection process will
1635 * restart.
1636 */
1637 if (state->restart_stdi_once) {
1638 v4l2_dbg(1, debug, sd, "%s: restart STDI\n", __func__);
1639 /* TODO restart STDI for Sync Channel 2 */
1640 /* enter one-shot mode */
1641 cp_write_and_or(sd, 0x86, 0xf9, 0x00);
1642 /* trigger STDI restart */
1643 cp_write_and_or(sd, 0x86, 0xf9, 0x04);
1644 /* reset to continuous mode */
1645 cp_write_and_or(sd, 0x86, 0xf9, 0x02);
1646 state->restart_stdi_once = false;
1647 return -ENOLINK;
1648 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03001649 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1650 return -ERANGE;
1651 }
Martin Bugge6e9071f2013-12-10 12:00:06 -03001652 state->restart_stdi_once = true;
Hans Verkuila89bcd42013-08-22 06:14:22 -03001653 }
Martin Bugge6e9071f2013-12-10 12:00:06 -03001654found:
Hans Verkuila89bcd42013-08-22 06:14:22 -03001655
1656 if (debug > 1)
Martin Bugge6e9071f2013-12-10 12:00:06 -03001657 v4l2_print_dv_timings(sd->name, "adv7842_query_dv_timings:",
1658 timings, true);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001659 return 0;
1660}
1661
1662static int adv7842_s_dv_timings(struct v4l2_subdev *sd,
1663 struct v4l2_dv_timings *timings)
1664{
1665 struct adv7842_state *state = to_state(sd);
1666 struct v4l2_bt_timings *bt;
1667 int err;
1668
Martin Buggee78d8342013-12-10 10:57:03 -03001669 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
1670
Hans Verkuila89bcd42013-08-22 06:14:22 -03001671 if (state->mode == ADV7842_MODE_SDP)
1672 return -ENODATA;
1673
Hans Verkuil85f9e062015-11-13 09:46:26 -02001674 if (v4l2_match_dv_timings(&state->timings, timings, 0, false)) {
Martin Bugge834a8be2013-12-12 10:10:57 -03001675 v4l2_dbg(1, debug, sd, "%s: no change\n", __func__);
1676 return 0;
1677 }
1678
Hans Verkuila89bcd42013-08-22 06:14:22 -03001679 bt = &timings->bt;
1680
1681 if (!v4l2_valid_dv_timings(timings, adv7842_get_dv_timings_cap(sd),
1682 adv7842_check_dv_timings, NULL))
1683 return -ERANGE;
1684
1685 adv7842_fill_optional_dv_timings_fields(sd, timings);
1686
1687 state->timings = *timings;
1688
Martin Bugge6251e652013-12-10 11:01:00 -03001689 cp_write(sd, 0x91, bt->interlaced ? 0x40 : 0x00);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001690
1691 /* Use prim_mode and vid_std when available */
1692 err = configure_predefined_video_timings(sd, timings);
1693 if (err) {
1694 /* custom settings when the video format
1695 does not have prim_mode/vid_std */
1696 configure_custom_video_timings(sd, bt);
1697 }
1698
1699 set_rgb_quantization_range(sd);
1700
1701
1702 if (debug > 1)
1703 v4l2_print_dv_timings(sd->name, "adv7842_s_dv_timings: ",
1704 timings, true);
1705 return 0;
1706}
1707
1708static int adv7842_g_dv_timings(struct v4l2_subdev *sd,
1709 struct v4l2_dv_timings *timings)
1710{
1711 struct adv7842_state *state = to_state(sd);
1712
1713 if (state->mode == ADV7842_MODE_SDP)
1714 return -ENODATA;
1715 *timings = state->timings;
1716 return 0;
1717}
1718
1719static void enable_input(struct v4l2_subdev *sd)
1720{
1721 struct adv7842_state *state = to_state(sd);
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001722
1723 set_rgb_quantization_range(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001724 switch (state->mode) {
1725 case ADV7842_MODE_SDP:
1726 case ADV7842_MODE_COMP:
1727 case ADV7842_MODE_RGB:
Hans Verkuila89bcd42013-08-22 06:14:22 -03001728 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
1729 break;
1730 case ADV7842_MODE_HDMI:
Hans Verkuila89bcd42013-08-22 06:14:22 -03001731 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1732 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
Mats Randgaard5b64b202013-12-05 12:08:45 -03001733 hdmi_write_and_or(sd, 0x1a, 0xef, 0x00); /* Unmute audio */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001734 break;
1735 default:
1736 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1737 __func__, state->mode);
1738 break;
1739 }
1740}
1741
1742static void disable_input(struct v4l2_subdev *sd)
1743{
Mats Randgaard5b64b202013-12-05 12:08:45 -03001744 hdmi_write_and_or(sd, 0x1a, 0xef, 0x10); /* Mute audio [REF_01, c. 2.2.2] */
1745 msleep(16); /* 512 samples with >= 32 kHz sample rate [REF_03, c. 8.29] */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001746 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
Hans Verkuila89bcd42013-08-22 06:14:22 -03001747 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1748}
1749
1750static void sdp_csc_coeff(struct v4l2_subdev *sd,
1751 const struct adv7842_sdp_csc_coeff *c)
1752{
1753 /* csc auto/manual */
1754 sdp_io_write_and_or(sd, 0xe0, 0xbf, c->manual ? 0x00 : 0x40);
1755
1756 if (!c->manual)
1757 return;
1758
1759 /* csc scaling */
1760 sdp_io_write_and_or(sd, 0xe0, 0x7f, c->scaling == 2 ? 0x80 : 0x00);
1761
1762 /* A coeff */
1763 sdp_io_write_and_or(sd, 0xe0, 0xe0, c->A1 >> 8);
1764 sdp_io_write(sd, 0xe1, c->A1);
1765 sdp_io_write_and_or(sd, 0xe2, 0xe0, c->A2 >> 8);
1766 sdp_io_write(sd, 0xe3, c->A2);
1767 sdp_io_write_and_or(sd, 0xe4, 0xe0, c->A3 >> 8);
1768 sdp_io_write(sd, 0xe5, c->A3);
1769
1770 /* A scale */
1771 sdp_io_write_and_or(sd, 0xe6, 0x80, c->A4 >> 8);
1772 sdp_io_write(sd, 0xe7, c->A4);
1773
1774 /* B coeff */
1775 sdp_io_write_and_or(sd, 0xe8, 0xe0, c->B1 >> 8);
1776 sdp_io_write(sd, 0xe9, c->B1);
1777 sdp_io_write_and_or(sd, 0xea, 0xe0, c->B2 >> 8);
1778 sdp_io_write(sd, 0xeb, c->B2);
1779 sdp_io_write_and_or(sd, 0xec, 0xe0, c->B3 >> 8);
1780 sdp_io_write(sd, 0xed, c->B3);
1781
1782 /* B scale */
1783 sdp_io_write_and_or(sd, 0xee, 0x80, c->B4 >> 8);
1784 sdp_io_write(sd, 0xef, c->B4);
1785
1786 /* C coeff */
1787 sdp_io_write_and_or(sd, 0xf0, 0xe0, c->C1 >> 8);
1788 sdp_io_write(sd, 0xf1, c->C1);
1789 sdp_io_write_and_or(sd, 0xf2, 0xe0, c->C2 >> 8);
1790 sdp_io_write(sd, 0xf3, c->C2);
1791 sdp_io_write_and_or(sd, 0xf4, 0xe0, c->C3 >> 8);
1792 sdp_io_write(sd, 0xf5, c->C3);
1793
1794 /* C scale */
1795 sdp_io_write_and_or(sd, 0xf6, 0x80, c->C4 >> 8);
1796 sdp_io_write(sd, 0xf7, c->C4);
1797}
1798
1799static void select_input(struct v4l2_subdev *sd,
1800 enum adv7842_vid_std_select vid_std_select)
1801{
1802 struct adv7842_state *state = to_state(sd);
1803
1804 switch (state->mode) {
1805 case ADV7842_MODE_SDP:
1806 io_write(sd, 0x00, vid_std_select); /* video std: CVBS or YC mode */
1807 io_write(sd, 0x01, 0); /* prim mode */
1808 /* enable embedded syncs for auto graphics mode */
1809 cp_write_and_or(sd, 0x81, 0xef, 0x10);
1810
1811 afe_write(sd, 0x00, 0x00); /* power up ADC */
1812 afe_write(sd, 0xc8, 0x00); /* phase control */
1813
Hans Verkuila89bcd42013-08-22 06:14:22 -03001814 io_write(sd, 0xdd, 0x90); /* Manual 2x output clock */
1815 /* script says register 0xde, which don't exist in manual */
1816
1817 /* Manual analog input muxing mode, CVBS (6.4)*/
1818 afe_write_and_or(sd, 0x02, 0x7f, 0x80);
1819 if (vid_std_select == ADV7842_SDP_VID_STD_CVBS_SD_4x1) {
1820 afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1821 afe_write(sd, 0x04, 0x00); /* ADC2 N/C,ADC3 N/C*/
1822 } else {
1823 afe_write(sd, 0x03, 0xa0); /* ADC0 to AIN10 (CVBS), ADC1 N/C*/
1824 afe_write(sd, 0x04, 0xc0); /* ADC2 to AIN12, ADC3 N/C*/
1825 }
1826 afe_write(sd, 0x0c, 0x1f); /* ADI recommend write */
1827 afe_write(sd, 0x12, 0x63); /* ADI recommend write */
1828
1829 sdp_io_write(sd, 0xb2, 0x60); /* Disable AV codes */
1830 sdp_io_write(sd, 0xc8, 0xe3); /* Disable Ancillary data */
1831
1832 /* SDP recommended settings */
1833 sdp_write(sd, 0x00, 0x3F); /* Autodetect PAL NTSC (not SECAM) */
1834 sdp_write(sd, 0x01, 0x00); /* Pedestal Off */
1835
1836 sdp_write(sd, 0x03, 0xE4); /* Manual VCR Gain Luma 0x40B */
1837 sdp_write(sd, 0x04, 0x0B); /* Manual Luma setting */
1838 sdp_write(sd, 0x05, 0xC3); /* Manual Chroma setting 0x3FE */
1839 sdp_write(sd, 0x06, 0xFE); /* Manual Chroma setting */
1840 sdp_write(sd, 0x12, 0x0D); /* Frame TBC,I_P, 3D comb enabled */
1841 sdp_write(sd, 0xA7, 0x00); /* ADI Recommended Write */
1842 sdp_io_write(sd, 0xB0, 0x00); /* Disable H and v blanking */
1843
1844 /* deinterlacer enabled and 3D comb */
1845 sdp_write_and_or(sd, 0x12, 0xf6, 0x09);
1846
Hans Verkuila89bcd42013-08-22 06:14:22 -03001847 break;
1848
1849 case ADV7842_MODE_COMP:
1850 case ADV7842_MODE_RGB:
1851 /* Automatic analog input muxing mode */
1852 afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1853 /* set mode and select free run resolution */
1854 io_write(sd, 0x00, vid_std_select); /* video std */
1855 io_write(sd, 0x01, 0x02); /* prim mode */
1856 cp_write_and_or(sd, 0x81, 0xef, 0x10); /* enable embedded syncs
1857 for auto graphics mode */
1858
1859 afe_write(sd, 0x00, 0x00); /* power up ADC */
1860 afe_write(sd, 0xc8, 0x00); /* phase control */
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001861 if (state->mode == ADV7842_MODE_COMP) {
1862 /* force to YCrCb */
1863 io_write_and_or(sd, 0x02, 0x0f, 0x60);
1864 } else {
1865 /* force to RGB */
1866 io_write_and_or(sd, 0x02, 0x0f, 0x10);
1867 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03001868
1869 /* set ADI recommended settings for digitizer */
1870 /* "ADV7842 Register Settings Recommendations
1871 * (rev. 1.8, November 2010)" p. 9. */
1872 afe_write(sd, 0x0c, 0x1f); /* ADC Range improvement */
1873 afe_write(sd, 0x12, 0x63); /* ADC Range improvement */
1874
1875 /* set to default gain for RGB */
1876 cp_write(sd, 0x73, 0x10);
1877 cp_write(sd, 0x74, 0x04);
1878 cp_write(sd, 0x75, 0x01);
1879 cp_write(sd, 0x76, 0x00);
1880
1881 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1882 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1883 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
1884 break;
1885
1886 case ADV7842_MODE_HDMI:
1887 /* Automatic analog input muxing mode */
1888 afe_write_and_or(sd, 0x02, 0x7f, 0x00);
1889 /* set mode and select free run resolution */
1890 if (state->hdmi_port_a)
1891 hdmi_write(sd, 0x00, 0x02); /* select port A */
1892 else
1893 hdmi_write(sd, 0x00, 0x03); /* select port B */
1894 io_write(sd, 0x00, vid_std_select); /* video std */
1895 io_write(sd, 0x01, 5); /* prim mode */
1896 cp_write_and_or(sd, 0x81, 0xef, 0x00); /* disable embedded syncs
1897 for auto graphics mode */
1898
1899 /* set ADI recommended settings for HDMI: */
1900 /* "ADV7842 Register Settings Recommendations
1901 * (rev. 1.8, November 2010)" p. 3. */
1902 hdmi_write(sd, 0xc0, 0x00);
1903 hdmi_write(sd, 0x0d, 0x34); /* ADI recommended write */
1904 hdmi_write(sd, 0x3d, 0x10); /* ADI recommended write */
1905 hdmi_write(sd, 0x44, 0x85); /* TMDS PLL optimization */
1906 hdmi_write(sd, 0x46, 0x1f); /* ADI recommended write */
1907 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1908 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1909 hdmi_write(sd, 0x60, 0x88); /* TMDS PLL optimization */
1910 hdmi_write(sd, 0x61, 0x88); /* TMDS PLL optimization */
1911 hdmi_write(sd, 0x6c, 0x18); /* Disable ISRC clearing bit,
1912 Improve robustness */
1913 hdmi_write(sd, 0x75, 0x10); /* DDC drive strength */
1914 hdmi_write(sd, 0x85, 0x1f); /* equaliser */
1915 hdmi_write(sd, 0x87, 0x70); /* ADI recommended write */
1916 hdmi_write(sd, 0x89, 0x04); /* equaliser */
1917 hdmi_write(sd, 0x8a, 0x1e); /* equaliser */
1918 hdmi_write(sd, 0x93, 0x04); /* equaliser */
1919 hdmi_write(sd, 0x94, 0x1e); /* equaliser */
1920 hdmi_write(sd, 0x99, 0xa1); /* ADI recommended write */
1921 hdmi_write(sd, 0x9b, 0x09); /* ADI recommended write */
1922 hdmi_write(sd, 0x9d, 0x02); /* equaliser */
1923
1924 afe_write(sd, 0x00, 0xff); /* power down ADC */
1925 afe_write(sd, 0xc8, 0x40); /* phase control */
1926
1927 /* set to default gain for HDMI */
1928 cp_write(sd, 0x73, 0x10);
1929 cp_write(sd, 0x74, 0x04);
1930 cp_write(sd, 0x75, 0x01);
1931 cp_write(sd, 0x76, 0x00);
1932
1933 /* reset ADI recommended settings for digitizer */
1934 /* "ADV7842 Register Settings Recommendations
1935 * (rev. 2.5, June 2010)" p. 17. */
1936 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1937 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
Martin Bugge933913d2014-01-24 10:50:03 -03001938 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1939
Hans Verkuila89bcd42013-08-22 06:14:22 -03001940 /* CP coast control */
1941 cp_write(sd, 0xc3, 0x33); /* Component mode */
1942
1943 /* color space conversion, autodetect color space */
1944 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
1945 break;
1946
1947 default:
1948 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1949 __func__, state->mode);
1950 break;
1951 }
1952}
1953
1954static int adv7842_s_routing(struct v4l2_subdev *sd,
1955 u32 input, u32 output, u32 config)
1956{
1957 struct adv7842_state *state = to_state(sd);
1958
1959 v4l2_dbg(2, debug, sd, "%s: input %d\n", __func__, input);
1960
1961 switch (input) {
1962 case ADV7842_SELECT_HDMI_PORT_A:
Hans Verkuila89bcd42013-08-22 06:14:22 -03001963 state->mode = ADV7842_MODE_HDMI;
1964 state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1965 state->hdmi_port_a = true;
1966 break;
1967 case ADV7842_SELECT_HDMI_PORT_B:
Hans Verkuila89bcd42013-08-22 06:14:22 -03001968 state->mode = ADV7842_MODE_HDMI;
1969 state->vid_std_select = ADV7842_HDMI_COMP_VID_STD_HD_1250P;
1970 state->hdmi_port_a = false;
1971 break;
1972 case ADV7842_SELECT_VGA_COMP:
Hans Verkuil69e9ba62013-12-20 05:44:27 -03001973 state->mode = ADV7842_MODE_COMP;
1974 state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1975 break;
Hans Verkuila89bcd42013-08-22 06:14:22 -03001976 case ADV7842_SELECT_VGA_RGB:
1977 state->mode = ADV7842_MODE_RGB;
1978 state->vid_std_select = ADV7842_RGB_VID_STD_AUTO_GRAPH_MODE;
1979 break;
1980 case ADV7842_SELECT_SDP_CVBS:
1981 state->mode = ADV7842_MODE_SDP;
1982 state->vid_std_select = ADV7842_SDP_VID_STD_CVBS_SD_4x1;
1983 break;
1984 case ADV7842_SELECT_SDP_YC:
1985 state->mode = ADV7842_MODE_SDP;
1986 state->vid_std_select = ADV7842_SDP_VID_STD_YC_SD4_x1;
1987 break;
1988 default:
1989 return -EINVAL;
1990 }
1991
1992 disable_input(sd);
1993 select_input(sd, state->vid_std_select);
1994 enable_input(sd);
1995
Lars-Peter Clausen2cf40902015-06-24 13:50:31 -03001996 v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
Hans Verkuila89bcd42013-08-22 06:14:22 -03001997
1998 return 0;
1999}
2000
Hans Verkuilebcff5f2015-04-09 04:01:33 -03002001static int adv7842_enum_mbus_code(struct v4l2_subdev *sd,
2002 struct v4l2_subdev_pad_config *cfg,
2003 struct v4l2_subdev_mbus_code_enum *code)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002004{
Hans Verkuilf888ae72015-05-01 11:31:30 -03002005 if (code->index >= ARRAY_SIZE(adv7842_formats))
Hans Verkuila89bcd42013-08-22 06:14:22 -03002006 return -EINVAL;
Hans Verkuilf888ae72015-05-01 11:31:30 -03002007 code->code = adv7842_formats[code->index].code;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002008 return 0;
2009}
2010
Hans Verkuilf888ae72015-05-01 11:31:30 -03002011static void adv7842_fill_format(struct adv7842_state *state,
2012 struct v4l2_mbus_framefmt *format)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002013{
Hans Verkuilf888ae72015-05-01 11:31:30 -03002014 memset(format, 0, sizeof(*format));
2015
2016 format->width = state->timings.bt.width;
2017 format->height = state->timings.bt.height;
2018 format->field = V4L2_FIELD_NONE;
2019 format->colorspace = V4L2_COLORSPACE_SRGB;
2020
2021 if (state->timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO)
2022 format->colorspace = (state->timings.bt.height <= 576) ?
2023 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
2024}
2025
2026/*
2027 * Compute the op_ch_sel value required to obtain on the bus the component order
2028 * corresponding to the selected format taking into account bus reordering
2029 * applied by the board at the output of the device.
2030 *
2031 * The following table gives the op_ch_value from the format component order
2032 * (expressed as op_ch_sel value in column) and the bus reordering (expressed as
2033 * adv7842_bus_order value in row).
2034 *
2035 * | GBR(0) GRB(1) BGR(2) RGB(3) BRG(4) RBG(5)
2036 * ----------+-------------------------------------------------
2037 * RGB (NOP) | GBR GRB BGR RGB BRG RBG
2038 * GRB (1-2) | BGR RGB GBR GRB RBG BRG
2039 * RBG (2-3) | GRB GBR BRG RBG BGR RGB
2040 * BGR (1-3) | RBG BRG RGB BGR GRB GBR
2041 * BRG (ROR) | BRG RBG GRB GBR RGB BGR
2042 * GBR (ROL) | RGB BGR RBG BRG GBR GRB
2043 */
2044static unsigned int adv7842_op_ch_sel(struct adv7842_state *state)
2045{
2046#define _SEL(a, b, c, d, e, f) { \
2047 ADV7842_OP_CH_SEL_##a, ADV7842_OP_CH_SEL_##b, ADV7842_OP_CH_SEL_##c, \
2048 ADV7842_OP_CH_SEL_##d, ADV7842_OP_CH_SEL_##e, ADV7842_OP_CH_SEL_##f }
2049#define _BUS(x) [ADV7842_BUS_ORDER_##x]
2050
2051 static const unsigned int op_ch_sel[6][6] = {
2052 _BUS(RGB) /* NOP */ = _SEL(GBR, GRB, BGR, RGB, BRG, RBG),
2053 _BUS(GRB) /* 1-2 */ = _SEL(BGR, RGB, GBR, GRB, RBG, BRG),
2054 _BUS(RBG) /* 2-3 */ = _SEL(GRB, GBR, BRG, RBG, BGR, RGB),
2055 _BUS(BGR) /* 1-3 */ = _SEL(RBG, BRG, RGB, BGR, GRB, GBR),
2056 _BUS(BRG) /* ROR */ = _SEL(BRG, RBG, GRB, GBR, RGB, BGR),
2057 _BUS(GBR) /* ROL */ = _SEL(RGB, BGR, RBG, BRG, GBR, GRB),
2058 };
2059
2060 return op_ch_sel[state->pdata.bus_order][state->format->op_ch_sel >> 5];
2061}
2062
2063static void adv7842_setup_format(struct adv7842_state *state)
2064{
2065 struct v4l2_subdev *sd = &state->sd;
2066
2067 io_write_clr_set(sd, 0x02, 0x02,
2068 state->format->rgb_out ? ADV7842_RGB_OUT : 0);
2069 io_write(sd, 0x03, state->format->op_format_sel |
2070 state->pdata.op_format_mode_sel);
2071 io_write_clr_set(sd, 0x04, 0xe0, adv7842_op_ch_sel(state));
2072 io_write_clr_set(sd, 0x05, 0x01,
2073 state->format->swap_cb_cr ? ADV7842_OP_SWAP_CB_CR : 0);
Hans Verkuilfd742462016-06-28 11:43:01 -03002074 set_rgb_quantization_range(sd);
Hans Verkuilf888ae72015-05-01 11:31:30 -03002075}
2076
2077static int adv7842_get_format(struct v4l2_subdev *sd,
2078 struct v4l2_subdev_pad_config *cfg,
2079 struct v4l2_subdev_format *format)
2080{
Hans Verkuila89bcd42013-08-22 06:14:22 -03002081 struct adv7842_state *state = to_state(sd);
2082
Hans Verkuilf888ae72015-05-01 11:31:30 -03002083 if (format->pad != ADV7842_PAD_SOURCE)
Hans Verkuilda298c62015-04-09 04:02:34 -03002084 return -EINVAL;
2085
Hans Verkuila89bcd42013-08-22 06:14:22 -03002086 if (state->mode == ADV7842_MODE_SDP) {
2087 /* SPD block */
Hans Verkuilf888ae72015-05-01 11:31:30 -03002088 if (!(sdp_read(sd, 0x5a) & 0x01))
Hans Verkuila89bcd42013-08-22 06:14:22 -03002089 return -EINVAL;
Hans Verkuilf888ae72015-05-01 11:31:30 -03002090 format->format.code = MEDIA_BUS_FMT_YUYV8_2X8;
2091 format->format.width = 720;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002092 /* valid signal */
2093 if (state->norm & V4L2_STD_525_60)
Hans Verkuilf888ae72015-05-01 11:31:30 -03002094 format->format.height = 480;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002095 else
Hans Verkuilf888ae72015-05-01 11:31:30 -03002096 format->format.height = 576;
2097 format->format.colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002098 return 0;
2099 }
2100
Hans Verkuilf888ae72015-05-01 11:31:30 -03002101 adv7842_fill_format(state, &format->format);
2102
2103 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2104 struct v4l2_mbus_framefmt *fmt;
2105
2106 fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
2107 format->format.code = fmt->code;
2108 } else {
2109 format->format.code = state->format->code;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002110 }
Hans Verkuilf888ae72015-05-01 11:31:30 -03002111
2112 return 0;
2113}
2114
2115static int adv7842_set_format(struct v4l2_subdev *sd,
2116 struct v4l2_subdev_pad_config *cfg,
2117 struct v4l2_subdev_format *format)
2118{
2119 struct adv7842_state *state = to_state(sd);
2120 const struct adv7842_format_info *info;
2121
2122 if (format->pad != ADV7842_PAD_SOURCE)
2123 return -EINVAL;
2124
2125 if (state->mode == ADV7842_MODE_SDP)
2126 return adv7842_get_format(sd, cfg, format);
2127
2128 info = adv7842_format_info(state, format->format.code);
2129 if (info == NULL)
2130 info = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
2131
2132 adv7842_fill_format(state, &format->format);
2133 format->format.code = info->code;
2134
2135 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
2136 struct v4l2_mbus_framefmt *fmt;
2137
2138 fmt = v4l2_subdev_get_try_format(sd, cfg, format->pad);
2139 fmt->code = format->format.code;
2140 } else {
2141 state->format = info;
2142 adv7842_setup_format(state);
2143 }
2144
Hans Verkuila89bcd42013-08-22 06:14:22 -03002145 return 0;
2146}
2147
2148static void adv7842_irq_enable(struct v4l2_subdev *sd, bool enable)
2149{
2150 if (enable) {
2151 /* Enable SSPD, STDI and CP locked/unlocked interrupts */
2152 io_write(sd, 0x46, 0x9c);
2153 /* ESDP_50HZ_DET interrupt */
2154 io_write(sd, 0x5a, 0x10);
2155 /* Enable CABLE_DET_A/B_ST (+5v) interrupt */
2156 io_write(sd, 0x73, 0x03);
2157 /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
2158 io_write(sd, 0x78, 0x03);
2159 /* Enable SDP Standard Detection Change and SDP Video Detected */
2160 io_write(sd, 0xa0, 0x09);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002161 /* Enable HDMI_MODE interrupt */
2162 io_write(sd, 0x69, 0x08);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002163 } else {
2164 io_write(sd, 0x46, 0x0);
2165 io_write(sd, 0x5a, 0x0);
2166 io_write(sd, 0x73, 0x0);
2167 io_write(sd, 0x78, 0x0);
2168 io_write(sd, 0xa0, 0x0);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002169 io_write(sd, 0x69, 0x0);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002170 }
2171}
2172
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002173#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
2174static void adv7842_cec_tx_raw_status(struct v4l2_subdev *sd, u8 tx_raw_status)
2175{
2176 struct adv7842_state *state = to_state(sd);
2177
2178 if ((cec_read(sd, 0x11) & 0x01) == 0) {
2179 v4l2_dbg(1, debug, sd, "%s: tx raw: tx disabled\n", __func__);
2180 return;
2181 }
2182
2183 if (tx_raw_status & 0x02) {
2184 v4l2_dbg(1, debug, sd, "%s: tx raw: arbitration lost\n",
2185 __func__);
2186 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_ARB_LOST,
2187 1, 0, 0, 0);
2188 return;
2189 }
2190 if (tx_raw_status & 0x04) {
2191 u8 status;
2192 u8 nack_cnt;
2193 u8 low_drive_cnt;
2194
2195 v4l2_dbg(1, debug, sd, "%s: tx raw: retry failed\n", __func__);
2196 /*
2197 * We set this status bit since this hardware performs
2198 * retransmissions.
2199 */
2200 status = CEC_TX_STATUS_MAX_RETRIES;
2201 nack_cnt = cec_read(sd, 0x14) & 0xf;
2202 if (nack_cnt)
2203 status |= CEC_TX_STATUS_NACK;
2204 low_drive_cnt = cec_read(sd, 0x14) >> 4;
2205 if (low_drive_cnt)
2206 status |= CEC_TX_STATUS_LOW_DRIVE;
2207 cec_transmit_done(state->cec_adap, status,
2208 0, nack_cnt, low_drive_cnt, 0);
2209 return;
2210 }
2211 if (tx_raw_status & 0x01) {
2212 v4l2_dbg(1, debug, sd, "%s: tx raw: ready ok\n", __func__);
2213 cec_transmit_done(state->cec_adap, CEC_TX_STATUS_OK, 0, 0, 0, 0);
2214 return;
2215 }
2216}
2217
2218static void adv7842_cec_isr(struct v4l2_subdev *sd, bool *handled)
2219{
2220 u8 cec_irq;
2221
2222 /* cec controller */
2223 cec_irq = io_read(sd, 0x93) & 0x0f;
2224 if (!cec_irq)
2225 return;
2226
2227 v4l2_dbg(1, debug, sd, "%s: cec: irq 0x%x\n", __func__, cec_irq);
2228 adv7842_cec_tx_raw_status(sd, cec_irq);
2229 if (cec_irq & 0x08) {
2230 struct adv7842_state *state = to_state(sd);
2231 struct cec_msg msg;
2232
2233 msg.len = cec_read(sd, 0x25) & 0x1f;
2234 if (msg.len > 16)
2235 msg.len = 16;
2236
2237 if (msg.len) {
2238 u8 i;
2239
2240 for (i = 0; i < msg.len; i++)
2241 msg.msg[i] = cec_read(sd, i + 0x15);
2242 cec_write(sd, 0x26, 0x01); /* re-enable rx */
2243 cec_received_msg(state->cec_adap, &msg);
2244 }
2245 }
2246
2247 io_write(sd, 0x94, cec_irq);
2248
2249 if (handled)
2250 *handled = true;
2251}
2252
2253static int adv7842_cec_adap_enable(struct cec_adapter *adap, bool enable)
2254{
Jose Abreu2e60ad12017-03-24 13:47:57 -03002255 struct adv7842_state *state = cec_get_drvdata(adap);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002256 struct v4l2_subdev *sd = &state->sd;
2257
2258 if (!state->cec_enabled_adap && enable) {
2259 cec_write_clr_set(sd, 0x2a, 0x01, 0x01); /* power up cec */
2260 cec_write(sd, 0x2c, 0x01); /* cec soft reset */
2261 cec_write_clr_set(sd, 0x11, 0x01, 0); /* initially disable tx */
2262 /* enabled irqs: */
2263 /* tx: ready */
2264 /* tx: arbitration lost */
2265 /* tx: retry timeout */
2266 /* rx: ready */
2267 io_write_clr_set(sd, 0x96, 0x0f, 0x0f);
2268 cec_write(sd, 0x26, 0x01); /* enable rx */
2269 } else if (state->cec_enabled_adap && !enable) {
2270 /* disable cec interrupts */
2271 io_write_clr_set(sd, 0x96, 0x0f, 0x00);
2272 /* disable address mask 1-3 */
2273 cec_write_clr_set(sd, 0x27, 0x70, 0x00);
2274 /* power down cec section */
2275 cec_write_clr_set(sd, 0x2a, 0x01, 0x00);
2276 state->cec_valid_addrs = 0;
2277 }
2278 state->cec_enabled_adap = enable;
2279 return 0;
2280}
2281
2282static int adv7842_cec_adap_log_addr(struct cec_adapter *adap, u8 addr)
2283{
Jose Abreu2e60ad12017-03-24 13:47:57 -03002284 struct adv7842_state *state = cec_get_drvdata(adap);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002285 struct v4l2_subdev *sd = &state->sd;
2286 unsigned int i, free_idx = ADV7842_MAX_ADDRS;
2287
2288 if (!state->cec_enabled_adap)
2289 return addr == CEC_LOG_ADDR_INVALID ? 0 : -EIO;
2290
2291 if (addr == CEC_LOG_ADDR_INVALID) {
2292 cec_write_clr_set(sd, 0x27, 0x70, 0);
2293 state->cec_valid_addrs = 0;
2294 return 0;
2295 }
2296
2297 for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2298 bool is_valid = state->cec_valid_addrs & (1 << i);
2299
2300 if (free_idx == ADV7842_MAX_ADDRS && !is_valid)
2301 free_idx = i;
2302 if (is_valid && state->cec_addr[i] == addr)
2303 return 0;
2304 }
2305 if (i == ADV7842_MAX_ADDRS) {
2306 i = free_idx;
2307 if (i == ADV7842_MAX_ADDRS)
2308 return -ENXIO;
2309 }
2310 state->cec_addr[i] = addr;
2311 state->cec_valid_addrs |= 1 << i;
2312
2313 switch (i) {
2314 case 0:
2315 /* enable address mask 0 */
2316 cec_write_clr_set(sd, 0x27, 0x10, 0x10);
2317 /* set address for mask 0 */
2318 cec_write_clr_set(sd, 0x28, 0x0f, addr);
2319 break;
2320 case 1:
2321 /* enable address mask 1 */
2322 cec_write_clr_set(sd, 0x27, 0x20, 0x20);
2323 /* set address for mask 1 */
2324 cec_write_clr_set(sd, 0x28, 0xf0, addr << 4);
2325 break;
2326 case 2:
2327 /* enable address mask 2 */
2328 cec_write_clr_set(sd, 0x27, 0x40, 0x40);
2329 /* set address for mask 1 */
2330 cec_write_clr_set(sd, 0x29, 0x0f, addr);
2331 break;
2332 }
2333 return 0;
2334}
2335
2336static int adv7842_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
2337 u32 signal_free_time, struct cec_msg *msg)
2338{
Jose Abreu2e60ad12017-03-24 13:47:57 -03002339 struct adv7842_state *state = cec_get_drvdata(adap);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002340 struct v4l2_subdev *sd = &state->sd;
2341 u8 len = msg->len;
2342 unsigned int i;
2343
2344 /*
2345 * The number of retries is the number of attempts - 1, but retry
2346 * at least once. It's not clear if a value of 0 is allowed, so
2347 * let's do at least one retry.
2348 */
2349 cec_write_clr_set(sd, 0x12, 0x70, max(1, attempts - 1) << 4);
2350
2351 if (len > 16) {
2352 v4l2_err(sd, "%s: len exceeded 16 (%d)\n", __func__, len);
2353 return -EINVAL;
2354 }
2355
2356 /* write data */
2357 for (i = 0; i < len; i++)
2358 cec_write(sd, i, msg->msg[i]);
2359
2360 /* set length (data + header) */
2361 cec_write(sd, 0x10, len);
2362 /* start transmit, enable tx */
2363 cec_write(sd, 0x11, 0x01);
2364 return 0;
2365}
2366
2367static const struct cec_adap_ops adv7842_cec_adap_ops = {
2368 .adap_enable = adv7842_cec_adap_enable,
2369 .adap_log_addr = adv7842_cec_adap_log_addr,
2370 .adap_transmit = adv7842_cec_adap_transmit,
2371};
2372#endif
2373
Hans Verkuila89bcd42013-08-22 06:14:22 -03002374static int adv7842_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
2375{
2376 struct adv7842_state *state = to_state(sd);
2377 u8 fmt_change_cp, fmt_change_digital, fmt_change_sdp;
Martin Bugge019aa8b2013-12-10 12:01:59 -03002378 u8 irq_status[6];
Hans Verkuila89bcd42013-08-22 06:14:22 -03002379
Martin Buggec9f1f272013-12-10 11:14:26 -03002380 adv7842_irq_enable(sd, false);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002381
2382 /* read status */
2383 irq_status[0] = io_read(sd, 0x43);
2384 irq_status[1] = io_read(sd, 0x57);
2385 irq_status[2] = io_read(sd, 0x70);
2386 irq_status[3] = io_read(sd, 0x75);
2387 irq_status[4] = io_read(sd, 0x9d);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002388 irq_status[5] = io_read(sd, 0x66);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002389
2390 /* and clear */
2391 if (irq_status[0])
2392 io_write(sd, 0x44, irq_status[0]);
2393 if (irq_status[1])
2394 io_write(sd, 0x58, irq_status[1]);
2395 if (irq_status[2])
2396 io_write(sd, 0x71, irq_status[2]);
2397 if (irq_status[3])
2398 io_write(sd, 0x76, irq_status[3]);
2399 if (irq_status[4])
2400 io_write(sd, 0x9e, irq_status[4]);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002401 if (irq_status[5])
2402 io_write(sd, 0x67, irq_status[5]);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002403
Martin Buggec9f1f272013-12-10 11:14:26 -03002404 adv7842_irq_enable(sd, true);
2405
Martin Bugge019aa8b2013-12-10 12:01:59 -03002406 v4l2_dbg(1, debug, sd, "%s: irq %x, %x, %x, %x, %x, %x\n", __func__,
Hans Verkuila89bcd42013-08-22 06:14:22 -03002407 irq_status[0], irq_status[1], irq_status[2],
Martin Bugge019aa8b2013-12-10 12:01:59 -03002408 irq_status[3], irq_status[4], irq_status[5]);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002409
2410 /* format change CP */
2411 fmt_change_cp = irq_status[0] & 0x9c;
2412
2413 /* format change SDP */
2414 if (state->mode == ADV7842_MODE_SDP)
2415 fmt_change_sdp = (irq_status[1] & 0x30) | (irq_status[4] & 0x09);
2416 else
2417 fmt_change_sdp = 0;
2418
2419 /* digital format CP */
2420 if (is_digital_input(sd))
2421 fmt_change_digital = irq_status[3] & 0x03;
2422 else
2423 fmt_change_digital = 0;
2424
Martin Bugge019aa8b2013-12-10 12:01:59 -03002425 /* format change */
Hans Verkuila89bcd42013-08-22 06:14:22 -03002426 if (fmt_change_cp || fmt_change_digital || fmt_change_sdp) {
2427 v4l2_dbg(1, debug, sd,
2428 "%s: fmt_change_cp = 0x%x, fmt_change_digital = 0x%x, fmt_change_sdp = 0x%x\n",
2429 __func__, fmt_change_cp, fmt_change_digital,
2430 fmt_change_sdp);
Lars-Peter Clausen2cf40902015-06-24 13:50:31 -03002431 v4l2_subdev_notify_event(sd, &adv7842_ev_fmt);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002432 if (handled)
2433 *handled = true;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002434 }
2435
Martin Bugge019aa8b2013-12-10 12:01:59 -03002436 /* HDMI/DVI mode */
2437 if (irq_status[5] & 0x08) {
2438 v4l2_dbg(1, debug, sd, "%s: irq %s mode\n", __func__,
2439 (io_read(sd, 0x65) & 0x08) ? "HDMI" : "DVI");
Martin Bugge5046f262014-03-19 06:43:43 -03002440 set_rgb_quantization_range(sd);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002441 if (handled)
2442 *handled = true;
2443 }
2444
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002445#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
2446 /* cec */
2447 adv7842_cec_isr(sd, handled);
2448#endif
2449
Martin Bugge019aa8b2013-12-10 12:01:59 -03002450 /* tx 5v detect */
2451 if (irq_status[2] & 0x3) {
2452 v4l2_dbg(1, debug, sd, "%s: irq tx_5v\n", __func__);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002453 adv7842_s_detect_tx_5v_ctrl(sd);
Martin Bugge019aa8b2013-12-10 12:01:59 -03002454 if (handled)
2455 *handled = true;
2456 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03002457 return 0;
2458}
2459
Hans Verkuilb09dfac2014-03-04 08:05:19 -03002460static int adv7842_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid)
Martin Bugge245b2b62013-12-05 12:14:02 -03002461{
2462 struct adv7842_state *state = to_state(sd);
2463 u8 *data = NULL;
2464
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002465 memset(edid->reserved, 0, sizeof(edid->reserved));
Martin Bugge245b2b62013-12-05 12:14:02 -03002466
2467 switch (edid->pad) {
2468 case ADV7842_EDID_PORT_A:
2469 case ADV7842_EDID_PORT_B:
2470 if (state->hdmi_edid.present & (0x04 << edid->pad))
2471 data = state->hdmi_edid.edid;
2472 break;
2473 case ADV7842_EDID_PORT_VGA:
2474 if (state->vga_edid.present)
2475 data = state->vga_edid.edid;
2476 break;
2477 default:
2478 return -EINVAL;
2479 }
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002480
2481 if (edid->start_block == 0 && edid->blocks == 0) {
2482 edid->blocks = data ? 2 : 0;
2483 return 0;
2484 }
2485
Martin Bugge245b2b62013-12-05 12:14:02 -03002486 if (!data)
2487 return -ENODATA;
2488
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002489 if (edid->start_block >= 2)
2490 return -EINVAL;
2491
2492 if (edid->start_block + edid->blocks > 2)
2493 edid->blocks = 2 - edid->start_block;
2494
2495 memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128);
2496
Martin Bugge245b2b62013-12-05 12:14:02 -03002497 return 0;
2498}
2499
Hans Verkuilb09dfac2014-03-04 08:05:19 -03002500static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002501{
2502 struct adv7842_state *state = to_state(sd);
2503 int err = 0;
2504
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002505 memset(e->reserved, 0, sizeof(e->reserved));
2506
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002507 if (e->pad > ADV7842_EDID_PORT_VGA)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002508 return -EINVAL;
2509 if (e->start_block != 0)
2510 return -EINVAL;
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002511 if (e->blocks > 2) {
2512 e->blocks = 2;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002513 return -E2BIG;
Hans Verkuilc909e5b2014-11-07 09:34:55 -03002514 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03002515
2516 /* todo, per edid */
2517 state->aspect_ratio = v4l2_calc_aspect_ratio(e->edid[0x15],
2518 e->edid[0x16]);
2519
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002520 switch (e->pad) {
2521 case ADV7842_EDID_PORT_VGA:
Hans Verkuila89bcd42013-08-22 06:14:22 -03002522 memset(&state->vga_edid.edid, 0, 256);
2523 state->vga_edid.present = e->blocks ? 0x1 : 0x0;
2524 memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks);
2525 err = edid_write_vga_segment(sd);
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002526 break;
2527 case ADV7842_EDID_PORT_A:
2528 case ADV7842_EDID_PORT_B:
Hans Verkuila89bcd42013-08-22 06:14:22 -03002529 memset(&state->hdmi_edid.edid, 0, 256);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002530 if (e->blocks) {
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002531 state->hdmi_edid.present |= 0x04 << e->pad;
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002532 } else {
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002533 state->hdmi_edid.present &= ~(0x04 << e->pad);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002534 adv7842_s_detect_tx_5v_ctrl(sd);
2535 }
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002536 memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002537 err = edid_write_hdmi_segment(sd, e->pad);
Mats Randgaard7de6fab2013-12-10 11:24:35 -03002538 break;
2539 default:
2540 return -EINVAL;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002541 }
2542 if (err < 0)
2543 v4l2_err(sd, "error %d writing edid on port %d\n", err, e->pad);
2544 return err;
2545}
2546
Martin Bugge09f90c52014-12-19 09:14:23 -03002547struct adv7842_cfg_read_infoframe {
2548 const char *desc;
2549 u8 present_mask;
2550 u8 head_addr;
2551 u8 payload_addr;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002552};
2553
Martin Bugge09f90c52014-12-19 09:14:23 -03002554static void log_infoframe(struct v4l2_subdev *sd, struct adv7842_cfg_read_infoframe *cri)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002555{
2556 int i;
Hans Verkuil28a769f2015-06-07 07:32:31 -03002557 u8 buffer[32];
Martin Bugge09f90c52014-12-19 09:14:23 -03002558 union hdmi_infoframe frame;
2559 u8 len;
2560 struct i2c_client *client = v4l2_get_subdevdata(sd);
2561 struct device *dev = &client->dev;
2562
2563 if (!(io_read(sd, 0x60) & cri->present_mask)) {
2564 v4l2_info(sd, "%s infoframe not received\n", cri->desc);
2565 return;
2566 }
2567
2568 for (i = 0; i < 3; i++)
2569 buffer[i] = infoframe_read(sd, cri->head_addr + i);
2570
2571 len = buffer[2] + 1;
2572
2573 if (len + 3 > sizeof(buffer)) {
2574 v4l2_err(sd, "%s: invalid %s infoframe length %d\n", __func__, cri->desc, len);
2575 return;
2576 }
2577
2578 for (i = 0; i < len; i++)
2579 buffer[i + 3] = infoframe_read(sd, cri->payload_addr + i);
2580
2581 if (hdmi_infoframe_unpack(&frame, buffer) < 0) {
2582 v4l2_err(sd, "%s: unpack of %s infoframe failed\n", __func__, cri->desc);
2583 return;
2584 }
2585
2586 hdmi_infoframe_log(KERN_INFO, dev, &frame);
2587}
2588
2589static void adv7842_log_infoframes(struct v4l2_subdev *sd)
2590{
2591 int i;
2592 struct adv7842_cfg_read_infoframe cri[] = {
2593 { "AVI", 0x01, 0xe0, 0x00 },
2594 { "Audio", 0x02, 0xe3, 0x1c },
2595 { "SDP", 0x04, 0xe6, 0x2a },
2596 { "Vendor", 0x10, 0xec, 0x54 }
2597 };
Hans Verkuila89bcd42013-08-22 06:14:22 -03002598
2599 if (!(hdmi_read(sd, 0x05) & 0x80)) {
Martin Bugge09f90c52014-12-19 09:14:23 -03002600 v4l2_info(sd, "receive DVI-D signal, no infoframes\n");
Hans Verkuila89bcd42013-08-22 06:14:22 -03002601 return;
2602 }
2603
Martin Bugge09f90c52014-12-19 09:14:23 -03002604 for (i = 0; i < ARRAY_SIZE(cri); i++)
2605 log_infoframe(sd, &cri[i]);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002606}
2607
Mauro Carvalho Chehab60eb9572016-06-24 12:17:27 -03002608#if 0
2609/* Let's keep it here for now, as it could be useful for debug */
Hans Verkuila89bcd42013-08-22 06:14:22 -03002610static const char * const prim_mode_txt[] = {
2611 "SDP",
2612 "Component",
2613 "Graphics",
2614 "Reserved",
2615 "CVBS & HDMI AUDIO",
2616 "HDMI-Comp",
2617 "HDMI-GR",
2618 "Reserved",
2619 "Reserved",
2620 "Reserved",
2621 "Reserved",
2622 "Reserved",
2623 "Reserved",
2624 "Reserved",
2625 "Reserved",
2626 "Reserved",
2627};
Mauro Carvalho Chehab60eb9572016-06-24 12:17:27 -03002628#endif
Hans Verkuila89bcd42013-08-22 06:14:22 -03002629
2630static int adv7842_sdp_log_status(struct v4l2_subdev *sd)
2631{
2632 /* SDP (Standard definition processor) block */
Hans Verkuil28a769f2015-06-07 07:32:31 -03002633 u8 sdp_signal_detected = sdp_read(sd, 0x5A) & 0x01;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002634
2635 v4l2_info(sd, "Chip powered %s\n", no_power(sd) ? "off" : "on");
2636 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
2637 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
2638
2639 v4l2_info(sd, "SDP: free run: %s\n",
2640 (sdp_read(sd, 0x56) & 0x01) ? "on" : "off");
2641 v4l2_info(sd, "SDP: %s\n", sdp_signal_detected ?
2642 "valid SD/PR signal detected" : "invalid/no signal");
2643 if (sdp_signal_detected) {
2644 static const char * const sdp_std_txt[] = {
2645 "NTSC-M/J",
2646 "1?",
2647 "NTSC-443",
2648 "60HzSECAM",
2649 "PAL-M",
2650 "5?",
2651 "PAL-60",
2652 "7?", "8?", "9?", "a?", "b?",
2653 "PAL-CombN",
2654 "d?",
2655 "PAL-BGHID",
2656 "SECAM"
2657 };
2658 v4l2_info(sd, "SDP: standard %s\n",
2659 sdp_std_txt[sdp_read(sd, 0x52) & 0x0f]);
2660 v4l2_info(sd, "SDP: %s\n",
2661 (sdp_read(sd, 0x59) & 0x08) ? "50Hz" : "60Hz");
2662 v4l2_info(sd, "SDP: %s\n",
2663 (sdp_read(sd, 0x57) & 0x08) ? "Interlaced" : "Progressive");
2664 v4l2_info(sd, "SDP: deinterlacer %s\n",
2665 (sdp_read(sd, 0x12) & 0x08) ? "enabled" : "disabled");
2666 v4l2_info(sd, "SDP: csc %s mode\n",
2667 (sdp_io_read(sd, 0xe0) & 0x40) ? "auto" : "manual");
2668 }
2669 return 0;
2670}
2671
2672static int adv7842_cp_log_status(struct v4l2_subdev *sd)
2673{
2674 /* CP block */
2675 struct adv7842_state *state = to_state(sd);
2676 struct v4l2_dv_timings timings;
Hans Verkuil28a769f2015-06-07 07:32:31 -03002677 u8 reg_io_0x02 = io_read(sd, 0x02);
2678 u8 reg_io_0x21 = io_read(sd, 0x21);
2679 u8 reg_rep_0x77 = rep_read(sd, 0x77);
2680 u8 reg_rep_0x7d = rep_read(sd, 0x7d);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002681 bool audio_pll_locked = hdmi_read(sd, 0x04) & 0x01;
2682 bool audio_sample_packet_detect = hdmi_read(sd, 0x18) & 0x01;
2683 bool audio_mute = io_read(sd, 0x65) & 0x40;
2684
2685 static const char * const csc_coeff_sel_rb[16] = {
2686 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
2687 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
2688 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
2689 "reserved", "reserved", "reserved", "reserved", "manual"
2690 };
2691 static const char * const input_color_space_txt[16] = {
2692 "RGB limited range (16-235)", "RGB full range (0-255)",
2693 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
Hans Verkuil69e9ba62013-12-20 05:44:27 -03002694 "xvYCC Bt.601", "xvYCC Bt.709",
Hans Verkuila89bcd42013-08-22 06:14:22 -03002695 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
2696 "invalid", "invalid", "invalid", "invalid", "invalid",
2697 "invalid", "invalid", "automatic"
2698 };
2699 static const char * const rgb_quantization_range_txt[] = {
2700 "Automatic",
2701 "RGB limited range (16-235)",
2702 "RGB full range (0-255)",
2703 };
2704 static const char * const deep_color_mode_txt[4] = {
2705 "8-bits per channel",
2706 "10-bits per channel",
2707 "12-bits per channel",
2708 "16-bits per channel (not supported)"
2709 };
2710
2711 v4l2_info(sd, "-----Chip status-----\n");
2712 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
Hans Verkuila89bcd42013-08-22 06:14:22 -03002713 v4l2_info(sd, "HDMI/DVI-D port selected: %s\n",
2714 state->hdmi_port_a ? "A" : "B");
2715 v4l2_info(sd, "EDID A %s, B %s\n",
2716 ((reg_rep_0x7d & 0x04) && (reg_rep_0x77 & 0x04)) ?
2717 "enabled" : "disabled",
2718 ((reg_rep_0x7d & 0x08) && (reg_rep_0x77 & 0x08)) ?
2719 "enabled" : "disabled");
2720 v4l2_info(sd, "HPD A %s, B %s\n",
2721 reg_io_0x21 & 0x02 ? "enabled" : "disabled",
2722 reg_io_0x21 & 0x01 ? "enabled" : "disabled");
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002723 v4l2_info(sd, "CEC: %s\n", state->cec_enabled_adap ?
Hans Verkuila89bcd42013-08-22 06:14:22 -03002724 "enabled" : "disabled");
Hans Verkuil25c84fb2015-09-07 08:13:26 -03002725 if (state->cec_enabled_adap) {
2726 int i;
2727
2728 for (i = 0; i < ADV7842_MAX_ADDRS; i++) {
2729 bool is_valid = state->cec_valid_addrs & (1 << i);
2730
2731 if (is_valid)
2732 v4l2_info(sd, "CEC Logical Address: 0x%x\n",
2733 state->cec_addr[i]);
2734 }
2735 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03002736
2737 v4l2_info(sd, "-----Signal status-----\n");
2738 if (state->hdmi_port_a) {
2739 v4l2_info(sd, "Cable detected (+5V power): %s\n",
2740 io_read(sd, 0x6f) & 0x02 ? "true" : "false");
2741 v4l2_info(sd, "TMDS signal detected: %s\n",
2742 (io_read(sd, 0x6a) & 0x02) ? "true" : "false");
2743 v4l2_info(sd, "TMDS signal locked: %s\n",
2744 (io_read(sd, 0x6a) & 0x20) ? "true" : "false");
2745 } else {
2746 v4l2_info(sd, "Cable detected (+5V power):%s\n",
2747 io_read(sd, 0x6f) & 0x01 ? "true" : "false");
2748 v4l2_info(sd, "TMDS signal detected: %s\n",
2749 (io_read(sd, 0x6a) & 0x01) ? "true" : "false");
2750 v4l2_info(sd, "TMDS signal locked: %s\n",
2751 (io_read(sd, 0x6a) & 0x10) ? "true" : "false");
2752 }
2753 v4l2_info(sd, "CP free run: %s\n",
2754 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
2755 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x, v_freq = 0x%x\n",
2756 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f,
2757 (io_read(sd, 0x01) & 0x70) >> 4);
2758
2759 v4l2_info(sd, "-----Video Timings-----\n");
2760 if (no_cp_signal(sd)) {
2761 v4l2_info(sd, "STDI: not locked\n");
2762 } else {
Hans Verkuil28a769f2015-06-07 07:32:31 -03002763 u32 bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
2764 u32 lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
2765 u32 lcvs = cp_read(sd, 0xb3) >> 3;
2766 u32 fcl = ((cp_read(sd, 0xb8) & 0x1f) << 8) | cp_read(sd, 0xb9);
Hans Verkuila89bcd42013-08-22 06:14:22 -03002767 char hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
2768 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
2769 char vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
2770 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
2771 v4l2_info(sd,
2772 "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, fcl = %d, %s, %chsync, %cvsync\n",
2773 lcf, bl, lcvs, fcl,
2774 (cp_read(sd, 0xb1) & 0x40) ?
2775 "interlaced" : "progressive",
2776 hs_pol, vs_pol);
2777 }
2778 if (adv7842_query_dv_timings(sd, &timings))
2779 v4l2_info(sd, "No video detected\n");
2780 else
2781 v4l2_print_dv_timings(sd->name, "Detected format: ",
2782 &timings, true);
2783 v4l2_print_dv_timings(sd->name, "Configured format: ",
2784 &state->timings, true);
2785
2786 if (no_cp_signal(sd))
2787 return 0;
2788
2789 v4l2_info(sd, "-----Color space-----\n");
2790 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
2791 rgb_quantization_range_txt[state->rgb_quantization_range]);
2792 v4l2_info(sd, "Input color space: %s\n",
2793 input_color_space_txt[reg_io_0x02 >> 4]);
Hans Verkuilfd742462016-06-28 11:43:01 -03002794 v4l2_info(sd, "Output color space: %s %s, alt-gamma %s\n",
Hans Verkuila89bcd42013-08-22 06:14:22 -03002795 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
Hans Verkuilfd742462016-06-28 11:43:01 -03002796 (((reg_io_0x02 >> 2) & 0x01) ^ (reg_io_0x02 & 0x01)) ?
2797 "(16-235)" : "(0-255)",
2798 (reg_io_0x02 & 0x08) ? "enabled" : "disabled");
Hans Verkuila89bcd42013-08-22 06:14:22 -03002799 v4l2_info(sd, "Color space conversion: %s\n",
2800 csc_coeff_sel_rb[cp_read(sd, 0xf4) >> 4]);
2801
2802 if (!is_digital_input(sd))
2803 return 0;
2804
2805 v4l2_info(sd, "-----%s status-----\n", is_hdmi(sd) ? "HDMI" : "DVI-D");
2806 v4l2_info(sd, "HDCP encrypted content: %s\n",
2807 (hdmi_read(sd, 0x05) & 0x40) ? "true" : "false");
2808 v4l2_info(sd, "HDCP keys read: %s%s\n",
2809 (hdmi_read(sd, 0x04) & 0x20) ? "yes" : "no",
2810 (hdmi_read(sd, 0x04) & 0x10) ? "ERROR" : "");
2811 if (!is_hdmi(sd))
2812 return 0;
2813
2814 v4l2_info(sd, "Audio: pll %s, samples %s, %s\n",
2815 audio_pll_locked ? "locked" : "not locked",
2816 audio_sample_packet_detect ? "detected" : "not detected",
2817 audio_mute ? "muted" : "enabled");
2818 if (audio_pll_locked && audio_sample_packet_detect) {
2819 v4l2_info(sd, "Audio format: %s\n",
2820 (hdmi_read(sd, 0x07) & 0x40) ? "multi-channel" : "stereo");
2821 }
2822 v4l2_info(sd, "Audio CTS: %u\n", (hdmi_read(sd, 0x5b) << 12) +
2823 (hdmi_read(sd, 0x5c) << 8) +
2824 (hdmi_read(sd, 0x5d) & 0xf0));
2825 v4l2_info(sd, "Audio N: %u\n", ((hdmi_read(sd, 0x5d) & 0x0f) << 16) +
2826 (hdmi_read(sd, 0x5e) << 8) +
2827 hdmi_read(sd, 0x5f));
2828 v4l2_info(sd, "AV Mute: %s\n",
2829 (hdmi_read(sd, 0x04) & 0x40) ? "on" : "off");
2830 v4l2_info(sd, "Deep color mode: %s\n",
2831 deep_color_mode_txt[hdmi_read(sd, 0x0b) >> 6]);
2832
Martin Bugge09f90c52014-12-19 09:14:23 -03002833 adv7842_log_infoframes(sd);
2834
Hans Verkuila89bcd42013-08-22 06:14:22 -03002835 return 0;
2836}
2837
2838static int adv7842_log_status(struct v4l2_subdev *sd)
2839{
2840 struct adv7842_state *state = to_state(sd);
2841
2842 if (state->mode == ADV7842_MODE_SDP)
2843 return adv7842_sdp_log_status(sd);
2844 return adv7842_cp_log_status(sd);
2845}
2846
2847static int adv7842_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
2848{
2849 struct adv7842_state *state = to_state(sd);
2850
2851 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2852
2853 if (state->mode != ADV7842_MODE_SDP)
2854 return -ENODATA;
2855
2856 if (!(sdp_read(sd, 0x5A) & 0x01)) {
2857 *std = 0;
2858 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
2859 return 0;
2860 }
2861
2862 switch (sdp_read(sd, 0x52) & 0x0f) {
2863 case 0:
2864 /* NTSC-M/J */
2865 *std &= V4L2_STD_NTSC;
2866 break;
2867 case 2:
2868 /* NTSC-443 */
2869 *std &= V4L2_STD_NTSC_443;
2870 break;
2871 case 3:
2872 /* 60HzSECAM */
2873 *std &= V4L2_STD_SECAM;
2874 break;
2875 case 4:
2876 /* PAL-M */
2877 *std &= V4L2_STD_PAL_M;
2878 break;
2879 case 6:
2880 /* PAL-60 */
2881 *std &= V4L2_STD_PAL_60;
2882 break;
2883 case 0xc:
2884 /* PAL-CombN */
2885 *std &= V4L2_STD_PAL_Nc;
2886 break;
2887 case 0xe:
2888 /* PAL-BGHID */
2889 *std &= V4L2_STD_PAL;
2890 break;
2891 case 0xf:
2892 /* SECAM */
2893 *std &= V4L2_STD_SECAM;
2894 break;
2895 default:
2896 *std &= V4L2_STD_ALL;
2897 break;
2898 }
2899 return 0;
2900}
2901
Martin Bugge3c4da742013-12-05 11:52:39 -03002902static void adv7842_s_sdp_io(struct v4l2_subdev *sd, struct adv7842_sdp_io_sync_adjustment *s)
2903{
2904 if (s && s->adjust) {
2905 sdp_io_write(sd, 0x94, (s->hs_beg >> 8) & 0xf);
2906 sdp_io_write(sd, 0x95, s->hs_beg & 0xff);
2907 sdp_io_write(sd, 0x96, (s->hs_width >> 8) & 0xf);
2908 sdp_io_write(sd, 0x97, s->hs_width & 0xff);
2909 sdp_io_write(sd, 0x98, (s->de_beg >> 8) & 0xf);
2910 sdp_io_write(sd, 0x99, s->de_beg & 0xff);
2911 sdp_io_write(sd, 0x9a, (s->de_end >> 8) & 0xf);
2912 sdp_io_write(sd, 0x9b, s->de_end & 0xff);
Martin Bugge15058aa2013-12-05 12:22:53 -03002913 sdp_io_write(sd, 0xa8, s->vs_beg_o);
2914 sdp_io_write(sd, 0xa9, s->vs_beg_e);
2915 sdp_io_write(sd, 0xaa, s->vs_end_o);
2916 sdp_io_write(sd, 0xab, s->vs_end_e);
Martin Bugge3c4da742013-12-05 11:52:39 -03002917 sdp_io_write(sd, 0xac, s->de_v_beg_o);
2918 sdp_io_write(sd, 0xad, s->de_v_beg_e);
2919 sdp_io_write(sd, 0xae, s->de_v_end_o);
2920 sdp_io_write(sd, 0xaf, s->de_v_end_e);
2921 } else {
2922 /* set to default */
2923 sdp_io_write(sd, 0x94, 0x00);
2924 sdp_io_write(sd, 0x95, 0x00);
2925 sdp_io_write(sd, 0x96, 0x00);
2926 sdp_io_write(sd, 0x97, 0x20);
2927 sdp_io_write(sd, 0x98, 0x00);
2928 sdp_io_write(sd, 0x99, 0x00);
2929 sdp_io_write(sd, 0x9a, 0x00);
2930 sdp_io_write(sd, 0x9b, 0x00);
Martin Bugge15058aa2013-12-05 12:22:53 -03002931 sdp_io_write(sd, 0xa8, 0x04);
2932 sdp_io_write(sd, 0xa9, 0x04);
2933 sdp_io_write(sd, 0xaa, 0x04);
2934 sdp_io_write(sd, 0xab, 0x04);
Martin Bugge3c4da742013-12-05 11:52:39 -03002935 sdp_io_write(sd, 0xac, 0x04);
2936 sdp_io_write(sd, 0xad, 0x04);
2937 sdp_io_write(sd, 0xae, 0x04);
2938 sdp_io_write(sd, 0xaf, 0x04);
2939 }
2940}
2941
Hans Verkuila89bcd42013-08-22 06:14:22 -03002942static int adv7842_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
2943{
2944 struct adv7842_state *state = to_state(sd);
Martin Bugge3c4da742013-12-05 11:52:39 -03002945 struct adv7842_platform_data *pdata = &state->pdata;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002946
2947 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2948
2949 if (state->mode != ADV7842_MODE_SDP)
2950 return -ENODATA;
2951
Martin Bugge3c4da742013-12-05 11:52:39 -03002952 if (norm & V4L2_STD_625_50)
2953 adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_625);
2954 else if (norm & V4L2_STD_525_60)
2955 adv7842_s_sdp_io(sd, &pdata->sdp_io_sync_525);
2956 else
2957 adv7842_s_sdp_io(sd, NULL);
2958
Hans Verkuila89bcd42013-08-22 06:14:22 -03002959 if (norm & V4L2_STD_ALL) {
2960 state->norm = norm;
2961 return 0;
2962 }
2963 return -EINVAL;
2964}
2965
2966static int adv7842_g_std(struct v4l2_subdev *sd, v4l2_std_id *norm)
2967{
2968 struct adv7842_state *state = to_state(sd);
2969
2970 v4l2_dbg(1, debug, sd, "%s:\n", __func__);
2971
2972 if (state->mode != ADV7842_MODE_SDP)
2973 return -ENODATA;
2974
2975 *norm = state->norm;
2976 return 0;
2977}
2978
2979/* ----------------------------------------------------------------------- */
2980
Hans Verkuil69e9ba62013-12-20 05:44:27 -03002981static int adv7842_core_init(struct v4l2_subdev *sd)
Hans Verkuila89bcd42013-08-22 06:14:22 -03002982{
Hans Verkuil69e9ba62013-12-20 05:44:27 -03002983 struct adv7842_state *state = to_state(sd);
2984 struct adv7842_platform_data *pdata = &state->pdata;
Hans Verkuila89bcd42013-08-22 06:14:22 -03002985 hdmi_write(sd, 0x48,
2986 (pdata->disable_pwrdnb ? 0x80 : 0) |
2987 (pdata->disable_cable_det_rst ? 0x40 : 0));
2988
2989 disable_input(sd);
2990
Martin Bugge2ff0f162014-03-19 06:43:45 -03002991 /*
2992 * Disable I2C access to internal EDID ram from HDMI DDC ports
2993 * Disable auto edid enable when leaving powerdown mode
2994 */
2995 rep_write_and_or(sd, 0x77, 0xd3, 0x20);
2996
Hans Verkuila89bcd42013-08-22 06:14:22 -03002997 /* power */
2998 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
2999 io_write(sd, 0x15, 0x80); /* Power up pads */
3000
3001 /* video format */
Hans Verkuilfd742462016-06-28 11:43:01 -03003002 io_write(sd, 0x02, 0xf0 | pdata->alt_gamma << 3);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003003 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
3004 pdata->insert_av_codes << 2 |
Hans Verkuilf888ae72015-05-01 11:31:30 -03003005 pdata->replicate_av_codes << 1);
3006 adv7842_setup_format(state);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003007
Mats Randgaard5b64b202013-12-05 12:08:45 -03003008 /* HDMI audio */
3009 hdmi_write_and_or(sd, 0x1a, 0xf1, 0x08); /* Wait 1 s before unmute */
3010
Hans Verkuila89bcd42013-08-22 06:14:22 -03003011 /* Drive strength */
Hans Verkuil7f95c902013-12-20 06:15:13 -03003012 io_write_and_or(sd, 0x14, 0xc0,
3013 pdata->dr_str_data << 4 |
3014 pdata->dr_str_clk << 2 |
3015 pdata->dr_str_sync);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003016
3017 /* HDMI free run */
Martin Buggef0ec1742013-12-20 06:02:24 -03003018 cp_write_and_or(sd, 0xba, 0xfc, pdata->hdmi_free_run_enable |
3019 (pdata->hdmi_free_run_mode << 1));
3020
3021 /* SPD free run */
3022 sdp_write_and_or(sd, 0xdd, 0xf0, pdata->sdp_free_run_force |
3023 (pdata->sdp_free_run_cbar_en << 1) |
3024 (pdata->sdp_free_run_man_col_en << 2) |
Martin Bugge57f05472014-01-29 06:50:20 -03003025 (pdata->sdp_free_run_auto << 3));
Hans Verkuila89bcd42013-08-22 06:14:22 -03003026
3027 /* TODO from platform data */
3028 cp_write(sd, 0x69, 0x14); /* Enable CP CSC */
3029 io_write(sd, 0x06, 0xa6); /* positive VS and HS and DE */
3030 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
3031 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
3032
3033 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
3034 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
3035
3036 sdp_csc_coeff(sd, &pdata->sdp_csc_coeff);
3037
Hans Verkuila89bcd42013-08-22 06:14:22 -03003038 /* todo, improve settings for sdram */
3039 if (pdata->sd_ram_size >= 128) {
3040 sdp_write(sd, 0x12, 0x0d); /* Frame TBC,3D comb enabled */
3041 if (pdata->sd_ram_ddr) {
3042 /* SDP setup for the AD eval board */
3043 sdp_io_write(sd, 0x6f, 0x00); /* DDR mode */
3044 sdp_io_write(sd, 0x75, 0x0a); /* 128 MB memory size */
3045 sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
3046 sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
3047 sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
3048 } else {
3049 sdp_io_write(sd, 0x75, 0x0a); /* 64 MB memory size ?*/
3050 sdp_io_write(sd, 0x74, 0x00); /* must be zero for sdr sdram */
3051 sdp_io_write(sd, 0x79, 0x33); /* CAS latency to 3,
3052 depends on memory */
3053 sdp_io_write(sd, 0x6f, 0x01); /* SDR mode */
3054 sdp_io_write(sd, 0x7a, 0xa5); /* Timing Adjustment */
3055 sdp_io_write(sd, 0x7b, 0x8f); /* Timing Adjustment */
3056 sdp_io_write(sd, 0x60, 0x01); /* SDRAM reset */
3057 }
3058 } else {
3059 /*
3060 * Manual UG-214, rev 0 is bit confusing on this bit
3061 * but a '1' disables any signal if the Ram is active.
3062 */
3063 sdp_io_write(sd, 0x29, 0x10); /* Tristate memory interface */
3064 }
3065
3066 select_input(sd, pdata->vid_std_select);
3067
3068 enable_input(sd);
3069
Martin Buggece2d2b22014-01-24 10:50:06 -03003070 if (pdata->hpa_auto) {
3071 /* HPA auto, HPA 0.5s after Edid set and Cable detect */
3072 hdmi_write(sd, 0x69, 0x5c);
3073 } else {
3074 /* HPA manual */
3075 hdmi_write(sd, 0x69, 0xa3);
3076 /* HPA disable on port A and B */
3077 io_write_and_or(sd, 0x20, 0xcf, 0x00);
3078 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03003079
3080 /* LLC */
Hans Verkuilfe808f32013-12-20 06:03:58 -03003081 io_write(sd, 0x19, 0x80 | pdata->llc_dll_phase);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003082 io_write(sd, 0x33, 0x40);
3083
3084 /* interrupts */
Martin Buggec9f1f272013-12-10 11:14:26 -03003085 io_write(sd, 0x40, 0xf2); /* Configure INT1 */
Hans Verkuila89bcd42013-08-22 06:14:22 -03003086
3087 adv7842_irq_enable(sd, true);
3088
3089 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
3090}
3091
3092/* ----------------------------------------------------------------------- */
3093
3094static int adv7842_ddr_ram_test(struct v4l2_subdev *sd)
3095{
3096 /*
3097 * From ADV784x external Memory test.pdf
3098 *
3099 * Reset must just been performed before running test.
3100 * Recommended to reset after test.
3101 */
3102 int i;
3103 int pass = 0;
3104 int fail = 0;
3105 int complete = 0;
3106
3107 io_write(sd, 0x00, 0x01); /* Program SDP 4x1 */
3108 io_write(sd, 0x01, 0x00); /* Program SDP mode */
3109 afe_write(sd, 0x80, 0x92); /* SDP Recommeneded Write */
3110 afe_write(sd, 0x9B, 0x01); /* SDP Recommeneded Write ADV7844ES1 */
3111 afe_write(sd, 0x9C, 0x60); /* SDP Recommeneded Write ADV7844ES1 */
3112 afe_write(sd, 0x9E, 0x02); /* SDP Recommeneded Write ADV7844ES1 */
3113 afe_write(sd, 0xA0, 0x0B); /* SDP Recommeneded Write ADV7844ES1 */
3114 afe_write(sd, 0xC3, 0x02); /* Memory BIST Initialisation */
3115 io_write(sd, 0x0C, 0x40); /* Power up ADV7844 */
3116 io_write(sd, 0x15, 0xBA); /* Enable outputs */
3117 sdp_write(sd, 0x12, 0x00); /* Disable 3D comb, Frame TBC & 3DNR */
3118 io_write(sd, 0xFF, 0x04); /* Reset memory controller */
3119
3120 mdelay(5);
3121
3122 sdp_write(sd, 0x12, 0x00); /* Disable 3D Comb, Frame TBC & 3DNR */
3123 sdp_io_write(sd, 0x2A, 0x01); /* Memory BIST Initialisation */
3124 sdp_io_write(sd, 0x7c, 0x19); /* Memory BIST Initialisation */
3125 sdp_io_write(sd, 0x80, 0x87); /* Memory BIST Initialisation */
3126 sdp_io_write(sd, 0x81, 0x4a); /* Memory BIST Initialisation */
3127 sdp_io_write(sd, 0x82, 0x2c); /* Memory BIST Initialisation */
3128 sdp_io_write(sd, 0x83, 0x0e); /* Memory BIST Initialisation */
3129 sdp_io_write(sd, 0x84, 0x94); /* Memory BIST Initialisation */
3130 sdp_io_write(sd, 0x85, 0x62); /* Memory BIST Initialisation */
3131 sdp_io_write(sd, 0x7d, 0x00); /* Memory BIST Initialisation */
3132 sdp_io_write(sd, 0x7e, 0x1a); /* Memory BIST Initialisation */
3133
3134 mdelay(5);
3135
3136 sdp_io_write(sd, 0xd9, 0xd5); /* Enable BIST Test */
3137 sdp_write(sd, 0x12, 0x05); /* Enable FRAME TBC & 3D COMB */
3138
3139 mdelay(20);
3140
3141 for (i = 0; i < 10; i++) {
3142 u8 result = sdp_io_read(sd, 0xdb);
3143 if (result & 0x10) {
3144 complete++;
3145 if (result & 0x20)
3146 fail++;
3147 else
3148 pass++;
3149 }
3150 mdelay(20);
3151 }
3152
3153 v4l2_dbg(1, debug, sd,
3154 "Ram Test: completed %d of %d: pass %d, fail %d\n",
3155 complete, i, pass, fail);
3156
3157 if (!complete || fail)
3158 return -EIO;
3159 return 0;
3160}
3161
3162static void adv7842_rewrite_i2c_addresses(struct v4l2_subdev *sd,
3163 struct adv7842_platform_data *pdata)
3164{
3165 io_write(sd, 0xf1, pdata->i2c_sdp << 1);
3166 io_write(sd, 0xf2, pdata->i2c_sdp_io << 1);
3167 io_write(sd, 0xf3, pdata->i2c_avlink << 1);
3168 io_write(sd, 0xf4, pdata->i2c_cec << 1);
3169 io_write(sd, 0xf5, pdata->i2c_infoframe << 1);
3170
3171 io_write(sd, 0xf8, pdata->i2c_afe << 1);
3172 io_write(sd, 0xf9, pdata->i2c_repeater << 1);
3173 io_write(sd, 0xfa, pdata->i2c_edid << 1);
3174 io_write(sd, 0xfb, pdata->i2c_hdmi << 1);
3175
3176 io_write(sd, 0xfd, pdata->i2c_cp << 1);
3177 io_write(sd, 0xfe, pdata->i2c_vdp << 1);
3178}
3179
3180static int adv7842_command_ram_test(struct v4l2_subdev *sd)
3181{
3182 struct i2c_client *client = v4l2_get_subdevdata(sd);
3183 struct adv7842_state *state = to_state(sd);
3184 struct adv7842_platform_data *pdata = client->dev.platform_data;
Martin Bugge1961b722013-12-05 12:18:14 -03003185 struct v4l2_dv_timings timings;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003186 int ret = 0;
3187
3188 if (!pdata)
3189 return -ENODEV;
3190
3191 if (!pdata->sd_ram_size || !pdata->sd_ram_ddr) {
3192 v4l2_info(sd, "no sdram or no ddr sdram\n");
3193 return -EINVAL;
3194 }
3195
3196 main_reset(sd);
3197
3198 adv7842_rewrite_i2c_addresses(sd, pdata);
3199
3200 /* run ram test */
3201 ret = adv7842_ddr_ram_test(sd);
3202
3203 main_reset(sd);
3204
3205 adv7842_rewrite_i2c_addresses(sd, pdata);
3206
3207 /* and re-init chip and state */
Hans Verkuil69e9ba62013-12-20 05:44:27 -03003208 adv7842_core_init(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003209
3210 disable_input(sd);
3211
3212 select_input(sd, state->vid_std_select);
3213
3214 enable_input(sd);
3215
Hans Verkuila89bcd42013-08-22 06:14:22 -03003216 edid_write_vga_segment(sd);
Martin Buggefc2e9912013-12-05 12:09:51 -03003217 edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_A);
3218 edid_write_hdmi_segment(sd, ADV7842_EDID_PORT_B);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003219
Martin Bugge1961b722013-12-05 12:18:14 -03003220 timings = state->timings;
3221
3222 memset(&state->timings, 0, sizeof(struct v4l2_dv_timings));
3223
3224 adv7842_s_dv_timings(sd, &timings);
3225
Hans Verkuila89bcd42013-08-22 06:14:22 -03003226 return ret;
3227}
3228
3229static long adv7842_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
3230{
3231 switch (cmd) {
3232 case ADV7842_CMD_RAM_TEST:
3233 return adv7842_command_ram_test(sd);
3234 }
3235 return -ENOTTY;
3236}
3237
Lars-Peter Clausen2cf40902015-06-24 13:50:31 -03003238static int adv7842_subscribe_event(struct v4l2_subdev *sd,
3239 struct v4l2_fh *fh,
3240 struct v4l2_event_subscription *sub)
3241{
3242 switch (sub->type) {
3243 case V4L2_EVENT_SOURCE_CHANGE:
3244 return v4l2_src_change_event_subdev_subscribe(sd, fh, sub);
3245 case V4L2_EVENT_CTRL:
3246 return v4l2_ctrl_subdev_subscribe_event(sd, fh, sub);
3247 default:
3248 return -EINVAL;
3249 }
3250}
3251
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003252static int adv7842_registered(struct v4l2_subdev *sd)
3253{
3254 struct adv7842_state *state = to_state(sd);
Hans Verkuilf51e8082016-11-25 06:23:34 -02003255 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003256 int err;
3257
Hans Verkuilf51e8082016-11-25 06:23:34 -02003258 err = cec_register_adapter(state->cec_adap, &client->dev);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003259 if (err)
3260 cec_delete_adapter(state->cec_adap);
3261 return err;
3262}
3263
3264static void adv7842_unregistered(struct v4l2_subdev *sd)
3265{
3266 struct adv7842_state *state = to_state(sd);
3267
3268 cec_unregister_adapter(state->cec_adap);
3269}
3270
Hans Verkuila89bcd42013-08-22 06:14:22 -03003271/* ----------------------------------------------------------------------- */
3272
3273static const struct v4l2_ctrl_ops adv7842_ctrl_ops = {
3274 .s_ctrl = adv7842_s_ctrl,
Hans Verkuile8979272016-01-27 11:31:42 -02003275 .g_volatile_ctrl = adv7842_g_volatile_ctrl,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003276};
3277
3278static const struct v4l2_subdev_core_ops adv7842_core_ops = {
3279 .log_status = adv7842_log_status,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003280 .ioctl = adv7842_ioctl,
3281 .interrupt_service_routine = adv7842_isr,
Lars-Peter Clausen2cf40902015-06-24 13:50:31 -03003282 .subscribe_event = adv7842_subscribe_event,
Lars-Peter Clausenaef51592015-06-24 13:50:28 -03003283 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003284#ifdef CONFIG_VIDEO_ADV_DEBUG
3285 .g_register = adv7842_g_register,
3286 .s_register = adv7842_s_register,
3287#endif
3288};
3289
3290static const struct v4l2_subdev_video_ops adv7842_video_ops = {
Laurent Pinchart8774bed2014-04-28 16:53:01 -03003291 .g_std = adv7842_g_std,
3292 .s_std = adv7842_s_std,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003293 .s_routing = adv7842_s_routing,
3294 .querystd = adv7842_querystd,
3295 .g_input_status = adv7842_g_input_status,
3296 .s_dv_timings = adv7842_s_dv_timings,
3297 .g_dv_timings = adv7842_g_dv_timings,
3298 .query_dv_timings = adv7842_query_dv_timings,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003299};
3300
3301static const struct v4l2_subdev_pad_ops adv7842_pad_ops = {
Hans Verkuilf888ae72015-05-01 11:31:30 -03003302 .enum_mbus_code = adv7842_enum_mbus_code,
3303 .get_fmt = adv7842_get_format,
3304 .set_fmt = adv7842_set_format,
Martin Bugge245b2b62013-12-05 12:14:02 -03003305 .get_edid = adv7842_get_edid,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003306 .set_edid = adv7842_set_edid,
Laurent Pinchartc9161942014-01-31 08:51:18 -03003307 .enum_dv_timings = adv7842_enum_dv_timings,
3308 .dv_timings_cap = adv7842_dv_timings_cap,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003309};
3310
3311static const struct v4l2_subdev_ops adv7842_ops = {
3312 .core = &adv7842_core_ops,
3313 .video = &adv7842_video_ops,
3314 .pad = &adv7842_pad_ops,
3315};
3316
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003317static const struct v4l2_subdev_internal_ops adv7842_int_ops = {
3318 .registered = adv7842_registered,
3319 .unregistered = adv7842_unregistered,
3320};
3321
Hans Verkuila89bcd42013-08-22 06:14:22 -03003322/* -------------------------- custom ctrls ---------------------------------- */
3323
3324static const struct v4l2_ctrl_config adv7842_ctrl_analog_sampling_phase = {
3325 .ops = &adv7842_ctrl_ops,
3326 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
3327 .name = "Analog Sampling Phase",
3328 .type = V4L2_CTRL_TYPE_INTEGER,
3329 .min = 0,
3330 .max = 0x1f,
3331 .step = 1,
3332 .def = 0,
3333};
3334
3335static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color_manual = {
3336 .ops = &adv7842_ctrl_ops,
3337 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
3338 .name = "Free Running Color, Manual",
3339 .type = V4L2_CTRL_TYPE_BOOLEAN,
3340 .max = 1,
3341 .step = 1,
3342 .def = 1,
3343};
3344
3345static const struct v4l2_ctrl_config adv7842_ctrl_free_run_color = {
3346 .ops = &adv7842_ctrl_ops,
3347 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
3348 .name = "Free Running Color",
3349 .type = V4L2_CTRL_TYPE_INTEGER,
3350 .max = 0xffffff,
3351 .step = 0x1,
3352};
3353
3354
Martin Buggeb82e2792013-12-05 12:14:45 -03003355static void adv7842_unregister_clients(struct v4l2_subdev *sd)
Hans Verkuila89bcd42013-08-22 06:14:22 -03003356{
Martin Buggeb82e2792013-12-05 12:14:45 -03003357 struct adv7842_state *state = to_state(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003358 if (state->i2c_avlink)
3359 i2c_unregister_device(state->i2c_avlink);
3360 if (state->i2c_cec)
3361 i2c_unregister_device(state->i2c_cec);
3362 if (state->i2c_infoframe)
3363 i2c_unregister_device(state->i2c_infoframe);
3364 if (state->i2c_sdp_io)
3365 i2c_unregister_device(state->i2c_sdp_io);
3366 if (state->i2c_sdp)
3367 i2c_unregister_device(state->i2c_sdp);
3368 if (state->i2c_afe)
3369 i2c_unregister_device(state->i2c_afe);
3370 if (state->i2c_repeater)
3371 i2c_unregister_device(state->i2c_repeater);
3372 if (state->i2c_edid)
3373 i2c_unregister_device(state->i2c_edid);
3374 if (state->i2c_hdmi)
3375 i2c_unregister_device(state->i2c_hdmi);
3376 if (state->i2c_cp)
3377 i2c_unregister_device(state->i2c_cp);
3378 if (state->i2c_vdp)
3379 i2c_unregister_device(state->i2c_vdp);
Martin Buggeb82e2792013-12-05 12:14:45 -03003380
3381 state->i2c_avlink = NULL;
3382 state->i2c_cec = NULL;
3383 state->i2c_infoframe = NULL;
3384 state->i2c_sdp_io = NULL;
3385 state->i2c_sdp = NULL;
3386 state->i2c_afe = NULL;
3387 state->i2c_repeater = NULL;
3388 state->i2c_edid = NULL;
3389 state->i2c_hdmi = NULL;
3390 state->i2c_cp = NULL;
3391 state->i2c_vdp = NULL;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003392}
3393
Martin Buggeb82e2792013-12-05 12:14:45 -03003394static struct i2c_client *adv7842_dummy_client(struct v4l2_subdev *sd, const char *desc,
Hans Verkuila89bcd42013-08-22 06:14:22 -03003395 u8 addr, u8 io_reg)
3396{
3397 struct i2c_client *client = v4l2_get_subdevdata(sd);
Martin Buggeb82e2792013-12-05 12:14:45 -03003398 struct i2c_client *cp;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003399
3400 io_write(sd, io_reg, addr << 1);
Martin Buggeb82e2792013-12-05 12:14:45 -03003401
3402 if (addr == 0) {
3403 v4l2_err(sd, "no %s i2c addr configured\n", desc);
3404 return NULL;
3405 }
3406
3407 cp = i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
3408 if (!cp)
3409 v4l2_err(sd, "register %s on i2c addr 0x%x failed\n", desc, addr);
3410
3411 return cp;
3412}
3413
3414static int adv7842_register_clients(struct v4l2_subdev *sd)
3415{
3416 struct adv7842_state *state = to_state(sd);
3417 struct adv7842_platform_data *pdata = &state->pdata;
3418
3419 state->i2c_avlink = adv7842_dummy_client(sd, "avlink", pdata->i2c_avlink, 0xf3);
3420 state->i2c_cec = adv7842_dummy_client(sd, "cec", pdata->i2c_cec, 0xf4);
3421 state->i2c_infoframe = adv7842_dummy_client(sd, "infoframe", pdata->i2c_infoframe, 0xf5);
3422 state->i2c_sdp_io = adv7842_dummy_client(sd, "sdp_io", pdata->i2c_sdp_io, 0xf2);
3423 state->i2c_sdp = adv7842_dummy_client(sd, "sdp", pdata->i2c_sdp, 0xf1);
3424 state->i2c_afe = adv7842_dummy_client(sd, "afe", pdata->i2c_afe, 0xf8);
3425 state->i2c_repeater = adv7842_dummy_client(sd, "repeater", pdata->i2c_repeater, 0xf9);
3426 state->i2c_edid = adv7842_dummy_client(sd, "edid", pdata->i2c_edid, 0xfa);
3427 state->i2c_hdmi = adv7842_dummy_client(sd, "hdmi", pdata->i2c_hdmi, 0xfb);
3428 state->i2c_cp = adv7842_dummy_client(sd, "cp", pdata->i2c_cp, 0xfd);
3429 state->i2c_vdp = adv7842_dummy_client(sd, "vdp", pdata->i2c_vdp, 0xfe);
3430
3431 if (!state->i2c_avlink ||
3432 !state->i2c_cec ||
3433 !state->i2c_infoframe ||
3434 !state->i2c_sdp_io ||
3435 !state->i2c_sdp ||
3436 !state->i2c_afe ||
3437 !state->i2c_repeater ||
3438 !state->i2c_edid ||
3439 !state->i2c_hdmi ||
3440 !state->i2c_cp ||
3441 !state->i2c_vdp)
3442 return -1;
3443
3444 return 0;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003445}
3446
3447static int adv7842_probe(struct i2c_client *client,
3448 const struct i2c_device_id *id)
3449{
3450 struct adv7842_state *state;
Hans Verkuil0bb4e7a2013-12-17 10:09:51 -03003451 static const struct v4l2_dv_timings cea640x480 =
3452 V4L2_DV_BT_CEA_640X480P59_94;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003453 struct adv7842_platform_data *pdata = client->dev.platform_data;
3454 struct v4l2_ctrl_handler *hdl;
Hans Verkuile8979272016-01-27 11:31:42 -02003455 struct v4l2_ctrl *ctrl;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003456 struct v4l2_subdev *sd;
3457 u16 rev;
3458 int err;
3459
3460 /* Check if the adapter supports the needed features */
3461 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
3462 return -EIO;
3463
3464 v4l_dbg(1, debug, client, "detecting adv7842 client on address 0x%x\n",
3465 client->addr << 1);
3466
3467 if (!pdata) {
3468 v4l_err(client, "No platform data!\n");
3469 return -ENODEV;
3470 }
3471
3472 state = devm_kzalloc(&client->dev, sizeof(struct adv7842_state), GFP_KERNEL);
3473 if (!state) {
3474 v4l_err(client, "Could not allocate adv7842_state memory!\n");
3475 return -ENOMEM;
3476 }
3477
Martin Bugge7de5be42013-12-05 11:39:37 -03003478 /* platform data */
3479 state->pdata = *pdata;
Hans Verkuil0bb4e7a2013-12-17 10:09:51 -03003480 state->timings = cea640x480;
Hans Verkuilf888ae72015-05-01 11:31:30 -03003481 state->format = adv7842_format_info(state, MEDIA_BUS_FMT_YUYV8_2X8);
Martin Bugge7de5be42013-12-05 11:39:37 -03003482
Hans Verkuila89bcd42013-08-22 06:14:22 -03003483 sd = &state->sd;
3484 v4l2_i2c_subdev_init(sd, client, &adv7842_ops);
Lars-Peter Clausenaef51592015-06-24 13:50:28 -03003485 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003486 sd->internal_ops = &adv7842_int_ops;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003487 state->mode = pdata->mode;
3488
Martin Bugge8e4e3632013-12-05 11:55:48 -03003489 state->hdmi_port_a = pdata->input == ADV7842_SELECT_HDMI_PORT_A;
Martin Bugge6e9071f2013-12-10 12:00:06 -03003490 state->restart_stdi_once = true;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003491
3492 /* i2c access to adv7842? */
3493 rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
3494 adv_smbus_read_byte_data_check(client, 0xeb, false);
3495 if (rev != 0x2012) {
3496 v4l2_info(sd, "got rev=0x%04x on first read attempt\n", rev);
3497 rev = adv_smbus_read_byte_data_check(client, 0xea, false) << 8 |
3498 adv_smbus_read_byte_data_check(client, 0xeb, false);
3499 }
3500 if (rev != 0x2012) {
3501 v4l2_info(sd, "not an adv7842 on address 0x%x (rev=0x%04x)\n",
3502 client->addr << 1, rev);
3503 return -ENODEV;
3504 }
3505
3506 if (pdata->chip_reset)
3507 main_reset(sd);
3508
3509 /* control handlers */
3510 hdl = &state->hdl;
3511 v4l2_ctrl_handler_init(hdl, 6);
3512
3513 /* add in ascending ID order */
3514 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3515 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
3516 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3517 V4L2_CID_CONTRAST, 0, 255, 1, 128);
3518 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3519 V4L2_CID_SATURATION, 0, 255, 1, 128);
3520 v4l2_ctrl_new_std(hdl, &adv7842_ctrl_ops,
3521 V4L2_CID_HUE, 0, 128, 1, 0);
Hans Verkuile8979272016-01-27 11:31:42 -02003522 ctrl = v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
3523 V4L2_CID_DV_RX_IT_CONTENT_TYPE, V4L2_DV_IT_CONTENT_TYPE_NO_ITC,
3524 0, V4L2_DV_IT_CONTENT_TYPE_NO_ITC);
3525 if (ctrl)
3526 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
Hans Verkuila89bcd42013-08-22 06:14:22 -03003527
3528 /* custom controls */
3529 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
3530 V4L2_CID_DV_RX_POWER_PRESENT, 0, 3, 0, 0);
3531 state->analog_sampling_phase_ctrl = v4l2_ctrl_new_custom(hdl,
3532 &adv7842_ctrl_analog_sampling_phase, NULL);
3533 state->free_run_color_ctrl_manual = v4l2_ctrl_new_custom(hdl,
3534 &adv7842_ctrl_free_run_color_manual, NULL);
3535 state->free_run_color_ctrl = v4l2_ctrl_new_custom(hdl,
3536 &adv7842_ctrl_free_run_color, NULL);
3537 state->rgb_quantization_range_ctrl =
3538 v4l2_ctrl_new_std_menu(hdl, &adv7842_ctrl_ops,
3539 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
3540 0, V4L2_DV_RGB_RANGE_AUTO);
3541 sd->ctrl_handler = hdl;
3542 if (hdl->error) {
3543 err = hdl->error;
3544 goto err_hdl;
3545 }
Hans Verkuila89bcd42013-08-22 06:14:22 -03003546 if (adv7842_s_detect_tx_5v_ctrl(sd)) {
3547 err = -ENODEV;
3548 goto err_hdl;
3549 }
3550
Martin Buggeb82e2792013-12-05 12:14:45 -03003551 if (adv7842_register_clients(sd) < 0) {
Hans Verkuila89bcd42013-08-22 06:14:22 -03003552 err = -ENOMEM;
3553 v4l2_err(sd, "failed to create all i2c clients\n");
3554 goto err_i2c;
3555 }
3556
Hans Verkuila89bcd42013-08-22 06:14:22 -03003557
3558 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
3559 adv7842_delayed_work_enable_hotplug);
3560
3561 state->pad.flags = MEDIA_PAD_FL_SOURCE;
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -02003562 err = media_entity_pads_init(&sd->entity, 1, &state->pad);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003563 if (err)
3564 goto err_work_queues;
3565
Martin Bugge7de5be42013-12-05 11:39:37 -03003566 err = adv7842_core_init(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003567 if (err)
3568 goto err_entity;
3569
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003570#if IS_ENABLED(CONFIG_VIDEO_ADV7842_CEC)
3571 state->cec_adap = cec_allocate_adapter(&adv7842_cec_adap_ops,
3572 state, dev_name(&client->dev),
Hans Verkuil57b79632017-08-04 06:41:52 -04003573 CEC_CAP_DEFAULTS, ADV7842_MAX_ADDRS);
Hans Verkuil25c84fb2015-09-07 08:13:26 -03003574 err = PTR_ERR_OR_ZERO(state->cec_adap);
3575 if (err)
3576 goto err_entity;
3577#endif
3578
Hans Verkuila89bcd42013-08-22 06:14:22 -03003579 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
3580 client->addr << 1, client->adapter->name);
3581 return 0;
3582
3583err_entity:
3584 media_entity_cleanup(&sd->entity);
3585err_work_queues:
3586 cancel_delayed_work(&state->delayed_work_enable_hotplug);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003587err_i2c:
Martin Buggeb82e2792013-12-05 12:14:45 -03003588 adv7842_unregister_clients(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003589err_hdl:
3590 v4l2_ctrl_handler_free(hdl);
3591 return err;
3592}
3593
3594/* ----------------------------------------------------------------------- */
3595
3596static int adv7842_remove(struct i2c_client *client)
3597{
3598 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3599 struct adv7842_state *state = to_state(sd);
3600
3601 adv7842_irq_enable(sd, false);
Yang Yingliang9e3a4c72021-04-06 15:50:53 +02003602 cancel_delayed_work_sync(&state->delayed_work_enable_hotplug);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003603 v4l2_device_unregister_subdev(sd);
3604 media_entity_cleanup(&sd->entity);
Martin Buggeb82e2792013-12-05 12:14:45 -03003605 adv7842_unregister_clients(sd);
Hans Verkuila89bcd42013-08-22 06:14:22 -03003606 v4l2_ctrl_handler_free(sd->ctrl_handler);
3607 return 0;
3608}
3609
3610/* ----------------------------------------------------------------------- */
3611
Arvind Yadav77c6cba2017-08-19 15:20:44 -04003612static const struct i2c_device_id adv7842_id[] = {
Hans Verkuila89bcd42013-08-22 06:14:22 -03003613 { "adv7842", 0 },
3614 { }
3615};
3616MODULE_DEVICE_TABLE(i2c, adv7842_id);
3617
3618/* ----------------------------------------------------------------------- */
3619
3620static struct i2c_driver adv7842_driver = {
3621 .driver = {
Hans Verkuila89bcd42013-08-22 06:14:22 -03003622 .name = "adv7842",
3623 },
3624 .probe = adv7842_probe,
3625 .remove = adv7842_remove,
3626 .id_table = adv7842_id,
3627};
3628
3629module_i2c_driver(adv7842_driver);