blob: b74607feb35cc87a5130d836f2e80898954f2ebd [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>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25
26#include "sun4i_backend.h"
27#include "sun4i_crtc.h"
28#include "sun4i_drv.h"
29#include "sun4i_hdmi.h"
30#include "sun4i_tcon.h"
31
Maxime Ripard9c568102017-05-27 18:09:35 +020032static inline struct sun4i_hdmi *
33drm_encoder_to_sun4i_hdmi(struct drm_encoder *encoder)
34{
35 return container_of(encoder, struct sun4i_hdmi,
36 encoder);
37}
38
39static inline struct sun4i_hdmi *
40drm_connector_to_sun4i_hdmi(struct drm_connector *connector)
41{
42 return container_of(connector, struct sun4i_hdmi,
43 connector);
44}
45
46static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi,
47 struct drm_display_mode *mode)
48{
49 struct hdmi_avi_infoframe frame;
50 u8 buffer[17];
51 int i, ret;
52
53 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame, mode);
54 if (ret < 0) {
55 DRM_ERROR("Failed to get infoframes from mode\n");
56 return ret;
57 }
58
59 ret = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
60 if (ret < 0) {
61 DRM_ERROR("Failed to pack infoframes\n");
62 return ret;
63 }
64
65 for (i = 0; i < sizeof(buffer); i++)
66 writeb(buffer[i], hdmi->base + SUN4I_HDMI_AVI_INFOFRAME_REG(i));
67
68 return 0;
69}
70
71static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder,
72 struct drm_crtc_state *crtc_state,
73 struct drm_connector_state *conn_state)
74{
75 struct drm_display_mode *mode = &crtc_state->mode;
76
77 if (mode->flags & DRM_MODE_FLAG_DBLCLK)
78 return -EINVAL;
79
80 return 0;
81}
82
83static void sun4i_hdmi_disable(struct drm_encoder *encoder)
84{
85 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
86 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
87 struct sun4i_tcon *tcon = crtc->tcon;
88 u32 val;
89
90 DRM_DEBUG_DRIVER("Disabling the HDMI Output\n");
91
92 val = readl(hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
93 val &= ~SUN4I_HDMI_VID_CTRL_ENABLE;
94 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
95
96 sun4i_tcon_channel_disable(tcon, 1);
97}
98
99static void sun4i_hdmi_enable(struct drm_encoder *encoder)
100{
101 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
102 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
103 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
104 struct sun4i_tcon *tcon = crtc->tcon;
105 u32 val = 0;
106
107 DRM_DEBUG_DRIVER("Enabling the HDMI Output\n");
108
109 sun4i_tcon_channel_enable(tcon, 1);
110
111 sun4i_hdmi_setup_avi_infoframes(hdmi, mode);
112 val |= SUN4I_HDMI_PKT_CTRL_TYPE(0, SUN4I_HDMI_PKT_AVI);
113 val |= SUN4I_HDMI_PKT_CTRL_TYPE(1, SUN4I_HDMI_PKT_END);
114 writel(val, hdmi->base + SUN4I_HDMI_PKT_CTRL_REG(0));
115
116 val = SUN4I_HDMI_VID_CTRL_ENABLE;
117 if (hdmi->hdmi_monitor)
118 val |= SUN4I_HDMI_VID_CTRL_HDMI_MODE;
119
120 writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
121}
122
123static void sun4i_hdmi_mode_set(struct drm_encoder *encoder,
124 struct drm_display_mode *mode,
125 struct drm_display_mode *adjusted_mode)
126{
127 struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
128 struct sun4i_crtc *crtc = drm_crtc_to_sun4i_crtc(encoder->crtc);
129 struct sun4i_tcon *tcon = crtc->tcon;
130 unsigned int x, y;
131 u32 val;
132
133 sun4i_tcon1_mode_set(tcon, mode);
134 sun4i_tcon_set_mux(tcon, 1, encoder);
135
136 clk_set_rate(tcon->sclk1, mode->crtc_clock * 1000);
137 clk_set_rate(hdmi->mod_clk, mode->crtc_clock * 1000);
138 clk_set_rate(hdmi->tmds_clk, mode->crtc_clock * 1000);
139
140 /* Set input sync enable */
141 writel(SUN4I_HDMI_UNKNOWN_INPUT_SYNC,
142 hdmi->base + SUN4I_HDMI_UNKNOWN_REG);
143
144 /* Setup timing registers */
145 writel(SUN4I_HDMI_VID_TIMING_X(mode->hdisplay) |
146 SUN4I_HDMI_VID_TIMING_Y(mode->vdisplay),
147 hdmi->base + SUN4I_HDMI_VID_TIMING_ACT_REG);
148
149 x = mode->htotal - mode->hsync_start;
150 y = mode->vtotal - mode->vsync_start;
151 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
152 hdmi->base + SUN4I_HDMI_VID_TIMING_BP_REG);
153
154 x = mode->hsync_start - mode->hdisplay;
155 y = mode->vsync_start - mode->vdisplay;
156 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
157 hdmi->base + SUN4I_HDMI_VID_TIMING_FP_REG);
158
159 x = mode->hsync_end - mode->hsync_start;
160 y = mode->vsync_end - mode->vsync_start;
161 writel(SUN4I_HDMI_VID_TIMING_X(x) | SUN4I_HDMI_VID_TIMING_Y(y),
162 hdmi->base + SUN4I_HDMI_VID_TIMING_SPW_REG);
163
164 val = SUN4I_HDMI_VID_TIMING_POL_TX_CLK;
165 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
166 val |= SUN4I_HDMI_VID_TIMING_POL_HSYNC;
167
168 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
169 val |= SUN4I_HDMI_VID_TIMING_POL_VSYNC;
170
171 writel(val, hdmi->base + SUN4I_HDMI_VID_TIMING_POL_REG);
172}
173
174static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
175 .atomic_check = sun4i_hdmi_atomic_check,
176 .disable = sun4i_hdmi_disable,
177 .enable = sun4i_hdmi_enable,
178 .mode_set = sun4i_hdmi_mode_set,
179};
180
181static const struct drm_encoder_funcs sun4i_hdmi_funcs = {
182 .destroy = drm_encoder_cleanup,
183};
184
Maxime Ripard9c568102017-05-27 18:09:35 +0200185static int sun4i_hdmi_get_modes(struct drm_connector *connector)
186{
187 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
Maxime Ripard9c568102017-05-27 18:09:35 +0200188 struct edid *edid;
189 int ret;
190
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000191 edid = drm_get_edid(connector, hdmi->i2c);
Maxime Ripard9c568102017-05-27 18:09:35 +0200192 if (!edid)
193 return 0;
194
195 hdmi->hdmi_monitor = drm_detect_hdmi_monitor(edid);
196 DRM_DEBUG_DRIVER("Monitor is %s monitor\n",
197 hdmi->hdmi_monitor ? "an HDMI" : "a DVI");
198
199 drm_mode_connector_update_edid_property(connector, edid);
200 ret = drm_add_edid_modes(connector, edid);
201 kfree(edid);
202
Maxime Ripard9c568102017-05-27 18:09:35 +0200203 return ret;
204}
205
206static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = {
207 .get_modes = sun4i_hdmi_get_modes,
208};
209
210static enum drm_connector_status
211sun4i_hdmi_connector_detect(struct drm_connector *connector, bool force)
212{
213 struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
214 unsigned long reg;
215
216 if (readl_poll_timeout(hdmi->base + SUN4I_HDMI_HPD_REG, reg,
217 reg & SUN4I_HDMI_HPD_HIGH,
218 0, 500000))
219 return connector_status_disconnected;
220
221 return connector_status_connected;
222}
223
224static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = {
225 .dpms = drm_atomic_helper_connector_dpms,
226 .detect = sun4i_hdmi_connector_detect,
227 .fill_modes = drm_helper_probe_single_connector_modes,
228 .destroy = drm_connector_cleanup,
229 .reset = drm_atomic_helper_connector_reset,
230 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
231 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
232};
233
234static int sun4i_hdmi_bind(struct device *dev, struct device *master,
235 void *data)
236{
237 struct platform_device *pdev = to_platform_device(dev);
238 struct drm_device *drm = data;
239 struct sun4i_drv *drv = drm->dev_private;
240 struct sun4i_hdmi *hdmi;
241 struct resource *res;
242 u32 reg;
243 int ret;
244
245 hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
246 if (!hdmi)
247 return -ENOMEM;
248 dev_set_drvdata(dev, hdmi);
249 hdmi->dev = dev;
250 hdmi->drv = drv;
251
252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 hdmi->base = devm_ioremap_resource(dev, res);
254 if (IS_ERR(hdmi->base)) {
255 dev_err(dev, "Couldn't map the HDMI encoder registers\n");
256 return PTR_ERR(hdmi->base);
257 }
258
259 hdmi->bus_clk = devm_clk_get(dev, "ahb");
260 if (IS_ERR(hdmi->bus_clk)) {
261 dev_err(dev, "Couldn't get the HDMI bus clock\n");
262 return PTR_ERR(hdmi->bus_clk);
263 }
264 clk_prepare_enable(hdmi->bus_clk);
265
266 hdmi->mod_clk = devm_clk_get(dev, "mod");
267 if (IS_ERR(hdmi->mod_clk)) {
268 dev_err(dev, "Couldn't get the HDMI mod clock\n");
269 return PTR_ERR(hdmi->mod_clk);
270 }
271 clk_prepare_enable(hdmi->mod_clk);
272
273 hdmi->pll0_clk = devm_clk_get(dev, "pll-0");
274 if (IS_ERR(hdmi->pll0_clk)) {
275 dev_err(dev, "Couldn't get the HDMI PLL 0 clock\n");
276 return PTR_ERR(hdmi->pll0_clk);
277 }
278
279 hdmi->pll1_clk = devm_clk_get(dev, "pll-1");
280 if (IS_ERR(hdmi->pll1_clk)) {
281 dev_err(dev, "Couldn't get the HDMI PLL 1 clock\n");
282 return PTR_ERR(hdmi->pll1_clk);
283 }
284
285 ret = sun4i_tmds_create(hdmi);
286 if (ret) {
287 dev_err(dev, "Couldn't create the TMDS clock\n");
288 return ret;
289 }
290
291 writel(SUN4I_HDMI_CTRL_ENABLE, hdmi->base + SUN4I_HDMI_CTRL_REG);
292
293 writel(SUN4I_HDMI_PAD_CTRL0_TXEN | SUN4I_HDMI_PAD_CTRL0_CKEN |
294 SUN4I_HDMI_PAD_CTRL0_PWENG | SUN4I_HDMI_PAD_CTRL0_PWEND |
295 SUN4I_HDMI_PAD_CTRL0_PWENC | SUN4I_HDMI_PAD_CTRL0_LDODEN |
296 SUN4I_HDMI_PAD_CTRL0_LDOCEN | SUN4I_HDMI_PAD_CTRL0_BIASEN,
297 hdmi->base + SUN4I_HDMI_PAD_CTRL0_REG);
298
299 /*
300 * We can't just initialize the register there, we need to
301 * protect the clock bits that have already been read out and
302 * cached by the clock framework.
303 */
304 reg = readl(hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
305 reg &= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
306 reg |= SUN4I_HDMI_PAD_CTRL1_REG_AMP(6) |
307 SUN4I_HDMI_PAD_CTRL1_REG_EMP(2) |
308 SUN4I_HDMI_PAD_CTRL1_REG_DENCK |
309 SUN4I_HDMI_PAD_CTRL1_REG_DEN |
310 SUN4I_HDMI_PAD_CTRL1_EMPCK_OPT |
311 SUN4I_HDMI_PAD_CTRL1_EMP_OPT |
312 SUN4I_HDMI_PAD_CTRL1_AMPCK_OPT |
313 SUN4I_HDMI_PAD_CTRL1_AMP_OPT;
314 writel(reg, hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
315
316 reg = readl(hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
317 reg &= SUN4I_HDMI_PLL_CTRL_DIV_MASK;
318 reg |= SUN4I_HDMI_PLL_CTRL_VCO_S(8) | SUN4I_HDMI_PLL_CTRL_CS(7) |
319 SUN4I_HDMI_PLL_CTRL_CP_S(15) | SUN4I_HDMI_PLL_CTRL_S(7) |
320 SUN4I_HDMI_PLL_CTRL_VCO_GAIN(4) | SUN4I_HDMI_PLL_CTRL_SDIV2 |
321 SUN4I_HDMI_PLL_CTRL_LDO2_EN | SUN4I_HDMI_PLL_CTRL_LDO1_EN |
322 SUN4I_HDMI_PLL_CTRL_HV_IS_33 | SUN4I_HDMI_PLL_CTRL_BWS |
323 SUN4I_HDMI_PLL_CTRL_PLL_EN;
324 writel(reg, hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
325
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000326 ret = sun4i_hdmi_i2c_create(dev, hdmi);
Maxime Ripard9c568102017-05-27 18:09:35 +0200327 if (ret) {
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000328 dev_err(dev, "Couldn't create the HDMI I2C adapter\n");
Maxime Ripard9c568102017-05-27 18:09:35 +0200329 return ret;
330 }
331
332 drm_encoder_helper_add(&hdmi->encoder,
333 &sun4i_hdmi_helper_funcs);
334 ret = drm_encoder_init(drm,
335 &hdmi->encoder,
336 &sun4i_hdmi_funcs,
337 DRM_MODE_ENCODER_TMDS,
338 NULL);
339 if (ret) {
340 dev_err(dev, "Couldn't initialise the HDMI encoder\n");
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000341 goto err_del_i2c_adapter;
Maxime Ripard9c568102017-05-27 18:09:35 +0200342 }
343
344 hdmi->encoder.possible_crtcs = drm_of_find_possible_crtcs(drm,
345 dev->of_node);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000346 if (!hdmi->encoder.possible_crtcs) {
347 ret = -EPROBE_DEFER;
348 goto err_del_i2c_adapter;
349 }
Maxime Ripard9c568102017-05-27 18:09:35 +0200350
351 drm_connector_helper_add(&hdmi->connector,
352 &sun4i_hdmi_connector_helper_funcs);
353 ret = drm_connector_init(drm, &hdmi->connector,
354 &sun4i_hdmi_connector_funcs,
355 DRM_MODE_CONNECTOR_HDMIA);
356 if (ret) {
357 dev_err(dev,
358 "Couldn't initialise the HDMI connector\n");
359 goto err_cleanup_connector;
360 }
361
362 /* There is no HPD interrupt, so we need to poll the controller */
363 hdmi->connector.polled = DRM_CONNECTOR_POLL_CONNECT |
364 DRM_CONNECTOR_POLL_DISCONNECT;
365
366 drm_mode_connector_attach_encoder(&hdmi->connector, &hdmi->encoder);
367
368 return 0;
369
370err_cleanup_connector:
371 drm_encoder_cleanup(&hdmi->encoder);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000372err_del_i2c_adapter:
373 i2c_del_adapter(hdmi->i2c);
Maxime Ripard9c568102017-05-27 18:09:35 +0200374 return ret;
375}
376
377static void sun4i_hdmi_unbind(struct device *dev, struct device *master,
378 void *data)
379{
380 struct sun4i_hdmi *hdmi = dev_get_drvdata(dev);
381
382 drm_connector_cleanup(&hdmi->connector);
383 drm_encoder_cleanup(&hdmi->encoder);
Jonathan Liuf0a3dd32017-07-02 17:27:10 +1000384 i2c_del_adapter(hdmi->i2c);
Maxime Ripard9c568102017-05-27 18:09:35 +0200385}
386
387static const struct component_ops sun4i_hdmi_ops = {
388 .bind = sun4i_hdmi_bind,
389 .unbind = sun4i_hdmi_unbind,
390};
391
392static int sun4i_hdmi_probe(struct platform_device *pdev)
393{
394 return component_add(&pdev->dev, &sun4i_hdmi_ops);
395}
396
397static int sun4i_hdmi_remove(struct platform_device *pdev)
398{
399 component_del(&pdev->dev, &sun4i_hdmi_ops);
400
401 return 0;
402}
403
404static const struct of_device_id sun4i_hdmi_of_table[] = {
405 { .compatible = "allwinner,sun5i-a10s-hdmi" },
406 { }
407};
408MODULE_DEVICE_TABLE(of, sun4i_hdmi_of_table);
409
410static struct platform_driver sun4i_hdmi_driver = {
411 .probe = sun4i_hdmi_probe,
412 .remove = sun4i_hdmi_remove,
413 .driver = {
414 .name = "sun4i-hdmi",
415 .of_match_table = sun4i_hdmi_of_table,
416 },
417};
418module_platform_driver(sun4i_hdmi_driver);
419
420MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
421MODULE_DESCRIPTION("Allwinner A10 HDMI Driver");
422MODULE_LICENSE("GPL");