Lines Matching +full:1 +full:- +full:sd

1 // SPDX-License-Identifier: GPL-2.0-only
10 * REF_01 - Analog Devices, Programming Guide, AD9889B/AD9389B,
21 #include <linux/v4l2-dv-timings.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-dv-timings.h>
25 #include <media/v4l2-ctrls.h>
30 MODULE_PARM_DESC(debug, "debug level (0-2)");
69 struct v4l2_subdev sd; member
75 /* Did we receive hotplug and rx-sense signals? */
92 static void ad9389b_check_monitor_present_status(struct v4l2_subdev *sd);
93 static bool ad9389b_check_edid_status(struct v4l2_subdev *sd);
94 static void ad9389b_setup(struct v4l2_subdev *sd);
95 static int ad9389b_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq);
96 static int ad9389b_s_clock_freq(struct v4l2_subdev *sd, u32 freq);
98 static inline struct ad9389b_state *get_ad9389b_state(struct v4l2_subdev *sd) in get_ad9389b_state() argument
100 return container_of(sd, struct ad9389b_state, sd); in get_ad9389b_state()
105 return &container_of(ctrl->handler, struct ad9389b_state, hdl)->sd; in to_sd()
108 /* ------------------------ I2C ----------------------------------------------- */
110 static int ad9389b_rd(struct v4l2_subdev *sd, u8 reg) in ad9389b_rd() argument
112 struct i2c_client *client = v4l2_get_subdevdata(sd); in ad9389b_rd()
117 static int ad9389b_wr(struct v4l2_subdev *sd, u8 reg, u8 val) in ad9389b_wr() argument
119 struct i2c_client *client = v4l2_get_subdevdata(sd); in ad9389b_wr()
128 v4l2_err(sd, "%s: failed reg 0x%x, val 0x%x\n", __func__, reg, val); in ad9389b_wr()
132 /* To set specific bits in the register, a clear-mask is given (to be AND-ed),
133 and then the value-mask (to be OR-ed). */
134 static inline void ad9389b_wr_and_or(struct v4l2_subdev *sd, u8 reg, in ad9389b_wr_and_or() argument
137 ad9389b_wr(sd, reg, (ad9389b_rd(sd, reg) & clr_mask) | val_mask); in ad9389b_wr_and_or()
140 static void ad9389b_edid_rd(struct v4l2_subdev *sd, u16 len, u8 *buf) in ad9389b_edid_rd() argument
142 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_edid_rd()
145 v4l2_dbg(1, debug, sd, "%s:\n", __func__); in ad9389b_edid_rd()
148 buf[i] = i2c_smbus_read_byte_data(state->edid_i2c_client, i); in ad9389b_edid_rd()
151 static inline bool ad9389b_have_hotplug(struct v4l2_subdev *sd) in ad9389b_have_hotplug() argument
153 return ad9389b_rd(sd, 0x42) & MASK_AD9389B_HPD_DETECT; in ad9389b_have_hotplug()
156 static inline bool ad9389b_have_rx_sense(struct v4l2_subdev *sd) in ad9389b_have_rx_sense() argument
158 return ad9389b_rd(sd, 0x42) & MASK_AD9389B_MSEN_DETECT; in ad9389b_have_rx_sense()
161 static void ad9389b_csc_conversion_mode(struct v4l2_subdev *sd, u8 mode) in ad9389b_csc_conversion_mode() argument
163 ad9389b_wr_and_or(sd, 0x17, 0xe7, (mode & 0x3)<<3); in ad9389b_csc_conversion_mode()
164 ad9389b_wr_and_or(sd, 0x18, 0x9f, (mode & 0x3)<<5); in ad9389b_csc_conversion_mode()
167 static void ad9389b_csc_coeff(struct v4l2_subdev *sd, in ad9389b_csc_coeff() argument
173 ad9389b_wr_and_or(sd, 0x18, 0xe0, A1>>8); in ad9389b_csc_coeff()
174 ad9389b_wr(sd, 0x19, A1); in ad9389b_csc_coeff()
175 ad9389b_wr_and_or(sd, 0x1A, 0xe0, A2>>8); in ad9389b_csc_coeff()
176 ad9389b_wr(sd, 0x1B, A2); in ad9389b_csc_coeff()
177 ad9389b_wr_and_or(sd, 0x1c, 0xe0, A3>>8); in ad9389b_csc_coeff()
178 ad9389b_wr(sd, 0x1d, A3); in ad9389b_csc_coeff()
179 ad9389b_wr_and_or(sd, 0x1e, 0xe0, A4>>8); in ad9389b_csc_coeff()
180 ad9389b_wr(sd, 0x1f, A4); in ad9389b_csc_coeff()
183 ad9389b_wr_and_or(sd, 0x20, 0xe0, B1>>8); in ad9389b_csc_coeff()
184 ad9389b_wr(sd, 0x21, B1); in ad9389b_csc_coeff()
185 ad9389b_wr_and_or(sd, 0x22, 0xe0, B2>>8); in ad9389b_csc_coeff()
186 ad9389b_wr(sd, 0x23, B2); in ad9389b_csc_coeff()
187 ad9389b_wr_and_or(sd, 0x24, 0xe0, B3>>8); in ad9389b_csc_coeff()
188 ad9389b_wr(sd, 0x25, B3); in ad9389b_csc_coeff()
189 ad9389b_wr_and_or(sd, 0x26, 0xe0, B4>>8); in ad9389b_csc_coeff()
190 ad9389b_wr(sd, 0x27, B4); in ad9389b_csc_coeff()
193 ad9389b_wr_and_or(sd, 0x28, 0xe0, C1>>8); in ad9389b_csc_coeff()
194 ad9389b_wr(sd, 0x29, C1); in ad9389b_csc_coeff()
195 ad9389b_wr_and_or(sd, 0x2A, 0xe0, C2>>8); in ad9389b_csc_coeff()
196 ad9389b_wr(sd, 0x2B, C2); in ad9389b_csc_coeff()
197 ad9389b_wr_and_or(sd, 0x2C, 0xe0, C3>>8); in ad9389b_csc_coeff()
198 ad9389b_wr(sd, 0x2D, C3); in ad9389b_csc_coeff()
199 ad9389b_wr_and_or(sd, 0x2E, 0xe0, C4>>8); in ad9389b_csc_coeff()
200 ad9389b_wr(sd, 0x2F, C4); in ad9389b_csc_coeff()
203 static void ad9389b_csc_rgb_full2limit(struct v4l2_subdev *sd, bool enable) in ad9389b_csc_rgb_full2limit() argument
208 ad9389b_csc_conversion_mode(sd, csc_mode); in ad9389b_csc_rgb_full2limit()
209 ad9389b_csc_coeff(sd, in ad9389b_csc_rgb_full2limit()
210 4096-564, 0, 0, 256, in ad9389b_csc_rgb_full2limit()
211 0, 4096-564, 0, 256, in ad9389b_csc_rgb_full2limit()
212 0, 0, 4096-564, 256); in ad9389b_csc_rgb_full2limit()
214 ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x1); in ad9389b_csc_rgb_full2limit()
215 /* AVI infoframe: Limited range RGB (16-235) */ in ad9389b_csc_rgb_full2limit()
216 ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x02); in ad9389b_csc_rgb_full2limit()
219 ad9389b_wr_and_or(sd, 0x3b, 0xfe, 0x0); in ad9389b_csc_rgb_full2limit()
220 /* AVI infoframe: Full range RGB (0-255) */ in ad9389b_csc_rgb_full2limit()
221 ad9389b_wr_and_or(sd, 0xcd, 0xf9, 0x04); in ad9389b_csc_rgb_full2limit()
225 static void ad9389b_set_IT_content_AVI_InfoFrame(struct v4l2_subdev *sd) in ad9389b_set_IT_content_AVI_InfoFrame() argument
227 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_set_IT_content_AVI_InfoFrame()
229 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) { in ad9389b_set_IT_content_AVI_InfoFrame()
231 ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x00); in ad9389b_set_IT_content_AVI_InfoFrame()
234 ad9389b_wr_and_or(sd, 0xcd, 0xbf, 0x40); in ad9389b_set_IT_content_AVI_InfoFrame()
238 static int ad9389b_set_rgb_quantization_mode(struct v4l2_subdev *sd, struct v4l2_ctrl *ctrl) in ad9389b_set_rgb_quantization_mode() argument
240 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_set_rgb_quantization_mode()
242 switch (ctrl->val) { in ad9389b_set_rgb_quantization_mode()
245 if (state->dv_timings.bt.flags & V4L2_DV_FL_IS_CE_VIDEO) { in ad9389b_set_rgb_quantization_mode()
246 /* CE format, RGB limited range (16-235) */ in ad9389b_set_rgb_quantization_mode()
247 ad9389b_csc_rgb_full2limit(sd, true); in ad9389b_set_rgb_quantization_mode()
249 /* not CE format, RGB full range (0-255) */ in ad9389b_set_rgb_quantization_mode()
250 ad9389b_csc_rgb_full2limit(sd, false); in ad9389b_set_rgb_quantization_mode()
254 /* RGB limited range (16-235) */ in ad9389b_set_rgb_quantization_mode()
255 ad9389b_csc_rgb_full2limit(sd, true); in ad9389b_set_rgb_quantization_mode()
258 /* RGB full range (0-255) */ in ad9389b_set_rgb_quantization_mode()
259 ad9389b_csc_rgb_full2limit(sd, false); in ad9389b_set_rgb_quantization_mode()
262 return -EINVAL; in ad9389b_set_rgb_quantization_mode()
267 static void ad9389b_set_manual_pll_gear(struct v4l2_subdev *sd, u32 pixelclock) in ad9389b_set_manual_pll_gear() argument
281 * (read from 0x9e [3:1]). in ad9389b_set_manual_pll_gear()
291 gear = 0x90; /* 1st gear */ in ad9389b_set_manual_pll_gear()
295 ad9389b_wr_and_or(sd, 0x98, 0x0f, gear); in ad9389b_set_manual_pll_gear()
298 /* ------------------------------ CTRL OPS ------------------------------ */
302 struct v4l2_subdev *sd = to_sd(ctrl); in ad9389b_s_ctrl() local
303 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_s_ctrl()
305 v4l2_dbg(1, debug, sd, in ad9389b_s_ctrl()
306 "%s: ctrl id: %d, ctrl->val %d\n", __func__, ctrl->id, ctrl->val); in ad9389b_s_ctrl()
308 if (state->hdmi_mode_ctrl == ctrl) { in ad9389b_s_ctrl()
309 /* Set HDMI or DVI-D */ in ad9389b_s_ctrl()
310 ad9389b_wr_and_or(sd, 0xaf, 0xfd, in ad9389b_s_ctrl()
311 ctrl->val == V4L2_DV_TX_MODE_HDMI ? 0x02 : 0x00); in ad9389b_s_ctrl()
314 if (state->rgb_quantization_range_ctrl == ctrl) in ad9389b_s_ctrl()
315 return ad9389b_set_rgb_quantization_mode(sd, ctrl); in ad9389b_s_ctrl()
316 return -EINVAL; in ad9389b_s_ctrl()
323 /* ---------------------------- CORE OPS ------------------------------------------- */
326 static int ad9389b_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg) in ad9389b_g_register() argument
328 reg->val = ad9389b_rd(sd, reg->reg & 0xff); in ad9389b_g_register()
329 reg->size = 1; in ad9389b_g_register()
333 static int ad9389b_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg) in ad9389b_s_register() argument
335 ad9389b_wr(sd, reg->reg & 0xff, reg->val & 0xff); in ad9389b_s_register()
340 static int ad9389b_log_status(struct v4l2_subdev *sd) in ad9389b_log_status() argument
342 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_log_status()
343 struct ad9389b_state_edid *edid = &state->edid; in ad9389b_log_status()
369 v4l2_info(sd, "chip revision %d\n", state->chip_revision); in ad9389b_log_status()
370 v4l2_info(sd, "power %s\n", state->power_on ? "on" : "off"); in ad9389b_log_status()
371 v4l2_info(sd, "%s hotplug, %s Rx Sense, %s EDID (%d block(s))\n", in ad9389b_log_status()
372 (ad9389b_rd(sd, 0x42) & MASK_AD9389B_HPD_DETECT) ? in ad9389b_log_status()
374 (ad9389b_rd(sd, 0x42) & MASK_AD9389B_MSEN_DETECT) ? in ad9389b_log_status()
376 edid->segments ? "found" : "no", edid->blocks); in ad9389b_log_status()
377 v4l2_info(sd, "%s output %s\n", in ad9389b_log_status()
378 (ad9389b_rd(sd, 0xaf) & 0x02) ? in ad9389b_log_status()
379 "HDMI" : "DVI-D", in ad9389b_log_status()
380 (ad9389b_rd(sd, 0xa1) & 0x3c) ? in ad9389b_log_status()
382 v4l2_info(sd, "ad9389b: %s\n", (ad9389b_rd(sd, 0xb8) & 0x40) ? in ad9389b_log_status()
384 v4l2_info(sd, "state: %s, error: %s, detect count: %u, msk/irq: %02x/%02x\n", in ad9389b_log_status()
385 states[ad9389b_rd(sd, 0xc8) & 0xf], in ad9389b_log_status()
386 errors[ad9389b_rd(sd, 0xc8) >> 4], in ad9389b_log_status()
387 state->edid_detect_counter, in ad9389b_log_status()
388 ad9389b_rd(sd, 0x94), ad9389b_rd(sd, 0x96)); in ad9389b_log_status()
389 manual_gear = ad9389b_rd(sd, 0x98) & 0x80; in ad9389b_log_status()
390 v4l2_info(sd, "ad9389b: RGB quantization: %s range\n", in ad9389b_log_status()
391 ad9389b_rd(sd, 0x3b) & 0x01 ? "limited" : "full"); in ad9389b_log_status()
392 v4l2_info(sd, "ad9389b: %s gear %d\n", in ad9389b_log_status()
394 manual_gear ? ((ad9389b_rd(sd, 0x98) & 0x70) >> 4) : in ad9389b_log_status()
395 ((ad9389b_rd(sd, 0x9e) & 0x0e) >> 1)); in ad9389b_log_status()
396 if (ad9389b_rd(sd, 0xaf) & 0x02) { in ad9389b_log_status()
398 u8 manual_cts = ad9389b_rd(sd, 0x0a) & 0x80; in ad9389b_log_status()
399 u32 N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 | in ad9389b_log_status()
400 ad9389b_rd(sd, 0x02) << 8 | in ad9389b_log_status()
401 ad9389b_rd(sd, 0x03); in ad9389b_log_status()
402 u8 vic_detect = ad9389b_rd(sd, 0x3e) >> 2; in ad9389b_log_status()
403 u8 vic_sent = ad9389b_rd(sd, 0x3d) & 0x3f; in ad9389b_log_status()
407 CTS = (ad9389b_rd(sd, 0x07) & 0xf) << 16 | in ad9389b_log_status()
408 ad9389b_rd(sd, 0x08) << 8 | in ad9389b_log_status()
409 ad9389b_rd(sd, 0x09); in ad9389b_log_status()
411 CTS = (ad9389b_rd(sd, 0x04) & 0xf) << 16 | in ad9389b_log_status()
412 ad9389b_rd(sd, 0x05) << 8 | in ad9389b_log_status()
413 ad9389b_rd(sd, 0x06); in ad9389b_log_status()
414 N = (ad9389b_rd(sd, 0x01) & 0xf) << 16 | in ad9389b_log_status()
415 ad9389b_rd(sd, 0x02) << 8 | in ad9389b_log_status()
416 ad9389b_rd(sd, 0x03); in ad9389b_log_status()
418 v4l2_info(sd, "ad9389b: CTS %s mode: N %d, CTS %d\n", in ad9389b_log_status()
421 v4l2_info(sd, "ad9389b: VIC: detected %d, sent %d\n", in ad9389b_log_status()
424 if (state->dv_timings.type == V4L2_DV_BT_656_1120) in ad9389b_log_status()
425 v4l2_print_dv_timings(sd->name, "timings: ", in ad9389b_log_status()
426 &state->dv_timings, false); in ad9389b_log_status()
428 v4l2_info(sd, "no timings set\n"); in ad9389b_log_status()
433 static int ad9389b_s_power(struct v4l2_subdev *sd, int on) in ad9389b_s_power() argument
435 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_s_power()
436 struct ad9389b_platform_data *pdata = &state->pdata; in ad9389b_s_power()
440 v4l2_dbg(1, debug, sd, "%s: power %s\n", __func__, on ? "on" : "off"); in ad9389b_s_power()
442 state->power_on = on; in ad9389b_s_power()
446 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40); in ad9389b_s_power()
454 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x0); in ad9389b_s_power()
455 if ((ad9389b_rd(sd, 0x41) & 0x40) == 0) in ad9389b_s_power()
457 ad9389b_wr_and_or(sd, 0x41, 0xbf, 0x40); in ad9389b_s_power()
461 v4l2_dbg(1, debug, sd, "failed to powerup the ad9389b\n"); in ad9389b_s_power()
462 ad9389b_s_power(sd, 0); in ad9389b_s_power()
465 if (i > 1) in ad9389b_s_power()
466 v4l2_dbg(1, debug, sd, in ad9389b_s_power()
470 ad9389b_wr_and_or(sd, 0xba, 0xef, 0x10); in ad9389b_s_power()
473 ad9389b_wr_and_or(sd, 0x98, 0xf0, 0x07); in ad9389b_s_power()
474 ad9389b_wr(sd, 0x9c, 0x38); in ad9389b_s_power()
475 ad9389b_wr_and_or(sd, 0x9d, 0xfc, 0x01); in ad9389b_s_power()
478 if (pdata->diff_data_drive_strength > 0) in ad9389b_s_power()
479 ad9389b_wr(sd, 0xa2, pdata->diff_data_drive_strength); in ad9389b_s_power()
481 ad9389b_wr(sd, 0xa2, 0x87); in ad9389b_s_power()
483 if (pdata->diff_clk_drive_strength > 0) in ad9389b_s_power()
484 ad9389b_wr(sd, 0xa3, pdata->diff_clk_drive_strength); in ad9389b_s_power()
486 ad9389b_wr(sd, 0xa3, 0x87); in ad9389b_s_power()
488 ad9389b_wr(sd, 0x0a, 0x01); in ad9389b_s_power()
489 ad9389b_wr(sd, 0xbb, 0xff); in ad9389b_s_power()
492 ad9389b_wr(sd, 0xc9, 0xf); in ad9389b_s_power()
497 static void ad9389b_set_isr(struct v4l2_subdev *sd, bool enable) in ad9389b_set_isr() argument
507 else if (ad9389b_have_hotplug(sd)) in ad9389b_set_isr()
511 * This i2c write can fail (approx. 1 in 1000 writes). But it in ad9389b_set_isr()
519 ad9389b_wr(sd, 0x94, irqs); in ad9389b_set_isr()
520 irqs_rd = ad9389b_rd(sd, 0x94); in ad9389b_set_isr()
521 } while (retries-- && irqs_rd != irqs); in ad9389b_set_isr()
524 v4l2_err(sd, "Could not set interrupts: hw failure?\n"); in ad9389b_set_isr()
528 static int ad9389b_isr(struct v4l2_subdev *sd, u32 status, bool *handled) in ad9389b_isr() argument
533 ad9389b_set_isr(sd, false); in ad9389b_isr()
534 irq_status = ad9389b_rd(sd, 0x96); in ad9389b_isr()
536 ad9389b_wr(sd, 0x96, irq_status); in ad9389b_isr()
538 ad9389b_set_isr(sd, true); in ad9389b_isr()
540 v4l2_dbg(1, debug, sd, "%s: irq_status 0x%x\n", __func__, irq_status); in ad9389b_isr()
543 ad9389b_check_monitor_present_status(sd); in ad9389b_isr()
545 ad9389b_check_edid_status(sd); in ad9389b_isr()
561 /* ------------------------------ VIDEO OPS ------------------------------ */
564 static int ad9389b_s_stream(struct v4l2_subdev *sd, int enable) in ad9389b_s_stream() argument
566 v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis")); in ad9389b_s_stream()
568 ad9389b_wr_and_or(sd, 0xa1, ~0x3c, (enable ? 0 : 0x3c)); in ad9389b_s_stream()
570 ad9389b_check_monitor_present_status(sd); in ad9389b_s_stream()
572 ad9389b_s_power(sd, 0); in ad9389b_s_stream()
588 static int ad9389b_s_dv_timings(struct v4l2_subdev *sd, in ad9389b_s_dv_timings() argument
591 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_s_dv_timings()
593 v4l2_dbg(1, debug, sd, "%s:\n", __func__); in ad9389b_s_dv_timings()
597 return -EINVAL; in ad9389b_s_dv_timings()
603 timings->bt.flags &= ~V4L2_DV_FL_REDUCED_FPS; in ad9389b_s_dv_timings()
606 state->dv_timings = *timings; in ad9389b_s_dv_timings()
609 ad9389b_set_rgb_quantization_mode(sd, state->rgb_quantization_range_ctrl); in ad9389b_s_dv_timings()
612 if (state->pdata.tmds_pll_gear == AD9389B_TMDS_PLL_GEAR_SEMI_AUTOMATIC) in ad9389b_s_dv_timings()
613 ad9389b_set_manual_pll_gear(sd, (u32)timings->bt.pixelclock); in ad9389b_s_dv_timings()
616 ad9389b_set_IT_content_AVI_InfoFrame(sd); in ad9389b_s_dv_timings()
621 static int ad9389b_g_dv_timings(struct v4l2_subdev *sd, in ad9389b_g_dv_timings() argument
624 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_g_dv_timings()
626 v4l2_dbg(1, debug, sd, "%s:\n", __func__); in ad9389b_g_dv_timings()
629 return -EINVAL; in ad9389b_g_dv_timings()
631 *timings = state->dv_timings; in ad9389b_g_dv_timings()
636 static int ad9389b_enum_dv_timings(struct v4l2_subdev *sd, in ad9389b_enum_dv_timings() argument
639 if (timings->pad != 0) in ad9389b_enum_dv_timings()
640 return -EINVAL; in ad9389b_enum_dv_timings()
646 static int ad9389b_dv_timings_cap(struct v4l2_subdev *sd, in ad9389b_dv_timings_cap() argument
649 if (cap->pad != 0) in ad9389b_dv_timings_cap()
650 return -EINVAL; in ad9389b_dv_timings_cap()
662 /* ------------------------------ PAD OPS ------------------------------ */
664 static int ad9389b_get_edid(struct v4l2_subdev *sd, struct v4l2_edid *edid) in ad9389b_get_edid() argument
666 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_get_edid()
668 if (edid->pad != 0) in ad9389b_get_edid()
669 return -EINVAL; in ad9389b_get_edid()
670 if (edid->blocks == 0 || edid->blocks > 256) in ad9389b_get_edid()
671 return -EINVAL; in ad9389b_get_edid()
672 if (!state->edid.segments) { in ad9389b_get_edid()
673 v4l2_dbg(1, debug, sd, "EDID segment 0 not found\n"); in ad9389b_get_edid()
674 return -ENODATA; in ad9389b_get_edid()
676 if (edid->start_block >= state->edid.segments * 2) in ad9389b_get_edid()
677 return -E2BIG; in ad9389b_get_edid()
678 if (edid->blocks + edid->start_block >= state->edid.segments * 2) in ad9389b_get_edid()
679 edid->blocks = state->edid.segments * 2 - edid->start_block; in ad9389b_get_edid()
680 memcpy(edid->edid, &state->edid.data[edid->start_block * 128], in ad9389b_get_edid()
681 128 * edid->blocks); in ad9389b_get_edid()
691 /* ------------------------------ AUDIO OPS ------------------------------ */
693 static int ad9389b_s_audio_stream(struct v4l2_subdev *sd, int enable) in ad9389b_s_audio_stream() argument
695 v4l2_dbg(1, debug, sd, "%s: %sable\n", __func__, (enable ? "en" : "dis")); in ad9389b_s_audio_stream()
698 ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x80); in ad9389b_s_audio_stream()
700 ad9389b_wr_and_or(sd, 0x45, 0x3f, 0x40); in ad9389b_s_audio_stream()
705 static int ad9389b_s_clock_freq(struct v4l2_subdev *sd, u32 freq) in ad9389b_s_clock_freq() argument
718 return -EINVAL; in ad9389b_s_clock_freq()
722 ad9389b_wr(sd, 0x01, (N >> 16) & 0xf); in ad9389b_s_clock_freq()
723 ad9389b_wr(sd, 0x02, (N >> 8) & 0xff); in ad9389b_s_clock_freq()
724 ad9389b_wr(sd, 0x03, N & 0xff); in ad9389b_s_clock_freq()
729 static int ad9389b_s_i2s_clock_freq(struct v4l2_subdev *sd, u32 freq) in ad9389b_s_i2s_clock_freq() argument
742 return -EINVAL; in ad9389b_s_i2s_clock_freq()
746 ad9389b_wr_and_or(sd, 0x15, 0xf, i2s_sf); in ad9389b_s_i2s_clock_freq()
751 static int ad9389b_s_routing(struct v4l2_subdev *sd, u32 input, u32 output, u32 config) in ad9389b_s_routing() argument
754 /* TODO See datasheet "Programmers guide" p. 39-40 */ in ad9389b_s_routing()
757 ad9389b_wr_and_or(sd, 0x50, 0x1f, 0x20); in ad9389b_s_routing()
759 ad9389b_wr(sd, 0x51, 0x00); in ad9389b_s_routing()
763 ad9389b_wr_and_or(sd, 0x14, 0xf0, 0x02); in ad9389b_s_routing()
775 /* --------------------- SUBDEV OPS --------------------------------------- */
784 /* ----------------------------------------------------------------------- */
785 static void ad9389b_dbg_dump_edid(int lvl, int debug, struct v4l2_subdev *sd, in ad9389b_dbg_dump_edid() argument
793 v4l2_dbg(lvl, debug, sd, "edid segment %d\n", segment); in ad9389b_dbg_dump_edid()
799 v4l2_dbg(lvl, debug, sd, "\n"); in ad9389b_dbg_dump_edid()
805 v4l2_dbg(lvl, debug, sd, "%s\n", b); in ad9389b_dbg_dump_edid()
814 struct v4l2_subdev *sd = &state->sd; in ad9389b_edid_handler() local
817 v4l2_dbg(1, debug, sd, "%s:\n", __func__); in ad9389b_edid_handler()
819 if (ad9389b_check_edid_status(sd)) { in ad9389b_edid_handler()
824 if (ad9389b_have_hotplug(sd)) { in ad9389b_edid_handler()
828 if (state->edid.read_retries) { in ad9389b_edid_handler()
829 state->edid.read_retries--; in ad9389b_edid_handler()
830 v4l2_dbg(1, debug, sd, "%s: edid read failed\n", __func__); in ad9389b_edid_handler()
831 ad9389b_s_power(sd, false); in ad9389b_edid_handler()
832 ad9389b_s_power(sd, true); in ad9389b_edid_handler()
833 schedule_delayed_work(&state->edid_handler, EDID_DELAY); in ad9389b_edid_handler()
840 ed.segment = ad9389b_rd(sd, 0xc4); in ad9389b_edid_handler()
841 v4l2_subdev_notify(sd, AD9389B_EDID_DETECT, (void *)&ed); in ad9389b_edid_handler()
842 v4l2_dbg(1, debug, sd, "%s: no edid found\n", __func__); in ad9389b_edid_handler()
845 static void ad9389b_audio_setup(struct v4l2_subdev *sd) in ad9389b_audio_setup() argument
847 v4l2_dbg(1, debug, sd, "%s\n", __func__); in ad9389b_audio_setup()
849 ad9389b_s_i2s_clock_freq(sd, 48000); in ad9389b_audio_setup()
850 ad9389b_s_clock_freq(sd, 48000); in ad9389b_audio_setup()
851 ad9389b_s_routing(sd, 0, 0, 0); in ad9389b_audio_setup()
857 static void ad9389b_setup(struct v4l2_subdev *sd) in ad9389b_setup() argument
859 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_setup()
861 v4l2_dbg(1, debug, sd, "%s\n", __func__); in ad9389b_setup()
864 ad9389b_wr_and_or(sd, 0x15, 0xf1, 0x0); in ad9389b_setup()
866 ad9389b_wr_and_or(sd, 0x16, 0x3f, 0x0); in ad9389b_setup()
867 /* 1st order interpolation 4:2:2 -> 4:4:4 up conversion, in ad9389b_setup()
869 ad9389b_wr_and_or(sd, 0x17, 0xf9, 0x06); in ad9389b_setup()
871 ad9389b_wr_and_or(sd, 0x45, 0xc7, 0x08); in ad9389b_setup()
873 ad9389b_wr_and_or(sd, 0x46, 0x3f, 0x80); in ad9389b_setup()
875 ad9389b_wr(sd, 0x3c, 0x0); in ad9389b_setup()
877 ad9389b_wr(sd, 0x47, 0x80); in ad9389b_setup()
879 ad9389b_wr_and_or(sd, 0xaf, 0xef, 0x0); in ad9389b_setup()
881 ad9389b_wr_and_or(sd, 0xba, 0x1f, 0x60); in ad9389b_setup()
883 ad9389b_audio_setup(sd); in ad9389b_setup()
885 v4l2_ctrl_handler_setup(&state->hdl); in ad9389b_setup()
887 ad9389b_set_IT_content_AVI_InfoFrame(sd); in ad9389b_setup()
890 static void ad9389b_notify_monitor_detect(struct v4l2_subdev *sd) in ad9389b_notify_monitor_detect() argument
893 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_notify_monitor_detect()
895 mdt.present = state->have_monitor; in ad9389b_notify_monitor_detect()
896 v4l2_subdev_notify(sd, AD9389B_MONITOR_DETECT, (void *)&mdt); in ad9389b_notify_monitor_detect()
899 static void ad9389b_update_monitor_present_status(struct v4l2_subdev *sd) in ad9389b_update_monitor_present_status() argument
901 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_update_monitor_present_status()
902 /* read hotplug and rx-sense state */ in ad9389b_update_monitor_present_status()
903 u8 status = ad9389b_rd(sd, 0x42); in ad9389b_update_monitor_present_status()
905 v4l2_dbg(1, debug, sd, "%s: status: 0x%x%s%s\n", in ad9389b_update_monitor_present_status()
909 status & MASK_AD9389B_MSEN_DETECT ? ", rx-sense" : ""); in ad9389b_update_monitor_present_status()
912 v4l2_dbg(1, debug, sd, "%s: hotplug detected\n", __func__); in ad9389b_update_monitor_present_status()
913 state->have_monitor = true; in ad9389b_update_monitor_present_status()
914 if (!ad9389b_s_power(sd, true)) { in ad9389b_update_monitor_present_status()
915 v4l2_dbg(1, debug, sd, in ad9389b_update_monitor_present_status()
919 ad9389b_setup(sd); in ad9389b_update_monitor_present_status()
920 ad9389b_notify_monitor_detect(sd); in ad9389b_update_monitor_present_status()
921 state->edid.read_retries = EDID_MAX_RETRIES; in ad9389b_update_monitor_present_status()
922 schedule_delayed_work(&state->edid_handler, EDID_DELAY); in ad9389b_update_monitor_present_status()
924 v4l2_dbg(1, debug, sd, "%s: hotplug not detected\n", __func__); in ad9389b_update_monitor_present_status()
925 state->have_monitor = false; in ad9389b_update_monitor_present_status()
926 ad9389b_notify_monitor_detect(sd); in ad9389b_update_monitor_present_status()
927 ad9389b_s_power(sd, false); in ad9389b_update_monitor_present_status()
928 memset(&state->edid, 0, sizeof(struct ad9389b_state_edid)); in ad9389b_update_monitor_present_status()
932 v4l2_ctrl_s_ctrl(state->hotplug_ctrl, ad9389b_have_hotplug(sd) ? 0x1 : 0x0); in ad9389b_update_monitor_present_status()
933 v4l2_ctrl_s_ctrl(state->rx_sense_ctrl, ad9389b_have_rx_sense(sd) ? 0x1 : 0x0); in ad9389b_update_monitor_present_status()
934 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0); in ad9389b_update_monitor_present_status()
937 ad9389b_s_ctrl(state->rgb_quantization_range_ctrl); in ad9389b_update_monitor_present_status()
938 ad9389b_s_ctrl(state->hdmi_mode_ctrl); in ad9389b_update_monitor_present_status()
941 static void ad9389b_check_monitor_present_status(struct v4l2_subdev *sd) in ad9389b_check_monitor_present_status() argument
943 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_check_monitor_present_status()
946 ad9389b_update_monitor_present_status(sd); in ad9389b_check_monitor_present_status()
952 while (state->power_on && (ad9389b_rd(sd, 0x41) & 0x40)) { in ad9389b_check_monitor_present_status()
954 v4l2_err(sd, "retried %d times, give up\n", retry); in ad9389b_check_monitor_present_status()
957 v4l2_dbg(1, debug, sd, "%s: reset and re-check status (%d)\n", __func__, retry); in ad9389b_check_monitor_present_status()
958 ad9389b_notify_monitor_detect(sd); in ad9389b_check_monitor_present_status()
959 cancel_delayed_work_sync(&state->edid_handler); in ad9389b_check_monitor_present_status()
960 memset(&state->edid, 0, sizeof(struct ad9389b_state_edid)); in ad9389b_check_monitor_present_status()
961 ad9389b_s_power(sd, false); in ad9389b_check_monitor_present_status()
962 ad9389b_update_monitor_present_status(sd); in ad9389b_check_monitor_present_status()
976 static bool edid_verify_crc(struct v4l2_subdev *sd, u32 segment) in edid_verify_crc() argument
978 struct ad9389b_state *state = get_ad9389b_state(sd); in edid_verify_crc()
979 u32 blocks = state->edid.blocks; in edid_verify_crc()
980 u8 *data = state->edid.data; in edid_verify_crc()
983 if ((segment + 1) * 2 <= blocks) in edid_verify_crc()
990 static bool edid_verify_header(struct v4l2_subdev *sd, u32 segment) in edid_verify_header() argument
995 struct ad9389b_state *state = get_ad9389b_state(sd); in edid_verify_header()
996 u8 *data = state->edid.data; in edid_verify_header()
1009 static bool ad9389b_check_edid_status(struct v4l2_subdev *sd) in ad9389b_check_edid_status() argument
1011 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_check_edid_status()
1014 u8 edidRdy = ad9389b_rd(sd, 0xc5); in ad9389b_check_edid_status()
1016 v4l2_dbg(1, debug, sd, "%s: edid ready (retries: %d)\n", in ad9389b_check_edid_status()
1017 __func__, EDID_MAX_RETRIES - state->edid.read_retries); in ad9389b_check_edid_status()
1022 segment = ad9389b_rd(sd, 0xc4); in ad9389b_check_edid_status()
1024 v4l2_err(sd, "edid segment number too big\n"); in ad9389b_check_edid_status()
1027 v4l2_dbg(1, debug, sd, "%s: got segment %d\n", __func__, segment); in ad9389b_check_edid_status()
1028 ad9389b_edid_rd(sd, 256, &state->edid.data[segment * 256]); in ad9389b_check_edid_status()
1029 ad9389b_dbg_dump_edid(2, debug, sd, segment, in ad9389b_check_edid_status()
1030 &state->edid.data[segment * 256]); in ad9389b_check_edid_status()
1032 state->edid.blocks = state->edid.data[0x7e] + 1; in ad9389b_check_edid_status()
1033 v4l2_dbg(1, debug, sd, "%s: %d blocks in total\n", in ad9389b_check_edid_status()
1034 __func__, state->edid.blocks); in ad9389b_check_edid_status()
1036 if (!edid_verify_crc(sd, segment) || in ad9389b_check_edid_status()
1037 !edid_verify_header(sd, segment)) { in ad9389b_check_edid_status()
1039 v4l2_err(sd, "%s: edid crc or header error\n", __func__); in ad9389b_check_edid_status()
1040 ad9389b_s_power(sd, false); in ad9389b_check_edid_status()
1041 ad9389b_s_power(sd, true); in ad9389b_check_edid_status()
1045 state->edid.segments = segment + 1; in ad9389b_check_edid_status()
1046 if (((state->edid.data[0x7e] >> 1) + 1) > state->edid.segments) { in ad9389b_check_edid_status()
1048 v4l2_dbg(1, debug, sd, "%s: request segment %d\n", in ad9389b_check_edid_status()
1049 __func__, state->edid.segments); in ad9389b_check_edid_status()
1050 ad9389b_wr(sd, 0xc9, 0xf); in ad9389b_check_edid_status()
1051 ad9389b_wr(sd, 0xc4, state->edid.segments); in ad9389b_check_edid_status()
1052 state->edid.read_retries = EDID_MAX_RETRIES; in ad9389b_check_edid_status()
1053 schedule_delayed_work(&state->edid_handler, EDID_DELAY); in ad9389b_check_edid_status()
1060 v4l2_subdev_notify(sd, AD9389B_EDID_DETECT, (void *)&ed); in ad9389b_check_edid_status()
1061 state->edid_detect_counter++; in ad9389b_check_edid_status()
1062 v4l2_ctrl_s_ctrl(state->have_edid0_ctrl, state->edid.segments ? 0x1 : 0x0); in ad9389b_check_edid_status()
1066 /* ----------------------------------------------------------------------- */
1068 static void ad9389b_init_setup(struct v4l2_subdev *sd) in ad9389b_init_setup() argument
1070 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_init_setup()
1071 struct ad9389b_state_edid *edid = &state->edid; in ad9389b_init_setup()
1073 v4l2_dbg(1, debug, sd, "%s\n", __func__); in ad9389b_init_setup()
1076 ad9389b_wr(sd, 0x96, 0xff); in ad9389b_init_setup()
1079 state->have_monitor = false; in ad9389b_init_setup()
1080 ad9389b_set_isr(sd, false); in ad9389b_init_setup()
1087 struct ad9389b_platform_data *pdata = client->dev.platform_data; in ad9389b_probe()
1089 struct v4l2_subdev *sd; in ad9389b_probe() local
1090 int err = -EIO; in ad9389b_probe()
1093 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) in ad9389b_probe()
1094 return -EIO; in ad9389b_probe()
1096 v4l_dbg(1, debug, client, "detecting ad9389b client on address 0x%x\n", in ad9389b_probe()
1097 client->addr << 1); in ad9389b_probe()
1099 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL); in ad9389b_probe()
1101 return -ENOMEM; in ad9389b_probe()
1106 return -ENODEV; in ad9389b_probe()
1108 memcpy(&state->pdata, pdata, sizeof(state->pdata)); in ad9389b_probe()
1110 sd = &state->sd; in ad9389b_probe()
1111 v4l2_i2c_subdev_init(sd, client, &ad9389b_ops); in ad9389b_probe()
1112 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; in ad9389b_probe()
1114 hdl = &state->hdl; in ad9389b_probe()
1117 state->hdmi_mode_ctrl = v4l2_ctrl_new_std_menu(hdl, &ad9389b_ctrl_ops, in ad9389b_probe()
1120 state->hotplug_ctrl = v4l2_ctrl_new_std(hdl, NULL, in ad9389b_probe()
1121 V4L2_CID_DV_TX_HOTPLUG, 0, 1, 0, 0); in ad9389b_probe()
1122 state->rx_sense_ctrl = v4l2_ctrl_new_std(hdl, NULL, in ad9389b_probe()
1123 V4L2_CID_DV_TX_RXSENSE, 0, 1, 0, 0); in ad9389b_probe()
1124 state->have_edid0_ctrl = v4l2_ctrl_new_std(hdl, NULL, in ad9389b_probe()
1125 V4L2_CID_DV_TX_EDID_PRESENT, 0, 1, 0, 0); in ad9389b_probe()
1126 state->rgb_quantization_range_ctrl = in ad9389b_probe()
1130 sd->ctrl_handler = hdl; in ad9389b_probe()
1131 if (hdl->error) { in ad9389b_probe()
1132 err = hdl->error; in ad9389b_probe()
1136 state->pad.flags = MEDIA_PAD_FL_SINK; in ad9389b_probe()
1137 sd->entity.function = MEDIA_ENT_F_DV_ENCODER; in ad9389b_probe()
1138 err = media_entity_pads_init(&sd->entity, 1, &state->pad); in ad9389b_probe()
1142 state->chip_revision = ad9389b_rd(sd, 0x0); in ad9389b_probe()
1143 if (state->chip_revision != 2) { in ad9389b_probe()
1144 v4l2_err(sd, "chip_revision %d != 2\n", state->chip_revision); in ad9389b_probe()
1145 err = -EIO; in ad9389b_probe()
1148 v4l2_dbg(1, debug, sd, "reg 0x41 0x%x, chip version (reg 0x00) 0x%x\n", in ad9389b_probe()
1149 ad9389b_rd(sd, 0x41), state->chip_revision); in ad9389b_probe()
1151 state->edid_i2c_client = i2c_new_dummy_device(client->adapter, (0x7e >> 1)); in ad9389b_probe()
1152 if (IS_ERR(state->edid_i2c_client)) { in ad9389b_probe()
1153 v4l2_err(sd, "failed to register edid i2c client\n"); in ad9389b_probe()
1154 err = PTR_ERR(state->edid_i2c_client); in ad9389b_probe()
1158 INIT_DELAYED_WORK(&state->edid_handler, ad9389b_edid_handler); in ad9389b_probe()
1159 state->dv_timings = dv1080p60; in ad9389b_probe()
1161 ad9389b_init_setup(sd); in ad9389b_probe()
1162 ad9389b_set_isr(sd, true); in ad9389b_probe()
1164 v4l2_info(sd, "%s found @ 0x%x (%s)\n", client->name, in ad9389b_probe()
1165 client->addr << 1, client->adapter->name); in ad9389b_probe()
1169 media_entity_cleanup(&sd->entity); in ad9389b_probe()
1171 v4l2_ctrl_handler_free(&state->hdl); in ad9389b_probe()
1175 /* ----------------------------------------------------------------------- */
1179 struct v4l2_subdev *sd = i2c_get_clientdata(client); in ad9389b_remove() local
1180 struct ad9389b_state *state = get_ad9389b_state(sd); in ad9389b_remove()
1182 state->chip_revision = -1; in ad9389b_remove()
1184 v4l2_dbg(1, debug, sd, "%s removed @ 0x%x (%s)\n", client->name, in ad9389b_remove()
1185 client->addr << 1, client->adapter->name); in ad9389b_remove()
1187 ad9389b_s_stream(sd, false); in ad9389b_remove()
1188 ad9389b_s_audio_stream(sd, false); in ad9389b_remove()
1189 ad9389b_init_setup(sd); in ad9389b_remove()
1190 cancel_delayed_work_sync(&state->edid_handler); in ad9389b_remove()
1191 i2c_unregister_device(state->edid_i2c_client); in ad9389b_remove()
1192 v4l2_device_unregister_subdev(sd); in ad9389b_remove()
1193 media_entity_cleanup(&sd->entity); in ad9389b_remove()
1194 v4l2_ctrl_handler_free(sd->ctrl_handler); in ad9389b_remove()
1198 /* ----------------------------------------------------------------------- */