blob: 213d892b6fa3815ae1bb65a02afae48194d49130 [file] [log] [blame]
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001/*
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>
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010011#include <linux/module.h>
Archit Taneja2437e7c2016-06-15 16:24:03 +053012#include <linux/of_device.h>
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010013#include <linux/slab.h>
14
15#include <drm/drmP.h>
Archit Tanejae12c2f62016-01-09 14:24:54 +053016#include <drm/drm_atomic.h>
17#include <drm/drm_atomic_helper.h>
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010018#include <drm/drm_edid.h>
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010019
20#include "adv7511.h"
21
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010022/* ADI recommended values for proper operation. */
Nariman Poushin8019ff62015-07-16 16:36:21 +010023static const struct reg_sequence adv7511_fixed_registers[] = {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +010024 { 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
39static 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
74static 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:
Wolfram Sangcd38a332016-01-04 03:33:46 +0100105 case ADV7511_REG_EDID_READ_CTRL:
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100106 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
116static 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
132static 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
163static 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
178static 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 */
194static 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
200static 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
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(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
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200328static void adv7511_power_on(struct adv7511 *adv7511)
329{
330 adv7511->current_edid_segment = -1;
331
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200332 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
333 ADV7511_POWER_POWER_DOWN, 0);
Wolfram Sangd0be8582016-01-04 03:33:45 +0100334 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 }
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200345
346 /*
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100347 * Per spec it is allowed to pulse the HPD signal to indicate that the
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200348 * EDID information has changed. Some monitors do this when they wakeup
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100349 * from standby or are enabled. When the HPD goes low the adv7511 is
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200350 * reset and the outputs are disabled which might cause the monitor to
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100351 * go to standby again. To avoid this we ignore the HPD pin for the
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200352 * first few seconds after enabling the output.
353 */
354 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100355 ADV7511_REG_POWER2_HPD_SRC_MASK,
356 ADV7511_REG_POWER2_HPD_SRC_NONE);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200357
358 /*
359 * Most of the registers are reset during power down or when HPD is low.
360 */
361 regcache_sync(adv7511->regmap);
362
Archit Taneja2437e7c2016-06-15 16:24:03 +0530363 if (adv7511->type == ADV7533)
364 adv7533_dsi_power_on(adv7511);
365
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200366 adv7511->powered = true;
367}
368
369static 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
Archit Taneja2437e7c2016-06-15 16:24:03 +0530377 if (adv7511->type == ADV7533)
378 adv7533_dsi_power_off(adv7511);
379
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200380 adv7511->powered = false;
381}
382
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100383/* -----------------------------------------------------------------------------
384 * Interrupt and hotplug detection
385 */
386
387static 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
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100396 if (irq0 & ADV7511_INT0_HPD) {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100397 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100398 ADV7511_INT0_HPD);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100399 return true;
400 }
401
402 return false;
403}
404
John Stultz8b5a7e42017-01-16 16:52:47 -0800405static void adv7511_hpd_work(struct work_struct *work)
406{
407 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
John Stultz8bc67f62017-01-16 16:52:48 -0800408 enum drm_connector_status status;
409 unsigned int val;
410 int ret;
John Stultz8b5a7e42017-01-16 16:52:47 -0800411
John Stultz8bc67f62017-01-16 16:52:48 -0800412 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
413 if (ret < 0)
414 status = connector_status_disconnected;
415 else if (val & ADV7511_STATUS_HPD)
416 status = connector_status_connected;
417 else
418 status = connector_status_disconnected;
419
420 if (adv7511->connector.status != status) {
421 adv7511->connector.status = status;
422 drm_kms_helper_hotplug_event(adv7511->connector.dev);
423 }
John Stultz8b5a7e42017-01-16 16:52:47 -0800424}
425
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530426static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100427{
428 unsigned int irq0, irq1;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100429 int ret;
430
431 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
432 if (ret < 0)
Laurent Pincharta5241282015-02-18 15:19:33 +0200433 return ret;
434
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100435 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
436 if (ret < 0)
Laurent Pincharta5241282015-02-18 15:19:33 +0200437 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100438
Laurent Pincharta5241282015-02-18 15:19:33 +0200439 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
440 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100441
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530442 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
John Stultz8b5a7e42017-01-16 16:52:47 -0800443 schedule_work(&adv7511->hpd_work);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100444
Laurent Pincharta5241282015-02-18 15:19:33 +0200445 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
446 adv7511->edid_read = true;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100447
Laurent Pincharta5241282015-02-18 15:19:33 +0200448 if (adv7511->i2c_main->irq)
449 wake_up_all(&adv7511->wq);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100450 }
451
Laurent Pincharta5241282015-02-18 15:19:33 +0200452 return 0;
453}
454
455static irqreturn_t adv7511_irq_handler(int irq, void *devid)
456{
457 struct adv7511 *adv7511 = devid;
458 int ret;
459
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530460 ret = adv7511_irq_process(adv7511, true);
Laurent Pincharta5241282015-02-18 15:19:33 +0200461 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100462}
463
464/* -----------------------------------------------------------------------------
465 * EDID retrieval
466 */
467
Laurent Pincharta5241282015-02-18 15:19:33 +0200468static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
469{
470 int ret;
471
472 if (adv7511->i2c_main->irq) {
473 ret = wait_event_interruptible_timeout(adv7511->wq,
474 adv7511->edid_read, msecs_to_jiffies(timeout));
475 } else {
476 for (; timeout > 0; timeout -= 25) {
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530477 ret = adv7511_irq_process(adv7511, false);
Laurent Pincharta5241282015-02-18 15:19:33 +0200478 if (ret < 0)
479 break;
480
481 if (adv7511->edid_read)
482 break;
483
484 msleep(25);
485 }
486 }
487
488 return adv7511->edid_read ? 0 : -EIO;
489}
490
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100491static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
492 size_t len)
493{
494 struct adv7511 *adv7511 = data;
495 struct i2c_msg xfer[2];
496 uint8_t offset;
497 unsigned int i;
498 int ret;
499
500 if (len > 128)
501 return -EINVAL;
502
503 if (adv7511->current_edid_segment != block / 2) {
504 unsigned int status;
505
506 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
507 &status);
508 if (ret < 0)
509 return ret;
510
511 if (status != 2) {
Laurent Pincharta5241282015-02-18 15:19:33 +0200512 adv7511->edid_read = false;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100513 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
514 block);
Laurent Pincharta5241282015-02-18 15:19:33 +0200515 ret = adv7511_wait_for_edid(adv7511, 200);
516 if (ret < 0)
517 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100518 }
519
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100520 /* Break this apart, hopefully more I2C controllers will
521 * support 64 byte transfers than 256 byte transfers
522 */
523
524 xfer[0].addr = adv7511->i2c_edid->addr;
525 xfer[0].flags = 0;
526 xfer[0].len = 1;
527 xfer[0].buf = &offset;
528 xfer[1].addr = adv7511->i2c_edid->addr;
529 xfer[1].flags = I2C_M_RD;
530 xfer[1].len = 64;
531 xfer[1].buf = adv7511->edid_buf;
532
533 offset = 0;
534
535 for (i = 0; i < 4; ++i) {
536 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
537 ARRAY_SIZE(xfer));
538 if (ret < 0)
539 return ret;
540 else if (ret != 2)
541 return -EIO;
542
543 xfer[1].buf += 64;
544 offset += 64;
545 }
546
547 adv7511->current_edid_segment = block / 2;
548 }
549
550 if (block % 2 == 0)
551 memcpy(buf, adv7511->edid_buf, len);
552 else
553 memcpy(buf, adv7511->edid_buf + 128, len);
554
555 return 0;
556}
557
558/* -----------------------------------------------------------------------------
Archit Tanejae12c2f62016-01-09 14:24:54 +0530559 * ADV75xx helpers
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100560 */
561
Archit Tanejae12c2f62016-01-09 14:24:54 +0530562static int adv7511_get_modes(struct adv7511 *adv7511,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100563 struct drm_connector *connector)
564{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100565 struct edid *edid;
566 unsigned int count;
567
568 /* Reading the EDID only works if the device is powered */
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200569 if (!adv7511->powered) {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100570 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
571 ADV7511_POWER_POWER_DOWN, 0);
Wolfram Sangd0be8582016-01-04 03:33:45 +0100572 if (adv7511->i2c_main->irq) {
573 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(0),
574 ADV7511_INT0_EDID_READY);
575 regmap_write(adv7511->regmap, ADV7511_REG_INT_ENABLE(1),
576 ADV7511_INT1_DDC_ERROR);
577 }
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100578 adv7511->current_edid_segment = -1;
579 }
580
581 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
582
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200583 if (!adv7511->powered)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100584 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
585 ADV7511_POWER_POWER_DOWN,
586 ADV7511_POWER_POWER_DOWN);
587
588 kfree(adv7511->edid);
589 adv7511->edid = edid;
590 if (!edid)
591 return 0;
592
593 drm_mode_connector_update_edid_property(connector, edid);
594 count = drm_add_edid_modes(connector, edid);
595
596 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
597
598 return count;
599}
600
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100601static enum drm_connector_status
Archit Tanejae12c2f62016-01-09 14:24:54 +0530602adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100603{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100604 enum drm_connector_status status;
605 unsigned int val;
606 bool hpd;
607 int ret;
608
609 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
610 if (ret < 0)
611 return connector_status_disconnected;
612
613 if (val & ADV7511_STATUS_HPD)
614 status = connector_status_connected;
615 else
616 status = connector_status_disconnected;
617
618 hpd = adv7511_hpd(adv7511);
619
620 /* The chip resets itself when the cable is disconnected, so in case
621 * there is a pending HPD interrupt and the cable is connected there was
622 * at least one transition from disconnected to connected and the chip
623 * has to be reinitialized. */
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200624 if (status == connector_status_connected && hpd && adv7511->powered) {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100625 regcache_mark_dirty(adv7511->regmap);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200626 adv7511_power_on(adv7511);
Archit Tanejae12c2f62016-01-09 14:24:54 +0530627 adv7511_get_modes(adv7511, connector);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100628 if (adv7511->status == connector_status_connected)
629 status = connector_status_disconnected;
630 } else {
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100631 /* Renable HPD sensing */
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100632 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100633 ADV7511_REG_POWER2_HPD_SRC_MASK,
634 ADV7511_REG_POWER2_HPD_SRC_BOTH);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100635 }
636
637 adv7511->status = status;
638 return status;
639}
640
Archit Tanejae12c2f62016-01-09 14:24:54 +0530641static int adv7511_mode_valid(struct adv7511 *adv7511,
642 struct drm_display_mode *mode)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100643{
644 if (mode->clock > 165000)
645 return MODE_CLOCK_HIGH;
646
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100647 return MODE_OK;
648}
649
Archit Tanejae12c2f62016-01-09 14:24:54 +0530650static void adv7511_mode_set(struct adv7511 *adv7511,
651 struct drm_display_mode *mode,
652 struct drm_display_mode *adj_mode)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100653{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100654 unsigned int low_refresh_rate;
655 unsigned int hsync_polarity = 0;
656 unsigned int vsync_polarity = 0;
657
658 if (adv7511->embedded_sync) {
659 unsigned int hsync_offset, hsync_len;
660 unsigned int vsync_offset, vsync_len;
661
662 hsync_offset = adj_mode->crtc_hsync_start -
663 adj_mode->crtc_hdisplay;
664 vsync_offset = adj_mode->crtc_vsync_start -
665 adj_mode->crtc_vdisplay;
666 hsync_len = adj_mode->crtc_hsync_end -
667 adj_mode->crtc_hsync_start;
668 vsync_len = adj_mode->crtc_vsync_end -
669 adj_mode->crtc_vsync_start;
670
671 /* The hardware vsync generator has a off-by-one bug */
672 vsync_offset += 1;
673
674 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
675 ((hsync_offset >> 10) & 0x7) << 5);
676 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
677 (hsync_offset >> 2) & 0xff);
678 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
679 ((hsync_offset & 0x3) << 6) |
680 ((hsync_len >> 4) & 0x3f));
681 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
682 ((hsync_len & 0xf) << 4) |
683 ((vsync_offset >> 6) & 0xf));
684 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
685 ((vsync_offset & 0x3f) << 2) |
686 ((vsync_len >> 8) & 0x3));
687 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
688 vsync_len & 0xff);
689
690 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
691 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
692 } else {
693 enum adv7511_sync_polarity mode_hsync_polarity;
694 enum adv7511_sync_polarity mode_vsync_polarity;
695
696 /**
697 * If the input signal is always low or always high we want to
698 * invert or let it passthrough depending on the polarity of the
699 * current mode.
700 **/
701 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
702 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
703 else
704 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
705
706 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
707 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
708 else
709 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
710
711 if (adv7511->hsync_polarity != mode_hsync_polarity &&
712 adv7511->hsync_polarity !=
713 ADV7511_SYNC_POLARITY_PASSTHROUGH)
714 hsync_polarity = 1;
715
716 if (adv7511->vsync_polarity != mode_vsync_polarity &&
717 adv7511->vsync_polarity !=
718 ADV7511_SYNC_POLARITY_PASSTHROUGH)
719 vsync_polarity = 1;
720 }
721
722 if (mode->vrefresh <= 24000)
723 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
724 else if (mode->vrefresh <= 25000)
725 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
726 else if (mode->vrefresh <= 30000)
727 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
728 else
729 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
730
731 regmap_update_bits(adv7511->regmap, 0xfb,
732 0x6, low_refresh_rate << 1);
733 regmap_update_bits(adv7511->regmap, 0x17,
734 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
735
Archit Taneja62b2f022016-06-17 12:15:52 +0530736 if (adv7511->type == ADV7533)
737 adv7533_mode_set(adv7511, adj_mode);
738
Archit Taneja78fa4792016-06-15 17:03:12 +0530739 drm_mode_copy(&adv7511->curr_mode, adj_mode);
740
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100741 /*
742 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
743 * supposed to give better results.
744 */
745
746 adv7511->f_tmds = mode->clock;
747}
748
Archit Tanejae12c2f62016-01-09 14:24:54 +0530749/* Connector funcs */
750static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
751{
752 return container_of(connector, struct adv7511, connector);
753}
754
755static int adv7511_connector_get_modes(struct drm_connector *connector)
756{
757 struct adv7511 *adv = connector_to_adv7511(connector);
758
759 return adv7511_get_modes(adv, connector);
760}
761
762static enum drm_mode_status
763adv7511_connector_mode_valid(struct drm_connector *connector,
764 struct drm_display_mode *mode)
765{
766 struct adv7511 *adv = connector_to_adv7511(connector);
767
768 return adv7511_mode_valid(adv, mode);
769}
770
771static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
772 .get_modes = adv7511_connector_get_modes,
773 .mode_valid = adv7511_connector_mode_valid,
774};
775
776static enum drm_connector_status
777adv7511_connector_detect(struct drm_connector *connector, bool force)
778{
779 struct adv7511 *adv = connector_to_adv7511(connector);
780
781 return adv7511_detect(adv, connector);
782}
783
784static struct drm_connector_funcs adv7511_connector_funcs = {
785 .dpms = drm_atomic_helper_connector_dpms,
786 .fill_modes = drm_helper_probe_single_connector_modes,
787 .detect = adv7511_connector_detect,
788 .destroy = drm_connector_cleanup,
789 .reset = drm_atomic_helper_connector_reset,
790 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
791 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
792};
793
794/* Bridge funcs */
795static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
796{
797 return container_of(bridge, struct adv7511, bridge);
798}
799
800static void adv7511_bridge_enable(struct drm_bridge *bridge)
801{
802 struct adv7511 *adv = bridge_to_adv7511(bridge);
803
804 adv7511_power_on(adv);
805}
806
807static void adv7511_bridge_disable(struct drm_bridge *bridge)
808{
809 struct adv7511 *adv = bridge_to_adv7511(bridge);
810
811 adv7511_power_off(adv);
812}
813
814static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
815 struct drm_display_mode *mode,
816 struct drm_display_mode *adj_mode)
817{
818 struct adv7511 *adv = bridge_to_adv7511(bridge);
819
820 adv7511_mode_set(adv, mode, adj_mode);
821}
822
823static int adv7511_bridge_attach(struct drm_bridge *bridge)
824{
825 struct adv7511 *adv = bridge_to_adv7511(bridge);
826 int ret;
827
828 if (!bridge->encoder) {
829 DRM_ERROR("Parent encoder object not found");
830 return -ENODEV;
831 }
832
833 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
834
835 ret = drm_connector_init(bridge->dev, &adv->connector,
836 &adv7511_connector_funcs,
837 DRM_MODE_CONNECTOR_HDMIA);
838 if (ret) {
839 DRM_ERROR("Failed to initialize connector with drm\n");
840 return ret;
841 }
842 drm_connector_helper_add(&adv->connector,
843 &adv7511_connector_helper_funcs);
844 drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder);
845
Archit Taneja1e4d58c2016-06-15 17:01:27 +0530846 if (adv->type == ADV7533)
847 ret = adv7533_attach_dsi(adv);
848
Archit Tanejae12c2f62016-01-09 14:24:54 +0530849 return ret;
850}
851
852static struct drm_bridge_funcs adv7511_bridge_funcs = {
853 .enable = adv7511_bridge_enable,
854 .disable = adv7511_bridge_disable,
855 .mode_set = adv7511_bridge_mode_set,
856 .attach = adv7511_bridge_attach,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100857};
858
859/* -----------------------------------------------------------------------------
860 * Probe & remove
861 */
862
863static int adv7511_parse_dt(struct device_node *np,
864 struct adv7511_link_config *config)
865{
866 const char *str;
867 int ret;
868
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100869 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
870 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
871 config->input_color_depth != 12)
872 return -EINVAL;
873
874 ret = of_property_read_string(np, "adi,input-colorspace", &str);
875 if (ret < 0)
876 return ret;
877
878 if (!strcmp(str, "rgb"))
879 config->input_colorspace = HDMI_COLORSPACE_RGB;
880 else if (!strcmp(str, "yuv422"))
881 config->input_colorspace = HDMI_COLORSPACE_YUV422;
882 else if (!strcmp(str, "yuv444"))
883 config->input_colorspace = HDMI_COLORSPACE_YUV444;
884 else
885 return -EINVAL;
886
887 ret = of_property_read_string(np, "adi,input-clock", &str);
888 if (ret < 0)
889 return ret;
890
891 if (!strcmp(str, "1x"))
892 config->input_clock = ADV7511_INPUT_CLOCK_1X;
893 else if (!strcmp(str, "2x"))
894 config->input_clock = ADV7511_INPUT_CLOCK_2X;
895 else if (!strcmp(str, "ddr"))
896 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
897 else
898 return -EINVAL;
899
900 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
901 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
902 ret = of_property_read_u32(np, "adi,input-style",
903 &config->input_style);
904 if (ret)
905 return ret;
906
907 if (config->input_style < 1 || config->input_style > 3)
908 return -EINVAL;
909
910 ret = of_property_read_string(np, "adi,input-justification",
911 &str);
912 if (ret < 0)
913 return ret;
914
915 if (!strcmp(str, "left"))
916 config->input_justification =
917 ADV7511_INPUT_JUSTIFICATION_LEFT;
918 else if (!strcmp(str, "evenly"))
919 config->input_justification =
920 ADV7511_INPUT_JUSTIFICATION_EVENLY;
921 else if (!strcmp(str, "right"))
922 config->input_justification =
923 ADV7511_INPUT_JUSTIFICATION_RIGHT;
924 else
925 return -EINVAL;
926
927 } else {
928 config->input_style = 1;
929 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
930 }
931
932 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
933 if (config->clock_delay < -1200 || config->clock_delay > 1600)
934 return -EINVAL;
935
936 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
937
938 /* Hardcode the sync pulse configurations for now. */
939 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
940 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
941 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
942
943 return 0;
944}
945
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100946static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
947{
948 struct adv7511_link_config link_config;
949 struct adv7511 *adv7511;
950 struct device *dev = &i2c->dev;
Sergei Shtylyovd25a4cb2016-09-05 22:43:17 +0300951 unsigned int main_i2c_addr = i2c->addr << 1;
952 unsigned int edid_i2c_addr = main_i2c_addr + 4;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100953 unsigned int val;
954 int ret;
955
956 if (!dev->of_node)
957 return -EINVAL;
958
959 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
960 if (!adv7511)
961 return -ENOMEM;
962
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200963 adv7511->powered = false;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100964 adv7511->status = connector_status_disconnected;
965
Archit Taneja2437e7c2016-06-15 16:24:03 +0530966 if (dev->of_node)
967 adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
968 else
969 adv7511->type = id->driver_data;
970
971 memset(&link_config, 0, sizeof(link_config));
972
Archit Taneja1e4d58c2016-06-15 17:01:27 +0530973 if (adv7511->type == ADV7511)
Archit Taneja2437e7c2016-06-15 16:24:03 +0530974 ret = adv7511_parse_dt(dev->of_node, &link_config);
Archit Taneja1e4d58c2016-06-15 17:01:27 +0530975 else
976 ret = adv7533_parse_dt(dev->of_node, adv7511);
977 if (ret)
978 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100979
980 /*
981 * The power down GPIO is optional. If present, toggle it from active to
982 * inactive to wake up the encoder.
983 */
984 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
985 if (IS_ERR(adv7511->gpio_pd))
986 return PTR_ERR(adv7511->gpio_pd);
987
988 if (adv7511->gpio_pd) {
989 mdelay(5);
990 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
991 }
992
993 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
994 if (IS_ERR(adv7511->regmap))
995 return PTR_ERR(adv7511->regmap);
996
997 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
998 if (ret)
999 return ret;
1000 dev_dbg(dev, "Rev. %d\n", val);
1001
Archit Taneja2437e7c2016-06-15 16:24:03 +05301002 if (adv7511->type == ADV7511)
1003 ret = regmap_register_patch(adv7511->regmap,
1004 adv7511_fixed_registers,
1005 ARRAY_SIZE(adv7511_fixed_registers));
1006 else
1007 ret = adv7533_patch_registers(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001008 if (ret)
1009 return ret;
1010
1011 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
1012 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
Sergei Shtylyovd25a4cb2016-09-05 22:43:17 +03001013 main_i2c_addr - 0xa);
1014 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
1015 main_i2c_addr - 2);
1016
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001017 adv7511_packet_disable(adv7511, 0xffff);
1018
1019 adv7511->i2c_main = i2c;
1020 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
1021 if (!adv7511->i2c_edid)
1022 return -ENOMEM;
1023
Archit Taneja2437e7c2016-06-15 16:24:03 +05301024 if (adv7511->type == ADV7533) {
1025 ret = adv7533_init_cec(adv7511);
1026 if (ret)
1027 goto err_i2c_unregister_edid;
1028 }
1029
John Stultz8b5a7e42017-01-16 16:52:47 -08001030 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1031
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001032 if (i2c->irq) {
1033 init_waitqueue_head(&adv7511->wq);
1034
1035 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1036 adv7511_irq_handler,
1037 IRQF_ONESHOT, dev_name(dev),
1038 adv7511);
1039 if (ret)
Archit Taneja2437e7c2016-06-15 16:24:03 +05301040 goto err_unregister_cec;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001041 }
1042
1043 /* CEC is unused for now */
1044 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1045 ADV7511_CEC_CTRL_POWER_DOWN);
1046
Laurent Pinchartc6169e42015-03-02 14:38:52 +02001047 adv7511_power_off(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001048
1049 i2c_set_clientdata(i2c, adv7511);
1050
Archit Taneja2437e7c2016-06-15 16:24:03 +05301051 if (adv7511->type == ADV7511)
1052 adv7511_set_link_config(adv7511, &link_config);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001053
Archit Tanejae12c2f62016-01-09 14:24:54 +05301054 adv7511->bridge.funcs = &adv7511_bridge_funcs;
1055 adv7511->bridge.of_node = dev->of_node;
1056
1057 ret = drm_bridge_add(&adv7511->bridge);
1058 if (ret) {
1059 dev_err(dev, "failed to add adv7511 bridge\n");
Archit Taneja2437e7c2016-06-15 16:24:03 +05301060 goto err_unregister_cec;
Archit Tanejae12c2f62016-01-09 14:24:54 +05301061 }
1062
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001063 return 0;
1064
Archit Taneja2437e7c2016-06-15 16:24:03 +05301065err_unregister_cec:
1066 adv7533_uninit_cec(adv7511);
1067err_i2c_unregister_edid:
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001068 i2c_unregister_device(adv7511->i2c_edid);
1069
1070 return ret;
1071}
1072
1073static int adv7511_remove(struct i2c_client *i2c)
1074{
1075 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1076
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301077 if (adv7511->type == ADV7533) {
1078 adv7533_detach_dsi(adv7511);
1079 adv7533_uninit_cec(adv7511);
1080 }
1081
Archit Tanejae12c2f62016-01-09 14:24:54 +05301082 drm_bridge_remove(&adv7511->bridge);
1083
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001084 i2c_unregister_device(adv7511->i2c_edid);
1085
1086 kfree(adv7511->edid);
1087
1088 return 0;
1089}
1090
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001091static const struct i2c_device_id adv7511_i2c_ids[] = {
Archit Taneja2437e7c2016-06-15 16:24:03 +05301092 { "adv7511", ADV7511 },
1093 { "adv7511w", ADV7511 },
1094 { "adv7513", ADV7511 },
1095#ifdef CONFIG_DRM_I2C_ADV7533
1096 { "adv7533", ADV7533 },
1097#endif
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001098 { }
1099};
1100MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1101
1102static const struct of_device_id adv7511_of_ids[] = {
Archit Taneja2437e7c2016-06-15 16:24:03 +05301103 { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1104 { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1105 { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1106#ifdef CONFIG_DRM_I2C_ADV7533
1107 { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1108#endif
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001109 { }
1110};
1111MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1112
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301113static struct mipi_dsi_driver adv7533_dsi_driver = {
1114 .driver.name = "adv7533",
1115};
1116
Archit Tanejae12c2f62016-01-09 14:24:54 +05301117static struct i2c_driver adv7511_driver = {
1118 .driver = {
1119 .name = "adv7511",
1120 .of_match_table = adv7511_of_ids,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001121 },
Archit Tanejae12c2f62016-01-09 14:24:54 +05301122 .id_table = adv7511_i2c_ids,
1123 .probe = adv7511_probe,
1124 .remove = adv7511_remove,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001125};
1126
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301127static int __init adv7511_init(void)
1128{
1129 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1130 mipi_dsi_driver_register(&adv7533_dsi_driver);
1131
1132 return i2c_add_driver(&adv7511_driver);
1133}
1134module_init(adv7511_init);
1135
1136static void __exit adv7511_exit(void)
1137{
1138 i2c_del_driver(&adv7511_driver);
1139
1140 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1141 mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1142}
1143module_exit(adv7511_exit);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001144
1145MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1146MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1147MODULE_LICENSE("GPL");