blob: 74a18c0fc10dfa7329e1f031c25ee3234f3cf73f [file] [log] [blame]
Hans Verkuil54450f52012-07-18 05:45:16 -03001/*
2 * adv7604 - Analog Devices ADV7604 video decoder driver
3 *
4 * Copyright 2012 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):
23 * REF_01 - Analog devices, ADV7604, Register Settings Recommendations,
24 * Revision 2.5, June 2010
25 * REF_02 - Analog devices, Register map documentation, Documentation of
26 * the register maps, Software manual, Rev. F, June 2010
27 * REF_03 - Analog devices, ADV7604, Hardware Manual, Rev. F, August 2010
28 */
29
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/slab.h>
34#include <linux/i2c.h>
35#include <linux/delay.h>
36#include <linux/videodev2.h>
37#include <linux/workqueue.h>
38#include <linux/v4l2-dv-timings.h>
39#include <media/v4l2-device.h>
40#include <media/v4l2-ctrls.h>
41#include <media/v4l2-chip-ident.h>
42#include <media/adv7604.h>
43
44static int debug;
45module_param(debug, int, 0644);
46MODULE_PARM_DESC(debug, "debug level (0-2)");
47
48MODULE_DESCRIPTION("Analog Devices ADV7604 video decoder driver");
49MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
50MODULE_AUTHOR("Mats Randgaard <mats.randgaard@cisco.com>");
51MODULE_LICENSE("GPL");
52
53/* ADV7604 system clock frequency */
54#define ADV7604_fsc (28636360)
55
Hans Verkuil6b0d5d32012-10-16 06:40:45 -030056#define DIGITAL_INPUT (state->mode == ADV7604_MODE_HDMI)
Hans Verkuil54450f52012-07-18 05:45:16 -030057
58/*
59 **********************************************************************
60 *
61 * Arrays with configuration parameters for the ADV7604
62 *
63 **********************************************************************
64 */
65struct adv7604_state {
66 struct adv7604_platform_data pdata;
67 struct v4l2_subdev sd;
68 struct media_pad pad;
69 struct v4l2_ctrl_handler hdl;
Hans Verkuil6b0d5d32012-10-16 06:40:45 -030070 enum adv7604_mode mode;
Hans Verkuil54450f52012-07-18 05:45:16 -030071 struct v4l2_dv_timings timings;
72 u8 edid[256];
73 unsigned edid_blocks;
74 struct v4l2_fract aspect_ratio;
75 u32 rgb_quantization_range;
76 struct workqueue_struct *work_queues;
77 struct delayed_work delayed_work_enable_hotplug;
78 bool connector_hdmi;
79
80 /* i2c clients */
81 struct i2c_client *i2c_avlink;
82 struct i2c_client *i2c_cec;
83 struct i2c_client *i2c_infoframe;
84 struct i2c_client *i2c_esdp;
85 struct i2c_client *i2c_dpp;
86 struct i2c_client *i2c_afe;
87 struct i2c_client *i2c_repeater;
88 struct i2c_client *i2c_edid;
89 struct i2c_client *i2c_hdmi;
90 struct i2c_client *i2c_test;
91 struct i2c_client *i2c_cp;
92 struct i2c_client *i2c_vdp;
93
94 /* controls */
95 struct v4l2_ctrl *detect_tx_5v_ctrl;
96 struct v4l2_ctrl *analog_sampling_phase_ctrl;
97 struct v4l2_ctrl *free_run_color_manual_ctrl;
98 struct v4l2_ctrl *free_run_color_ctrl;
99 struct v4l2_ctrl *rgb_quantization_range_ctrl;
100};
101
102/* Supported CEA and DMT timings */
103static const struct v4l2_dv_timings adv7604_timings[] = {
104 V4L2_DV_BT_CEA_720X480P59_94,
105 V4L2_DV_BT_CEA_720X576P50,
106 V4L2_DV_BT_CEA_1280X720P24,
107 V4L2_DV_BT_CEA_1280X720P25,
108 V4L2_DV_BT_CEA_1280X720P30,
109 V4L2_DV_BT_CEA_1280X720P50,
110 V4L2_DV_BT_CEA_1280X720P60,
111 V4L2_DV_BT_CEA_1920X1080P24,
112 V4L2_DV_BT_CEA_1920X1080P25,
113 V4L2_DV_BT_CEA_1920X1080P30,
114 V4L2_DV_BT_CEA_1920X1080P50,
115 V4L2_DV_BT_CEA_1920X1080P60,
116
117 V4L2_DV_BT_DMT_640X350P85,
118 V4L2_DV_BT_DMT_640X400P85,
119 V4L2_DV_BT_DMT_720X400P85,
120 V4L2_DV_BT_DMT_640X480P60,
121 V4L2_DV_BT_DMT_640X480P72,
122 V4L2_DV_BT_DMT_640X480P75,
123 V4L2_DV_BT_DMT_640X480P85,
124 V4L2_DV_BT_DMT_800X600P56,
125 V4L2_DV_BT_DMT_800X600P60,
126 V4L2_DV_BT_DMT_800X600P72,
127 V4L2_DV_BT_DMT_800X600P75,
128 V4L2_DV_BT_DMT_800X600P85,
129 V4L2_DV_BT_DMT_848X480P60,
130 V4L2_DV_BT_DMT_1024X768P60,
131 V4L2_DV_BT_DMT_1024X768P70,
132 V4L2_DV_BT_DMT_1024X768P75,
133 V4L2_DV_BT_DMT_1024X768P85,
134 V4L2_DV_BT_DMT_1152X864P75,
135 V4L2_DV_BT_DMT_1280X768P60_RB,
136 V4L2_DV_BT_DMT_1280X768P60,
137 V4L2_DV_BT_DMT_1280X768P75,
138 V4L2_DV_BT_DMT_1280X768P85,
139 V4L2_DV_BT_DMT_1280X800P60_RB,
140 V4L2_DV_BT_DMT_1280X800P60,
141 V4L2_DV_BT_DMT_1280X800P75,
142 V4L2_DV_BT_DMT_1280X800P85,
143 V4L2_DV_BT_DMT_1280X960P60,
144 V4L2_DV_BT_DMT_1280X960P85,
145 V4L2_DV_BT_DMT_1280X1024P60,
146 V4L2_DV_BT_DMT_1280X1024P75,
147 V4L2_DV_BT_DMT_1280X1024P85,
148 V4L2_DV_BT_DMT_1360X768P60,
149 V4L2_DV_BT_DMT_1400X1050P60_RB,
150 V4L2_DV_BT_DMT_1400X1050P60,
151 V4L2_DV_BT_DMT_1400X1050P75,
152 V4L2_DV_BT_DMT_1400X1050P85,
153 V4L2_DV_BT_DMT_1440X900P60_RB,
154 V4L2_DV_BT_DMT_1440X900P60,
155 V4L2_DV_BT_DMT_1600X1200P60,
156 V4L2_DV_BT_DMT_1680X1050P60_RB,
157 V4L2_DV_BT_DMT_1680X1050P60,
158 V4L2_DV_BT_DMT_1792X1344P60,
159 V4L2_DV_BT_DMT_1856X1392P60,
160 V4L2_DV_BT_DMT_1920X1200P60_RB,
161 V4L2_DV_BT_DMT_1366X768P60,
162 V4L2_DV_BT_DMT_1920X1080P60,
163 { },
164};
165
166/* ----------------------------------------------------------------------- */
167
168static inline struct adv7604_state *to_state(struct v4l2_subdev *sd)
169{
170 return container_of(sd, struct adv7604_state, sd);
171}
172
173static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
174{
175 return &container_of(ctrl->handler, struct adv7604_state, hdl)->sd;
176}
177
178static inline unsigned hblanking(const struct v4l2_bt_timings *t)
179{
180 return t->hfrontporch + t->hsync + t->hbackporch;
181}
182
183static inline unsigned htotal(const struct v4l2_bt_timings *t)
184{
185 return t->width + t->hfrontporch + t->hsync + t->hbackporch;
186}
187
188static inline unsigned vblanking(const struct v4l2_bt_timings *t)
189{
190 return t->vfrontporch + t->vsync + t->vbackporch;
191}
192
193static inline unsigned vtotal(const struct v4l2_bt_timings *t)
194{
195 return t->height + t->vfrontporch + t->vsync + t->vbackporch;
196}
197
198/* ----------------------------------------------------------------------- */
199
200static s32 adv_smbus_read_byte_data_check(struct i2c_client *client,
201 u8 command, bool check)
202{
203 union i2c_smbus_data data;
204
205 if (!i2c_smbus_xfer(client->adapter, client->addr, client->flags,
206 I2C_SMBUS_READ, command,
207 I2C_SMBUS_BYTE_DATA, &data))
208 return data.byte;
209 if (check)
210 v4l_err(client, "error reading %02x, %02x\n",
211 client->addr, command);
212 return -EIO;
213}
214
215static s32 adv_smbus_read_byte_data(struct i2c_client *client, u8 command)
216{
217 return adv_smbus_read_byte_data_check(client, command, true);
218}
219
220static s32 adv_smbus_write_byte_data(struct i2c_client *client,
221 u8 command, u8 value)
222{
223 union i2c_smbus_data data;
224 int err;
225 int i;
226
227 data.byte = value;
228 for (i = 0; i < 3; i++) {
229 err = i2c_smbus_xfer(client->adapter, client->addr,
230 client->flags,
231 I2C_SMBUS_WRITE, command,
232 I2C_SMBUS_BYTE_DATA, &data);
233 if (!err)
234 break;
235 }
236 if (err < 0)
237 v4l_err(client, "error writing %02x, %02x, %02x\n",
238 client->addr, command, value);
239 return err;
240}
241
242static s32 adv_smbus_write_i2c_block_data(struct i2c_client *client,
243 u8 command, unsigned length, const u8 *values)
244{
245 union i2c_smbus_data data;
246
247 if (length > I2C_SMBUS_BLOCK_MAX)
248 length = I2C_SMBUS_BLOCK_MAX;
249 data.block[0] = length;
250 memcpy(data.block + 1, values, length);
251 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
252 I2C_SMBUS_WRITE, command,
253 I2C_SMBUS_I2C_BLOCK_DATA, &data);
254}
255
256/* ----------------------------------------------------------------------- */
257
258static inline int io_read(struct v4l2_subdev *sd, u8 reg)
259{
260 struct i2c_client *client = v4l2_get_subdevdata(sd);
261
262 return adv_smbus_read_byte_data(client, reg);
263}
264
265static inline int io_write(struct v4l2_subdev *sd, u8 reg, u8 val)
266{
267 struct i2c_client *client = v4l2_get_subdevdata(sd);
268
269 return adv_smbus_write_byte_data(client, reg, val);
270}
271
272static inline int io_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
273{
274 return io_write(sd, reg, (io_read(sd, reg) & mask) | val);
275}
276
277static inline int avlink_read(struct v4l2_subdev *sd, u8 reg)
278{
279 struct adv7604_state *state = to_state(sd);
280
281 return adv_smbus_read_byte_data(state->i2c_avlink, reg);
282}
283
284static inline int avlink_write(struct v4l2_subdev *sd, u8 reg, u8 val)
285{
286 struct adv7604_state *state = to_state(sd);
287
288 return adv_smbus_write_byte_data(state->i2c_avlink, reg, val);
289}
290
291static inline int cec_read(struct v4l2_subdev *sd, u8 reg)
292{
293 struct adv7604_state *state = to_state(sd);
294
295 return adv_smbus_read_byte_data(state->i2c_cec, reg);
296}
297
298static inline int cec_write(struct v4l2_subdev *sd, u8 reg, u8 val)
299{
300 struct adv7604_state *state = to_state(sd);
301
302 return adv_smbus_write_byte_data(state->i2c_cec, reg, val);
303}
304
305static inline int cec_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
306{
307 return cec_write(sd, reg, (cec_read(sd, reg) & mask) | val);
308}
309
310static inline int infoframe_read(struct v4l2_subdev *sd, u8 reg)
311{
312 struct adv7604_state *state = to_state(sd);
313
314 return adv_smbus_read_byte_data(state->i2c_infoframe, reg);
315}
316
317static inline int infoframe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
318{
319 struct adv7604_state *state = to_state(sd);
320
321 return adv_smbus_write_byte_data(state->i2c_infoframe, reg, val);
322}
323
324static inline int esdp_read(struct v4l2_subdev *sd, u8 reg)
325{
326 struct adv7604_state *state = to_state(sd);
327
328 return adv_smbus_read_byte_data(state->i2c_esdp, reg);
329}
330
331static inline int esdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
332{
333 struct adv7604_state *state = to_state(sd);
334
335 return adv_smbus_write_byte_data(state->i2c_esdp, reg, val);
336}
337
338static inline int dpp_read(struct v4l2_subdev *sd, u8 reg)
339{
340 struct adv7604_state *state = to_state(sd);
341
342 return adv_smbus_read_byte_data(state->i2c_dpp, reg);
343}
344
345static inline int dpp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
346{
347 struct adv7604_state *state = to_state(sd);
348
349 return adv_smbus_write_byte_data(state->i2c_dpp, reg, val);
350}
351
352static inline int afe_read(struct v4l2_subdev *sd, u8 reg)
353{
354 struct adv7604_state *state = to_state(sd);
355
356 return adv_smbus_read_byte_data(state->i2c_afe, reg);
357}
358
359static inline int afe_write(struct v4l2_subdev *sd, u8 reg, u8 val)
360{
361 struct adv7604_state *state = to_state(sd);
362
363 return adv_smbus_write_byte_data(state->i2c_afe, reg, val);
364}
365
366static inline int rep_read(struct v4l2_subdev *sd, u8 reg)
367{
368 struct adv7604_state *state = to_state(sd);
369
370 return adv_smbus_read_byte_data(state->i2c_repeater, reg);
371}
372
373static inline int rep_write(struct v4l2_subdev *sd, u8 reg, u8 val)
374{
375 struct adv7604_state *state = to_state(sd);
376
377 return adv_smbus_write_byte_data(state->i2c_repeater, reg, val);
378}
379
380static inline int rep_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
381{
382 return rep_write(sd, reg, (rep_read(sd, reg) & mask) | val);
383}
384
385static inline int edid_read(struct v4l2_subdev *sd, u8 reg)
386{
387 struct adv7604_state *state = to_state(sd);
388
389 return adv_smbus_read_byte_data(state->i2c_edid, reg);
390}
391
392static inline int edid_write(struct v4l2_subdev *sd, u8 reg, u8 val)
393{
394 struct adv7604_state *state = to_state(sd);
395
396 return adv_smbus_write_byte_data(state->i2c_edid, reg, val);
397}
398
399static inline int edid_read_block(struct v4l2_subdev *sd, unsigned len, u8 *val)
400{
401 struct adv7604_state *state = to_state(sd);
402 struct i2c_client *client = state->i2c_edid;
403 u8 msgbuf0[1] = { 0 };
404 u8 msgbuf1[256];
405 struct i2c_msg msg[2] = { { client->addr, 0, 1, msgbuf0 },
406 { client->addr, 0 | I2C_M_RD, len, msgbuf1 }
407 };
408
409 if (i2c_transfer(client->adapter, msg, 2) < 0)
410 return -EIO;
411 memcpy(val, msgbuf1, len);
412 return 0;
413}
414
415static void adv7604_delayed_work_enable_hotplug(struct work_struct *work)
416{
417 struct delayed_work *dwork = to_delayed_work(work);
418 struct adv7604_state *state = container_of(dwork, struct adv7604_state,
419 delayed_work_enable_hotplug);
420 struct v4l2_subdev *sd = &state->sd;
421
422 v4l2_dbg(2, debug, sd, "%s: enable hotplug\n", __func__);
423
424 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)1);
425}
426
427static inline int edid_write_block(struct v4l2_subdev *sd,
428 unsigned len, const u8 *val)
429{
430 struct i2c_client *client = v4l2_get_subdevdata(sd);
431 struct adv7604_state *state = to_state(sd);
432 int err = 0;
433 int i;
434
435 v4l2_dbg(2, debug, sd, "%s: write EDID block (%d byte)\n", __func__, len);
436
437 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
438
439 /* Disables I2C access to internal EDID ram from DDC port */
440 rep_write_and_or(sd, 0x77, 0xf0, 0x0);
441
442 for (i = 0; !err && i < len; i += I2C_SMBUS_BLOCK_MAX)
443 err = adv_smbus_write_i2c_block_data(state->i2c_edid, i,
444 I2C_SMBUS_BLOCK_MAX, val + i);
445 if (err)
446 return err;
447
448 /* adv7604 calculates the checksums and enables I2C access to internal
449 EDID ram from DDC port. */
450 rep_write_and_or(sd, 0x77, 0xf0, 0x1);
451
452 for (i = 0; i < 1000; i++) {
453 if (rep_read(sd, 0x7d) & 1)
454 break;
455 mdelay(1);
456 }
457 if (i == 1000) {
458 v4l_err(client, "error enabling edid\n");
459 return -EIO;
460 }
461
462 /* enable hotplug after 100 ms */
463 queue_delayed_work(state->work_queues,
464 &state->delayed_work_enable_hotplug, HZ / 10);
465 return 0;
466}
467
468static inline int hdmi_read(struct v4l2_subdev *sd, u8 reg)
469{
470 struct adv7604_state *state = to_state(sd);
471
472 return adv_smbus_read_byte_data(state->i2c_hdmi, reg);
473}
474
475static inline int hdmi_write(struct v4l2_subdev *sd, u8 reg, u8 val)
476{
477 struct adv7604_state *state = to_state(sd);
478
479 return adv_smbus_write_byte_data(state->i2c_hdmi, reg, val);
480}
481
482static inline int test_read(struct v4l2_subdev *sd, u8 reg)
483{
484 struct adv7604_state *state = to_state(sd);
485
486 return adv_smbus_read_byte_data(state->i2c_test, reg);
487}
488
489static inline int test_write(struct v4l2_subdev *sd, u8 reg, u8 val)
490{
491 struct adv7604_state *state = to_state(sd);
492
493 return adv_smbus_write_byte_data(state->i2c_test, reg, val);
494}
495
496static inline int cp_read(struct v4l2_subdev *sd, u8 reg)
497{
498 struct adv7604_state *state = to_state(sd);
499
500 return adv_smbus_read_byte_data(state->i2c_cp, reg);
501}
502
503static inline int cp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
504{
505 struct adv7604_state *state = to_state(sd);
506
507 return adv_smbus_write_byte_data(state->i2c_cp, reg, val);
508}
509
510static inline int cp_write_and_or(struct v4l2_subdev *sd, u8 reg, u8 mask, u8 val)
511{
512 return cp_write(sd, reg, (cp_read(sd, reg) & mask) | val);
513}
514
515static inline int vdp_read(struct v4l2_subdev *sd, u8 reg)
516{
517 struct adv7604_state *state = to_state(sd);
518
519 return adv_smbus_read_byte_data(state->i2c_vdp, reg);
520}
521
522static inline int vdp_write(struct v4l2_subdev *sd, u8 reg, u8 val)
523{
524 struct adv7604_state *state = to_state(sd);
525
526 return adv_smbus_write_byte_data(state->i2c_vdp, reg, val);
527}
528
529/* ----------------------------------------------------------------------- */
530
531#ifdef CONFIG_VIDEO_ADV_DEBUG
532static void adv7604_inv_register(struct v4l2_subdev *sd)
533{
534 v4l2_info(sd, "0x000-0x0ff: IO Map\n");
535 v4l2_info(sd, "0x100-0x1ff: AVLink Map\n");
536 v4l2_info(sd, "0x200-0x2ff: CEC Map\n");
537 v4l2_info(sd, "0x300-0x3ff: InfoFrame Map\n");
538 v4l2_info(sd, "0x400-0x4ff: ESDP Map\n");
539 v4l2_info(sd, "0x500-0x5ff: DPP Map\n");
540 v4l2_info(sd, "0x600-0x6ff: AFE Map\n");
541 v4l2_info(sd, "0x700-0x7ff: Repeater Map\n");
542 v4l2_info(sd, "0x800-0x8ff: EDID Map\n");
543 v4l2_info(sd, "0x900-0x9ff: HDMI Map\n");
544 v4l2_info(sd, "0xa00-0xaff: Test Map\n");
545 v4l2_info(sd, "0xb00-0xbff: CP Map\n");
546 v4l2_info(sd, "0xc00-0xcff: VDP Map\n");
547}
548
549static int adv7604_g_register(struct v4l2_subdev *sd,
550 struct v4l2_dbg_register *reg)
551{
552 struct i2c_client *client = v4l2_get_subdevdata(sd);
553
554 if (!v4l2_chip_match_i2c_client(client, &reg->match))
555 return -EINVAL;
556 if (!capable(CAP_SYS_ADMIN))
557 return -EPERM;
558 reg->size = 1;
559 switch (reg->reg >> 8) {
560 case 0:
561 reg->val = io_read(sd, reg->reg & 0xff);
562 break;
563 case 1:
564 reg->val = avlink_read(sd, reg->reg & 0xff);
565 break;
566 case 2:
567 reg->val = cec_read(sd, reg->reg & 0xff);
568 break;
569 case 3:
570 reg->val = infoframe_read(sd, reg->reg & 0xff);
571 break;
572 case 4:
573 reg->val = esdp_read(sd, reg->reg & 0xff);
574 break;
575 case 5:
576 reg->val = dpp_read(sd, reg->reg & 0xff);
577 break;
578 case 6:
579 reg->val = afe_read(sd, reg->reg & 0xff);
580 break;
581 case 7:
582 reg->val = rep_read(sd, reg->reg & 0xff);
583 break;
584 case 8:
585 reg->val = edid_read(sd, reg->reg & 0xff);
586 break;
587 case 9:
588 reg->val = hdmi_read(sd, reg->reg & 0xff);
589 break;
590 case 0xa:
591 reg->val = test_read(sd, reg->reg & 0xff);
592 break;
593 case 0xb:
594 reg->val = cp_read(sd, reg->reg & 0xff);
595 break;
596 case 0xc:
597 reg->val = vdp_read(sd, reg->reg & 0xff);
598 break;
599 default:
600 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
601 adv7604_inv_register(sd);
602 break;
603 }
604 return 0;
605}
606
607static int adv7604_s_register(struct v4l2_subdev *sd,
608 struct v4l2_dbg_register *reg)
609{
610 struct i2c_client *client = v4l2_get_subdevdata(sd);
611
612 if (!v4l2_chip_match_i2c_client(client, &reg->match))
613 return -EINVAL;
614 if (!capable(CAP_SYS_ADMIN))
615 return -EPERM;
616 switch (reg->reg >> 8) {
617 case 0:
618 io_write(sd, reg->reg & 0xff, reg->val & 0xff);
619 break;
620 case 1:
621 avlink_write(sd, reg->reg & 0xff, reg->val & 0xff);
622 break;
623 case 2:
624 cec_write(sd, reg->reg & 0xff, reg->val & 0xff);
625 break;
626 case 3:
627 infoframe_write(sd, reg->reg & 0xff, reg->val & 0xff);
628 break;
629 case 4:
630 esdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
631 break;
632 case 5:
633 dpp_write(sd, reg->reg & 0xff, reg->val & 0xff);
634 break;
635 case 6:
636 afe_write(sd, reg->reg & 0xff, reg->val & 0xff);
637 break;
638 case 7:
639 rep_write(sd, reg->reg & 0xff, reg->val & 0xff);
640 break;
641 case 8:
642 edid_write(sd, reg->reg & 0xff, reg->val & 0xff);
643 break;
644 case 9:
645 hdmi_write(sd, reg->reg & 0xff, reg->val & 0xff);
646 break;
647 case 0xa:
648 test_write(sd, reg->reg & 0xff, reg->val & 0xff);
649 break;
650 case 0xb:
651 cp_write(sd, reg->reg & 0xff, reg->val & 0xff);
652 break;
653 case 0xc:
654 vdp_write(sd, reg->reg & 0xff, reg->val & 0xff);
655 break;
656 default:
657 v4l2_info(sd, "Register %03llx not supported\n", reg->reg);
658 adv7604_inv_register(sd);
659 break;
660 }
661 return 0;
662}
663#endif
664
665static int adv7604_s_detect_tx_5v_ctrl(struct v4l2_subdev *sd)
666{
667 struct adv7604_state *state = to_state(sd);
668
669 /* port A only */
670 return v4l2_ctrl_s_ctrl(state->detect_tx_5v_ctrl,
671 ((io_read(sd, 0x6f) & 0x10) >> 4));
672}
673
674static void configure_free_run(struct v4l2_subdev *sd, const struct v4l2_bt_timings *timings)
675{
676 struct i2c_client *client = v4l2_get_subdevdata(sd);
677 u32 width = htotal(timings);
678 u32 height = vtotal(timings);
679 u16 ch1_fr_ll = (((u32)timings->pixelclock / 100) > 0) ?
680 ((width * (ADV7604_fsc / 100)) / ((u32)timings->pixelclock / 100)) : 0;
681
682 v4l2_dbg(2, debug, sd, "%s\n", __func__);
683
684 cp_write(sd, 0x8f, (ch1_fr_ll >> 8) & 0x7); /* CH1_FR_LL */
685 cp_write(sd, 0x90, ch1_fr_ll & 0xff); /* CH1_FR_LL */
686 cp_write(sd, 0xab, (height >> 4) & 0xff); /* CP_LCOUNT_MAX */
687 cp_write(sd, 0xac, (height & 0x0f) << 4); /* CP_LCOUNT_MAX */
688 /* TODO support interlaced */
689 cp_write(sd, 0x91, 0x10); /* INTERLACED */
690
691 /* Should only be set in auto-graphics mode [REF_02 p. 91-92] */
692 if ((io_read(sd, 0x00) == 0x07) && (io_read(sd, 0x01) == 0x02)) {
693 u16 cp_start_sav, cp_start_eav, cp_start_vbi, cp_end_vbi;
694 const u8 pll[2] = {
695 (0xc0 | ((width >> 8) & 0x1f)),
696 (width & 0xff)
697 };
698
699 /* setup PLL_DIV_MAN_EN and PLL_DIV_RATIO */
700 /* IO-map reg. 0x16 and 0x17 should be written in sequence */
701 if (adv_smbus_write_i2c_block_data(client, 0x16, 2, pll)) {
702 v4l2_err(sd, "writing to reg 0x16 and 0x17 failed\n");
703 return;
704 }
705
706 /* active video - horizontal timing */
707 cp_start_sav = timings->hsync + timings->hbackporch - 4;
708 cp_start_eav = width - timings->hfrontporch;
709 cp_write(sd, 0xa2, (cp_start_sav >> 4) & 0xff);
710 cp_write(sd, 0xa3, ((cp_start_sav & 0x0f) << 4) | ((cp_start_eav >> 8) & 0x0f));
711 cp_write(sd, 0xa4, cp_start_eav & 0xff);
712
713 /* active video - vertical timing */
714 cp_start_vbi = height - timings->vfrontporch;
715 cp_end_vbi = timings->vsync + timings->vbackporch;
716 cp_write(sd, 0xa5, (cp_start_vbi >> 4) & 0xff);
717 cp_write(sd, 0xa6, ((cp_start_vbi & 0xf) << 4) | ((cp_end_vbi >> 8) & 0xf));
718 cp_write(sd, 0xa7, cp_end_vbi & 0xff);
719 } else {
720 /* reset to default values */
721 io_write(sd, 0x16, 0x43);
722 io_write(sd, 0x17, 0x5a);
723 cp_write(sd, 0xa2, 0x00);
724 cp_write(sd, 0xa3, 0x00);
725 cp_write(sd, 0xa4, 0x00);
726 cp_write(sd, 0xa5, 0x00);
727 cp_write(sd, 0xa6, 0x00);
728 cp_write(sd, 0xa7, 0x00);
729 }
730}
731
732
733static void set_rgb_quantization_range(struct v4l2_subdev *sd)
734{
735 struct adv7604_state *state = to_state(sd);
736
737 switch (state->rgb_quantization_range) {
738 case V4L2_DV_RGB_RANGE_AUTO:
739 /* automatic */
Hans Verkuil6b0d5d32012-10-16 06:40:45 -0300740 if (DIGITAL_INPUT && !(hdmi_read(sd, 0x05) & 0x80)) {
Hans Verkuil54450f52012-07-18 05:45:16 -0300741 /* receiving DVI-D signal */
742
743 /* ADV7604 selects RGB limited range regardless of
744 input format (CE/IT) in automatic mode */
745 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
746 /* RGB limited range (16-235) */
747 io_write_and_or(sd, 0x02, 0x0f, 0x00);
748
749 } else {
750 /* RGB full range (0-255) */
751 io_write_and_or(sd, 0x02, 0x0f, 0x10);
752 }
Hans Verkuil6b0d5d32012-10-16 06:40:45 -0300753 } else {
754 /* receiving HDMI or analog signal, set automode */
755 io_write_and_or(sd, 0x02, 0x0f, 0xf0);
Hans Verkuil54450f52012-07-18 05:45:16 -0300756 }
757 break;
758 case V4L2_DV_RGB_RANGE_LIMITED:
759 /* RGB limited range (16-235) */
760 io_write_and_or(sd, 0x02, 0x0f, 0x00);
761 break;
762 case V4L2_DV_RGB_RANGE_FULL:
763 /* RGB full range (0-255) */
764 io_write_and_or(sd, 0x02, 0x0f, 0x10);
765 break;
766 }
767}
768
769
770static int adv7604_s_ctrl(struct v4l2_ctrl *ctrl)
771{
772 struct v4l2_subdev *sd = to_sd(ctrl);
773 struct adv7604_state *state = to_state(sd);
774
775 switch (ctrl->id) {
776 case V4L2_CID_BRIGHTNESS:
777 cp_write(sd, 0x3c, ctrl->val);
778 return 0;
779 case V4L2_CID_CONTRAST:
780 cp_write(sd, 0x3a, ctrl->val);
781 return 0;
782 case V4L2_CID_SATURATION:
783 cp_write(sd, 0x3b, ctrl->val);
784 return 0;
785 case V4L2_CID_HUE:
786 cp_write(sd, 0x3d, ctrl->val);
787 return 0;
788 case V4L2_CID_DV_RX_RGB_RANGE:
789 state->rgb_quantization_range = ctrl->val;
790 set_rgb_quantization_range(sd);
791 return 0;
792 case V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE:
793 /* Set the analog sampling phase. This is needed to find the
794 best sampling phase for analog video: an application or
795 driver has to try a number of phases and analyze the picture
796 quality before settling on the best performing phase. */
797 afe_write(sd, 0xc8, ctrl->val);
798 return 0;
799 case V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL:
800 /* Use the default blue color for free running mode,
801 or supply your own. */
802 cp_write_and_or(sd, 0xbf, ~0x04, (ctrl->val << 2));
803 return 0;
804 case V4L2_CID_ADV_RX_FREE_RUN_COLOR:
805 cp_write(sd, 0xc0, (ctrl->val & 0xff0000) >> 16);
806 cp_write(sd, 0xc1, (ctrl->val & 0x00ff00) >> 8);
807 cp_write(sd, 0xc2, (u8)(ctrl->val & 0x0000ff));
808 return 0;
809 }
810 return -EINVAL;
811}
812
813static int adv7604_g_chip_ident(struct v4l2_subdev *sd,
814 struct v4l2_dbg_chip_ident *chip)
815{
816 struct i2c_client *client = v4l2_get_subdevdata(sd);
817
818 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7604, 0);
819}
820
821/* ----------------------------------------------------------------------- */
822
823static inline bool no_power(struct v4l2_subdev *sd)
824{
825 /* Entire chip or CP powered off */
826 return io_read(sd, 0x0c) & 0x24;
827}
828
829static inline bool no_signal_tmds(struct v4l2_subdev *sd)
830{
831 /* TODO port B, C and D */
832 return !(io_read(sd, 0x6a) & 0x10);
833}
834
835static inline bool no_lock_tmds(struct v4l2_subdev *sd)
836{
837 return (io_read(sd, 0x6a) & 0xe0) != 0xe0;
838}
839
840static inline bool no_lock_sspd(struct v4l2_subdev *sd)
841{
842 /* TODO channel 2 */
843 return ((cp_read(sd, 0xb5) & 0xd0) != 0xd0);
844}
845
846static inline bool no_lock_stdi(struct v4l2_subdev *sd)
847{
848 /* TODO channel 2 */
849 return !(cp_read(sd, 0xb1) & 0x80);
850}
851
852static inline bool no_signal(struct v4l2_subdev *sd)
853{
854 struct adv7604_state *state = to_state(sd);
855 bool ret;
856
857 ret = no_power(sd);
858
859 ret |= no_lock_stdi(sd);
860 ret |= no_lock_sspd(sd);
861
862 if (DIGITAL_INPUT) {
863 ret |= no_lock_tmds(sd);
864 ret |= no_signal_tmds(sd);
865 }
866
867 return ret;
868}
869
870static inline bool no_lock_cp(struct v4l2_subdev *sd)
871{
872 /* CP has detected a non standard number of lines on the incoming
873 video compared to what it is configured to receive by s_dv_timings */
874 return io_read(sd, 0x12) & 0x01;
875}
876
877static int adv7604_g_input_status(struct v4l2_subdev *sd, u32 *status)
878{
879 struct adv7604_state *state = to_state(sd);
880
881 *status = 0;
882 *status |= no_power(sd) ? V4L2_IN_ST_NO_POWER : 0;
883 *status |= no_signal(sd) ? V4L2_IN_ST_NO_SIGNAL : 0;
884 if (no_lock_cp(sd))
885 *status |= DIGITAL_INPUT ? V4L2_IN_ST_NO_SYNC : V4L2_IN_ST_NO_H_LOCK;
886
887 v4l2_dbg(1, debug, sd, "%s: status = 0x%x\n", __func__, *status);
888
889 return 0;
890}
891
892/* ----------------------------------------------------------------------- */
893
894static void adv7604_print_timings(struct v4l2_subdev *sd,
895 struct v4l2_dv_timings *timings, const char *txt, bool detailed)
896{
897 struct v4l2_bt_timings *bt = &timings->bt;
898 u32 htot, vtot;
899
900 if (timings->type != V4L2_DV_BT_656_1120)
901 return;
902
903 htot = htotal(bt);
904 vtot = vtotal(bt);
905
906 v4l2_info(sd, "%s %dx%d%s%d (%dx%d)",
907 txt, bt->width, bt->height, bt->interlaced ? "i" : "p",
908 (htot * vtot) > 0 ? ((u32)bt->pixelclock /
909 (htot * vtot)) : 0,
910 htot, vtot);
911
912 if (detailed) {
913 v4l2_info(sd, " horizontal: fp = %d, %ssync = %d, bp = %d\n",
914 bt->hfrontporch,
915 (bt->polarities & V4L2_DV_HSYNC_POS_POL) ? "+" : "-",
916 bt->hsync, bt->hbackporch);
917 v4l2_info(sd, " vertical: fp = %d, %ssync = %d, bp = %d\n",
918 bt->vfrontporch,
919 (bt->polarities & V4L2_DV_VSYNC_POS_POL) ? "+" : "-",
920 bt->vsync, bt->vbackporch);
921 v4l2_info(sd, " pixelclock: %lld, flags: 0x%x, standards: 0x%x\n",
922 bt->pixelclock, bt->flags, bt->standards);
923 }
924}
925
926struct stdi_readback {
927 u16 bl, lcf, lcvs;
928 u8 hs_pol, vs_pol;
929 bool interlaced;
930};
931
932static int stdi2dv_timings(struct v4l2_subdev *sd,
933 struct stdi_readback *stdi,
934 struct v4l2_dv_timings *timings)
935{
936 struct adv7604_state *state = to_state(sd);
937 u32 hfreq = (ADV7604_fsc * 8) / stdi->bl;
938 u32 pix_clk;
939 int i;
940
941 for (i = 0; adv7604_timings[i].bt.height; i++) {
942 if (vtotal(&adv7604_timings[i].bt) != stdi->lcf + 1)
943 continue;
944 if (adv7604_timings[i].bt.vsync != stdi->lcvs)
945 continue;
946
947 pix_clk = hfreq * htotal(&adv7604_timings[i].bt);
948
949 if ((pix_clk < adv7604_timings[i].bt.pixelclock + 1000000) &&
950 (pix_clk > adv7604_timings[i].bt.pixelclock - 1000000)) {
951 *timings = adv7604_timings[i];
952 return 0;
953 }
954 }
955
956 if (v4l2_detect_cvt(stdi->lcf + 1, hfreq, stdi->lcvs,
957 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
958 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
959 timings))
960 return 0;
961 if (v4l2_detect_gtf(stdi->lcf + 1, hfreq, stdi->lcvs,
962 (stdi->hs_pol == '+' ? V4L2_DV_HSYNC_POS_POL : 0) |
963 (stdi->vs_pol == '+' ? V4L2_DV_VSYNC_POS_POL : 0),
964 state->aspect_ratio, timings))
965 return 0;
966
967 v4l2_dbg(2, debug, sd, "%s: No format candidate found for lcf=%d, bl = %d\n",
968 __func__, stdi->lcf, stdi->bl);
969 return -1;
970}
971
972static int read_stdi(struct v4l2_subdev *sd, struct stdi_readback *stdi)
973{
974 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
975 v4l2_dbg(2, debug, sd, "%s: STDI and/or SSPD not locked\n", __func__);
976 return -1;
977 }
978
979 /* read STDI */
980 stdi->bl = ((cp_read(sd, 0xb1) & 0x3f) << 8) | cp_read(sd, 0xb2);
981 stdi->lcf = ((cp_read(sd, 0xb3) & 0x7) << 8) | cp_read(sd, 0xb4);
982 stdi->lcvs = cp_read(sd, 0xb3) >> 3;
983 stdi->interlaced = io_read(sd, 0x12) & 0x10;
984
985 /* read SSPD */
986 if ((cp_read(sd, 0xb5) & 0x03) == 0x01) {
987 stdi->hs_pol = ((cp_read(sd, 0xb5) & 0x10) ?
988 ((cp_read(sd, 0xb5) & 0x08) ? '+' : '-') : 'x');
989 stdi->vs_pol = ((cp_read(sd, 0xb5) & 0x40) ?
990 ((cp_read(sd, 0xb5) & 0x20) ? '+' : '-') : 'x');
991 } else {
992 stdi->hs_pol = 'x';
993 stdi->vs_pol = 'x';
994 }
995
996 if (no_lock_stdi(sd) || no_lock_sspd(sd)) {
997 v4l2_dbg(2, debug, sd,
998 "%s: signal lost during readout of STDI/SSPD\n", __func__);
999 return -1;
1000 }
1001
1002 if (stdi->lcf < 239 || stdi->bl < 8 || stdi->bl == 0x3fff) {
1003 v4l2_dbg(2, debug, sd, "%s: invalid signal\n", __func__);
1004 memset(stdi, 0, sizeof(struct stdi_readback));
1005 return -1;
1006 }
1007
1008 v4l2_dbg(2, debug, sd,
1009 "%s: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %chsync, %cvsync, %s\n",
1010 __func__, stdi->lcf, stdi->bl, stdi->lcvs,
1011 stdi->hs_pol, stdi->vs_pol,
1012 stdi->interlaced ? "interlaced" : "progressive");
1013
1014 return 0;
1015}
1016
1017static int adv7604_enum_dv_timings(struct v4l2_subdev *sd,
1018 struct v4l2_enum_dv_timings *timings)
1019{
1020 if (timings->index >= ARRAY_SIZE(adv7604_timings) - 1)
1021 return -EINVAL;
1022 memset(timings->reserved, 0, sizeof(timings->reserved));
1023 timings->timings = adv7604_timings[timings->index];
1024 return 0;
1025}
1026
1027static int adv7604_dv_timings_cap(struct v4l2_subdev *sd,
1028 struct v4l2_dv_timings_cap *cap)
1029{
1030 struct adv7604_state *state = to_state(sd);
1031
1032 cap->type = V4L2_DV_BT_656_1120;
1033 cap->bt.max_width = 1920;
1034 cap->bt.max_height = 1200;
1035 cap->bt.min_pixelclock = 27000000;
1036 if (DIGITAL_INPUT)
1037 cap->bt.max_pixelclock = 225000000;
1038 else
1039 cap->bt.max_pixelclock = 170000000;
1040 cap->bt.standards = V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
1041 V4L2_DV_BT_STD_GTF | V4L2_DV_BT_STD_CVT;
1042 cap->bt.capabilities = V4L2_DV_BT_CAP_PROGRESSIVE |
1043 V4L2_DV_BT_CAP_REDUCED_BLANKING | V4L2_DV_BT_CAP_CUSTOM;
1044 return 0;
1045}
1046
1047/* Fill the optional fields .standards and .flags in struct v4l2_dv_timings
1048 if the format is listed in adv7604_timings[] */
1049static void adv7604_fill_optional_dv_timings_fields(struct v4l2_subdev *sd,
1050 struct v4l2_dv_timings *timings)
1051{
1052 struct adv7604_state *state = to_state(sd);
1053 int i;
1054
1055 for (i = 0; adv7604_timings[i].bt.width; i++) {
1056 if (v4l_match_dv_timings(timings, &adv7604_timings[i],
1057 DIGITAL_INPUT ? 250000 : 1000000)) {
1058 *timings = adv7604_timings[i];
1059 break;
1060 }
1061 }
1062}
1063
1064static int adv7604_query_dv_timings(struct v4l2_subdev *sd,
1065 struct v4l2_dv_timings *timings)
1066{
1067 struct adv7604_state *state = to_state(sd);
1068 struct v4l2_bt_timings *bt = &timings->bt;
1069 struct stdi_readback stdi;
1070
1071 if (!timings)
1072 return -EINVAL;
1073
1074 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1075
1076 if (no_signal(sd)) {
1077 v4l2_dbg(1, debug, sd, "%s: no valid signal\n", __func__);
1078 return -ENOLINK;
1079 }
1080
1081 /* read STDI */
1082 if (read_stdi(sd, &stdi)) {
1083 v4l2_dbg(1, debug, sd, "%s: STDI/SSPD not locked\n", __func__);
1084 return -ENOLINK;
1085 }
1086 bt->interlaced = stdi.interlaced ?
1087 V4L2_DV_INTERLACED : V4L2_DV_PROGRESSIVE;
1088
1089 if (DIGITAL_INPUT) {
1090 timings->type = V4L2_DV_BT_656_1120;
1091
1092 bt->width = (hdmi_read(sd, 0x07) & 0x0f) * 256 + hdmi_read(sd, 0x08);
1093 bt->height = (hdmi_read(sd, 0x09) & 0x0f) * 256 + hdmi_read(sd, 0x0a);
1094 bt->pixelclock = (hdmi_read(sd, 0x06) * 1000000) +
1095 ((hdmi_read(sd, 0x3b) & 0x30) >> 4) * 250000;
1096 bt->hfrontporch = (hdmi_read(sd, 0x20) & 0x03) * 256 +
1097 hdmi_read(sd, 0x21);
1098 bt->hsync = (hdmi_read(sd, 0x22) & 0x03) * 256 +
1099 hdmi_read(sd, 0x23);
1100 bt->hbackporch = (hdmi_read(sd, 0x24) & 0x03) * 256 +
1101 hdmi_read(sd, 0x25);
1102 bt->vfrontporch = ((hdmi_read(sd, 0x2a) & 0x1f) * 256 +
1103 hdmi_read(sd, 0x2b)) / 2;
1104 bt->vsync = ((hdmi_read(sd, 0x2e) & 0x1f) * 256 +
1105 hdmi_read(sd, 0x2f)) / 2;
1106 bt->vbackporch = ((hdmi_read(sd, 0x32) & 0x1f) * 256 +
1107 hdmi_read(sd, 0x33)) / 2;
1108 bt->polarities = ((hdmi_read(sd, 0x05) & 0x10) ? V4L2_DV_VSYNC_POS_POL : 0) |
1109 ((hdmi_read(sd, 0x05) & 0x20) ? V4L2_DV_HSYNC_POS_POL : 0);
1110 if (bt->interlaced == V4L2_DV_INTERLACED) {
1111 bt->height += (hdmi_read(sd, 0x0b) & 0x0f) * 256 +
1112 hdmi_read(sd, 0x0c);
1113 bt->il_vfrontporch = ((hdmi_read(sd, 0x2c) & 0x1f) * 256 +
1114 hdmi_read(sd, 0x2d)) / 2;
1115 bt->il_vsync = ((hdmi_read(sd, 0x30) & 0x1f) * 256 +
1116 hdmi_read(sd, 0x31)) / 2;
1117 bt->vbackporch = ((hdmi_read(sd, 0x34) & 0x1f) * 256 +
1118 hdmi_read(sd, 0x35)) / 2;
1119 }
1120 adv7604_fill_optional_dv_timings_fields(sd, timings);
1121 } else {
1122 /* find format
Hans Verkuil80939642012-10-16 05:46:21 -03001123 * Since LCVS values are inaccurate [REF_03, p. 275-276],
Hans Verkuil54450f52012-07-18 05:45:16 -03001124 * stdi2dv_timings() is called with lcvs +-1 if the first attempt fails.
1125 */
1126 if (!stdi2dv_timings(sd, &stdi, timings))
1127 goto found;
1128 stdi.lcvs += 1;
1129 v4l2_dbg(1, debug, sd, "%s: lcvs + 1 = %d\n", __func__, stdi.lcvs);
1130 if (!stdi2dv_timings(sd, &stdi, timings))
1131 goto found;
1132 stdi.lcvs -= 2;
1133 v4l2_dbg(1, debug, sd, "%s: lcvs - 1 = %d\n", __func__, stdi.lcvs);
1134 if (stdi2dv_timings(sd, &stdi, timings)) {
1135 v4l2_dbg(1, debug, sd, "%s: format not supported\n", __func__);
1136 return -ERANGE;
1137 }
1138 }
1139found:
1140
1141 if (no_signal(sd)) {
1142 v4l2_dbg(1, debug, sd, "%s: signal lost during readout\n", __func__);
1143 memset(timings, 0, sizeof(struct v4l2_dv_timings));
1144 return -ENOLINK;
1145 }
1146
1147 if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
1148 (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
1149 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1150 __func__, (u32)bt->pixelclock);
1151 return -ERANGE;
1152 }
1153
1154 if (debug > 1)
1155 adv7604_print_timings(sd, timings,
1156 "adv7604_query_dv_timings:", true);
1157
1158 return 0;
1159}
1160
1161static int adv7604_s_dv_timings(struct v4l2_subdev *sd,
1162 struct v4l2_dv_timings *timings)
1163{
1164 struct adv7604_state *state = to_state(sd);
1165 struct v4l2_bt_timings *bt;
1166
1167 if (!timings)
1168 return -EINVAL;
1169
1170 bt = &timings->bt;
1171
1172 if ((!DIGITAL_INPUT && bt->pixelclock > 170000000) ||
1173 (DIGITAL_INPUT && bt->pixelclock > 225000000)) {
1174 v4l2_dbg(1, debug, sd, "%s: pixelclock out of range %d\n",
1175 __func__, (u32)bt->pixelclock);
1176 return -ERANGE;
1177 }
1178 adv7604_fill_optional_dv_timings_fields(sd, timings);
1179
1180 state->timings = *timings;
1181
1182 /* freerun */
1183 configure_free_run(sd, bt);
1184
1185 set_rgb_quantization_range(sd);
1186
1187
1188 if (debug > 1)
1189 adv7604_print_timings(sd, timings,
1190 "adv7604_s_dv_timings:", true);
1191 return 0;
1192}
1193
1194static int adv7604_g_dv_timings(struct v4l2_subdev *sd,
1195 struct v4l2_dv_timings *timings)
1196{
1197 struct adv7604_state *state = to_state(sd);
1198
1199 *timings = state->timings;
1200 return 0;
1201}
1202
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001203static void enable_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001204{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001205 struct adv7604_state *state = to_state(sd);
1206
1207 switch (state->mode) {
1208 case ADV7604_MODE_COMP:
1209 case ADV7604_MODE_GR:
Hans Verkuil54450f52012-07-18 05:45:16 -03001210 /* enable */
1211 io_write(sd, 0x15, 0xb0); /* Disable Tristate of Pins (no audio) */
1212 break;
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001213 case ADV7604_MODE_HDMI:
Hans Verkuil54450f52012-07-18 05:45:16 -03001214 /* enable */
1215 hdmi_write(sd, 0x1a, 0x0a); /* Unmute audio */
1216 hdmi_write(sd, 0x01, 0x00); /* Enable HDMI clock terminators */
1217 io_write(sd, 0x15, 0xa0); /* Disable Tristate of Pins */
1218 break;
1219 default:
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001220 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1221 __func__, state->mode);
Hans Verkuil54450f52012-07-18 05:45:16 -03001222 break;
1223 }
1224}
1225
1226static void disable_input(struct v4l2_subdev *sd)
1227{
1228 /* disable */
1229 io_write(sd, 0x15, 0xbe); /* Tristate all outputs from video core */
1230 hdmi_write(sd, 0x1a, 0x1a); /* Mute audio */
1231 hdmi_write(sd, 0x01, 0x78); /* Disable HDMI clock terminators */
1232}
1233
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001234static void select_input(struct v4l2_subdev *sd)
Hans Verkuil54450f52012-07-18 05:45:16 -03001235{
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001236 struct adv7604_state *state = to_state(sd);
1237
1238 switch (state->mode) {
1239 case ADV7604_MODE_COMP:
1240 case ADV7604_MODE_GR:
Hans Verkuil54450f52012-07-18 05:45:16 -03001241 /* set mode and select free run resolution */
1242 io_write(sd, 0x00, 0x07); /* video std */
1243 io_write(sd, 0x01, 0x02); /* prim mode */
1244 /* enable embedded syncs for auto graphics mode */
1245 cp_write_and_or(sd, 0x81, 0xef, 0x10);
1246
1247 /* reset ADI recommended settings for HDMI: */
1248 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1249 hdmi_write(sd, 0x0d, 0x04); /* HDMI filter optimization */
1250 hdmi_write(sd, 0x3d, 0x00); /* DDC bus active pull-up control */
1251 hdmi_write(sd, 0x3e, 0x74); /* TMDS PLL optimization */
1252 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1253 hdmi_write(sd, 0x57, 0x74); /* TMDS PLL optimization */
1254 hdmi_write(sd, 0x58, 0x63); /* TMDS PLL optimization */
1255 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1256 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1257 hdmi_write(sd, 0x93, 0x88); /* equaliser */
1258 hdmi_write(sd, 0x94, 0x2e); /* equaliser */
1259 hdmi_write(sd, 0x96, 0x00); /* enable automatic EQ changing */
1260
1261 afe_write(sd, 0x00, 0x08); /* power up ADC */
1262 afe_write(sd, 0x01, 0x06); /* power up Analog Front End */
1263 afe_write(sd, 0xc8, 0x00); /* phase control */
1264
1265 /* set ADI recommended settings for digitizer */
1266 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1267 afe_write(sd, 0x12, 0x7b); /* ADC noise shaping filter controls */
1268 afe_write(sd, 0x0c, 0x1f); /* CP core gain controls */
1269 cp_write(sd, 0x3e, 0x04); /* CP core pre-gain control */
1270 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1271 cp_write(sd, 0x40, 0x5c); /* CP core pre-gain control. Graphics mode */
1272 break;
1273
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001274 case ADV7604_MODE_HDMI:
Hans Verkuil54450f52012-07-18 05:45:16 -03001275 /* set mode and select free run resolution */
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001276 io_write(sd, 0x00, 0x02); /* video std */
1277 io_write(sd, 0x01, 0x06); /* prim mode */
Hans Verkuil54450f52012-07-18 05:45:16 -03001278 /* disable embedded syncs for auto graphics mode */
1279 cp_write_and_or(sd, 0x81, 0xef, 0x00);
1280
1281 /* set ADI recommended settings for HDMI: */
1282 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 4. */
1283 hdmi_write(sd, 0x0d, 0x84); /* HDMI filter optimization */
1284 hdmi_write(sd, 0x3d, 0x10); /* DDC bus active pull-up control */
1285 hdmi_write(sd, 0x3e, 0x39); /* TMDS PLL optimization */
1286 hdmi_write(sd, 0x4e, 0x3b); /* TMDS PLL optimization */
1287 hdmi_write(sd, 0x57, 0xb6); /* TMDS PLL optimization */
1288 hdmi_write(sd, 0x58, 0x03); /* TMDS PLL optimization */
1289 hdmi_write(sd, 0x8d, 0x18); /* equaliser */
1290 hdmi_write(sd, 0x8e, 0x34); /* equaliser */
1291 hdmi_write(sd, 0x93, 0x8b); /* equaliser */
1292 hdmi_write(sd, 0x94, 0x2d); /* equaliser */
1293 hdmi_write(sd, 0x96, 0x01); /* enable automatic EQ changing */
1294
1295 afe_write(sd, 0x00, 0xff); /* power down ADC */
1296 afe_write(sd, 0x01, 0xfe); /* power down Analog Front End */
1297 afe_write(sd, 0xc8, 0x40); /* phase control */
1298
1299 /* reset ADI recommended settings for digitizer */
1300 /* "ADV7604 Register Settings Recommendations (rev. 2.5, June 2010)" p. 17. */
1301 afe_write(sd, 0x12, 0xfb); /* ADC noise shaping filter controls */
1302 afe_write(sd, 0x0c, 0x0d); /* CP core gain controls */
1303 cp_write(sd, 0x3e, 0x00); /* CP core pre-gain control */
1304 cp_write(sd, 0xc3, 0x39); /* CP coast control. Graphics mode */
1305 cp_write(sd, 0x40, 0x80); /* CP core pre-gain control. Graphics mode */
1306
1307 break;
1308 default:
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001309 v4l2_dbg(2, debug, sd, "%s: Unknown mode %d\n",
1310 __func__, state->mode);
Hans Verkuil54450f52012-07-18 05:45:16 -03001311 break;
1312 }
1313}
1314
1315static int adv7604_s_routing(struct v4l2_subdev *sd,
1316 u32 input, u32 output, u32 config)
1317{
1318 struct adv7604_state *state = to_state(sd);
1319
1320 v4l2_dbg(2, debug, sd, "%s: input %d", __func__, input);
1321
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001322 state->mode = input;
Hans Verkuil54450f52012-07-18 05:45:16 -03001323
1324 disable_input(sd);
1325
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001326 select_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001327
Hans Verkuil6b0d5d32012-10-16 06:40:45 -03001328 enable_input(sd);
Hans Verkuil54450f52012-07-18 05:45:16 -03001329
1330 return 0;
1331}
1332
1333static int adv7604_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
1334 enum v4l2_mbus_pixelcode *code)
1335{
1336 if (index)
1337 return -EINVAL;
1338 /* Good enough for now */
1339 *code = V4L2_MBUS_FMT_FIXED;
1340 return 0;
1341}
1342
1343static int adv7604_g_mbus_fmt(struct v4l2_subdev *sd,
1344 struct v4l2_mbus_framefmt *fmt)
1345{
1346 struct adv7604_state *state = to_state(sd);
1347
1348 fmt->width = state->timings.bt.width;
1349 fmt->height = state->timings.bt.height;
1350 fmt->code = V4L2_MBUS_FMT_FIXED;
1351 fmt->field = V4L2_FIELD_NONE;
1352 if (state->timings.bt.standards & V4L2_DV_BT_STD_CEA861) {
1353 fmt->colorspace = (state->timings.bt.height <= 576) ?
1354 V4L2_COLORSPACE_SMPTE170M : V4L2_COLORSPACE_REC709;
1355 }
1356 return 0;
1357}
1358
1359static int adv7604_isr(struct v4l2_subdev *sd, u32 status, bool *handled)
1360{
1361 struct adv7604_state *state = to_state(sd);
1362 u8 fmt_change, fmt_change_digital, tx_5v;
1363
1364 /* format change */
1365 fmt_change = io_read(sd, 0x43) & 0x98;
1366 if (fmt_change)
1367 io_write(sd, 0x44, fmt_change);
1368 fmt_change_digital = DIGITAL_INPUT ? (io_read(sd, 0x6b) & 0xc0) : 0;
1369 if (fmt_change_digital)
1370 io_write(sd, 0x6c, fmt_change_digital);
1371 if (fmt_change || fmt_change_digital) {
1372 v4l2_dbg(1, debug, sd,
1373 "%s: ADV7604_FMT_CHANGE, fmt_change = 0x%x, fmt_change_digital = 0x%x\n",
1374 __func__, fmt_change, fmt_change_digital);
1375 v4l2_subdev_notify(sd, ADV7604_FMT_CHANGE, NULL);
1376 if (handled)
1377 *handled = true;
1378 }
1379 /* tx 5v detect */
1380 tx_5v = io_read(sd, 0x70) & 0x10;
1381 if (tx_5v) {
1382 v4l2_dbg(1, debug, sd, "%s: tx_5v: 0x%x\n", __func__, tx_5v);
1383 io_write(sd, 0x71, tx_5v);
1384 adv7604_s_detect_tx_5v_ctrl(sd);
1385 if (handled)
1386 *handled = true;
1387 }
1388 return 0;
1389}
1390
1391static int adv7604_get_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
1392{
1393 struct adv7604_state *state = to_state(sd);
1394
1395 if (edid->pad != 0)
1396 return -EINVAL;
1397 if (edid->blocks == 0)
1398 return -EINVAL;
1399 if (edid->start_block >= state->edid_blocks)
1400 return -EINVAL;
1401 if (edid->start_block + edid->blocks > state->edid_blocks)
1402 edid->blocks = state->edid_blocks - edid->start_block;
1403 if (!edid->edid)
1404 return -EINVAL;
1405 memcpy(edid->edid + edid->start_block * 128,
1406 state->edid + edid->start_block * 128,
1407 edid->blocks * 128);
1408 return 0;
1409}
1410
1411static int adv7604_set_edid(struct v4l2_subdev *sd, struct v4l2_subdev_edid *edid)
1412{
1413 struct adv7604_state *state = to_state(sd);
1414 int err;
1415
1416 if (edid->pad != 0)
1417 return -EINVAL;
1418 if (edid->start_block != 0)
1419 return -EINVAL;
1420 if (edid->blocks == 0) {
1421 /* Pull down the hotplug pin */
1422 v4l2_subdev_notify(sd, ADV7604_HOTPLUG, (void *)0);
1423 /* Disables I2C access to internal EDID ram from DDC port */
1424 rep_write_and_or(sd, 0x77, 0xf0, 0x0);
1425 state->edid_blocks = 0;
1426 /* Fall back to a 16:9 aspect ratio */
1427 state->aspect_ratio.numerator = 16;
1428 state->aspect_ratio.denominator = 9;
1429 return 0;
1430 }
1431 if (edid->blocks > 2)
1432 return -E2BIG;
1433 if (!edid->edid)
1434 return -EINVAL;
1435 memcpy(state->edid, edid->edid, 128 * edid->blocks);
1436 state->edid_blocks = edid->blocks;
1437 state->aspect_ratio = v4l2_calc_aspect_ratio(edid->edid[0x15],
1438 edid->edid[0x16]);
1439 err = edid_write_block(sd, 128 * edid->blocks, state->edid);
1440 if (err < 0)
1441 v4l2_err(sd, "error %d writing edid\n", err);
1442 return err;
1443}
1444
1445/*********** avi info frame CEA-861-E **************/
1446
1447static void print_avi_infoframe(struct v4l2_subdev *sd)
1448{
1449 int i;
1450 u8 buf[14];
1451 u8 avi_len;
1452 u8 avi_ver;
1453
1454 if (!(hdmi_read(sd, 0x05) & 0x80)) {
1455 v4l2_info(sd, "receive DVI-D signal (AVI infoframe not supported)\n");
1456 return;
1457 }
1458 if (!(io_read(sd, 0x60) & 0x01)) {
1459 v4l2_info(sd, "AVI infoframe not received\n");
1460 return;
1461 }
1462
1463 if (io_read(sd, 0x83) & 0x01) {
1464 v4l2_info(sd, "AVI infoframe checksum error has occurred earlier\n");
1465 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1466 if (io_read(sd, 0x83) & 0x01) {
1467 v4l2_info(sd, "AVI infoframe checksum error still present\n");
1468 io_write(sd, 0x85, 0x01); /* clear AVI_INF_CKS_ERR_RAW */
1469 }
1470 }
1471
1472 avi_len = infoframe_read(sd, 0xe2);
1473 avi_ver = infoframe_read(sd, 0xe1);
1474 v4l2_info(sd, "AVI infoframe version %d (%d byte)\n",
1475 avi_ver, avi_len);
1476
1477 if (avi_ver != 0x02)
1478 return;
1479
1480 for (i = 0; i < 14; i++)
1481 buf[i] = infoframe_read(sd, i);
1482
1483 v4l2_info(sd,
1484 "\t%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1485 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
1486 buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
1487}
1488
1489static int adv7604_log_status(struct v4l2_subdev *sd)
1490{
1491 struct adv7604_state *state = to_state(sd);
1492 struct v4l2_dv_timings timings;
1493 struct stdi_readback stdi;
1494 u8 reg_io_0x02 = io_read(sd, 0x02);
1495
1496 char *csc_coeff_sel_rb[16] = {
1497 "bypassed", "YPbPr601 -> RGB", "reserved", "YPbPr709 -> RGB",
1498 "reserved", "RGB -> YPbPr601", "reserved", "RGB -> YPbPr709",
1499 "reserved", "YPbPr709 -> YPbPr601", "YPbPr601 -> YPbPr709",
1500 "reserved", "reserved", "reserved", "reserved", "manual"
1501 };
1502 char *input_color_space_txt[16] = {
1503 "RGB limited range (16-235)", "RGB full range (0-255)",
1504 "YCbCr Bt.601 (16-235)", "YCbCr Bt.709 (16-235)",
1505 "XvYCC Bt.601", "XvYCC Bt.709",
1506 "YCbCr Bt.601 (0-255)", "YCbCr Bt.709 (0-255)",
1507 "invalid", "invalid", "invalid", "invalid", "invalid",
1508 "invalid", "invalid", "automatic"
1509 };
1510 char *rgb_quantization_range_txt[] = {
1511 "Automatic",
1512 "RGB limited range (16-235)",
1513 "RGB full range (0-255)",
1514 };
1515
1516 v4l2_info(sd, "-----Chip status-----\n");
1517 v4l2_info(sd, "Chip power: %s\n", no_power(sd) ? "off" : "on");
1518 v4l2_info(sd, "Connector type: %s\n", state->connector_hdmi ?
1519 "HDMI" : (DIGITAL_INPUT ? "DVI-D" : "DVI-A"));
1520 v4l2_info(sd, "EDID: %s\n", ((rep_read(sd, 0x7d) & 0x01) &&
1521 (rep_read(sd, 0x77) & 0x01)) ? "enabled" : "disabled ");
1522 v4l2_info(sd, "CEC: %s\n", !!(cec_read(sd, 0x2a) & 0x01) ?
1523 "enabled" : "disabled");
1524
1525 v4l2_info(sd, "-----Signal status-----\n");
1526 v4l2_info(sd, "Cable detected (+5V power): %s\n",
1527 (io_read(sd, 0x6f) & 0x10) ? "true" : "false");
1528 v4l2_info(sd, "TMDS signal detected: %s\n",
1529 no_signal_tmds(sd) ? "false" : "true");
1530 v4l2_info(sd, "TMDS signal locked: %s\n",
1531 no_lock_tmds(sd) ? "false" : "true");
1532 v4l2_info(sd, "SSPD locked: %s\n", no_lock_sspd(sd) ? "false" : "true");
1533 v4l2_info(sd, "STDI locked: %s\n", no_lock_stdi(sd) ? "false" : "true");
1534 v4l2_info(sd, "CP locked: %s\n", no_lock_cp(sd) ? "false" : "true");
1535 v4l2_info(sd, "CP free run: %s\n",
1536 (!!(cp_read(sd, 0xff) & 0x10) ? "on" : "off"));
1537 v4l2_info(sd, "Prim-mode = 0x%x, video std = 0x%x\n",
1538 io_read(sd, 0x01) & 0x0f, io_read(sd, 0x00) & 0x3f);
1539
1540 v4l2_info(sd, "-----Video Timings-----\n");
1541 if (read_stdi(sd, &stdi))
1542 v4l2_info(sd, "STDI: not locked\n");
1543 else
1544 v4l2_info(sd, "STDI: lcf (frame height - 1) = %d, bl = %d, lcvs (vsync) = %d, %s, %chsync, %cvsync\n",
1545 stdi.lcf, stdi.bl, stdi.lcvs,
1546 stdi.interlaced ? "interlaced" : "progressive",
1547 stdi.hs_pol, stdi.vs_pol);
1548 if (adv7604_query_dv_timings(sd, &timings))
1549 v4l2_info(sd, "No video detected\n");
1550 else
1551 adv7604_print_timings(sd, &timings, "Detected format:", true);
1552 adv7604_print_timings(sd, &state->timings, "Configured format:", true);
1553
1554 v4l2_info(sd, "-----Color space-----\n");
1555 v4l2_info(sd, "RGB quantization range ctrl: %s\n",
1556 rgb_quantization_range_txt[state->rgb_quantization_range]);
1557 v4l2_info(sd, "Input color space: %s\n",
1558 input_color_space_txt[reg_io_0x02 >> 4]);
1559 v4l2_info(sd, "Output color space: %s %s, saturator %s\n",
1560 (reg_io_0x02 & 0x02) ? "RGB" : "YCbCr",
1561 (reg_io_0x02 & 0x04) ? "(16-235)" : "(0-255)",
1562 ((reg_io_0x02 & 0x04) ^ (reg_io_0x02 & 0x01)) ?
1563 "enabled" : "disabled");
1564 v4l2_info(sd, "Color space conversion: %s\n",
1565 csc_coeff_sel_rb[cp_read(sd, 0xfc) >> 4]);
1566
1567 /* Digital video */
1568 if (DIGITAL_INPUT) {
1569 v4l2_info(sd, "-----HDMI status-----\n");
1570 v4l2_info(sd, "HDCP encrypted content: %s\n",
1571 hdmi_read(sd, 0x05) & 0x40 ? "true" : "false");
1572
1573 print_avi_infoframe(sd);
1574 }
1575
1576 return 0;
1577}
1578
1579/* ----------------------------------------------------------------------- */
1580
1581static const struct v4l2_ctrl_ops adv7604_ctrl_ops = {
1582 .s_ctrl = adv7604_s_ctrl,
1583};
1584
1585static const struct v4l2_subdev_core_ops adv7604_core_ops = {
1586 .log_status = adv7604_log_status,
1587 .g_ext_ctrls = v4l2_subdev_g_ext_ctrls,
1588 .try_ext_ctrls = v4l2_subdev_try_ext_ctrls,
1589 .s_ext_ctrls = v4l2_subdev_s_ext_ctrls,
1590 .g_ctrl = v4l2_subdev_g_ctrl,
1591 .s_ctrl = v4l2_subdev_s_ctrl,
1592 .queryctrl = v4l2_subdev_queryctrl,
1593 .querymenu = v4l2_subdev_querymenu,
1594 .g_chip_ident = adv7604_g_chip_ident,
1595 .interrupt_service_routine = adv7604_isr,
1596#ifdef CONFIG_VIDEO_ADV_DEBUG
1597 .g_register = adv7604_g_register,
1598 .s_register = adv7604_s_register,
1599#endif
1600};
1601
1602static const struct v4l2_subdev_video_ops adv7604_video_ops = {
1603 .s_routing = adv7604_s_routing,
1604 .g_input_status = adv7604_g_input_status,
1605 .s_dv_timings = adv7604_s_dv_timings,
1606 .g_dv_timings = adv7604_g_dv_timings,
1607 .query_dv_timings = adv7604_query_dv_timings,
1608 .enum_dv_timings = adv7604_enum_dv_timings,
1609 .dv_timings_cap = adv7604_dv_timings_cap,
1610 .enum_mbus_fmt = adv7604_enum_mbus_fmt,
1611 .g_mbus_fmt = adv7604_g_mbus_fmt,
1612 .try_mbus_fmt = adv7604_g_mbus_fmt,
1613 .s_mbus_fmt = adv7604_g_mbus_fmt,
1614};
1615
1616static const struct v4l2_subdev_pad_ops adv7604_pad_ops = {
1617 .get_edid = adv7604_get_edid,
1618 .set_edid = adv7604_set_edid,
1619};
1620
1621static const struct v4l2_subdev_ops adv7604_ops = {
1622 .core = &adv7604_core_ops,
1623 .video = &adv7604_video_ops,
1624 .pad = &adv7604_pad_ops,
1625};
1626
1627/* -------------------------- custom ctrls ---------------------------------- */
1628
1629static const struct v4l2_ctrl_config adv7604_ctrl_analog_sampling_phase = {
1630 .ops = &adv7604_ctrl_ops,
1631 .id = V4L2_CID_ADV_RX_ANALOG_SAMPLING_PHASE,
1632 .name = "Analog Sampling Phase",
1633 .type = V4L2_CTRL_TYPE_INTEGER,
1634 .min = 0,
1635 .max = 0x1f,
1636 .step = 1,
1637 .def = 0,
1638};
1639
1640static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color_manual = {
1641 .ops = &adv7604_ctrl_ops,
1642 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR_MANUAL,
1643 .name = "Free Running Color, Manual",
1644 .type = V4L2_CTRL_TYPE_BOOLEAN,
1645 .min = false,
1646 .max = true,
1647 .step = 1,
1648 .def = false,
1649};
1650
1651static const struct v4l2_ctrl_config adv7604_ctrl_free_run_color = {
1652 .ops = &adv7604_ctrl_ops,
1653 .id = V4L2_CID_ADV_RX_FREE_RUN_COLOR,
1654 .name = "Free Running Color",
1655 .type = V4L2_CTRL_TYPE_INTEGER,
1656 .min = 0x0,
1657 .max = 0xffffff,
1658 .step = 0x1,
1659 .def = 0x0,
1660};
1661
1662/* ----------------------------------------------------------------------- */
1663
1664static int adv7604_core_init(struct v4l2_subdev *sd)
1665{
1666 struct adv7604_state *state = to_state(sd);
1667 struct adv7604_platform_data *pdata = &state->pdata;
1668
1669 hdmi_write(sd, 0x48,
1670 (pdata->disable_pwrdnb ? 0x80 : 0) |
1671 (pdata->disable_cable_det_rst ? 0x40 : 0));
1672
1673 disable_input(sd);
1674
1675 /* power */
1676 io_write(sd, 0x0c, 0x42); /* Power up part and power down VDP */
1677 io_write(sd, 0x0b, 0x44); /* Power down ESDP block */
1678 cp_write(sd, 0xcf, 0x01); /* Power down macrovision */
1679
1680 /* video format */
1681 io_write_and_or(sd, 0x02, 0xf0,
1682 pdata->alt_gamma << 3 |
1683 pdata->op_656_range << 2 |
1684 pdata->rgb_out << 1 |
1685 pdata->alt_data_sat << 0);
1686 io_write(sd, 0x03, pdata->op_format_sel);
1687 io_write_and_or(sd, 0x04, 0x1f, pdata->op_ch_sel << 5);
1688 io_write_and_or(sd, 0x05, 0xf0, pdata->blank_data << 3 |
1689 pdata->insert_av_codes << 2 |
1690 pdata->replicate_av_codes << 1 |
1691 pdata->invert_cbcr << 0);
1692
1693 /* TODO from platform data */
1694 cp_write(sd, 0x69, 0x30); /* Enable CP CSC */
1695 io_write(sd, 0x06, 0xa6); /* positive VS and HS */
1696 io_write(sd, 0x14, 0x7f); /* Drive strength adjusted to max */
1697 cp_write(sd, 0xba, (pdata->hdmi_free_run_mode << 1) | 0x01); /* HDMI free run */
1698 cp_write(sd, 0xf3, 0xdc); /* Low threshold to enter/exit free run mode */
1699 cp_write(sd, 0xf9, 0x23); /* STDI ch. 1 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03001700 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001701 cp_write(sd, 0x45, 0x23); /* STDI ch. 2 - LCVS change threshold -
Hans Verkuil80939642012-10-16 05:46:21 -03001702 ADI recommended setting [REF_01, c. 2.3.3] */
Hans Verkuil54450f52012-07-18 05:45:16 -03001703 cp_write(sd, 0xc9, 0x2d); /* use prim_mode and vid_std as free run resolution
1704 for digital formats */
1705
1706 /* TODO from platform data */
1707 afe_write(sd, 0xb5, 0x01); /* Setting MCLK to 256Fs */
1708
1709 afe_write(sd, 0x02, pdata->ain_sel); /* Select analog input muxing mode */
1710 io_write_and_or(sd, 0x30, ~(1 << 4), pdata->output_bus_lsb_to_msb << 4);
1711
Hans Verkuil54450f52012-07-18 05:45:16 -03001712 /* interrupts */
1713 io_write(sd, 0x40, 0xc2); /* Configure INT1 */
1714 io_write(sd, 0x41, 0xd7); /* STDI irq for any change, disable INT2 */
1715 io_write(sd, 0x46, 0x98); /* Enable SSPD, STDI and CP unlocked interrupts */
1716 io_write(sd, 0x6e, 0xc0); /* Enable V_LOCKED and DE_REGEN_LCK interrupts */
1717 io_write(sd, 0x73, 0x10); /* Enable CABLE_DET_A_ST (+5v) interrupt */
1718
1719 return v4l2_ctrl_handler_setup(sd->ctrl_handler);
1720}
1721
1722static void adv7604_unregister_clients(struct adv7604_state *state)
1723{
1724 if (state->i2c_avlink)
1725 i2c_unregister_device(state->i2c_avlink);
1726 if (state->i2c_cec)
1727 i2c_unregister_device(state->i2c_cec);
1728 if (state->i2c_infoframe)
1729 i2c_unregister_device(state->i2c_infoframe);
1730 if (state->i2c_esdp)
1731 i2c_unregister_device(state->i2c_esdp);
1732 if (state->i2c_dpp)
1733 i2c_unregister_device(state->i2c_dpp);
1734 if (state->i2c_afe)
1735 i2c_unregister_device(state->i2c_afe);
1736 if (state->i2c_repeater)
1737 i2c_unregister_device(state->i2c_repeater);
1738 if (state->i2c_edid)
1739 i2c_unregister_device(state->i2c_edid);
1740 if (state->i2c_hdmi)
1741 i2c_unregister_device(state->i2c_hdmi);
1742 if (state->i2c_test)
1743 i2c_unregister_device(state->i2c_test);
1744 if (state->i2c_cp)
1745 i2c_unregister_device(state->i2c_cp);
1746 if (state->i2c_vdp)
1747 i2c_unregister_device(state->i2c_vdp);
1748}
1749
1750static struct i2c_client *adv7604_dummy_client(struct v4l2_subdev *sd,
1751 u8 addr, u8 io_reg)
1752{
1753 struct i2c_client *client = v4l2_get_subdevdata(sd);
1754
1755 if (addr)
1756 io_write(sd, io_reg, addr << 1);
1757 return i2c_new_dummy(client->adapter, io_read(sd, io_reg) >> 1);
1758}
1759
1760static int adv7604_probe(struct i2c_client *client,
1761 const struct i2c_device_id *id)
1762{
1763 struct adv7604_state *state;
1764 struct adv7604_platform_data *pdata = client->dev.platform_data;
1765 struct v4l2_ctrl_handler *hdl;
1766 struct v4l2_subdev *sd;
1767 int err;
1768
1769 /* Check if the adapter supports the needed features */
1770 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1771 return -EIO;
1772 v4l_dbg(1, debug, client, "detecting adv7604 client on address 0x%x\n",
1773 client->addr << 1);
1774
1775 state = kzalloc(sizeof(struct adv7604_state), GFP_KERNEL);
1776 if (!state) {
1777 v4l_err(client, "Could not allocate adv7604_state memory!\n");
1778 return -ENOMEM;
1779 }
1780
1781 /* platform data */
1782 if (!pdata) {
1783 v4l_err(client, "No platform data!\n");
1784 err = -ENODEV;
1785 goto err_state;
1786 }
1787 memcpy(&state->pdata, pdata, sizeof(state->pdata));
1788
1789 sd = &state->sd;
1790 v4l2_i2c_subdev_init(sd, client, &adv7604_ops);
1791 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1792 state->connector_hdmi = pdata->connector_hdmi;
1793
1794 /* i2c access to adv7604? */
1795 if (adv_smbus_read_byte_data_check(client, 0xfb, false) != 0x68) {
1796 v4l2_info(sd, "not an adv7604 on address 0x%x\n",
1797 client->addr << 1);
1798 err = -ENODEV;
1799 goto err_state;
1800 }
1801
1802 /* control handlers */
1803 hdl = &state->hdl;
1804 v4l2_ctrl_handler_init(hdl, 9);
1805
1806 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
1807 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
1808 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
1809 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1810 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
1811 V4L2_CID_SATURATION, 0, 255, 1, 128);
1812 v4l2_ctrl_new_std(hdl, &adv7604_ctrl_ops,
1813 V4L2_CID_HUE, 0, 128, 1, 0);
1814
1815 /* private controls */
1816 state->detect_tx_5v_ctrl = v4l2_ctrl_new_std(hdl, NULL,
1817 V4L2_CID_DV_RX_POWER_PRESENT, 0, 1, 0, 0);
1818 state->detect_tx_5v_ctrl->is_private = true;
1819 state->rgb_quantization_range_ctrl =
1820 v4l2_ctrl_new_std_menu(hdl, &adv7604_ctrl_ops,
1821 V4L2_CID_DV_RX_RGB_RANGE, V4L2_DV_RGB_RANGE_FULL,
1822 0, V4L2_DV_RGB_RANGE_AUTO);
1823 state->rgb_quantization_range_ctrl->is_private = true;
1824
1825 /* custom controls */
1826 state->analog_sampling_phase_ctrl =
1827 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_analog_sampling_phase, NULL);
1828 state->analog_sampling_phase_ctrl->is_private = true;
1829 state->free_run_color_manual_ctrl =
1830 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color_manual, NULL);
1831 state->free_run_color_manual_ctrl->is_private = true;
1832 state->free_run_color_ctrl =
1833 v4l2_ctrl_new_custom(hdl, &adv7604_ctrl_free_run_color, NULL);
1834 state->free_run_color_ctrl->is_private = true;
1835
1836 sd->ctrl_handler = hdl;
1837 if (hdl->error) {
1838 err = hdl->error;
1839 goto err_hdl;
1840 }
1841 if (adv7604_s_detect_tx_5v_ctrl(sd)) {
1842 err = -ENODEV;
1843 goto err_hdl;
1844 }
1845
1846 state->i2c_avlink = adv7604_dummy_client(sd, pdata->i2c_avlink, 0xf3);
1847 state->i2c_cec = adv7604_dummy_client(sd, pdata->i2c_cec, 0xf4);
1848 state->i2c_infoframe = adv7604_dummy_client(sd, pdata->i2c_infoframe, 0xf5);
1849 state->i2c_esdp = adv7604_dummy_client(sd, pdata->i2c_esdp, 0xf6);
1850 state->i2c_dpp = adv7604_dummy_client(sd, pdata->i2c_dpp, 0xf7);
1851 state->i2c_afe = adv7604_dummy_client(sd, pdata->i2c_afe, 0xf8);
1852 state->i2c_repeater = adv7604_dummy_client(sd, pdata->i2c_repeater, 0xf9);
1853 state->i2c_edid = adv7604_dummy_client(sd, pdata->i2c_edid, 0xfa);
1854 state->i2c_hdmi = adv7604_dummy_client(sd, pdata->i2c_hdmi, 0xfb);
1855 state->i2c_test = adv7604_dummy_client(sd, pdata->i2c_test, 0xfc);
1856 state->i2c_cp = adv7604_dummy_client(sd, pdata->i2c_cp, 0xfd);
1857 state->i2c_vdp = adv7604_dummy_client(sd, pdata->i2c_vdp, 0xfe);
1858 if (!state->i2c_avlink || !state->i2c_cec || !state->i2c_infoframe ||
1859 !state->i2c_esdp || !state->i2c_dpp || !state->i2c_afe ||
1860 !state->i2c_repeater || !state->i2c_edid || !state->i2c_hdmi ||
1861 !state->i2c_test || !state->i2c_cp || !state->i2c_vdp) {
1862 err = -ENOMEM;
1863 v4l2_err(sd, "failed to create all i2c clients\n");
1864 goto err_i2c;
1865 }
1866
1867 /* work queues */
1868 state->work_queues = create_singlethread_workqueue(client->name);
1869 if (!state->work_queues) {
1870 v4l2_err(sd, "Could not create work queue\n");
1871 err = -ENOMEM;
1872 goto err_i2c;
1873 }
1874
1875 INIT_DELAYED_WORK(&state->delayed_work_enable_hotplug,
1876 adv7604_delayed_work_enable_hotplug);
1877
1878 state->pad.flags = MEDIA_PAD_FL_SOURCE;
1879 err = media_entity_init(&sd->entity, 1, &state->pad, 0);
1880 if (err)
1881 goto err_work_queues;
1882
1883 err = adv7604_core_init(sd);
1884 if (err)
1885 goto err_entity;
1886 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name,
1887 client->addr << 1, client->adapter->name);
1888 return 0;
1889
1890err_entity:
1891 media_entity_cleanup(&sd->entity);
1892err_work_queues:
1893 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1894 destroy_workqueue(state->work_queues);
1895err_i2c:
1896 adv7604_unregister_clients(state);
1897err_hdl:
1898 v4l2_ctrl_handler_free(hdl);
1899err_state:
1900 kfree(state);
1901 return err;
1902}
1903
1904/* ----------------------------------------------------------------------- */
1905
1906static int adv7604_remove(struct i2c_client *client)
1907{
1908 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1909 struct adv7604_state *state = to_state(sd);
1910
1911 cancel_delayed_work(&state->delayed_work_enable_hotplug);
1912 destroy_workqueue(state->work_queues);
1913 v4l2_device_unregister_subdev(sd);
1914 media_entity_cleanup(&sd->entity);
1915 adv7604_unregister_clients(to_state(sd));
1916 v4l2_ctrl_handler_free(sd->ctrl_handler);
1917 kfree(to_state(sd));
1918 return 0;
1919}
1920
1921/* ----------------------------------------------------------------------- */
1922
1923static struct i2c_device_id adv7604_id[] = {
1924 { "adv7604", 0 },
1925 { }
1926};
1927MODULE_DEVICE_TABLE(i2c, adv7604_id);
1928
1929static struct i2c_driver adv7604_driver = {
1930 .driver = {
1931 .owner = THIS_MODULE,
1932 .name = "adv7604",
1933 },
1934 .probe = adv7604_probe,
1935 .remove = adv7604_remove,
1936 .id_table = adv7604_id,
1937};
1938
1939module_i2c_driver(adv7604_driver);