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

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