1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DesignWare High-Definition Multimedia Interface (HDMI) driver
4 *
5 * Copyright (C) 2013-2015 Mentor Graphics Inc.
6 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc.
7 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/extcon.h>
13 #include <linux/extcon-provider.h>
14 #include <linux/hdmi.h>
15 #include <linux/irq.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of_device.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/regmap.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/spinlock.h>
23 #include <linux/pinctrl/consumer.h>
24
25 #include <media/cec-notifier.h>
26
27 #include <uapi/linux/media-bus-format.h>
28 #include <uapi/linux/videodev2.h>
29
30 #include <drm/bridge/dw_hdmi.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_bridge.h>
34 #include <drm/drm_edid.h>
35 #include <drm/drm_of.h>
36 #include <drm/drm_print.h>
37 #include <drm/drm_probe_helper.h>
38 #include <drm/drm_scdc_helper.h>
39
40 #include "dw-hdmi-audio.h"
41 #include "dw-hdmi-cec.h"
42 #include "dw-hdmi-hdcp.h"
43 #include "dw-hdmi.h"
44
45 #define DDC_CI_ADDR 0x37
46 #define DDC_SEGMENT_ADDR 0x30
47
48 #define HDMI_EDID_LEN 512
49 #define HDMI_EDID_BLOCK_LEN 128
50
51 /* DW-HDMI Controller >= 0x200a are at least compliant with SCDC version 1 */
52 #define SCDC_MIN_SOURCE_VERSION 0x1
53
54 #define HDMI14_MAX_TMDSCLK 340000000
55
56 static const unsigned int dw_hdmi_cable[] = {
57 EXTCON_DISP_HDMI,
58 EXTCON_NONE,
59 };
60
61 enum hdmi_datamap {
62 RGB444_8B = 0x01,
63 RGB444_10B = 0x03,
64 RGB444_12B = 0x05,
65 RGB444_16B = 0x07,
66 YCbCr444_8B = 0x09,
67 YCbCr444_10B = 0x0B,
68 YCbCr444_12B = 0x0D,
69 YCbCr444_16B = 0x0F,
70 YCbCr422_8B = 0x16,
71 YCbCr422_10B = 0x14,
72 YCbCr422_12B = 0x12,
73 };
74
75 /*
76 * Unless otherwise noted, entries in this table are 100% optimization.
77 * Values can be obtained from hdmi_compute_n() but that function is
78 * slow so we pre-compute values we expect to see.
79 *
80 * All 32k and 48k values are expected to be the same (due to the way
81 * the math works) for any rate that's an exact kHz.
82 */
83 static const struct dw_hdmi_audio_tmds_n common_tmds_n_table[] = {
84 { .tmds = 25175000, .n_32k = 4096, .n_44k1 = 12854, .n_48k = 6144, },
85 { .tmds = 25200000, .n_32k = 4096, .n_44k1 = 5656, .n_48k = 6144, },
86 { .tmds = 27000000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
87 { .tmds = 28320000, .n_32k = 4096, .n_44k1 = 5586, .n_48k = 6144, },
88 { .tmds = 30240000, .n_32k = 4096, .n_44k1 = 5642, .n_48k = 6144, },
89 { .tmds = 31500000, .n_32k = 4096, .n_44k1 = 5600, .n_48k = 6144, },
90 { .tmds = 32000000, .n_32k = 4096, .n_44k1 = 5733, .n_48k = 6144, },
91 { .tmds = 33750000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
92 { .tmds = 36000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
93 { .tmds = 40000000, .n_32k = 4096, .n_44k1 = 5733, .n_48k = 6144, },
94 { .tmds = 49500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
95 { .tmds = 50000000, .n_32k = 4096, .n_44k1 = 5292, .n_48k = 6144, },
96 { .tmds = 54000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
97 { .tmds = 65000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
98 { .tmds = 68250000, .n_32k = 4096, .n_44k1 = 5376, .n_48k = 6144, },
99 { .tmds = 71000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
100 { .tmds = 72000000, .n_32k = 4096, .n_44k1 = 5635, .n_48k = 6144, },
101 { .tmds = 73250000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
102 { .tmds = 74250000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
103 { .tmds = 75000000, .n_32k = 4096, .n_44k1 = 5880, .n_48k = 6144, },
104 { .tmds = 78750000, .n_32k = 4096, .n_44k1 = 5600, .n_48k = 6144, },
105 { .tmds = 78800000, .n_32k = 4096, .n_44k1 = 5292, .n_48k = 6144, },
106 { .tmds = 79500000, .n_32k = 4096, .n_44k1 = 4704, .n_48k = 6144, },
107 { .tmds = 83500000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
108 { .tmds = 85500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
109 { .tmds = 88750000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
110 { .tmds = 97750000, .n_32k = 4096, .n_44k1 = 14112, .n_48k = 6144, },
111 { .tmds = 101000000, .n_32k = 4096, .n_44k1 = 7056, .n_48k = 6144, },
112 { .tmds = 106500000, .n_32k = 4096, .n_44k1 = 4704, .n_48k = 6144, },
113 { .tmds = 108000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
114 { .tmds = 115500000, .n_32k = 4096, .n_44k1 = 5712, .n_48k = 6144, },
115 { .tmds = 119000000, .n_32k = 4096, .n_44k1 = 5544, .n_48k = 6144, },
116 { .tmds = 135000000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
117 { .tmds = 146250000, .n_32k = 4096, .n_44k1 = 6272, .n_48k = 6144, },
118 { .tmds = 148500000, .n_32k = 4096, .n_44k1 = 5488, .n_48k = 6144, },
119 { .tmds = 154000000, .n_32k = 4096, .n_44k1 = 5544, .n_48k = 6144, },
120 { .tmds = 162000000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
121
122 /* For 297 MHz+ HDMI spec have some other rule for setting N */
123 { .tmds = 297000000, .n_32k = 3073, .n_44k1 = 4704, .n_48k = 5120, },
124 { .tmds = 594000000, .n_32k = 3073, .n_44k1 = 9408, .n_48k = 10240, },
125
126 /* End of table */
127 { .tmds = 0, .n_32k = 0, .n_44k1 = 0, .n_48k = 0, },
128 };
129
130 static const u16 csc_coeff_default[3][4] = {
131 { 0x2000, 0x0000, 0x0000, 0x0000 },
132 { 0x0000, 0x2000, 0x0000, 0x0000 },
133 { 0x0000, 0x0000, 0x2000, 0x0000 }
134 };
135
136 static const u16 csc_coeff_rgb_out_eitu601[3][4] = {
137 { 0x2000, 0x6926, 0x74fd, 0x010e },
138 { 0x2000, 0x2cdd, 0x0000, 0x7e9a },
139 { 0x2000, 0x0000, 0x38b4, 0x7e3b }
140 };
141
142 static const u16 csc_coeff_rgb_out_eitu709[3][4] = {
143 { 0x2000, 0x7106, 0x7a02, 0x00a7 },
144 { 0x2000, 0x3264, 0x0000, 0x7e6d },
145 { 0x2000, 0x0000, 0x3b61, 0x7e25 }
146 };
147
148 static const u16 csc_coeff_rgb_in_eitu601[3][4] = {
149 { 0x2591, 0x1322, 0x074b, 0x0000 },
150 { 0x6535, 0x2000, 0x7acc, 0x0200 },
151 { 0x6acd, 0x7534, 0x2000, 0x0200 }
152 };
153
154 static const u16 csc_coeff_rgb_in_eitu709[3][4] = {
155 { 0x2dc5, 0x0d9b, 0x049e, 0x0000 },
156 { 0x62f0, 0x2000, 0x7d11, 0x0200 },
157 { 0x6756, 0x78ab, 0x2000, 0x0200 }
158 };
159
160 static const u16 csc_coeff_rgb_full_to_rgb_limited[3][4] = {
161 { 0x1b7c, 0x0000, 0x0000, 0x0020 },
162 { 0x0000, 0x1b7c, 0x0000, 0x0020 },
163 { 0x0000, 0x0000, 0x1b7c, 0x0020 }
164 };
165
166 static const struct drm_display_mode dw_hdmi_default_modes[] = {
167 /* 4 - 1280x720@60Hz 16:9 */
168 { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1390,
169 1430, 1650, 0, 720, 725, 730, 750, 0,
170 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
171 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
172 /* 16 - 1920x1080@60Hz 16:9 */
173 { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2008,
174 2052, 2200, 0, 1080, 1084, 1089, 1125, 0,
175 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
176 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
177 /* 31 - 1920x1080@50Hz 16:9 */
178 { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 148500, 1920, 2448,
179 2492, 2640, 0, 1080, 1084, 1089, 1125, 0,
180 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
181 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
182 /* 19 - 1280x720@50Hz 16:9 */
183 { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74250, 1280, 1720,
184 1760, 1980, 0, 720, 725, 730, 750, 0,
185 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC),
186 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9, },
187 /* 17 - 720x576@50Hz 4:3 */
188 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
189 796, 864, 0, 576, 581, 586, 625, 0,
190 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
191 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
192 /* 2 - 720x480@60Hz 4:3 */
193 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
194 798, 858, 0, 480, 489, 495, 525, 0,
195 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
196 .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, },
197 };
198
199 struct hdmi_vmode {
200 bool mdataenablepolarity;
201
202 unsigned int previous_pixelclock;
203 unsigned int mpixelclock;
204 unsigned int mpixelrepetitioninput;
205 unsigned int mpixelrepetitionoutput;
206 unsigned int previous_tmdsclock;
207 unsigned int mtmdsclock;
208 };
209
210 struct hdmi_data_info {
211 unsigned int enc_in_bus_format;
212 unsigned int enc_out_bus_format;
213 unsigned int enc_in_encoding;
214 unsigned int enc_out_encoding;
215 unsigned int quant_range;
216 unsigned int pix_repet_factor;
217 struct hdmi_vmode video_mode;
218 bool rgb_limited_range;
219 };
220
221 struct dw_hdmi_i2c {
222 struct i2c_adapter adap;
223
224 struct mutex lock; /* used to serialize data transfers */
225 struct completion cmp;
226 u8 stat;
227
228 u8 slave_reg;
229 bool is_regaddr;
230 bool is_segment;
231
232 unsigned int scl_high_ns;
233 unsigned int scl_low_ns;
234 };
235
236 struct dw_hdmi_phy_data {
237 enum dw_hdmi_phy_type type;
238 const char *name;
239 unsigned int gen;
240 bool has_svsret;
241 int (*configure)(struct dw_hdmi *hdmi,
242 const struct dw_hdmi_plat_data *pdata,
243 unsigned long mpixelclock);
244 };
245
246 struct dw_hdmi {
247 struct drm_connector connector;
248 struct drm_bridge bridge;
249 struct drm_bridge *next_bridge;
250 struct platform_device *hdcp_dev;
251
252 unsigned int version;
253
254 struct platform_device *audio;
255 struct platform_device *cec;
256 struct device *dev;
257 struct clk *isfr_clk;
258 struct clk *iahb_clk;
259 struct clk *cec_clk;
260 struct dw_hdmi_i2c *i2c;
261
262 struct hdmi_data_info hdmi_data;
263 const struct dw_hdmi_plat_data *plat_data;
264 const struct dw_hdmi_cec_wake_ops *cec_ops;
265 struct dw_hdcp *hdcp;
266
267 int vic;
268 int irq;
269
270 u8 edid[HDMI_EDID_LEN];
271
272 struct {
273 const struct dw_hdmi_phy_ops *ops;
274 const char *name;
275 void *data;
276 bool enabled;
277 } phy;
278
279 struct drm_display_mode previous_mode;
280
281 struct i2c_adapter *ddc;
282 void __iomem *regs;
283 bool sink_is_hdmi;
284 bool sink_has_audio;
285 bool hpd_state;
286 bool support_hdmi;
287 bool force_logo;
288 int force_output;
289
290 struct delayed_work work;
291 struct workqueue_struct *workqueue;
292
293 struct pinctrl *pinctrl;
294 struct pinctrl_state *default_state;
295 struct pinctrl_state *unwedge_state;
296
297 struct mutex mutex; /* for state below and previous_mode */
298 enum drm_connector_force force; /* mutex-protected force state */
299 struct drm_connector *curr_conn;/* current connector (only valid when !disabled) */
300 bool disabled; /* DRM has disabled our bridge */
301 bool bridge_is_on; /* indicates the bridge is on */
302 bool rxsense; /* rxsense state */
303 u8 phy_mask; /* desired phy int mask settings */
304 u8 mc_clkdis; /* clock disable register */
305
306 spinlock_t audio_lock;
307 struct mutex audio_mutex;
308 struct dentry *debugfs_dir;
309 unsigned int sample_rate;
310 unsigned int audio_cts;
311 unsigned int audio_n;
312 bool audio_enable;
313 bool scramble_low_rates;
314
315 struct extcon_dev *extcon;
316
317 unsigned int reg_shift;
318 struct regmap *regm;
319 void (*enable_audio)(struct dw_hdmi *hdmi);
320 void (*disable_audio)(struct dw_hdmi *hdmi);
321
322 struct mutex cec_notifier_mutex;
323 struct cec_notifier *cec_notifier;
324 struct cec_adapter *cec_adap;
325
326 hdmi_codec_plugged_cb plugged_cb;
327 struct device *codec_dev;
328 enum drm_connector_status last_connector_result;
329 bool initialized; /* hdmi is enabled before bind */
330 bool logo_plug_out; /* hdmi is plug out when kernel logo */
331 bool update;
332 bool hdr2sdr; /* from hdr to sdr */
333 };
334
335 #define HDMI_IH_PHY_STAT0_RX_SENSE \
336 (HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \
337 HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)
338
339 #define HDMI_PHY_RX_SENSE \
340 (HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \
341 HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)
342
hdmi_writeb(struct dw_hdmi * hdmi,u8 val,int offset)343 static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)
344 {
345 regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);
346 }
347
hdmi_readb(struct dw_hdmi * hdmi,int offset)348 static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)
349 {
350 unsigned int val = 0;
351
352 regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);
353
354 return val;
355 }
356
handle_plugged_change(struct dw_hdmi * hdmi,bool plugged)357 static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)
358 {
359 if (hdmi->plugged_cb && hdmi->codec_dev)
360 hdmi->plugged_cb(hdmi->codec_dev, plugged);
361 }
362
dw_hdmi_set_plugged_cb(struct dw_hdmi * hdmi,hdmi_codec_plugged_cb fn,struct device * codec_dev)363 int dw_hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn,
364 struct device *codec_dev)
365 {
366 bool plugged;
367
368 mutex_lock(&hdmi->mutex);
369 hdmi->plugged_cb = fn;
370 hdmi->codec_dev = codec_dev;
371 plugged = hdmi->last_connector_result == connector_status_connected;
372 handle_plugged_change(hdmi, plugged);
373 mutex_unlock(&hdmi->mutex);
374
375 return 0;
376 }
377 EXPORT_SYMBOL_GPL(dw_hdmi_set_plugged_cb);
378
hdmi_modb(struct dw_hdmi * hdmi,u8 data,u8 mask,unsigned reg)379 static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)
380 {
381 regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);
382 }
383
hdmi_mask_writeb(struct dw_hdmi * hdmi,u8 data,unsigned int reg,u8 shift,u8 mask)384 static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,
385 u8 shift, u8 mask)
386 {
387 hdmi_modb(hdmi, data << shift, mask, reg);
388 }
389
dw_hdmi_check_output_type_changed(struct dw_hdmi * hdmi)390 static bool dw_hdmi_check_output_type_changed(struct dw_hdmi *hdmi)
391 {
392 bool sink_hdmi;
393
394 sink_hdmi = hdmi->sink_is_hdmi;
395
396 if (hdmi->force_output == 1)
397 hdmi->sink_is_hdmi = true;
398 else if (hdmi->force_output == 2)
399 hdmi->sink_is_hdmi = false;
400 else
401 hdmi->sink_is_hdmi = hdmi->support_hdmi;
402
403 if (sink_hdmi != hdmi->sink_is_hdmi)
404 return true;
405
406 return false;
407 }
408
repo_hpd_event(struct work_struct * p_work)409 static void repo_hpd_event(struct work_struct *p_work)
410 {
411 struct dw_hdmi *hdmi = container_of(p_work, struct dw_hdmi, work.work);
412 enum drm_connector_status status = hdmi->hpd_state ?
413 connector_status_connected : connector_status_disconnected;
414 u8 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
415
416 mutex_lock(&hdmi->mutex);
417 if (!(phy_stat & HDMI_PHY_RX_SENSE))
418 hdmi->rxsense = false;
419 if (phy_stat & HDMI_PHY_HPD)
420 hdmi->rxsense = true;
421 mutex_unlock(&hdmi->mutex);
422
423 if (hdmi->bridge.dev) {
424 bool change;
425
426 change = drm_helper_hpd_irq_event(hdmi->bridge.dev);
427
428 if (change && hdmi->cec_adap &&
429 hdmi->cec_adap->devnode.registered)
430 cec_queue_pin_hpd_event(hdmi->cec_adap,
431 hdmi->hpd_state,
432 ktime_get());
433 drm_bridge_hpd_notify(&hdmi->bridge, status);
434 }
435 }
436
check_hdmi_irq(struct dw_hdmi * hdmi,int intr_stat,int phy_int_pol)437 static bool check_hdmi_irq(struct dw_hdmi *hdmi, int intr_stat,
438 int phy_int_pol)
439 {
440 int msecs;
441
442 /* To determine whether interrupt type is HPD */
443 if (!(intr_stat & HDMI_IH_PHY_STAT0_HPD))
444 return false;
445
446 if (phy_int_pol & HDMI_PHY_HPD) {
447 dev_dbg(hdmi->dev, "dw hdmi plug in\n");
448 msecs = 150;
449 hdmi->hpd_state = true;
450 } else {
451 dev_dbg(hdmi->dev, "dw hdmi plug out\n");
452 msecs = 20;
453 hdmi->hpd_state = false;
454 }
455 mod_delayed_work(hdmi->workqueue, &hdmi->work, msecs_to_jiffies(msecs));
456
457 return true;
458 }
459
init_hpd_work(struct dw_hdmi * hdmi)460 static void init_hpd_work(struct dw_hdmi *hdmi)
461 {
462 hdmi->workqueue = create_workqueue("hpd_queue");
463 INIT_DELAYED_WORK(&hdmi->work, repo_hpd_event);
464 }
465
dw_hdmi_i2c_set_divs(struct dw_hdmi * hdmi)466 static void dw_hdmi_i2c_set_divs(struct dw_hdmi *hdmi)
467 {
468 unsigned long clk_rate_khz;
469 unsigned long low_ns, high_ns;
470 unsigned long div_low, div_high;
471
472 /* Standard-mode */
473 if (hdmi->i2c->scl_high_ns < 4000)
474 high_ns = 4708;
475 else
476 high_ns = hdmi->i2c->scl_high_ns;
477
478 if (hdmi->i2c->scl_low_ns < 4700)
479 low_ns = 4916;
480 else
481 low_ns = hdmi->i2c->scl_low_ns;
482
483 /* Adjust to avoid overflow */
484 clk_rate_khz = DIV_ROUND_UP(clk_get_rate(hdmi->isfr_clk), 1000);
485
486 div_low = (clk_rate_khz * low_ns) / 1000000;
487 if ((clk_rate_khz * low_ns) % 1000000)
488 div_low++;
489
490 div_high = (clk_rate_khz * high_ns) / 1000000;
491 if ((clk_rate_khz * high_ns) % 1000000)
492 div_high++;
493
494 /* Maximum divider supported by hw is 0xffff */
495 if (div_low > 0xffff)
496 div_low = 0xffff;
497
498 if (div_high > 0xffff)
499 div_high = 0xffff;
500
501 hdmi_writeb(hdmi, div_high & 0xff, HDMI_I2CM_SS_SCL_HCNT_0_ADDR);
502 hdmi_writeb(hdmi, (div_high >> 8) & 0xff,
503 HDMI_I2CM_SS_SCL_HCNT_1_ADDR);
504 hdmi_writeb(hdmi, div_low & 0xff, HDMI_I2CM_SS_SCL_LCNT_0_ADDR);
505 hdmi_writeb(hdmi, (div_low >> 8) & 0xff,
506 HDMI_I2CM_SS_SCL_LCNT_1_ADDR);
507 }
508
dw_hdmi_i2c_init(struct dw_hdmi * hdmi)509 static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)
510 {
511 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
512 HDMI_PHY_I2CM_INT_ADDR);
513
514 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
515 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
516 HDMI_PHY_I2CM_CTLINT_ADDR);
517
518 /* Software reset */
519 hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);
520
521 /* Set Standard Mode speed (determined to be 100KHz on iMX6) */
522 hdmi_modb(hdmi, HDMI_I2CM_DIV_STD_MODE,
523 HDMI_I2CM_DIV_FAST_STD_MODE, HDMI_I2CM_DIV);
524
525 /* Set done, not acknowledged and arbitration interrupt polarities */
526 hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);
527 hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,
528 HDMI_I2CM_CTLINT);
529
530 /* Clear DONE and ERROR interrupts */
531 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
532 HDMI_IH_I2CM_STAT0);
533
534 /* Mute DONE and ERROR interrupts */
535 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
536 HDMI_IH_MUTE_I2CM_STAT0);
537
538 /* set SDA high level holding time */
539 hdmi_writeb(hdmi, 0x48, HDMI_I2CM_SDA_HOLD);
540
541 dw_hdmi_i2c_set_divs(hdmi);
542 }
543
dw_hdmi_i2c_unwedge(struct dw_hdmi * hdmi)544 static bool dw_hdmi_i2c_unwedge(struct dw_hdmi *hdmi)
545 {
546 /* If no unwedge state then give up */
547 if (!hdmi->unwedge_state)
548 return false;
549
550 dev_info(hdmi->dev, "Attempting to unwedge stuck i2c bus\n");
551
552 /*
553 * This is a huge hack to workaround a problem where the dw_hdmi i2c
554 * bus could sometimes get wedged. Once wedged there doesn't appear
555 * to be any way to unwedge it (including the HDMI_I2CM_SOFTRSTZ)
556 * other than pulsing the SDA line.
557 *
558 * We appear to be able to pulse the SDA line (in the eyes of dw_hdmi)
559 * by:
560 * 1. Remux the pin as a GPIO output, driven low.
561 * 2. Wait a little while. 1 ms seems to work, but we'll do 10.
562 * 3. Immediately jump to remux the pin as dw_hdmi i2c again.
563 *
564 * At the moment of remuxing, the line will still be low due to its
565 * recent stint as an output, but then it will be pulled high by the
566 * (presumed) external pullup. dw_hdmi seems to see this as a rising
567 * edge and that seems to get it out of its jam.
568 *
569 * This wedging was only ever seen on one TV, and only on one of
570 * its HDMI ports. It happened when the TV was powered on while the
571 * device was plugged in. A scope trace shows the TV bringing both SDA
572 * and SCL low, then bringing them both back up at roughly the same
573 * time. Presumably this confuses dw_hdmi because it saw activity but
574 * no real STOP (maybe it thinks there's another master on the bus?).
575 * Giving it a clean rising edge of SDA while SCL is already high
576 * presumably makes dw_hdmi see a STOP which seems to bring dw_hdmi out
577 * of its stupor.
578 *
579 * Note that after coming back alive, transfers seem to immediately
580 * resume, so if we unwedge due to a timeout we should wait a little
581 * longer for our transfer to finish, since it might have just started
582 * now.
583 */
584 pinctrl_select_state(hdmi->pinctrl, hdmi->unwedge_state);
585 msleep(10);
586 pinctrl_select_state(hdmi->pinctrl, hdmi->default_state);
587
588 return true;
589 }
590
dw_hdmi_i2c_wait(struct dw_hdmi * hdmi)591 static int dw_hdmi_i2c_wait(struct dw_hdmi *hdmi)
592 {
593 struct dw_hdmi_i2c *i2c = hdmi->i2c;
594 int stat;
595
596 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
597 if (!stat) {
598 /* If we can't unwedge, return timeout */
599 if (!dw_hdmi_i2c_unwedge(hdmi))
600 return -EAGAIN;
601
602 /* We tried to unwedge; give it another chance */
603 stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);
604 if (!stat)
605 return -EAGAIN;
606 }
607
608 /* Check for error condition on the bus */
609 if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)
610 return -EIO;
611
612 return 0;
613 }
614
dw_hdmi_i2c_read(struct dw_hdmi * hdmi,unsigned char * buf,unsigned int length)615 static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,
616 unsigned char *buf, unsigned int length)
617 {
618 struct dw_hdmi_i2c *i2c = hdmi->i2c;
619 int ret, retry, i;
620 bool read_edid = false;
621
622 if (!i2c->is_regaddr) {
623 dev_dbg(hdmi->dev, "set read register address to 0\n");
624 i2c->slave_reg = 0x00;
625 i2c->is_regaddr = true;
626 }
627
628 /* edid reads are in 128 bytes. scdc reads are in 1 byte */
629 if (length == HDMI_EDID_BLOCK_LEN)
630 read_edid = true;
631
632 while (length > 0) {
633 retry = 100;
634 hdmi_writeb(hdmi, i2c->slave_reg, HDMI_I2CM_ADDRESS);
635
636 if (read_edid) {
637 i2c->slave_reg += 8;
638 length -= 8;
639 } else {
640 i2c->slave_reg++;
641 length--;
642 }
643
644 while (retry > 0) {
645 if (!(hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD)) {
646 void *data = hdmi->plat_data->phy_data;
647
648 dev_dbg(hdmi->dev, "hdmi disconnect, stop ddc read\n");
649 if (hdmi->plat_data->set_ddc_io)
650 hdmi->plat_data->set_ddc_io(data, false);
651 return -EPERM;
652 }
653
654 if (i2c->is_segment) {
655 if (read_edid)
656 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ8_EXT,
657 HDMI_I2CM_OPERATION);
658 else
659 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,
660 HDMI_I2CM_OPERATION);
661 } else {
662 if (read_edid)
663 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ8,
664 HDMI_I2CM_OPERATION);
665 else
666 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,
667 HDMI_I2CM_OPERATION);
668 }
669
670 ret = dw_hdmi_i2c_wait(hdmi);
671 if (ret == -EAGAIN) {
672 dev_dbg(hdmi->dev, "ddc read time out\n");
673 hdmi_writeb(hdmi, 0, HDMI_I2CM_SOFTRSTZ);
674 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_BUS_CLEAR,
675 HDMI_I2CM_OPERATION);
676 retry -= 10;
677 continue;
678 } else if (ret == -EIO) {
679 dev_dbg(hdmi->dev, "ddc read err\n");
680 hdmi_writeb(hdmi, 0, HDMI_I2CM_SOFTRSTZ);
681 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_BUS_CLEAR,
682 HDMI_I2CM_OPERATION);
683 retry--;
684 usleep_range(10000, 11000);
685 continue;
686 }
687 /* read success */
688 break;
689 }
690 if (retry <= 0) {
691 dev_err(hdmi->dev, "ddc read failed\n");
692 return -EIO;
693 }
694
695 if (read_edid)
696 for (i = 0; i < 8; i++)
697 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_READ_BUFF0 + i);
698 else
699 *buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);
700 }
701
702 i2c->is_segment = false;
703
704 return 0;
705 }
706
dw_hdmi_i2c_write(struct dw_hdmi * hdmi,unsigned char * buf,unsigned int length)707 static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,
708 unsigned char *buf, unsigned int length)
709 {
710 struct dw_hdmi_i2c *i2c = hdmi->i2c;
711 int ret, retry;
712
713 if (!i2c->is_regaddr) {
714 /* Use the first write byte as register address */
715 i2c->slave_reg = buf[0];
716 length--;
717 buf++;
718 i2c->is_regaddr = true;
719 }
720
721 while (length--) {
722 retry = 100;
723
724 hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);
725 hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);
726
727 while (retry > 0) {
728 if (!(hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD)) {
729 void *data = hdmi->plat_data->phy_data;
730
731 dev_dbg(hdmi->dev, "hdmi disconnect, stop ddc write\n");
732 if (hdmi->plat_data->set_ddc_io)
733 hdmi->plat_data->set_ddc_io(data, false);
734 return -EPERM;
735 }
736
737 reinit_completion(&i2c->cmp);
738 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,
739 HDMI_I2CM_OPERATION);
740
741 ret = dw_hdmi_i2c_wait(hdmi);
742 if (ret == -EAGAIN) {
743 dev_dbg(hdmi->dev, "ddc write time out\n");
744 hdmi_writeb(hdmi, 0, HDMI_I2CM_SOFTRSTZ);
745 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_BUS_CLEAR,
746 HDMI_I2CM_OPERATION);
747 retry -= 10;
748 continue;
749 } else if (ret == -EIO) {
750 dev_dbg(hdmi->dev, "ddc write err\n");
751 hdmi_writeb(hdmi, 0, HDMI_I2CM_SOFTRSTZ);
752 hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_BUS_CLEAR,
753 HDMI_I2CM_OPERATION);
754 retry--;
755 usleep_range(10000, 11000);
756 continue;
757 }
758
759 /* write success */
760 break;
761 }
762
763 if (retry <= 0) {
764 dev_err(hdmi->dev, "ddc write failed\n");
765 return -EIO;
766 }
767 }
768
769 return 0;
770 }
771
dw_hdmi_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)772 static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,
773 struct i2c_msg *msgs, int num)
774 {
775 struct dw_hdmi *hdmi = i2c_get_adapdata(adap);
776 struct dw_hdmi_i2c *i2c = hdmi->i2c;
777 u8 addr = msgs[0].addr;
778 void *data = hdmi->plat_data->phy_data;
779 int i, ret = 0;
780
781 if (addr == DDC_CI_ADDR)
782 /*
783 * The internal I2C controller does not support the multi-byte
784 * read and write operations needed for DDC/CI.
785 * TOFIX: Blacklist the DDC/CI address until we filter out
786 * unsupported I2C operations.
787 */
788 return -EOPNOTSUPP;
789
790 dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);
791
792 for (i = 0; i < num; i++) {
793 if (msgs[i].len == 0) {
794 dev_dbg(hdmi->dev,
795 "unsupported transfer %d/%d, no data\n",
796 i + 1, num);
797 return -EOPNOTSUPP;
798 }
799 }
800
801 mutex_lock(&i2c->lock);
802
803 if (hdmi->plat_data->set_ddc_io)
804 hdmi->plat_data->set_ddc_io(data, true);
805
806 hdmi_writeb(hdmi, 0, HDMI_I2CM_SOFTRSTZ);
807 udelay(100);
808
809 /* Unmute DONE and ERROR interrupts */
810 hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);
811
812 /* Set slave device address taken from the first I2C message */
813 if (addr == DDC_SEGMENT_ADDR && msgs[0].len == 1)
814 addr = DDC_ADDR;
815 hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);
816
817 /* Set slave device register address on transfer */
818 i2c->is_regaddr = false;
819
820 /* Set segment pointer for I2C extended read mode operation */
821 i2c->is_segment = false;
822
823 for (i = 0; i < num; i++) {
824 dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",
825 i + 1, num, msgs[i].len, msgs[i].flags);
826 if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {
827 i2c->is_segment = true;
828 hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);
829 hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);
830 } else {
831 if (msgs[i].flags & I2C_M_RD)
832 ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,
833 msgs[i].len);
834 else
835 ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,
836 msgs[i].len);
837 }
838 if (ret < 0)
839 break;
840 }
841
842 if (!ret)
843 ret = num;
844
845 /* Mute DONE and ERROR interrupts */
846 hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,
847 HDMI_IH_MUTE_I2CM_STAT0);
848
849 mutex_unlock(&i2c->lock);
850
851 return ret;
852 }
853
dw_hdmi_i2c_func(struct i2c_adapter * adapter)854 static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)
855 {
856 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
857 }
858
859 static const struct i2c_algorithm dw_hdmi_algorithm = {
860 .master_xfer = dw_hdmi_i2c_xfer,
861 .functionality = dw_hdmi_i2c_func,
862 };
863
dw_hdmi_i2c_adapter(struct dw_hdmi * hdmi)864 static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)
865 {
866 struct i2c_adapter *adap;
867 struct dw_hdmi_i2c *i2c;
868 int ret;
869
870 i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);
871 if (!i2c)
872 return ERR_PTR(-ENOMEM);
873
874 mutex_init(&i2c->lock);
875 init_completion(&i2c->cmp);
876
877 adap = &i2c->adap;
878 adap->class = I2C_CLASS_DDC;
879 adap->owner = THIS_MODULE;
880 adap->dev.parent = hdmi->dev;
881 adap->algo = &dw_hdmi_algorithm;
882 strlcpy(adap->name, "DesignWare HDMI", sizeof(adap->name));
883 i2c_set_adapdata(adap, hdmi);
884
885 ret = i2c_add_adapter(adap);
886 if (ret) {
887 dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);
888 devm_kfree(hdmi->dev, i2c);
889 return ERR_PTR(ret);
890 }
891
892 hdmi->i2c = i2c;
893
894 dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);
895
896 return adap;
897 }
898
hdmi_set_cts_n(struct dw_hdmi * hdmi,unsigned int cts,unsigned int n)899 static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,
900 unsigned int n)
901 {
902 /* Must be set/cleared first */
903 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);
904
905 /* nshift factor = 0 */
906 hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);
907
908 /* Use automatic CTS generation mode when CTS is not set */
909 if (cts)
910 hdmi_writeb(hdmi, ((cts >> 16) &
911 HDMI_AUD_CTS3_AUDCTS19_16_MASK) |
912 HDMI_AUD_CTS3_CTS_MANUAL,
913 HDMI_AUD_CTS3);
914 else
915 hdmi_writeb(hdmi, 0, HDMI_AUD_CTS3);
916 hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);
917 hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);
918
919 hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);
920 hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);
921 hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);
922 }
923
hdmi_match_tmds_n_table(struct dw_hdmi * hdmi,unsigned long pixel_clk,unsigned long freq)924 static int hdmi_match_tmds_n_table(struct dw_hdmi *hdmi,
925 unsigned long pixel_clk,
926 unsigned long freq)
927 {
928 const struct dw_hdmi_plat_data *plat_data = hdmi->plat_data;
929 const struct dw_hdmi_audio_tmds_n *tmds_n = NULL;
930 int i;
931
932 if (plat_data->tmds_n_table) {
933 for (i = 0; plat_data->tmds_n_table[i].tmds != 0; i++) {
934 if (pixel_clk == plat_data->tmds_n_table[i].tmds) {
935 tmds_n = &plat_data->tmds_n_table[i];
936 break;
937 }
938 }
939 }
940
941 if (tmds_n == NULL) {
942 for (i = 0; common_tmds_n_table[i].tmds != 0; i++) {
943 if (pixel_clk == common_tmds_n_table[i].tmds) {
944 tmds_n = &common_tmds_n_table[i];
945 break;
946 }
947 }
948 }
949
950 if (tmds_n == NULL)
951 return -ENOENT;
952
953 switch (freq) {
954 case 32000:
955 return tmds_n->n_32k;
956 case 44100:
957 case 88200:
958 case 176400:
959 return (freq / 44100) * tmds_n->n_44k1;
960 case 48000:
961 case 96000:
962 case 192000:
963 return (freq / 48000) * tmds_n->n_48k;
964 default:
965 return -ENOENT;
966 }
967 }
968
hdmi_audio_math_diff(unsigned int freq,unsigned int n,unsigned int pixel_clk)969 static u64 hdmi_audio_math_diff(unsigned int freq, unsigned int n,
970 unsigned int pixel_clk)
971 {
972 u64 final, diff;
973 u64 cts;
974
975 final = (u64)pixel_clk * n;
976
977 cts = final;
978 do_div(cts, 128 * freq);
979
980 diff = final - (u64)cts * (128 * freq);
981
982 return diff;
983 }
984
hdmi_compute_n(struct dw_hdmi * hdmi,unsigned long pixel_clk,unsigned long freq)985 static unsigned int hdmi_compute_n(struct dw_hdmi *hdmi,
986 unsigned long pixel_clk,
987 unsigned long freq)
988 {
989 unsigned int min_n = DIV_ROUND_UP((128 * freq), 1500);
990 unsigned int max_n = (128 * freq) / 300;
991 unsigned int ideal_n = (128 * freq) / 1000;
992 unsigned int best_n_distance = ideal_n;
993 unsigned int best_n = 0;
994 u64 best_diff = U64_MAX;
995 int n;
996
997 /* If the ideal N could satisfy the audio math, then just take it */
998 if (hdmi_audio_math_diff(freq, ideal_n, pixel_clk) == 0)
999 return ideal_n;
1000
1001 for (n = min_n; n <= max_n; n++) {
1002 u64 diff = hdmi_audio_math_diff(freq, n, pixel_clk);
1003
1004 if (diff < best_diff || (diff == best_diff &&
1005 abs(n - ideal_n) < best_n_distance)) {
1006 best_n = n;
1007 best_diff = diff;
1008 best_n_distance = abs(best_n - ideal_n);
1009 }
1010
1011 /*
1012 * The best N already satisfy the audio math, and also be
1013 * the closest value to ideal N, so just cut the loop.
1014 */
1015 if ((best_diff == 0) && (abs(n - ideal_n) > best_n_distance))
1016 break;
1017 }
1018
1019 return best_n;
1020 }
1021
hdmi_find_n(struct dw_hdmi * hdmi,unsigned long pixel_clk,unsigned long sample_rate)1022 static unsigned int hdmi_find_n(struct dw_hdmi *hdmi, unsigned long pixel_clk,
1023 unsigned long sample_rate)
1024 {
1025 int n;
1026
1027 n = hdmi_match_tmds_n_table(hdmi, pixel_clk, sample_rate);
1028 if (n > 0)
1029 return n;
1030
1031 dev_warn(hdmi->dev, "Rate %lu missing; compute N dynamically\n",
1032 pixel_clk);
1033
1034 return hdmi_compute_n(hdmi, pixel_clk, sample_rate);
1035 }
1036
1037 /*
1038 * When transmitting IEC60958 linear PCM audio, these registers allow to
1039 * configure the channel status information of all the channel status
1040 * bits in the IEC60958 frame. For the moment this configuration is only
1041 * used when the I2S audio interface, General Purpose Audio (GPA),
1042 * or AHB audio DMA (AHBAUDDMA) interface is active
1043 * (for S/PDIF interface this information comes from the stream).
1044 */
dw_hdmi_set_channel_status(struct dw_hdmi * hdmi,u8 * channel_status)1045 void dw_hdmi_set_channel_status(struct dw_hdmi *hdmi,
1046 u8 *channel_status)
1047 {
1048 /*
1049 * Set channel status register for frequency and word length.
1050 * Use default values for other registers.
1051 */
1052 hdmi_writeb(hdmi, channel_status[3], HDMI_FC_AUDSCHNLS7);
1053 hdmi_writeb(hdmi, channel_status[4], HDMI_FC_AUDSCHNLS8);
1054 }
1055 EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_status);
1056
hdmi_set_clk_regenerator(struct dw_hdmi * hdmi,unsigned long pixel_clk,unsigned int sample_rate)1057 static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,
1058 unsigned long pixel_clk, unsigned int sample_rate)
1059 {
1060 unsigned long ftdms = pixel_clk;
1061 unsigned int n, cts;
1062 u8 config3;
1063 u64 tmp;
1064
1065 n = hdmi_find_n(hdmi, pixel_clk, sample_rate);
1066
1067 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
1068
1069 /* Only compute CTS when using internal AHB audio */
1070 if (config3 & HDMI_CONFIG3_AHBAUDDMA) {
1071 /*
1072 * Compute the CTS value from the N value. Note that CTS and N
1073 * can be up to 20 bits in total, so we need 64-bit math. Also
1074 * note that our TDMS clock is not fully accurate; it is
1075 * accurate to kHz. This can introduce an unnecessary remainder
1076 * in the calculation below, so we don't try to warn about that.
1077 */
1078 tmp = (u64)ftdms * n;
1079 do_div(tmp, 128 * sample_rate);
1080 cts = tmp;
1081
1082 dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",
1083 __func__, sample_rate,
1084 ftdms / 1000000, (ftdms / 1000) % 1000,
1085 n, cts);
1086 } else {
1087 cts = 0;
1088 }
1089
1090 spin_lock_irq(&hdmi->audio_lock);
1091 hdmi->audio_n = n;
1092 hdmi->audio_cts = cts;
1093 hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);
1094 spin_unlock_irq(&hdmi->audio_lock);
1095 }
1096
hdmi_init_clk_regenerator(struct dw_hdmi * hdmi)1097 static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)
1098 {
1099 mutex_lock(&hdmi->audio_mutex);
1100 hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);
1101 mutex_unlock(&hdmi->audio_mutex);
1102 }
1103
hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi * hdmi)1104 static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)
1105 {
1106 mutex_lock(&hdmi->audio_mutex);
1107 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
1108 hdmi->sample_rate);
1109 mutex_unlock(&hdmi->audio_mutex);
1110 }
1111
dw_hdmi_set_sample_rate(struct dw_hdmi * hdmi,unsigned int rate)1112 void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)
1113 {
1114 mutex_lock(&hdmi->audio_mutex);
1115 hdmi->sample_rate = rate;
1116 hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,
1117 hdmi->sample_rate);
1118 mutex_unlock(&hdmi->audio_mutex);
1119 }
1120 EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);
1121
dw_hdmi_set_channel_count(struct dw_hdmi * hdmi,unsigned int cnt)1122 void dw_hdmi_set_channel_count(struct dw_hdmi *hdmi, unsigned int cnt)
1123 {
1124 u8 layout;
1125
1126 mutex_lock(&hdmi->audio_mutex);
1127
1128 /*
1129 * For >2 channel PCM audio, we need to select layout 1
1130 * and set an appropriate channel map.
1131 */
1132 if (cnt > 2)
1133 layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT1;
1134 else
1135 layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT0;
1136
1137 hdmi_modb(hdmi, layout, HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_MASK,
1138 HDMI_FC_AUDSCONF);
1139
1140 /* Set the audio infoframes channel count */
1141 hdmi_modb(hdmi, (cnt - 1) << HDMI_FC_AUDICONF0_CC_OFFSET,
1142 HDMI_FC_AUDICONF0_CC_MASK, HDMI_FC_AUDICONF0);
1143
1144 mutex_unlock(&hdmi->audio_mutex);
1145 }
1146 EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_count);
1147
dw_hdmi_set_channel_allocation(struct dw_hdmi * hdmi,unsigned int ca)1148 void dw_hdmi_set_channel_allocation(struct dw_hdmi *hdmi, unsigned int ca)
1149 {
1150 mutex_lock(&hdmi->audio_mutex);
1151
1152 hdmi_writeb(hdmi, ca, HDMI_FC_AUDICONF2);
1153
1154 mutex_unlock(&hdmi->audio_mutex);
1155 }
1156 EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_allocation);
1157
hdmi_enable_audio_clk(struct dw_hdmi * hdmi,bool enable)1158 static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)
1159 {
1160 if (enable)
1161 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;
1162 else
1163 hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;
1164 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
1165 }
1166
hdmi_audio_get_eld(struct dw_hdmi * hdmi)1167 static u8 *hdmi_audio_get_eld(struct dw_hdmi *hdmi)
1168 {
1169 if (!hdmi->curr_conn)
1170 return NULL;
1171
1172 return hdmi->curr_conn->eld;
1173 }
1174
dw_hdmi_ahb_audio_enable(struct dw_hdmi * hdmi)1175 static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)
1176 {
1177 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
1178 }
1179
dw_hdmi_ahb_audio_disable(struct dw_hdmi * hdmi)1180 static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)
1181 {
1182 hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);
1183 }
1184
dw_hdmi_i2s_audio_enable(struct dw_hdmi * hdmi)1185 static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)
1186 {
1187 hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);
1188 hdmi_enable_audio_clk(hdmi, true);
1189 }
1190
dw_hdmi_i2s_audio_disable(struct dw_hdmi * hdmi)1191 static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)
1192 {
1193 hdmi_enable_audio_clk(hdmi, false);
1194 }
1195
dw_hdmi_audio_enable(struct dw_hdmi * hdmi)1196 void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)
1197 {
1198 unsigned long flags;
1199
1200 spin_lock_irqsave(&hdmi->audio_lock, flags);
1201 hdmi->audio_enable = true;
1202 if (hdmi->enable_audio)
1203 hdmi->enable_audio(hdmi);
1204 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
1205 }
1206 EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);
1207
dw_hdmi_audio_disable(struct dw_hdmi * hdmi)1208 void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)
1209 {
1210 unsigned long flags;
1211
1212 spin_lock_irqsave(&hdmi->audio_lock, flags);
1213 hdmi->audio_enable = false;
1214 if (hdmi->disable_audio)
1215 hdmi->disable_audio(hdmi);
1216 spin_unlock_irqrestore(&hdmi->audio_lock, flags);
1217 }
1218 EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);
1219
hdmi_bus_fmt_is_rgb(unsigned int bus_format)1220 static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)
1221 {
1222 switch (bus_format) {
1223 case MEDIA_BUS_FMT_RGB888_1X24:
1224 case MEDIA_BUS_FMT_RGB101010_1X30:
1225 case MEDIA_BUS_FMT_RGB121212_1X36:
1226 case MEDIA_BUS_FMT_RGB161616_1X48:
1227 return true;
1228
1229 default:
1230 return false;
1231 }
1232 }
1233
hdmi_bus_fmt_is_yuv444(unsigned int bus_format)1234 static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)
1235 {
1236 switch (bus_format) {
1237 case MEDIA_BUS_FMT_YUV8_1X24:
1238 case MEDIA_BUS_FMT_YUV10_1X30:
1239 case MEDIA_BUS_FMT_YUV12_1X36:
1240 case MEDIA_BUS_FMT_YUV16_1X48:
1241 return true;
1242
1243 default:
1244 return false;
1245 }
1246 }
1247
hdmi_bus_fmt_is_yuv422(unsigned int bus_format)1248 static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)
1249 {
1250 switch (bus_format) {
1251 case MEDIA_BUS_FMT_UYVY8_1X16:
1252 case MEDIA_BUS_FMT_UYVY10_1X20:
1253 case MEDIA_BUS_FMT_UYVY12_1X24:
1254 return true;
1255
1256 default:
1257 return false;
1258 }
1259 }
1260
hdmi_bus_fmt_is_yuv420(unsigned int bus_format)1261 static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)
1262 {
1263 switch (bus_format) {
1264 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
1265 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
1266 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
1267 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
1268 return true;
1269
1270 default:
1271 return false;
1272 }
1273 }
1274
hdmi_bus_fmt_color_depth(unsigned int bus_format)1275 static int hdmi_bus_fmt_color_depth(unsigned int bus_format)
1276 {
1277 switch (bus_format) {
1278 case MEDIA_BUS_FMT_RGB888_1X24:
1279 case MEDIA_BUS_FMT_YUV8_1X24:
1280 case MEDIA_BUS_FMT_UYVY8_1X16:
1281 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
1282 return 8;
1283
1284 case MEDIA_BUS_FMT_RGB101010_1X30:
1285 case MEDIA_BUS_FMT_YUV10_1X30:
1286 case MEDIA_BUS_FMT_UYVY10_1X20:
1287 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
1288 return 10;
1289
1290 case MEDIA_BUS_FMT_RGB121212_1X36:
1291 case MEDIA_BUS_FMT_YUV12_1X36:
1292 case MEDIA_BUS_FMT_UYVY12_1X24:
1293 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
1294 return 12;
1295
1296 case MEDIA_BUS_FMT_RGB161616_1X48:
1297 case MEDIA_BUS_FMT_YUV16_1X48:
1298 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
1299 return 16;
1300
1301 default:
1302 return 0;
1303 }
1304 }
1305
1306 /*
1307 * this submodule is responsible for the video data synchronization.
1308 * for example, for RGB 4:4:4 input, the data map is defined as
1309 * pin{47~40} <==> R[7:0]
1310 * pin{31~24} <==> G[7:0]
1311 * pin{15~8} <==> B[7:0]
1312 */
hdmi_video_sample(struct dw_hdmi * hdmi)1313 static void hdmi_video_sample(struct dw_hdmi *hdmi)
1314 {
1315 int color_format = 0;
1316 u8 val;
1317
1318 switch (hdmi->hdmi_data.enc_in_bus_format) {
1319 case MEDIA_BUS_FMT_RGB888_1X24:
1320 color_format = 0x01;
1321 break;
1322 case MEDIA_BUS_FMT_RGB101010_1X30:
1323 color_format = 0x03;
1324 break;
1325 case MEDIA_BUS_FMT_RGB121212_1X36:
1326 color_format = 0x05;
1327 break;
1328 case MEDIA_BUS_FMT_RGB161616_1X48:
1329 color_format = 0x07;
1330 break;
1331
1332 case MEDIA_BUS_FMT_YUV8_1X24:
1333 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
1334 color_format = 0x09;
1335 break;
1336 case MEDIA_BUS_FMT_YUV10_1X30:
1337 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
1338 color_format = 0x0B;
1339 break;
1340 case MEDIA_BUS_FMT_YUV12_1X36:
1341 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
1342 color_format = 0x0D;
1343 break;
1344 case MEDIA_BUS_FMT_YUV16_1X48:
1345 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
1346 color_format = 0x0F;
1347 break;
1348
1349 case MEDIA_BUS_FMT_UYVY8_1X16:
1350 color_format = 0x16;
1351 break;
1352 case MEDIA_BUS_FMT_UYVY10_1X20:
1353 color_format = 0x14;
1354 break;
1355 case MEDIA_BUS_FMT_UYVY12_1X24:
1356 color_format = 0x12;
1357 break;
1358
1359 default:
1360 return;
1361 }
1362
1363 val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |
1364 ((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &
1365 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
1366 hdmi_writeb(hdmi, val, HDMI_TX_INVID0);
1367
1368 /* Enable TX stuffing: When DE is inactive, fix the output data to 0 */
1369 val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |
1370 HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |
1371 HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;
1372 hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);
1373 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);
1374 hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);
1375 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);
1376 hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);
1377 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);
1378 hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);
1379 }
1380
is_color_space_conversion(struct dw_hdmi * hdmi)1381 static int is_color_space_conversion(struct dw_hdmi *hdmi)
1382 {
1383 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
1384 bool is_input_rgb, is_output_rgb;
1385
1386 is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_in_bus_format);
1387 is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_out_bus_format);
1388
1389 return (is_input_rgb != is_output_rgb) ||
1390 (is_input_rgb && is_output_rgb && hdmi_data->rgb_limited_range);
1391 }
1392
is_color_space_decimation(struct dw_hdmi * hdmi)1393 static int is_color_space_decimation(struct dw_hdmi *hdmi)
1394 {
1395 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
1396 return 0;
1397
1398 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||
1399 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))
1400 return 1;
1401
1402 return 0;
1403 }
1404
is_color_space_interpolation(struct dw_hdmi * hdmi)1405 static int is_color_space_interpolation(struct dw_hdmi *hdmi)
1406 {
1407 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))
1408 return 0;
1409
1410 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
1411 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
1412 return 1;
1413
1414 return 0;
1415 }
1416
is_csc_needed(struct dw_hdmi * hdmi)1417 static bool is_csc_needed(struct dw_hdmi *hdmi)
1418 {
1419 return is_color_space_conversion(hdmi) ||
1420 is_color_space_decimation(hdmi) ||
1421 is_color_space_interpolation(hdmi);
1422 }
1423
is_rgb_full_to_limited_needed(struct dw_hdmi * hdmi)1424 static bool is_rgb_full_to_limited_needed(struct dw_hdmi *hdmi)
1425 {
1426 if (hdmi->hdmi_data.quant_range == HDMI_QUANTIZATION_RANGE_LIMITED ||
1427 (!hdmi->hdmi_data.quant_range && hdmi->hdmi_data.rgb_limited_range))
1428 return true;
1429
1430 return false;
1431 }
1432
dw_hdmi_update_csc_coeffs(struct dw_hdmi * hdmi)1433 static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)
1434 {
1435 const u16 (*csc_coeff)[3][4] = &csc_coeff_default;
1436 bool is_input_rgb, is_output_rgb;
1437 unsigned i;
1438 u32 csc_scale = 1;
1439
1440 is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format);
1441 is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format);
1442
1443 if (!is_input_rgb && is_output_rgb) {
1444 if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)
1445 csc_coeff = &csc_coeff_rgb_out_eitu601;
1446 else
1447 csc_coeff = &csc_coeff_rgb_out_eitu709;
1448 } else if (is_input_rgb && !is_output_rgb) {
1449 if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)
1450 csc_coeff = &csc_coeff_rgb_in_eitu601;
1451 else
1452 csc_coeff = &csc_coeff_rgb_in_eitu709;
1453 csc_scale = 0;
1454 } else if (is_input_rgb && is_output_rgb &&
1455 is_rgb_full_to_limited_needed(hdmi)) {
1456 csc_coeff = &csc_coeff_rgb_full_to_rgb_limited;
1457 }
1458
1459 /* The CSC registers are sequential, alternating MSB then LSB */
1460 for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {
1461 u16 coeff_a = (*csc_coeff)[0][i];
1462 u16 coeff_b = (*csc_coeff)[1][i];
1463 u16 coeff_c = (*csc_coeff)[2][i];
1464
1465 hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);
1466 hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);
1467 hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);
1468 hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);
1469 hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);
1470 hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);
1471 }
1472
1473 hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,
1474 HDMI_CSC_SCALE);
1475 }
1476
hdmi_video_csc(struct dw_hdmi * hdmi)1477 static void hdmi_video_csc(struct dw_hdmi *hdmi)
1478 {
1479 int color_depth = 0;
1480 int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;
1481 int decimation = 0;
1482
1483 /* YCC422 interpolation to 444 mode */
1484 if (is_color_space_interpolation(hdmi))
1485 interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;
1486 else if (is_color_space_decimation(hdmi))
1487 decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA1;
1488
1489 switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {
1490 case 8:
1491 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;
1492 break;
1493 case 10:
1494 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;
1495 break;
1496 case 12:
1497 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;
1498 break;
1499 case 16:
1500 color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;
1501 break;
1502
1503 default:
1504 return;
1505 }
1506
1507 /* Configure the CSC registers */
1508 hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);
1509 hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,
1510 HDMI_CSC_SCALE);
1511
1512 dw_hdmi_update_csc_coeffs(hdmi);
1513 }
1514
1515 /*
1516 * HDMI video packetizer is used to packetize the data.
1517 * for example, if input is YCC422 mode or repeater is used,
1518 * data should be repacked this module can be bypassed.
1519 */
hdmi_video_packetize(struct dw_hdmi * hdmi)1520 static void hdmi_video_packetize(struct dw_hdmi *hdmi)
1521 {
1522 unsigned int color_depth = 0;
1523 unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;
1524 unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;
1525 struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;
1526 u8 val, vp_conf;
1527
1528 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||
1529 hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format) ||
1530 hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {
1531 switch (hdmi_bus_fmt_color_depth(
1532 hdmi->hdmi_data.enc_out_bus_format)) {
1533 case 8:
1534 color_depth = 0;
1535 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
1536 break;
1537 case 10:
1538 color_depth = 5;
1539 break;
1540 case 12:
1541 color_depth = 6;
1542 break;
1543 case 16:
1544 color_depth = 7;
1545 break;
1546 default:
1547 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;
1548 }
1549 } else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
1550 switch (hdmi_bus_fmt_color_depth(
1551 hdmi->hdmi_data.enc_out_bus_format)) {
1552 case 0:
1553 case 8:
1554 remap_size = HDMI_VP_REMAP_YCC422_16bit;
1555 break;
1556 case 10:
1557 remap_size = HDMI_VP_REMAP_YCC422_20bit;
1558 break;
1559 case 12:
1560 remap_size = HDMI_VP_REMAP_YCC422_24bit;
1561 break;
1562
1563 default:
1564 return;
1565 }
1566 output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;
1567 } else {
1568 return;
1569 }
1570
1571 /* set the packetizer registers */
1572 val = (color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &
1573 HDMI_VP_PR_CD_COLOR_DEPTH_MASK;
1574 hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);
1575
1576 hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,
1577 HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);
1578
1579 /* Data from pixel repeater block */
1580 if (hdmi_data->pix_repet_factor > 0) {
1581 vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |
1582 HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;
1583 } else { /* data from packetizer block */
1584 vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |
1585 HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;
1586 }
1587
1588 hdmi_modb(hdmi, vp_conf,
1589 HDMI_VP_CONF_PR_EN_MASK |
1590 HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);
1591
1592 hdmi_modb(hdmi, 0, HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);
1593 hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);
1594
1595 if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {
1596 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
1597 HDMI_VP_CONF_PP_EN_ENABLE |
1598 HDMI_VP_CONF_YCC422_EN_DISABLE;
1599 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {
1600 vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |
1601 HDMI_VP_CONF_PP_EN_DISABLE |
1602 HDMI_VP_CONF_YCC422_EN_ENABLE;
1603 } else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {
1604 vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |
1605 HDMI_VP_CONF_PP_EN_DISABLE |
1606 HDMI_VP_CONF_YCC422_EN_DISABLE;
1607 } else {
1608 return;
1609 }
1610
1611 hdmi_modb(hdmi, vp_conf,
1612 HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |
1613 HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);
1614
1615 hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |
1616 HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,
1617 HDMI_VP_STUFF_PP_STUFFING_MASK |
1618 HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);
1619
1620 hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,
1621 HDMI_VP_CONF);
1622 }
1623
1624 /* -----------------------------------------------------------------------------
1625 * Synopsys PHY Handling
1626 */
1627
hdmi_phy_test_clear(struct dw_hdmi * hdmi,unsigned char bit)1628 static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,
1629 unsigned char bit)
1630 {
1631 hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,
1632 HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);
1633 }
1634
hdmi_phy_wait_i2c_done(struct dw_hdmi * hdmi,int msec)1635 static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)
1636 {
1637 u32 val;
1638
1639 while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {
1640 if (msec-- == 0)
1641 return false;
1642 udelay(1000);
1643 }
1644 hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);
1645
1646 return true;
1647 }
1648
dw_hdmi_phy_i2c_write(struct dw_hdmi * hdmi,unsigned short data,unsigned char addr)1649 void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,
1650 unsigned char addr)
1651 {
1652 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
1653 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
1654 hdmi_writeb(hdmi, (unsigned char)(data >> 8),
1655 HDMI_PHY_I2CM_DATAO_1_ADDR);
1656 hdmi_writeb(hdmi, (unsigned char)(data >> 0),
1657 HDMI_PHY_I2CM_DATAO_0_ADDR);
1658 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,
1659 HDMI_PHY_I2CM_OPERATION_ADDR);
1660 hdmi_phy_wait_i2c_done(hdmi, 1000);
1661 }
1662 EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);
1663
1664 /* Filter out invalid setups to avoid configuring SCDC and scrambling */
dw_hdmi_support_scdc(struct dw_hdmi * hdmi,const struct drm_display_info * display)1665 static bool dw_hdmi_support_scdc(struct dw_hdmi *hdmi,
1666 const struct drm_display_info *display)
1667 {
1668 /* Completely disable SCDC support for older controllers */
1669 if (hdmi->version < 0x200a)
1670 return false;
1671
1672 /* Disable if no DDC bus */
1673 if (!hdmi->ddc)
1674 return false;
1675
1676 /* Disable if SCDC is not supported, or if an HF-VSDB block is absent */
1677 if (!display->hdmi.scdc.supported ||
1678 !display->hdmi.scdc.scrambling.supported)
1679 return false;
1680
1681 /*
1682 * Disable if display only support low TMDS rates and scrambling
1683 * for low rates is not supported either
1684 */
1685 if (!display->hdmi.scdc.scrambling.low_rates &&
1686 display->max_tmds_clock <= 340000)
1687 return false;
1688
1689 return true;
1690 }
1691
hdmi_phy_i2c_read(struct dw_hdmi * hdmi,unsigned char addr)1692 static int hdmi_phy_i2c_read(struct dw_hdmi *hdmi, unsigned char addr)
1693 {
1694 int val;
1695
1696 hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);
1697 hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);
1698 hdmi_writeb(hdmi, 0, HDMI_PHY_I2CM_DATAI_1_ADDR);
1699 hdmi_writeb(hdmi, 0, HDMI_PHY_I2CM_DATAI_0_ADDR);
1700 hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_READ,
1701 HDMI_PHY_I2CM_OPERATION_ADDR);
1702 hdmi_phy_wait_i2c_done(hdmi, 1000);
1703 val = hdmi_readb(hdmi, HDMI_PHY_I2CM_DATAI_1_ADDR);
1704 val = (val & 0xff) << 8;
1705 val += hdmi_readb(hdmi, HDMI_PHY_I2CM_DATAI_0_ADDR) & 0xff;
1706 return val;
1707 }
1708
1709 /*
1710 * HDMI2.0 Specifies the following procedure for High TMDS Bit Rates:
1711 * - The Source shall suspend transmission of the TMDS clock and data
1712 * - The Source shall write to the TMDS_Bit_Clock_Ratio bit to change it
1713 * from a 0 to a 1 or from a 1 to a 0
1714 * - The Source shall allow a minimum of 1 ms and a maximum of 100 ms from
1715 * the time the TMDS_Bit_Clock_Ratio bit is written until resuming
1716 * transmission of TMDS clock and data
1717 *
1718 * To respect the 100ms maximum delay, the dw_hdmi_set_high_tmds_clock_ratio()
1719 * helper should called right before enabling the TMDS Clock and Data in
1720 * the PHY configuration callback.
1721 */
dw_hdmi_set_high_tmds_clock_ratio(struct dw_hdmi * hdmi,const struct drm_display_info * display)1722 void dw_hdmi_set_high_tmds_clock_ratio(struct dw_hdmi *hdmi,
1723 const struct drm_display_info *display)
1724 {
1725 unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;
1726
1727 /* Control for TMDS Bit Period/TMDS Clock-Period Ratio */
1728 if (dw_hdmi_support_scdc(hdmi, display)) {
1729 if (mtmdsclock > HDMI14_MAX_TMDSCLK)
1730 drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 1);
1731 else
1732 drm_scdc_set_high_tmds_clock_ratio(hdmi->ddc, 0);
1733 }
1734 }
1735 EXPORT_SYMBOL_GPL(dw_hdmi_set_high_tmds_clock_ratio);
1736
dw_hdmi_phy_enable_powerdown(struct dw_hdmi * hdmi,bool enable)1737 static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)
1738 {
1739 hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,
1740 HDMI_PHY_CONF0_PDZ_OFFSET,
1741 HDMI_PHY_CONF0_PDZ_MASK);
1742 }
1743
dw_hdmi_phy_enable_tmds(struct dw_hdmi * hdmi,u8 enable)1744 static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)
1745 {
1746 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1747 HDMI_PHY_CONF0_ENTMDS_OFFSET,
1748 HDMI_PHY_CONF0_ENTMDS_MASK);
1749 }
1750
dw_hdmi_phy_enable_svsret(struct dw_hdmi * hdmi,u8 enable)1751 static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)
1752 {
1753 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1754 HDMI_PHY_CONF0_SVSRET_OFFSET,
1755 HDMI_PHY_CONF0_SVSRET_MASK);
1756 }
1757
dw_hdmi_phy_gen2_pddq(struct dw_hdmi * hdmi,u8 enable)1758 void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)
1759 {
1760 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1761 HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,
1762 HDMI_PHY_CONF0_GEN2_PDDQ_MASK);
1763 }
1764 EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);
1765
dw_hdmi_phy_gen2_txpwron(struct dw_hdmi * hdmi,u8 enable)1766 void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)
1767 {
1768 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1769 HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,
1770 HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);
1771 }
1772 EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);
1773
dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi * hdmi,u8 enable)1774 static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)
1775 {
1776 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1777 HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,
1778 HDMI_PHY_CONF0_SELDATAENPOL_MASK);
1779 }
1780
dw_hdmi_phy_sel_interface_control(struct dw_hdmi * hdmi,u8 enable)1781 static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)
1782 {
1783 hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,
1784 HDMI_PHY_CONF0_SELDIPIF_OFFSET,
1785 HDMI_PHY_CONF0_SELDIPIF_MASK);
1786 }
1787
dw_hdmi_phy_reset(struct dw_hdmi * hdmi)1788 void dw_hdmi_phy_reset(struct dw_hdmi *hdmi)
1789 {
1790 /* PHY reset. The reset signal is active high on Gen2 PHYs. */
1791 hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);
1792 hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);
1793 }
1794 EXPORT_SYMBOL_GPL(dw_hdmi_phy_reset);
1795
dw_hdmi_phy_i2c_set_addr(struct dw_hdmi * hdmi,u8 address)1796 void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)
1797 {
1798 hdmi_phy_test_clear(hdmi, 1);
1799 hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);
1800 hdmi_phy_test_clear(hdmi, 0);
1801 }
1802 EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);
1803
dw_hdmi_phy_power_off(struct dw_hdmi * hdmi)1804 static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)
1805 {
1806 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1807 unsigned int i;
1808 u16 val;
1809
1810 if (phy->gen == 1) {
1811 dw_hdmi_phy_enable_tmds(hdmi, 0);
1812 dw_hdmi_phy_enable_powerdown(hdmi, true);
1813 return;
1814 }
1815
1816 dw_hdmi_phy_gen2_txpwron(hdmi, 0);
1817
1818 /*
1819 * Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went
1820 * to low power mode.
1821 */
1822 for (i = 0; i < 5; ++i) {
1823 val = hdmi_readb(hdmi, HDMI_PHY_STAT0);
1824 if (!(val & HDMI_PHY_TX_PHY_LOCK))
1825 break;
1826
1827 usleep_range(1000, 2000);
1828 }
1829
1830 if (val & HDMI_PHY_TX_PHY_LOCK)
1831 dev_warn(hdmi->dev, "PHY failed to power down\n");
1832 else
1833 dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);
1834
1835 dw_hdmi_phy_gen2_pddq(hdmi, 1);
1836 }
1837
dw_hdmi_phy_power_on(struct dw_hdmi * hdmi)1838 static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)
1839 {
1840 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1841 unsigned int i;
1842 u8 val;
1843
1844 if (phy->gen == 1) {
1845 dw_hdmi_phy_enable_powerdown(hdmi, false);
1846
1847 /* Toggle TMDS enable. */
1848 dw_hdmi_phy_enable_tmds(hdmi, 0);
1849 dw_hdmi_phy_enable_tmds(hdmi, 1);
1850 return 0;
1851 }
1852
1853 dw_hdmi_phy_gen2_txpwron(hdmi, 1);
1854 dw_hdmi_phy_gen2_pddq(hdmi, 0);
1855
1856 /* Wait for PHY PLL lock */
1857 for (i = 0; i < 5; ++i) {
1858 val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;
1859 if (val)
1860 break;
1861
1862 usleep_range(1000, 2000);
1863 }
1864
1865 if (!val) {
1866 dev_err(hdmi->dev, "PHY PLL failed to lock\n");
1867 return -ETIMEDOUT;
1868 }
1869
1870 dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);
1871 return 0;
1872 }
1873
1874 /*
1875 * PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available
1876 * information the DWC MHL PHY has the same register layout and is thus also
1877 * supported by this function.
1878 */
hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi * hdmi,const struct dw_hdmi_plat_data * pdata,unsigned long mpixelclock)1879 static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,
1880 const struct dw_hdmi_plat_data *pdata,
1881 unsigned long mpixelclock)
1882 {
1883 const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;
1884 const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;
1885 const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;
1886 unsigned int tmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;
1887 unsigned int depth =
1888 hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format);
1889
1890 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format) &&
1891 pdata->mpll_cfg_420)
1892 mpll_config = pdata->mpll_cfg_420;
1893
1894 /* TOFIX Will need 420 specific PHY configuration tables */
1895
1896 /* PLL/MPLL Cfg - always match on final entry */
1897 for (; mpll_config->mpixelclock != ~0UL; mpll_config++)
1898 if (mpixelclock <= mpll_config->mpixelclock)
1899 break;
1900
1901 for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)
1902 if (tmdsclock <= curr_ctrl->mpixelclock)
1903 break;
1904
1905 for (; phy_config->mpixelclock != ~0UL; phy_config++)
1906 if (tmdsclock <= phy_config->mpixelclock)
1907 break;
1908
1909 if (mpll_config->mpixelclock == ~0UL ||
1910 curr_ctrl->mpixelclock == ~0UL ||
1911 phy_config->mpixelclock == ~0UL)
1912 return -EINVAL;
1913
1914 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
1915 depth = fls(depth - 8);
1916 else
1917 depth = 0;
1918 if (depth)
1919 depth--;
1920
1921 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[depth].cpce,
1922 HDMI_3D_TX_PHY_CPCE_CTRL);
1923 dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[depth].gmp,
1924 HDMI_3D_TX_PHY_GMPCTRL);
1925 dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[depth],
1926 HDMI_3D_TX_PHY_CURRCTRL);
1927
1928 dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);
1929 dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,
1930 HDMI_3D_TX_PHY_MSM_CTRL);
1931
1932 dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);
1933 dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,
1934 HDMI_3D_TX_PHY_CKSYMTXCTRL);
1935 dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,
1936 HDMI_3D_TX_PHY_VLEVCTRL);
1937
1938 return 0;
1939 }
1940
hdmi_phy_configure(struct dw_hdmi * hdmi,const struct drm_display_info * display)1941 static int hdmi_phy_configure(struct dw_hdmi *hdmi,
1942 const struct drm_display_info *display)
1943 {
1944 const struct dw_hdmi_phy_data *phy = hdmi->phy.data;
1945 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
1946 unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;
1947 unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;
1948 int ret;
1949
1950 dw_hdmi_phy_power_off(hdmi);
1951
1952 dw_hdmi_set_high_tmds_clock_ratio(hdmi, display);
1953
1954 /* Leave low power consumption mode by asserting SVSRET. */
1955 if (phy->has_svsret)
1956 dw_hdmi_phy_enable_svsret(hdmi, 1);
1957
1958 dw_hdmi_phy_reset(hdmi);
1959
1960 hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);
1961
1962 dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);
1963
1964 /* Write to the PHY as configured by the platform */
1965 if (pdata->configure_phy)
1966 ret = pdata->configure_phy(hdmi, pdata->priv_data, mpixelclock);
1967 else
1968 ret = phy->configure(hdmi, pdata, mpixelclock);
1969 if (ret) {
1970 dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",
1971 mpixelclock);
1972 return ret;
1973 }
1974
1975 /* Wait for resuming transmission of TMDS clock and data */
1976 if (mtmdsclock > HDMI14_MAX_TMDSCLK)
1977 msleep(100);
1978
1979 return dw_hdmi_phy_power_on(hdmi);
1980 }
1981
dw_hdmi_phy_init(struct dw_hdmi * hdmi,void * data,const struct drm_display_info * display,const struct drm_display_mode * mode)1982 static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,
1983 const struct drm_display_info *display,
1984 const struct drm_display_mode *mode)
1985 {
1986 int i, ret;
1987
1988 /* HDMI Phy spec says to do the phy initialization sequence twice */
1989 for (i = 0; i < 2; i++) {
1990 dw_hdmi_phy_sel_data_en_pol(hdmi, 1);
1991 dw_hdmi_phy_sel_interface_control(hdmi, 0);
1992
1993 ret = hdmi_phy_configure(hdmi, display);
1994 if (ret)
1995 return ret;
1996 }
1997
1998 return 0;
1999 }
2000
dw_hdmi_phy_disable(struct dw_hdmi * hdmi,void * data)2001 static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
2002 {
2003 dw_hdmi_phy_power_off(hdmi);
2004 }
2005
dw_hdmi_phy_read_hpd(struct dw_hdmi * hdmi,void * data)2006 enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,
2007 void *data)
2008 {
2009 return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?
2010 connector_status_connected : connector_status_disconnected;
2011 }
2012 EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);
2013
dw_hdmi_phy_update_hpd(struct dw_hdmi * hdmi,void * data,bool force,bool disabled,bool rxsense)2014 void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,
2015 bool force, bool disabled, bool rxsense)
2016 {
2017 u8 old_mask = hdmi->phy_mask;
2018
2019 if (force || disabled || !rxsense)
2020 hdmi->phy_mask |= HDMI_PHY_RX_SENSE;
2021 else
2022 hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;
2023
2024 if (old_mask != hdmi->phy_mask)
2025 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
2026 }
2027 EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);
2028
dw_hdmi_phy_setup_hpd(struct dw_hdmi * hdmi,void * data)2029 void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)
2030 {
2031 /*
2032 * Configure the PHY RX SENSE and HPD interrupts polarities and clear
2033 * any pending interrupt.
2034 */
2035 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);
2036 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
2037 HDMI_IH_PHY_STAT0);
2038
2039 if (!hdmi->next_bridge) {
2040 /* Enable cable hot plug irq. */
2041 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
2042
2043 /* Clear and unmute interrupts. */
2044 hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,
2045 HDMI_IH_PHY_STAT0);
2046 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),
2047 HDMI_IH_MUTE_PHY_STAT0);
2048 }
2049 }
2050 EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);
2051
2052 static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {
2053 .init = dw_hdmi_phy_init,
2054 .disable = dw_hdmi_phy_disable,
2055 .read_hpd = dw_hdmi_phy_read_hpd,
2056 .update_hpd = dw_hdmi_phy_update_hpd,
2057 .setup_hpd = dw_hdmi_phy_setup_hpd,
2058 };
2059
2060 /* -----------------------------------------------------------------------------
2061 * HDMI TX Setup
2062 */
2063
hdmi_tx_hdcp_config(struct dw_hdmi * hdmi,const struct drm_display_mode * mode)2064 static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi,
2065 const struct drm_display_mode *mode)
2066 {
2067 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
2068 u8 vsync_pol, hsync_pol, data_pol, hdmi_dvi;
2069
2070 /* Configure the video polarity */
2071 vsync_pol = mode->flags & DRM_MODE_FLAG_PVSYNC ?
2072 HDMI_A_VIDPOLCFG_VSYNCPOL_ACTIVE_HIGH :
2073 HDMI_A_VIDPOLCFG_VSYNCPOL_ACTIVE_LOW;
2074 hsync_pol = mode->flags & DRM_MODE_FLAG_PHSYNC ?
2075 HDMI_A_VIDPOLCFG_HSYNCPOL_ACTIVE_HIGH :
2076 HDMI_A_VIDPOLCFG_HSYNCPOL_ACTIVE_LOW;
2077 data_pol = vmode->mdataenablepolarity ?
2078 HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH :
2079 HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;
2080 hdmi_modb(hdmi, vsync_pol | hsync_pol | data_pol,
2081 HDMI_A_VIDPOLCFG_VSYNCPOL_MASK |
2082 HDMI_A_VIDPOLCFG_HSYNCPOL_MASK |
2083 HDMI_A_VIDPOLCFG_DATAENPOL_MASK,
2084 HDMI_A_VIDPOLCFG);
2085
2086 /* Config the display mode */
2087 hdmi_dvi = hdmi->sink_is_hdmi ? HDMI_A_HDCPCFG0_HDMIDVI_HDMI :
2088 HDMI_A_HDCPCFG0_HDMIDVI_DVI;
2089 hdmi_modb(hdmi, hdmi_dvi, HDMI_A_HDCPCFG0_HDMIDVI_MASK,
2090 HDMI_A_HDCPCFG0);
2091
2092 if (hdmi->hdcp && hdmi->hdcp->hdcp_start)
2093 hdmi->hdcp->hdcp_start(hdmi->hdcp);
2094 }
2095
hdmi_config_AVI(struct dw_hdmi * hdmi,const struct drm_connector * connector,const struct drm_display_mode * mode)2096 static void hdmi_config_AVI(struct dw_hdmi *hdmi,
2097 const struct drm_connector *connector,
2098 const struct drm_display_mode *mode)
2099 {
2100 struct hdmi_avi_infoframe frame;
2101 u8 val;
2102 bool is_hdmi2;
2103 const struct drm_display_info *info = &connector->display_info;
2104
2105 is_hdmi2 = info->hdmi.scdc.supported || (info->color_formats & DRM_COLOR_FORMAT_YCRCB420);
2106
2107 /* Initialise info frame from DRM mode */
2108 drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
2109
2110 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
2111 /* default range */
2112 if (!hdmi->hdmi_data.quant_range)
2113 drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,
2114 hdmi->hdmi_data.rgb_limited_range ?
2115 HDMI_QUANTIZATION_RANGE_LIMITED :
2116 HDMI_QUANTIZATION_RANGE_FULL);
2117 else
2118 drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,
2119 hdmi->hdmi_data.quant_range);
2120 } else {
2121 frame.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;
2122 frame.ycc_quantization_range =
2123 HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
2124 }
2125
2126 if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
2127 frame.colorspace = HDMI_COLORSPACE_YUV444;
2128 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
2129 frame.colorspace = HDMI_COLORSPACE_YUV422;
2130 else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
2131 frame.colorspace = HDMI_COLORSPACE_YUV420;
2132 else
2133 frame.colorspace = HDMI_COLORSPACE_RGB;
2134
2135 /* Set up colorimetry */
2136 if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
2137 switch (hdmi->hdmi_data.enc_out_encoding) {
2138 case V4L2_YCBCR_ENC_601:
2139 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)
2140 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
2141 else
2142 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
2143 frame.extended_colorimetry =
2144 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
2145 break;
2146 case V4L2_YCBCR_ENC_709:
2147 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)
2148 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
2149 else
2150 frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
2151 frame.extended_colorimetry =
2152 HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;
2153 break;
2154 case V4L2_YCBCR_ENC_BT2020:
2155 if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_BT2020)
2156 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
2157 else
2158 frame.colorimetry = HDMI_COLORIMETRY_ITU_709;
2159 frame.extended_colorimetry =
2160 HDMI_EXTENDED_COLORIMETRY_BT2020;
2161 break;
2162 default: /* Carries no data */
2163 frame.colorimetry = HDMI_COLORIMETRY_ITU_601;
2164 frame.extended_colorimetry =
2165 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
2166 break;
2167 }
2168 frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
2169 } else {
2170 if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_BT2020) {
2171 frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;
2172 frame.extended_colorimetry =
2173 HDMI_EXTENDED_COLORIMETRY_BT2020;
2174 } else {
2175 frame.colorimetry = HDMI_COLORIMETRY_NONE;
2176 frame.extended_colorimetry =
2177 HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;
2178 }
2179
2180 if (is_hdmi2 && frame.quantization_range == HDMI_QUANTIZATION_RANGE_FULL)
2181 frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_FULL;
2182 else
2183 frame.ycc_quantization_range = HDMI_YCC_QUANTIZATION_RANGE_LIMITED;
2184 }
2185
2186 /*
2187 * The Designware IP uses a different byte format from standard
2188 * AVI info frames, though generally the bits are in the correct
2189 * bytes.
2190 */
2191
2192 /*
2193 * AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,
2194 * scan info in bits 4,5 rather than 0,1 and active aspect present in
2195 * bit 6 rather than 4.
2196 */
2197 val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);
2198 if (frame.active_aspect & 15)
2199 val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;
2200 if (frame.top_bar || frame.bottom_bar)
2201 val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;
2202 if (frame.left_bar || frame.right_bar)
2203 val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;
2204 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);
2205
2206 /* AVI data byte 2 differences: none */
2207 val = ((frame.colorimetry & 0x3) << 6) |
2208 ((frame.picture_aspect & 0x3) << 4) |
2209 (frame.active_aspect & 0xf);
2210 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);
2211
2212 /* AVI data byte 3 differences: none */
2213 val = ((frame.extended_colorimetry & 0x7) << 4) |
2214 ((frame.quantization_range & 0x3) << 2) |
2215 (frame.nups & 0x3);
2216 if (frame.itc)
2217 val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;
2218 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);
2219
2220 /* AVI data byte 4 differences: none */
2221 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format) ||
2222 hdmi->connector.display_info.hdmi.scdc.supported)
2223 val = hdmi->vic;
2224 else
2225 val = frame.video_code & 0x7f;
2226 hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);
2227
2228 /* AVI Data Byte 5- set up input and output pixel repetition */
2229 val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<
2230 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &
2231 HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |
2232 ((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<
2233 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &
2234 HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);
2235 hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);
2236
2237 /*
2238 * AVI data byte 5 differences: content type in 0,1 rather than 4,5,
2239 * ycc range in bits 2,3 rather than 6,7
2240 */
2241 val = ((frame.ycc_quantization_range & 0x3) << 2) |
2242 (frame.content_type & 0x3);
2243 hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);
2244
2245 /* AVI Data Bytes 6-13 */
2246 hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);
2247 hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);
2248 hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);
2249 hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);
2250 hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);
2251 hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);
2252 hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);
2253 hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);
2254 }
2255
hdmi_config_vendor_specific_infoframe(struct dw_hdmi * hdmi,const struct drm_connector * connector,const struct drm_display_mode * mode)2256 static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,
2257 const struct drm_connector *connector,
2258 const struct drm_display_mode *mode)
2259 {
2260 struct hdmi_vendor_infoframe frame;
2261 u8 buffer[10];
2262 ssize_t err;
2263
2264 /* if sink support hdmi2.0, don't send vsi */
2265 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format) ||
2266 hdmi->connector.display_info.hdmi.scdc.supported) {
2267 hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
2268 HDMI_FC_DATAUTO0_VSD_MASK);
2269 return;
2270 }
2271
2272 err = drm_hdmi_vendor_infoframe_from_display_mode(&frame,
2273 &hdmi->connector,
2274 mode);
2275 if (err < 0)
2276 /*
2277 * Going into that statement does not means vendor infoframe
2278 * fails. It just informed us that vendor infoframe is not
2279 * needed for the selected mode. Only 4k or stereoscopic 3D
2280 * mode requires vendor infoframe. So just simply return.
2281 */
2282 return;
2283
2284 err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));
2285 if (err < 0) {
2286 dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",
2287 err);
2288 return;
2289 }
2290 hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
2291 HDMI_FC_DATAUTO0_VSD_MASK);
2292
2293 /* Set the length of HDMI vendor specific InfoFrame payload */
2294 hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);
2295
2296 /* Set 24bit IEEE Registration Identifier */
2297 hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);
2298 hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);
2299 hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);
2300
2301 /* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */
2302 hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);
2303 hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);
2304
2305 if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
2306 hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);
2307
2308 /* Packet frame interpolation */
2309 hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);
2310
2311 /* Auto packets per frame and line spacing */
2312 hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);
2313
2314 /* Configures the Frame Composer On RDRB mode */
2315 hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,
2316 HDMI_FC_DATAUTO0_VSD_MASK);
2317 }
2318
hdmi_config_drm_infoframe(struct dw_hdmi * hdmi,const struct drm_connector * connector)2319 static void hdmi_config_drm_infoframe(struct dw_hdmi *hdmi,
2320 const struct drm_connector *connector)
2321 {
2322 const struct drm_connector_state *conn_state = connector->state;
2323 struct hdr_output_metadata *hdr_metadata;
2324 struct hdmi_drm_infoframe frame;
2325 u8 buffer[30];
2326 ssize_t err;
2327 int i;
2328
2329 /* Dynamic Range and Mastering Infoframe is introduced in v2.11a. */
2330 if (hdmi->version < 0x211a) {
2331 DRM_ERROR("Not support DRM Infoframe\n");
2332 return;
2333 }
2334
2335 if (!hdmi->plat_data->use_drm_infoframe)
2336 return;
2337
2338 hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_DISABLE,
2339 HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);
2340
2341 if (!hdmi->connector.hdr_sink_metadata.hdmi_type1.eotf) {
2342 DRM_DEBUG("No need to set HDR metadata in infoframe\n");
2343 return;
2344 }
2345
2346 if (!conn_state->hdr_output_metadata) {
2347 DRM_DEBUG("source metadata not set yet\n");
2348 return;
2349 }
2350
2351 hdr_metadata = (struct hdr_output_metadata *)
2352 conn_state->hdr_output_metadata->data;
2353
2354 if (!(hdmi->connector.hdr_sink_metadata.hdmi_type1.eotf &
2355 BIT(hdr_metadata->hdmi_metadata_type1.eotf))) {
2356 DRM_ERROR("Not support EOTF %d\n",
2357 hdr_metadata->hdmi_metadata_type1.eotf);
2358 return;
2359 }
2360
2361 err = drm_hdmi_infoframe_set_hdr_metadata(&frame, conn_state);
2362 if (err < 0)
2363 return;
2364
2365 err = hdmi_drm_infoframe_pack(&frame, buffer, sizeof(buffer));
2366 if (err < 0) {
2367 dev_err(hdmi->dev, "Failed to pack drm infoframe: %zd\n", err);
2368 return;
2369 }
2370
2371 hdmi_writeb(hdmi, frame.version, HDMI_FC_DRM_HB0);
2372 hdmi_writeb(hdmi, frame.length, HDMI_FC_DRM_HB1);
2373
2374 for (i = 0; i < frame.length; i++)
2375 hdmi_writeb(hdmi, buffer[4 + i], HDMI_FC_DRM_PB0 + i);
2376
2377 hdmi_writeb(hdmi, 1, HDMI_FC_DRM_UP);
2378 /*
2379 * avi and hdr infoframe cannot be sent at the same time
2380 * for compatibility with Huawei TV
2381 */
2382 msleep(300);
2383 hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_ENABLE,
2384 HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);
2385
2386 DRM_DEBUG("%s eotf %d end\n", __func__,
2387 hdr_metadata->hdmi_metadata_type1.eotf);
2388 }
2389
2390 static unsigned int
hdmi_get_tmdsclock(struct dw_hdmi * hdmi,unsigned long mpixelclock)2391 hdmi_get_tmdsclock(struct dw_hdmi *hdmi, unsigned long mpixelclock)
2392 {
2393 unsigned int tmdsclock = mpixelclock;
2394 unsigned int depth =
2395 hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format);
2396
2397 if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {
2398 switch (depth) {
2399 case 16:
2400 tmdsclock = mpixelclock * 2;
2401 break;
2402 case 12:
2403 tmdsclock = mpixelclock * 3 / 2;
2404 break;
2405 case 10:
2406 tmdsclock = mpixelclock * 5 / 4;
2407 break;
2408 default:
2409 break;
2410 }
2411 }
2412
2413 return tmdsclock;
2414 }
2415
hdmi_av_composer(struct dw_hdmi * hdmi,const struct drm_display_info * display,const struct drm_display_mode * mode)2416 static void hdmi_av_composer(struct dw_hdmi *hdmi,
2417 const struct drm_display_info *display,
2418 const struct drm_display_mode *mode)
2419 {
2420 u8 inv_val, bytes;
2421 const struct drm_hdmi_info *hdmi_info = &display->hdmi;
2422 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
2423 int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;
2424 unsigned int vdisplay, hdisplay;
2425
2426 vmode->previous_pixelclock = vmode->mpixelclock;
2427 vmode->mpixelclock = mode->crtc_clock * 1000;
2428 if ((mode->flags & DRM_MODE_FLAG_3D_MASK) ==
2429 DRM_MODE_FLAG_3D_FRAME_PACKING)
2430 vmode->mpixelclock *= 2;
2431 dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);
2432
2433 vmode->previous_tmdsclock = vmode->mtmdsclock;
2434 vmode->mtmdsclock = hdmi_get_tmdsclock(hdmi, vmode->mpixelclock);
2435 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
2436 vmode->mtmdsclock /= 2;
2437 dev_dbg(hdmi->dev, "final tmdsclock = %d\n", vmode->mtmdsclock);
2438
2439 if (hdmi->update)
2440 return;
2441
2442 /* Set up HDMI_FC_INVIDCONF
2443 * Some display equipments require that the interval
2444 * between Video Data and Data island must be at least 58 pixels,
2445 * and fc_invidconf.HDCP_keepout set (1'b1) can meet the requirement.
2446 */
2447 inv_val = HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE;
2448
2449 inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?
2450 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :
2451 HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;
2452
2453 inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?
2454 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :
2455 HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;
2456
2457 inv_val |= (vmode->mdataenablepolarity ?
2458 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :
2459 HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);
2460
2461 if (hdmi->vic == 39)
2462 inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;
2463 else
2464 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
2465 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :
2466 HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;
2467
2468 inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?
2469 HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :
2470 HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;
2471
2472 inv_val |= hdmi->sink_is_hdmi ?
2473 HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :
2474 HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;
2475
2476 hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);
2477
2478 hdisplay = mode->hdisplay;
2479 hblank = mode->htotal - mode->hdisplay;
2480 h_de_hs = mode->hsync_start - mode->hdisplay;
2481 hsync_len = mode->hsync_end - mode->hsync_start;
2482
2483 /*
2484 * When we're setting a YCbCr420 mode, we need
2485 * to adjust the horizontal timing to suit.
2486 */
2487 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {
2488 hdisplay /= 2;
2489 hblank /= 2;
2490 h_de_hs /= 2;
2491 hsync_len /= 2;
2492 }
2493
2494 vdisplay = mode->vdisplay;
2495 vblank = mode->vtotal - mode->vdisplay;
2496 v_de_vs = mode->vsync_start - mode->vdisplay;
2497 vsync_len = mode->vsync_end - mode->vsync_start;
2498
2499 /*
2500 * When we're setting an interlaced mode, we need
2501 * to adjust the vertical timing to suit.
2502 */
2503 if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
2504 vdisplay /= 2;
2505 vblank /= 2;
2506 v_de_vs /= 2;
2507 vsync_len /= 2;
2508 }
2509
2510 /* Scrambling Control */
2511 if (dw_hdmi_support_scdc(hdmi, display)) {
2512 if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||
2513 (hdmi_info->scdc.scrambling.low_rates &&
2514 hdmi->scramble_low_rates)) {
2515 /*
2516 * HDMI2.0 Specifies the following procedure:
2517 * After the Source Device has determined that
2518 * SCDC_Present is set (=1), the Source Device should
2519 * write the accurate Version of the Source Device
2520 * to the Source Version field in the SCDCS.
2521 * Source Devices compliant shall set the
2522 * Source Version = 1.
2523 */
2524 drm_scdc_readb(hdmi->ddc, SCDC_SINK_VERSION,
2525 &bytes);
2526 drm_scdc_writeb(hdmi->ddc, SCDC_SOURCE_VERSION,
2527 min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION));
2528
2529 /* Enabled Scrambling in the Sink */
2530 drm_scdc_set_scrambling(hdmi->ddc, 1);
2531
2532 /*
2533 * To activate the scrambler feature, you must ensure
2534 * that the quasi-static configuration bit
2535 * fc_invidconf.HDCP_keepout is set at configuration
2536 * time, before the required mc_swrstzreq.tmdsswrst_req
2537 * reset request is issued.
2538 */
2539 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,
2540 HDMI_MC_SWRSTZ);
2541 hdmi_writeb(hdmi, 1, HDMI_FC_SCRAMBLER_CTRL);
2542 } else {
2543 hdmi_writeb(hdmi, 0, HDMI_FC_SCRAMBLER_CTRL);
2544 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,
2545 HDMI_MC_SWRSTZ);
2546 drm_scdc_set_scrambling(hdmi->ddc, 0);
2547 }
2548 } else {
2549 hdmi_writeb(hdmi, 0, HDMI_FC_SCRAMBLER_CTRL);
2550 }
2551
2552 /* Set up horizontal active pixel width */
2553 hdmi_writeb(hdmi, hdisplay >> 8, HDMI_FC_INHACTV1);
2554 hdmi_writeb(hdmi, hdisplay, HDMI_FC_INHACTV0);
2555
2556 /* Set up vertical active lines */
2557 hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);
2558 hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);
2559
2560 /* Set up horizontal blanking pixel region width */
2561 hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);
2562 hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);
2563
2564 /* Set up vertical blanking pixel region width */
2565 hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);
2566
2567 /* Set up HSYNC active edge delay width (in pixel clks) */
2568 hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);
2569 hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);
2570
2571 /* Set up VSYNC active edge delay (in lines) */
2572 hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);
2573
2574 /* Set up HSYNC active pulse width (in pixel clks) */
2575 hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);
2576 hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);
2577
2578 /* Set up VSYNC active edge delay (in lines) */
2579 hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);
2580 }
2581
2582 /* HDMI Initialization Step B.4 */
dw_hdmi_enable_video_path(struct dw_hdmi * hdmi)2583 static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)
2584 {
2585 /* control period minimum duration */
2586 hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);
2587 hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);
2588 hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);
2589
2590 /* Set to fill TMDS data channels */
2591 hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);
2592 hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);
2593 hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);
2594
2595 /* Enable pixel clock and tmds data path */
2596
2597 if (!hdmi->update)
2598 hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |
2599 HDMI_MC_CLKDIS_CSCCLK_DISABLE |
2600 HDMI_MC_CLKDIS_AUDCLK_DISABLE |
2601 HDMI_MC_CLKDIS_PREPCLK_DISABLE |
2602 HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
2603 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;
2604 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
2605
2606 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;
2607 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
2608
2609 /* Enable pixel repetition path */
2610 if (hdmi->hdmi_data.video_mode.mpixelrepetitioninput) {
2611 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PREPCLK_DISABLE;
2612 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
2613 }
2614
2615 /* Enable csc path */
2616 if (is_csc_needed(hdmi)) {
2617 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;
2618 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
2619
2620 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,
2621 HDMI_MC_FLOWCTRL);
2622 } else {
2623 hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CSCCLK_DISABLE;
2624 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
2625
2626 hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,
2627 HDMI_MC_FLOWCTRL);
2628 }
2629 }
2630
2631 /* Workaround to clear the overflow condition */
dw_hdmi_clear_overflow(struct dw_hdmi * hdmi)2632 static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)
2633 {
2634 unsigned int count;
2635 unsigned int i;
2636 u8 val;
2637
2638 /*
2639 * Under some circumstances the Frame Composer arithmetic unit can miss
2640 * an FC register write due to being busy processing the previous one.
2641 * The issue can be worked around by issuing a TMDS software reset and
2642 * then write one of the FC registers several times.
2643 *
2644 * The number of iterations matters and depends on the HDMI TX revision
2645 * (and possibly on the platform). So far i.MX6Q (v1.30a), i.MX6DL
2646 * (v1.31a) and multiple Allwinner SoCs (v1.32a) have been identified
2647 * as needing the workaround, with 4 iterations for v1.30a and 1
2648 * iteration for others.
2649 * The Amlogic Meson GX SoCs (v2.01a) have been identified as needing
2650 * the workaround with a single iteration.
2651 * The Rockchip RK3288 SoC (v2.00a) and RK3328/RK3399 SoCs (v2.11a) have
2652 * been identified as needing the workaround with a single iteration.
2653 */
2654
2655 switch (hdmi->version) {
2656 case 0x130a:
2657 count = 4;
2658 break;
2659 case 0x131a:
2660 case 0x132a:
2661 case 0x200a:
2662 case 0x201a:
2663 case 0x211a:
2664 case 0x212a:
2665 count = 1;
2666 break;
2667 default:
2668 return;
2669 }
2670
2671 /* TMDS software reset */
2672 hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);
2673
2674 val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);
2675 for (i = 0; i < count; i++)
2676 hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);
2677 }
2678
hdmi_disable_overflow_interrupts(struct dw_hdmi * hdmi)2679 static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)
2680 {
2681 hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,
2682 HDMI_IH_MUTE_FC_STAT2);
2683 }
2684
dw_hdmi_force_output_pattern(struct dw_hdmi * hdmi,const struct drm_display_mode * mode)2685 static void dw_hdmi_force_output_pattern(struct dw_hdmi *hdmi, const struct drm_display_mode *mode)
2686 {
2687 /* force output black */
2688 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {
2689 enum hdmi_quantization_range rgb_quant_range = drm_default_rgb_quant_range(mode);
2690
2691 if (hdmi->hdmi_data.quant_range == HDMI_QUANTIZATION_RANGE_FULL) {
2692 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS2); /*R*/
2693 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS1); /*G*/
2694 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS0); /*B*/
2695 } else if (hdmi->hdmi_data.quant_range == HDMI_QUANTIZATION_RANGE_LIMITED) {
2696 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS2); /*R*/
2697 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS1); /*G*/
2698 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS0); /*B*/
2699 } else if (hdmi->hdmi_data.quant_range == HDMI_QUANTIZATION_RANGE_DEFAULT) {
2700 if (rgb_quant_range == HDMI_QUANTIZATION_RANGE_FULL) {
2701 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS2); /*R*/
2702 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS1); /*G*/
2703 hdmi_writeb(hdmi, 0x00, HDMI_FC_DBGTMDS0); /*B*/
2704 } else if (rgb_quant_range == HDMI_QUANTIZATION_RANGE_LIMITED) {
2705 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS2); /*R*/
2706 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS1); /*G*/
2707 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS0); /*B*/
2708 }
2709 }
2710 } else {
2711 hdmi_writeb(hdmi, 0x80, HDMI_FC_DBGTMDS2); /*Cr*/
2712 hdmi_writeb(hdmi, 0x10, HDMI_FC_DBGTMDS1); /*Y*/
2713 hdmi_writeb(hdmi, 0x80, HDMI_FC_DBGTMDS0); /*Cb*/
2714 }
2715 }
2716
dw_hdmi_setup(struct dw_hdmi * hdmi,const struct drm_connector * connector,const struct drm_display_mode * mode)2717 static int dw_hdmi_setup(struct dw_hdmi *hdmi,
2718 const struct drm_connector *connector,
2719 const struct drm_display_mode *mode)
2720 {
2721 int ret;
2722 void *data = hdmi->plat_data->phy_data;
2723
2724 hdmi_disable_overflow_interrupts(hdmi);
2725
2726 hdmi->vic = drm_match_cea_mode(mode);
2727
2728 if (!hdmi->vic) {
2729 dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");
2730 } else {
2731 dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);
2732 }
2733
2734 if (hdmi->plat_data->get_enc_out_encoding)
2735 hdmi->hdmi_data.enc_out_encoding =
2736 hdmi->plat_data->get_enc_out_encoding(data);
2737 else if ((hdmi->vic == 6) || (hdmi->vic == 7) ||
2738 (hdmi->vic == 21) || (hdmi->vic == 22) ||
2739 (hdmi->vic == 2) || (hdmi->vic == 3) ||
2740 (hdmi->vic == 17) || (hdmi->vic == 18))
2741 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;
2742 else
2743 hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;
2744
2745 if (mode->flags & DRM_MODE_FLAG_DBLCLK) {
2746 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 1;
2747 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 1;
2748 } else {
2749 hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;
2750 hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;
2751 }
2752 /* TOFIX: Get input format from plat data or fallback to RGB888 */
2753 if (hdmi->plat_data->get_input_bus_format)
2754 hdmi->hdmi_data.enc_in_bus_format =
2755 hdmi->plat_data->get_input_bus_format(data);
2756 else if (hdmi->plat_data->input_bus_format)
2757 hdmi->hdmi_data.enc_in_bus_format =
2758 hdmi->plat_data->input_bus_format;
2759 else
2760 hdmi->hdmi_data.enc_in_bus_format =
2761 MEDIA_BUS_FMT_RGB888_1X24;
2762
2763 /* TOFIX: Default to RGB888 output format */
2764 if (hdmi->plat_data->get_output_bus_format)
2765 hdmi->hdmi_data.enc_out_bus_format =
2766 hdmi->plat_data->get_output_bus_format(data);
2767 else
2768 hdmi->hdmi_data.enc_out_bus_format =
2769 MEDIA_BUS_FMT_RGB888_1X24;
2770
2771 if (hdmi->plat_data->set_prev_bus_format)
2772 hdmi->plat_data->set_prev_bus_format(data, hdmi->hdmi_data.enc_out_bus_format);
2773
2774 /* TOFIX: Get input encoding from plat data or fallback to none */
2775 if (hdmi->plat_data->get_enc_in_encoding)
2776 hdmi->hdmi_data.enc_in_encoding =
2777 hdmi->plat_data->get_enc_in_encoding(data);
2778 else if (hdmi->plat_data->input_bus_encoding)
2779 hdmi->hdmi_data.enc_in_encoding =
2780 hdmi->plat_data->input_bus_encoding;
2781 else
2782 hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;
2783
2784
2785 if (hdmi->plat_data->get_quant_range)
2786 hdmi->hdmi_data.quant_range =
2787 hdmi->plat_data->get_quant_range(data);
2788
2789 hdmi->hdmi_data.rgb_limited_range = hdmi->sink_is_hdmi &&
2790 drm_default_rgb_quant_range(mode) ==
2791 HDMI_QUANTIZATION_RANGE_LIMITED;
2792
2793 if (!hdmi->sink_is_hdmi)
2794 hdmi->hdmi_data.quant_range = HDMI_QUANTIZATION_RANGE_FULL;
2795
2796 /*
2797 * According to the dw-hdmi specification 6.4.2
2798 * vp_pr_cd[3:0]:
2799 * 0000b: No pixel repetition (pixel sent only once)
2800 * 0001b: Pixel sent two times (pixel repeated once)
2801 */
2802 hdmi->hdmi_data.pix_repet_factor =
2803 (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 1 : 0;
2804 hdmi->hdmi_data.video_mode.mdataenablepolarity = true;
2805
2806 dw_hdmi_force_output_pattern(hdmi, mode);
2807
2808 /* HDMI Initialization Step B.1 */
2809 hdmi_av_composer(hdmi, &connector->display_info, mode);
2810
2811 /* HDMI Initialization Step B.3 */
2812 dw_hdmi_enable_video_path(hdmi);
2813
2814 if (hdmi->sink_has_audio) {
2815 dev_dbg(hdmi->dev, "sink has audio support\n");
2816
2817 /* HDMI Initialization Step E - Configure audio */
2818 hdmi_clk_regenerator_update_pixel_clock(hdmi);
2819 hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);
2820 }
2821
2822 /* not for DVI mode */
2823 if (hdmi->sink_is_hdmi) {
2824 dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);
2825
2826 /* HDMI Initialization Step F - Configure AVI InfoFrame */
2827 hdmi_config_AVI(hdmi, connector, mode);
2828 hdmi_config_vendor_specific_infoframe(hdmi, connector, mode);
2829 hdmi_config_drm_infoframe(hdmi, connector);
2830 } else {
2831 dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);
2832 }
2833
2834 hdmi_video_packetize(hdmi);
2835 hdmi_video_csc(hdmi);
2836 hdmi_video_sample(hdmi);
2837 hdmi_tx_hdcp_config(hdmi, mode);
2838
2839 /* HDMI Enable phy output */
2840 if (!hdmi->phy.enabled ||
2841 hdmi->hdmi_data.video_mode.previous_pixelclock !=
2842 hdmi->hdmi_data.video_mode.mpixelclock ||
2843 hdmi->hdmi_data.video_mode.previous_tmdsclock !=
2844 hdmi->hdmi_data.video_mode.mtmdsclock) {
2845 ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data,
2846 &connector->display_info,
2847 &hdmi->previous_mode);
2848 if (ret)
2849 return ret;
2850 hdmi->phy.enabled = true;
2851 }
2852
2853 dw_hdmi_clear_overflow(hdmi);
2854
2855 /*
2856 * konka tv should switch pattern after set to yuv420 10bit or
2857 * the TV might not recognize the signal.
2858 */
2859 if (!hdmi->update) {
2860 hdmi_writeb(hdmi, 1, HDMI_FC_DBGFORCE);
2861 msleep(50);
2862 hdmi_writeb(hdmi, 0, HDMI_FC_DBGFORCE);
2863 }
2864
2865 return 0;
2866 }
2867
initialize_hdmi_ih_mutes(struct dw_hdmi * hdmi)2868 static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)
2869 {
2870 u8 ih_mute;
2871
2872 /*
2873 * Boot up defaults are:
2874 * HDMI_IH_MUTE = 0x03 (disabled)
2875 * HDMI_IH_MUTE_* = 0x00 (enabled)
2876 *
2877 * Disable top level interrupt bits in HDMI block
2878 */
2879 ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |
2880 HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
2881 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;
2882
2883 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
2884
2885 /* by default mask all interrupts */
2886 hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);
2887 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);
2888 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);
2889 hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);
2890 hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);
2891 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);
2892 hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);
2893 hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);
2894 hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);
2895 hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);
2896 hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);
2897 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
2898 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);
2899 hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);
2900
2901 /* Disable interrupts in the IH_MUTE_* registers */
2902 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);
2903 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);
2904 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);
2905 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);
2906 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);
2907 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);
2908 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);
2909 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);
2910 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);
2911 hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
2912
2913 /* Enable top level interrupt bits in HDMI block */
2914 ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |
2915 HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);
2916 hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);
2917 }
2918
dw_hdmi_poweron(struct dw_hdmi * hdmi)2919 static void dw_hdmi_poweron(struct dw_hdmi *hdmi)
2920 {
2921 hdmi->bridge_is_on = true;
2922
2923 /*
2924 * The curr_conn field is guaranteed to be valid here, as this function
2925 * is only be called when !hdmi->disabled.
2926 */
2927 dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
2928 }
2929
dw_hdmi_poweroff(struct dw_hdmi * hdmi)2930 static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)
2931 {
2932 if (hdmi->phy.enabled) {
2933 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
2934 hdmi->phy.enabled = false;
2935 }
2936
2937 if (hdmi->hdcp && hdmi->hdcp->hdcp_stop)
2938 hdmi->hdcp->hdcp_stop(hdmi->hdcp);
2939 hdmi->bridge_is_on = false;
2940 }
2941
dw_hdmi_update_power(struct dw_hdmi * hdmi)2942 static void dw_hdmi_update_power(struct dw_hdmi *hdmi)
2943 {
2944 int force = hdmi->force;
2945
2946 if (hdmi->disabled) {
2947 force = DRM_FORCE_OFF;
2948 } else if (force == DRM_FORCE_UNSPECIFIED) {
2949 if (hdmi->rxsense)
2950 force = DRM_FORCE_ON;
2951 else
2952 force = DRM_FORCE_OFF;
2953 }
2954
2955 if (force == DRM_FORCE_OFF) {
2956 if (hdmi->initialized) {
2957 hdmi->initialized = false;
2958 hdmi->disabled = true;
2959 hdmi->logo_plug_out = true;
2960 }
2961 if (hdmi->bridge_is_on)
2962 dw_hdmi_poweroff(hdmi);
2963 } else {
2964 if (!hdmi->bridge_is_on)
2965 dw_hdmi_poweron(hdmi);
2966 }
2967 }
2968
2969 /*
2970 * Adjust the detection of RXSENSE according to whether we have a forced
2971 * connection mode enabled, or whether we have been disabled. There is
2972 * no point processing RXSENSE interrupts if we have a forced connection
2973 * state, or DRM has us disabled.
2974 *
2975 * We also disable rxsense interrupts when we think we're disconnected
2976 * to avoid floating TDMS signals giving false rxsense interrupts.
2977 *
2978 * Note: we still need to listen for HPD interrupts even when DRM has us
2979 * disabled so that we can detect a connect event.
2980 */
dw_hdmi_update_phy_mask(struct dw_hdmi * hdmi)2981 static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)
2982 {
2983 if (hdmi->phy.ops->update_hpd)
2984 hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,
2985 hdmi->force, hdmi->disabled,
2986 hdmi->rxsense);
2987 }
2988
dw_hdmi_detect(struct dw_hdmi * hdmi)2989 static enum drm_connector_status dw_hdmi_detect(struct dw_hdmi *hdmi)
2990 {
2991 enum drm_connector_status result;
2992
2993 if (!hdmi->force_logo) {
2994 mutex_lock(&hdmi->mutex);
2995 hdmi->force = DRM_FORCE_UNSPECIFIED;
2996 dw_hdmi_update_power(hdmi);
2997 dw_hdmi_update_phy_mask(hdmi);
2998 mutex_unlock(&hdmi->mutex);
2999 }
3000
3001 result = hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
3002 mutex_lock(&hdmi->mutex);
3003 if (result != hdmi->last_connector_result) {
3004 dev_dbg(hdmi->dev, "read_hpd result: %d", result);
3005 handle_plugged_change(hdmi,
3006 result == connector_status_connected);
3007 hdmi->last_connector_result = result;
3008 }
3009 mutex_unlock(&hdmi->mutex);
3010
3011 if (result == connector_status_connected)
3012 extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI, true);
3013 else
3014 extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI, false);
3015
3016 return result;
3017 }
3018
dw_hdmi_get_edid(struct dw_hdmi * hdmi,struct drm_connector * connector)3019 static struct edid *dw_hdmi_get_edid(struct dw_hdmi *hdmi,
3020 struct drm_connector *connector)
3021 {
3022 struct edid *edid;
3023
3024 if (!hdmi->ddc)
3025 return NULL;
3026
3027 edid = drm_get_edid(connector, hdmi->ddc);
3028 if (!edid) {
3029 dev_dbg(hdmi->dev, "failed to get edid\n");
3030 return NULL;
3031 }
3032
3033 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
3034 edid->width_cm, edid->height_cm);
3035
3036 hdmi->support_hdmi = drm_detect_hdmi_monitor(edid);
3037 hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
3038
3039 return edid;
3040 }
3041
3042 /* -----------------------------------------------------------------------------
3043 * DRM Connector Operations
3044 */
3045
3046 static enum drm_connector_status
dw_hdmi_connector_detect(struct drm_connector * connector,bool force)3047 dw_hdmi_connector_detect(struct drm_connector *connector, bool force)
3048 {
3049 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3050 connector);
3051 return dw_hdmi_detect(hdmi);
3052 }
3053
3054 static int
dw_hdmi_update_hdr_property(struct drm_connector * connector)3055 dw_hdmi_update_hdr_property(struct drm_connector *connector)
3056 {
3057 struct drm_device *dev = connector->dev;
3058 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3059 connector);
3060 void *data = hdmi->plat_data->phy_data;
3061 const struct hdr_static_metadata *metadata =
3062 &connector->hdr_sink_metadata.hdmi_type1;
3063 size_t size = sizeof(*metadata);
3064 struct drm_property *property;
3065 struct drm_property_blob *blob;
3066 int ret;
3067
3068 if (hdmi->plat_data->get_hdr_property)
3069 property = hdmi->plat_data->get_hdr_property(data);
3070 else
3071 return -EINVAL;
3072
3073 if (hdmi->plat_data->get_hdr_blob)
3074 blob = hdmi->plat_data->get_hdr_blob(data);
3075 else
3076 return -EINVAL;
3077
3078 ret = drm_property_replace_global_blob(dev, &blob, size, metadata,
3079 &connector->base, property);
3080 return ret;
3081 }
3082
dw_hdmi_connector_get_modes(struct drm_connector * connector)3083 static int dw_hdmi_connector_get_modes(struct drm_connector *connector)
3084 {
3085 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3086 connector);
3087 struct hdr_static_metadata *metedata =
3088 &connector->hdr_sink_metadata.hdmi_type1;
3089 struct edid *edid;
3090 struct drm_display_mode *mode;
3091 struct drm_display_info *info = &connector->display_info;
3092 void *data = hdmi->plat_data->phy_data;
3093 int i, ret = 0;
3094
3095 memset(metedata, 0, sizeof(*metedata));
3096 edid = dw_hdmi_get_edid(hdmi, connector);
3097 if (edid) {
3098 int vic = 0;
3099
3100 dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",
3101 edid->width_cm, edid->height_cm);
3102 drm_connector_update_edid_property(connector, edid);
3103 cec_notifier_set_phys_addr_from_edid(hdmi->cec_notifier, edid);
3104 ret = drm_add_edid_modes(connector, edid);
3105 if (hdmi->plat_data->get_color_changed)
3106 hdmi->plat_data->get_yuv422_format(connector, edid);
3107 if (hdmi->plat_data->get_colorimetry)
3108 hdmi->plat_data->get_colorimetry(data, edid);
3109
3110 list_for_each_entry(mode, &connector->probed_modes, head) {
3111 vic = drm_match_cea_mode(mode);
3112
3113 if (mode->picture_aspect_ratio == HDMI_PICTURE_ASPECT_NONE) {
3114 if (vic >= 93 && vic <= 95)
3115 mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_16_9;
3116 else if (vic == 98)
3117 mode->picture_aspect_ratio = HDMI_PICTURE_ASPECT_256_135;
3118 }
3119 }
3120
3121 kfree(edid);
3122 } else {
3123 hdmi->support_hdmi = true;
3124 hdmi->sink_has_audio = true;
3125 for (i = 0; i < ARRAY_SIZE(dw_hdmi_default_modes); i++) {
3126 const struct drm_display_mode *ptr =
3127 &dw_hdmi_default_modes[i];
3128
3129 mode = drm_mode_duplicate(connector->dev, ptr);
3130 if (mode) {
3131 if (!i)
3132 mode->type = DRM_MODE_TYPE_PREFERRED;
3133 drm_mode_probed_add(connector, mode);
3134 ret++;
3135 }
3136 }
3137 info->edid_hdmi_dc_modes = 0;
3138 info->hdmi.y420_dc_modes = 0;
3139 info->color_formats = 0;
3140
3141 dev_info(hdmi->dev, "failed to get edid\n");
3142 }
3143 dw_hdmi_update_hdr_property(connector);
3144 dw_hdmi_check_output_type_changed(hdmi);
3145
3146 return ret;
3147 }
3148
3149 static struct drm_encoder *
dw_hdmi_connector_best_encoder(struct drm_connector * connector)3150 dw_hdmi_connector_best_encoder(struct drm_connector *connector)
3151 {
3152 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3153 connector);
3154
3155 return hdmi->bridge.encoder;
3156 }
3157
dw_hdmi_color_changed(struct drm_connector * connector)3158 static bool dw_hdmi_color_changed(struct drm_connector *connector)
3159 {
3160 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3161 connector);
3162 void *data = hdmi->plat_data->phy_data;
3163 bool ret = false;
3164
3165 if (hdmi->plat_data->get_color_changed)
3166 ret = hdmi->plat_data->get_color_changed(data);
3167
3168 return ret;
3169 }
3170
hdr_metadata_equal(struct dw_hdmi * hdmi,const struct drm_connector_state * old_state,const struct drm_connector_state * new_state)3171 static bool hdr_metadata_equal(struct dw_hdmi *hdmi, const struct drm_connector_state *old_state,
3172 const struct drm_connector_state *new_state)
3173 {
3174 struct drm_property_blob *old_blob = old_state->hdr_output_metadata;
3175 struct drm_property_blob *new_blob = new_state->hdr_output_metadata;
3176 int i, ret;
3177 u8 *data;
3178
3179 hdmi->hdr2sdr = false;
3180
3181 if (!old_blob && !new_blob)
3182 return true;
3183
3184 if (!old_blob) {
3185 data = (u8 *)new_blob->data;
3186
3187 for (i = 0; i < new_blob->length; i++)
3188 if (data[i])
3189 return false;
3190
3191 return true;
3192 }
3193
3194 if (!new_blob) {
3195 data = (u8 *)old_blob->data;
3196
3197 for (i = 0; i < old_blob->length; i++)
3198 if (data[i])
3199 return false;
3200
3201 return true;
3202 }
3203
3204 if (old_blob->length != new_blob->length)
3205 return false;
3206
3207 ret = !memcmp(old_blob->data, new_blob->data, old_blob->length);
3208
3209 if (!ret && new_blob) {
3210 data = (u8 *)new_blob->data;
3211
3212 for (i = 0; i < new_blob->length; i++)
3213 if (data[i])
3214 break;
3215
3216 if (i == new_blob->length)
3217 hdmi->hdr2sdr = true;
3218 }
3219
3220 return ret;
3221 }
3222
check_hdr_color_change(struct drm_connector_state * old_state,struct drm_connector_state * new_state,struct dw_hdmi * hdmi)3223 static bool check_hdr_color_change(struct drm_connector_state *old_state,
3224 struct drm_connector_state *new_state,
3225 struct dw_hdmi *hdmi)
3226 {
3227 void *data = hdmi->plat_data->phy_data;
3228
3229 if (!hdr_metadata_equal(hdmi, old_state, new_state)) {
3230 hdmi->plat_data->check_hdr_color_change(new_state, data);
3231 return true;
3232 }
3233
3234 return false;
3235 }
3236
dw_hdmi_connector_atomic_check(struct drm_connector * connector,struct drm_atomic_state * state)3237 static int dw_hdmi_connector_atomic_check(struct drm_connector *connector,
3238 struct drm_atomic_state *state)
3239 {
3240 struct drm_connector_state *old_state =
3241 drm_atomic_get_old_connector_state(state, connector);
3242 struct drm_connector_state *new_state =
3243 drm_atomic_get_new_connector_state(state, connector);
3244 struct drm_crtc *crtc = new_state->crtc;
3245 struct drm_crtc_state *crtc_state;
3246 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3247 connector);
3248 struct drm_display_mode *mode = NULL;
3249 void *data = hdmi->plat_data->phy_data;
3250 struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;
3251
3252 if (!crtc)
3253 return 0;
3254
3255 crtc_state = drm_atomic_get_crtc_state(state, crtc);
3256 if (IS_ERR(crtc_state))
3257 return PTR_ERR(crtc_state);
3258
3259 mode = &crtc_state->mode;
3260
3261 /*
3262 * If HDMI is enabled in uboot, it's need to record
3263 * drm_display_mode and set phy status to enabled.
3264 */
3265 if (!vmode->mpixelclock) {
3266 u8 val;
3267
3268 hdmi->curr_conn = connector;
3269
3270 if (hdmi->plat_data->get_enc_in_encoding)
3271 hdmi->hdmi_data.enc_in_encoding =
3272 hdmi->plat_data->get_enc_in_encoding(data);
3273 if (hdmi->plat_data->get_enc_out_encoding)
3274 hdmi->hdmi_data.enc_out_encoding =
3275 hdmi->plat_data->get_enc_out_encoding(data);
3276 if (hdmi->plat_data->get_input_bus_format)
3277 hdmi->hdmi_data.enc_in_bus_format =
3278 hdmi->plat_data->get_input_bus_format(data);
3279 if (hdmi->plat_data->get_output_bus_format)
3280 hdmi->hdmi_data.enc_out_bus_format =
3281 hdmi->plat_data->get_output_bus_format(data);
3282
3283 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
3284 vmode->mpixelclock = mode->crtc_clock * 1000;
3285 vmode->previous_pixelclock = mode->clock * 1000;
3286 vmode->previous_tmdsclock = mode->clock * 1000;
3287 vmode->mtmdsclock = hdmi_get_tmdsclock(hdmi,
3288 vmode->mpixelclock);
3289 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
3290 vmode->mtmdsclock /= 2;
3291
3292 dw_hdmi_force_output_pattern(hdmi, mode);
3293 drm_scdc_readb(hdmi->ddc, SCDC_TMDS_CONFIG, &val);
3294
3295 /* if plug out before hdmi bind, reset hdmi */
3296 if (vmode->mtmdsclock >= 340000000 && !(val & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40))
3297 hdmi->logo_plug_out = true;
3298 }
3299
3300 if (check_hdr_color_change(old_state, new_state, hdmi) || hdmi->logo_plug_out ||
3301 dw_hdmi_color_changed(connector)) {
3302 u32 mtmdsclk;
3303
3304 if (hdmi->plat_data->update_color_format)
3305 hdmi->plat_data->update_color_format(new_state, data);
3306 if (hdmi->plat_data->get_enc_in_encoding)
3307 hdmi->hdmi_data.enc_in_encoding =
3308 hdmi->plat_data->get_enc_in_encoding(data);
3309 if (hdmi->plat_data->get_enc_out_encoding)
3310 hdmi->hdmi_data.enc_out_encoding =
3311 hdmi->plat_data->get_enc_out_encoding(data);
3312 if (hdmi->plat_data->get_input_bus_format)
3313 hdmi->hdmi_data.enc_in_bus_format =
3314 hdmi->plat_data->get_input_bus_format(data);
3315 if (hdmi->plat_data->get_output_bus_format)
3316 hdmi->hdmi_data.enc_out_bus_format =
3317 hdmi->plat_data->get_output_bus_format(data);
3318
3319 mtmdsclk = hdmi_get_tmdsclock(hdmi, mode->clock);
3320
3321 if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
3322 mtmdsclk /= 2;
3323
3324 if (!(hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD))
3325 return 0;
3326
3327 if (hdmi->hdmi_data.video_mode.mpixelclock == (mode->clock * 1000) &&
3328 hdmi->hdmi_data.video_mode.mtmdsclock == (mtmdsclk * 1000) &&
3329 !hdmi->logo_plug_out && !hdmi->disabled) {
3330 hdmi->update = true;
3331 hdmi_writeb(hdmi, HDMI_FC_GCP_SET_AVMUTE, HDMI_FC_GCP);
3332 mdelay(180);
3333 handle_plugged_change(hdmi, false);
3334 } else {
3335 hdmi->update = false;
3336 crtc_state->mode_changed = true;
3337 hdmi->logo_plug_out = false;
3338 }
3339 }
3340
3341 return 0;
3342 }
3343
3344 static int
dw_hdmi_atomic_connector_set_property(struct drm_connector * connector,struct drm_connector_state * state,struct drm_property * property,uint64_t val)3345 dw_hdmi_atomic_connector_set_property(struct drm_connector *connector,
3346 struct drm_connector_state *state,
3347 struct drm_property *property,
3348 uint64_t val)
3349 {
3350 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3351 connector);
3352 const struct dw_hdmi_property_ops *ops =
3353 hdmi->plat_data->property_ops;
3354
3355 if (ops && ops->set_property)
3356 return ops->set_property(connector, state, property,
3357 val, hdmi->plat_data->phy_data);
3358 else
3359 return -EINVAL;
3360 }
3361
3362 static int
dw_hdmi_atomic_connector_get_property(struct drm_connector * connector,const struct drm_connector_state * state,struct drm_property * property,uint64_t * val)3363 dw_hdmi_atomic_connector_get_property(struct drm_connector *connector,
3364 const struct drm_connector_state *state,
3365 struct drm_property *property,
3366 uint64_t *val)
3367 {
3368 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3369 connector);
3370 const struct dw_hdmi_property_ops *ops =
3371 hdmi->plat_data->property_ops;
3372
3373 if (ops && ops->get_property)
3374 return ops->get_property(connector, state, property,
3375 val, hdmi->plat_data->phy_data);
3376 else
3377 return -EINVAL;
3378 }
3379
3380 static int
dw_hdmi_connector_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t val)3381 dw_hdmi_connector_set_property(struct drm_connector *connector,
3382 struct drm_property *property, uint64_t val)
3383 {
3384 return dw_hdmi_atomic_connector_set_property(connector, NULL,
3385 property, val);
3386 }
3387
dw_hdmi_connector_atomic_commit(struct drm_connector * connector,struct drm_connector_state * state)3388 static void dw_hdmi_connector_atomic_commit(struct drm_connector *connector,
3389 struct drm_connector_state *state)
3390 {
3391 struct dw_hdmi *hdmi =
3392 container_of(connector, struct dw_hdmi, connector);
3393
3394 if (hdmi->update) {
3395 dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
3396 mdelay(50);
3397 handle_plugged_change(hdmi, true);
3398 hdmi_writeb(hdmi, HDMI_FC_GCP_CLEAR_AVMUTE, HDMI_FC_GCP);
3399 hdmi->update = false;
3400 }
3401 }
3402
dw_hdmi_set_quant_range(struct dw_hdmi * hdmi)3403 void dw_hdmi_set_quant_range(struct dw_hdmi *hdmi)
3404 {
3405 if (!hdmi->bridge_is_on)
3406 return;
3407
3408 hdmi_writeb(hdmi, HDMI_FC_GCP_SET_AVMUTE, HDMI_FC_GCP);
3409 dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
3410 hdmi_writeb(hdmi, HDMI_FC_GCP_CLEAR_AVMUTE, HDMI_FC_GCP);
3411 }
3412 EXPORT_SYMBOL_GPL(dw_hdmi_set_quant_range);
3413
dw_hdmi_set_output_type(struct dw_hdmi * hdmi,u64 val)3414 void dw_hdmi_set_output_type(struct dw_hdmi *hdmi, u64 val)
3415 {
3416 hdmi->force_output = val;
3417
3418 if (!dw_hdmi_check_output_type_changed(hdmi))
3419 return;
3420
3421 if (!hdmi->bridge_is_on)
3422 return;
3423
3424 hdmi_writeb(hdmi, HDMI_FC_GCP_SET_AVMUTE, HDMI_FC_GCP);
3425 dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);
3426 hdmi_writeb(hdmi, HDMI_FC_GCP_CLEAR_AVMUTE, HDMI_FC_GCP);
3427 }
3428 EXPORT_SYMBOL_GPL(dw_hdmi_set_output_type);
3429
dw_hdmi_get_output_whether_hdmi(struct dw_hdmi * hdmi)3430 bool dw_hdmi_get_output_whether_hdmi(struct dw_hdmi *hdmi)
3431 {
3432 return hdmi->sink_is_hdmi;
3433 }
3434 EXPORT_SYMBOL_GPL(dw_hdmi_get_output_whether_hdmi);
3435
dw_hdmi_get_output_type_cap(struct dw_hdmi * hdmi)3436 int dw_hdmi_get_output_type_cap(struct dw_hdmi *hdmi)
3437 {
3438 return hdmi->support_hdmi;
3439 }
3440 EXPORT_SYMBOL_GPL(dw_hdmi_get_output_type_cap);
3441
dw_hdmi_set_hpd_wake(struct dw_hdmi * hdmi)3442 void dw_hdmi_set_hpd_wake(struct dw_hdmi *hdmi)
3443 {
3444 if (!hdmi->cec)
3445 return;
3446
3447 if (!hdmi->cec_ops)
3448 return;
3449
3450 if (hdmi->cec_ops->hpd_wake_up)
3451 hdmi->cec_ops->hpd_wake_up(hdmi->cec);
3452 }
3453 EXPORT_SYMBOL_GPL(dw_hdmi_set_hpd_wake);
3454
dw_hdmi_connector_force(struct drm_connector * connector)3455 static void dw_hdmi_connector_force(struct drm_connector *connector)
3456 {
3457 struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,
3458 connector);
3459
3460 mutex_lock(&hdmi->mutex);
3461
3462 if (hdmi->force != connector->force) {
3463 if (!hdmi->disabled && connector->force == DRM_FORCE_OFF)
3464 extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI,
3465 false);
3466 else if (hdmi->disabled && connector->force == DRM_FORCE_ON)
3467 extcon_set_state_sync(hdmi->extcon, EXTCON_DISP_HDMI,
3468 true);
3469 }
3470
3471 hdmi->force = connector->force;
3472 dw_hdmi_update_power(hdmi);
3473 dw_hdmi_update_phy_mask(hdmi);
3474 mutex_unlock(&hdmi->mutex);
3475 }
3476
3477 static const struct drm_connector_funcs dw_hdmi_connector_funcs = {
3478 .fill_modes = drm_helper_probe_single_connector_modes,
3479 .detect = dw_hdmi_connector_detect,
3480 .destroy = drm_connector_cleanup,
3481 .force = dw_hdmi_connector_force,
3482 .reset = drm_atomic_helper_connector_reset,
3483 .set_property = dw_hdmi_connector_set_property,
3484 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
3485 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
3486 .atomic_set_property = dw_hdmi_atomic_connector_set_property,
3487 .atomic_get_property = dw_hdmi_atomic_connector_get_property,
3488 };
3489
3490 static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {
3491 .get_modes = dw_hdmi_connector_get_modes,
3492 .best_encoder = dw_hdmi_connector_best_encoder,
3493 .atomic_check = dw_hdmi_connector_atomic_check,
3494 .atomic_commit = dw_hdmi_connector_atomic_commit,
3495 };
3496
dw_hdmi_attach_properties(struct dw_hdmi * hdmi)3497 static void dw_hdmi_attach_properties(struct dw_hdmi *hdmi)
3498 {
3499 unsigned int color = MEDIA_BUS_FMT_RGB888_1X24;
3500 int video_mapping, colorspace;
3501 enum drm_connector_status connect_status =
3502 hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);
3503 const struct dw_hdmi_property_ops *ops =
3504 hdmi->plat_data->property_ops;
3505
3506 if (connect_status == connector_status_connected) {
3507 video_mapping = (hdmi_readb(hdmi, HDMI_TX_INVID0) &
3508 HDMI_TX_INVID0_VIDEO_MAPPING_MASK);
3509 colorspace = (hdmi_readb(hdmi, HDMI_FC_AVICONF0) &
3510 HDMI_FC_AVICONF0_PIX_FMT_MASK);
3511 switch (video_mapping) {
3512 case 0x01:
3513 color = MEDIA_BUS_FMT_RGB888_1X24;
3514 break;
3515 case 0x03:
3516 color = MEDIA_BUS_FMT_RGB101010_1X30;
3517 break;
3518 case 0x09:
3519 if (colorspace == HDMI_COLORSPACE_YUV420)
3520 color = MEDIA_BUS_FMT_UYYVYY8_0_5X24;
3521 else if (colorspace == HDMI_COLORSPACE_YUV422)
3522 color = MEDIA_BUS_FMT_UYVY8_1X16;
3523 else
3524 color = MEDIA_BUS_FMT_YUV8_1X24;
3525 break;
3526 case 0x0b:
3527 if (colorspace == HDMI_COLORSPACE_YUV420)
3528 color = MEDIA_BUS_FMT_UYYVYY10_0_5X30;
3529 else if (colorspace == HDMI_COLORSPACE_YUV422)
3530 color = MEDIA_BUS_FMT_UYVY10_1X20;
3531 else
3532 color = MEDIA_BUS_FMT_YUV10_1X30;
3533 break;
3534 case 0x14:
3535 color = MEDIA_BUS_FMT_UYVY10_1X20;
3536 break;
3537 case 0x16:
3538 color = MEDIA_BUS_FMT_UYVY8_1X16;
3539 break;
3540 default:
3541 color = MEDIA_BUS_FMT_RGB888_1X24;
3542 dev_err(hdmi->dev, "unexpected mapping: 0x%x\n",
3543 video_mapping);
3544 }
3545
3546 hdmi->hdmi_data.enc_in_bus_format = color;
3547 hdmi->hdmi_data.enc_out_bus_format = color;
3548 /*
3549 * input format will be set as yuv444 when output
3550 * format is yuv420
3551 */
3552 if (color == MEDIA_BUS_FMT_UYVY10_1X20)
3553 hdmi->hdmi_data.enc_in_bus_format =
3554 MEDIA_BUS_FMT_YUV10_1X30;
3555 else if (color == MEDIA_BUS_FMT_UYVY8_1X16)
3556 hdmi->hdmi_data.enc_in_bus_format =
3557 MEDIA_BUS_FMT_YUV8_1X24;
3558 }
3559
3560 if (ops && ops->attach_properties)
3561 return ops->attach_properties(&hdmi->connector,
3562 color, hdmi->version,
3563 hdmi->plat_data->phy_data, 0);
3564 }
3565
dw_hdmi_destroy_properties(struct dw_hdmi * hdmi)3566 static void dw_hdmi_destroy_properties(struct dw_hdmi *hdmi)
3567 {
3568 const struct dw_hdmi_property_ops *ops =
3569 hdmi->plat_data->property_ops;
3570
3571 if (ops && ops->destroy_properties)
3572 return ops->destroy_properties(&hdmi->connector,
3573 hdmi->plat_data->phy_data);
3574 }
3575
dw_hdmi_connector_create(struct dw_hdmi * hdmi)3576 static int dw_hdmi_connector_create(struct dw_hdmi *hdmi)
3577 {
3578 struct drm_connector *connector = &hdmi->connector;
3579 struct cec_connector_info conn_info;
3580 struct cec_notifier *notifier;
3581
3582 if (hdmi->version >= 0x200a)
3583 connector->ycbcr_420_allowed =
3584 hdmi->plat_data->ycbcr_420_allowed;
3585 else
3586 connector->ycbcr_420_allowed = false;
3587
3588 connector->interlace_allowed = 1;
3589 connector->polled = DRM_CONNECTOR_POLL_HPD;
3590
3591 drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);
3592
3593 drm_connector_init_with_ddc(hdmi->bridge.dev, connector,
3594 &dw_hdmi_connector_funcs,
3595 DRM_MODE_CONNECTOR_HDMIA,
3596 hdmi->ddc);
3597
3598 /*
3599 * drm_connector_attach_max_bpc_property() requires the
3600 * connector to have a state.
3601 */
3602 drm_atomic_helper_connector_reset(connector);
3603
3604 drm_connector_attach_max_bpc_property(connector, 8, 16);
3605
3606 if (hdmi->version >= 0x200a && hdmi->plat_data->use_drm_infoframe)
3607 drm_object_attach_property(&connector->base,
3608 connector->dev->mode_config.hdr_output_metadata_property, 0);
3609
3610 drm_connector_attach_encoder(connector, hdmi->bridge.encoder);
3611
3612 dw_hdmi_attach_properties(hdmi);
3613
3614 cec_fill_conn_info_from_drm(&conn_info, connector);
3615
3616 notifier = cec_notifier_conn_register(hdmi->dev, NULL, &conn_info);
3617 if (!notifier)
3618 return -ENOMEM;
3619
3620 mutex_lock(&hdmi->cec_notifier_mutex);
3621 hdmi->cec_notifier = notifier;
3622 mutex_unlock(&hdmi->cec_notifier_mutex);
3623
3624 return 0;
3625 }
3626
3627 /* -----------------------------------------------------------------------------
3628 * DRM Bridge Operations
3629 */
3630
3631 /*
3632 * Possible output formats :
3633 * - MEDIA_BUS_FMT_UYYVYY16_0_5X48,
3634 * - MEDIA_BUS_FMT_UYYVYY12_0_5X36,
3635 * - MEDIA_BUS_FMT_UYYVYY10_0_5X30,
3636 * - MEDIA_BUS_FMT_UYYVYY8_0_5X24,
3637 * - MEDIA_BUS_FMT_YUV16_1X48,
3638 * - MEDIA_BUS_FMT_RGB161616_1X48,
3639 * - MEDIA_BUS_FMT_UYVY12_1X24,
3640 * - MEDIA_BUS_FMT_YUV12_1X36,
3641 * - MEDIA_BUS_FMT_RGB121212_1X36,
3642 * - MEDIA_BUS_FMT_UYVY10_1X20,
3643 * - MEDIA_BUS_FMT_YUV10_1X30,
3644 * - MEDIA_BUS_FMT_RGB101010_1X30,
3645 * - MEDIA_BUS_FMT_UYVY8_1X16,
3646 * - MEDIA_BUS_FMT_YUV8_1X24,
3647 * - MEDIA_BUS_FMT_RGB888_1X24,
3648 */
3649
3650 /* Can return a maximum of 11 possible output formats for a mode/connector */
3651 #define MAX_OUTPUT_SEL_FORMATS 11
3652
dw_hdmi_bridge_atomic_get_output_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,unsigned int * num_output_fmts)3653 static u32 *dw_hdmi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,
3654 struct drm_bridge_state *bridge_state,
3655 struct drm_crtc_state *crtc_state,
3656 struct drm_connector_state *conn_state,
3657 unsigned int *num_output_fmts)
3658 {
3659 struct drm_connector *conn = conn_state->connector;
3660 struct drm_display_info *info = &conn->display_info;
3661 struct drm_display_mode *mode = &crtc_state->mode;
3662 u8 max_bpc = conn_state->max_requested_bpc;
3663 bool is_hdmi2_sink = info->hdmi.scdc.supported ||
3664 (info->color_formats & DRM_COLOR_FORMAT_YCRCB420);
3665 u32 *output_fmts;
3666 unsigned int i = 0;
3667
3668 *num_output_fmts = 0;
3669
3670 output_fmts = kcalloc(MAX_OUTPUT_SEL_FORMATS, sizeof(*output_fmts),
3671 GFP_KERNEL);
3672 if (!output_fmts)
3673 return NULL;
3674
3675 /* If dw-hdmi is the first or only bridge, avoid negociating with ourselves */
3676 if (list_is_singular(&bridge->encoder->bridge_chain) ||
3677 list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain)) {
3678 *num_output_fmts = 1;
3679 output_fmts[0] = MEDIA_BUS_FMT_FIXED;
3680
3681 return output_fmts;
3682 }
3683
3684 /*
3685 * If the current mode enforces 4:2:0, force the output but format
3686 * to 4:2:0 and do not add the YUV422/444/RGB formats
3687 */
3688 if (conn->ycbcr_420_allowed &&
3689 (drm_mode_is_420_only(info, mode) ||
3690 (is_hdmi2_sink && drm_mode_is_420_also(info, mode)))) {
3691
3692 /* Order bus formats from 16bit to 8bit if supported */
3693 if (max_bpc >= 16 && info->bpc == 16 &&
3694 (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48))
3695 output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY16_0_5X48;
3696
3697 if (max_bpc >= 12 && info->bpc >= 12 &&
3698 (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36))
3699 output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY12_0_5X36;
3700
3701 if (max_bpc >= 10 && info->bpc >= 10 &&
3702 (info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30))
3703 output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY10_0_5X30;
3704
3705 /* Default 8bit fallback */
3706 output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY8_0_5X24;
3707
3708 *num_output_fmts = i;
3709
3710 return output_fmts;
3711 }
3712
3713 /*
3714 * Order bus formats from 16bit to 8bit and from YUV422 to RGB
3715 * if supported. In any case the default RGB888 format is added
3716 */
3717
3718 /* Default 8bit RGB fallback */
3719 output_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
3720
3721 if (max_bpc >= 16 && info->bpc == 16) {
3722 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB444)
3723 output_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
3724
3725 output_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
3726 }
3727
3728 if (max_bpc >= 12 && info->bpc >= 12) {
3729 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB422)
3730 output_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
3731
3732 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB444)
3733 output_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
3734
3735 output_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
3736 }
3737
3738 if (max_bpc >= 10 && info->bpc >= 10) {
3739 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB422)
3740 output_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
3741
3742 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB444)
3743 output_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
3744
3745 output_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
3746 }
3747
3748 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB422)
3749 output_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
3750
3751 if (info->color_formats & DRM_COLOR_FORMAT_YCRCB444)
3752 output_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
3753
3754 *num_output_fmts = i;
3755
3756 return output_fmts;
3757 }
3758
3759 /*
3760 * Possible input formats :
3761 * - MEDIA_BUS_FMT_RGB888_1X24
3762 * - MEDIA_BUS_FMT_YUV8_1X24
3763 * - MEDIA_BUS_FMT_UYVY8_1X16
3764 * - MEDIA_BUS_FMT_UYYVYY8_0_5X24
3765 * - MEDIA_BUS_FMT_RGB101010_1X30
3766 * - MEDIA_BUS_FMT_YUV10_1X30
3767 * - MEDIA_BUS_FMT_UYVY10_1X20
3768 * - MEDIA_BUS_FMT_UYYVYY10_0_5X30
3769 * - MEDIA_BUS_FMT_RGB121212_1X36
3770 * - MEDIA_BUS_FMT_YUV12_1X36
3771 * - MEDIA_BUS_FMT_UYVY12_1X24
3772 * - MEDIA_BUS_FMT_UYYVYY12_0_5X36
3773 * - MEDIA_BUS_FMT_RGB161616_1X48
3774 * - MEDIA_BUS_FMT_YUV16_1X48
3775 * - MEDIA_BUS_FMT_UYYVYY16_0_5X48
3776 */
3777
3778 /* Can return a maximum of 3 possible input formats for an output format */
3779 #define MAX_INPUT_SEL_FORMATS 3
3780
dw_hdmi_bridge_atomic_get_input_bus_fmts(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state,u32 output_fmt,unsigned int * num_input_fmts)3781 static u32 *dw_hdmi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
3782 struct drm_bridge_state *bridge_state,
3783 struct drm_crtc_state *crtc_state,
3784 struct drm_connector_state *conn_state,
3785 u32 output_fmt,
3786 unsigned int *num_input_fmts)
3787 {
3788 u32 *input_fmts;
3789 unsigned int i = 0;
3790
3791 *num_input_fmts = 0;
3792
3793 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
3794 GFP_KERNEL);
3795 if (!input_fmts)
3796 return NULL;
3797
3798 switch (output_fmt) {
3799 /* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */
3800 case MEDIA_BUS_FMT_FIXED:
3801 input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
3802 break;
3803 /* 8bit */
3804 case MEDIA_BUS_FMT_RGB888_1X24:
3805 input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
3806 input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
3807 input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
3808 break;
3809 case MEDIA_BUS_FMT_YUV8_1X24:
3810 input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
3811 input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
3812 input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
3813 break;
3814 case MEDIA_BUS_FMT_UYVY8_1X16:
3815 input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;
3816 input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;
3817 input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;
3818 break;
3819
3820 /* 10bit */
3821 case MEDIA_BUS_FMT_RGB101010_1X30:
3822 input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
3823 input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
3824 input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
3825 break;
3826 case MEDIA_BUS_FMT_YUV10_1X30:
3827 input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
3828 input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
3829 input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
3830 break;
3831 case MEDIA_BUS_FMT_UYVY10_1X20:
3832 input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;
3833 input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;
3834 input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;
3835 break;
3836
3837 /* 12bit */
3838 case MEDIA_BUS_FMT_RGB121212_1X36:
3839 input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
3840 input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
3841 input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
3842 break;
3843 case MEDIA_BUS_FMT_YUV12_1X36:
3844 input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
3845 input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
3846 input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
3847 break;
3848 case MEDIA_BUS_FMT_UYVY12_1X24:
3849 input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;
3850 input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;
3851 input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;
3852 break;
3853
3854 /* 16bit */
3855 case MEDIA_BUS_FMT_RGB161616_1X48:
3856 input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
3857 input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
3858 break;
3859 case MEDIA_BUS_FMT_YUV16_1X48:
3860 input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;
3861 input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;
3862 break;
3863
3864 /*YUV 4:2:0 */
3865 case MEDIA_BUS_FMT_UYYVYY8_0_5X24:
3866 case MEDIA_BUS_FMT_UYYVYY10_0_5X30:
3867 case MEDIA_BUS_FMT_UYYVYY12_0_5X36:
3868 case MEDIA_BUS_FMT_UYYVYY16_0_5X48:
3869 input_fmts[i++] = output_fmt;
3870 break;
3871 }
3872
3873 *num_input_fmts = i;
3874
3875 if (*num_input_fmts == 0) {
3876 kfree(input_fmts);
3877 input_fmts = NULL;
3878 }
3879
3880 return input_fmts;
3881 }
3882
dw_hdmi_bridge_atomic_check(struct drm_bridge * bridge,struct drm_bridge_state * bridge_state,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)3883 static int dw_hdmi_bridge_atomic_check(struct drm_bridge *bridge,
3884 struct drm_bridge_state *bridge_state,
3885 struct drm_crtc_state *crtc_state,
3886 struct drm_connector_state *conn_state)
3887 {
3888 struct dw_hdmi *hdmi = bridge->driver_private;
3889 void *data = hdmi->plat_data->phy_data;
3890
3891 if (bridge_state->output_bus_cfg.format == MEDIA_BUS_FMT_FIXED) {
3892 if (hdmi->plat_data->get_output_bus_format)
3893 hdmi->hdmi_data.enc_out_bus_format =
3894 hdmi->plat_data->get_output_bus_format(data);
3895 else
3896 hdmi->hdmi_data.enc_out_bus_format =
3897 MEDIA_BUS_FMT_RGB888_1X24;
3898
3899 if (hdmi->plat_data->get_input_bus_format)
3900 hdmi->hdmi_data.enc_in_bus_format =
3901 hdmi->plat_data->get_input_bus_format(data);
3902 else if (hdmi->plat_data->input_bus_format)
3903 hdmi->hdmi_data.enc_in_bus_format =
3904 hdmi->plat_data->input_bus_format;
3905 else
3906 hdmi->hdmi_data.enc_in_bus_format =
3907 MEDIA_BUS_FMT_RGB888_1X24;
3908 } else {
3909 hdmi->hdmi_data.enc_out_bus_format =
3910 bridge_state->output_bus_cfg.format;
3911
3912 hdmi->hdmi_data.enc_in_bus_format =
3913 bridge_state->input_bus_cfg.format;
3914
3915 dev_dbg(hdmi->dev, "input format 0x%04x, output format 0x%04x\n",
3916 bridge_state->input_bus_cfg.format,
3917 bridge_state->output_bus_cfg.format);
3918 }
3919
3920 return 0;
3921 }
3922
dw_hdmi_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)3923 static int dw_hdmi_bridge_attach(struct drm_bridge *bridge,
3924 enum drm_bridge_attach_flags flags)
3925 {
3926 struct dw_hdmi *hdmi = bridge->driver_private;
3927 int ret;
3928
3929 if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)
3930 return 0;
3931
3932 if (hdmi->next_bridge) {
3933 hdmi->next_bridge->encoder = bridge->encoder;
3934 ret = drm_bridge_attach(bridge->encoder, hdmi->next_bridge, bridge, flags);
3935 if (ret) {
3936 DRM_ERROR("Failed to attach bridge with dw-hdmi\n");
3937 return ret;
3938 }
3939
3940 return 0;
3941 }
3942
3943 return dw_hdmi_connector_create(hdmi);
3944 }
3945
dw_hdmi_bridge_detach(struct drm_bridge * bridge)3946 static void dw_hdmi_bridge_detach(struct drm_bridge *bridge)
3947 {
3948 struct dw_hdmi *hdmi = bridge->driver_private;
3949
3950 mutex_lock(&hdmi->cec_notifier_mutex);
3951 cec_notifier_conn_unregister(hdmi->cec_notifier);
3952 hdmi->cec_notifier = NULL;
3953 mutex_unlock(&hdmi->cec_notifier_mutex);
3954 }
3955
3956 static enum drm_mode_status
dw_hdmi_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)3957 dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,
3958 const struct drm_display_info *info,
3959 const struct drm_display_mode *mode)
3960 {
3961 struct dw_hdmi *hdmi = bridge->driver_private;
3962 const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;
3963 enum drm_mode_status mode_status = MODE_OK;
3964
3965 if (hdmi->next_bridge)
3966 return MODE_OK;
3967
3968 if (!(hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD) && hdmi->hdr2sdr)
3969 return MODE_OK;
3970
3971 if (pdata->mode_valid)
3972 mode_status = pdata->mode_valid(hdmi, pdata->priv_data, info,
3973 mode);
3974
3975 return mode_status;
3976 }
3977
dw_hdmi_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * orig_mode,const struct drm_display_mode * mode)3978 static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,
3979 const struct drm_display_mode *orig_mode,
3980 const struct drm_display_mode *mode)
3981 {
3982 struct dw_hdmi *hdmi = bridge->driver_private;
3983
3984 mutex_lock(&hdmi->mutex);
3985
3986 /* Store the display mode for plugin/DKMS poweron events */
3987 memcpy(&hdmi->previous_mode, mode, sizeof(hdmi->previous_mode));
3988
3989 mutex_unlock(&hdmi->mutex);
3990 }
3991
dw_hdmi_bridge_atomic_disable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)3992 static void dw_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,
3993 struct drm_bridge_state *old_state)
3994 {
3995 struct dw_hdmi *hdmi = bridge->driver_private;
3996 void *data = hdmi->plat_data->phy_data;
3997
3998 mutex_lock(&hdmi->mutex);
3999 hdmi->disabled = true;
4000 handle_plugged_change(hdmi, false);
4001 hdmi->curr_conn = NULL;
4002 dw_hdmi_update_power(hdmi);
4003 dw_hdmi_update_phy_mask(hdmi);
4004 if (hdmi->plat_data->dclk_set)
4005 hdmi->plat_data->dclk_set(hdmi->plat_data->phy_data, false, 0);
4006 mutex_unlock(&hdmi->mutex);
4007
4008 mutex_lock(&hdmi->i2c->lock);
4009 if (hdmi->plat_data->set_ddc_io)
4010 hdmi->plat_data->set_ddc_io(data, false);
4011 mutex_unlock(&hdmi->i2c->lock);
4012 }
4013
dw_hdmi_bridge_atomic_enable(struct drm_bridge * bridge,struct drm_bridge_state * old_state)4014 static void dw_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,
4015 struct drm_bridge_state *old_state)
4016 {
4017 struct dw_hdmi *hdmi = bridge->driver_private;
4018 struct drm_atomic_state *state = old_state->base.state;
4019 struct drm_connector *connector;
4020
4021 connector = drm_atomic_get_new_connector_for_encoder(state,
4022 bridge->encoder);
4023
4024 mutex_lock(&hdmi->mutex);
4025 hdmi->disabled = false;
4026 hdmi->curr_conn = connector;
4027 if (hdmi->plat_data->dclk_set)
4028 hdmi->plat_data->dclk_set(hdmi->plat_data->phy_data, true, 0);
4029 dw_hdmi_update_power(hdmi);
4030 dw_hdmi_update_phy_mask(hdmi);
4031 handle_plugged_change(hdmi, true);
4032 mutex_unlock(&hdmi->mutex);
4033 }
4034
dw_hdmi_bridge_detect(struct drm_bridge * bridge)4035 static enum drm_connector_status dw_hdmi_bridge_detect(struct drm_bridge *bridge)
4036 {
4037 struct dw_hdmi *hdmi = bridge->driver_private;
4038
4039 return dw_hdmi_detect(hdmi);
4040 }
4041
dw_hdmi_bridge_get_edid(struct drm_bridge * bridge,struct drm_connector * connector)4042 static struct edid *dw_hdmi_bridge_get_edid(struct drm_bridge *bridge,
4043 struct drm_connector *connector)
4044 {
4045 struct dw_hdmi *hdmi = bridge->driver_private;
4046
4047 return dw_hdmi_get_edid(hdmi, connector);
4048 }
4049
4050 static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {
4051 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
4052 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
4053 .atomic_reset = drm_atomic_helper_bridge_reset,
4054 .attach = dw_hdmi_bridge_attach,
4055 .detach = dw_hdmi_bridge_detach,
4056 .atomic_check = dw_hdmi_bridge_atomic_check,
4057 .atomic_get_output_bus_fmts = dw_hdmi_bridge_atomic_get_output_bus_fmts,
4058 .atomic_get_input_bus_fmts = dw_hdmi_bridge_atomic_get_input_bus_fmts,
4059 .atomic_enable = dw_hdmi_bridge_atomic_enable,
4060 .atomic_disable = dw_hdmi_bridge_atomic_disable,
4061 .mode_set = dw_hdmi_bridge_mode_set,
4062 .mode_valid = dw_hdmi_bridge_mode_valid,
4063 .detect = dw_hdmi_bridge_detect,
4064 .get_edid = dw_hdmi_bridge_get_edid,
4065 };
4066
dw_hdmi_set_cec_adap(struct dw_hdmi * hdmi,struct cec_adapter * adap)4067 void dw_hdmi_set_cec_adap(struct dw_hdmi *hdmi, struct cec_adapter *adap)
4068 {
4069 hdmi->cec_adap = adap;
4070 }
4071 EXPORT_SYMBOL_GPL(dw_hdmi_set_cec_adap);
4072
4073 /* -----------------------------------------------------------------------------
4074 * IRQ Handling
4075 */
4076
dw_hdmi_i2c_irq(struct dw_hdmi * hdmi)4077 static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)
4078 {
4079 struct dw_hdmi_i2c *i2c = hdmi->i2c;
4080 unsigned int stat;
4081
4082 stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);
4083 if (!stat)
4084 return IRQ_NONE;
4085
4086 hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);
4087
4088 i2c->stat = stat;
4089
4090 complete(&i2c->cmp);
4091
4092 return IRQ_HANDLED;
4093 }
4094
dw_hdmi_hardirq(int irq,void * dev_id)4095 static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)
4096 {
4097 struct dw_hdmi *hdmi = dev_id;
4098 u8 intr_stat, hdcp_stat;
4099 irqreturn_t ret = IRQ_NONE;
4100
4101 if (hdmi->i2c)
4102 ret = dw_hdmi_i2c_irq(hdmi);
4103
4104 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
4105 if (intr_stat) {
4106 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
4107 return IRQ_WAKE_THREAD;
4108 }
4109
4110 hdcp_stat = hdmi_readb(hdmi, HDMI_A_APIINTSTAT);
4111 if (hdcp_stat) {
4112 dev_dbg(hdmi->dev, "HDCP irq %#x\n", hdcp_stat);
4113 hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);
4114 return IRQ_WAKE_THREAD;
4115 }
4116
4117 return ret;
4118 }
4119
dw_hdmi_setup_rx_sense(struct dw_hdmi * hdmi,bool hpd,bool rx_sense)4120 void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)
4121 {
4122 mutex_lock(&hdmi->mutex);
4123
4124 if (!hdmi->force && !hdmi->force_logo) {
4125 /*
4126 * If the RX sense status indicates we're disconnected,
4127 * clear the software rxsense status.
4128 */
4129 if (!rx_sense)
4130 hdmi->rxsense = false;
4131
4132 /*
4133 * Only set the software rxsense status when both
4134 * rxsense and hpd indicates we're connected.
4135 * This avoids what seems to be bad behaviour in
4136 * at least iMX6S versions of the phy.
4137 */
4138 if (hpd)
4139 hdmi->rxsense = true;
4140
4141 dw_hdmi_update_power(hdmi);
4142 dw_hdmi_update_phy_mask(hdmi);
4143 }
4144 mutex_unlock(&hdmi->mutex);
4145 }
4146 EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);
4147
dw_hdmi_irq(int irq,void * dev_id)4148 static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)
4149 {
4150 struct dw_hdmi *hdmi = dev_id;
4151 u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat, hdcp_stat;
4152
4153 intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);
4154 phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);
4155 phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);
4156
4157 phy_pol_mask = 0;
4158 if (intr_stat & HDMI_IH_PHY_STAT0_HPD)
4159 phy_pol_mask |= HDMI_PHY_HPD;
4160 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)
4161 phy_pol_mask |= HDMI_PHY_RX_SENSE0;
4162 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)
4163 phy_pol_mask |= HDMI_PHY_RX_SENSE1;
4164 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)
4165 phy_pol_mask |= HDMI_PHY_RX_SENSE2;
4166 if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)
4167 phy_pol_mask |= HDMI_PHY_RX_SENSE3;
4168
4169 if (phy_pol_mask)
4170 hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);
4171
4172 /*
4173 * RX sense tells us whether the TDMS transmitters are detecting
4174 * load - in other words, there's something listening on the
4175 * other end of the link. Use this to decide whether we should
4176 * power on the phy as HPD may be toggled by the sink to merely
4177 * ask the source to re-read the EDID.
4178 */
4179 if (intr_stat &
4180 (HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {
4181 dw_hdmi_setup_rx_sense(hdmi,
4182 phy_stat & HDMI_PHY_HPD,
4183 phy_stat & HDMI_PHY_RX_SENSE);
4184
4185 if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0) {
4186 mutex_lock(&hdmi->cec_notifier_mutex);
4187 cec_notifier_phys_addr_invalidate(hdmi->cec_notifier);
4188 mutex_unlock(&hdmi->cec_notifier_mutex);
4189 }
4190 }
4191
4192 check_hdmi_irq(hdmi, intr_stat, phy_int_pol);
4193
4194 hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);
4195 if (!hdmi->next_bridge)
4196 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD |
4197 HDMI_IH_PHY_STAT0_RX_SENSE),
4198 HDMI_IH_MUTE_PHY_STAT0);
4199
4200 hdcp_stat = hdmi_readb(hdmi, HDMI_A_APIINTSTAT);
4201 if (hdcp_stat) {
4202 if (hdmi->hdcp)
4203 hdmi->hdcp->hdcp_isr(hdmi->hdcp, hdcp_stat);
4204 hdmi_writeb(hdmi, hdcp_stat, HDMI_A_APIINTCLR);
4205 hdmi_writeb(hdmi, 0x00, HDMI_A_APIINTMSK);
4206 }
4207 return IRQ_HANDLED;
4208 }
4209
4210 static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {
4211 {
4212 .type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,
4213 .name = "DWC HDMI TX PHY",
4214 .gen = 1,
4215 }, {
4216 .type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,
4217 .name = "DWC MHL PHY + HEAC PHY",
4218 .gen = 2,
4219 .has_svsret = true,
4220 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
4221 }, {
4222 .type = DW_HDMI_PHY_DWC_MHL_PHY,
4223 .name = "DWC MHL PHY",
4224 .gen = 2,
4225 .has_svsret = true,
4226 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
4227 }, {
4228 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,
4229 .name = "DWC HDMI 3D TX PHY + HEAC PHY",
4230 .gen = 2,
4231 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
4232 }, {
4233 .type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,
4234 .name = "DWC HDMI 3D TX PHY",
4235 .gen = 2,
4236 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
4237 }, {
4238 .type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,
4239 .name = "DWC HDMI 2.0 TX PHY",
4240 .gen = 2,
4241 .has_svsret = true,
4242 .configure = hdmi_phy_configure_dwc_hdmi_3d_tx,
4243 }, {
4244 .type = DW_HDMI_PHY_VENDOR_PHY,
4245 .name = "Vendor PHY",
4246 }
4247 };
4248
dw_hdmi_detect_phy(struct dw_hdmi * hdmi)4249 static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)
4250 {
4251 unsigned int i;
4252 u8 phy_type;
4253
4254 phy_type = hdmi->plat_data->phy_force_vendor ?
4255 DW_HDMI_PHY_VENDOR_PHY :
4256 hdmi_readb(hdmi, HDMI_CONFIG2_ID);
4257
4258 if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {
4259 /* Vendor PHYs require support from the glue layer. */
4260 if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {
4261 dev_err(hdmi->dev,
4262 "Vendor HDMI PHY not supported by glue layer\n");
4263 return -ENODEV;
4264 }
4265
4266 hdmi->phy.ops = hdmi->plat_data->phy_ops;
4267 hdmi->phy.data = hdmi->plat_data->phy_data;
4268 hdmi->phy.name = hdmi->plat_data->phy_name;
4269 return 0;
4270 }
4271
4272 /* Synopsys PHYs are handled internally. */
4273 for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {
4274 if (dw_hdmi_phys[i].type == phy_type) {
4275 hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;
4276 hdmi->phy.name = dw_hdmi_phys[i].name;
4277 hdmi->phy.data = (void *)&dw_hdmi_phys[i];
4278
4279 if (!dw_hdmi_phys[i].configure &&
4280 !hdmi->plat_data->configure_phy) {
4281 dev_err(hdmi->dev, "%s requires platform support\n",
4282 hdmi->phy.name);
4283 return -ENODEV;
4284 }
4285
4286 return 0;
4287 }
4288 }
4289
4290 dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);
4291 return -ENODEV;
4292 }
4293
dw_hdmi_cec_enable(struct dw_hdmi * hdmi)4294 static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)
4295 {
4296 mutex_lock(&hdmi->mutex);
4297 hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;
4298 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
4299 mutex_unlock(&hdmi->mutex);
4300 }
4301
dw_hdmi_cec_disable(struct dw_hdmi * hdmi)4302 static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)
4303 {
4304 mutex_lock(&hdmi->mutex);
4305 hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;
4306 hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);
4307 mutex_unlock(&hdmi->mutex);
4308 }
4309
4310 static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {
4311 .write = hdmi_writeb,
4312 .read = hdmi_readb,
4313 .mod = hdmi_modb,
4314 .enable = dw_hdmi_cec_enable,
4315 .disable = dw_hdmi_cec_disable,
4316 };
4317
4318 static const struct regmap_config hdmi_regmap_8bit_config = {
4319 .reg_bits = 32,
4320 .val_bits = 8,
4321 .reg_stride = 1,
4322 .max_register = HDMI_I2CM_SCDC_UPDATE1,
4323 };
4324
4325 static const struct regmap_config hdmi_regmap_32bit_config = {
4326 .reg_bits = 32,
4327 .val_bits = 32,
4328 .reg_stride = 4,
4329 .max_register = HDMI_I2CM_SCDC_UPDATE1 << 2,
4330 };
4331
dw_hdmi_init_hw(struct dw_hdmi * hdmi)4332 static void dw_hdmi_init_hw(struct dw_hdmi *hdmi)
4333 {
4334 initialize_hdmi_ih_mutes(hdmi);
4335
4336 /*
4337 * Reset HDMI DDC I2C master controller and mute I2CM interrupts.
4338 * Even if we are using a separate i2c adapter doing this doesn't
4339 * hurt.
4340 */
4341 if (hdmi->i2c)
4342 dw_hdmi_i2c_init(hdmi);
4343
4344 if (hdmi->phy.ops->setup_hpd)
4345 hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);
4346 }
4347
dw_hdmi_status_show(struct seq_file * s,void * v)4348 static int dw_hdmi_status_show(struct seq_file *s, void *v)
4349 {
4350 struct dw_hdmi *hdmi = s->private;
4351 u32 val;
4352
4353 seq_puts(s, "PHY: ");
4354 if (!hdmi->phy.enabled) {
4355 seq_puts(s, "disabled\n");
4356 return 0;
4357 }
4358 seq_puts(s, "enabled\t\t\tMode: ");
4359 if (hdmi->sink_is_hdmi)
4360 seq_puts(s, "HDMI\n");
4361 else
4362 seq_puts(s, "DVI\n");
4363 if (hdmi->hdmi_data.video_mode.mtmdsclock > 340000000)
4364 val = hdmi->hdmi_data.video_mode.mtmdsclock / 4;
4365 else
4366 val = hdmi->hdmi_data.video_mode.mtmdsclock;
4367 seq_printf(s, "Pixel Clk: %uHz\t\tTMDS Clk: %uHz\n",
4368 hdmi->hdmi_data.video_mode.mpixelclock, val);
4369 seq_puts(s, "Color Format: ");
4370 if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format))
4371 seq_puts(s, "RGB");
4372 else if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))
4373 seq_puts(s, "YUV444");
4374 else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))
4375 seq_puts(s, "YUV422");
4376 else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))
4377 seq_puts(s, "YUV420");
4378 else
4379 seq_puts(s, "UNKNOWN");
4380 val = hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format);
4381 seq_printf(s, "\t\tColor Depth: %d bit\n", val);
4382 seq_puts(s, "Colorimetry: ");
4383 switch (hdmi->hdmi_data.enc_out_encoding) {
4384 case V4L2_YCBCR_ENC_601:
4385 seq_puts(s, "ITU.BT601");
4386 break;
4387 case V4L2_YCBCR_ENC_709:
4388 seq_puts(s, "ITU.BT709");
4389 break;
4390 case V4L2_YCBCR_ENC_BT2020:
4391 seq_puts(s, "ITU.BT2020");
4392 break;
4393 default: /* Carries no data */
4394 seq_puts(s, "ITU.BT601");
4395 break;
4396 }
4397
4398 seq_puts(s, "\t\tEOTF: ");
4399
4400 if (hdmi->version < 0x211a) {
4401 seq_puts(s, "Unsupported\n");
4402 return 0;
4403 }
4404
4405 val = hdmi_readb(hdmi, HDMI_FC_PACKET_TX_EN);
4406 if (!(val & HDMI_FC_PACKET_TX_EN_DRM_MASK)) {
4407 seq_puts(s, "Off\n");
4408 return 0;
4409 }
4410
4411 switch (hdmi_readb(hdmi, HDMI_FC_DRM_PB0)) {
4412 case HDMI_EOTF_TRADITIONAL_GAMMA_SDR:
4413 seq_puts(s, "SDR");
4414 break;
4415 case HDMI_EOTF_TRADITIONAL_GAMMA_HDR:
4416 seq_puts(s, "HDR");
4417 break;
4418 case HDMI_EOTF_SMPTE_ST2084:
4419 seq_puts(s, "ST2084");
4420 break;
4421 case HDMI_EOTF_BT_2100_HLG:
4422 seq_puts(s, "HLG");
4423 break;
4424 default:
4425 seq_puts(s, "Not Defined\n");
4426 return 0;
4427 }
4428
4429 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB3) << 8;
4430 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB2);
4431 seq_printf(s, "\nx0: %d", val);
4432 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB5) << 8;
4433 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB4);
4434 seq_printf(s, "\t\t\t\ty0: %d\n", val);
4435 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB7) << 8;
4436 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB6);
4437 seq_printf(s, "x1: %d", val);
4438 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB9) << 8;
4439 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB8);
4440 seq_printf(s, "\t\t\t\ty1: %d\n", val);
4441 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB11) << 8;
4442 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB10);
4443 seq_printf(s, "x2: %d", val);
4444 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB13) << 8;
4445 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB12);
4446 seq_printf(s, "\t\t\t\ty2: %d\n", val);
4447 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB15) << 8;
4448 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB14);
4449 seq_printf(s, "white x: %d", val);
4450 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB17) << 8;
4451 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB16);
4452 seq_printf(s, "\t\t\twhite y: %d\n", val);
4453 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB19) << 8;
4454 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB18);
4455 seq_printf(s, "max lum: %d", val);
4456 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB21) << 8;
4457 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB20);
4458 seq_printf(s, "\t\t\tmin lum: %d\n", val);
4459 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB23) << 8;
4460 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB22);
4461 seq_printf(s, "max cll: %d", val);
4462 val = hdmi_readb(hdmi, HDMI_FC_DRM_PB25) << 8;
4463 val |= hdmi_readb(hdmi, HDMI_FC_DRM_PB24);
4464 seq_printf(s, "\t\t\tmax fall: %d\n", val);
4465 return 0;
4466 }
4467
dw_hdmi_status_open(struct inode * inode,struct file * file)4468 static int dw_hdmi_status_open(struct inode *inode, struct file *file)
4469 {
4470 return single_open(file, dw_hdmi_status_show, inode->i_private);
4471 }
4472
4473 static const struct file_operations dw_hdmi_status_fops = {
4474 .owner = THIS_MODULE,
4475 .open = dw_hdmi_status_open,
4476 .read = seq_read,
4477 .llseek = seq_lseek,
4478 .release = single_release,
4479 };
4480
4481 #include <linux/fs.h>
4482 #include <linux/debugfs.h>
4483 #include <linux/seq_file.h>
4484
4485 struct dw_hdmi_reg_table {
4486 int reg_base;
4487 int reg_end;
4488 };
4489
4490 static const struct dw_hdmi_reg_table hdmi_reg_table[] = {
4491 {HDMI_DESIGN_ID, HDMI_CONFIG3_ID},
4492 {HDMI_IH_FC_STAT0, HDMI_IH_MUTE},
4493 {HDMI_TX_INVID0, HDMI_TX_BCBDATA1},
4494 {HDMI_VP_STATUS, HDMI_VP_POL},
4495 {HDMI_FC_INVIDCONF, HDMI_FC_DBGTMDS2},
4496 {HDMI_PHY_CONF0, HDMI_PHY_POL0},
4497 {HDMI_PHY_I2CM_SLAVE_ADDR, HDMI_PHY_I2CM_FS_SCL_LCNT_0_ADDR},
4498 {HDMI_AUD_CONF0, 0x3624},
4499 {HDMI_MC_SFRDIV, HDMI_MC_HEACPHY_RST},
4500 {HDMI_CSC_CFG, HDMI_CSC_COEF_C4_LSB},
4501 {HDMI_A_HDCPCFG0, 0x52bb},
4502 {0x7800, 0x7818},
4503 {0x7900, 0x790e},
4504 {HDMI_CEC_CTRL, HDMI_CEC_WKUPCTRL},
4505 {HDMI_I2CM_SLAVE, 0x7e31},
4506 };
4507
dw_hdmi_ctrl_show(struct seq_file * s,void * v)4508 static int dw_hdmi_ctrl_show(struct seq_file *s, void *v)
4509 {
4510 struct dw_hdmi *hdmi = s->private;
4511 u32 i = 0, j = 0, val = 0;
4512
4513 seq_puts(s, "\n>>>hdmi_ctl reg ");
4514 for (i = 0; i < 16; i++)
4515 seq_printf(s, " %2x", i);
4516 seq_puts(s, "\n---------------------------------------------------");
4517
4518 for (i = 0; i < ARRAY_SIZE(hdmi_reg_table); i++) {
4519 for (j = hdmi_reg_table[i].reg_base;
4520 j <= hdmi_reg_table[i].reg_end; j++) {
4521 val = hdmi_readb(hdmi, j);
4522 if ((j - hdmi_reg_table[i].reg_base) % 16 == 0)
4523 seq_printf(s, "\n>>>hdmi_ctl %04x:", j);
4524 seq_printf(s, " %02x", val);
4525 }
4526 }
4527 seq_puts(s, "\n---------------------------------------------------\n");
4528
4529 return 0;
4530 }
4531
dw_hdmi_ctrl_open(struct inode * inode,struct file * file)4532 static int dw_hdmi_ctrl_open(struct inode *inode, struct file *file)
4533 {
4534 return single_open(file, dw_hdmi_ctrl_show, inode->i_private);
4535 }
4536
4537 static ssize_t
dw_hdmi_ctrl_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)4538 dw_hdmi_ctrl_write(struct file *file, const char __user *buf,
4539 size_t count, loff_t *ppos)
4540 {
4541 struct dw_hdmi *hdmi =
4542 ((struct seq_file *)file->private_data)->private;
4543 u32 reg, val;
4544 char kbuf[25];
4545
4546 if (copy_from_user(kbuf, buf, count))
4547 return -EFAULT;
4548 if (sscanf(kbuf, "%x%x", ®, &val) == -1)
4549 return -EFAULT;
4550 if (reg > HDMI_I2CM_FS_SCL_LCNT_0_ADDR) {
4551 dev_err(hdmi->dev, "it is no a hdmi register\n");
4552 return count;
4553 }
4554 dev_info(hdmi->dev, "/**********hdmi register config******/");
4555 dev_info(hdmi->dev, "\n reg=%x val=%x\n", reg, val);
4556 hdmi_writeb(hdmi, val, reg);
4557 return count;
4558 }
4559
4560 static const struct file_operations dw_hdmi_ctrl_fops = {
4561 .owner = THIS_MODULE,
4562 .open = dw_hdmi_ctrl_open,
4563 .read = seq_read,
4564 .write = dw_hdmi_ctrl_write,
4565 .llseek = seq_lseek,
4566 .release = single_release,
4567 };
4568
dw_hdmi_phy_show(struct seq_file * s,void * v)4569 static int dw_hdmi_phy_show(struct seq_file *s, void *v)
4570 {
4571 struct dw_hdmi *hdmi = s->private;
4572 u32 i;
4573
4574 seq_puts(s, "\n>>>hdmi_phy reg ");
4575 for (i = 0; i < 0x28; i++)
4576 seq_printf(s, "regs %02x val %04x\n",
4577 i, hdmi_phy_i2c_read(hdmi, i));
4578 return 0;
4579 }
4580
dw_hdmi_phy_open(struct inode * inode,struct file * file)4581 static int dw_hdmi_phy_open(struct inode *inode, struct file *file)
4582 {
4583 return single_open(file, dw_hdmi_phy_show, inode->i_private);
4584 }
4585
4586 static ssize_t
dw_hdmi_phy_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)4587 dw_hdmi_phy_write(struct file *file, const char __user *buf,
4588 size_t count, loff_t *ppos)
4589 {
4590 struct dw_hdmi *hdmi =
4591 ((struct seq_file *)file->private_data)->private;
4592 u32 reg, val;
4593 char kbuf[25];
4594
4595 if (copy_from_user(kbuf, buf, count))
4596 return -EFAULT;
4597 if (sscanf(kbuf, "%x%x", ®, &val) == -1)
4598 return -EFAULT;
4599 if (reg > 0x28) {
4600 dev_err(hdmi->dev, "it is not a hdmi phy register\n");
4601 return count;
4602 }
4603 dev_info(hdmi->dev, "/*******hdmi phy register config******/");
4604 dev_info(hdmi->dev, "\n reg=%x val=%x\n", reg, val);
4605 dw_hdmi_phy_i2c_write(hdmi, val, reg);
4606 return count;
4607 }
4608
4609 static const struct file_operations dw_hdmi_phy_fops = {
4610 .owner = THIS_MODULE,
4611 .open = dw_hdmi_phy_open,
4612 .read = seq_read,
4613 .write = dw_hdmi_phy_write,
4614 .llseek = seq_lseek,
4615 .release = single_release,
4616 };
4617
dw_hdmi_register_debugfs(struct device * dev,struct dw_hdmi * hdmi)4618 static void dw_hdmi_register_debugfs(struct device *dev, struct dw_hdmi *hdmi)
4619 {
4620 hdmi->debugfs_dir = debugfs_create_dir("dw-hdmi", NULL);
4621 if (IS_ERR(hdmi->debugfs_dir)) {
4622 dev_err(dev, "failed to create debugfs dir!\n");
4623 return;
4624 }
4625 debugfs_create_file("status", 0400, hdmi->debugfs_dir,
4626 hdmi, &dw_hdmi_status_fops);
4627 debugfs_create_file("ctrl", 0400, hdmi->debugfs_dir,
4628 hdmi, &dw_hdmi_ctrl_fops);
4629 debugfs_create_file("phy", 0400, hdmi->debugfs_dir,
4630 hdmi, &dw_hdmi_phy_fops);
4631 }
4632
dw_hdmi_register_hdcp(struct device * dev,struct dw_hdmi * hdmi,u32 val,bool hdcp1x_enable)4633 static void dw_hdmi_register_hdcp(struct device *dev, struct dw_hdmi *hdmi,
4634 u32 val, bool hdcp1x_enable)
4635 {
4636 struct dw_hdcp hdmi_hdcp = {
4637 .hdmi = hdmi,
4638 .write = hdmi_writeb,
4639 .read = hdmi_readb,
4640 .regs = hdmi->regs,
4641 .reg_io_width = val,
4642 .enable = hdcp1x_enable,
4643 };
4644 struct platform_device_info hdcp_device_info = {
4645 .parent = dev,
4646 .id = PLATFORM_DEVID_AUTO,
4647 .res = NULL,
4648 .num_res = 0,
4649 .name = DW_HDCP_DRIVER_NAME,
4650 .data = &hdmi_hdcp,
4651 .size_data = sizeof(hdmi_hdcp),
4652 .dma_mask = DMA_BIT_MASK(32),
4653 };
4654
4655 hdmi->hdcp_dev = platform_device_register_full(&hdcp_device_info);
4656 if (IS_ERR(hdmi->hdcp_dev))
4657 dev_err(dev, "failed to register hdcp!\n");
4658 else
4659 hdmi->hdcp = hdmi->hdcp_dev->dev.platform_data;
4660 }
4661
get_force_logo_property(struct dw_hdmi * hdmi)4662 static int get_force_logo_property(struct dw_hdmi *hdmi)
4663 {
4664 struct device_node *dss;
4665 struct device_node *route;
4666 struct device_node *route_hdmi;
4667
4668 dss = of_find_node_by_name(NULL, "display-subsystem");
4669 if (!dss) {
4670 dev_err(hdmi->dev, "can't find display-subsystem\n");
4671 return -ENODEV;
4672 }
4673
4674 route = of_find_node_by_name(dss, "route");
4675 if (!route) {
4676 dev_err(hdmi->dev, "can't find route\n");
4677 of_node_put(dss);
4678 return -ENODEV;
4679 }
4680 of_node_put(dss);
4681
4682 route_hdmi = of_find_node_by_name(route, "route-hdmi");
4683 if (!route_hdmi) {
4684 dev_err(hdmi->dev, "can't find route-hdmi\n");
4685 of_node_put(route);
4686 return -ENODEV;
4687 }
4688 of_node_put(route);
4689
4690 hdmi->force_logo =
4691 of_property_read_bool(route_hdmi, "force-output");
4692
4693 of_node_put(route_hdmi);
4694
4695 return 0;
4696 }
4697
4698 void
dw_hdmi_cec_wake_ops_register(struct dw_hdmi * hdmi,const struct dw_hdmi_cec_wake_ops * cec_ops)4699 dw_hdmi_cec_wake_ops_register(struct dw_hdmi *hdmi, const struct dw_hdmi_cec_wake_ops *cec_ops)
4700 {
4701 if (!cec_ops || !hdmi)
4702 return;
4703
4704 hdmi->cec_ops = cec_ops;
4705 }
4706 EXPORT_SYMBOL_GPL(dw_hdmi_cec_wake_ops_register);
4707
4708
4709 /* -----------------------------------------------------------------------------
4710 * Probe/remove API, used from platforms based on the DRM bridge API.
4711 */
dw_hdmi_probe(struct platform_device * pdev,const struct dw_hdmi_plat_data * plat_data)4712 struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,
4713 const struct dw_hdmi_plat_data *plat_data)
4714 {
4715 struct device *dev = &pdev->dev;
4716 struct device_node *np = dev->of_node;
4717 struct device_node *endpoint;
4718 struct platform_device_info pdevinfo;
4719 struct device_node *ddc_node;
4720 struct dw_hdmi_cec_data cec;
4721 struct dw_hdmi *hdmi;
4722 struct resource *iores = NULL;
4723 int irq;
4724 int ret;
4725 u32 val = 1;
4726 u8 prod_id0;
4727 u8 prod_id1;
4728 u8 config0;
4729 u8 config3;
4730 bool hdcp1x_enable = 0;
4731
4732 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
4733 if (!hdmi)
4734 return ERR_PTR(-ENOMEM);
4735
4736 hdmi->connector.stereo_allowed = 1;
4737 hdmi->plat_data = plat_data;
4738 hdmi->dev = dev;
4739 hdmi->sample_rate = 48000;
4740 hdmi->disabled = true;
4741 hdmi->rxsense = true;
4742 hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);
4743 hdmi->mc_clkdis = 0x7f;
4744 hdmi->last_connector_result = connector_status_disconnected;
4745
4746 mutex_init(&hdmi->mutex);
4747 mutex_init(&hdmi->audio_mutex);
4748 mutex_init(&hdmi->cec_notifier_mutex);
4749 spin_lock_init(&hdmi->audio_lock);
4750
4751 ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);
4752 if (ddc_node) {
4753 hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);
4754 of_node_put(ddc_node);
4755 if (!hdmi->ddc) {
4756 dev_dbg(hdmi->dev, "failed to read ddc node\n");
4757 return ERR_PTR(-EPROBE_DEFER);
4758 }
4759
4760 } else {
4761 dev_dbg(hdmi->dev, "no ddc property found\n");
4762 }
4763
4764 if (!plat_data->regm) {
4765 const struct regmap_config *reg_config;
4766
4767 of_property_read_u32(np, "reg-io-width", &val);
4768 switch (val) {
4769 case 4:
4770 reg_config = &hdmi_regmap_32bit_config;
4771 hdmi->reg_shift = 2;
4772 break;
4773 case 1:
4774 reg_config = &hdmi_regmap_8bit_config;
4775 break;
4776 default:
4777 dev_err(dev, "reg-io-width must be 1 or 4\n");
4778 return ERR_PTR(-EINVAL);
4779 }
4780
4781 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4782 hdmi->regs = devm_ioremap_resource(dev, iores);
4783 if (IS_ERR(hdmi->regs)) {
4784 ret = PTR_ERR(hdmi->regs);
4785 goto err_res;
4786 }
4787
4788 hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);
4789 if (IS_ERR(hdmi->regm)) {
4790 dev_err(dev, "Failed to configure regmap\n");
4791 ret = PTR_ERR(hdmi->regm);
4792 goto err_res;
4793 }
4794 } else {
4795 hdmi->regm = plat_data->regm;
4796 }
4797
4798 hdmi->isfr_clk = devm_clk_get(hdmi->dev, "isfr");
4799 if (IS_ERR(hdmi->isfr_clk)) {
4800 ret = PTR_ERR(hdmi->isfr_clk);
4801 dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);
4802 goto err_res;
4803 }
4804
4805 ret = clk_prepare_enable(hdmi->isfr_clk);
4806 if (ret) {
4807 dev_err(hdmi->dev, "Cannot enable HDMI isfr clock: %d\n", ret);
4808 goto err_res;
4809 }
4810
4811 hdmi->iahb_clk = devm_clk_get(hdmi->dev, "iahb");
4812 if (IS_ERR(hdmi->iahb_clk)) {
4813 ret = PTR_ERR(hdmi->iahb_clk);
4814 dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);
4815 goto err_isfr;
4816 }
4817
4818 ret = clk_prepare_enable(hdmi->iahb_clk);
4819 if (ret) {
4820 dev_err(hdmi->dev, "Cannot enable HDMI iahb clock: %d\n", ret);
4821 goto err_isfr;
4822 }
4823
4824 hdmi->cec_clk = devm_clk_get(hdmi->dev, "cec");
4825 if (PTR_ERR(hdmi->cec_clk) == -ENOENT) {
4826 hdmi->cec_clk = NULL;
4827 } else if (IS_ERR(hdmi->cec_clk)) {
4828 ret = PTR_ERR(hdmi->cec_clk);
4829 if (ret != -EPROBE_DEFER)
4830 dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",
4831 ret);
4832
4833 hdmi->cec_clk = NULL;
4834 goto err_iahb;
4835 } else {
4836 ret = clk_prepare_enable(hdmi->cec_clk);
4837 if (ret) {
4838 dev_err(hdmi->dev, "Cannot enable HDMI cec clock: %d\n",
4839 ret);
4840 goto err_iahb;
4841 }
4842 }
4843
4844 /* Product and revision IDs */
4845 hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)
4846 | (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);
4847 prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);
4848 prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);
4849
4850 if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||
4851 (prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {
4852 dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",
4853 hdmi->version, prod_id0, prod_id1);
4854 ret = -ENODEV;
4855 goto err_iahb;
4856 }
4857
4858 ret = dw_hdmi_detect_phy(hdmi);
4859 if (ret < 0)
4860 goto err_iahb;
4861
4862 dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",
4863 hdmi->version >> 12, hdmi->version & 0xfff,
4864 prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",
4865 hdmi->phy.name);
4866
4867 ret = get_force_logo_property(hdmi);
4868 if (ret)
4869 goto err_iahb;
4870
4871 hdmi->logo_plug_out = false;
4872 hdmi->initialized = false;
4873 ret = hdmi_readb(hdmi, HDMI_PHY_STAT0);
4874 if (((ret & HDMI_PHY_TX_PHY_LOCK) && (ret & HDMI_PHY_HPD) &&
4875 hdmi_readb(hdmi, HDMI_FC_EXCTRLDUR)) || hdmi->force_logo) {
4876 hdmi->mc_clkdis = hdmi_readb(hdmi, HDMI_MC_CLKDIS);
4877 hdmi->disabled = false;
4878 hdmi->bridge_is_on = true;
4879 hdmi->phy.enabled = true;
4880 hdmi->initialized = true;
4881 if (hdmi->plat_data->set_ddc_io)
4882 hdmi->plat_data->set_ddc_io(hdmi->plat_data->phy_data, true);
4883 if (hdmi->plat_data->dclk_set)
4884 hdmi->plat_data->dclk_set(hdmi->plat_data->phy_data, true, 0);
4885 } else if (ret & HDMI_PHY_TX_PHY_LOCK) {
4886 hdmi->phy.ops->disable(hdmi, hdmi->phy.data);
4887 if (hdmi->plat_data->set_ddc_io)
4888 hdmi->plat_data->set_ddc_io(hdmi->plat_data->phy_data, false);
4889 }
4890
4891 init_hpd_work(hdmi);
4892
4893 irq = platform_get_irq(pdev, 0);
4894 if (irq < 0) {
4895 ret = irq;
4896 goto err_iahb;
4897 }
4898
4899 hdmi->irq = irq;
4900 ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,
4901 dw_hdmi_irq, IRQF_SHARED | IRQF_ONESHOT,
4902 dev_name(dev), hdmi);
4903 if (ret)
4904 goto err_iahb;
4905
4906 /*
4907 * To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator
4908 * N and cts values before enabling phy
4909 */
4910 hdmi_init_clk_regenerator(hdmi);
4911
4912 /* If DDC bus is not specified, try to register HDMI I2C bus */
4913 if (!hdmi->ddc) {
4914 /* Look for (optional) stuff related to unwedging */
4915 hdmi->pinctrl = devm_pinctrl_get(dev);
4916 if (!IS_ERR(hdmi->pinctrl)) {
4917 hdmi->unwedge_state =
4918 pinctrl_lookup_state(hdmi->pinctrl, "unwedge");
4919 hdmi->default_state =
4920 pinctrl_lookup_state(hdmi->pinctrl, "default");
4921
4922 if (IS_ERR(hdmi->default_state) ||
4923 IS_ERR(hdmi->unwedge_state)) {
4924 if (!IS_ERR(hdmi->unwedge_state))
4925 dev_warn(dev,
4926 "Unwedge requires default pinctrl\n");
4927 hdmi->default_state = NULL;
4928 hdmi->unwedge_state = NULL;
4929 }
4930 }
4931
4932 hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);
4933 if (IS_ERR(hdmi->ddc))
4934 hdmi->ddc = NULL;
4935 /*
4936 * Read high and low time from device tree. If not available use
4937 * the default timing scl clock rate is about 99.6KHz.
4938 */
4939 if (of_property_read_u32(np, "ddc-i2c-scl-high-time-ns",
4940 &hdmi->i2c->scl_high_ns))
4941 hdmi->i2c->scl_high_ns = 4708;
4942 if (of_property_read_u32(np, "ddc-i2c-scl-low-time-ns",
4943 &hdmi->i2c->scl_low_ns))
4944 hdmi->i2c->scl_low_ns = 4916;
4945 }
4946
4947 dw_hdmi_init_hw(hdmi);
4948
4949 hdmi->bridge.driver_private = hdmi;
4950 hdmi->bridge.funcs = &dw_hdmi_bridge_funcs;
4951 hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID
4952 | DRM_BRIDGE_OP_HPD;
4953 #ifdef CONFIG_OF
4954 hdmi->bridge.of_node = pdev->dev.of_node;
4955 #endif
4956
4957 endpoint = of_graph_get_endpoint_by_regs(hdmi->dev->of_node, 1, -1);
4958 if (endpoint && of_device_is_available(endpoint)) {
4959 struct device_node *remote;
4960
4961 remote = of_graph_get_remote_port_parent(endpoint);
4962 of_node_put(endpoint);
4963 if (!remote || !of_device_is_available(remote)) {
4964 of_node_put(remote);
4965 ret = -ENODEV;
4966 goto err_iahb;
4967 }
4968
4969 hdmi->next_bridge = of_drm_find_bridge(remote);
4970 of_node_put(remote);
4971 if (!hdmi->next_bridge) {
4972 dev_err(hdmi->dev, "can't find next bridge\n");
4973 ret = -EPROBE_DEFER;
4974 goto err_iahb;
4975 }
4976
4977 hdmi->sink_is_hdmi = true;
4978 hdmi->sink_has_audio = true;
4979 }
4980
4981 memset(&pdevinfo, 0, sizeof(pdevinfo));
4982 pdevinfo.parent = dev;
4983 pdevinfo.id = PLATFORM_DEVID_AUTO;
4984
4985 config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);
4986 config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);
4987
4988 if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {
4989 struct dw_hdmi_audio_data audio;
4990
4991 audio.phys = iores->start;
4992 audio.base = hdmi->regs;
4993 audio.irq = irq;
4994 audio.hdmi = hdmi;
4995 audio.get_eld = hdmi_audio_get_eld;
4996 hdmi->enable_audio = dw_hdmi_ahb_audio_enable;
4997 hdmi->disable_audio = dw_hdmi_ahb_audio_disable;
4998
4999 pdevinfo.name = "dw-hdmi-ahb-audio";
5000 pdevinfo.data = &audio;
5001 pdevinfo.size_data = sizeof(audio);
5002 pdevinfo.dma_mask = DMA_BIT_MASK(32);
5003 hdmi->audio = platform_device_register_full(&pdevinfo);
5004 } else if (config0 & HDMI_CONFIG0_I2S) {
5005 struct dw_hdmi_i2s_audio_data audio;
5006
5007 audio.hdmi = hdmi;
5008 audio.get_eld = hdmi_audio_get_eld;
5009 audio.write = hdmi_writeb;
5010 audio.read = hdmi_readb;
5011 hdmi->enable_audio = dw_hdmi_i2s_audio_enable;
5012 hdmi->disable_audio = dw_hdmi_i2s_audio_disable;
5013
5014 pdevinfo.name = "dw-hdmi-i2s-audio";
5015 pdevinfo.data = &audio;
5016 pdevinfo.size_data = sizeof(audio);
5017 pdevinfo.dma_mask = DMA_BIT_MASK(32);
5018 hdmi->audio = platform_device_register_full(&pdevinfo);
5019 }
5020
5021 if (config0 & HDMI_CONFIG0_CEC) {
5022 cec.hdmi = hdmi;
5023 cec.ops = &dw_hdmi_cec_ops;
5024 cec.irq = irq;
5025
5026 irq = platform_get_irq(pdev, 1);
5027 if (irq < 0)
5028 dev_dbg(hdmi->dev, "can't get cec wake up irq\n");
5029
5030 cec.wake_irq = irq;
5031
5032 pdevinfo.name = "dw-hdmi-cec";
5033 pdevinfo.data = &cec;
5034 pdevinfo.size_data = sizeof(cec);
5035 pdevinfo.dma_mask = 0;
5036
5037 hdmi->cec = platform_device_register_full(&pdevinfo);
5038 }
5039
5040 hdmi->extcon = devm_extcon_dev_allocate(hdmi->dev, dw_hdmi_cable);
5041 if (IS_ERR(hdmi->extcon)) {
5042 ret = PTR_ERR(hdmi->extcon);
5043 dev_err(hdmi->dev, "allocate extcon failed: %d\n", ret);
5044 goto err_iahb;
5045 }
5046
5047 ret = devm_extcon_dev_register(hdmi->dev, hdmi->extcon);
5048 if (ret) {
5049 dev_err(hdmi->dev, "failed to register extcon: %d\n",
5050 ret);
5051 goto err_iahb;
5052 }
5053
5054 ret = extcon_set_property_capability(hdmi->extcon, EXTCON_DISP_HDMI,
5055 EXTCON_PROP_DISP_HPD);
5056 if (ret) {
5057 dev_err(hdmi->dev,
5058 "failed to set USB property capability: %d\n",
5059 ret);
5060 goto err_iahb;
5061 }
5062
5063 drm_bridge_add(&hdmi->bridge);
5064
5065 dw_hdmi_register_debugfs(dev, hdmi);
5066
5067 if (of_property_read_bool(np, "scramble-low-rates"))
5068 hdmi->scramble_low_rates = true;
5069
5070 if (of_property_read_bool(np, "hdcp1x-enable"))
5071 hdcp1x_enable = 1;
5072 dw_hdmi_register_hdcp(dev, hdmi, val, hdcp1x_enable);
5073
5074 return hdmi;
5075
5076 err_iahb:
5077 clk_disable_unprepare(hdmi->iahb_clk);
5078 if (hdmi->cec_clk)
5079 clk_disable_unprepare(hdmi->cec_clk);
5080 err_isfr:
5081 clk_disable_unprepare(hdmi->isfr_clk);
5082 err_res:
5083 if (hdmi->i2c)
5084 i2c_del_adapter(&hdmi->i2c->adap);
5085 else
5086 i2c_put_adapter(hdmi->ddc);
5087
5088 return ERR_PTR(ret);
5089 }
5090 EXPORT_SYMBOL_GPL(dw_hdmi_probe);
5091
dw_hdmi_remove(struct dw_hdmi * hdmi)5092 void dw_hdmi_remove(struct dw_hdmi *hdmi)
5093 {
5094 if (hdmi->irq)
5095 disable_irq(hdmi->irq);
5096
5097 cancel_delayed_work(&hdmi->work);
5098 flush_workqueue(hdmi->workqueue);
5099 destroy_workqueue(hdmi->workqueue);
5100
5101 debugfs_remove_recursive(hdmi->debugfs_dir);
5102
5103 drm_bridge_remove(&hdmi->bridge);
5104
5105 if (hdmi->audio && !IS_ERR(hdmi->audio))
5106 platform_device_unregister(hdmi->audio);
5107 if (hdmi->hdcp_dev && !IS_ERR(hdmi->hdcp_dev))
5108 platform_device_unregister(hdmi->hdcp_dev);
5109 if (!IS_ERR(hdmi->cec))
5110 platform_device_unregister(hdmi->cec);
5111
5112 /* Disable all interrupts */
5113 hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);
5114
5115 if (!hdmi->next_bridge) {
5116 dw_hdmi_destroy_properties(hdmi);
5117 hdmi->connector.funcs->destroy(&hdmi->connector);
5118 }
5119
5120 if (hdmi->bridge.encoder)
5121 hdmi->bridge.encoder->funcs->destroy(hdmi->bridge.encoder);
5122
5123 clk_disable_unprepare(hdmi->iahb_clk);
5124 clk_disable_unprepare(hdmi->isfr_clk);
5125 if (hdmi->cec_clk)
5126 clk_disable_unprepare(hdmi->cec_clk);
5127
5128 if (hdmi->i2c)
5129 i2c_del_adapter(&hdmi->i2c->adap);
5130 else
5131 i2c_put_adapter(hdmi->ddc);
5132 }
5133 EXPORT_SYMBOL_GPL(dw_hdmi_remove);
5134
5135 /* -----------------------------------------------------------------------------
5136 * Bind/unbind API, used from platforms based on the component framework.
5137 */
dw_hdmi_bind(struct platform_device * pdev,struct drm_encoder * encoder,struct dw_hdmi_plat_data * plat_data)5138 struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,
5139 struct drm_encoder *encoder,
5140 struct dw_hdmi_plat_data *plat_data)
5141 {
5142 struct dw_hdmi *hdmi;
5143 int ret;
5144
5145 hdmi = dw_hdmi_probe(pdev, plat_data);
5146 if (IS_ERR(hdmi))
5147 return hdmi;
5148
5149 ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL, 0);
5150 if (ret) {
5151 dw_hdmi_remove(hdmi);
5152 DRM_ERROR("Failed to initialize bridge with drm\n");
5153 return ERR_PTR(ret);
5154 }
5155
5156 if (!hdmi->next_bridge)
5157 plat_data->connector = &hdmi->connector;
5158
5159 return hdmi;
5160 }
5161 EXPORT_SYMBOL_GPL(dw_hdmi_bind);
5162
dw_hdmi_unbind(struct dw_hdmi * hdmi)5163 void dw_hdmi_unbind(struct dw_hdmi *hdmi)
5164 {
5165 dw_hdmi_remove(hdmi);
5166 }
5167 EXPORT_SYMBOL_GPL(dw_hdmi_unbind);
5168
dw_hdmi_reg_initial(struct dw_hdmi * hdmi)5169 static void dw_hdmi_reg_initial(struct dw_hdmi *hdmi)
5170 {
5171 if (hdmi_readb(hdmi, HDMI_IH_MUTE)) {
5172 initialize_hdmi_ih_mutes(hdmi);
5173 /* unmute cec irq */
5174 hdmi_writeb(hdmi, 0x68, HDMI_IH_MUTE_CEC_STAT0);
5175
5176 hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,
5177 HDMI_PHY_I2CM_INT_ADDR);
5178
5179 hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |
5180 HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,
5181 HDMI_PHY_I2CM_CTLINT_ADDR);
5182
5183 if (!hdmi->next_bridge) {
5184 hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE,
5185 HDMI_PHY_POL0);
5186 hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);
5187 hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD |
5188 HDMI_IH_PHY_STAT0_RX_SENSE),
5189 HDMI_IH_MUTE_PHY_STAT0);
5190 }
5191 }
5192 }
5193
dw_hdmi_suspend(struct dw_hdmi * hdmi)5194 void dw_hdmi_suspend(struct dw_hdmi *hdmi)
5195 {
5196 if (!hdmi)
5197 return;
5198
5199 mutex_lock(&hdmi->mutex);
5200
5201 /*
5202 * When system shutdown, hdmi should be disabled.
5203 * When system suspend, dw_hdmi_bridge_disable will disable hdmi first.
5204 * To prevent duplicate operation, we should determine whether hdmi
5205 * has been disabled.
5206 */
5207 if (!hdmi->disabled) {
5208 hdmi->disabled = true;
5209 dw_hdmi_update_power(hdmi);
5210 dw_hdmi_update_phy_mask(hdmi);
5211 }
5212 mutex_unlock(&hdmi->mutex);
5213
5214 if (hdmi->irq)
5215 disable_irq(hdmi->irq);
5216 cancel_delayed_work(&hdmi->work);
5217 flush_workqueue(hdmi->workqueue);
5218 pinctrl_pm_select_sleep_state(hdmi->dev);
5219 }
5220 EXPORT_SYMBOL_GPL(dw_hdmi_suspend);
5221
dw_hdmi_resume(struct dw_hdmi * hdmi)5222 void dw_hdmi_resume(struct dw_hdmi *hdmi)
5223 {
5224 if (!hdmi)
5225 return;
5226
5227 pinctrl_pm_select_default_state(hdmi->dev);
5228 mutex_lock(&hdmi->mutex);
5229 dw_hdmi_reg_initial(hdmi);
5230 if (hdmi->i2c)
5231 dw_hdmi_i2c_init(hdmi);
5232 if (hdmi->irq)
5233 enable_irq(hdmi->irq);
5234 /*
5235 * HDMI status maybe incorrect in the following condition:
5236 * HDMI plug in -> system sleep -> HDMI plug out -> system wake up.
5237 * At this time, cat /sys/class/drm/card 0-HDMI-A-1/status is connected.
5238 * There is no hpd interrupt, because HDMI is powerdown during suspend.
5239 * So we need check the current HDMI status in this case.
5240 */
5241 if (hdmi->connector.status == connector_status_connected) {
5242 if (hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data) ==
5243 connector_status_disconnected) {
5244 hdmi->hpd_state = false;
5245 mod_delayed_work(hdmi->workqueue, &hdmi->work,
5246 msecs_to_jiffies(20));
5247 }
5248 }
5249 mutex_unlock(&hdmi->mutex);
5250 }
5251 EXPORT_SYMBOL_GPL(dw_hdmi_resume);
5252
5253 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
5254 MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
5255 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
5256 MODULE_AUTHOR("Vladimir Zapolskiy <vladimir_zapolskiy@mentor.com>");
5257 MODULE_DESCRIPTION("DW HDMI transmitter driver");
5258 MODULE_LICENSE("GPL");
5259 MODULE_ALIAS("platform:dw-hdmi");
5260