blob: 80bb512a869fac21276517dcd296363e6e90c524 [file] [log] [blame]
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001/*
2 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
Andy Yanb21f4b62014-12-05 14:26:31 +08009 * Designware High-Definition Multimedia Interface (HDMI) driver
Fabio Estevam9aaf8802013-11-29 08:46:32 -020010 *
11 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
12 */
Andy Yanb21f4b62014-12-05 14:26:31 +080013#include <linux/module.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020014#include <linux/irq.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/clk.h>
Sachin Kamat5a819ed2014-01-28 10:33:16 +053018#include <linux/hdmi.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020019#include <linux/of_device.h>
20
Andy Yan3d1b35a2014-12-05 14:25:05 +080021#include <drm/drm_of.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020022#include <drm/drmP.h>
23#include <drm/drm_crtc_helper.h>
24#include <drm/drm_edid.h>
25#include <drm/drm_encoder_slave.h>
Andy Yanb21f4b62014-12-05 14:26:31 +080026#include <drm/bridge/dw_hdmi.h>
Fabio Estevam9aaf8802013-11-29 08:46:32 -020027
Andy Yanb21f4b62014-12-05 14:26:31 +080028#include "dw_hdmi.h"
Fabio Estevam9aaf8802013-11-29 08:46:32 -020029
30#define HDMI_EDID_LEN 512
31
32#define RGB 0
33#define YCBCR444 1
34#define YCBCR422_16BITS 2
35#define YCBCR422_8BITS 3
36#define XVYCC444 4
37
38enum hdmi_datamap {
39 RGB444_8B = 0x01,
40 RGB444_10B = 0x03,
41 RGB444_12B = 0x05,
42 RGB444_16B = 0x07,
43 YCbCr444_8B = 0x09,
44 YCbCr444_10B = 0x0B,
45 YCbCr444_12B = 0x0D,
46 YCbCr444_16B = 0x0F,
47 YCbCr422_8B = 0x16,
48 YCbCr422_10B = 0x14,
49 YCbCr422_12B = 0x12,
50};
51
Fabio Estevam9aaf8802013-11-29 08:46:32 -020052static const u16 csc_coeff_default[3][4] = {
53 { 0x2000, 0x0000, 0x0000, 0x0000 },
54 { 0x0000, 0x2000, 0x0000, 0x0000 },
55 { 0x0000, 0x0000, 0x2000, 0x0000 }
56};
57
58static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
59 { 0x2000, 0x6926, 0x74fd, 0x010e },
60 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
61 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
62};
63
64static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
65 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
66 { 0x2000, 0x3264, 0x0000, 0x7e6d },
67 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
68};
69
70static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
71 { 0x2591, 0x1322, 0x074b, 0x0000 },
72 { 0x6535, 0x2000, 0x7acc, 0x0200 },
73 { 0x6acd, 0x7534, 0x2000, 0x0200 }
74};
75
76static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
77 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
78 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
79 { 0x6756, 0x78ab, 0x2000, 0x0200 }
80};
81
82struct hdmi_vmode {
83 bool mdvi;
84 bool mhsyncpolarity;
85 bool mvsyncpolarity;
86 bool minterlaced;
87 bool mdataenablepolarity;
88
89 unsigned int mpixelclock;
90 unsigned int mpixelrepetitioninput;
91 unsigned int mpixelrepetitionoutput;
92};
93
94struct hdmi_data_info {
95 unsigned int enc_in_format;
96 unsigned int enc_out_format;
97 unsigned int enc_color_depth;
98 unsigned int colorimetry;
99 unsigned int pix_repet_factor;
100 unsigned int hdcp_enable;
101 struct hdmi_vmode video_mode;
102};
103
Andy Yanb21f4b62014-12-05 14:26:31 +0800104struct dw_hdmi {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200105 struct drm_connector connector;
Andy Yan3d1b35a2014-12-05 14:25:05 +0800106 struct drm_encoder *encoder;
107 struct drm_bridge *bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200108
Andy Yanb21f4b62014-12-05 14:26:31 +0800109 enum dw_hdmi_devtype dev_type;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200110 struct device *dev;
111 struct clk *isfr_clk;
112 struct clk *iahb_clk;
113
114 struct hdmi_data_info hdmi_data;
Andy Yanb21f4b62014-12-05 14:26:31 +0800115 const struct dw_hdmi_plat_data *plat_data;
116
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200117 int vic;
118
119 u8 edid[HDMI_EDID_LEN];
120 bool cable_plugin;
121
122 bool phy_enabled;
123 struct drm_display_mode previous_mode;
124
125 struct regmap *regmap;
126 struct i2c_adapter *ddc;
127 void __iomem *regs;
128
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200129 unsigned int sample_rate;
130 int ratio;
Andy Yan0cd9d142014-12-05 14:28:24 +0800131
132 void (*write)(struct dw_hdmi *hdmi, u8 val, int offset);
133 u8 (*read)(struct dw_hdmi *hdmi, int offset);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200134};
135
Andy Yan0cd9d142014-12-05 14:28:24 +0800136static void dw_hdmi_writel(struct dw_hdmi *hdmi, u8 val, int offset)
137{
138 writel(val, hdmi->regs + (offset << 2));
139}
140
141static u8 dw_hdmi_readl(struct dw_hdmi *hdmi, int offset)
142{
143 return readl(hdmi->regs + (offset << 2));
144}
145
146static void dw_hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200147{
148 writeb(val, hdmi->regs + offset);
149}
150
Andy Yan0cd9d142014-12-05 14:28:24 +0800151static u8 dw_hdmi_readb(struct dw_hdmi *hdmi, int offset)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200152{
153 return readb(hdmi->regs + offset);
154}
155
Andy Yan0cd9d142014-12-05 14:28:24 +0800156static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
157{
158 hdmi->write(hdmi, val, offset);
159}
160
161static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
162{
163 return hdmi->read(hdmi, offset);
164}
165
Andy Yanb21f4b62014-12-05 14:26:31 +0800166static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
Russell King812bc612013-11-04 12:42:02 +0000167{
168 u8 val = hdmi_readb(hdmi, reg) & ~mask;
Fabio Estevamb44ab1b2014-04-28 08:01:07 -0300169
Russell King812bc612013-11-04 12:42:02 +0000170 val |= data & mask;
171 hdmi_writeb(hdmi, val, reg);
172}
173
Andy Yanb21f4b62014-12-05 14:26:31 +0800174static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
Andy Yanb5878332014-12-05 14:23:52 +0800175 u8 shift, u8 mask)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200176{
Russell King812bc612013-11-04 12:42:02 +0000177 hdmi_modb(hdmi, data << shift, mask, reg);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200178}
179
Andy Yanb21f4b62014-12-05 14:26:31 +0800180static void hdmi_set_clock_regenerator_n(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200181 unsigned int value)
182{
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200183 hdmi_writeb(hdmi, value & 0xff, HDMI_AUD_N1);
184 hdmi_writeb(hdmi, (value >> 8) & 0xff, HDMI_AUD_N2);
185 hdmi_writeb(hdmi, (value >> 16) & 0x0f, HDMI_AUD_N3);
186
187 /* nshift factor = 0 */
Russell King812bc612013-11-04 12:42:02 +0000188 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200189}
190
Andy Yanb21f4b62014-12-05 14:26:31 +0800191static void hdmi_regenerate_cts(struct dw_hdmi *hdmi, unsigned int cts)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200192{
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200193 /* Must be set/cleared first */
Russell King812bc612013-11-04 12:42:02 +0000194 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200195
196 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
197 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
198 hdmi_writeb(hdmi, ((cts >> 16) & HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
199 HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
200}
201
202static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk,
203 unsigned int ratio)
204{
205 unsigned int n = (128 * freq) / 1000;
206
207 switch (freq) {
208 case 32000:
209 if (pixel_clk == 25170000)
210 n = (ratio == 150) ? 9152 : 4576;
211 else if (pixel_clk == 27020000)
212 n = (ratio == 150) ? 8192 : 4096;
213 else if (pixel_clk == 74170000 || pixel_clk == 148350000)
214 n = 11648;
215 else
216 n = 4096;
217 break;
218
219 case 44100:
220 if (pixel_clk == 25170000)
221 n = 7007;
222 else if (pixel_clk == 74170000)
223 n = 17836;
224 else if (pixel_clk == 148350000)
225 n = (ratio == 150) ? 17836 : 8918;
226 else
227 n = 6272;
228 break;
229
230 case 48000:
231 if (pixel_clk == 25170000)
232 n = (ratio == 150) ? 9152 : 6864;
233 else if (pixel_clk == 27020000)
234 n = (ratio == 150) ? 8192 : 6144;
235 else if (pixel_clk == 74170000)
236 n = 11648;
237 else if (pixel_clk == 148350000)
238 n = (ratio == 150) ? 11648 : 5824;
239 else
240 n = 6144;
241 break;
242
243 case 88200:
244 n = hdmi_compute_n(44100, pixel_clk, ratio) * 2;
245 break;
246
247 case 96000:
248 n = hdmi_compute_n(48000, pixel_clk, ratio) * 2;
249 break;
250
251 case 176400:
252 n = hdmi_compute_n(44100, pixel_clk, ratio) * 4;
253 break;
254
255 case 192000:
256 n = hdmi_compute_n(48000, pixel_clk, ratio) * 4;
257 break;
258
259 default:
260 break;
261 }
262
263 return n;
264}
265
266static unsigned int hdmi_compute_cts(unsigned int freq, unsigned long pixel_clk,
267 unsigned int ratio)
268{
269 unsigned int cts = 0;
270
271 pr_debug("%s: freq: %d pixel_clk: %ld ratio: %d\n", __func__, freq,
272 pixel_clk, ratio);
273
274 switch (freq) {
275 case 32000:
276 if (pixel_clk == 297000000) {
277 cts = 222750;
278 break;
279 }
280 case 48000:
281 case 96000:
282 case 192000:
283 switch (pixel_clk) {
284 case 25200000:
285 case 27000000:
286 case 54000000:
287 case 74250000:
288 case 148500000:
289 cts = pixel_clk / 1000;
290 break;
291 case 297000000:
292 cts = 247500;
293 break;
294 /*
295 * All other TMDS clocks are not supported by
296 * DWC_hdmi_tx. The TMDS clocks divided or
297 * multiplied by 1,001 coefficients are not
298 * supported.
299 */
300 default:
301 break;
302 }
303 break;
304 case 44100:
305 case 88200:
306 case 176400:
307 switch (pixel_clk) {
308 case 25200000:
309 cts = 28000;
310 break;
311 case 27000000:
312 cts = 30000;
313 break;
314 case 54000000:
315 cts = 60000;
316 break;
317 case 74250000:
318 cts = 82500;
319 break;
320 case 148500000:
321 cts = 165000;
322 break;
323 case 297000000:
324 cts = 247500;
325 break;
326 default:
327 break;
328 }
329 break;
330 default:
331 break;
332 }
333 if (ratio == 100)
334 return cts;
Catalina Mocanu7557b6e2014-09-24 14:27:36 -0700335 return (cts * ratio) / 100;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200336}
337
Andy Yanb21f4b62014-12-05 14:26:31 +0800338static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800339 unsigned long pixel_clk)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200340{
341 unsigned int clk_n, clk_cts;
342
Russell King40678382013-11-07 15:35:06 +0000343 clk_n = hdmi_compute_n(hdmi->sample_rate, pixel_clk,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200344 hdmi->ratio);
Russell King40678382013-11-07 15:35:06 +0000345 clk_cts = hdmi_compute_cts(hdmi->sample_rate, pixel_clk,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200346 hdmi->ratio);
347
348 if (!clk_cts) {
349 dev_dbg(hdmi->dev, "%s: pixel clock not supported: %lu\n",
Andy Yanb5878332014-12-05 14:23:52 +0800350 __func__, pixel_clk);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200351 return;
352 }
353
354 dev_dbg(hdmi->dev, "%s: samplerate=%d ratio=%d pixelclk=%lu N=%d cts=%d\n",
355 __func__, hdmi->sample_rate, hdmi->ratio,
Russell King40678382013-11-07 15:35:06 +0000356 pixel_clk, clk_n, clk_cts);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200357
358 hdmi_set_clock_regenerator_n(hdmi, clk_n);
359 hdmi_regenerate_cts(hdmi, clk_cts);
360}
361
Andy Yanb21f4b62014-12-05 14:26:31 +0800362static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200363{
Russell King40678382013-11-07 15:35:06 +0000364 hdmi_set_clk_regenerator(hdmi, 74250000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200365}
366
Andy Yanb21f4b62014-12-05 14:26:31 +0800367static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200368{
Russell King40678382013-11-07 15:35:06 +0000369 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mpixelclock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200370}
371
372/*
373 * this submodule is responsible for the video data synchronization.
374 * for example, for RGB 4:4:4 input, the data map is defined as
375 * pin{47~40} <==> R[7:0]
376 * pin{31~24} <==> G[7:0]
377 * pin{15~8} <==> B[7:0]
378 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800379static void hdmi_video_sample(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200380{
381 int color_format = 0;
382 u8 val;
383
384 if (hdmi->hdmi_data.enc_in_format == RGB) {
385 if (hdmi->hdmi_data.enc_color_depth == 8)
386 color_format = 0x01;
387 else if (hdmi->hdmi_data.enc_color_depth == 10)
388 color_format = 0x03;
389 else if (hdmi->hdmi_data.enc_color_depth == 12)
390 color_format = 0x05;
391 else if (hdmi->hdmi_data.enc_color_depth == 16)
392 color_format = 0x07;
393 else
394 return;
395 } else if (hdmi->hdmi_data.enc_in_format == YCBCR444) {
396 if (hdmi->hdmi_data.enc_color_depth == 8)
397 color_format = 0x09;
398 else if (hdmi->hdmi_data.enc_color_depth == 10)
399 color_format = 0x0B;
400 else if (hdmi->hdmi_data.enc_color_depth == 12)
401 color_format = 0x0D;
402 else if (hdmi->hdmi_data.enc_color_depth == 16)
403 color_format = 0x0F;
404 else
405 return;
406 } else if (hdmi->hdmi_data.enc_in_format == YCBCR422_8BITS) {
407 if (hdmi->hdmi_data.enc_color_depth == 8)
408 color_format = 0x16;
409 else if (hdmi->hdmi_data.enc_color_depth == 10)
410 color_format = 0x14;
411 else if (hdmi->hdmi_data.enc_color_depth == 12)
412 color_format = 0x12;
413 else
414 return;
415 }
416
417 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
418 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
419 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
420 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
421
422 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
423 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
424 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
425 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
426 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
427 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
428 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
429 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
430 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
431 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
432 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
433}
434
Andy Yanb21f4b62014-12-05 14:26:31 +0800435static int is_color_space_conversion(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200436{
Fabio Estevamba92b222014-02-06 10:12:03 -0200437 return hdmi->hdmi_data.enc_in_format != hdmi->hdmi_data.enc_out_format;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200438}
439
Andy Yanb21f4b62014-12-05 14:26:31 +0800440static int is_color_space_decimation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200441{
Fabio Estevamba92b222014-02-06 10:12:03 -0200442 if (hdmi->hdmi_data.enc_out_format != YCBCR422_8BITS)
443 return 0;
444 if (hdmi->hdmi_data.enc_in_format == RGB ||
445 hdmi->hdmi_data.enc_in_format == YCBCR444)
446 return 1;
447 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200448}
449
Andy Yanb21f4b62014-12-05 14:26:31 +0800450static int is_color_space_interpolation(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200451{
Fabio Estevamba92b222014-02-06 10:12:03 -0200452 if (hdmi->hdmi_data.enc_in_format != YCBCR422_8BITS)
453 return 0;
454 if (hdmi->hdmi_data.enc_out_format == RGB ||
455 hdmi->hdmi_data.enc_out_format == YCBCR444)
456 return 1;
457 return 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200458}
459
Andy Yanb21f4b62014-12-05 14:26:31 +0800460static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200461{
462 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
Russell Kingc082f9d2013-11-04 12:10:40 +0000463 unsigned i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200464 u32 csc_scale = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200465
466 if (is_color_space_conversion(hdmi)) {
467 if (hdmi->hdmi_data.enc_out_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200468 if (hdmi->hdmi_data.colorimetry ==
469 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200470 csc_coeff = &csc_coeff_rgb_out_eitu601;
471 else
472 csc_coeff = &csc_coeff_rgb_out_eitu709;
473 } else if (hdmi->hdmi_data.enc_in_format == RGB) {
Gulsah Kose256a38b2014-03-09 20:11:07 +0200474 if (hdmi->hdmi_data.colorimetry ==
475 HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200476 csc_coeff = &csc_coeff_rgb_in_eitu601;
477 else
478 csc_coeff = &csc_coeff_rgb_in_eitu709;
479 csc_scale = 0;
480 }
481 }
482
Russell Kingc082f9d2013-11-04 12:10:40 +0000483 /* The CSC registers are sequential, alternating MSB then LSB */
484 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
485 u16 coeff_a = (*csc_coeff)[0][i];
486 u16 coeff_b = (*csc_coeff)[1][i];
487 u16 coeff_c = (*csc_coeff)[2][i];
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200488
Andy Yanb5878332014-12-05 14:23:52 +0800489 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000490 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
491 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
492 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
Andy Yanb5878332014-12-05 14:23:52 +0800493 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
Russell Kingc082f9d2013-11-04 12:10:40 +0000494 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
495 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200496
Russell King812bc612013-11-04 12:42:02 +0000497 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
498 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200499}
500
Andy Yanb21f4b62014-12-05 14:26:31 +0800501static void hdmi_video_csc(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200502{
503 int color_depth = 0;
504 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
505 int decimation = 0;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200506
507 /* YCC422 interpolation to 444 mode */
508 if (is_color_space_interpolation(hdmi))
509 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
510 else if (is_color_space_decimation(hdmi))
511 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;
512
513 if (hdmi->hdmi_data.enc_color_depth == 8)
514 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
515 else if (hdmi->hdmi_data.enc_color_depth == 10)
516 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
517 else if (hdmi->hdmi_data.enc_color_depth == 12)
518 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
519 else if (hdmi->hdmi_data.enc_color_depth == 16)
520 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
521 else
522 return;
523
524 /* Configure the CSC registers */
525 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
Russell King812bc612013-11-04 12:42:02 +0000526 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
527 HDMI_CSC_SCALE);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200528
Andy Yanb21f4b62014-12-05 14:26:31 +0800529 dw_hdmi_update_csc_coeffs(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200530}
531
532/*
533 * HDMI video packetizer is used to packetize the data.
534 * for example, if input is YCC422 mode or repeater is used,
535 * data should be repacked this module can be bypassed.
536 */
Andy Yanb21f4b62014-12-05 14:26:31 +0800537static void hdmi_video_packetize(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200538{
539 unsigned int color_depth = 0;
540 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
541 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
542 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
Russell Kingbebdf662013-11-04 12:55:30 +0000543 u8 val, vp_conf;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200544
Andy Yanb5878332014-12-05 14:23:52 +0800545 if (hdmi_data->enc_out_format == RGB ||
546 hdmi_data->enc_out_format == YCBCR444) {
547 if (!hdmi_data->enc_color_depth) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200548 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800549 } else if (hdmi_data->enc_color_depth == 8) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200550 color_depth = 4;
551 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
Andy Yanb5878332014-12-05 14:23:52 +0800552 } else if (hdmi_data->enc_color_depth == 10) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200553 color_depth = 5;
Andy Yanb5878332014-12-05 14:23:52 +0800554 } else if (hdmi_data->enc_color_depth == 12) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200555 color_depth = 6;
Andy Yanb5878332014-12-05 14:23:52 +0800556 } else if (hdmi_data->enc_color_depth == 16) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200557 color_depth = 7;
Andy Yanb5878332014-12-05 14:23:52 +0800558 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200559 return;
Andy Yanb5878332014-12-05 14:23:52 +0800560 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200561 } else if (hdmi_data->enc_out_format == YCBCR422_8BITS) {
562 if (!hdmi_data->enc_color_depth ||
563 hdmi_data->enc_color_depth == 8)
564 remap_size = HDMI_VP_REMAP_YCC422_16bit;
565 else if (hdmi_data->enc_color_depth == 10)
566 remap_size = HDMI_VP_REMAP_YCC422_20bit;
567 else if (hdmi_data->enc_color_depth == 12)
568 remap_size = HDMI_VP_REMAP_YCC422_24bit;
569 else
570 return;
571 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
Andy Yanb5878332014-12-05 14:23:52 +0800572 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200573 return;
Andy Yanb5878332014-12-05 14:23:52 +0800574 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200575
576 /* set the packetizer registers */
577 val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
578 HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |
579 ((hdmi_data->pix_repet_factor <<
580 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &
581 HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);
582 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
583
Russell King812bc612013-11-04 12:42:02 +0000584 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
585 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200586
587 /* Data from pixel repeater block */
588 if (hdmi_data->pix_repet_factor > 1) {
Russell Kingbebdf662013-11-04 12:55:30 +0000589 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
590 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200591 } else { /* data from packetizer block */
Russell Kingbebdf662013-11-04 12:55:30 +0000592 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
593 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200594 }
595
Russell Kingbebdf662013-11-04 12:55:30 +0000596 hdmi_modb(hdmi, vp_conf,
597 HDMI_VP_CONF_PR_EN_MASK |
598 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
599
Russell King812bc612013-11-04 12:42:02 +0000600 hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,
601 HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200602
603 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
604
605 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
Russell Kingbebdf662013-11-04 12:55:30 +0000606 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
607 HDMI_VP_CONF_PP_EN_ENABLE |
608 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200609 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
Russell Kingbebdf662013-11-04 12:55:30 +0000610 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
611 HDMI_VP_CONF_PP_EN_DISABLE |
612 HDMI_VP_CONF_YCC422_EN_ENABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200613 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
Russell Kingbebdf662013-11-04 12:55:30 +0000614 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
615 HDMI_VP_CONF_PP_EN_DISABLE |
616 HDMI_VP_CONF_YCC422_EN_DISABLE;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200617 } else {
618 return;
619 }
620
Russell Kingbebdf662013-11-04 12:55:30 +0000621 hdmi_modb(hdmi, vp_conf,
622 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
623 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200624
Russell King812bc612013-11-04 12:42:02 +0000625 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
626 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
627 HDMI_VP_STUFF_PP_STUFFING_MASK |
628 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200629
Russell King812bc612013-11-04 12:42:02 +0000630 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
631 HDMI_VP_CONF);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200632}
633
Andy Yanb21f4b62014-12-05 14:26:31 +0800634static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800635 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200636{
Russell King812bc612013-11-04 12:42:02 +0000637 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
638 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200639}
640
Andy Yanb21f4b62014-12-05 14:26:31 +0800641static inline void hdmi_phy_test_enable(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800642 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200643{
Russell King812bc612013-11-04 12:42:02 +0000644 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTEN_OFFSET,
645 HDMI_PHY_TST0_TSTEN_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200646}
647
Andy Yanb21f4b62014-12-05 14:26:31 +0800648static inline void hdmi_phy_test_clock(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800649 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200650{
Russell King812bc612013-11-04 12:42:02 +0000651 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLK_OFFSET,
652 HDMI_PHY_TST0_TSTCLK_MASK, HDMI_PHY_TST0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200653}
654
Andy Yanb21f4b62014-12-05 14:26:31 +0800655static inline void hdmi_phy_test_din(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800656 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200657{
658 hdmi_writeb(hdmi, bit, HDMI_PHY_TST1);
659}
660
Andy Yanb21f4b62014-12-05 14:26:31 +0800661static inline void hdmi_phy_test_dout(struct dw_hdmi *hdmi,
Andy Yanb5878332014-12-05 14:23:52 +0800662 unsigned char bit)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200663{
664 hdmi_writeb(hdmi, bit, HDMI_PHY_TST2);
665}
666
Andy Yanb21f4b62014-12-05 14:26:31 +0800667static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200668{
Andy Yana4d3b8b2014-12-05 14:31:09 +0800669 u32 val;
670
671 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200672 if (msec-- == 0)
673 return false;
Emil Renner Berthing0e6bcf32014-03-30 00:21:21 +0100674 udelay(1000);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200675 }
Andy Yana4d3b8b2014-12-05 14:31:09 +0800676 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
677
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200678 return true;
679}
680
Andy Yanb21f4b62014-12-05 14:26:31 +0800681static void __hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800682 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200683{
684 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
685 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
686 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
Andy Yanb5878332014-12-05 14:23:52 +0800687 HDMI_PHY_I2CM_DATAO_1_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200688 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
Andy Yanb5878332014-12-05 14:23:52 +0800689 HDMI_PHY_I2CM_DATAO_0_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200690 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
Andy Yanb5878332014-12-05 14:23:52 +0800691 HDMI_PHY_I2CM_OPERATION_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200692 hdmi_phy_wait_i2c_done(hdmi, 1000);
693}
694
Andy Yanb21f4b62014-12-05 14:26:31 +0800695static int hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
Andy Yanb5878332014-12-05 14:23:52 +0800696 unsigned char addr)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200697{
698 __hdmi_phy_i2c_write(hdmi, data, addr);
699 return 0;
700}
701
Andy Yanb21f4b62014-12-05 14:26:31 +0800702static void dw_hdmi_phy_enable_power(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200703{
704 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
705 HDMI_PHY_CONF0_PDZ_OFFSET,
706 HDMI_PHY_CONF0_PDZ_MASK);
707}
708
Andy Yanb21f4b62014-12-05 14:26:31 +0800709static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200710{
711 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
712 HDMI_PHY_CONF0_ENTMDS_OFFSET,
713 HDMI_PHY_CONF0_ENTMDS_MASK);
714}
715
Andy Yand346c142014-12-05 14:31:53 +0800716static void dw_hdmi_phy_enable_spare(struct dw_hdmi *hdmi, u8 enable)
717{
718 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
719 HDMI_PHY_CONF0_SPARECTRL_OFFSET,
720 HDMI_PHY_CONF0_SPARECTRL_MASK);
721}
722
Andy Yanb21f4b62014-12-05 14:26:31 +0800723static void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200724{
725 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
726 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
727 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
728}
729
Andy Yanb21f4b62014-12-05 14:26:31 +0800730static void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200731{
732 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
733 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
734 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
735}
736
Andy Yanb21f4b62014-12-05 14:26:31 +0800737static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200738{
739 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
740 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
741 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
742}
743
Andy Yanb21f4b62014-12-05 14:26:31 +0800744static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200745{
746 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
747 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
748 HDMI_PHY_CONF0_SELDIPIF_MASK);
749}
750
Andy Yanb21f4b62014-12-05 14:26:31 +0800751static int hdmi_phy_configure(struct dw_hdmi *hdmi, unsigned char prep,
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200752 unsigned char res, int cscon)
753{
Russell King3e46f152013-11-04 11:24:00 +0000754 unsigned res_idx, i;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200755 u8 val, msec;
Andy Yanb21f4b62014-12-05 14:26:31 +0800756 const struct dw_hdmi_mpll_config *mpll_config =
757 hdmi->plat_data->mpll_cfg;
758 const struct dw_hdmi_curr_ctrl *curr_ctrl = hdmi->plat_data->cur_ctr;
759 const struct dw_hdmi_sym_term *sym_term = hdmi->plat_data->sym_term;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200760
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200761 if (prep)
762 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000763
764 switch (res) {
765 case 0: /* color resolution 0 is 8 bit colour depth */
766 case 8:
Andy Yanb21f4b62014-12-05 14:26:31 +0800767 res_idx = DW_HDMI_RES_8;
Russell King3e46f152013-11-04 11:24:00 +0000768 break;
769 case 10:
Andy Yanb21f4b62014-12-05 14:26:31 +0800770 res_idx = DW_HDMI_RES_10;
Russell King3e46f152013-11-04 11:24:00 +0000771 break;
772 case 12:
Andy Yanb21f4b62014-12-05 14:26:31 +0800773 res_idx = DW_HDMI_RES_12;
Russell King3e46f152013-11-04 11:24:00 +0000774 break;
775 default:
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200776 return -EINVAL;
Russell King3e46f152013-11-04 11:24:00 +0000777 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200778
779 /* Enable csc path */
780 if (cscon)
781 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH;
782 else
783 val = HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS;
784
785 hdmi_writeb(hdmi, val, HDMI_MC_FLOWCTRL);
786
787 /* gen2 tx power off */
Andy Yanb21f4b62014-12-05 14:26:31 +0800788 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200789
790 /* gen2 pddq */
Andy Yanb21f4b62014-12-05 14:26:31 +0800791 dw_hdmi_phy_gen2_pddq(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200792
793 /* PHY reset */
794 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_DEASSERT, HDMI_MC_PHYRSTZ);
795 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_ASSERT, HDMI_MC_PHYRSTZ);
796
797 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
798
799 hdmi_phy_test_clear(hdmi, 1);
800 hdmi_writeb(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2,
Andy Yanb5878332014-12-05 14:23:52 +0800801 HDMI_PHY_I2CM_SLAVE_ADDR);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200802 hdmi_phy_test_clear(hdmi, 0);
803
Russell King3e46f152013-11-04 11:24:00 +0000804 /* PLL/MPLL Cfg - always match on final entry */
Andy Yanaaa757a2014-12-05 14:25:50 +0800805 for (i = 0; mpll_config[i].mpixelclock != (~0UL); i++)
Russell King3e46f152013-11-04 11:24:00 +0000806 if (hdmi->hdmi_data.video_mode.mpixelclock <=
807 mpll_config[i].mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200808 break;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200809
Russell King3e46f152013-11-04 11:24:00 +0000810 hdmi_phy_i2c_write(hdmi, mpll_config[i].res[res_idx].cpce, 0x06);
811 hdmi_phy_i2c_write(hdmi, mpll_config[i].res[res_idx].gmp, 0x15);
812
Andy Yanaaa757a2014-12-05 14:25:50 +0800813 for (i = 0; curr_ctrl[i].mpixelclock != (~0UL); i++)
Russell King3e46f152013-11-04 11:24:00 +0000814 if (hdmi->hdmi_data.video_mode.mpixelclock <=
815 curr_ctrl[i].mpixelclock)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200816 break;
Russell King3e46f152013-11-04 11:24:00 +0000817
Andy Yanaaa757a2014-12-05 14:25:50 +0800818 if (curr_ctrl[i].mpixelclock == (~0UL)) {
Andy Yanb5878332014-12-05 14:23:52 +0800819 dev_err(hdmi->dev, "Pixel clock %d - unsupported by HDMI\n",
820 hdmi->hdmi_data.video_mode.mpixelclock);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200821 return -EINVAL;
822 }
823
Russell King3e46f152013-11-04 11:24:00 +0000824 /* CURRCTRL */
825 hdmi_phy_i2c_write(hdmi, curr_ctrl[i].curr[res_idx], 0x10);
826
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200827 hdmi_phy_i2c_write(hdmi, 0x0000, 0x13); /* PLLPHBYCTRL */
828 hdmi_phy_i2c_write(hdmi, 0x0006, 0x17);
Andy Yanaaa757a2014-12-05 14:25:50 +0800829
830 for (i = 0; sym_term[i].mpixelclock != (~0UL); i++)
831 if (hdmi->hdmi_data.video_mode.mpixelclock <=
832 sym_term[i].mpixelclock)
833 break;
834
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200835 /* RESISTANCE TERM 133Ohm Cfg */
Andy Yanaaa757a2014-12-05 14:25:50 +0800836 hdmi_phy_i2c_write(hdmi, sym_term[i].term, 0x19); /* TXTERM */
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200837 /* PREEMP Cgf 0.00 */
Andy Yanaaa757a2014-12-05 14:25:50 +0800838 hdmi_phy_i2c_write(hdmi, sym_term[i].sym_ctr, 0x09); /* CKSYMTXCTRL */
839
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200840 /* TX/CK LVL 10 */
841 hdmi_phy_i2c_write(hdmi, 0x01ad, 0x0E); /* VLEVCTRL */
842 /* REMOVE CLK TERM */
843 hdmi_phy_i2c_write(hdmi, 0x8000, 0x05); /* CKCALCTRL */
844
Andy Yanb21f4b62014-12-05 14:26:31 +0800845 dw_hdmi_phy_enable_power(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200846
847 /* toggle TMDS enable */
Andy Yanb21f4b62014-12-05 14:26:31 +0800848 dw_hdmi_phy_enable_tmds(hdmi, 0);
849 dw_hdmi_phy_enable_tmds(hdmi, 1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200850
851 /* gen2 tx power on */
Andy Yanb21f4b62014-12-05 14:26:31 +0800852 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
853 dw_hdmi_phy_gen2_pddq(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200854
Andy Yan12b9f202015-01-07 15:48:27 +0800855 if (hdmi->dev_type == RK3288_HDMI)
856 dw_hdmi_phy_enable_spare(hdmi, 1);
857
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200858 /*Wait for PHY PLL lock */
859 msec = 5;
860 do {
861 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
862 if (!val)
863 break;
864
865 if (msec == 0) {
866 dev_err(hdmi->dev, "PHY PLL not locked\n");
867 return -ETIMEDOUT;
868 }
869
870 udelay(1000);
871 msec--;
872 } while (1);
873
874 return 0;
875}
876
Andy Yanb21f4b62014-12-05 14:26:31 +0800877static int dw_hdmi_phy_init(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200878{
879 int i, ret;
880 bool cscon = false;
881
882 /*check csc whether needed activated in HDMI mode */
883 cscon = (is_color_space_conversion(hdmi) &&
884 !hdmi->hdmi_data.video_mode.mdvi);
885
886 /* HDMI Phy spec says to do the phy initialization sequence twice */
887 for (i = 0; i < 2; i++) {
Andy Yanb21f4b62014-12-05 14:26:31 +0800888 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
889 dw_hdmi_phy_sel_interface_control(hdmi, 0);
890 dw_hdmi_phy_enable_tmds(hdmi, 0);
891 dw_hdmi_phy_enable_power(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200892
893 /* Enable CSC */
894 ret = hdmi_phy_configure(hdmi, 0, 8, cscon);
895 if (ret)
896 return ret;
897 }
898
899 hdmi->phy_enabled = true;
900 return 0;
901}
902
Andy Yanb21f4b62014-12-05 14:26:31 +0800903static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200904{
Russell King812bc612013-11-04 12:42:02 +0000905 u8 de;
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200906
907 if (hdmi->hdmi_data.video_mode.mdataenablepolarity)
908 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;
909 else
910 de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
911
912 /* disable rx detect */
Russell King812bc612013-11-04 12:42:02 +0000913 hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,
914 HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200915
Russell King812bc612013-11-04 12:42:02 +0000916 hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200917
Russell King812bc612013-11-04 12:42:02 +0000918 hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,
919 HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200920}
921
Andy Yanb21f4b62014-12-05 14:26:31 +0800922static void hdmi_config_AVI(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200923{
924 u8 val, pix_fmt, under_scan;
925 u8 act_ratio, coded_ratio, colorimetry, ext_colorimetry;
926 bool aspect_16_9;
927
928 aspect_16_9 = false; /* FIXME */
929
930 /* AVI Data Byte 1 */
931 if (hdmi->hdmi_data.enc_out_format == YCBCR444)
932 pix_fmt = HDMI_FC_AVICONF0_PIX_FMT_YCBCR444;
933 else if (hdmi->hdmi_data.enc_out_format == YCBCR422_8BITS)
934 pix_fmt = HDMI_FC_AVICONF0_PIX_FMT_YCBCR422;
935 else
936 pix_fmt = HDMI_FC_AVICONF0_PIX_FMT_RGB;
937
938 under_scan = HDMI_FC_AVICONF0_SCAN_INFO_NODATA;
939
940 /*
941 * Active format identification data is present in the AVI InfoFrame.
942 * Under scan info, no bar data
943 */
944 val = pix_fmt | under_scan |
945 HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT |
946 HDMI_FC_AVICONF0_BAR_DATA_NO_DATA;
947
948 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
949
950 /* AVI Data Byte 2 -Set the Aspect Ratio */
951 if (aspect_16_9) {
952 act_ratio = HDMI_FC_AVICONF1_ACTIVE_ASPECT_RATIO_16_9;
953 coded_ratio = HDMI_FC_AVICONF1_CODED_ASPECT_RATIO_16_9;
954 } else {
955 act_ratio = HDMI_FC_AVICONF1_ACTIVE_ASPECT_RATIO_4_3;
956 coded_ratio = HDMI_FC_AVICONF1_CODED_ASPECT_RATIO_4_3;
957 }
958
959 /* Set up colorimetry */
960 if (hdmi->hdmi_data.enc_out_format == XVYCC444) {
961 colorimetry = HDMI_FC_AVICONF1_COLORIMETRY_EXTENDED_INFO;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530962 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200963 ext_colorimetry =
964 HDMI_FC_AVICONF2_EXT_COLORIMETRY_XVYCC601;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530965 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200966 ext_colorimetry =
967 HDMI_FC_AVICONF2_EXT_COLORIMETRY_XVYCC709;
968 } else if (hdmi->hdmi_data.enc_out_format != RGB) {
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530969 if (hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_601)
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200970 colorimetry = HDMI_FC_AVICONF1_COLORIMETRY_SMPTE;
Sachin Kamat5a819ed2014-01-28 10:33:16 +0530971 else /*hdmi->hdmi_data.colorimetry == HDMI_COLORIMETRY_ITU_709*/
Fabio Estevam9aaf8802013-11-29 08:46:32 -0200972 colorimetry = HDMI_FC_AVICONF1_COLORIMETRY_ITUR;
973 ext_colorimetry = HDMI_FC_AVICONF2_EXT_COLORIMETRY_XVYCC601;
974 } else { /* Carries no data */
975 colorimetry = HDMI_FC_AVICONF1_COLORIMETRY_NO_DATA;
976 ext_colorimetry = HDMI_FC_AVICONF2_EXT_COLORIMETRY_XVYCC601;
977 }
978
979 val = colorimetry | coded_ratio | act_ratio;
980 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
981
982 /* AVI Data Byte 3 */
983 val = HDMI_FC_AVICONF2_IT_CONTENT_NO_DATA | ext_colorimetry |
984 HDMI_FC_AVICONF2_RGB_QUANT_DEFAULT |
985 HDMI_FC_AVICONF2_SCALING_NONE;
986 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
987
988 /* AVI Data Byte 4 */
989 hdmi_writeb(hdmi, hdmi->vic, HDMI_FC_AVIVID);
990
991 /* AVI Data Byte 5- set up input and output pixel repetition */
992 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
993 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
994 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
995 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
996 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
997 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
998 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
999
1000 /* IT Content and quantization range = don't care */
1001 val = HDMI_FC_AVICONF3_IT_CONTENT_TYPE_GRAPHICS |
1002 HDMI_FC_AVICONF3_QUANT_RANGE_LIMITED;
1003 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
1004
1005 /* AVI Data Bytes 6-13 */
1006 hdmi_writeb(hdmi, 0, HDMI_FC_AVIETB0);
1007 hdmi_writeb(hdmi, 0, HDMI_FC_AVIETB1);
1008 hdmi_writeb(hdmi, 0, HDMI_FC_AVISBB0);
1009 hdmi_writeb(hdmi, 0, HDMI_FC_AVISBB1);
1010 hdmi_writeb(hdmi, 0, HDMI_FC_AVIELB0);
1011 hdmi_writeb(hdmi, 0, HDMI_FC_AVIELB1);
1012 hdmi_writeb(hdmi, 0, HDMI_FC_AVISRB0);
1013 hdmi_writeb(hdmi, 0, HDMI_FC_AVISRB1);
1014}
1015
Andy Yanb21f4b62014-12-05 14:26:31 +08001016static void hdmi_av_composer(struct dw_hdmi *hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001017 const struct drm_display_mode *mode)
1018{
1019 u8 inv_val;
1020 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
1021 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
1022
1023 vmode->mhsyncpolarity = !!(mode->flags & DRM_MODE_FLAG_PHSYNC);
1024 vmode->mvsyncpolarity = !!(mode->flags & DRM_MODE_FLAG_PVSYNC);
1025 vmode->minterlaced = !!(mode->flags & DRM_MODE_FLAG_INTERLACE);
1026 vmode->mpixelclock = mode->clock * 1000;
1027
1028 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
1029
1030 /* Set up HDMI_FC_INVIDCONF */
1031 inv_val = (hdmi->hdmi_data.hdcp_enable ?
1032 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :
1033 HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);
1034
1035 inv_val |= (vmode->mvsyncpolarity ?
1036 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
1037 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW);
1038
1039 inv_val |= (vmode->mhsyncpolarity ?
1040 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
1041 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW);
1042
1043 inv_val |= (vmode->mdataenablepolarity ?
1044 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
1045 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
1046
1047 if (hdmi->vic == 39)
1048 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
1049 else
1050 inv_val |= (vmode->minterlaced ?
1051 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
1052 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW);
1053
1054 inv_val |= (vmode->minterlaced ?
1055 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
1056 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE);
1057
1058 inv_val |= (vmode->mdvi ?
1059 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE :
1060 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE);
1061
1062 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
1063
1064 /* Set up horizontal active pixel width */
1065 hdmi_writeb(hdmi, mode->hdisplay >> 8, HDMI_FC_INHACTV1);
1066 hdmi_writeb(hdmi, mode->hdisplay, HDMI_FC_INHACTV0);
1067
1068 /* Set up vertical active lines */
1069 hdmi_writeb(hdmi, mode->vdisplay >> 8, HDMI_FC_INVACTV1);
1070 hdmi_writeb(hdmi, mode->vdisplay, HDMI_FC_INVACTV0);
1071
1072 /* Set up horizontal blanking pixel region width */
1073 hblank = mode->htotal - mode->hdisplay;
1074 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
1075 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
1076
1077 /* Set up vertical blanking pixel region width */
1078 vblank = mode->vtotal - mode->vdisplay;
1079 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
1080
1081 /* Set up HSYNC active edge delay width (in pixel clks) */
1082 h_de_hs = mode->hsync_start - mode->hdisplay;
1083 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
1084 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
1085
1086 /* Set up VSYNC active edge delay (in lines) */
1087 v_de_vs = mode->vsync_start - mode->vdisplay;
1088 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
1089
1090 /* Set up HSYNC active pulse width (in pixel clks) */
1091 hsync_len = mode->hsync_end - mode->hsync_start;
1092 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
1093 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
1094
1095 /* Set up VSYNC active edge delay (in lines) */
1096 vsync_len = mode->vsync_end - mode->vsync_start;
1097 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
1098}
1099
Andy Yanb21f4b62014-12-05 14:26:31 +08001100static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001101{
1102 if (!hdmi->phy_enabled)
1103 return;
1104
Andy Yanb21f4b62014-12-05 14:26:31 +08001105 dw_hdmi_phy_enable_tmds(hdmi, 0);
1106 dw_hdmi_phy_enable_power(hdmi, 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001107
1108 hdmi->phy_enabled = false;
1109}
1110
1111/* HDMI Initialization Step B.4 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001112static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001113{
1114 u8 clkdis;
1115
1116 /* control period minimum duration */
1117 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
1118 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
1119 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
1120
1121 /* Set to fill TMDS data channels */
1122 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
1123 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
1124 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
1125
1126 /* Enable pixel clock and tmds data path */
1127 clkdis = 0x7F;
1128 clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
1129 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1130
1131 clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
1132 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1133
1134 /* Enable csc path */
1135 if (is_color_space_conversion(hdmi)) {
1136 clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
1137 hdmi_writeb(hdmi, clkdis, HDMI_MC_CLKDIS);
1138 }
1139}
1140
Andy Yanb21f4b62014-12-05 14:26:31 +08001141static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001142{
Russell King812bc612013-11-04 12:42:02 +00001143 hdmi_modb(hdmi, 0, HDMI_MC_CLKDIS_AUDCLK_DISABLE, HDMI_MC_CLKDIS);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001144}
1145
1146/* Workaround to clear the overflow condition */
Andy Yanb21f4b62014-12-05 14:26:31 +08001147static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001148{
1149 int count;
1150 u8 val;
1151
1152 /* TMDS software reset */
1153 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
1154
1155 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
1156 if (hdmi->dev_type == IMX6DL_HDMI) {
1157 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1158 return;
1159 }
1160
1161 for (count = 0; count < 4; count++)
1162 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
1163}
1164
Andy Yanb21f4b62014-12-05 14:26:31 +08001165static void hdmi_enable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001166{
1167 hdmi_writeb(hdmi, 0, HDMI_FC_MASK2);
1168 hdmi_writeb(hdmi, 0, HDMI_IH_MUTE_FC_STAT2);
1169}
1170
Andy Yanb21f4b62014-12-05 14:26:31 +08001171static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001172{
1173 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
1174 HDMI_IH_MUTE_FC_STAT2);
1175}
1176
Andy Yanb21f4b62014-12-05 14:26:31 +08001177static int dw_hdmi_setup(struct dw_hdmi *hdmi, struct drm_display_mode *mode)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001178{
1179 int ret;
1180
1181 hdmi_disable_overflow_interrupts(hdmi);
1182
1183 hdmi->vic = drm_match_cea_mode(mode);
1184
1185 if (!hdmi->vic) {
1186 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
1187 hdmi->hdmi_data.video_mode.mdvi = true;
1188 } else {
1189 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
1190 hdmi->hdmi_data.video_mode.mdvi = false;
1191 }
1192
1193 if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
Andy Yanb5878332014-12-05 14:23:52 +08001194 (hdmi->vic == 21) || (hdmi->vic == 22) ||
1195 (hdmi->vic == 2) || (hdmi->vic == 3) ||
1196 (hdmi->vic == 17) || (hdmi->vic == 18))
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301197 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_601;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001198 else
Sachin Kamat5a819ed2014-01-28 10:33:16 +05301199 hdmi->hdmi_data.colorimetry = HDMI_COLORIMETRY_ITU_709;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001200
1201 if ((hdmi->vic == 10) || (hdmi->vic == 11) ||
Andy Yanb5878332014-12-05 14:23:52 +08001202 (hdmi->vic == 12) || (hdmi->vic == 13) ||
1203 (hdmi->vic == 14) || (hdmi->vic == 15) ||
1204 (hdmi->vic == 25) || (hdmi->vic == 26) ||
1205 (hdmi->vic == 27) || (hdmi->vic == 28) ||
1206 (hdmi->vic == 29) || (hdmi->vic == 30) ||
1207 (hdmi->vic == 35) || (hdmi->vic == 36) ||
1208 (hdmi->vic == 37) || (hdmi->vic == 38))
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001209 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 1;
1210 else
1211 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
1212
1213 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
1214
1215 /* TODO: Get input format from IPU (via FB driver interface) */
1216 hdmi->hdmi_data.enc_in_format = RGB;
1217
1218 hdmi->hdmi_data.enc_out_format = RGB;
1219
1220 hdmi->hdmi_data.enc_color_depth = 8;
1221 hdmi->hdmi_data.pix_repet_factor = 0;
1222 hdmi->hdmi_data.hdcp_enable = 0;
1223 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
1224
1225 /* HDMI Initialization Step B.1 */
1226 hdmi_av_composer(hdmi, mode);
1227
1228 /* HDMI Initializateion Step B.2 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001229 ret = dw_hdmi_phy_init(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001230 if (ret)
1231 return ret;
1232
1233 /* HDMI Initialization Step B.3 */
Andy Yanb21f4b62014-12-05 14:26:31 +08001234 dw_hdmi_enable_video_path(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001235
1236 /* not for DVI mode */
Andy Yanb5878332014-12-05 14:23:52 +08001237 if (hdmi->hdmi_data.video_mode.mdvi) {
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001238 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
Andy Yanb5878332014-12-05 14:23:52 +08001239 } else {
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001240 dev_dbg(hdmi->dev, "%s CEA mode\n", __func__);
1241
1242 /* HDMI Initialization Step E - Configure audio */
1243 hdmi_clk_regenerator_update_pixel_clock(hdmi);
1244 hdmi_enable_audio_clk(hdmi);
1245
1246 /* HDMI Initialization Step F - Configure AVI InfoFrame */
1247 hdmi_config_AVI(hdmi);
1248 }
1249
1250 hdmi_video_packetize(hdmi);
1251 hdmi_video_csc(hdmi);
1252 hdmi_video_sample(hdmi);
1253 hdmi_tx_hdcp_config(hdmi);
1254
Andy Yanb21f4b62014-12-05 14:26:31 +08001255 dw_hdmi_clear_overflow(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001256 if (hdmi->cable_plugin && !hdmi->hdmi_data.video_mode.mdvi)
1257 hdmi_enable_overflow_interrupts(hdmi);
1258
1259 return 0;
1260}
1261
1262/* Wait until we are registered to enable interrupts */
Andy Yanb21f4b62014-12-05 14:26:31 +08001263static int dw_hdmi_fb_registered(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001264{
1265 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
1266 HDMI_PHY_I2CM_INT_ADDR);
1267
1268 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
1269 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
1270 HDMI_PHY_I2CM_CTLINT_ADDR);
1271
1272 /* enable cable hot plug irq */
1273 hdmi_writeb(hdmi, (u8)~HDMI_PHY_HPD, HDMI_PHY_MASK0);
1274
1275 /* Clear Hotplug interrupts */
1276 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD, HDMI_IH_PHY_STAT0);
1277
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001278 return 0;
1279}
1280
Andy Yanb21f4b62014-12-05 14:26:31 +08001281static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001282{
1283 u8 ih_mute;
1284
1285 /*
1286 * Boot up defaults are:
1287 * HDMI_IH_MUTE = 0x03 (disabled)
1288 * HDMI_IH_MUTE_* = 0x00 (enabled)
1289 *
1290 * Disable top level interrupt bits in HDMI block
1291 */
1292 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
1293 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1294 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
1295
1296 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1297
1298 /* by default mask all interrupts */
1299 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
1300 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
1301 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
1302 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
1303 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
1304 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
1305 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
1306 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
1307 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
1308 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
1309 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
1310 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
1311 hdmi_writeb(hdmi, 0xff, HDMI_CEC_MASK);
1312 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
1313 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
1314
1315 /* Disable interrupts in the IH_MUTE_* registers */
1316 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
1317 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
1318 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
1319 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
1320 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
1321 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
1322 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
1323 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
1324 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
1325 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
1326
1327 /* Enable top level interrupt bits in HDMI block */
1328 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
1329 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
1330 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
1331}
1332
Andy Yanb21f4b62014-12-05 14:26:31 +08001333static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001334{
Andy Yanb21f4b62014-12-05 14:26:31 +08001335 dw_hdmi_setup(hdmi, &hdmi->previous_mode);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001336}
1337
Andy Yanb21f4b62014-12-05 14:26:31 +08001338static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001339{
Andy Yanb21f4b62014-12-05 14:26:31 +08001340 dw_hdmi_phy_disable(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001341}
1342
Andy Yanb21f4b62014-12-05 14:26:31 +08001343static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
1344 struct drm_display_mode *mode,
1345 struct drm_display_mode *adjusted_mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001346{
Andy Yanb21f4b62014-12-05 14:26:31 +08001347 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001348
Andy Yanb21f4b62014-12-05 14:26:31 +08001349 dw_hdmi_setup(hdmi, mode);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001350
1351 /* Store the display mode for plugin/DKMS poweron events */
1352 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
1353}
1354
Andy Yanb21f4b62014-12-05 14:26:31 +08001355static bool dw_hdmi_bridge_mode_fixup(struct drm_bridge *bridge,
1356 const struct drm_display_mode *mode,
1357 struct drm_display_mode *adjusted_mode)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001358{
1359 return true;
1360}
1361
Andy Yanb21f4b62014-12-05 14:26:31 +08001362static void dw_hdmi_bridge_disable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001363{
Andy Yanb21f4b62014-12-05 14:26:31 +08001364 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001365
Andy Yanb21f4b62014-12-05 14:26:31 +08001366 dw_hdmi_poweroff(hdmi);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001367}
1368
Andy Yanb21f4b62014-12-05 14:26:31 +08001369static void dw_hdmi_bridge_enable(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001370{
Andy Yanb21f4b62014-12-05 14:26:31 +08001371 struct dw_hdmi *hdmi = bridge->driver_private;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001372
Andy Yanb21f4b62014-12-05 14:26:31 +08001373 dw_hdmi_poweron(hdmi);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001374}
1375
Andy Yanb21f4b62014-12-05 14:26:31 +08001376static void dw_hdmi_bridge_destroy(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001377{
1378 drm_bridge_cleanup(bridge);
1379 kfree(bridge);
1380}
1381
Andy Yanb21f4b62014-12-05 14:26:31 +08001382static void dw_hdmi_bridge_nop(struct drm_bridge *bridge)
Andy Yan3d1b35a2014-12-05 14:25:05 +08001383{
1384 /* do nothing */
1385}
1386
Andy Yanb21f4b62014-12-05 14:26:31 +08001387static enum drm_connector_status
1388dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001389{
Andy Yanb21f4b62014-12-05 14:26:31 +08001390 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Russell Kingd94905e2013-11-03 22:23:24 +00001391 connector);
Russell King98dbead2014-04-18 10:46:45 +01001392
1393 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
1394 connector_status_connected : connector_status_disconnected;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001395}
1396
Andy Yanb21f4b62014-12-05 14:26:31 +08001397static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001398{
Andy Yanb21f4b62014-12-05 14:26:31 +08001399 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001400 connector);
1401 struct edid *edid;
1402 int ret;
1403
1404 if (!hdmi->ddc)
1405 return 0;
1406
1407 edid = drm_get_edid(connector, hdmi->ddc);
1408 if (edid) {
1409 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
1410 edid->width_cm, edid->height_cm);
1411
1412 drm_mode_connector_update_edid_property(connector, edid);
1413 ret = drm_add_edid_modes(connector, edid);
1414 kfree(edid);
1415 } else {
1416 dev_dbg(hdmi->dev, "failed to get edid\n");
1417 }
1418
1419 return 0;
1420}
1421
Andy Yan632d0352014-12-05 14:30:21 +08001422static enum drm_mode_status
1423dw_hdmi_connector_mode_valid(struct drm_connector *connector,
1424 struct drm_display_mode *mode)
1425{
1426 struct dw_hdmi *hdmi = container_of(connector,
1427 struct dw_hdmi, connector);
1428 enum drm_mode_status mode_status = MODE_OK;
1429
1430 if (hdmi->plat_data->mode_valid)
1431 mode_status = hdmi->plat_data->mode_valid(connector, mode);
1432
1433 return mode_status;
1434}
1435
Andy Yanb21f4b62014-12-05 14:26:31 +08001436static struct drm_encoder *dw_hdmi_connector_best_encoder(struct drm_connector
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001437 *connector)
1438{
Andy Yanb21f4b62014-12-05 14:26:31 +08001439 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001440 connector);
1441
Andy Yan3d1b35a2014-12-05 14:25:05 +08001442 return hdmi->encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001443}
1444
Andy Yanb21f4b62014-12-05 14:26:31 +08001445static void dw_hdmi_connector_destroy(struct drm_connector *connector)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001446{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001447 drm_connector_unregister(connector);
1448 drm_connector_cleanup(connector);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001449}
1450
Andy Yanb21f4b62014-12-05 14:26:31 +08001451static struct drm_connector_funcs dw_hdmi_connector_funcs = {
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001452 .dpms = drm_helper_connector_dpms,
1453 .fill_modes = drm_helper_probe_single_connector_modes,
Andy Yanb21f4b62014-12-05 14:26:31 +08001454 .detect = dw_hdmi_connector_detect,
1455 .destroy = dw_hdmi_connector_destroy,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001456};
1457
Andy Yanb21f4b62014-12-05 14:26:31 +08001458static struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
1459 .get_modes = dw_hdmi_connector_get_modes,
Andy Yan632d0352014-12-05 14:30:21 +08001460 .mode_valid = dw_hdmi_connector_mode_valid,
Andy Yanb21f4b62014-12-05 14:26:31 +08001461 .best_encoder = dw_hdmi_connector_best_encoder,
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001462};
1463
Andy Yanb21f4b62014-12-05 14:26:31 +08001464struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
1465 .enable = dw_hdmi_bridge_enable,
1466 .disable = dw_hdmi_bridge_disable,
1467 .pre_enable = dw_hdmi_bridge_nop,
1468 .post_disable = dw_hdmi_bridge_nop,
1469 .mode_set = dw_hdmi_bridge_mode_set,
1470 .mode_fixup = dw_hdmi_bridge_mode_fixup,
1471 .destroy = dw_hdmi_bridge_destroy,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001472};
1473
Andy Yanb21f4b62014-12-05 14:26:31 +08001474static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
Russell Kingd94905e2013-11-03 22:23:24 +00001475{
Andy Yanb21f4b62014-12-05 14:26:31 +08001476 struct dw_hdmi *hdmi = dev_id;
Russell Kingd94905e2013-11-03 22:23:24 +00001477 u8 intr_stat;
1478
1479 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
1480 if (intr_stat)
1481 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1482
1483 return intr_stat ? IRQ_WAKE_THREAD : IRQ_NONE;
1484}
1485
Andy Yanb21f4b62014-12-05 14:26:31 +08001486static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001487{
Andy Yanb21f4b62014-12-05 14:26:31 +08001488 struct dw_hdmi *hdmi = dev_id;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001489 u8 intr_stat;
1490 u8 phy_int_pol;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001491
1492 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
1493
1494 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
1495
1496 if (intr_stat & HDMI_IH_PHY_STAT0_HPD) {
1497 if (phy_int_pol & HDMI_PHY_HPD) {
1498 dev_dbg(hdmi->dev, "EVENT=plugin\n");
1499
Russell King812bc612013-11-04 12:42:02 +00001500 hdmi_modb(hdmi, 0, HDMI_PHY_HPD, HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001501
Andy Yanb21f4b62014-12-05 14:26:31 +08001502 dw_hdmi_poweron(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001503 } else {
1504 dev_dbg(hdmi->dev, "EVENT=plugout\n");
1505
Gulsah Kose256a38b2014-03-09 20:11:07 +02001506 hdmi_modb(hdmi, HDMI_PHY_HPD, HDMI_PHY_HPD,
Andy Yanb5878332014-12-05 14:23:52 +08001507 HDMI_PHY_POL0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001508
Andy Yanb21f4b62014-12-05 14:26:31 +08001509 dw_hdmi_poweroff(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001510 }
Russell Kingd94905e2013-11-03 22:23:24 +00001511 drm_helper_hpd_irq_event(hdmi->connector.dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001512 }
1513
1514 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
Russell Kingd94905e2013-11-03 22:23:24 +00001515 hdmi_writeb(hdmi, ~HDMI_IH_PHY_STAT0_HPD, HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001516
1517 return IRQ_HANDLED;
1518}
1519
Andy Yanb21f4b62014-12-05 14:26:31 +08001520static int dw_hdmi_register(struct drm_device *drm, struct dw_hdmi *hdmi)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001521{
Andy Yan3d1b35a2014-12-05 14:25:05 +08001522 struct drm_encoder *encoder = hdmi->encoder;
1523 struct drm_bridge *bridge;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001524 int ret;
1525
Andy Yan3d1b35a2014-12-05 14:25:05 +08001526 bridge = devm_kzalloc(drm->dev, sizeof(*bridge), GFP_KERNEL);
1527 if (!bridge) {
1528 DRM_ERROR("Failed to allocate drm bridge\n");
1529 return -ENOMEM;
1530 }
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001531
Andy Yan3d1b35a2014-12-05 14:25:05 +08001532 hdmi->bridge = bridge;
1533 bridge->driver_private = hdmi;
1534
Andy Yanb21f4b62014-12-05 14:26:31 +08001535 ret = drm_bridge_init(drm, bridge, &dw_hdmi_bridge_funcs);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001536 if (ret) {
1537 DRM_ERROR("Failed to initialize bridge with drm\n");
1538 return -EINVAL;
1539 }
1540
1541 encoder->bridge = bridge;
Russell Kingd94905e2013-11-03 22:23:24 +00001542 hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001543
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001544 drm_connector_helper_add(&hdmi->connector,
Andy Yanb21f4b62014-12-05 14:26:31 +08001545 &dw_hdmi_connector_helper_funcs);
1546 drm_connector_init(drm, &hdmi->connector, &dw_hdmi_connector_funcs,
Russell King1b3f7672013-11-03 13:30:48 +00001547 DRM_MODE_CONNECTOR_HDMIA);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001548
Andy Yan3d1b35a2014-12-05 14:25:05 +08001549 hdmi->connector.encoder = encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001550
Andy Yan3d1b35a2014-12-05 14:25:05 +08001551 drm_mode_connector_attach_encoder(&hdmi->connector, encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001552
1553 return 0;
1554}
1555
Andy Yanb21f4b62014-12-05 14:26:31 +08001556int dw_hdmi_bind(struct device *dev, struct device *master,
Andy Yan3d1b35a2014-12-05 14:25:05 +08001557 void *data, struct drm_encoder *encoder,
1558 struct resource *iores, int irq,
1559 const struct dw_hdmi_plat_data *plat_data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001560{
Russell King1b3f7672013-11-03 13:30:48 +00001561 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +00001562 struct device_node *np = dev->of_node;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001563 struct device_node *ddc_node;
Andy Yanb21f4b62014-12-05 14:26:31 +08001564 struct dw_hdmi *hdmi;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001565 int ret;
Andy Yan0cd9d142014-12-05 14:28:24 +08001566 u32 val = 1;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001567
Russell King17b50012013-11-03 11:23:34 +00001568 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001569 if (!hdmi)
1570 return -ENOMEM;
1571
Andy Yan3d1b35a2014-12-05 14:25:05 +08001572 hdmi->plat_data = plat_data;
Russell King17b50012013-11-03 11:23:34 +00001573 hdmi->dev = dev;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001574 hdmi->dev_type = plat_data->dev_type;
Russell King40678382013-11-07 15:35:06 +00001575 hdmi->sample_rate = 48000;
1576 hdmi->ratio = 100;
Andy Yan3d1b35a2014-12-05 14:25:05 +08001577 hdmi->encoder = encoder;
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001578
Andy Yan0cd9d142014-12-05 14:28:24 +08001579 of_property_read_u32(np, "reg-io-width", &val);
1580
1581 switch (val) {
1582 case 4:
1583 hdmi->write = dw_hdmi_writel;
1584 hdmi->read = dw_hdmi_readl;
1585 break;
1586 case 1:
1587 hdmi->write = dw_hdmi_writeb;
1588 hdmi->read = dw_hdmi_readb;
1589 break;
1590 default:
1591 dev_err(dev, "reg-io-width must be 1 or 4\n");
1592 return -EINVAL;
1593 }
1594
Philipp Zabelb5d45902014-03-05 10:20:56 +01001595 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001596 if (ddc_node) {
1597 hdmi->ddc = of_find_i2c_adapter_by_node(ddc_node);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001598 of_node_put(ddc_node);
Andy Yanc2c38482014-12-05 14:24:28 +08001599 if (!hdmi->ddc) {
1600 dev_dbg(hdmi->dev, "failed to read ddc node\n");
1601 return -EPROBE_DEFER;
1602 }
1603
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001604 } else {
1605 dev_dbg(hdmi->dev, "no ddc property found\n");
1606 }
1607
Andy Yanb21f4b62014-12-05 14:26:31 +08001608 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
1609 dw_hdmi_irq, IRQF_SHARED,
Russell Kingd94905e2013-11-03 22:23:24 +00001610 dev_name(dev), hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001611 if (ret)
1612 return ret;
1613
Russell King17b50012013-11-03 11:23:34 +00001614 hdmi->regs = devm_ioremap_resource(dev, iores);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001615 if (IS_ERR(hdmi->regs))
1616 return PTR_ERR(hdmi->regs);
1617
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001618 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
1619 if (IS_ERR(hdmi->isfr_clk)) {
1620 ret = PTR_ERR(hdmi->isfr_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001621 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001622 return ret;
1623 }
1624
1625 ret = clk_prepare_enable(hdmi->isfr_clk);
1626 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001627 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001628 return ret;
1629 }
1630
1631 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
1632 if (IS_ERR(hdmi->iahb_clk)) {
1633 ret = PTR_ERR(hdmi->iahb_clk);
Andy Yanb5878332014-12-05 14:23:52 +08001634 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001635 goto err_isfr;
1636 }
1637
1638 ret = clk_prepare_enable(hdmi->iahb_clk);
1639 if (ret) {
Andy Yanb5878332014-12-05 14:23:52 +08001640 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001641 goto err_isfr;
1642 }
1643
1644 /* Product and revision IDs */
Russell King17b50012013-11-03 11:23:34 +00001645 dev_info(dev,
Andy Yanb5878332014-12-05 14:23:52 +08001646 "Detected HDMI controller 0x%x:0x%x:0x%x:0x%x\n",
1647 hdmi_readb(hdmi, HDMI_DESIGN_ID),
1648 hdmi_readb(hdmi, HDMI_REVISION_ID),
1649 hdmi_readb(hdmi, HDMI_PRODUCT_ID0),
1650 hdmi_readb(hdmi, HDMI_PRODUCT_ID1));
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001651
1652 initialize_hdmi_ih_mutes(hdmi);
1653
1654 /*
1655 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
1656 * N and cts values before enabling phy
1657 */
1658 hdmi_init_clk_regenerator(hdmi);
1659
1660 /*
1661 * Configure registers related to HDMI interrupt
1662 * generation before registering IRQ.
1663 */
1664 hdmi_writeb(hdmi, HDMI_PHY_HPD, HDMI_PHY_POL0);
1665
1666 /* Clear Hotplug interrupts */
1667 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD, HDMI_IH_PHY_STAT0);
1668
Andy Yanb21f4b62014-12-05 14:26:31 +08001669 ret = dw_hdmi_fb_registered(hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001670 if (ret)
1671 goto err_iahb;
1672
Andy Yanb21f4b62014-12-05 14:26:31 +08001673 ret = dw_hdmi_register(drm, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001674 if (ret)
1675 goto err_iahb;
1676
Russell Kingd94905e2013-11-03 22:23:24 +00001677 /* Unmute interrupts */
1678 hdmi_writeb(hdmi, ~HDMI_IH_PHY_STAT0_HPD, HDMI_IH_MUTE_PHY_STAT0);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001679
Russell King17b50012013-11-03 11:23:34 +00001680 dev_set_drvdata(dev, hdmi);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001681
1682 return 0;
1683
1684err_iahb:
1685 clk_disable_unprepare(hdmi->iahb_clk);
1686err_isfr:
1687 clk_disable_unprepare(hdmi->isfr_clk);
1688
1689 return ret;
1690}
Andy Yanb21f4b62014-12-05 14:26:31 +08001691EXPORT_SYMBOL_GPL(dw_hdmi_bind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001692
Andy Yanb21f4b62014-12-05 14:26:31 +08001693void dw_hdmi_unbind(struct device *dev, struct device *master, void *data)
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001694{
Andy Yanb21f4b62014-12-05 14:26:31 +08001695 struct dw_hdmi *hdmi = dev_get_drvdata(dev);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001696
Russell Kingd94905e2013-11-03 22:23:24 +00001697 /* Disable all interrupts */
1698 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
1699
Russell King1b3f7672013-11-03 13:30:48 +00001700 hdmi->connector.funcs->destroy(&hdmi->connector);
Andy Yan3d1b35a2014-12-05 14:25:05 +08001701 hdmi->encoder->funcs->destroy(hdmi->encoder);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001702
1703 clk_disable_unprepare(hdmi->iahb_clk);
1704 clk_disable_unprepare(hdmi->isfr_clk);
1705 i2c_put_adapter(hdmi->ddc);
Russell King17b50012013-11-03 11:23:34 +00001706}
Andy Yanb21f4b62014-12-05 14:26:31 +08001707EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001708
1709MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
Andy Yan3d1b35a2014-12-05 14:25:05 +08001710MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
1711MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
Andy Yanb21f4b62014-12-05 14:26:31 +08001712MODULE_DESCRIPTION("DW HDMI transmitter driver");
Fabio Estevam9aaf8802013-11-29 08:46:32 -02001713MODULE_LICENSE("GPL");
Andy Yanb21f4b62014-12-05 14:26:31 +08001714MODULE_ALIAS("platform:dw-hdmi");