1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Analog Devices ADV7511 HDMI transmitter driver
4 *
5 * Copyright 2012 Analog Devices Inc.
6 */
7
8#include <linux/clk.h>
9#include <linux/device.h>
10#include <linux/gpio/consumer.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/slab.h>
14
15#include <sound/pcm.h>
16
17#include <drm/drm_atomic.h>
18#include <drm/drm_atomic_helper.h>
19#include <drm/drm_bridge_connector.h>
20#include <drm/drm_edid.h>
21#include <drm/drm_of.h>
22#include <drm/drm_print.h>
23#include <drm/drm_probe_helper.h>
24#include <drm/display/drm_hdmi_helper.h>
25#include <drm/display/drm_hdmi_state_helper.h>
26
27#include "adv7511.h"
28
29/* ADI recommended values for proper operation. */
30static const struct reg_sequence adv7511_fixed_registers[] = {
31 { 0x98, 0x03 },
32 { 0x9a, 0xe0 },
33 { 0x9c, 0x30 },
34 { 0x9d, 0x61 },
35 { 0xa2, 0xa4 },
36 { 0xa3, 0xa4 },
37 { 0xe0, 0xd0 },
38 { 0xf9, 0x00 },
39 { 0x55, 0x02 },
40};
41
42/* -----------------------------------------------------------------------------
43 * Register access
44 */
45
46static const uint8_t adv7511_register_defaults[] = {
47 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */
48 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13,
49 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */
50 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84,
51 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */
52 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */
54 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0,
55 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */
56 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */
62 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */
66 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00,
67 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */
68 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14,
69 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */
72 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04,
73 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01,
75 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */
78 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79};
80
81static bool adv7511_register_volatile(struct device *dev, unsigned int reg)
82{
83 switch (reg) {
84 case ADV7511_REG_CHIP_REVISION:
85 case ADV7511_REG_SPDIF_FREQ:
86 case ADV7511_REG_CTS_AUTOMATIC1:
87 case ADV7511_REG_CTS_AUTOMATIC2:
88 case ADV7511_REG_VIC_DETECTED:
89 case ADV7511_REG_VIC_SEND:
90 case ADV7511_REG_AUX_VIC_DETECTED:
91 case ADV7511_REG_STATUS:
92 case ADV7511_REG_GC(1):
93 case ADV7511_REG_INT(0):
94 case ADV7511_REG_INT(1):
95 case ADV7511_REG_PLL_STATUS:
96 case ADV7511_REG_AN(0):
97 case ADV7511_REG_AN(1):
98 case ADV7511_REG_AN(2):
99 case ADV7511_REG_AN(3):
100 case ADV7511_REG_AN(4):
101 case ADV7511_REG_AN(5):
102 case ADV7511_REG_AN(6):
103 case ADV7511_REG_AN(7):
104 case ADV7511_REG_HDCP_STATUS:
105 case ADV7511_REG_BCAPS:
106 case ADV7511_REG_BKSV(0):
107 case ADV7511_REG_BKSV(1):
108 case ADV7511_REG_BKSV(2):
109 case ADV7511_REG_BKSV(3):
110 case ADV7511_REG_BKSV(4):
111 case ADV7511_REG_DDC_STATUS:
112 case ADV7511_REG_EDID_READ_CTRL:
113 case ADV7511_REG_BSTATUS(0):
114 case ADV7511_REG_BSTATUS(1):
115 case ADV7511_REG_CHIP_ID_HIGH:
116 case ADV7511_REG_CHIP_ID_LOW:
117 return true;
118 }
119
120 return false;
121}
122
123static const struct regmap_config adv7511_regmap_config = {
124 .reg_bits = 8,
125 .val_bits = 8,
126
127 .max_register = 0xff,
128 .cache_type = REGCACHE_MAPLE,
129 .reg_defaults_raw = adv7511_register_defaults,
130 .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults),
131
132 .volatile_reg = adv7511_register_volatile,
133};
134
135static const struct regmap_config adv7511_packet_config = {
136 .reg_bits = 8,
137 .val_bits = 8,
138
139 .max_register = 0xff,
140};
141
142/* -----------------------------------------------------------------------------
143 * Hardware configuration
144 */
145
146static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable,
147 const uint16_t *coeff,
148 unsigned int scaling_factor)
149{
150 unsigned int i;
151
152 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
153 ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE);
154
155 if (enable) {
156 for (i = 0; i < 12; ++i) {
157 regmap_update_bits(map: adv7511->regmap,
158 ADV7511_REG_CSC_UPPER(i),
159 mask: 0x1f, val: coeff[i] >> 8);
160 regmap_write(map: adv7511->regmap,
161 ADV7511_REG_CSC_LOWER(i),
162 val: coeff[i] & 0xff);
163 }
164 }
165
166 if (enable)
167 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
168 mask: 0xe0, val: 0x80 | (scaling_factor << 5));
169 else
170 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_CSC_UPPER(0),
171 mask: 0x80, val: 0x00);
172
173 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_CSC_UPPER(1),
174 ADV7511_CSC_UPDATE_MODE, val: 0);
175}
176
177static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet)
178{
179 if (packet & 0xff)
180 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
181 mask: packet, val: 0xff);
182
183 if (packet & 0xff00) {
184 packet >>= 8;
185 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
186 mask: packet, val: 0xff);
187 }
188
189 return 0;
190}
191
192static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet)
193{
194 if (packet & 0xff)
195 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_PACKET_ENABLE0,
196 mask: packet, val: 0x00);
197
198 if (packet & 0xff00) {
199 packet >>= 8;
200 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_PACKET_ENABLE1,
201 mask: packet, val: 0x00);
202 }
203
204 return 0;
205}
206
207/* Coefficients for adv7511 color space conversion */
208static const uint16_t adv7511_csc_ycbcr_to_rgb[] = {
209 0x0734, 0x04ad, 0x0000, 0x1c1b,
210 0x1ddc, 0x04ad, 0x1f24, 0x0135,
211 0x0000, 0x04ad, 0x087c, 0x1b77,
212};
213
214static void adv7511_set_config_csc(struct adv7511 *adv7511,
215 struct drm_connector *connector,
216 bool rgb)
217{
218 struct adv7511_video_config config;
219 bool output_format_422, output_format_ycbcr;
220 unsigned int mode;
221
222 if (rgb) {
223 config.csc_enable = false;
224 output_format_422 = false;
225 output_format_ycbcr = false;
226 } else {
227 config.csc_scaling_factor = ADV7511_CSC_SCALING_4;
228 config.csc_coefficents = adv7511_csc_ycbcr_to_rgb;
229
230 if ((connector->display_info.color_formats &
231 DRM_COLOR_FORMAT_YCBCR422) &&
232 connector->display_info.is_hdmi) {
233 config.csc_enable = false;
234 output_format_422 = true;
235 output_format_ycbcr = true;
236 } else {
237 config.csc_enable = true;
238 output_format_422 = false;
239 output_format_ycbcr = false;
240 }
241 }
242
243 if (connector->display_info.is_hdmi)
244 mode = ADV7511_HDMI_CFG_MODE_HDMI;
245 else
246 mode = ADV7511_HDMI_CFG_MODE_DVI;
247
248 adv7511_set_colormap(adv7511, enable: config.csc_enable,
249 coeff: config.csc_coefficents,
250 scaling_factor: config.csc_scaling_factor);
251
252 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, mask: 0x81,
253 val: (output_format_422 << 7) | output_format_ycbcr);
254
255 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG,
256 ADV7511_HDMI_CFG_MODE_MASK, val: mode);
257}
258
259static void adv7511_set_link_config(struct adv7511 *adv7511,
260 const struct adv7511_link_config *config)
261{
262 /*
263 * The input style values documented in the datasheet don't match the
264 * hardware register field values :-(
265 */
266 static const unsigned int input_styles[4] = { 0, 2, 1, 3 };
267
268 unsigned int clock_delay;
269 unsigned int color_depth;
270 unsigned int input_id;
271
272 clock_delay = (config->clock_delay + 1200) / 400;
273 color_depth = config->input_color_depth == 8 ? 3
274 : (config->input_color_depth == 10 ? 1 : 2);
275
276 /* TODO Support input ID 6 */
277 if (config->input_colorspace != HDMI_COLORSPACE_YUV422)
278 input_id = config->input_clock == ADV7511_INPUT_CLOCK_DDR
279 ? 5 : 0;
280 else if (config->input_clock == ADV7511_INPUT_CLOCK_DDR)
281 input_id = config->embedded_sync ? 8 : 7;
282 else if (config->input_clock == ADV7511_INPUT_CLOCK_2X)
283 input_id = config->embedded_sync ? 4 : 3;
284 else
285 input_id = config->embedded_sync ? 2 : 1;
286
287 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, mask: 0xf,
288 val: input_id);
289 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, mask: 0x7e,
290 val: (color_depth << 4) |
291 (input_styles[config->input_style] << 2));
292 regmap_write(map: adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2,
293 val: config->input_justification << 3);
294 regmap_write(map: adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ,
295 val: config->sync_pulse << 2);
296
297 regmap_write(map: adv7511->regmap, reg: 0xba, val: clock_delay << 5);
298
299 adv7511->embedded_sync = config->embedded_sync;
300 adv7511->hsync_polarity = config->hsync_polarity;
301 adv7511->vsync_polarity = config->vsync_polarity;
302 adv7511->rgb = config->input_colorspace == HDMI_COLORSPACE_RGB;
303}
304
305static void __adv7511_power_on(struct adv7511 *adv7511)
306{
307 adv7511->current_edid_segment = -1;
308
309 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER,
310 ADV7511_POWER_POWER_DOWN, val: 0);
311 if (adv7511->i2c_main->irq) {
312 /*
313 * Documentation says the INT_ENABLE registers are reset in
314 * POWER_DOWN mode. My 7511w preserved the bits, however.
315 * Still, let's be safe and stick to the documentation.
316 */
317 regmap_write(map: adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
318 ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
319 regmap_update_bits(map: adv7511->regmap,
320 ADV7511_REG_INT_ENABLE(1),
321 ADV7511_INT1_DDC_ERROR,
322 ADV7511_INT1_DDC_ERROR);
323 }
324
325 /*
326 * Per spec it is allowed to pulse the HPD signal to indicate that the
327 * EDID information has changed. Some monitors do this when they wakeup
328 * from standby or are enabled. When the HPD goes low the adv7511 is
329 * reset and the outputs are disabled which might cause the monitor to
330 * go to standby again. To avoid this we ignore the HPD pin for the
331 * first few seconds after enabling the output. On the other hand
332 * adv7535 require to enable HPD Override bit for proper HPD.
333 */
334 if (adv7511->info->hpd_override_enable)
335 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER2,
336 ADV7535_REG_POWER2_HPD_OVERRIDE,
337 ADV7535_REG_POWER2_HPD_OVERRIDE);
338 else
339 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER2,
340 ADV7511_REG_POWER2_HPD_SRC_MASK,
341 ADV7511_REG_POWER2_HPD_SRC_NONE);
342}
343
344static void adv7511_power_on(struct adv7511 *adv7511)
345{
346 __adv7511_power_on(adv7511);
347
348 /*
349 * Most of the registers are reset during power down or when HPD is low.
350 */
351 regcache_sync(map: adv7511->regmap);
352
353 if (adv7511->info->has_dsi)
354 adv7533_dsi_power_on(adv: adv7511);
355 adv7511->powered = true;
356}
357
358static void __adv7511_power_off(struct adv7511 *adv7511)
359{
360 /* TODO: setup additional power down modes */
361 if (adv7511->info->hpd_override_enable)
362 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER2,
363 ADV7535_REG_POWER2_HPD_OVERRIDE, val: 0);
364
365 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER,
366 ADV7511_POWER_POWER_DOWN,
367 ADV7511_POWER_POWER_DOWN);
368 regmap_update_bits(map: adv7511->regmap,
369 ADV7511_REG_INT_ENABLE(1),
370 ADV7511_INT1_DDC_ERROR, val: 0);
371 regcache_mark_dirty(map: adv7511->regmap);
372}
373
374static void adv7511_power_off(struct adv7511 *adv7511)
375{
376 __adv7511_power_off(adv7511);
377 if (adv7511->info->has_dsi)
378 adv7533_dsi_power_off(adv: adv7511);
379 adv7511->powered = false;
380}
381
382/* -----------------------------------------------------------------------------
383 * Interrupt and hotplug detection
384 */
385
386static bool adv7511_hpd(struct adv7511 *adv7511)
387{
388 unsigned int irq0;
389 int ret;
390
391 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_INT(0), val: &irq0);
392 if (ret < 0)
393 return false;
394
395 if (irq0 & ADV7511_INT0_HPD) {
396 regmap_write(map: adv7511->regmap, ADV7511_REG_INT(0),
397 ADV7511_INT0_HPD);
398 return true;
399 }
400
401 return false;
402}
403
404static void adv7511_hpd_work(struct work_struct *work)
405{
406 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
407 enum drm_connector_status status;
408 unsigned int val;
409 int ret;
410
411 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_STATUS, val: &val);
412 if (ret < 0)
413 status = connector_status_disconnected;
414 else if (val & ADV7511_STATUS_HPD)
415 status = connector_status_connected;
416 else
417 status = connector_status_disconnected;
418
419 /*
420 * The bridge resets its registers on unplug. So when we get a plug
421 * event and we're already supposed to be powered, cycle the bridge to
422 * restore its state.
423 */
424 if (status == connector_status_connected &&
425 adv7511->status == connector_status_disconnected &&
426 adv7511->powered) {
427 regcache_mark_dirty(map: adv7511->regmap);
428 adv7511_power_on(adv7511);
429 }
430
431 if (adv7511->status != status) {
432 adv7511->status = status;
433
434 drm_bridge_hpd_notify(bridge: &adv7511->bridge, status);
435 }
436}
437
438static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
439{
440 unsigned int irq0, irq1;
441 int ret;
442 int cec_status = IRQ_NONE;
443 int irq_status = IRQ_NONE;
444
445 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_INT(0), val: &irq0);
446 if (ret < 0)
447 return ret;
448
449 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_INT(1), val: &irq1);
450 if (ret < 0)
451 return ret;
452
453 regmap_write(map: adv7511->regmap, ADV7511_REG_INT(0), val: irq0);
454 regmap_write(map: adv7511->regmap, ADV7511_REG_INT(1), val: irq1);
455
456 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder) {
457 schedule_work(work: &adv7511->hpd_work);
458 irq_status = IRQ_HANDLED;
459 }
460
461 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
462 adv7511->edid_read = true;
463
464 if (adv7511->i2c_main->irq)
465 wake_up_all(&adv7511->wq);
466 irq_status = IRQ_HANDLED;
467 }
468
469#ifdef CONFIG_DRM_I2C_ADV7511_CEC
470 cec_status = adv7511_cec_irq_process(adv7511, irq1);
471#endif
472
473 /* If there is no IRQ to handle, exit indicating no IRQ data */
474 if (irq_status == IRQ_HANDLED || cec_status == IRQ_HANDLED)
475 return IRQ_HANDLED;
476
477 return IRQ_NONE;
478}
479
480static irqreturn_t adv7511_irq_handler(int irq, void *devid)
481{
482 struct adv7511 *adv7511 = devid;
483 int ret;
484
485 ret = adv7511_irq_process(adv7511, process_hpd: true);
486 return ret < 0 ? IRQ_NONE : ret;
487}
488
489/* -----------------------------------------------------------------------------
490 * EDID retrieval
491 */
492
493static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
494{
495 int ret;
496
497 if (adv7511->i2c_main->irq) {
498 ret = wait_event_interruptible_timeout(adv7511->wq,
499 adv7511->edid_read, msecs_to_jiffies(timeout));
500 } else {
501 for (; timeout > 0; timeout -= 25) {
502 ret = adv7511_irq_process(adv7511, process_hpd: false);
503 if (ret < 0)
504 break;
505
506 if (adv7511->edid_read)
507 break;
508
509 msleep(msecs: 25);
510 }
511 }
512
513 return adv7511->edid_read ? 0 : -EIO;
514}
515
516static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
517 size_t len)
518{
519 struct adv7511 *adv7511 = data;
520 struct i2c_msg xfer[2];
521 uint8_t offset;
522 unsigned int i;
523 int ret;
524
525 if (len > 128)
526 return -EINVAL;
527
528 if (adv7511->current_edid_segment != block / 2) {
529 unsigned int status;
530
531 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_DDC_STATUS,
532 val: &status);
533 if (ret < 0)
534 return ret;
535
536 if (status != 2) {
537 adv7511->edid_read = false;
538 regmap_write(map: adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
539 val: block);
540 ret = adv7511_wait_for_edid(adv7511, timeout: 200);
541 if (ret < 0)
542 return ret;
543 }
544
545 /* Break this apart, hopefully more I2C controllers will
546 * support 64 byte transfers than 256 byte transfers
547 */
548
549 xfer[0].addr = adv7511->i2c_edid->addr;
550 xfer[0].flags = 0;
551 xfer[0].len = 1;
552 xfer[0].buf = &offset;
553 xfer[1].addr = adv7511->i2c_edid->addr;
554 xfer[1].flags = I2C_M_RD;
555 xfer[1].len = 64;
556 xfer[1].buf = adv7511->edid_buf;
557
558 offset = 0;
559
560 for (i = 0; i < 4; ++i) {
561 ret = i2c_transfer(adap: adv7511->i2c_edid->adapter, msgs: xfer,
562 ARRAY_SIZE(xfer));
563 if (ret < 0)
564 return ret;
565 else if (ret != 2)
566 return -EIO;
567
568 xfer[1].buf += 64;
569 offset += 64;
570 }
571
572 adv7511->current_edid_segment = block / 2;
573 }
574
575 if (block % 2 == 0)
576 memcpy(buf, adv7511->edid_buf, len);
577 else
578 memcpy(buf, adv7511->edid_buf + 128, len);
579
580 return 0;
581}
582
583/* -----------------------------------------------------------------------------
584 * ADV75xx helpers
585 */
586
587static const struct drm_edid *adv7511_edid_read(struct adv7511 *adv7511,
588 struct drm_connector *connector)
589{
590 const struct drm_edid *drm_edid;
591
592 /* Reading the EDID only works if the device is powered */
593 if (!adv7511->powered) {
594 unsigned int edid_i2c_addr =
595 (adv7511->i2c_edid->addr << 1);
596
597 __adv7511_power_on(adv7511);
598
599 /* Reset the EDID_I2C_ADDR register as it might be cleared */
600 regmap_write(map: adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
601 val: edid_i2c_addr);
602 }
603
604 drm_edid = drm_edid_read_custom(connector, read_block: adv7511_get_edid_block, context: adv7511);
605
606 if (!adv7511->powered)
607 __adv7511_power_off(adv7511);
608
609 return drm_edid;
610}
611
612static enum drm_connector_status
613adv7511_detect(struct adv7511 *adv7511)
614{
615 enum drm_connector_status status;
616 unsigned int val;
617 bool hpd;
618 int ret;
619
620 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_STATUS, val: &val);
621 if (ret < 0)
622 return connector_status_disconnected;
623
624 if (val & ADV7511_STATUS_HPD)
625 status = connector_status_connected;
626 else
627 status = connector_status_disconnected;
628
629 hpd = adv7511_hpd(adv7511);
630
631 /* The chip resets itself when the cable is disconnected, so in case
632 * there is a pending HPD interrupt and the cable is connected there was
633 * at least one transition from disconnected to connected and the chip
634 * has to be reinitialized. */
635 if (status == connector_status_connected && hpd && adv7511->powered) {
636 regcache_mark_dirty(map: adv7511->regmap);
637 adv7511_power_on(adv7511);
638 if (adv7511->status == connector_status_connected)
639 status = connector_status_disconnected;
640 } else {
641 /* Renable HPD sensing */
642 if (adv7511->info->hpd_override_enable)
643 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER2,
644 ADV7535_REG_POWER2_HPD_OVERRIDE,
645 ADV7535_REG_POWER2_HPD_OVERRIDE);
646 else
647 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_POWER2,
648 ADV7511_REG_POWER2_HPD_SRC_MASK,
649 ADV7511_REG_POWER2_HPD_SRC_BOTH);
650 }
651
652 adv7511->status = status;
653 return status;
654}
655
656static void adv7511_mode_set(struct adv7511 *adv7511,
657 const struct drm_display_mode *adj_mode)
658{
659 unsigned int low_refresh_rate;
660 unsigned int hsync_polarity = 0;
661 unsigned int vsync_polarity = 0;
662
663 if (adv7511->embedded_sync) {
664 unsigned int hsync_offset, hsync_len;
665 unsigned int vsync_offset, vsync_len;
666
667 hsync_offset = adj_mode->crtc_hsync_start -
668 adj_mode->crtc_hdisplay;
669 vsync_offset = adj_mode->crtc_vsync_start -
670 adj_mode->crtc_vdisplay;
671 hsync_len = adj_mode->crtc_hsync_end -
672 adj_mode->crtc_hsync_start;
673 vsync_len = adj_mode->crtc_vsync_end -
674 adj_mode->crtc_vsync_start;
675
676 /* The hardware vsync generator has a off-by-one bug */
677 vsync_offset += 1;
678
679 regmap_write(map: adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
680 val: ((hsync_offset >> 10) & 0x7) << 5);
681 regmap_write(map: adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
682 val: (hsync_offset >> 2) & 0xff);
683 regmap_write(map: adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
684 val: ((hsync_offset & 0x3) << 6) |
685 ((hsync_len >> 4) & 0x3f));
686 regmap_write(map: adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
687 val: ((hsync_len & 0xf) << 4) |
688 ((vsync_offset >> 6) & 0xf));
689 regmap_write(map: adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
690 val: ((vsync_offset & 0x3f) << 2) |
691 ((vsync_len >> 8) & 0x3));
692 regmap_write(map: adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
693 val: vsync_len & 0xff);
694
695 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
696 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
697 } else {
698 enum adv7511_sync_polarity mode_hsync_polarity;
699 enum adv7511_sync_polarity mode_vsync_polarity;
700
701 /**
702 * If the input signal is always low or always high we want to
703 * invert or let it passthrough depending on the polarity of the
704 * current mode.
705 **/
706 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
707 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
708 else
709 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
710
711 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
712 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
713 else
714 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
715
716 if (adv7511->hsync_polarity != mode_hsync_polarity &&
717 adv7511->hsync_polarity !=
718 ADV7511_SYNC_POLARITY_PASSTHROUGH)
719 hsync_polarity = 1;
720
721 if (adv7511->vsync_polarity != mode_vsync_polarity &&
722 adv7511->vsync_polarity !=
723 ADV7511_SYNC_POLARITY_PASSTHROUGH)
724 vsync_polarity = 1;
725 }
726
727 if (drm_mode_vrefresh(mode: adj_mode) <= 24)
728 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
729 else if (drm_mode_vrefresh(mode: adj_mode) <= 25)
730 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
731 else if (drm_mode_vrefresh(mode: adj_mode) <= 30)
732 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
733 else
734 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
735
736 if (adv7511->info->type == ADV7511)
737 regmap_update_bits(map: adv7511->regmap, reg: 0xfb,
738 mask: 0x6, val: low_refresh_rate << 1);
739 else
740 regmap_update_bits(map: adv7511->regmap, reg: 0x4a,
741 mask: 0xc, val: low_refresh_rate << 2);
742
743 regmap_update_bits(map: adv7511->regmap, reg: 0x17,
744 mask: 0x60, val: (vsync_polarity << 6) | (hsync_polarity << 5));
745
746 drm_mode_copy(dst: &adv7511->curr_mode, src: adj_mode);
747
748 /* Update horizontal/vertical porch params */
749 if (adv7511->info->has_dsi && adv7511->use_timing_gen)
750 adv7533_dsi_config_timing_gen(adv: adv7511);
751
752 /*
753 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
754 * supposed to give better results.
755 */
756
757 adv7511->f_tmds = adj_mode->clock;
758}
759
760static int adv7511_connector_init(struct adv7511 *adv)
761{
762 struct drm_bridge *bridge = &adv->bridge;
763 struct drm_connector *connector;
764
765 connector = drm_bridge_connector_init(drm: bridge->dev, encoder: bridge->encoder);
766 if (IS_ERR(ptr: connector)) {
767 DRM_ERROR("Failed to initialize connector with drm\n");
768 return PTR_ERR(ptr: connector);
769 }
770
771 drm_connector_attach_encoder(connector, encoder: bridge->encoder);
772
773 return 0;
774}
775
776/* -----------------------------------------------------------------------------
777 * DRM Bridge Operations
778 */
779
780static const struct adv7511 *bridge_to_adv7511_const(const struct drm_bridge *bridge)
781{
782 return container_of(bridge, struct adv7511, bridge);
783}
784
785static void adv7511_bridge_atomic_enable(struct drm_bridge *bridge,
786 struct drm_atomic_state *state)
787{
788 struct adv7511 *adv = bridge_to_adv7511(bridge);
789 struct drm_connector *connector;
790 struct drm_connector_state *conn_state;
791 struct drm_crtc_state *crtc_state;
792
793 adv7511_power_on(adv7511: adv);
794
795 connector = drm_atomic_get_new_connector_for_encoder(state, encoder: bridge->encoder);
796 if (WARN_ON(!connector))
797 return;
798
799 conn_state = drm_atomic_get_new_connector_state(state, connector);
800 if (WARN_ON(!conn_state))
801 return;
802
803 crtc_state = drm_atomic_get_new_crtc_state(state, crtc: conn_state->crtc);
804 if (WARN_ON(!crtc_state))
805 return;
806
807 adv7511_set_config_csc(adv7511: adv, connector, rgb: adv->rgb);
808
809 adv7511_mode_set(adv7511: adv, adj_mode: &crtc_state->adjusted_mode);
810
811 drm_atomic_helper_connector_hdmi_update_infoframes(connector, state);
812}
813
814static void adv7511_bridge_atomic_disable(struct drm_bridge *bridge,
815 struct drm_atomic_state *state)
816{
817 struct adv7511 *adv = bridge_to_adv7511(bridge);
818
819 adv7511_power_off(adv7511: adv);
820}
821
822static enum drm_mode_status
823adv7511_bridge_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge,
824 const struct drm_display_mode *mode,
825 unsigned long long tmds_rate)
826{
827 const struct adv7511 *adv = bridge_to_adv7511_const(bridge);
828
829 if (tmds_rate > 1000ULL * adv->info->max_mode_clock_khz)
830 return MODE_CLOCK_HIGH;
831
832 return MODE_OK;
833}
834
835static enum drm_mode_status adv7511_bridge_mode_valid(struct drm_bridge *bridge,
836 const struct drm_display_info *info,
837 const struct drm_display_mode *mode)
838{
839 struct adv7511 *adv = bridge_to_adv7511(bridge);
840
841 if (!adv->info->has_dsi)
842 return MODE_OK;
843
844 return adv7533_mode_valid(adv, mode);
845}
846
847static int adv7511_bridge_attach(struct drm_bridge *bridge,
848 struct drm_encoder *encoder,
849 enum drm_bridge_attach_flags flags)
850{
851 struct adv7511 *adv = bridge_to_adv7511(bridge);
852 int ret = 0;
853
854 if (adv->next_bridge) {
855 ret = drm_bridge_attach(encoder, bridge: adv->next_bridge, previous: bridge,
856 flags: flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);
857 if (ret)
858 return ret;
859 }
860
861 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
862 ret = adv7511_connector_init(adv);
863 if (ret < 0)
864 return ret;
865 }
866
867 if (adv->i2c_main->irq)
868 regmap_write(map: adv->regmap, ADV7511_REG_INT_ENABLE(0),
869 ADV7511_INT0_HPD);
870
871 return ret;
872}
873
874static enum drm_connector_status
875adv7511_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
876{
877 struct adv7511 *adv = bridge_to_adv7511(bridge);
878
879 return adv7511_detect(adv7511: adv);
880}
881
882static const struct drm_edid *adv7511_bridge_edid_read(struct drm_bridge *bridge,
883 struct drm_connector *connector)
884{
885 struct adv7511 *adv = bridge_to_adv7511(bridge);
886
887 return adv7511_edid_read(adv7511: adv, connector);
888}
889
890static int adv7511_bridge_hdmi_clear_infoframe(struct drm_bridge *bridge,
891 enum hdmi_infoframe_type type)
892{
893 struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
894
895 switch (type) {
896 case HDMI_INFOFRAME_TYPE_AUDIO:
897 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AUDIO_INFOFRAME);
898 break;
899 case HDMI_INFOFRAME_TYPE_AVI:
900 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
901 break;
902 case HDMI_INFOFRAME_TYPE_SPD:
903 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPD);
904 break;
905 case HDMI_INFOFRAME_TYPE_VENDOR:
906 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
907 break;
908 default:
909 drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
910 break;
911 }
912
913 return 0;
914}
915
916static int adv7511_bridge_hdmi_write_infoframe(struct drm_bridge *bridge,
917 enum hdmi_infoframe_type type,
918 const u8 *buffer, size_t len)
919{
920 struct adv7511 *adv7511 = bridge_to_adv7511(bridge);
921
922 switch (type) {
923 case HDMI_INFOFRAME_TYPE_AUDIO:
924 /* send current Audio infoframe values while updating */
925 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
926 BIT(5), BIT(5));
927
928 /* The Audio infoframe id is not configurable */
929 regmap_bulk_write(map: adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME_VERSION,
930 val: buffer + 1, val_count: len - 1);
931
932 /* use Audio infoframe updated info */
933 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
934 BIT(5), val: 0);
935
936 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AUDIO_INFOFRAME);
937 break;
938 case HDMI_INFOFRAME_TYPE_AVI:
939 /* send current AVI infoframe values while updating */
940 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
941 BIT(6), BIT(6));
942
943 /* The AVI infoframe id is not configurable */
944 regmap_bulk_write(map: adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION,
945 val: buffer + 1, val_count: len - 1);
946
947 regmap_write(map: adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME_LENGTH, val: 0x2);
948 regmap_write(map: adv7511->regmap, ADV7511_REG_AUDIO_INFOFRAME(1), val: 0x1);
949
950 /* use AVI infoframe updated info */
951 regmap_update_bits(map: adv7511->regmap, ADV7511_REG_INFOFRAME_UPDATE,
952 BIT(6), val: 0);
953
954 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME);
955 break;
956 case HDMI_INFOFRAME_TYPE_SPD:
957 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPD);
958 regmap_bulk_write(map: adv7511->regmap_packet, ADV7511_PACKET_SPD(0),
959 val: buffer, val_count: len);
960 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPD);
961 break;
962 case HDMI_INFOFRAME_TYPE_VENDOR:
963 adv7511_packet_disable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
964 regmap_bulk_write(map: adv7511->regmap_packet, ADV7511_PACKET_SPARE1(0),
965 val: buffer, val_count: len);
966 adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_SPARE1);
967 break;
968 default:
969 drm_dbg_driver(adv7511->bridge.dev, "Unsupported HDMI InfoFrame %x\n", type);
970 break;
971 }
972
973 return 0;
974}
975
976static const struct drm_bridge_funcs adv7511_bridge_funcs = {
977 .mode_valid = adv7511_bridge_mode_valid,
978 .attach = adv7511_bridge_attach,
979 .detect = adv7511_bridge_detect,
980 .edid_read = adv7511_bridge_edid_read,
981
982 .atomic_enable = adv7511_bridge_atomic_enable,
983 .atomic_disable = adv7511_bridge_atomic_disable,
984 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
985 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
986 .atomic_reset = drm_atomic_helper_bridge_reset,
987
988 .hdmi_tmds_char_rate_valid = adv7511_bridge_hdmi_tmds_char_rate_valid,
989 .hdmi_clear_infoframe = adv7511_bridge_hdmi_clear_infoframe,
990 .hdmi_write_infoframe = adv7511_bridge_hdmi_write_infoframe,
991
992 .hdmi_audio_startup = adv7511_hdmi_audio_startup,
993 .hdmi_audio_prepare = adv7511_hdmi_audio_prepare,
994 .hdmi_audio_shutdown = adv7511_hdmi_audio_shutdown,
995
996 .hdmi_cec_init = adv7511_cec_init,
997 .hdmi_cec_enable = adv7511_cec_enable,
998 .hdmi_cec_log_addr = adv7511_cec_log_addr,
999 .hdmi_cec_transmit = adv7511_cec_transmit,
1000};
1001
1002/* -----------------------------------------------------------------------------
1003 * Probe & remove
1004 */
1005
1006static const char * const adv7511_supply_names[] = {
1007 "avdd",
1008 "dvdd",
1009 "pvdd",
1010 "bgvdd",
1011 "dvdd-3v",
1012};
1013
1014static const char * const adv7533_supply_names[] = {
1015 "avdd",
1016 "dvdd",
1017 "pvdd",
1018 "a2vdd",
1019 "v3p3",
1020 "v1p2",
1021};
1022
1023static int adv7511_init_regulators(struct adv7511 *adv)
1024{
1025 const char * const *supply_names = adv->info->supply_names;
1026 unsigned int num_supplies = adv->info->num_supplies;
1027 struct device *dev = &adv->i2c_main->dev;
1028 unsigned int i;
1029 int ret;
1030
1031 adv->supplies = devm_kcalloc(dev, n: num_supplies,
1032 size: sizeof(*adv->supplies), GFP_KERNEL);
1033 if (!adv->supplies)
1034 return -ENOMEM;
1035
1036 for (i = 0; i < num_supplies; i++)
1037 adv->supplies[i].supply = supply_names[i];
1038
1039 ret = devm_regulator_bulk_get(dev, num_consumers: num_supplies, consumers: adv->supplies);
1040 if (ret)
1041 return ret;
1042
1043 return regulator_bulk_enable(num_consumers: num_supplies, consumers: adv->supplies);
1044}
1045
1046static void adv7511_uninit_regulators(struct adv7511 *adv)
1047{
1048 regulator_bulk_disable(num_consumers: adv->info->num_supplies, consumers: adv->supplies);
1049}
1050
1051static bool adv7511_cec_register_volatile(struct device *dev, unsigned int reg)
1052{
1053 struct i2c_client *i2c = to_i2c_client(dev);
1054 struct adv7511 *adv7511 = i2c_get_clientdata(client: i2c);
1055
1056 reg -= adv7511->info->reg_cec_offset;
1057
1058 switch (reg) {
1059 case ADV7511_REG_CEC_RX1_FRAME_HDR:
1060 case ADV7511_REG_CEC_RX1_FRAME_DATA0 ... ADV7511_REG_CEC_RX1_FRAME_DATA0 + 14:
1061 case ADV7511_REG_CEC_RX1_FRAME_LEN:
1062 case ADV7511_REG_CEC_RX2_FRAME_HDR:
1063 case ADV7511_REG_CEC_RX2_FRAME_DATA0 ... ADV7511_REG_CEC_RX2_FRAME_DATA0 + 14:
1064 case ADV7511_REG_CEC_RX2_FRAME_LEN:
1065 case ADV7511_REG_CEC_RX3_FRAME_HDR:
1066 case ADV7511_REG_CEC_RX3_FRAME_DATA0 ... ADV7511_REG_CEC_RX3_FRAME_DATA0 + 14:
1067 case ADV7511_REG_CEC_RX3_FRAME_LEN:
1068 case ADV7511_REG_CEC_RX_STATUS:
1069 case ADV7511_REG_CEC_RX_BUFFERS:
1070 case ADV7511_REG_CEC_TX_LOW_DRV_CNT:
1071 return true;
1072 }
1073
1074 return false;
1075}
1076
1077static const struct regmap_config adv7511_cec_regmap_config = {
1078 .reg_bits = 8,
1079 .val_bits = 8,
1080
1081 .max_register = 0xff,
1082 .cache_type = REGCACHE_MAPLE,
1083 .volatile_reg = adv7511_cec_register_volatile,
1084};
1085
1086static int adv7511_init_cec_regmap(struct adv7511 *adv)
1087{
1088 int ret;
1089
1090 adv->i2c_cec = i2c_new_ancillary_device(client: adv->i2c_main, name: "cec",
1091 ADV7511_CEC_I2C_ADDR_DEFAULT);
1092 if (IS_ERR(ptr: adv->i2c_cec))
1093 return PTR_ERR(ptr: adv->i2c_cec);
1094
1095 regmap_write(map: adv->regmap, ADV7511_REG_CEC_I2C_ADDR,
1096 val: adv->i2c_cec->addr << 1);
1097
1098 i2c_set_clientdata(client: adv->i2c_cec, data: adv);
1099
1100 adv->regmap_cec = devm_regmap_init_i2c(adv->i2c_cec,
1101 &adv7511_cec_regmap_config);
1102 if (IS_ERR(ptr: adv->regmap_cec)) {
1103 ret = PTR_ERR(ptr: adv->regmap_cec);
1104 goto err;
1105 }
1106
1107 if (adv->info->reg_cec_offset == ADV7533_REG_CEC_OFFSET) {
1108 ret = adv7533_patch_cec_registers(adv);
1109 if (ret)
1110 goto err;
1111 }
1112
1113 return 0;
1114err:
1115 i2c_unregister_device(client: adv->i2c_cec);
1116 return ret;
1117}
1118
1119static int adv7511_parse_dt(struct device_node *np,
1120 struct adv7511_link_config *config)
1121{
1122 const char *str;
1123 int ret;
1124
1125 of_property_read_u32(np, propname: "adi,input-depth", out_value: &config->input_color_depth);
1126 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
1127 config->input_color_depth != 12)
1128 return -EINVAL;
1129
1130 ret = of_property_read_string(np, propname: "adi,input-colorspace", out_string: &str);
1131 if (ret < 0)
1132 return ret;
1133
1134 if (!strcmp(str, "rgb"))
1135 config->input_colorspace = HDMI_COLORSPACE_RGB;
1136 else if (!strcmp(str, "yuv422"))
1137 config->input_colorspace = HDMI_COLORSPACE_YUV422;
1138 else if (!strcmp(str, "yuv444"))
1139 config->input_colorspace = HDMI_COLORSPACE_YUV444;
1140 else
1141 return -EINVAL;
1142
1143 ret = of_property_read_string(np, propname: "adi,input-clock", out_string: &str);
1144 if (ret < 0)
1145 return ret;
1146
1147 if (!strcmp(str, "1x"))
1148 config->input_clock = ADV7511_INPUT_CLOCK_1X;
1149 else if (!strcmp(str, "2x"))
1150 config->input_clock = ADV7511_INPUT_CLOCK_2X;
1151 else if (!strcmp(str, "ddr"))
1152 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
1153 else
1154 return -EINVAL;
1155
1156 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
1157 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
1158 ret = of_property_read_u32(np, propname: "adi,input-style",
1159 out_value: &config->input_style);
1160 if (ret)
1161 return ret;
1162
1163 if (config->input_style < 1 || config->input_style > 3)
1164 return -EINVAL;
1165
1166 ret = of_property_read_string(np, propname: "adi,input-justification",
1167 out_string: &str);
1168 if (ret < 0)
1169 return ret;
1170
1171 if (!strcmp(str, "left"))
1172 config->input_justification =
1173 ADV7511_INPUT_JUSTIFICATION_LEFT;
1174 else if (!strcmp(str, "evenly"))
1175 config->input_justification =
1176 ADV7511_INPUT_JUSTIFICATION_EVENLY;
1177 else if (!strcmp(str, "right"))
1178 config->input_justification =
1179 ADV7511_INPUT_JUSTIFICATION_RIGHT;
1180 else
1181 return -EINVAL;
1182
1183 } else {
1184 config->input_style = 1;
1185 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
1186 }
1187
1188 of_property_read_u32(np, propname: "adi,clock-delay", out_value: &config->clock_delay);
1189 if (config->clock_delay < -1200 || config->clock_delay > 1600)
1190 return -EINVAL;
1191
1192 config->embedded_sync = of_property_read_bool(np, propname: "adi,embedded-sync");
1193
1194 /* Hardcode the sync pulse configurations for now. */
1195 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1196 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1197 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1198
1199 return 0;
1200}
1201
1202static int adv7511_probe(struct i2c_client *i2c)
1203{
1204 struct adv7511_link_config link_config;
1205 struct adv7511 *adv7511;
1206 struct device *dev = &i2c->dev;
1207 unsigned int val;
1208 int ret;
1209
1210 if (!dev->of_node)
1211 return -EINVAL;
1212
1213 adv7511 = devm_drm_bridge_alloc(dev, struct adv7511, bridge,
1214 &adv7511_bridge_funcs);
1215 if (IS_ERR(ptr: adv7511))
1216 return PTR_ERR(ptr: adv7511);
1217
1218 adv7511->i2c_main = i2c;
1219 adv7511->powered = false;
1220 adv7511->status = connector_status_disconnected;
1221 adv7511->info = i2c_get_match_data(client: i2c);
1222
1223 memset(&link_config, 0, sizeof(link_config));
1224
1225 ret = drm_of_find_panel_or_bridge(np: dev->of_node, port: 1, endpoint: -1, NULL,
1226 bridge: &adv7511->next_bridge);
1227 if (ret && ret != -ENODEV)
1228 return ret;
1229
1230 if (adv7511->info->link_config)
1231 ret = adv7511_parse_dt(np: dev->of_node, config: &link_config);
1232 else
1233 ret = adv7533_parse_dt(np: dev->of_node, adv: adv7511);
1234 if (ret)
1235 return ret;
1236
1237 ret = adv7511_init_regulators(adv: adv7511);
1238 if (ret) {
1239 dev_err_probe(dev, err: ret, fmt: "failed to init regulators\n");
1240 goto err_of_node_put;
1241 }
1242
1243 /*
1244 * The power down GPIO is optional. If present, toggle it from active to
1245 * inactive to wake up the encoder.
1246 */
1247 adv7511->gpio_pd = devm_gpiod_get_optional(dev, con_id: "pd", flags: GPIOD_OUT_HIGH);
1248 if (IS_ERR(ptr: adv7511->gpio_pd)) {
1249 ret = PTR_ERR(ptr: adv7511->gpio_pd);
1250 goto uninit_regulators;
1251 }
1252
1253 if (adv7511->gpio_pd) {
1254 usleep_range(min: 5000, max: 6000);
1255 gpiod_set_value_cansleep(desc: adv7511->gpio_pd, value: 0);
1256 }
1257
1258 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
1259 if (IS_ERR(ptr: adv7511->regmap)) {
1260 ret = PTR_ERR(ptr: adv7511->regmap);
1261 goto uninit_regulators;
1262 }
1263
1264 ret = regmap_read(map: adv7511->regmap, ADV7511_REG_CHIP_REVISION, val: &val);
1265 if (ret)
1266 goto uninit_regulators;
1267 dev_dbg(dev, "Rev. %d\n", val);
1268
1269 if (adv7511->info->type == ADV7511)
1270 ret = regmap_register_patch(map: adv7511->regmap,
1271 regs: adv7511_fixed_registers,
1272 ARRAY_SIZE(adv7511_fixed_registers));
1273 else
1274 ret = adv7533_patch_registers(adv: adv7511);
1275 if (ret)
1276 goto uninit_regulators;
1277
1278 adv7511_packet_disable(adv7511, packet: 0xffff);
1279
1280 adv7511->i2c_edid = i2c_new_ancillary_device(client: i2c, name: "edid",
1281 ADV7511_EDID_I2C_ADDR_DEFAULT);
1282 if (IS_ERR(ptr: adv7511->i2c_edid)) {
1283 ret = PTR_ERR(ptr: adv7511->i2c_edid);
1284 goto uninit_regulators;
1285 }
1286
1287 regmap_write(map: adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
1288 val: adv7511->i2c_edid->addr << 1);
1289
1290 adv7511->i2c_packet = i2c_new_ancillary_device(client: i2c, name: "packet",
1291 ADV7511_PACKET_I2C_ADDR_DEFAULT);
1292 if (IS_ERR(ptr: adv7511->i2c_packet)) {
1293 ret = PTR_ERR(ptr: adv7511->i2c_packet);
1294 goto err_i2c_unregister_edid;
1295 }
1296
1297 adv7511->regmap_packet = devm_regmap_init_i2c(adv7511->i2c_packet,
1298 &adv7511_packet_config);
1299 if (IS_ERR(ptr: adv7511->regmap_packet)) {
1300 ret = PTR_ERR(ptr: adv7511->regmap_packet);
1301 goto err_i2c_unregister_packet;
1302 }
1303
1304 regmap_write(map: adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
1305 val: adv7511->i2c_packet->addr << 1);
1306
1307 ret = adv7511_init_cec_regmap(adv: adv7511);
1308 if (ret)
1309 goto err_i2c_unregister_packet;
1310
1311 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1312
1313 adv7511_power_off(adv7511);
1314
1315 i2c_set_clientdata(client: i2c, data: adv7511);
1316
1317 if (adv7511->info->link_config)
1318 adv7511_set_link_config(adv7511, config: &link_config);
1319
1320 regmap_write(map: adv7511->regmap, ADV7511_REG_CEC_CTRL,
1321 ADV7511_CEC_CTRL_POWER_DOWN);
1322
1323 adv7511->bridge.ops = DRM_BRIDGE_OP_DETECT |
1324 DRM_BRIDGE_OP_EDID |
1325 DRM_BRIDGE_OP_HDMI;
1326 if (adv7511->i2c_main->irq)
1327 adv7511->bridge.ops |= DRM_BRIDGE_OP_HPD;
1328
1329 adv7511->bridge.vendor = "Analog";
1330 adv7511->bridge.product = adv7511->info->name;
1331
1332#ifdef CONFIG_DRM_I2C_ADV7511_AUDIO
1333 adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_AUDIO;
1334 adv7511->bridge.hdmi_audio_dev = dev;
1335 adv7511->bridge.hdmi_audio_max_i2s_playback_channels = 2;
1336 adv7511->bridge.hdmi_audio_i2s_formats = (SNDRV_PCM_FMTBIT_S16_LE |
1337 SNDRV_PCM_FMTBIT_S20_3LE |
1338 SNDRV_PCM_FMTBIT_S24_3LE |
1339 SNDRV_PCM_FMTBIT_S24_LE |
1340 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE),
1341 adv7511->bridge.hdmi_audio_spdif_playback = 1;
1342 adv7511->bridge.hdmi_audio_dai_port = 2;
1343#endif
1344
1345#ifdef CONFIG_DRM_I2C_ADV7511_CEC
1346 adv7511->bridge.ops |= DRM_BRIDGE_OP_HDMI_CEC_ADAPTER;
1347 adv7511->bridge.hdmi_cec_dev = dev;
1348 adv7511->bridge.hdmi_cec_adapter_name = dev_name(dev);
1349 adv7511->bridge.hdmi_cec_available_las = ADV7511_MAX_ADDRS;
1350#endif
1351
1352 adv7511->bridge.of_node = dev->of_node;
1353 adv7511->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1354
1355 drm_bridge_add(bridge: &adv7511->bridge);
1356
1357 if (i2c->irq) {
1358 init_waitqueue_head(&adv7511->wq);
1359
1360 ret = devm_request_threaded_irq(dev, irq: i2c->irq, NULL,
1361 thread_fn: adv7511_irq_handler,
1362 IRQF_ONESHOT | IRQF_SHARED,
1363 devname: dev_name(dev),
1364 dev_id: adv7511);
1365 if (ret)
1366 goto err_unregister_audio;
1367 }
1368
1369 if (adv7511->info->has_dsi) {
1370 ret = adv7533_attach_dsi(adv: adv7511);
1371 if (ret)
1372 goto err_unregister_audio;
1373 }
1374
1375 return 0;
1376
1377err_unregister_audio:
1378 drm_bridge_remove(bridge: &adv7511->bridge);
1379 i2c_unregister_device(client: adv7511->i2c_cec);
1380 clk_disable_unprepare(clk: adv7511->cec_clk);
1381err_i2c_unregister_packet:
1382 i2c_unregister_device(client: adv7511->i2c_packet);
1383err_i2c_unregister_edid:
1384 i2c_unregister_device(client: adv7511->i2c_edid);
1385uninit_regulators:
1386 adv7511_uninit_regulators(adv: adv7511);
1387err_of_node_put:
1388 of_node_put(node: adv7511->host_node);
1389
1390 return ret;
1391}
1392
1393static void adv7511_remove(struct i2c_client *i2c)
1394{
1395 struct adv7511 *adv7511 = i2c_get_clientdata(client: i2c);
1396
1397 of_node_put(node: adv7511->host_node);
1398
1399 adv7511_uninit_regulators(adv: adv7511);
1400
1401 drm_bridge_remove(bridge: &adv7511->bridge);
1402
1403 i2c_unregister_device(client: adv7511->i2c_cec);
1404 clk_disable_unprepare(clk: adv7511->cec_clk);
1405
1406 i2c_unregister_device(client: adv7511->i2c_packet);
1407 i2c_unregister_device(client: adv7511->i2c_edid);
1408}
1409
1410static const struct adv7511_chip_info adv7511_chip_info = {
1411 .type = ADV7511,
1412 .name = "ADV7511",
1413 .max_mode_clock_khz = 165000,
1414 .supply_names = adv7511_supply_names,
1415 .num_supplies = ARRAY_SIZE(adv7511_supply_names),
1416 .link_config = true,
1417};
1418
1419static const struct adv7511_chip_info adv7533_chip_info = {
1420 .type = ADV7533,
1421 .name = "ADV7533",
1422 .max_mode_clock_khz = 80000,
1423 .max_lane_freq_khz = 800000,
1424 .supply_names = adv7533_supply_names,
1425 .num_supplies = ARRAY_SIZE(adv7533_supply_names),
1426 .reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1427 .has_dsi = true,
1428};
1429
1430static const struct adv7511_chip_info adv7535_chip_info = {
1431 .type = ADV7535,
1432 .name = "ADV7535",
1433 .max_mode_clock_khz = 148500,
1434 .max_lane_freq_khz = 891000,
1435 .supply_names = adv7533_supply_names,
1436 .num_supplies = ARRAY_SIZE(adv7533_supply_names),
1437 .reg_cec_offset = ADV7533_REG_CEC_OFFSET,
1438 .has_dsi = true,
1439 .hpd_override_enable = true,
1440};
1441
1442static const struct i2c_device_id adv7511_i2c_ids[] = {
1443 { "adv7511", (kernel_ulong_t)&adv7511_chip_info },
1444 { "adv7511w", (kernel_ulong_t)&adv7511_chip_info },
1445 { "adv7513", (kernel_ulong_t)&adv7511_chip_info },
1446 { "adv7533", (kernel_ulong_t)&adv7533_chip_info },
1447 { "adv7535", (kernel_ulong_t)&adv7535_chip_info },
1448 { }
1449};
1450MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1451
1452static const struct of_device_id adv7511_of_ids[] = {
1453 { .compatible = "adi,adv7511", .data = &adv7511_chip_info },
1454 { .compatible = "adi,adv7511w", .data = &adv7511_chip_info },
1455 { .compatible = "adi,adv7513", .data = &adv7511_chip_info },
1456 { .compatible = "adi,adv7533", .data = &adv7533_chip_info },
1457 { .compatible = "adi,adv7535", .data = &adv7535_chip_info },
1458 { }
1459};
1460MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1461
1462static struct mipi_dsi_driver adv7533_dsi_driver = {
1463 .driver.name = "adv7533",
1464};
1465
1466static struct i2c_driver adv7511_driver = {
1467 .driver = {
1468 .name = "adv7511",
1469 .of_match_table = adv7511_of_ids,
1470 },
1471 .id_table = adv7511_i2c_ids,
1472 .probe = adv7511_probe,
1473 .remove = adv7511_remove,
1474};
1475
1476static int __init adv7511_init(void)
1477{
1478 int ret;
1479
1480 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
1481 ret = mipi_dsi_driver_register(&adv7533_dsi_driver);
1482 if (ret)
1483 return ret;
1484 }
1485
1486 ret = i2c_add_driver(&adv7511_driver);
1487 if (ret) {
1488 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1489 mipi_dsi_driver_unregister(driver: &adv7533_dsi_driver);
1490 }
1491
1492 return ret;
1493}
1494module_init(adv7511_init);
1495
1496static void __exit adv7511_exit(void)
1497{
1498 i2c_del_driver(driver: &adv7511_driver);
1499
1500 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1501 mipi_dsi_driver_unregister(driver: &adv7533_dsi_driver);
1502}
1503module_exit(adv7511_exit);
1504
1505MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1506MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1507MODULE_LICENSE("GPL");
1508

source code of linux/drivers/gpu/drm/bridge/adv7511/adv7511_drv.c