2 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/clk.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
16 #include <drm/drm_of.h>
18 #include <drm/drm_crtc_helper.h>
19 #include <drm/drm_edid.h>
20 #include <drm/drm_encoder_slave.h>
21 #include <drm/bridge/dw_hdmi.h>
23 #include "rockchip_drm_drv.h"
24 #include "rockchip_drm_vop.h"
26 #define RK3288_GRF_SOC_CON6 0x025C
27 #define RK3288_HDMI_LCDC_SEL BIT(4)
28 #define RK3399_GRF_SOC_CON20 0x6250
29 #define RK3399_HDMI_LCDC_SEL BIT(6)
31 #define HIWORD_UPDATE(val, mask) (val | (mask) << 16)
33 struct rockchip_hdmi {
35 struct regmap *regmap;
36 struct drm_encoder encoder;
37 enum dw_hdmi_devtype dev_type;
42 #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
45 * There are some rates that would be ranged for better clock jitter at
46 * Chrome OS tree, like 25.175Mhz would range to 25.170732Mhz. But due
47 * to the clock is aglined to KHz in struct drm_display_mode, this would
48 * bring some inaccurate error if we still run the compute_n math, so
49 * let's just code an const table for it until we can actually get the
52 static const struct dw_hdmi_audio_tmds_n rockchip_werid_tmds_n_table[] = {
53 /* 25176471 for 25.175 MHz = 428000000 / 17. */
54 { .tmds = 25177000, .n_32k = 4352, .n_44k1 = 14994, .n_48k = 6528, },
55 /* 57290323 for 57.284 MHz */
56 { .tmds = 57291000, .n_32k = 3968, .n_44k1 = 4557, .n_48k = 5952, },
57 /* 74437500 for 74.44 MHz = 297750000 / 4 */
58 { .tmds = 74438000, .n_32k = 8192, .n_44k1 = 18816, .n_48k = 4096, },
59 /* 118666667 for 118.68 MHz */
60 { .tmds = 118667000, .n_32k = 4224, .n_44k1 = 5292, .n_48k = 6336, },
61 /* 121714286 for 121.75 MHz */
62 { .tmds = 121715000, .n_32k = 4480, .n_44k1 = 6174, .n_48k = 6272, },
63 /* 136800000 for 136.75 MHz */
64 { .tmds = 136800000, .n_32k = 4096, .n_44k1 = 5684, .n_48k = 6144, },
66 { .tmds = 0, .n_32k = 0, .n_44k1 = 0, .n_48k = 0, },
69 static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
157 static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
158 /* pixelclk bpp8 bpp10 bpp12 */
160 600000000, { 0x0000, 0x0000, 0x0000 },
162 ~0UL, { 0x0000, 0x0000, 0x0000},
166 static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
167 /*pixelclk symbol term vlev*/
168 { 74250000, 0x8009, 0x0004, 0x0272},
169 { 165000000, 0x802b, 0x0004, 0x0209},
170 { 297000000, 0x8039, 0x0005, 0x028d},
171 { ~0UL, 0x0000, 0x0000, 0x0000}
174 static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
176 struct device_node *np = hdmi->dev->of_node;
179 hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
180 if (IS_ERR(hdmi->regmap)) {
181 dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
182 return PTR_ERR(hdmi->regmap);
185 hdmi->vpll_clk = devm_clk_get(hdmi->dev, "vpll");
186 if (PTR_ERR(hdmi->vpll_clk) == -ENOENT) {
187 hdmi->vpll_clk = NULL;
188 } else if (PTR_ERR(hdmi->vpll_clk) == -EPROBE_DEFER) {
189 return -EPROBE_DEFER;
190 } else if (IS_ERR(hdmi->vpll_clk)) {
191 dev_err(hdmi->dev, "failed to get grf clock\n");
192 return PTR_ERR(hdmi->vpll_clk);
195 hdmi->grf_clk = devm_clk_get(hdmi->dev, "grf");
196 if (PTR_ERR(hdmi->grf_clk) == -ENOENT) {
197 hdmi->grf_clk = NULL;
198 } else if (PTR_ERR(hdmi->grf_clk) == -EPROBE_DEFER) {
199 return -EPROBE_DEFER;
200 } else if (IS_ERR(hdmi->grf_clk)) {
201 dev_err(hdmi->dev, "failed to get grf clock\n");
202 return PTR_ERR(hdmi->grf_clk);
205 ret = clk_prepare_enable(hdmi->vpll_clk);
207 dev_err(hdmi->dev, "Failed to enable HDMI vpll: %d\n", ret);
214 static enum drm_mode_status
215 dw_hdmi_rockchip_mode_valid(struct drm_connector *connector,
216 struct drm_display_mode *mode)
218 struct drm_encoder *encoder = connector->encoder;
219 enum drm_mode_status status = MODE_OK;
220 struct drm_device *dev = connector->dev;
221 struct rockchip_drm_private *priv = dev->dev_private;
222 struct drm_crtc *crtc;
225 * Pixel clocks we support are always < 2GHz and so fit in an
226 * int. We should make sure source rate does too so we don't get
227 * overflow when we multiply by 1000.
229 if (mode->clock > INT_MAX / 1000)
233 const struct drm_connector_helper_funcs *funcs;
235 funcs = connector->helper_private;
236 if (funcs->atomic_best_encoder)
237 encoder = funcs->atomic_best_encoder(connector,
240 encoder = funcs->best_encoder(connector);
243 if (!encoder || !encoder->possible_crtcs)
246 * ensure all drm display mode can work, if someone want support more
247 * resolutions, please limit the possible_crtc, only connect to
250 drm_for_each_crtc(crtc, connector->dev) {
251 int pipe = drm_crtc_index(crtc);
252 const struct rockchip_crtc_funcs *funcs =
253 priv->crtc_funcs[pipe];
255 if (!(encoder->possible_crtcs & drm_crtc_mask(crtc)))
257 if (!funcs || !funcs->mode_valid)
260 status = funcs->mode_valid(crtc, mode,
261 DRM_MODE_CONNECTOR_HDMIA);
262 if (status != MODE_OK)
269 static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs = {
270 .destroy = drm_encoder_cleanup,
273 static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
277 static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
279 struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
280 struct drm_crtc *crtc = encoder->crtc;
281 u32 lcdsel_grf_reg, lcdsel_mask;
286 if (WARN_ON(!crtc || !crtc->state))
289 clk_set_rate(hdmi->vpll_clk,
290 crtc->state->adjusted_mode.crtc_clock * 1000);
292 switch (hdmi->dev_type) {
294 lcdsel_grf_reg = RK3288_GRF_SOC_CON6;
295 lcdsel_mask = RK3288_HDMI_LCDC_SEL;
298 lcdsel_grf_reg = RK3399_GRF_SOC_CON20;
299 lcdsel_mask = RK3399_HDMI_LCDC_SEL;
305 mux = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
307 val = HIWORD_UPDATE(lcdsel_mask, lcdsel_mask);
309 val = HIWORD_UPDATE(0, lcdsel_mask);
311 ret = clk_prepare_enable(hdmi->grf_clk);
313 dev_err(hdmi->dev, "failed to enable grfclk %d\n", ret);
317 regmap_write(hdmi->regmap, lcdsel_grf_reg, val);
318 dev_dbg(hdmi->dev, "vop %s output to hdmi\n",
319 (mux) ? "LIT" : "BIG");
321 clk_disable_unprepare(hdmi->grf_clk);
325 dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
326 struct drm_crtc_state *crtc_state,
327 struct drm_connector_state *conn_state)
329 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
331 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
332 s->output_type = DRM_MODE_CONNECTOR_HDMIA;
333 s->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
338 static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
339 .enable = dw_hdmi_rockchip_encoder_enable,
340 .disable = dw_hdmi_rockchip_encoder_disable,
341 .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
344 static const struct dw_hdmi_plat_data rk3288_hdmi_drv_data = {
345 .mode_valid = dw_hdmi_rockchip_mode_valid,
346 .mpll_cfg = rockchip_mpll_cfg,
347 .cur_ctr = rockchip_cur_ctr,
348 .phy_config = rockchip_phy_config,
349 .dev_type = RK3288_HDMI,
350 .tmds_n_table = rockchip_werid_tmds_n_table,
353 static const struct dw_hdmi_plat_data rk3399_hdmi_drv_data = {
354 .mode_valid = dw_hdmi_rockchip_mode_valid,
355 .mpll_cfg = rockchip_mpll_cfg,
356 .cur_ctr = rockchip_cur_ctr,
357 .phy_config = rockchip_phy_config,
358 .dev_type = RK3399_HDMI,
361 static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
362 { .compatible = "rockchip,rk3288-dw-hdmi",
363 .data = &rk3288_hdmi_drv_data
365 { .compatible = "rockchip,rk3399-dw-hdmi",
366 .data = &rk3399_hdmi_drv_data
370 MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
372 static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
375 struct platform_device *pdev = to_platform_device(dev);
376 const struct dw_hdmi_plat_data *plat_data;
377 const struct of_device_id *match;
378 struct drm_device *drm = data;
379 struct drm_encoder *encoder;
380 struct rockchip_hdmi *hdmi;
381 struct resource *iores;
385 if (!pdev->dev.of_node)
388 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
392 match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
393 plat_data = match->data;
394 hdmi->dev = &pdev->dev;
395 hdmi->dev_type = plat_data->dev_type;
396 encoder = &hdmi->encoder;
398 irq = platform_get_irq(pdev, 0);
402 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
406 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
408 * If we failed to find the CRTC(s) which this encoder is
409 * supposed to be connected to, it's because the CRTC has
410 * not been registered yet. Defer probing, and hope that
411 * the required CRTC is added later.
413 if (encoder->possible_crtcs == 0)
414 return -EPROBE_DEFER;
416 ret = rockchip_hdmi_parse_dt(hdmi);
418 dev_err(hdmi->dev, "Unable to parse OF data\n");
422 drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
423 drm_encoder_init(drm, encoder, &dw_hdmi_rockchip_encoder_funcs,
424 DRM_MODE_ENCODER_TMDS, NULL);
426 ret = dw_hdmi_bind(dev, master, data, encoder, iores, irq, plat_data);
429 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
430 * which would have called the encoder cleanup. Do it manually.
433 drm_encoder_cleanup(encoder);
438 static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
441 return dw_hdmi_unbind(dev, master, data);
444 static const struct component_ops dw_hdmi_rockchip_ops = {
445 .bind = dw_hdmi_rockchip_bind,
446 .unbind = dw_hdmi_rockchip_unbind,
449 static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
451 return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
454 static int dw_hdmi_rockchip_remove(struct platform_device *pdev)
456 component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
461 static struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
462 .probe = dw_hdmi_rockchip_probe,
463 .remove = dw_hdmi_rockchip_remove,
465 .name = "dwhdmi-rockchip",
466 .of_match_table = dw_hdmi_rockchip_dt_ids,
470 module_platform_driver(dw_hdmi_rockchip_pltfm_driver);
472 MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
473 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
474 MODULE_DESCRIPTION("Rockchip Specific DW-HDMI Driver Extension");
475 MODULE_LICENSE("GPL");
476 MODULE_ALIAS("platform:dwhdmi-rockchip");