Commit | Line | Data |
---|---|---|
9c8af882 LPC |
1 | /* |
2 | * Analog Devices ADV7511 HDMI transmitter driver | |
3 | * | |
4 | * Copyright 2012 Analog Devices Inc. | |
5 | * | |
6 | * Licensed under the GPL-2. | |
7 | */ | |
8 | ||
9 | #include <linux/device.h> | |
10 | #include <linux/gpio/consumer.h> | |
9c8af882 | 11 | #include <linux/module.h> |
2437e7cd | 12 | #include <linux/of_device.h> |
9c8af882 LPC |
13 | #include <linux/slab.h> |
14 | ||
15 | #include <drm/drmP.h> | |
e12c2f64 AT |
16 | #include <drm/drm_atomic.h> |
17 | #include <drm/drm_atomic_helper.h> | |
9c8af882 | 18 | #include <drm/drm_edid.h> |
9c8af882 LPC |
19 | |
20 | #include "adv7511.h" | |
21 | ||
9c8af882 | 22 | /* ADI recommended values for proper operation. */ |
8019ff6c | 23 | static const struct reg_sequence adv7511_fixed_registers[] = { |
9c8af882 LPC |
24 | { 0x98, 0x03 }, |
25 | { 0x9a, 0xe0 }, | |
26 | { 0x9c, 0x30 }, | |
27 | { 0x9d, 0x61 }, | |
28 | { 0xa2, 0xa4 }, | |
29 | { 0xa3, 0xa4 }, | |
30 | { 0xe0, 0xd0 }, | |
31 | { 0xf9, 0x00 }, | |
32 | { 0x55, 0x02 }, | |
33 | }; | |
34 | ||
35 | /* ----------------------------------------------------------------------------- | |
36 | * Register access | |
37 | */ | |
38 | ||
39 | static const uint8_t adv7511_register_defaults[] = { | |
40 | 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00 */ | |
41 | 0x00, 0x00, 0x01, 0x0e, 0xbc, 0x18, 0x01, 0x13, | |
42 | 0x25, 0x37, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 10 */ | |
43 | 0x46, 0x62, 0x04, 0xa8, 0x00, 0x00, 0x1c, 0x84, | |
44 | 0x1c, 0xbf, 0x04, 0xa8, 0x1e, 0x70, 0x02, 0x1e, /* 20 */ | |
45 | 0x00, 0x00, 0x04, 0xa8, 0x08, 0x12, 0x1b, 0xac, | |
46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 30 */ | |
47 | 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xb0, | |
48 | 0x00, 0x50, 0x90, 0x7e, 0x79, 0x70, 0x00, 0x00, /* 40 */ | |
49 | 0x00, 0xa8, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, | |
50 | 0x00, 0x00, 0x02, 0x0d, 0x00, 0x00, 0x00, 0x00, /* 50 */ | |
51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 60 */ | |
53 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
54 | 0x01, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 70 */ | |
55 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 80 */ | |
57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
58 | 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, /* 90 */ | |
59 | 0x0b, 0x02, 0x00, 0x18, 0x5a, 0x60, 0x00, 0x00, | |
60 | 0x00, 0x00, 0x80, 0x80, 0x08, 0x04, 0x00, 0x00, /* a0 */ | |
61 | 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x14, | |
62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b0 */ | |
63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c0 */ | |
65 | 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x01, 0x04, | |
66 | 0x30, 0xff, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, /* d0 */ | |
67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x01, | |
68 | 0x80, 0x75, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, /* e0 */ | |
69 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
70 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x11, 0x00, /* f0 */ | |
71 | 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
72 | }; | |
73 | ||
74 | static bool adv7511_register_volatile(struct device *dev, unsigned int reg) | |
75 | { | |
76 | switch (reg) { | |
77 | case ADV7511_REG_CHIP_REVISION: | |
78 | case ADV7511_REG_SPDIF_FREQ: | |
79 | case ADV7511_REG_CTS_AUTOMATIC1: | |
80 | case ADV7511_REG_CTS_AUTOMATIC2: | |
81 | case ADV7511_REG_VIC_DETECTED: | |
82 | case ADV7511_REG_VIC_SEND: | |
83 | case ADV7511_REG_AUX_VIC_DETECTED: | |
84 | case ADV7511_REG_STATUS: | |
85 | case ADV7511_REG_GC(1): | |
86 | case ADV7511_REG_INT(0): | |
87 | case ADV7511_REG_INT(1): | |
88 | case ADV7511_REG_PLL_STATUS: | |
89 | case ADV7511_REG_AN(0): | |
90 | case ADV7511_REG_AN(1): | |
91 | case ADV7511_REG_AN(2): | |
92 | case ADV7511_REG_AN(3): | |
93 | case ADV7511_REG_AN(4): | |
94 | case ADV7511_REG_AN(5): | |
95 | case ADV7511_REG_AN(6): | |
96 | case ADV7511_REG_AN(7): | |
97 | case ADV7511_REG_HDCP_STATUS: | |
98 | case ADV7511_REG_BCAPS: | |
99 | case ADV7511_REG_BKSV(0): | |
100 | case ADV7511_REG_BKSV(1): | |
101 | case ADV7511_REG_BKSV(2): | |
102 | case ADV7511_REG_BKSV(3): | |
103 | case ADV7511_REG_BKSV(4): | |
104 | case ADV7511_REG_DDC_STATUS: | |
cd38a337 | 105 | case ADV7511_REG_EDID_READ_CTRL: |
9c8af882 LPC |
106 | case ADV7511_REG_BSTATUS(0): |
107 | case ADV7511_REG_BSTATUS(1): | |
108 | case ADV7511_REG_CHIP_ID_HIGH: | |
109 | case ADV7511_REG_CHIP_ID_LOW: | |
110 | return true; | |
111 | } | |
112 | ||
113 | return false; | |
114 | } | |
115 | ||
116 | static const struct regmap_config adv7511_regmap_config = { | |
117 | .reg_bits = 8, | |
118 | .val_bits = 8, | |
119 | ||
120 | .max_register = 0xff, | |
121 | .cache_type = REGCACHE_RBTREE, | |
122 | .reg_defaults_raw = adv7511_register_defaults, | |
123 | .num_reg_defaults_raw = ARRAY_SIZE(adv7511_register_defaults), | |
124 | ||
125 | .volatile_reg = adv7511_register_volatile, | |
126 | }; | |
127 | ||
128 | /* ----------------------------------------------------------------------------- | |
129 | * Hardware configuration | |
130 | */ | |
131 | ||
132 | static void adv7511_set_colormap(struct adv7511 *adv7511, bool enable, | |
133 | const uint16_t *coeff, | |
134 | unsigned int scaling_factor) | |
135 | { | |
136 | unsigned int i; | |
137 | ||
138 | regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1), | |
139 | ADV7511_CSC_UPDATE_MODE, ADV7511_CSC_UPDATE_MODE); | |
140 | ||
141 | if (enable) { | |
142 | for (i = 0; i < 12; ++i) { | |
143 | regmap_update_bits(adv7511->regmap, | |
144 | ADV7511_REG_CSC_UPPER(i), | |
145 | 0x1f, coeff[i] >> 8); | |
146 | regmap_write(adv7511->regmap, | |
147 | ADV7511_REG_CSC_LOWER(i), | |
148 | coeff[i] & 0xff); | |
149 | } | |
150 | } | |
151 | ||
152 | if (enable) | |
153 | regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0), | |
154 | 0xe0, 0x80 | (scaling_factor << 5)); | |
155 | else | |
156 | regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(0), | |
157 | 0x80, 0x00); | |
158 | ||
159 | regmap_update_bits(adv7511->regmap, ADV7511_REG_CSC_UPPER(1), | |
160 | ADV7511_CSC_UPDATE_MODE, 0); | |
161 | } | |
162 | ||
163 | static int adv7511_packet_enable(struct adv7511 *adv7511, unsigned int packet) | |
164 | { | |
165 | if (packet & 0xff) | |
166 | regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0, | |
167 | packet, 0xff); | |
168 | ||
169 | if (packet & 0xff00) { | |
170 | packet >>= 8; | |
171 | regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1, | |
172 | packet, 0xff); | |
173 | } | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | static int adv7511_packet_disable(struct adv7511 *adv7511, unsigned int packet) | |
179 | { | |
180 | if (packet & 0xff) | |
181 | regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE0, | |
182 | packet, 0x00); | |
183 | ||
184 | if (packet & 0xff00) { | |
185 | packet >>= 8; | |
186 | regmap_update_bits(adv7511->regmap, ADV7511_REG_PACKET_ENABLE1, | |
187 | packet, 0x00); | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | /* Coefficients for adv7511 color space conversion */ | |
194 | static const uint16_t adv7511_csc_ycbcr_to_rgb[] = { | |
195 | 0x0734, 0x04ad, 0x0000, 0x1c1b, | |
196 | 0x1ddc, 0x04ad, 0x1f24, 0x0135, | |
197 | 0x0000, 0x04ad, 0x087c, 0x1b77, | |
198 | }; | |
199 | ||
200 | static void adv7511_set_config_csc(struct adv7511 *adv7511, | |
201 | struct drm_connector *connector, | |
202 | bool rgb) | |
203 | { | |
204 | struct adv7511_video_config config; | |
205 | bool output_format_422, output_format_ycbcr; | |
206 | unsigned int mode; | |
207 | uint8_t infoframe[17]; | |
208 | ||
209 | if (adv7511->edid) | |
210 | config.hdmi_mode = drm_detect_hdmi_monitor(adv7511->edid); | |
211 | else | |
212 | config.hdmi_mode = false; | |
213 | ||
214 | hdmi_avi_infoframe_init(&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_YCRCB422) && | |
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, config.csc_enable, | |
263 | config.csc_coefficents, | |
264 | config.csc_scaling_factor); | |
265 | ||
266 | regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x81, | |
267 | (output_format_422 << 7) | output_format_ycbcr); | |
268 | ||
269 | regmap_update_bits(adv7511->regmap, ADV7511_REG_HDCP_HDMI_CFG, | |
270 | ADV7511_HDMI_CFG_MODE_MASK, mode); | |
271 | ||
272 | hdmi_avi_infoframe_pack(&config.avi_infoframe, infoframe, | |
273 | sizeof(infoframe)); | |
274 | ||
275 | /* The AVI infoframe id is not configurable */ | |
276 | regmap_bulk_write(adv7511->regmap, ADV7511_REG_AVI_INFOFRAME_VERSION, | |
277 | infoframe + 1, sizeof(infoframe) - 1); | |
278 | ||
279 | adv7511_packet_enable(adv7511, ADV7511_PACKET_ENABLE_AVI_INFOFRAME); | |
280 | } | |
281 | ||
282 | static 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(adv7511->regmap, ADV7511_REG_I2C_FREQ_ID_CFG, 0xf, | |
311 | input_id); | |
312 | regmap_update_bits(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG1, 0x7e, | |
313 | (color_depth << 4) | | |
314 | (input_styles[config->input_style] << 2)); | |
315 | regmap_write(adv7511->regmap, ADV7511_REG_VIDEO_INPUT_CFG2, | |
316 | config->input_justification << 3); | |
317 | regmap_write(adv7511->regmap, ADV7511_REG_TIMING_GEN_SEQ, | |
318 | config->sync_pulse << 2); | |
319 | ||
320 | regmap_write(adv7511->regmap, 0xba, 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 | ||
c6169e49 LP |
328 | static void adv7511_power_on(struct adv7511 *adv7511) |
329 | { | |
330 | adv7511->current_edid_segment = -1; | |
331 | ||
c6169e49 LP |
332 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, |
333 | ADV7511_POWER_POWER_DOWN, 0); | |
d0be8584 WS |
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(adv7511->regmap, ADV7511_REG_INT_ENABLE(0), | |
341 | ADV7511_INT0_EDID_READY); | |
342 | regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1), | |
343 | ADV7511_INT1_DDC_ERROR); | |
344 | } | |
c6169e49 LP |
345 | |
346 | /* | |
29ce4ed4 | 347 | * Per spec it is allowed to pulse the HPD signal to indicate that the |
c6169e49 | 348 | * EDID information has changed. Some monitors do this when they wakeup |
29ce4ed4 | 349 | * from standby or are enabled. When the HPD goes low the adv7511 is |
c6169e49 | 350 | * reset and the outputs are disabled which might cause the monitor to |
29ce4ed4 | 351 | * go to standby again. To avoid this we ignore the HPD pin for the |
c6169e49 LP |
352 | * first few seconds after enabling the output. |
353 | */ | |
354 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, | |
29ce4ed4 WS |
355 | ADV7511_REG_POWER2_HPD_SRC_MASK, |
356 | ADV7511_REG_POWER2_HPD_SRC_NONE); | |
c6169e49 LP |
357 | |
358 | /* | |
359 | * Most of the registers are reset during power down or when HPD is low. | |
360 | */ | |
361 | regcache_sync(adv7511->regmap); | |
362 | ||
2437e7cd AT |
363 | if (adv7511->type == ADV7533) |
364 | adv7533_dsi_power_on(adv7511); | |
365 | ||
c6169e49 LP |
366 | adv7511->powered = true; |
367 | } | |
368 | ||
369 | static void adv7511_power_off(struct adv7511 *adv7511) | |
370 | { | |
371 | /* TODO: setup additional power down modes */ | |
372 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, | |
373 | ADV7511_POWER_POWER_DOWN, | |
374 | ADV7511_POWER_POWER_DOWN); | |
375 | regcache_mark_dirty(adv7511->regmap); | |
376 | ||
2437e7cd AT |
377 | if (adv7511->type == ADV7533) |
378 | adv7533_dsi_power_off(adv7511); | |
379 | ||
c6169e49 LP |
380 | adv7511->powered = false; |
381 | } | |
382 | ||
9c8af882 LPC |
383 | /* ----------------------------------------------------------------------------- |
384 | * Interrupt and hotplug detection | |
385 | */ | |
386 | ||
387 | static bool adv7511_hpd(struct adv7511 *adv7511) | |
388 | { | |
389 | unsigned int irq0; | |
390 | int ret; | |
391 | ||
392 | ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0); | |
393 | if (ret < 0) | |
394 | return false; | |
395 | ||
29ce4ed4 | 396 | if (irq0 & ADV7511_INT0_HPD) { |
9c8af882 | 397 | regmap_write(adv7511->regmap, ADV7511_REG_INT(0), |
29ce4ed4 | 398 | ADV7511_INT0_HPD); |
9c8af882 LPC |
399 | return true; |
400 | } | |
401 | ||
402 | return false; | |
403 | } | |
404 | ||
518cb705 JS |
405 | static void adv7511_hpd_work(struct work_struct *work) |
406 | { | |
407 | struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work); | |
408 | ||
409 | drm_helper_hpd_irq_event(adv7511->connector.dev); | |
410 | } | |
411 | ||
f0bfcc22 | 412 | static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd) |
9c8af882 LPC |
413 | { |
414 | unsigned int irq0, irq1; | |
9c8af882 LPC |
415 | int ret; |
416 | ||
417 | ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0); | |
418 | if (ret < 0) | |
a5241289 LP |
419 | return ret; |
420 | ||
9c8af882 LPC |
421 | ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1); |
422 | if (ret < 0) | |
a5241289 LP |
423 | return ret; |
424 | ||
425 | regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0); | |
426 | regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1); | |
9c8af882 | 427 | |
f0bfcc22 | 428 | if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder) |
518cb705 | 429 | schedule_work(&adv7511->hpd_work); |
a5241289 LP |
430 | |
431 | if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) { | |
432 | adv7511->edid_read = true; | |
433 | ||
434 | if (adv7511->i2c_main->irq) | |
435 | wake_up_all(&adv7511->wq); | |
436 | } | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | static irqreturn_t adv7511_irq_handler(int irq, void *devid) | |
442 | { | |
443 | struct adv7511 *adv7511 = devid; | |
444 | int ret; | |
9c8af882 | 445 | |
f0bfcc22 | 446 | ret = adv7511_irq_process(adv7511, true); |
a5241289 | 447 | return ret < 0 ? IRQ_NONE : IRQ_HANDLED; |
9c8af882 LPC |
448 | } |
449 | ||
a5241289 LP |
450 | /* ----------------------------------------------------------------------------- |
451 | * EDID retrieval | |
452 | */ | |
453 | ||
454 | static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout) | |
9c8af882 | 455 | { |
9c8af882 LPC |
456 | int ret; |
457 | ||
458 | if (adv7511->i2c_main->irq) { | |
459 | ret = wait_event_interruptible_timeout(adv7511->wq, | |
a5241289 | 460 | adv7511->edid_read, msecs_to_jiffies(timeout)); |
9c8af882 | 461 | } else { |
a5241289 | 462 | for (; timeout > 0; timeout -= 25) { |
f0bfcc22 | 463 | ret = adv7511_irq_process(adv7511, false); |
a5241289 LP |
464 | if (ret < 0) |
465 | break; | |
466 | ||
467 | if (adv7511->edid_read) | |
9c8af882 | 468 | break; |
a5241289 | 469 | |
9c8af882 | 470 | msleep(25); |
a5241289 | 471 | } |
9c8af882 LPC |
472 | } |
473 | ||
a5241289 | 474 | return adv7511->edid_read ? 0 : -EIO; |
9c8af882 LPC |
475 | } |
476 | ||
9c8af882 LPC |
477 | static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block, |
478 | size_t len) | |
479 | { | |
480 | struct adv7511 *adv7511 = data; | |
481 | struct i2c_msg xfer[2]; | |
482 | uint8_t offset; | |
483 | unsigned int i; | |
484 | int ret; | |
485 | ||
486 | if (len > 128) | |
487 | return -EINVAL; | |
488 | ||
489 | if (adv7511->current_edid_segment != block / 2) { | |
490 | unsigned int status; | |
491 | ||
492 | ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS, | |
493 | &status); | |
494 | if (ret < 0) | |
495 | return ret; | |
496 | ||
497 | if (status != 2) { | |
a5241289 | 498 | adv7511->edid_read = false; |
9c8af882 LPC |
499 | regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT, |
500 | block); | |
a5241289 LP |
501 | ret = adv7511_wait_for_edid(adv7511, 200); |
502 | if (ret < 0) | |
503 | return ret; | |
9c8af882 LPC |
504 | } |
505 | ||
9c8af882 LPC |
506 | /* Break this apart, hopefully more I2C controllers will |
507 | * support 64 byte transfers than 256 byte transfers | |
508 | */ | |
509 | ||
510 | xfer[0].addr = adv7511->i2c_edid->addr; | |
511 | xfer[0].flags = 0; | |
512 | xfer[0].len = 1; | |
513 | xfer[0].buf = &offset; | |
514 | xfer[1].addr = adv7511->i2c_edid->addr; | |
515 | xfer[1].flags = I2C_M_RD; | |
516 | xfer[1].len = 64; | |
517 | xfer[1].buf = adv7511->edid_buf; | |
518 | ||
519 | offset = 0; | |
520 | ||
521 | for (i = 0; i < 4; ++i) { | |
522 | ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer, | |
523 | ARRAY_SIZE(xfer)); | |
524 | if (ret < 0) | |
525 | return ret; | |
526 | else if (ret != 2) | |
527 | return -EIO; | |
528 | ||
529 | xfer[1].buf += 64; | |
530 | offset += 64; | |
531 | } | |
532 | ||
533 | adv7511->current_edid_segment = block / 2; | |
534 | } | |
535 | ||
536 | if (block % 2 == 0) | |
537 | memcpy(buf, adv7511->edid_buf, len); | |
538 | else | |
539 | memcpy(buf, adv7511->edid_buf + 128, len); | |
540 | ||
541 | return 0; | |
542 | } | |
543 | ||
544 | /* ----------------------------------------------------------------------------- | |
e12c2f64 | 545 | * ADV75xx helpers |
9c8af882 LPC |
546 | */ |
547 | ||
e12c2f64 | 548 | static int adv7511_get_modes(struct adv7511 *adv7511, |
9c8af882 LPC |
549 | struct drm_connector *connector) |
550 | { | |
9c8af882 LPC |
551 | struct edid *edid; |
552 | unsigned int count; | |
553 | ||
554 | /* Reading the EDID only works if the device is powered */ | |
c6169e49 | 555 | if (!adv7511->powered) { |
9c8af882 LPC |
556 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, |
557 | ADV7511_POWER_POWER_DOWN, 0); | |
d0be8584 WS |
558 | if (adv7511->i2c_main->irq) { |
559 | regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0), | |
560 | ADV7511_INT0_EDID_READY); | |
561 | regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1), | |
562 | ADV7511_INT1_DDC_ERROR); | |
563 | } | |
9c8af882 LPC |
564 | adv7511->current_edid_segment = -1; |
565 | } | |
566 | ||
567 | edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511); | |
568 | ||
c6169e49 | 569 | if (!adv7511->powered) |
9c8af882 LPC |
570 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER, |
571 | ADV7511_POWER_POWER_DOWN, | |
572 | ADV7511_POWER_POWER_DOWN); | |
573 | ||
574 | kfree(adv7511->edid); | |
575 | adv7511->edid = edid; | |
576 | if (!edid) | |
577 | return 0; | |
578 | ||
579 | drm_mode_connector_update_edid_property(connector, edid); | |
580 | count = drm_add_edid_modes(connector, edid); | |
581 | ||
582 | adv7511_set_config_csc(adv7511, connector, adv7511->rgb); | |
583 | ||
584 | return count; | |
585 | } | |
586 | ||
9c8af882 | 587 | static enum drm_connector_status |
e12c2f64 | 588 | adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector) |
9c8af882 | 589 | { |
9c8af882 LPC |
590 | enum drm_connector_status status; |
591 | unsigned int val; | |
592 | bool hpd; | |
593 | int ret; | |
594 | ||
595 | ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val); | |
596 | if (ret < 0) | |
597 | return connector_status_disconnected; | |
598 | ||
599 | if (val & ADV7511_STATUS_HPD) | |
600 | status = connector_status_connected; | |
601 | else | |
602 | status = connector_status_disconnected; | |
603 | ||
604 | hpd = adv7511_hpd(adv7511); | |
605 | ||
606 | /* The chip resets itself when the cable is disconnected, so in case | |
607 | * there is a pending HPD interrupt and the cable is connected there was | |
608 | * at least one transition from disconnected to connected and the chip | |
609 | * has to be reinitialized. */ | |
c6169e49 | 610 | if (status == connector_status_connected && hpd && adv7511->powered) { |
9c8af882 | 611 | regcache_mark_dirty(adv7511->regmap); |
c6169e49 | 612 | adv7511_power_on(adv7511); |
e12c2f64 | 613 | adv7511_get_modes(adv7511, connector); |
9c8af882 LPC |
614 | if (adv7511->status == connector_status_connected) |
615 | status = connector_status_disconnected; | |
616 | } else { | |
29ce4ed4 | 617 | /* Renable HPD sensing */ |
9c8af882 | 618 | regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2, |
29ce4ed4 WS |
619 | ADV7511_REG_POWER2_HPD_SRC_MASK, |
620 | ADV7511_REG_POWER2_HPD_SRC_BOTH); | |
9c8af882 LPC |
621 | } |
622 | ||
623 | adv7511->status = status; | |
624 | return status; | |
625 | } | |
626 | ||
e12c2f64 AT |
627 | static int adv7511_mode_valid(struct adv7511 *adv7511, |
628 | struct drm_display_mode *mode) | |
9c8af882 LPC |
629 | { |
630 | if (mode->clock > 165000) | |
631 | return MODE_CLOCK_HIGH; | |
632 | ||
9c8af882 LPC |
633 | return MODE_OK; |
634 | } | |
635 | ||
e12c2f64 AT |
636 | static void adv7511_mode_set(struct adv7511 *adv7511, |
637 | struct drm_display_mode *mode, | |
638 | struct drm_display_mode *adj_mode) | |
9c8af882 | 639 | { |
9c8af882 LPC |
640 | unsigned int low_refresh_rate; |
641 | unsigned int hsync_polarity = 0; | |
642 | unsigned int vsync_polarity = 0; | |
643 | ||
644 | if (adv7511->embedded_sync) { | |
645 | unsigned int hsync_offset, hsync_len; | |
646 | unsigned int vsync_offset, vsync_len; | |
647 | ||
648 | hsync_offset = adj_mode->crtc_hsync_start - | |
649 | adj_mode->crtc_hdisplay; | |
650 | vsync_offset = adj_mode->crtc_vsync_start - | |
651 | adj_mode->crtc_vdisplay; | |
652 | hsync_len = adj_mode->crtc_hsync_end - | |
653 | adj_mode->crtc_hsync_start; | |
654 | vsync_len = adj_mode->crtc_vsync_end - | |
655 | adj_mode->crtc_vsync_start; | |
656 | ||
657 | /* The hardware vsync generator has a off-by-one bug */ | |
658 | vsync_offset += 1; | |
659 | ||
660 | regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB, | |
661 | ((hsync_offset >> 10) & 0x7) << 5); | |
662 | regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0), | |
663 | (hsync_offset >> 2) & 0xff); | |
664 | regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1), | |
665 | ((hsync_offset & 0x3) << 6) | | |
666 | ((hsync_len >> 4) & 0x3f)); | |
667 | regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2), | |
668 | ((hsync_len & 0xf) << 4) | | |
669 | ((vsync_offset >> 6) & 0xf)); | |
670 | regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3), | |
671 | ((vsync_offset & 0x3f) << 2) | | |
672 | ((vsync_len >> 8) & 0x3)); | |
673 | regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4), | |
674 | vsync_len & 0xff); | |
675 | ||
676 | hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC); | |
677 | vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC); | |
678 | } else { | |
679 | enum adv7511_sync_polarity mode_hsync_polarity; | |
680 | enum adv7511_sync_polarity mode_vsync_polarity; | |
681 | ||
682 | /** | |
683 | * If the input signal is always low or always high we want to | |
684 | * invert or let it passthrough depending on the polarity of the | |
685 | * current mode. | |
686 | **/ | |
687 | if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC) | |
688 | mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW; | |
689 | else | |
690 | mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH; | |
691 | ||
692 | if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC) | |
693 | mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW; | |
694 | else | |
695 | mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH; | |
696 | ||
697 | if (adv7511->hsync_polarity != mode_hsync_polarity && | |
698 | adv7511->hsync_polarity != | |
699 | ADV7511_SYNC_POLARITY_PASSTHROUGH) | |
700 | hsync_polarity = 1; | |
701 | ||
702 | if (adv7511->vsync_polarity != mode_vsync_polarity && | |
703 | adv7511->vsync_polarity != | |
704 | ADV7511_SYNC_POLARITY_PASSTHROUGH) | |
705 | vsync_polarity = 1; | |
706 | } | |
707 | ||
708 | if (mode->vrefresh <= 24000) | |
709 | low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ; | |
710 | else if (mode->vrefresh <= 25000) | |
711 | low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ; | |
712 | else if (mode->vrefresh <= 30000) | |
713 | low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ; | |
714 | else | |
715 | low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE; | |
716 | ||
717 | regmap_update_bits(adv7511->regmap, 0xfb, | |
718 | 0x6, low_refresh_rate << 1); | |
719 | regmap_update_bits(adv7511->regmap, 0x17, | |
720 | 0x60, (vsync_polarity << 6) | (hsync_polarity << 5)); | |
721 | ||
62b2f026 AT |
722 | if (adv7511->type == ADV7533) |
723 | adv7533_mode_set(adv7511, adj_mode); | |
724 | ||
78fa479d AT |
725 | drm_mode_copy(&adv7511->curr_mode, adj_mode); |
726 | ||
9c8af882 LPC |
727 | /* |
728 | * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is | |
729 | * supposed to give better results. | |
730 | */ | |
731 | ||
732 | adv7511->f_tmds = mode->clock; | |
733 | } | |
734 | ||
e12c2f64 AT |
735 | /* Connector funcs */ |
736 | static struct adv7511 *connector_to_adv7511(struct drm_connector *connector) | |
737 | { | |
738 | return container_of(connector, struct adv7511, connector); | |
739 | } | |
740 | ||
741 | static int adv7511_connector_get_modes(struct drm_connector *connector) | |
742 | { | |
743 | struct adv7511 *adv = connector_to_adv7511(connector); | |
744 | ||
745 | return adv7511_get_modes(adv, connector); | |
746 | } | |
747 | ||
748 | static enum drm_mode_status | |
749 | adv7511_connector_mode_valid(struct drm_connector *connector, | |
750 | struct drm_display_mode *mode) | |
751 | { | |
752 | struct adv7511 *adv = connector_to_adv7511(connector); | |
753 | ||
754 | return adv7511_mode_valid(adv, mode); | |
755 | } | |
756 | ||
757 | static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = { | |
758 | .get_modes = adv7511_connector_get_modes, | |
759 | .mode_valid = adv7511_connector_mode_valid, | |
760 | }; | |
761 | ||
762 | static enum drm_connector_status | |
763 | adv7511_connector_detect(struct drm_connector *connector, bool force) | |
764 | { | |
765 | struct adv7511 *adv = connector_to_adv7511(connector); | |
766 | ||
767 | return adv7511_detect(adv, connector); | |
768 | } | |
769 | ||
770 | static struct drm_connector_funcs adv7511_connector_funcs = { | |
771 | .dpms = drm_atomic_helper_connector_dpms, | |
772 | .fill_modes = drm_helper_probe_single_connector_modes, | |
773 | .detect = adv7511_connector_detect, | |
774 | .destroy = drm_connector_cleanup, | |
775 | .reset = drm_atomic_helper_connector_reset, | |
776 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, | |
777 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | |
778 | }; | |
779 | ||
780 | /* Bridge funcs */ | |
781 | static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge) | |
782 | { | |
783 | return container_of(bridge, struct adv7511, bridge); | |
784 | } | |
785 | ||
786 | static void adv7511_bridge_enable(struct drm_bridge *bridge) | |
787 | { | |
788 | struct adv7511 *adv = bridge_to_adv7511(bridge); | |
789 | ||
790 | adv7511_power_on(adv); | |
791 | } | |
792 | ||
793 | static void adv7511_bridge_disable(struct drm_bridge *bridge) | |
794 | { | |
795 | struct adv7511 *adv = bridge_to_adv7511(bridge); | |
796 | ||
797 | adv7511_power_off(adv); | |
798 | } | |
799 | ||
800 | static void adv7511_bridge_mode_set(struct drm_bridge *bridge, | |
801 | struct drm_display_mode *mode, | |
802 | struct drm_display_mode *adj_mode) | |
803 | { | |
804 | struct adv7511 *adv = bridge_to_adv7511(bridge); | |
805 | ||
806 | adv7511_mode_set(adv, mode, adj_mode); | |
807 | } | |
808 | ||
809 | static int adv7511_bridge_attach(struct drm_bridge *bridge) | |
810 | { | |
811 | struct adv7511 *adv = bridge_to_adv7511(bridge); | |
812 | int ret; | |
813 | ||
814 | if (!bridge->encoder) { | |
815 | DRM_ERROR("Parent encoder object not found"); | |
816 | return -ENODEV; | |
817 | } | |
818 | ||
819 | adv->connector.polled = DRM_CONNECTOR_POLL_HPD; | |
820 | ||
821 | ret = drm_connector_init(bridge->dev, &adv->connector, | |
822 | &adv7511_connector_funcs, | |
823 | DRM_MODE_CONNECTOR_HDMIA); | |
824 | if (ret) { | |
825 | DRM_ERROR("Failed to initialize connector with drm\n"); | |
826 | return ret; | |
827 | } | |
828 | drm_connector_helper_add(&adv->connector, | |
829 | &adv7511_connector_helper_funcs); | |
830 | drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder); | |
831 | ||
1e4d58cd AT |
832 | if (adv->type == ADV7533) |
833 | ret = adv7533_attach_dsi(adv); | |
834 | ||
e12c2f64 AT |
835 | return ret; |
836 | } | |
837 | ||
838 | static struct drm_bridge_funcs adv7511_bridge_funcs = { | |
839 | .enable = adv7511_bridge_enable, | |
840 | .disable = adv7511_bridge_disable, | |
841 | .mode_set = adv7511_bridge_mode_set, | |
842 | .attach = adv7511_bridge_attach, | |
9c8af882 LPC |
843 | }; |
844 | ||
845 | /* ----------------------------------------------------------------------------- | |
846 | * Probe & remove | |
847 | */ | |
848 | ||
5b06ba23 AT |
849 | static const char * const adv7511_supply_names[] = { |
850 | "avdd", | |
851 | "dvdd", | |
852 | "pvdd", | |
853 | "bgvdd", | |
854 | "dvdd-3v", | |
855 | }; | |
856 | ||
857 | static const char * const adv7533_supply_names[] = { | |
858 | "avdd", | |
859 | "dvdd", | |
860 | "pvdd", | |
861 | "a2vdd", | |
862 | "v3p3", | |
863 | "v1p2", | |
864 | }; | |
865 | ||
866 | static int adv7511_init_regulators(struct adv7511 *adv) | |
867 | { | |
868 | struct device *dev = &adv->i2c_main->dev; | |
869 | const char * const *supply_names; | |
870 | unsigned int i; | |
871 | int ret; | |
872 | ||
873 | if (adv->type == ADV7511) { | |
874 | supply_names = adv7511_supply_names; | |
875 | adv->num_supplies = ARRAY_SIZE(adv7511_supply_names); | |
876 | } else { | |
877 | supply_names = adv7533_supply_names; | |
878 | adv->num_supplies = ARRAY_SIZE(adv7533_supply_names); | |
879 | } | |
880 | ||
881 | adv->supplies = devm_kcalloc(dev, adv->num_supplies, | |
882 | sizeof(*adv->supplies), GFP_KERNEL); | |
883 | if (!adv->supplies) | |
884 | return -ENOMEM; | |
885 | ||
886 | for (i = 0; i < adv->num_supplies; i++) | |
887 | adv->supplies[i].supply = supply_names[i]; | |
888 | ||
889 | ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies); | |
890 | if (ret) | |
891 | return ret; | |
892 | ||
893 | return regulator_bulk_enable(adv->num_supplies, adv->supplies); | |
894 | } | |
895 | ||
896 | static void adv7511_uninit_regulators(struct adv7511 *adv) | |
897 | { | |
898 | regulator_bulk_disable(adv->num_supplies, adv->supplies); | |
899 | } | |
900 | ||
9c8af882 LPC |
901 | static int adv7511_parse_dt(struct device_node *np, |
902 | struct adv7511_link_config *config) | |
903 | { | |
904 | const char *str; | |
905 | int ret; | |
906 | ||
9c8af882 LPC |
907 | of_property_read_u32(np, "adi,input-depth", &config->input_color_depth); |
908 | if (config->input_color_depth != 8 && config->input_color_depth != 10 && | |
909 | config->input_color_depth != 12) | |
910 | return -EINVAL; | |
911 | ||
912 | ret = of_property_read_string(np, "adi,input-colorspace", &str); | |
913 | if (ret < 0) | |
914 | return ret; | |
915 | ||
916 | if (!strcmp(str, "rgb")) | |
917 | config->input_colorspace = HDMI_COLORSPACE_RGB; | |
918 | else if (!strcmp(str, "yuv422")) | |
919 | config->input_colorspace = HDMI_COLORSPACE_YUV422; | |
920 | else if (!strcmp(str, "yuv444")) | |
921 | config->input_colorspace = HDMI_COLORSPACE_YUV444; | |
922 | else | |
923 | return -EINVAL; | |
924 | ||
925 | ret = of_property_read_string(np, "adi,input-clock", &str); | |
926 | if (ret < 0) | |
927 | return ret; | |
928 | ||
929 | if (!strcmp(str, "1x")) | |
930 | config->input_clock = ADV7511_INPUT_CLOCK_1X; | |
931 | else if (!strcmp(str, "2x")) | |
932 | config->input_clock = ADV7511_INPUT_CLOCK_2X; | |
933 | else if (!strcmp(str, "ddr")) | |
934 | config->input_clock = ADV7511_INPUT_CLOCK_DDR; | |
935 | else | |
936 | return -EINVAL; | |
937 | ||
938 | if (config->input_colorspace == HDMI_COLORSPACE_YUV422 || | |
939 | config->input_clock != ADV7511_INPUT_CLOCK_1X) { | |
940 | ret = of_property_read_u32(np, "adi,input-style", | |
941 | &config->input_style); | |
942 | if (ret) | |
943 | return ret; | |
944 | ||
945 | if (config->input_style < 1 || config->input_style > 3) | |
946 | return -EINVAL; | |
947 | ||
948 | ret = of_property_read_string(np, "adi,input-justification", | |
949 | &str); | |
950 | if (ret < 0) | |
951 | return ret; | |
952 | ||
953 | if (!strcmp(str, "left")) | |
954 | config->input_justification = | |
955 | ADV7511_INPUT_JUSTIFICATION_LEFT; | |
956 | else if (!strcmp(str, "evenly")) | |
957 | config->input_justification = | |
958 | ADV7511_INPUT_JUSTIFICATION_EVENLY; | |
959 | else if (!strcmp(str, "right")) | |
960 | config->input_justification = | |
961 | ADV7511_INPUT_JUSTIFICATION_RIGHT; | |
962 | else | |
963 | return -EINVAL; | |
964 | ||
965 | } else { | |
966 | config->input_style = 1; | |
967 | config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT; | |
968 | } | |
969 | ||
970 | of_property_read_u32(np, "adi,clock-delay", &config->clock_delay); | |
971 | if (config->clock_delay < -1200 || config->clock_delay > 1600) | |
972 | return -EINVAL; | |
973 | ||
974 | config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync"); | |
975 | ||
976 | /* Hardcode the sync pulse configurations for now. */ | |
977 | config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE; | |
978 | config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH; | |
979 | config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH; | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
9c8af882 LPC |
984 | static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id) |
985 | { | |
986 | struct adv7511_link_config link_config; | |
987 | struct adv7511 *adv7511; | |
988 | struct device *dev = &i2c->dev; | |
d25a4cbb SS |
989 | unsigned int main_i2c_addr = i2c->addr << 1; |
990 | unsigned int edid_i2c_addr = main_i2c_addr + 4; | |
9c8af882 LPC |
991 | unsigned int val; |
992 | int ret; | |
993 | ||
994 | if (!dev->of_node) | |
995 | return -EINVAL; | |
996 | ||
997 | adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL); | |
998 | if (!adv7511) | |
999 | return -ENOMEM; | |
1000 | ||
5b06ba23 | 1001 | adv7511->i2c_main = i2c; |
c6169e49 | 1002 | adv7511->powered = false; |
9c8af882 LPC |
1003 | adv7511->status = connector_status_disconnected; |
1004 | ||
2437e7cd AT |
1005 | if (dev->of_node) |
1006 | adv7511->type = (enum adv7511_type)of_device_get_match_data(dev); | |
1007 | else | |
1008 | adv7511->type = id->driver_data; | |
1009 | ||
1010 | memset(&link_config, 0, sizeof(link_config)); | |
1011 | ||
1e4d58cd | 1012 | if (adv7511->type == ADV7511) |
2437e7cd | 1013 | ret = adv7511_parse_dt(dev->of_node, &link_config); |
1e4d58cd AT |
1014 | else |
1015 | ret = adv7533_parse_dt(dev->of_node, adv7511); | |
1016 | if (ret) | |
1017 | return ret; | |
9c8af882 | 1018 | |
5b06ba23 AT |
1019 | ret = adv7511_init_regulators(adv7511); |
1020 | if (ret) { | |
1021 | dev_err(dev, "failed to init regulators\n"); | |
1022 | return ret; | |
1023 | } | |
1024 | ||
9c8af882 LPC |
1025 | /* |
1026 | * The power down GPIO is optional. If present, toggle it from active to | |
1027 | * inactive to wake up the encoder. | |
1028 | */ | |
1029 | adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH); | |
5b06ba23 AT |
1030 | if (IS_ERR(adv7511->gpio_pd)) { |
1031 | ret = PTR_ERR(adv7511->gpio_pd); | |
1032 | goto uninit_regulators; | |
1033 | } | |
9c8af882 LPC |
1034 | |
1035 | if (adv7511->gpio_pd) { | |
1036 | mdelay(5); | |
1037 | gpiod_set_value_cansleep(adv7511->gpio_pd, 0); | |
1038 | } | |
1039 | ||
1040 | adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config); | |
5b06ba23 AT |
1041 | if (IS_ERR(adv7511->regmap)) { |
1042 | ret = PTR_ERR(adv7511->regmap); | |
1043 | goto uninit_regulators; | |
1044 | } | |
9c8af882 LPC |
1045 | |
1046 | ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val); | |
1047 | if (ret) | |
5b06ba23 | 1048 | goto uninit_regulators; |
9c8af882 LPC |
1049 | dev_dbg(dev, "Rev. %d\n", val); |
1050 | ||
2437e7cd AT |
1051 | if (adv7511->type == ADV7511) |
1052 | ret = regmap_register_patch(adv7511->regmap, | |
1053 | adv7511_fixed_registers, | |
1054 | ARRAY_SIZE(adv7511_fixed_registers)); | |
1055 | else | |
1056 | ret = adv7533_patch_registers(adv7511); | |
9c8af882 | 1057 | if (ret) |
5b06ba23 | 1058 | goto uninit_regulators; |
9c8af882 LPC |
1059 | |
1060 | regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr); | |
1061 | regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR, | |
d25a4cbb SS |
1062 | main_i2c_addr - 0xa); |
1063 | regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR, | |
1064 | main_i2c_addr - 2); | |
1065 | ||
9c8af882 LPC |
1066 | adv7511_packet_disable(adv7511, 0xffff); |
1067 | ||
9c8af882 | 1068 | adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1); |
5b06ba23 AT |
1069 | if (!adv7511->i2c_edid) { |
1070 | ret = -ENOMEM; | |
1071 | goto uninit_regulators; | |
1072 | } | |
9c8af882 | 1073 | |
2437e7cd AT |
1074 | if (adv7511->type == ADV7533) { |
1075 | ret = adv7533_init_cec(adv7511); | |
1076 | if (ret) | |
1077 | goto err_i2c_unregister_edid; | |
1078 | } | |
1079 | ||
518cb705 JS |
1080 | INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work); |
1081 | ||
9c8af882 LPC |
1082 | if (i2c->irq) { |
1083 | init_waitqueue_head(&adv7511->wq); | |
1084 | ||
1085 | ret = devm_request_threaded_irq(dev, i2c->irq, NULL, | |
1086 | adv7511_irq_handler, | |
1087 | IRQF_ONESHOT, dev_name(dev), | |
1088 | adv7511); | |
1089 | if (ret) | |
2437e7cd | 1090 | goto err_unregister_cec; |
9c8af882 LPC |
1091 | } |
1092 | ||
1093 | /* CEC is unused for now */ | |
1094 | regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL, | |
1095 | ADV7511_CEC_CTRL_POWER_DOWN); | |
1096 | ||
c6169e49 | 1097 | adv7511_power_off(adv7511); |
9c8af882 LPC |
1098 | |
1099 | i2c_set_clientdata(i2c, adv7511); | |
1100 | ||
2437e7cd AT |
1101 | if (adv7511->type == ADV7511) |
1102 | adv7511_set_link_config(adv7511, &link_config); | |
9c8af882 | 1103 | |
e12c2f64 AT |
1104 | adv7511->bridge.funcs = &adv7511_bridge_funcs; |
1105 | adv7511->bridge.of_node = dev->of_node; | |
1106 | ||
1107 | ret = drm_bridge_add(&adv7511->bridge); | |
1108 | if (ret) { | |
1109 | dev_err(dev, "failed to add adv7511 bridge\n"); | |
2437e7cd | 1110 | goto err_unregister_cec; |
e12c2f64 AT |
1111 | } |
1112 | ||
53c515be JS |
1113 | adv7511_audio_init(dev, adv7511); |
1114 | ||
9c8af882 LPC |
1115 | return 0; |
1116 | ||
2437e7cd AT |
1117 | err_unregister_cec: |
1118 | adv7533_uninit_cec(adv7511); | |
1119 | err_i2c_unregister_edid: | |
9c8af882 | 1120 | i2c_unregister_device(adv7511->i2c_edid); |
5b06ba23 AT |
1121 | uninit_regulators: |
1122 | adv7511_uninit_regulators(adv7511); | |
9c8af882 LPC |
1123 | |
1124 | return ret; | |
1125 | } | |
1126 | ||
1127 | static int adv7511_remove(struct i2c_client *i2c) | |
1128 | { | |
1129 | struct adv7511 *adv7511 = i2c_get_clientdata(i2c); | |
1130 | ||
1e4d58cd AT |
1131 | if (adv7511->type == ADV7533) { |
1132 | adv7533_detach_dsi(adv7511); | |
1133 | adv7533_uninit_cec(adv7511); | |
1134 | } | |
1135 | ||
5b06ba23 AT |
1136 | adv7511_uninit_regulators(adv7511); |
1137 | ||
e12c2f64 AT |
1138 | drm_bridge_remove(&adv7511->bridge); |
1139 | ||
53c515be JS |
1140 | adv7511_audio_exit(adv7511); |
1141 | ||
9c8af882 LPC |
1142 | i2c_unregister_device(adv7511->i2c_edid); |
1143 | ||
1144 | kfree(adv7511->edid); | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
9c8af882 | 1149 | static const struct i2c_device_id adv7511_i2c_ids[] = { |
2437e7cd AT |
1150 | { "adv7511", ADV7511 }, |
1151 | { "adv7511w", ADV7511 }, | |
1152 | { "adv7513", ADV7511 }, | |
1153 | #ifdef CONFIG_DRM_I2C_ADV7533 | |
1154 | { "adv7533", ADV7533 }, | |
1155 | #endif | |
9c8af882 LPC |
1156 | { } |
1157 | }; | |
1158 | MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids); | |
1159 | ||
1160 | static const struct of_device_id adv7511_of_ids[] = { | |
2437e7cd AT |
1161 | { .compatible = "adi,adv7511", .data = (void *)ADV7511 }, |
1162 | { .compatible = "adi,adv7511w", .data = (void *)ADV7511 }, | |
1163 | { .compatible = "adi,adv7513", .data = (void *)ADV7511 }, | |
1164 | #ifdef CONFIG_DRM_I2C_ADV7533 | |
1165 | { .compatible = "adi,adv7533", .data = (void *)ADV7533 }, | |
1166 | #endif | |
9c8af882 LPC |
1167 | { } |
1168 | }; | |
1169 | MODULE_DEVICE_TABLE(of, adv7511_of_ids); | |
1170 | ||
1e4d58cd AT |
1171 | static struct mipi_dsi_driver adv7533_dsi_driver = { |
1172 | .driver.name = "adv7533", | |
1173 | }; | |
1174 | ||
e12c2f64 AT |
1175 | static struct i2c_driver adv7511_driver = { |
1176 | .driver = { | |
1177 | .name = "adv7511", | |
1178 | .of_match_table = adv7511_of_ids, | |
9c8af882 | 1179 | }, |
e12c2f64 AT |
1180 | .id_table = adv7511_i2c_ids, |
1181 | .probe = adv7511_probe, | |
1182 | .remove = adv7511_remove, | |
9c8af882 LPC |
1183 | }; |
1184 | ||
1e4d58cd AT |
1185 | static int __init adv7511_init(void) |
1186 | { | |
1187 | if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) | |
1188 | mipi_dsi_driver_register(&adv7533_dsi_driver); | |
1189 | ||
1190 | return i2c_add_driver(&adv7511_driver); | |
1191 | } | |
1192 | module_init(adv7511_init); | |
1193 | ||
1194 | static void __exit adv7511_exit(void) | |
1195 | { | |
1196 | i2c_del_driver(&adv7511_driver); | |
1197 | ||
1198 | if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) | |
1199 | mipi_dsi_driver_unregister(&adv7533_dsi_driver); | |
1200 | } | |
1201 | module_exit(adv7511_exit); | |
9c8af882 LPC |
1202 | |
1203 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); | |
1204 | MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver"); | |
1205 | MODULE_LICENSE("GPL"); |