blob: f75ab627811349beb55180a2c651e1c666da08a7 [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
John Stultz651e4762017-01-16 16:52:50 -0800328static void __adv7511_power_on(struct adv7511 *adv7511)
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200329{
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),
Archit Taneja40d86d22017-01-16 16:52:49 -0800341 ADV7511_INT0_EDID_READY | ADV7511_INT0_HPD);
Wolfram Sangd0be8582016-01-04 03:33:45 +0100342 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);
John Stultz651e4762017-01-16 16:52:50 -0800357}
358
359static void adv7511_power_on(struct adv7511 *adv7511)
360{
361 __adv7511_power_on(adv7511);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200362
363 /*
364 * Most of the registers are reset during power down or when HPD is low.
365 */
366 regcache_sync(adv7511->regmap);
367
Archit Taneja2437e7c2016-06-15 16:24:03 +0530368 if (adv7511->type == ADV7533)
369 adv7533_dsi_power_on(adv7511);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200370 adv7511->powered = true;
371}
372
John Stultz651e4762017-01-16 16:52:50 -0800373static void __adv7511_power_off(struct adv7511 *adv7511)
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200374{
375 /* TODO: setup additional power down modes */
376 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER,
377 ADV7511_POWER_POWER_DOWN,
378 ADV7511_POWER_POWER_DOWN);
379 regcache_mark_dirty(adv7511->regmap);
John Stultz651e4762017-01-16 16:52:50 -0800380}
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200381
John Stultz651e4762017-01-16 16:52:50 -0800382static void adv7511_power_off(struct adv7511 *adv7511)
383{
384 __adv7511_power_off(adv7511);
Archit Taneja2437e7c2016-06-15 16:24:03 +0530385 if (adv7511->type == ADV7533)
386 adv7533_dsi_power_off(adv7511);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200387 adv7511->powered = false;
388}
389
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100390/* -----------------------------------------------------------------------------
391 * Interrupt and hotplug detection
392 */
393
394static bool adv7511_hpd(struct adv7511 *adv7511)
395{
396 unsigned int irq0;
397 int ret;
398
399 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
400 if (ret < 0)
401 return false;
402
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100403 if (irq0 & ADV7511_INT0_HPD) {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100404 regmap_write(adv7511->regmap, ADV7511_REG_INT(0),
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100405 ADV7511_INT0_HPD);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100406 return true;
407 }
408
409 return false;
410}
411
John Stultz518cb702017-01-16 16:52:47 -0800412static void adv7511_hpd_work(struct work_struct *work)
413{
414 struct adv7511 *adv7511 = container_of(work, struct adv7511, hpd_work);
John Stultz6d5104c2017-01-16 16:52:48 -0800415 enum drm_connector_status status;
416 unsigned int val;
417 int ret;
John Stultz518cb702017-01-16 16:52:47 -0800418
John Stultz6d5104c2017-01-16 16:52:48 -0800419 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
420 if (ret < 0)
421 status = connector_status_disconnected;
422 else if (val & ADV7511_STATUS_HPD)
423 status = connector_status_connected;
424 else
425 status = connector_status_disconnected;
426
427 if (adv7511->connector.status != status) {
428 adv7511->connector.status = status;
429 drm_kms_helper_hotplug_event(adv7511->connector.dev);
430 }
John Stultz518cb702017-01-16 16:52:47 -0800431}
432
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530433static int adv7511_irq_process(struct adv7511 *adv7511, bool process_hpd)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100434{
435 unsigned int irq0, irq1;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100436 int ret;
437
438 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(0), &irq0);
439 if (ret < 0)
Laurent Pincharta5241282015-02-18 15:19:33 +0200440 return ret;
441
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100442 ret = regmap_read(adv7511->regmap, ADV7511_REG_INT(1), &irq1);
443 if (ret < 0)
Laurent Pincharta5241282015-02-18 15:19:33 +0200444 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100445
Laurent Pincharta5241282015-02-18 15:19:33 +0200446 regmap_write(adv7511->regmap, ADV7511_REG_INT(0), irq0);
447 regmap_write(adv7511->regmap, ADV7511_REG_INT(1), irq1);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100448
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530449 if (process_hpd && irq0 & ADV7511_INT0_HPD && adv7511->bridge.encoder)
John Stultz518cb702017-01-16 16:52:47 -0800450 schedule_work(&adv7511->hpd_work);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100451
Laurent Pincharta5241282015-02-18 15:19:33 +0200452 if (irq0 & ADV7511_INT0_EDID_READY || irq1 & ADV7511_INT1_DDC_ERROR) {
453 adv7511->edid_read = true;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100454
Laurent Pincharta5241282015-02-18 15:19:33 +0200455 if (adv7511->i2c_main->irq)
456 wake_up_all(&adv7511->wq);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100457 }
458
Laurent Pincharta5241282015-02-18 15:19:33 +0200459 return 0;
460}
461
462static irqreturn_t adv7511_irq_handler(int irq, void *devid)
463{
464 struct adv7511 *adv7511 = devid;
465 int ret;
466
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530467 ret = adv7511_irq_process(adv7511, true);
Laurent Pincharta5241282015-02-18 15:19:33 +0200468 return ret < 0 ? IRQ_NONE : IRQ_HANDLED;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100469}
470
471/* -----------------------------------------------------------------------------
472 * EDID retrieval
473 */
474
Laurent Pincharta5241282015-02-18 15:19:33 +0200475static int adv7511_wait_for_edid(struct adv7511 *adv7511, int timeout)
476{
477 int ret;
478
479 if (adv7511->i2c_main->irq) {
480 ret = wait_event_interruptible_timeout(adv7511->wq,
481 adv7511->edid_read, msecs_to_jiffies(timeout));
482 } else {
483 for (; timeout > 0; timeout -= 25) {
Archit Tanejaf0bfcc22016-06-15 16:20:45 +0530484 ret = adv7511_irq_process(adv7511, false);
Laurent Pincharta5241282015-02-18 15:19:33 +0200485 if (ret < 0)
486 break;
487
488 if (adv7511->edid_read)
489 break;
490
491 msleep(25);
492 }
493 }
494
495 return adv7511->edid_read ? 0 : -EIO;
496}
497
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100498static int adv7511_get_edid_block(void *data, u8 *buf, unsigned int block,
499 size_t len)
500{
501 struct adv7511 *adv7511 = data;
502 struct i2c_msg xfer[2];
503 uint8_t offset;
504 unsigned int i;
505 int ret;
506
507 if (len > 128)
508 return -EINVAL;
509
510 if (adv7511->current_edid_segment != block / 2) {
511 unsigned int status;
512
513 ret = regmap_read(adv7511->regmap, ADV7511_REG_DDC_STATUS,
514 &status);
515 if (ret < 0)
516 return ret;
517
518 if (status != 2) {
Laurent Pincharta5241282015-02-18 15:19:33 +0200519 adv7511->edid_read = false;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100520 regmap_write(adv7511->regmap, ADV7511_REG_EDID_SEGMENT,
521 block);
Laurent Pincharta5241282015-02-18 15:19:33 +0200522 ret = adv7511_wait_for_edid(adv7511, 200);
523 if (ret < 0)
524 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100525 }
526
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100527 /* Break this apart, hopefully more I2C controllers will
528 * support 64 byte transfers than 256 byte transfers
529 */
530
531 xfer[0].addr = adv7511->i2c_edid->addr;
532 xfer[0].flags = 0;
533 xfer[0].len = 1;
534 xfer[0].buf = &offset;
535 xfer[1].addr = adv7511->i2c_edid->addr;
536 xfer[1].flags = I2C_M_RD;
537 xfer[1].len = 64;
538 xfer[1].buf = adv7511->edid_buf;
539
540 offset = 0;
541
542 for (i = 0; i < 4; ++i) {
543 ret = i2c_transfer(adv7511->i2c_edid->adapter, xfer,
544 ARRAY_SIZE(xfer));
545 if (ret < 0)
546 return ret;
547 else if (ret != 2)
548 return -EIO;
549
550 xfer[1].buf += 64;
551 offset += 64;
552 }
553
554 adv7511->current_edid_segment = block / 2;
555 }
556
557 if (block % 2 == 0)
558 memcpy(buf, adv7511->edid_buf, len);
559 else
560 memcpy(buf, adv7511->edid_buf + 128, len);
561
562 return 0;
563}
564
565/* -----------------------------------------------------------------------------
Archit Tanejae12c2f62016-01-09 14:24:54 +0530566 * ADV75xx helpers
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100567 */
568
Archit Tanejae12c2f62016-01-09 14:24:54 +0530569static int adv7511_get_modes(struct adv7511 *adv7511,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100570 struct drm_connector *connector)
571{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100572 struct edid *edid;
573 unsigned int count;
574
575 /* Reading the EDID only works if the device is powered */
John Stultz3587c852017-01-16 16:52:52 -0800576 if (!adv7511->powered) {
577 unsigned int edid_i2c_addr =
578 (adv7511->i2c_main->addr << 1) + 4;
579
John Stultz4226d9b2017-01-16 16:52:51 -0800580 __adv7511_power_on(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100581
John Stultz3587c852017-01-16 16:52:52 -0800582 /* Reset the EDID_I2C_ADDR register as it might be cleared */
583 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR,
584 edid_i2c_addr);
585 }
586
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100587 edid = drm_do_get_edid(connector, adv7511_get_edid_block, adv7511);
588
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200589 if (!adv7511->powered)
John Stultz4226d9b2017-01-16 16:52:51 -0800590 __adv7511_power_off(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100591
592 kfree(adv7511->edid);
593 adv7511->edid = edid;
594 if (!edid)
595 return 0;
596
597 drm_mode_connector_update_edid_property(connector, edid);
598 count = drm_add_edid_modes(connector, edid);
599
600 adv7511_set_config_csc(adv7511, connector, adv7511->rgb);
601
602 return count;
603}
604
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100605static enum drm_connector_status
Archit Tanejae12c2f62016-01-09 14:24:54 +0530606adv7511_detect(struct adv7511 *adv7511, struct drm_connector *connector)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100607{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100608 enum drm_connector_status status;
609 unsigned int val;
610 bool hpd;
611 int ret;
612
613 ret = regmap_read(adv7511->regmap, ADV7511_REG_STATUS, &val);
614 if (ret < 0)
615 return connector_status_disconnected;
616
617 if (val & ADV7511_STATUS_HPD)
618 status = connector_status_connected;
619 else
620 status = connector_status_disconnected;
621
622 hpd = adv7511_hpd(adv7511);
623
624 /* The chip resets itself when the cable is disconnected, so in case
625 * there is a pending HPD interrupt and the cable is connected there was
626 * at least one transition from disconnected to connected and the chip
627 * has to be reinitialized. */
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200628 if (status == connector_status_connected && hpd && adv7511->powered) {
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100629 regcache_mark_dirty(adv7511->regmap);
Laurent Pinchartc6169e42015-03-02 14:38:52 +0200630 adv7511_power_on(adv7511);
Archit Tanejae12c2f62016-01-09 14:24:54 +0530631 adv7511_get_modes(adv7511, connector);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100632 if (adv7511->status == connector_status_connected)
633 status = connector_status_disconnected;
634 } else {
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100635 /* Renable HPD sensing */
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100636 regmap_update_bits(adv7511->regmap, ADV7511_REG_POWER2,
Wolfram Sang29ce4ed2016-01-04 03:33:47 +0100637 ADV7511_REG_POWER2_HPD_SRC_MASK,
638 ADV7511_REG_POWER2_HPD_SRC_BOTH);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100639 }
640
641 adv7511->status = status;
642 return status;
643}
644
Archit Tanejae12c2f62016-01-09 14:24:54 +0530645static int adv7511_mode_valid(struct adv7511 *adv7511,
646 struct drm_display_mode *mode)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100647{
648 if (mode->clock > 165000)
649 return MODE_CLOCK_HIGH;
650
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100651 return MODE_OK;
652}
653
Archit Tanejae12c2f62016-01-09 14:24:54 +0530654static void adv7511_mode_set(struct adv7511 *adv7511,
655 struct drm_display_mode *mode,
656 struct drm_display_mode *adj_mode)
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100657{
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100658 unsigned int low_refresh_rate;
659 unsigned int hsync_polarity = 0;
660 unsigned int vsync_polarity = 0;
661
662 if (adv7511->embedded_sync) {
663 unsigned int hsync_offset, hsync_len;
664 unsigned int vsync_offset, vsync_len;
665
666 hsync_offset = adj_mode->crtc_hsync_start -
667 adj_mode->crtc_hdisplay;
668 vsync_offset = adj_mode->crtc_vsync_start -
669 adj_mode->crtc_vdisplay;
670 hsync_len = adj_mode->crtc_hsync_end -
671 adj_mode->crtc_hsync_start;
672 vsync_len = adj_mode->crtc_vsync_end -
673 adj_mode->crtc_vsync_start;
674
675 /* The hardware vsync generator has a off-by-one bug */
676 vsync_offset += 1;
677
678 regmap_write(adv7511->regmap, ADV7511_REG_HSYNC_PLACEMENT_MSB,
679 ((hsync_offset >> 10) & 0x7) << 5);
680 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(0),
681 (hsync_offset >> 2) & 0xff);
682 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(1),
683 ((hsync_offset & 0x3) << 6) |
684 ((hsync_len >> 4) & 0x3f));
685 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(2),
686 ((hsync_len & 0xf) << 4) |
687 ((vsync_offset >> 6) & 0xf));
688 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(3),
689 ((vsync_offset & 0x3f) << 2) |
690 ((vsync_len >> 8) & 0x3));
691 regmap_write(adv7511->regmap, ADV7511_REG_SYNC_DECODER(4),
692 vsync_len & 0xff);
693
694 hsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PHSYNC);
695 vsync_polarity = !(adj_mode->flags & DRM_MODE_FLAG_PVSYNC);
696 } else {
697 enum adv7511_sync_polarity mode_hsync_polarity;
698 enum adv7511_sync_polarity mode_vsync_polarity;
699
700 /**
701 * If the input signal is always low or always high we want to
702 * invert or let it passthrough depending on the polarity of the
703 * current mode.
704 **/
705 if (adj_mode->flags & DRM_MODE_FLAG_NHSYNC)
706 mode_hsync_polarity = ADV7511_SYNC_POLARITY_LOW;
707 else
708 mode_hsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
709
710 if (adj_mode->flags & DRM_MODE_FLAG_NVSYNC)
711 mode_vsync_polarity = ADV7511_SYNC_POLARITY_LOW;
712 else
713 mode_vsync_polarity = ADV7511_SYNC_POLARITY_HIGH;
714
715 if (adv7511->hsync_polarity != mode_hsync_polarity &&
716 adv7511->hsync_polarity !=
717 ADV7511_SYNC_POLARITY_PASSTHROUGH)
718 hsync_polarity = 1;
719
720 if (adv7511->vsync_polarity != mode_vsync_polarity &&
721 adv7511->vsync_polarity !=
722 ADV7511_SYNC_POLARITY_PASSTHROUGH)
723 vsync_polarity = 1;
724 }
725
726 if (mode->vrefresh <= 24000)
727 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_24HZ;
728 else if (mode->vrefresh <= 25000)
729 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_25HZ;
730 else if (mode->vrefresh <= 30000)
731 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_30HZ;
732 else
733 low_refresh_rate = ADV7511_LOW_REFRESH_RATE_NONE;
734
735 regmap_update_bits(adv7511->regmap, 0xfb,
736 0x6, low_refresh_rate << 1);
737 regmap_update_bits(adv7511->regmap, 0x17,
738 0x60, (vsync_polarity << 6) | (hsync_polarity << 5));
739
Archit Taneja62b2f022016-06-17 12:15:52 +0530740 if (adv7511->type == ADV7533)
741 adv7533_mode_set(adv7511, adj_mode);
742
Archit Taneja78fa4792016-06-15 17:03:12 +0530743 drm_mode_copy(&adv7511->curr_mode, adj_mode);
744
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100745 /*
746 * TODO Test first order 4:2:2 to 4:4:4 up conversion method, which is
747 * supposed to give better results.
748 */
749
750 adv7511->f_tmds = mode->clock;
751}
752
Archit Tanejae12c2f62016-01-09 14:24:54 +0530753/* Connector funcs */
754static struct adv7511 *connector_to_adv7511(struct drm_connector *connector)
755{
756 return container_of(connector, struct adv7511, connector);
757}
758
759static int adv7511_connector_get_modes(struct drm_connector *connector)
760{
761 struct adv7511 *adv = connector_to_adv7511(connector);
762
763 return adv7511_get_modes(adv, connector);
764}
765
766static enum drm_mode_status
767adv7511_connector_mode_valid(struct drm_connector *connector,
768 struct drm_display_mode *mode)
769{
770 struct adv7511 *adv = connector_to_adv7511(connector);
771
772 return adv7511_mode_valid(adv, mode);
773}
774
775static struct drm_connector_helper_funcs adv7511_connector_helper_funcs = {
776 .get_modes = adv7511_connector_get_modes,
777 .mode_valid = adv7511_connector_mode_valid,
778};
779
780static enum drm_connector_status
781adv7511_connector_detect(struct drm_connector *connector, bool force)
782{
783 struct adv7511 *adv = connector_to_adv7511(connector);
784
785 return adv7511_detect(adv, connector);
786}
787
788static struct drm_connector_funcs adv7511_connector_funcs = {
789 .dpms = drm_atomic_helper_connector_dpms,
790 .fill_modes = drm_helper_probe_single_connector_modes,
791 .detect = adv7511_connector_detect,
792 .destroy = drm_connector_cleanup,
793 .reset = drm_atomic_helper_connector_reset,
794 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
795 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
796};
797
798/* Bridge funcs */
799static struct adv7511 *bridge_to_adv7511(struct drm_bridge *bridge)
800{
801 return container_of(bridge, struct adv7511, bridge);
802}
803
804static void adv7511_bridge_enable(struct drm_bridge *bridge)
805{
806 struct adv7511 *adv = bridge_to_adv7511(bridge);
807
808 adv7511_power_on(adv);
809}
810
811static void adv7511_bridge_disable(struct drm_bridge *bridge)
812{
813 struct adv7511 *adv = bridge_to_adv7511(bridge);
814
815 adv7511_power_off(adv);
816}
817
818static void adv7511_bridge_mode_set(struct drm_bridge *bridge,
819 struct drm_display_mode *mode,
820 struct drm_display_mode *adj_mode)
821{
822 struct adv7511 *adv = bridge_to_adv7511(bridge);
823
824 adv7511_mode_set(adv, mode, adj_mode);
825}
826
827static int adv7511_bridge_attach(struct drm_bridge *bridge)
828{
829 struct adv7511 *adv = bridge_to_adv7511(bridge);
830 int ret;
831
832 if (!bridge->encoder) {
833 DRM_ERROR("Parent encoder object not found");
834 return -ENODEV;
835 }
836
837 adv->connector.polled = DRM_CONNECTOR_POLL_HPD;
838
839 ret = drm_connector_init(bridge->dev, &adv->connector,
840 &adv7511_connector_funcs,
841 DRM_MODE_CONNECTOR_HDMIA);
842 if (ret) {
843 DRM_ERROR("Failed to initialize connector with drm\n");
844 return ret;
845 }
846 drm_connector_helper_add(&adv->connector,
847 &adv7511_connector_helper_funcs);
848 drm_mode_connector_attach_encoder(&adv->connector, bridge->encoder);
849
Archit Taneja1e4d58c2016-06-15 17:01:27 +0530850 if (adv->type == ADV7533)
851 ret = adv7533_attach_dsi(adv);
852
Archit Taneja40d86d22017-01-16 16:52:49 -0800853 if (adv->i2c_main->irq)
854 regmap_write(adv->regmap, ADV7511_REG_INT_ENABLE(0),
855 ADV7511_INT0_HPD);
856
Archit Tanejae12c2f62016-01-09 14:24:54 +0530857 return ret;
858}
859
860static struct drm_bridge_funcs adv7511_bridge_funcs = {
861 .enable = adv7511_bridge_enable,
862 .disable = adv7511_bridge_disable,
863 .mode_set = adv7511_bridge_mode_set,
864 .attach = adv7511_bridge_attach,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100865};
866
867/* -----------------------------------------------------------------------------
868 * Probe & remove
869 */
870
Archit Taneja5b06ba22017-01-11 12:22:27 +0530871static const char * const adv7511_supply_names[] = {
872 "avdd",
873 "dvdd",
874 "pvdd",
875 "bgvdd",
876 "dvdd-3v",
877};
878
879static const char * const adv7533_supply_names[] = {
880 "avdd",
881 "dvdd",
882 "pvdd",
883 "a2vdd",
884 "v3p3",
885 "v1p2",
886};
887
888static int adv7511_init_regulators(struct adv7511 *adv)
889{
890 struct device *dev = &adv->i2c_main->dev;
891 const char * const *supply_names;
892 unsigned int i;
893 int ret;
894
895 if (adv->type == ADV7511) {
896 supply_names = adv7511_supply_names;
897 adv->num_supplies = ARRAY_SIZE(adv7511_supply_names);
898 } else {
899 supply_names = adv7533_supply_names;
900 adv->num_supplies = ARRAY_SIZE(adv7533_supply_names);
901 }
902
903 adv->supplies = devm_kcalloc(dev, adv->num_supplies,
904 sizeof(*adv->supplies), GFP_KERNEL);
905 if (!adv->supplies)
906 return -ENOMEM;
907
908 for (i = 0; i < adv->num_supplies; i++)
909 adv->supplies[i].supply = supply_names[i];
910
911 ret = devm_regulator_bulk_get(dev, adv->num_supplies, adv->supplies);
912 if (ret)
913 return ret;
914
915 return regulator_bulk_enable(adv->num_supplies, adv->supplies);
916}
917
918static void adv7511_uninit_regulators(struct adv7511 *adv)
919{
920 regulator_bulk_disable(adv->num_supplies, adv->supplies);
921}
922
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100923static int adv7511_parse_dt(struct device_node *np,
924 struct adv7511_link_config *config)
925{
926 const char *str;
927 int ret;
928
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +0100929 of_property_read_u32(np, "adi,input-depth", &config->input_color_depth);
930 if (config->input_color_depth != 8 && config->input_color_depth != 10 &&
931 config->input_color_depth != 12)
932 return -EINVAL;
933
934 ret = of_property_read_string(np, "adi,input-colorspace", &str);
935 if (ret < 0)
936 return ret;
937
938 if (!strcmp(str, "rgb"))
939 config->input_colorspace = HDMI_COLORSPACE_RGB;
940 else if (!strcmp(str, "yuv422"))
941 config->input_colorspace = HDMI_COLORSPACE_YUV422;
942 else if (!strcmp(str, "yuv444"))
943 config->input_colorspace = HDMI_COLORSPACE_YUV444;
944 else
945 return -EINVAL;
946
947 ret = of_property_read_string(np, "adi,input-clock", &str);
948 if (ret < 0)
949 return ret;
950
951 if (!strcmp(str, "1x"))
952 config->input_clock = ADV7511_INPUT_CLOCK_1X;
953 else if (!strcmp(str, "2x"))
954 config->input_clock = ADV7511_INPUT_CLOCK_2X;
955 else if (!strcmp(str, "ddr"))
956 config->input_clock = ADV7511_INPUT_CLOCK_DDR;
957 else
958 return -EINVAL;
959
960 if (config->input_colorspace == HDMI_COLORSPACE_YUV422 ||
961 config->input_clock != ADV7511_INPUT_CLOCK_1X) {
962 ret = of_property_read_u32(np, "adi,input-style",
963 &config->input_style);
964 if (ret)
965 return ret;
966
967 if (config->input_style < 1 || config->input_style > 3)
968 return -EINVAL;
969
970 ret = of_property_read_string(np, "adi,input-justification",
971 &str);
972 if (ret < 0)
973 return ret;
974
975 if (!strcmp(str, "left"))
976 config->input_justification =
977 ADV7511_INPUT_JUSTIFICATION_LEFT;
978 else if (!strcmp(str, "evenly"))
979 config->input_justification =
980 ADV7511_INPUT_JUSTIFICATION_EVENLY;
981 else if (!strcmp(str, "right"))
982 config->input_justification =
983 ADV7511_INPUT_JUSTIFICATION_RIGHT;
984 else
985 return -EINVAL;
986
987 } else {
988 config->input_style = 1;
989 config->input_justification = ADV7511_INPUT_JUSTIFICATION_LEFT;
990 }
991
992 of_property_read_u32(np, "adi,clock-delay", &config->clock_delay);
993 if (config->clock_delay < -1200 || config->clock_delay > 1600)
994 return -EINVAL;
995
996 config->embedded_sync = of_property_read_bool(np, "adi,embedded-sync");
997
998 /* Hardcode the sync pulse configurations for now. */
999 config->sync_pulse = ADV7511_INPUT_SYNC_PULSE_NONE;
1000 config->vsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1001 config->hsync_polarity = ADV7511_SYNC_POLARITY_PASSTHROUGH;
1002
1003 return 0;
1004}
1005
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001006static int adv7511_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1007{
1008 struct adv7511_link_config link_config;
1009 struct adv7511 *adv7511;
1010 struct device *dev = &i2c->dev;
Sergei Shtylyovd25a4cb2016-09-05 22:43:17 +03001011 unsigned int main_i2c_addr = i2c->addr << 1;
1012 unsigned int edid_i2c_addr = main_i2c_addr + 4;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001013 unsigned int val;
1014 int ret;
1015
1016 if (!dev->of_node)
1017 return -EINVAL;
1018
1019 adv7511 = devm_kzalloc(dev, sizeof(*adv7511), GFP_KERNEL);
1020 if (!adv7511)
1021 return -ENOMEM;
1022
Archit Taneja5b06ba22017-01-11 12:22:27 +05301023 adv7511->i2c_main = i2c;
Laurent Pinchartc6169e42015-03-02 14:38:52 +02001024 adv7511->powered = false;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001025 adv7511->status = connector_status_disconnected;
1026
Archit Taneja2437e7c2016-06-15 16:24:03 +05301027 if (dev->of_node)
1028 adv7511->type = (enum adv7511_type)of_device_get_match_data(dev);
1029 else
1030 adv7511->type = id->driver_data;
1031
1032 memset(&link_config, 0, sizeof(link_config));
1033
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301034 if (adv7511->type == ADV7511)
Archit Taneja2437e7c2016-06-15 16:24:03 +05301035 ret = adv7511_parse_dt(dev->of_node, &link_config);
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301036 else
1037 ret = adv7533_parse_dt(dev->of_node, adv7511);
1038 if (ret)
1039 return ret;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001040
Archit Taneja5b06ba22017-01-11 12:22:27 +05301041 ret = adv7511_init_regulators(adv7511);
1042 if (ret) {
1043 dev_err(dev, "failed to init regulators\n");
1044 return ret;
1045 }
1046
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001047 /*
1048 * The power down GPIO is optional. If present, toggle it from active to
1049 * inactive to wake up the encoder.
1050 */
1051 adv7511->gpio_pd = devm_gpiod_get_optional(dev, "pd", GPIOD_OUT_HIGH);
Archit Taneja5b06ba22017-01-11 12:22:27 +05301052 if (IS_ERR(adv7511->gpio_pd)) {
1053 ret = PTR_ERR(adv7511->gpio_pd);
1054 goto uninit_regulators;
1055 }
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001056
1057 if (adv7511->gpio_pd) {
1058 mdelay(5);
1059 gpiod_set_value_cansleep(adv7511->gpio_pd, 0);
1060 }
1061
1062 adv7511->regmap = devm_regmap_init_i2c(i2c, &adv7511_regmap_config);
Archit Taneja5b06ba22017-01-11 12:22:27 +05301063 if (IS_ERR(adv7511->regmap)) {
1064 ret = PTR_ERR(adv7511->regmap);
1065 goto uninit_regulators;
1066 }
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001067
1068 ret = regmap_read(adv7511->regmap, ADV7511_REG_CHIP_REVISION, &val);
1069 if (ret)
Archit Taneja5b06ba22017-01-11 12:22:27 +05301070 goto uninit_regulators;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001071 dev_dbg(dev, "Rev. %d\n", val);
1072
Archit Taneja2437e7c2016-06-15 16:24:03 +05301073 if (adv7511->type == ADV7511)
1074 ret = regmap_register_patch(adv7511->regmap,
1075 adv7511_fixed_registers,
1076 ARRAY_SIZE(adv7511_fixed_registers));
1077 else
1078 ret = adv7533_patch_registers(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001079 if (ret)
Archit Taneja5b06ba22017-01-11 12:22:27 +05301080 goto uninit_regulators;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001081
1082 regmap_write(adv7511->regmap, ADV7511_REG_EDID_I2C_ADDR, edid_i2c_addr);
1083 regmap_write(adv7511->regmap, ADV7511_REG_PACKET_I2C_ADDR,
Sergei Shtylyovd25a4cb2016-09-05 22:43:17 +03001084 main_i2c_addr - 0xa);
1085 regmap_write(adv7511->regmap, ADV7511_REG_CEC_I2C_ADDR,
1086 main_i2c_addr - 2);
1087
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001088 adv7511_packet_disable(adv7511, 0xffff);
1089
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001090 adv7511->i2c_edid = i2c_new_dummy(i2c->adapter, edid_i2c_addr >> 1);
Archit Taneja5b06ba22017-01-11 12:22:27 +05301091 if (!adv7511->i2c_edid) {
1092 ret = -ENOMEM;
1093 goto uninit_regulators;
1094 }
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001095
Archit Taneja2437e7c2016-06-15 16:24:03 +05301096 if (adv7511->type == ADV7533) {
1097 ret = adv7533_init_cec(adv7511);
1098 if (ret)
1099 goto err_i2c_unregister_edid;
1100 }
1101
John Stultz518cb702017-01-16 16:52:47 -08001102 INIT_WORK(&adv7511->hpd_work, adv7511_hpd_work);
1103
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001104 if (i2c->irq) {
1105 init_waitqueue_head(&adv7511->wq);
1106
1107 ret = devm_request_threaded_irq(dev, i2c->irq, NULL,
1108 adv7511_irq_handler,
1109 IRQF_ONESHOT, dev_name(dev),
1110 adv7511);
1111 if (ret)
Archit Taneja2437e7c2016-06-15 16:24:03 +05301112 goto err_unregister_cec;
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001113 }
1114
1115 /* CEC is unused for now */
1116 regmap_write(adv7511->regmap, ADV7511_REG_CEC_CTRL,
1117 ADV7511_CEC_CTRL_POWER_DOWN);
1118
Laurent Pinchartc6169e42015-03-02 14:38:52 +02001119 adv7511_power_off(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001120
1121 i2c_set_clientdata(i2c, adv7511);
1122
Archit Taneja2437e7c2016-06-15 16:24:03 +05301123 if (adv7511->type == ADV7511)
1124 adv7511_set_link_config(adv7511, &link_config);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001125
Archit Tanejae12c2f62016-01-09 14:24:54 +05301126 adv7511->bridge.funcs = &adv7511_bridge_funcs;
1127 adv7511->bridge.of_node = dev->of_node;
1128
1129 ret = drm_bridge_add(&adv7511->bridge);
1130 if (ret) {
1131 dev_err(dev, "failed to add adv7511 bridge\n");
Archit Taneja2437e7c2016-06-15 16:24:03 +05301132 goto err_unregister_cec;
Archit Tanejae12c2f62016-01-09 14:24:54 +05301133 }
1134
John Stultz53c515b2016-11-28 17:22:31 -08001135 adv7511_audio_init(dev, adv7511);
1136
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001137 return 0;
1138
Archit Taneja2437e7c2016-06-15 16:24:03 +05301139err_unregister_cec:
1140 adv7533_uninit_cec(adv7511);
1141err_i2c_unregister_edid:
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001142 i2c_unregister_device(adv7511->i2c_edid);
Archit Taneja5b06ba22017-01-11 12:22:27 +05301143uninit_regulators:
1144 adv7511_uninit_regulators(adv7511);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001145
1146 return ret;
1147}
1148
1149static int adv7511_remove(struct i2c_client *i2c)
1150{
1151 struct adv7511 *adv7511 = i2c_get_clientdata(i2c);
1152
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301153 if (adv7511->type == ADV7533) {
1154 adv7533_detach_dsi(adv7511);
1155 adv7533_uninit_cec(adv7511);
1156 }
1157
Archit Taneja5b06ba22017-01-11 12:22:27 +05301158 adv7511_uninit_regulators(adv7511);
1159
Archit Tanejae12c2f62016-01-09 14:24:54 +05301160 drm_bridge_remove(&adv7511->bridge);
1161
John Stultz53c515b2016-11-28 17:22:31 -08001162 adv7511_audio_exit(adv7511);
1163
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001164 i2c_unregister_device(adv7511->i2c_edid);
1165
1166 kfree(adv7511->edid);
1167
1168 return 0;
1169}
1170
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001171static const struct i2c_device_id adv7511_i2c_ids[] = {
Archit Taneja2437e7c2016-06-15 16:24:03 +05301172 { "adv7511", ADV7511 },
1173 { "adv7511w", ADV7511 },
1174 { "adv7513", ADV7511 },
1175#ifdef CONFIG_DRM_I2C_ADV7533
1176 { "adv7533", ADV7533 },
1177#endif
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001178 { }
1179};
1180MODULE_DEVICE_TABLE(i2c, adv7511_i2c_ids);
1181
1182static const struct of_device_id adv7511_of_ids[] = {
Archit Taneja2437e7c2016-06-15 16:24:03 +05301183 { .compatible = "adi,adv7511", .data = (void *)ADV7511 },
1184 { .compatible = "adi,adv7511w", .data = (void *)ADV7511 },
1185 { .compatible = "adi,adv7513", .data = (void *)ADV7511 },
1186#ifdef CONFIG_DRM_I2C_ADV7533
1187 { .compatible = "adi,adv7533", .data = (void *)ADV7533 },
1188#endif
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001189 { }
1190};
1191MODULE_DEVICE_TABLE(of, adv7511_of_ids);
1192
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301193static struct mipi_dsi_driver adv7533_dsi_driver = {
1194 .driver.name = "adv7533",
1195};
1196
Archit Tanejae12c2f62016-01-09 14:24:54 +05301197static struct i2c_driver adv7511_driver = {
1198 .driver = {
1199 .name = "adv7511",
1200 .of_match_table = adv7511_of_ids,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001201 },
Archit Tanejae12c2f62016-01-09 14:24:54 +05301202 .id_table = adv7511_i2c_ids,
1203 .probe = adv7511_probe,
1204 .remove = adv7511_remove,
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001205};
1206
Archit Taneja1e4d58c2016-06-15 17:01:27 +05301207static int __init adv7511_init(void)
1208{
1209 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1210 mipi_dsi_driver_register(&adv7533_dsi_driver);
1211
1212 return i2c_add_driver(&adv7511_driver);
1213}
1214module_init(adv7511_init);
1215
1216static void __exit adv7511_exit(void)
1217{
1218 i2c_del_driver(&adv7511_driver);
1219
1220 if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
1221 mipi_dsi_driver_unregister(&adv7533_dsi_driver);
1222}
1223module_exit(adv7511_exit);
Lars-Peter Clausen9c8af882012-03-05 16:30:57 +01001224
1225MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
1226MODULE_DESCRIPTION("ADV7511 HDMI transmitter driver");
1227MODULE_LICENSE("GPL");