blob: 6ca6e6a74c4ac4f438b8303d3810b10566f3242d [file] [log] [blame]
Maxime Ripard9c568102017-05-27 18:09:35 +02001/*
2 * Copyright (C) 2016 Maxime Ripard
3 *
4 * Maxime Ripard <maxime.ripard@free-electrons.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 */
11
12#include <drm/drmP.h>
13#include <drm/drm_atomic_helper.h>
14#include <drm/drm_crtc_helper.h>
15#include <drm/drm_edid.h>
16#include <drm/drm_encoder.h>
17#include <drm/drm_of.h>
18#include <drm/drm_panel.h>
19
20#include <linux/clk.h>
21#include <linux/component.h>
22#include <linux/iopoll.h>
Chen-Yu Tsai939d7492017-10-10 11:20:04 +080023#include <linux/of_device.h>
Maxime Ripard9c568102017-05-27 18:09:35 +020024#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
Chen-Yu Tsai4b1c9242017-10-10 11:20:01 +080026#include <linux/regmap.h>
Chen-Yu Tsai939d7492017-10-10 11:20:04 +080027#include <linux/reset.h>
Maxime Ripard9c568102017-05-27 18:09:35 +020028
29#include "sun4i_backend.h"
30#include "sun4i_crtc.h"
31#include "sun4i_drv.h"
32#include "sun4i_hdmi.h"
33#include "sun4i_tcon.h"
34
Maxime Ripard9c568102017-05-27 18:09:35 +020035static inline struct sun4i_hdmi *
36drm_encoder_to_sun4i_hdmi(struct drm_encoder *encoder)
37{
38 return container_of(encoder, struct sun4i_hdmi,
39 encoder);
40}
41
42static inline struct sun4i_hdmi *
43drm_connector_to_sun4i_hdmi(struct drm_connector *connector)
44{
45 return container_of(connector, struct sun4i_hdmi,
46 connector);
47}
48
49static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi,
50 struct drm_display_mode *mode)
51{
52 struct hdmi_avi_infoframe frame;
53 u8 buffer[17];
54 int i, ret;
55
Shashank Sharma0c1f5282017-07-13 21:03:07 +053056 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode, false);
Maxime Ripard9c568102017-05-27 18:09:35 +020057 if (ret < 0) {
58 DRM_ERROR("Failed to get infoframes from mode\n");
59 return ret;
60 }
61
62 ret = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
63 if (ret < 0) {
64 DRM_ERROR("Failed to pack infoframes\n");
65 return ret;
66 }
67
68 for (i = 0; i < sizeof(buffer); i++)
69 writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i));
70
71 return 0;
72}
73
74static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder,
75 struct drm_crtc_state *crtc_state,
76 struct drm_connector_state *conn_state)
77{
78 struct drm_display_mode *mode = &crtc_state->mode;
79
80 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
81 return -EINVAL;
82
83 return 0;
84}
85
86static void sun4i_hdmi_disable(struct drm_encoder *encoder)
87{
88 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
89 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
90 struct sun4i_tcon *tcon = crtc->tcon;
91 u32 val;
92
93 DRM_DEBUG_DRIVER("Disabling the HDMI Output\n");
94
95 val = readl(hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
96 val &= ~SUN4I_HDMI_VID_CTRL_ENABLE;
97 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
98
99 sun4i_tcon_channel_disable(tcon, 1);
100}
101
102static void sun4i_hdmi_enable(struct drm_encoder *encoder)
103{
104 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
105 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
106 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
107 struct sun4i_tcon *tcon = crtc->tcon;
108 u32 val = 0;
109
110 DRM_DEBUG_DRIVER("Enabling the HDMI Output\n");
111
112 sun4i_tcon_channel_enable(tcon, 1);
113
114 sun4i_hdmi_setup_avi_infoframes(hdmi, mode);
115 val |= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI);
116 val |= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END);
117 writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0));
118
119 val = SUN4I_HDMI_VID_CTRL_ENABLE;
120 if (hdmi->hdmi_monitor)
121 val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE;
122
123 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
124}
125
126static void sun4i_hdmi_mode_set(struct drm_encoder *encoder,
127 struct drm_display_mode *mode,
128 struct drm_display_mode *adjusted_mode)
129{
130 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
131 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
132 struct sun4i_tcon *tcon = crtc->tcon;
133 unsigned int x, y;
134 u32 val;
135
136 sun4i_tcon1_mode_set(tcon, mode);
137 sun4i_tcon_set_mux(tcon, 1, encoder);
138
139 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
140 clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000);
141 clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000);
142
143 /* Set input sync enable */
144 writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC,
145 hdmi->base + SUN4I_HDMI_UNKNOWN_REG);
146
Chen-Yu Tsaibfddd142017-10-14 12:02:52 +0800147 /*
148 * Setup output pad (?) controls
149 *
150 * This is done here instead of at probe/bind time because
151 * the controller seems to toggle some of the bits on its own.
152 *
153 * We can't just initialize the register there, we need to
154 * protect the clock bits that have already been read out and
155 * cached by the clock framework.
156 */
157 val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
158 val &= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
159 val |= hdmi->variant->pad_ctrl1_init_val;
160 writel(val, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
161 val = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
162
Maxime Ripard9c568102017-05-27 18:09:35 +0200163 /* Setup timing registers */
164 writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) |
165 SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay),
166 hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG);
167
168 x = mode->htotal - mode->hsync_start;
169 y = mode->vtotal - mode->vsync_start;
170 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
171 hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG);
172
173 x = mode->hsync_start - mode->hdisplay;
174 y = mode->vsync_start - mode->vdisplay;
175 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
176 hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG);
177
178 x = mode->hsync_end - mode->hsync_start;
179 y = mode->vsync_end - mode->vsync_start;
180 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
181 hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG);
182
183 val = SUN4I_HDMI_VID_TIMING_POL_TX_CLK;
184 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
185 val |= SUN4I_HDMI_VID_TIMING_POL_HSYNC;
186
187 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
188 val |= SUN4I_HDMI_VID_TIMING_POL_VSYNC;
189
190 writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG);
191}
192
193static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
194 .atomic_check = sun4i_hdmi_atomic_check,
195 .disable = sun4i_hdmi_disable,
196 .enable = sun4i_hdmi_enable,
197 .mode_set = sun4i_hdmi_mode_set,
198};
199
200static const struct drm_encoder_funcs sun4i_hdmi_funcs = {
201 .destroy = drm_encoder_cleanup,
202};
203
Maxime Ripard9c568102017-05-27 18:09:35 +0200204static int sun4i_hdmi_get_modes(struct drm_connector *connector)
205{
206 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
Maxime Ripard9c568102017-05-27 18:09:35 +0200207 struct edid *edid;
208 int ret;
209
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000210 edid = drm_get_edid(connector, hdmi->i2c);
Maxime Ripard9c568102017-05-27 18:09:35 +0200211 if (!edid)
212 return 0;
213
214 hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid);
215 DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
216 hdmi->hdmi_monitor ? "an HDMI" : "a DVI");
217
218 drm_mode_connector_update_edid_property(connector, edid);
Hans Verkuil998140d2017-07-11 08:30:44 +0200219 cec_s_phys_addr_from_edid(hdmi->cec_adap, edid);
Maxime Ripard9c568102017-05-27 18:09:35 +0200220 ret = drm_add_edid_modes(connector, edid);
221 kfree(edid);
222
Maxime Ripard9c568102017-05-27 18:09:35 +0200223 return ret;
224}
225
226static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = {
227 .get_modes = sun4i_hdmi_get_modes,
228};
229
230static enum drm_connector_status
231sun4i_hdmi_connector_detect(struct drm_connector *connector, bool force)
232{
233 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
234 unsigned long reg;
235
236 if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_HPD_REG, reg,
237 reg & SUN4I_HDMI_HPD_HIGH,
Hans Verkuil998140d2017-07-11 08:30:44 +0200238 0, 500000)) {
239 cec_phys_addr_invalidate(hdmi->cec_adap);
Maxime Ripard9c568102017-05-27 18:09:35 +0200240 return connector_status_disconnected;
Hans Verkuil998140d2017-07-11 08:30:44 +0200241 }
Maxime Ripard9c568102017-05-27 18:09:35 +0200242
243 return connector_status_connected;
244}
245
246static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = {
Maxime Ripard9c568102017-05-27 18:09:35 +0200247 .detect = sun4i_hdmi_connector_detect,
248 .fill_modes = drm_helper_probe_single_connector_modes,
249 .destroy = drm_connector_cleanup,
250 .reset = drm_atomic_helper_connector_reset,
251 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
252 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
253};
254
Hans Verkuil998140d2017-07-11 08:30:44 +0200255#ifdef CONFIG_DRM_SUN4I_HDMI_CEC
256static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap)
257{
258 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
259
260 return readl(hdmi->base + SUN4I_HDMI_CEC) & SUN4I_HDMI_CEC_RX;
261}
262
263static void sun4i_hdmi_cec_pin_low(struct cec_adapter *adap)
264{
265 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
266
267 /* Start driving the CEC pin low */
268 writel(SUN4I_HDMI_CEC_ENABLE, hdmi->base + SUN4I_HDMI_CEC);
269}
270
271static void sun4i_hdmi_cec_pin_high(struct cec_adapter *adap)
272{
273 struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);
274
275 /*
276 * Stop driving the CEC pin, the pull up will take over
277 * unless another CEC device is driving the pin low.
278 */
279 writel(0, hdmi->base + SUN4I_HDMI_CEC);
280}
281
282static const struct cec_pin_ops sun4i_hdmi_cec_pin_ops = {
283 .read = sun4i_hdmi_cec_pin_read,
284 .low = sun4i_hdmi_cec_pin_low,
285 .high = sun4i_hdmi_cec_pin_high,
286};
287#endif
288
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800289#define SUN4I_HDMI_PAD_CTRL1_MASK (GENMASK(24, 7) | GENMASK(5, 0))
290#define SUN4I_HDMI_PLL_CTRL_MASK (GENMASK(31, 8) | GENMASK(3, 0))
291
292static const struct sun4i_hdmi_variant sun5i_variant = {
293 .pad_ctrl0_init_val = SUN4I_HDMI_PAD_CTRL0_TXEN |
294 SUN4I_HDMI_PAD_CTRL0_CKEN |
295 SUN4I_HDMI_PAD_CTRL0_PWENG |
296 SUN4I_HDMI_PAD_CTRL0_PWEND |
297 SUN4I_HDMI_PAD_CTRL0_PWENC |
298 SUN4I_HDMI_PAD_CTRL0_LDODEN |
299 SUN4I_HDMI_PAD_CTRL0_LDOCEN |
300 SUN4I_HDMI_PAD_CTRL0_BIASEN,
301 .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
302 SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
303 SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
304 SUN4I_HDMI_PAD_CTRL1_REG_DEN |
305 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
306 SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
307 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
308 SUN4I_HDMI_PAD_CTRL1_AMP_OPT,
309 .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
310 SUN4I_HDMI_PLL_CTRL_CS(7) |
311 SUN4I_HDMI_PLL_CTRL_CP_S(15) |
312 SUN4I_HDMI_PLL_CTRL_S(7) |
313 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
314 SUN4I_HDMI_PLL_CTRL_SDIV2 |
315 SUN4I_HDMI_PLL_CTRL_LDO2_EN |
316 SUN4I_HDMI_PLL_CTRL_LDO1_EN |
317 SUN4I_HDMI_PLL_CTRL_HV_IS_33 |
318 SUN4I_HDMI_PLL_CTRL_BWS |
319 SUN4I_HDMI_PLL_CTRL_PLL_EN,
320
321 .ddc_clk_reg = REG_FIELD(SUN4I_HDMI_DDC_CLK_REG, 0, 6),
322 .ddc_clk_pre_divider = 2,
323 .ddc_clk_m_offset = 1,
324
325 .field_ddc_en = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 31, 31),
326 .field_ddc_start = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 30, 30),
327 .field_ddc_reset = REG_FIELD(SUN4I_HDMI_DDC_CTRL_REG, 0, 0),
328 .field_ddc_addr_reg = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 31),
329 .field_ddc_slave_addr = REG_FIELD(SUN4I_HDMI_DDC_ADDR_REG, 0, 6),
330 .field_ddc_int_status = REG_FIELD(SUN4I_HDMI_DDC_INT_STATUS_REG, 0, 8),
331 .field_ddc_fifo_clear = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 31, 31),
332 .field_ddc_fifo_rx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 4, 7),
333 .field_ddc_fifo_tx_thres = REG_FIELD(SUN4I_HDMI_DDC_FIFO_CTRL_REG, 0, 3),
334 .field_ddc_byte_count = REG_FIELD(SUN4I_HDMI_DDC_BYTE_COUNT_REG, 0, 9),
335 .field_ddc_cmd = REG_FIELD(SUN4I_HDMI_DDC_CMD_REG, 0, 2),
336 .field_ddc_sda_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 9, 9),
337 .field_ddc_sck_en = REG_FIELD(SUN4I_HDMI_DDC_LINE_CTRL_REG, 8, 8),
338
339 .ddc_fifo_reg = SUN4I_HDMI_DDC_FIFO_DATA_REG,
340 .ddc_fifo_has_dir = true,
341};
342
Chen-Yu Tsaida184dee2017-10-10 11:20:06 +0800343static const struct sun4i_hdmi_variant sun6i_variant = {
344 .has_ddc_parent_clk = true,
345 .has_reset_control = true,
346 .pad_ctrl0_init_val = 0xff |
347 SUN4I_HDMI_PAD_CTRL0_TXEN |
348 SUN4I_HDMI_PAD_CTRL0_CKEN |
349 SUN4I_HDMI_PAD_CTRL0_PWENG |
350 SUN4I_HDMI_PAD_CTRL0_PWEND |
351 SUN4I_HDMI_PAD_CTRL0_PWENC |
352 SUN4I_HDMI_PAD_CTRL0_LDODEN |
353 SUN4I_HDMI_PAD_CTRL0_LDOCEN,
354 .pad_ctrl1_init_val = SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
355 SUN4I_HDMI_PAD_CTRL1_REG_EMP(4) |
356 SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
357 SUN4I_HDMI_PAD_CTRL1_REG_DEN |
358 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
359 SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
360 SUN4I_HDMI_PAD_CTRL1_PWSDT |
361 SUN4I_HDMI_PAD_CTRL1_PWSCK |
362 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
363 SUN4I_HDMI_PAD_CTRL1_AMP_OPT |
364 SUN4I_HDMI_PAD_CTRL1_UNKNOWN,
365 .pll_ctrl_init_val = SUN4I_HDMI_PLL_CTRL_VCO_S(8) |
366 SUN4I_HDMI_PLL_CTRL_CS(3) |
367 SUN4I_HDMI_PLL_CTRL_CP_S(10) |
368 SUN4I_HDMI_PLL_CTRL_S(4) |
369 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) |
370 SUN4I_HDMI_PLL_CTRL_SDIV2 |
371 SUN4I_HDMI_PLL_CTRL_LDO2_EN |
372 SUN4I_HDMI_PLL_CTRL_LDO1_EN |
373 SUN4I_HDMI_PLL_CTRL_HV_IS_33 |
374 SUN4I_HDMI_PLL_CTRL_PLL_EN,
375
376 .ddc_clk_reg = REG_FIELD(SUN6I_HDMI_DDC_CLK_REG, 0, 6),
377 .ddc_clk_pre_divider = 1,
378 .ddc_clk_m_offset = 2,
379
380 .tmds_clk_div_offset = 1,
381
382 .field_ddc_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 0, 0),
383 .field_ddc_start = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 27, 27),
384 .field_ddc_reset = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 31, 31),
385 .field_ddc_addr_reg = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 31),
386 .field_ddc_slave_addr = REG_FIELD(SUN6I_HDMI_DDC_ADDR_REG, 1, 7),
387 .field_ddc_int_status = REG_FIELD(SUN6I_HDMI_DDC_INT_STATUS_REG, 0, 8),
388 .field_ddc_fifo_clear = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 18, 18),
389 .field_ddc_fifo_rx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 4, 7),
390 .field_ddc_fifo_tx_thres = REG_FIELD(SUN6I_HDMI_DDC_FIFO_CTRL_REG, 0, 3),
391 .field_ddc_byte_count = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 16, 25),
392 .field_ddc_cmd = REG_FIELD(SUN6I_HDMI_DDC_CMD_REG, 0, 2),
393 .field_ddc_sda_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 6, 6),
394 .field_ddc_sck_en = REG_FIELD(SUN6I_HDMI_DDC_CTRL_REG, 4, 4),
395
396 .ddc_fifo_reg = SUN6I_HDMI_DDC_FIFO_DATA_REG,
397 .ddc_fifo_thres_incl = true,
398};
399
Chen-Yu Tsai4b1c9242017-10-10 11:20:01 +0800400static const struct regmap_config sun4i_hdmi_regmap_config = {
401 .reg_bits = 32,
402 .val_bits = 32,
403 .reg_stride = 4,
404 .max_register = 0x580,
405};
406
Maxime Ripard9c568102017-05-27 18:09:35 +0200407static int sun4i_hdmi_bind(struct device *dev, struct device *master,
408 void *data)
409{
410 struct platform_device *pdev = to_platform_device(dev);
411 struct drm_device *drm = data;
412 struct sun4i_drv *drv = drm->dev_private;
413 struct sun4i_hdmi *hdmi;
414 struct resource *res;
415 u32 reg;
416 int ret;
417
418 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
419 if (!hdmi)
420 return -ENOMEM;
421 dev_set_drvdata(dev, hdmi);
422 hdmi->dev = dev;
423 hdmi->drv = drv;
424
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800425 hdmi->variant = of_device_get_match_data(dev);
426 if (!hdmi->variant)
427 return -EINVAL;
428
Maxime Ripard9c568102017-05-27 18:09:35 +0200429 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
430 hdmi->base = devm_ioremap_resource(dev, res);
431 if (IS_ERR(hdmi->base)) {
432 dev_err(dev, "Couldn't map the HDMI encoder registers\n");
433 return PTR_ERR(hdmi->base);
434 }
435
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800436 if (hdmi->variant->has_reset_control) {
437 hdmi->reset = devm_reset_control_get(dev, NULL);
438 if (IS_ERR(hdmi->reset)) {
439 dev_err(dev, "Couldn't get the HDMI reset control\n");
440 return PTR_ERR(hdmi->reset);
441 }
442
443 ret = reset_control_deassert(hdmi->reset);
444 if (ret) {
445 dev_err(dev, "Couldn't deassert HDMI reset\n");
446 return ret;
447 }
448 }
449
Maxime Ripard9c568102017-05-27 18:09:35 +0200450 hdmi->bus_clk = devm_clk_get(dev, "ahb");
451 if (IS_ERR(hdmi->bus_clk)) {
452 dev_err(dev, "Couldn't get the HDMI bus clock\n");
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800453 ret = PTR_ERR(hdmi->bus_clk);
454 goto err_assert_reset;
Maxime Ripard9c568102017-05-27 18:09:35 +0200455 }
456 clk_prepare_enable(hdmi->bus_clk);
457
458 hdmi->mod_clk = devm_clk_get(dev, "mod");
459 if (IS_ERR(hdmi->mod_clk)) {
460 dev_err(dev, "Couldn't get the HDMI mod clock\n");
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800461 ret = PTR_ERR(hdmi->mod_clk);
462 goto err_disable_bus_clk;
Maxime Ripard9c568102017-05-27 18:09:35 +0200463 }
464 clk_prepare_enable(hdmi->mod_clk);
465
466 hdmi->pll0_clk = devm_clk_get(dev, "pll-0");
467 if (IS_ERR(hdmi->pll0_clk)) {
468 dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n");
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800469 ret = PTR_ERR(hdmi->pll0_clk);
470 goto err_disable_mod_clk;
Maxime Ripard9c568102017-05-27 18:09:35 +0200471 }
472
473 hdmi->pll1_clk = devm_clk_get(dev, "pll-1");
474 if (IS_ERR(hdmi->pll1_clk)) {
475 dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n");
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800476 ret = PTR_ERR(hdmi->pll1_clk);
477 goto err_disable_mod_clk;
Maxime Ripard9c568102017-05-27 18:09:35 +0200478 }
479
Chen-Yu Tsai4b1c9242017-10-10 11:20:01 +0800480 hdmi->regmap = devm_regmap_init_mmio(dev, hdmi->base,
481 &sun4i_hdmi_regmap_config);
482 if (IS_ERR(hdmi->regmap)) {
483 dev_err(dev, "Couldn't create HDMI encoder regmap\n");
484 return PTR_ERR(hdmi->regmap);
485 }
486
Maxime Ripard9c568102017-05-27 18:09:35 +0200487 ret = sun4i_tmds_create(hdmi);
488 if (ret) {
489 dev_err(dev, "Couldn't create the TMDS clock\n");
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800490 goto err_disable_mod_clk;
Maxime Ripard9c568102017-05-27 18:09:35 +0200491 }
492
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800493 if (hdmi->variant->has_ddc_parent_clk) {
494 hdmi->ddc_parent_clk = devm_clk_get(dev, "ddc");
495 if (IS_ERR(hdmi->ddc_parent_clk)) {
496 dev_err(dev, "Couldn't get the HDMI DDC clock\n");
497 return PTR_ERR(hdmi->ddc_parent_clk);
498 }
499 } else {
500 hdmi->ddc_parent_clk = hdmi->tmds_clk;
501 }
502
Maxime Ripard9c568102017-05-27 18:09:35 +0200503 writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG);
504
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800505 writel(hdmi->variant->pad_ctrl0_init_val,
Maxime Ripard9c568102017-05-27 18:09:35 +0200506 hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG);
507
Maxime Ripard9c568102017-05-27 18:09:35 +0200508 reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
509 reg &= SUN4I_HDMI_PLL_CTRL_DIV_MASK;
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800510 reg |= hdmi->variant->pll_ctrl_init_val;
Maxime Ripard9c568102017-05-27 18:09:35 +0200511 writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
512
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000513 ret = sun4i_hdmi_i2c_create(dev, hdmi);
Maxime Ripard9c568102017-05-27 18:09:35 +0200514 if (ret) {
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000515 dev_err(dev, "Couldn't create the HDMI I2C adapter\n");
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800516 goto err_disable_mod_clk;
Maxime Ripard9c568102017-05-27 18:09:35 +0200517 }
518
519 drm_encoder_helper_add(&hdmi->encoder,
520 &sun4i_hdmi_helper_funcs);
521 ret = drm_encoder_init(drm,
522 &hdmi->encoder,
523 &sun4i_hdmi_funcs,
524 DRM_MODE_ENCODER_TMDS,
525 NULL);
526 if (ret) {
527 dev_err(dev, "Couldn't initialise the HDMI encoder\n");
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000528 goto err_del_i2c_adapter;
Maxime Ripard9c568102017-05-27 18:09:35 +0200529 }
530
531 hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm,
532 dev->of_node);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000533 if (!hdmi->encoder.possible_crtcs) {
534 ret = -EPROBE_DEFER;
535 goto err_del_i2c_adapter;
536 }
Maxime Ripard9c568102017-05-27 18:09:35 +0200537
Hans Verkuil998140d2017-07-11 08:30:44 +0200538#ifdef CONFIG_DRM_SUN4I_HDMI_CEC
539 hdmi->cec_adap = cec_pin_allocate_adapter(&sun4i_hdmi_cec_pin_ops,
540 hdmi, "sun4i", CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS |
541 CEC_CAP_PASSTHROUGH | CEC_CAP_RC);
542 ret = PTR_ERR_OR_ZERO(hdmi->cec_adap);
543 if (ret < 0)
544 goto err_cleanup_connector;
545 writel(readl(hdmi->base + SUN4I_HDMI_CEC) & ~SUN4I_HDMI_CEC_TX,
546 hdmi->base + SUN4I_HDMI_CEC);
547#endif
Maxime Ripard9c568102017-05-27 18:09:35 +0200548
549 drm_connector_helper_add(&hdmi->connector,
550 &sun4i_hdmi_connector_helper_funcs);
551 ret = drm_connector_init(drm, &hdmi->connector,
552 &sun4i_hdmi_connector_funcs,
553 DRM_MODE_CONNECTOR_HDMIA);
554 if (ret) {
555 dev_err(dev,
556 "Couldn't initialise the HDMI connector\n");
557 goto err_cleanup_connector;
558 }
559
560 /* There is no HPD interrupt, so we need to poll the controller */
561 hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
562 DRM_CONNECTOR_POLL_DISCONNECT;
563
Hans Verkuil998140d2017-07-11 08:30:44 +0200564 ret = cec_register_adapter(hdmi->cec_adap, dev);
565 if (ret < 0)
566 goto err_cleanup_connector;
Maxime Ripard9c568102017-05-27 18:09:35 +0200567 drm_mode_connector_attach_encoder(&hdmi->connector, &hdmi->encoder);
568
569 return 0;
570
571err_cleanup_connector:
Hans Verkuil998140d2017-07-11 08:30:44 +0200572 cec_delete_adapter(hdmi->cec_adap);
Maxime Ripard9c568102017-05-27 18:09:35 +0200573 drm_encoder_cleanup(&hdmi->encoder);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000574err_del_i2c_adapter:
575 i2c_del_adapter(hdmi->i2c);
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800576err_disable_mod_clk:
577 clk_disable_unprepare(hdmi->mod_clk);
578err_disable_bus_clk:
579 clk_disable_unprepare(hdmi->bus_clk);
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800580err_assert_reset:
581 reset_control_assert(hdmi->reset);
Maxime Ripard9c568102017-05-27 18:09:35 +0200582 return ret;
583}
584
585static void sun4i_hdmi_unbind(struct device *dev, struct device *master,
586 void *data)
587{
588 struct sun4i_hdmi *hdmi = dev_get_drvdata(dev);
589
Hans Verkuil998140d2017-07-11 08:30:44 +0200590 cec_unregister_adapter(hdmi->cec_adap);
Maxime Ripard9c568102017-05-27 18:09:35 +0200591 drm_connector_cleanup(&hdmi->connector);
592 drm_encoder_cleanup(&hdmi->encoder);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000593 i2c_del_adapter(hdmi->i2c);
Chen-Yu Tsai544c5042017-10-10 11:20:00 +0800594 clk_disable_unprepare(hdmi->mod_clk);
595 clk_disable_unprepare(hdmi->bus_clk);
Maxime Ripard9c568102017-05-27 18:09:35 +0200596}
597
598static const struct component_ops sun4i_hdmi_ops = {
599 .bind = sun4i_hdmi_bind,
600 .unbind = sun4i_hdmi_unbind,
601};
602
603static int sun4i_hdmi_probe(struct platform_device *pdev)
604{
605 return component_add(&pdev->dev, &sun4i_hdmi_ops);
606}
607
608static int sun4i_hdmi_remove(struct platform_device *pdev)
609{
610 component_del(&pdev->dev, &sun4i_hdmi_ops);
611
612 return 0;
613}
614
615static const struct of_device_id sun4i_hdmi_of_table[] = {
Chen-Yu Tsai939d7492017-10-10 11:20:04 +0800616 { .compatible = "allwinner,sun5i-a10s-hdmi", .data = &sun5i_variant, },
Chen-Yu Tsaida184dee2017-10-10 11:20:06 +0800617 { .compatible = "allwinner,sun6i-a31-hdmi", .data = &sun6i_variant, },
Maxime Ripard9c568102017-05-27 18:09:35 +0200618 { }
619};
620MODULE_DEVICE_TABLE(of, sun4i_hdmi_of_table);
621
622static struct platform_driver sun4i_hdmi_driver = {
623 .probe = sun4i_hdmi_probe,
624 .remove = sun4i_hdmi_remove,
625 .driver = {
626 .name = "sun4i-hdmi",
627 .of_match_table = sun4i_hdmi_of_table,
628 },
629};
630module_platform_driver(sun4i_hdmi_driver);
631
632MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
633MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");
634MODULE_LICENSE("GPL");