26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
31 #include <linux/videodev2.h>
49 #define MASK_AD9389B_EDID_RDY_INT 0x04
50 #define MASK_AD9389B_MSEN_INT 0x40
51 #define MASK_AD9389B_HPD_INT 0x80
53 #define MASK_AD9389B_HPD_DETECT 0x40
54 #define MASK_AD9389B_MSEN_DETECT 0x20
55 #define MASK_AD9389B_EDID_RDY 0x10
57 #define EDID_MAX_RETRIES (8)
58 #define EDID_DELAY 250
59 #define EDID_MAX_SEGM 8
110 static void ad9389b_check_monitor_present_status(
struct v4l2_subdev *
sd);
111 static bool ad9389b_check_edid_status(
struct v4l2_subdev *
sd);
141 for (i = 0; i < 3; i++) {
146 v4l2_err(sd,
"I2C Write Problem\n");
152 static inline void ad9389b_wr_and_or(
struct v4l2_subdev *sd,
u8 reg,
153 u8 clr_mask,
u8 val_mask)
155 ad9389b_wr(sd, reg, (ad9389b_rd(sd, reg) & clr_mask) | val_mask);
165 for (i = 0; i < len; i++)
169 static inline bool ad9389b_have_hotplug(
struct v4l2_subdev *sd)
174 static inline bool ad9389b_have_rx_sense(
struct v4l2_subdev *sd)
181 ad9389b_wr_and_or(sd, 0x17, 0xe7, (mode & 0x3)<<3);
182 ad9389b_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5);
185 static void ad9389b_csc_coeff(
struct v4l2_subdev *sd,
191 ad9389b_wr_and_or(sd, 0x18, 0xe0, A1>>8);
192 ad9389b_wr(sd, 0x19, A1);
193 ad9389b_wr_and_or(sd, 0x1A, 0xe0, A2>>8);
194 ad9389b_wr(sd, 0x1B, A2);
195 ad9389b_wr_and_or(sd, 0x1c, 0xe0, A3>>8);
196 ad9389b_wr(sd, 0x1d, A3);
197 ad9389b_wr_and_or(sd, 0x1e, 0xe0, A4>>8);
198 ad9389b_wr(sd, 0x1f, A4);
201 ad9389b_wr_and_or(sd, 0x20, 0xe0, B1>>8);
202 ad9389b_wr(sd, 0x21, B1);
203 ad9389b_wr_and_or(sd, 0x22, 0xe0, B2>>8);
204 ad9389b_wr(sd, 0x23, B2);
205 ad9389b_wr_and_or(sd, 0x24, 0xe0, B3>>8);
206 ad9389b_wr(sd, 0x25, B3);
207 ad9389b_wr_and_or(sd, 0x26, 0xe0, B4>>8);
208 ad9389b_wr(sd, 0x27, B4);
211 ad9389b_wr_and_or(sd, 0x28, 0xe0, C1>>8);
212 ad9389b_wr(sd, 0x29, C1);
213 ad9389b_wr_and_or(sd, 0x2A, 0xe0, C2>>8);
214 ad9389b_wr(sd, 0x2B, C2);
215 ad9389b_wr_and_or(sd, 0x2C, 0xe0, C3>>8);
216 ad9389b_wr(sd, 0x2D, C3);
217 ad9389b_wr_and_or(sd, 0x2E, 0xe0, C4>>8);
218 ad9389b_wr(sd, 0x2F, C4);
226 ad9389b_csc_conversion_mode(sd, csc_mode);
227 ad9389b_csc_coeff(sd,
230 0, 0, 4096-564, 256);
232 ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x1);
234 ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x02);
237 ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x0);
239 ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x04);
243 static void ad9389b_set_IT_content_AVI_InfoFrame(
struct v4l2_subdev *sd)
249 ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x00);
252 ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x40);
265 ad9389b_csc_rgb_full2limit(sd,
true);
268 ad9389b_csc_rgb_full2limit(sd,
false);
273 ad9389b_csc_rgb_full2limit(sd,
true);
277 ad9389b_csc_rgb_full2limit(sd,
false);
302 if (pixelclock > 140000000)
304 else if (pixelclock > 117000000)
306 else if (pixelclock > 87000000)
308 else if (pixelclock > 60000000)
313 ad9389b_wr_and_or(sd, 0x98, 0x0f, gear);
318 static int ad9389b_s_ctrl(
struct v4l2_ctrl *ctrl)
324 "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->
id, ctrl->
val);
328 ad9389b_wr_and_or(sd, 0xaf, 0xfd,
333 return ad9389b_set_rgb_quantization_mode(sd, ctrl);
338 .s_ctrl = ad9389b_s_ctrl,
343 #ifdef CONFIG_VIDEO_ADV_DEBUG
346 struct i2c_client *client = v4l2_get_subdevdata(sd);
352 reg->
val = ad9389b_rd(sd, reg->
reg & 0xff);
359 struct i2c_client *client = v4l2_get_subdevdata(sd);
365 ad9389b_wr(sd, reg->
reg & 0xff, reg->
val & 0xff);
372 struct i2c_client *client = v4l2_get_subdevdata(sd);
377 static int ad9389b_log_status(
struct v4l2_subdev *sd)
382 static const char *
const states[] = {
388 "initializing HDCP repeater",
389 "6",
"7",
"8",
"9",
"A",
"B",
"C",
"D",
"E",
"F"
391 static const char *
const errors[] = {
398 "max repeater cascade exceeded",
401 "9",
"A",
"B",
"C",
"D",
"E",
"F"
408 v4l2_info(sd,
"%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n",
416 (ad9389b_rd(sd, 0xaf) & 0x02) ?
418 (ad9389b_rd(sd, 0xa1) & 0x3c) ?
419 "disabled" :
"enabled");
421 v4l2_info(sd,
"ad9389b: %s\n", (ad9389b_rd(sd, 0xb8) & 0x40) ?
422 "encrypted" :
"no encryption");
423 v4l2_info(sd,
"state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n",
424 states[ad9389b_rd(sd, 0xc8) & 0xf],
425 errors[ad9389b_rd(sd, 0xc8) >> 4],
427 ad9389b_rd(sd, 0x94), ad9389b_rd(sd, 0x96));
428 manual_gear = ad9389b_rd(sd, 0x98) & 0x80;
429 v4l2_info(sd,
"ad9389b: RGB quantization: %s range\n",
430 ad9389b_rd(sd, 0x3b) & 0x01 ?
"limited" :
"full");
432 manual_gear ?
"manual" :
"automatic",
433 manual_gear ? ((ad9389b_rd(sd, 0x98) & 0x70) >> 4) :
434 ((ad9389b_rd(sd, 0x9e) & 0x0e) >> 1));
436 if (ad9389b_rd(sd, 0xaf) & 0x02) {
438 u8 manual_cts = ad9389b_rd(sd, 0x0a) & 0x80;
439 u32 N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 |
440 ad9389b_rd(sd, 0x02) << 8 |
441 ad9389b_rd(sd, 0x03);
442 u8 vic_detect = ad9389b_rd(sd, 0x3e) >> 2;
443 u8 vic_sent = ad9389b_rd(sd, 0x3d) & 0x3f;
447 CTS = (ad9389b_rd(sd, 0x07) & 0xf) << 16 |
448 ad9389b_rd(sd, 0x08) << 8 |
449 ad9389b_rd(sd, 0x09);
451 CTS = (ad9389b_rd(sd, 0x04) & 0xf) << 16 |
452 ad9389b_rd(sd, 0x05) << 8 |
453 ad9389b_rd(sd, 0x06);
454 N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 |
455 ad9389b_rd(sd, 0x02) << 8 |
456 ad9389b_rd(sd, 0x03);
458 v4l2_info(sd,
"ad9389b: CTS %s mode: N %d, CTS %d\n",
459 manual_cts ?
"manual" :
"automatic", N, CTS);
461 v4l2_info(sd,
"ad9389b: VIC: detected %d, sent %d\n",
462 vic_detect, vic_sent);
471 u32 frame_size = frame_width * frame_height;
473 v4l2_info(sd,
"timings: %ux%u%s%u (%ux%u). Pix freq. = %u Hz. Polarities = 0x%x\n",
475 frame_size > 0 ? (
unsigned)bt->
pixelclock / frame_size : 0,
476 frame_width, frame_height,
485 static int ad9389b_s_power(
struct v4l2_subdev *sd,
int on)
492 v4l2_dbg(1,
debug, sd,
"%s: power %s\n", __func__, on ?
"on" :
"off");
498 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40);
505 for (i = 0; i <
retries; i++) {
506 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x0);
507 if ((ad9389b_rd(sd, 0x41) & 0x40) == 0)
509 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40);
514 ad9389b_s_power(sd, 0);
519 "needed %d retries to powerup the ad9389b\n", i);
522 ad9389b_wr_and_or(sd, 0xba, 0xef, 0x10);
525 ad9389b_wr_and_or(sd, 0x98, 0xf0, 0x07);
526 ad9389b_wr(sd, 0x9c, 0x38);
527 ad9389b_wr_and_or(sd, 0x9d, 0xfc, 0x01);
533 ad9389b_wr(sd, 0xa2, 0x87);
538 ad9389b_wr(sd, 0xa3, 0x87);
540 ad9389b_wr(sd, 0x0a, 0x01);
541 ad9389b_wr(sd, 0xbb, 0xff);
544 ad9389b_wr(sd, 0xc9, 0xf);
549 static void ad9389b_set_isr(
struct v4l2_subdev *sd,
bool enable)
559 else if (ad9389b_have_hotplug(sd))
571 ad9389b_wr(sd, 0x94, irqs);
572 irqs_rd = ad9389b_rd(sd, 0x94);
573 }
while (retries-- && irqs_rd != irqs);
576 v4l2_err(sd,
"Could not set interrupts: hw failure?\n");
585 ad9389b_set_isr(sd,
false);
586 irq_status = ad9389b_rd(sd, 0x96);
588 ad9389b_wr(sd, 0x96, irq_status);
591 ad9389b_check_monitor_present_status(sd);
593 ad9389b_check_edid_status(sd);
596 ad9389b_set_isr(sd,
true);
602 .log_status = ad9389b_log_status,
603 .g_chip_ident = ad9389b_g_chip_ident,
604 #ifdef CONFIG_VIDEO_ADV_DEBUG
605 .g_register = ad9389b_g_register,
606 .s_register = ad9389b_s_register,
608 .s_power = ad9389b_s_power,
609 .interrupt_service_routine = ad9389b_isr,
614 static int ad9389b_get_edid(
struct v4l2_subdev *sd,
struct v4l2_subdev_edid *edid)
620 if (edid->blocks == 0 || edid->blocks > 256)
624 if (!state->
edid.segments) {
628 if (edid->start_block >= state->
edid.segments * 2)
630 if (edid->blocks + edid->start_block >= state->
edid.segments * 2)
631 edid->blocks = state->
edid.segments * 2 - edid->start_block;
632 memcpy(edid->edid, &state->
edid.data[edid->start_block * 128],
638 .get_edid = ad9389b_get_edid,
644 static int ad9389b_s_stream(
struct v4l2_subdev *sd,
int enable)
648 v4l2_dbg(1,
debug, sd,
"%s: %sable\n", __func__, (enable ?
"en" :
"dis"));
650 ad9389b_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c));
652 ad9389b_check_monitor_present_status(sd);
654 ad9389b_s_power(sd, 0);
723 static int ad9389b_s_dv_timings(
struct v4l2_subdev *sd,
735 if (timings->
bt.interlaced)
737 if (timings->
bt.pixelclock < 27000000 ||
738 timings->
bt.pixelclock > 170000000)
743 for (i = 0; ad9389b_timings[
i].
bt.width; i++) {
745 *timings = ad9389b_timings[
i];
760 ad9389b_set_manual_pll_gear(sd, (
u32)timings->
bt.pixelclock);
763 ad9389b_set_IT_content_AVI_InfoFrame(sd);
768 static int ad9389b_g_dv_timings(
struct v4l2_subdev *sd,
783 static int ad9389b_enum_dv_timings(
struct v4l2_subdev *sd,
794 static int ad9389b_dv_timings_cap(
struct v4l2_subdev *sd,
798 cap->
bt.max_width = 1920;
799 cap->
bt.max_height = 1200;
800 cap->
bt.min_pixelclock = 27000000;
801 cap->
bt.max_pixelclock = 170000000;
810 .s_stream = ad9389b_s_stream,
811 .s_dv_timings = ad9389b_s_dv_timings,
812 .g_dv_timings = ad9389b_g_dv_timings,
813 .enum_dv_timings = ad9389b_enum_dv_timings,
814 .dv_timings_cap = ad9389b_dv_timings_cap,
817 static int ad9389b_s_audio_stream(
struct v4l2_subdev *sd,
int enable)
819 v4l2_dbg(1,
debug, sd,
"%s: %sable\n", __func__, (enable ?
"en" :
"dis"));
822 ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x80);
824 ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x40);
834 case 32000: N = 4096;
break;
835 case 44100: N = 6272;
break;
836 case 48000: N = 6144;
break;
837 case 88200: N = 12544;
break;
838 case 96000: N = 12288;
break;
839 case 176400: N = 25088;
break;
840 case 192000: N = 24576;
break;
846 ad9389b_wr(sd, 0x01, (N >> 16) & 0xf);
847 ad9389b_wr(sd, 0x02, (N >> 8) & 0xff);
848 ad9389b_wr(sd, 0x03, N & 0xff);
853 static int ad9389b_s_i2s_clock_freq(
struct v4l2_subdev *sd,
u32 freq)
858 case 32000: i2s_sf = 0x30;
break;
859 case 44100: i2s_sf = 0x00;
break;
860 case 48000: i2s_sf = 0x20;
break;
861 case 88200: i2s_sf = 0x80;
break;
862 case 96000: i2s_sf = 0xa0;
break;
863 case 176400: i2s_sf = 0xc0;
break;
864 case 192000: i2s_sf = 0xe0;
break;
870 ad9389b_wr_and_or(sd, 0x15, 0xf, i2s_sf);
881 ad9389b_wr_and_or(sd, 0x50, 0x1f, 0x20);
883 ad9389b_wr(sd, 0x51, 0x00);
887 ad9389b_wr_and_or(sd, 0x14, 0xf0, 0x02);
893 .s_stream = ad9389b_s_audio_stream,
894 .s_clock_freq = ad9389b_s_clock_freq,
895 .s_i2s_clock_freq = ad9389b_s_i2s_clock_freq,
896 .s_routing = ad9389b_s_routing,
902 .core = &ad9389b_core_ops,
903 .video = &ad9389b_video_ops,
904 .audio = &ad9389b_audio_ops,
905 .pad = &ad9389b_pad_ops,
909 static void ad9389b_dbg_dump_edid(
int lvl,
int debug,
struct v4l2_subdev *sd,
917 v4l2_dbg(lvl, debug, sd,
"edid segment %d\n", segment);
918 for (i = 0; i < 256; i += 16) {
924 for (j = i; j < i + 16; j++) {
925 sprintf(bp,
"0x%02x, ", buf[j]);
929 v4l2_dbg(lvl, debug, sd,
"%s\n", b);
941 v4l2_dbg(1, debug, sd,
"%s:\n", __func__);
943 if (ad9389b_check_edid_status(sd)) {
948 if (ad9389b_have_hotplug(sd)) {
952 if (state->
edid.read_retries) {
953 state->
edid.read_retries--;
955 ad9389b_wr(sd, 0xc9, 0xf);
964 ed.segment = ad9389b_rd(sd, 0xc4);
966 v4l2_dbg(1, debug, sd,
"%s: no edid found\n", __func__);
969 static void ad9389b_audio_setup(
struct v4l2_subdev *sd)
971 v4l2_dbg(1, debug, sd,
"%s\n", __func__);
973 ad9389b_s_i2s_clock_freq(sd, 48000);
974 ad9389b_s_clock_freq(sd, 48000);
975 ad9389b_s_routing(sd, 0, 0, 0);
985 v4l2_dbg(1, debug, sd,
"%s\n", __func__);
988 ad9389b_wr_and_or(sd, 0x15, 0xf1, 0x0);
990 ad9389b_wr_and_or(sd, 0x16, 0x3f, 0x0);
993 ad9389b_wr_and_or(sd, 0x17, 0xe1, 0x0e);
995 ad9389b_wr_and_or(sd, 0x3b, 0x9e, 0x0);
997 ad9389b_wr_and_or(sd, 0x45, 0xc7, 0x08);
999 ad9389b_wr_and_or(sd, 0x46, 0x3f, 0x80);
1001 ad9389b_wr(sd, 0x3c, 0x0);
1003 ad9389b_wr(sd, 0x47, 0x80);
1005 ad9389b_wr_and_or(sd, 0xaf, 0xef, 0x0);
1007 ad9389b_wr_and_or(sd, 0xba, 0x1f, 0x60);
1009 ad9389b_audio_setup(sd);
1013 ad9389b_set_IT_content_AVI_InfoFrame(sd);
1016 static void ad9389b_notify_monitor_detect(
struct v4l2_subdev *sd)
1025 static void ad9389b_check_monitor_present_status(
struct v4l2_subdev *sd)
1029 u8 status = ad9389b_rd(sd, 0x42);
1031 v4l2_dbg(1, debug, sd,
"%s: status: 0x%x%s%s\n",
1034 status & MASK_AD9389B_HPD_DETECT ?
", hotplug" :
"",
1037 if ((status & MASK_AD9389B_HPD_DETECT) &&
1040 "%s: hotplug and (rx-sense or edid)\n", __func__);
1042 v4l2_dbg(1, debug, sd,
"%s: monitor detected\n", __func__);
1044 ad9389b_set_isr(sd,
true);
1045 if (!ad9389b_s_power(sd,
true)) {
1047 "%s: monitor detected, powerup failed\n", __func__);
1051 ad9389b_notify_monitor_detect(sd);
1056 }
else if (status & MASK_AD9389B_HPD_DETECT) {
1057 v4l2_dbg(1, debug, sd,
"%s: hotplug detected\n", __func__);
1061 }
else if (!(status & MASK_AD9389B_HPD_DETECT)) {
1062 v4l2_dbg(1, debug, sd,
"%s: hotplug not detected\n", __func__);
1064 v4l2_dbg(1, debug, sd,
"%s: monitor not detected\n", __func__);
1066 ad9389b_notify_monitor_detect(sd);
1068 ad9389b_s_power(sd,
false);
1078 static bool edid_block_verify_crc(
u8 *edid_block)
1083 for (i = 0; i < 127; i++)
1084 sum += *(edid_block + i);
1085 return ((255 - sum + 1) == edid_block[127]);
1088 static bool edid_segment_verify_crc(
struct v4l2_subdev *sd,
u32 segment)
1091 u32 blocks = state->
edid.blocks;
1094 if (edid_block_verify_crc(&data[segment * 256])) {
1095 if ((segment + 1) * 2 <= blocks)
1096 return edid_block_verify_crc(&data[segment * 256 + 128]);
1102 static bool ad9389b_check_edid_status(
struct v4l2_subdev *sd)
1107 u8 edidRdy = ad9389b_rd(sd, 0xc5);
1109 v4l2_dbg(1, debug, sd,
"%s: edid ready (retries: %d)\n",
1115 segment = ad9389b_rd(sd, 0xc4);
1117 v4l2_err(sd,
"edid segment number too big\n");
1120 v4l2_dbg(1, debug, sd,
"%s: got segment %d\n", __func__, segment);
1121 ad9389b_edid_rd(sd, 256, &state->
edid.data[segment * 256]);
1122 ad9389b_dbg_dump_edid(2, debug, sd, segment,
1123 &state->
edid.data[segment * 256]);
1125 state->
edid.blocks = state->
edid.data[0x7e] + 1;
1126 v4l2_dbg(1, debug, sd,
"%s: %d blocks in total\n",
1127 __func__, state->
edid.blocks);
1129 if (!edid_segment_verify_crc(sd, segment)) {
1131 ad9389b_s_power(sd,
false);
1132 ad9389b_s_power(sd,
true);
1136 state->
edid.segments = segment + 1;
1137 if (((state->
edid.data[0x7e] >> 1) + 1) > state->
edid.segments) {
1139 v4l2_dbg(1, debug, sd,
"%s: request segment %d\n",
1140 __func__, state->
edid.segments);
1141 ad9389b_wr(sd, 0xc9, 0xf);
1142 ad9389b_wr(sd, 0xc4, state->
edid.segments);
1160 static void ad9389b_init_setup(
struct v4l2_subdev *sd)
1165 v4l2_dbg(1, debug, sd,
"%s\n", __func__);
1168 ad9389b_wr(sd, 0x96, 0xff);
1172 ad9389b_set_isr(sd,
false);
1188 v4l_dbg(1, debug, client,
"detecting ad9389b client on address 0x%x\n",
1196 if (pdata ==
NULL) {
1197 v4l_err(client,
"No platform data!\n");
1248 v4l2_dbg(1, debug, sd,
"reg 0x41 0x%x, chip version (reg 0x00) 0x%x\n",
1253 v4l2_err(sd,
"failed to register edid i2c client\n");
1259 v4l2_err(sd,
"could not create workqueue\n");
1266 ad9389b_init_setup(sd);
1267 ad9389b_set_isr(sd,
true);
1286 static int ad9389b_remove(
struct i2c_client *client)
1288 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1293 v4l2_dbg(1, debug, sd,
"%s removed @ 0x%x (%s)\n", client->
name,
1296 ad9389b_s_stream(sd,
false);
1297 ad9389b_s_audio_stream(sd,
false);
1298 ad9389b_init_setup(sd);
1305 kfree(get_ad9389b_state(sd));
1323 .probe = ad9389b_probe,
1324 .remove = ad9389b_remove,
1325 .id_table = ad9389b_id,