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/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/regmap.h>
14 #include <drm/drm_of.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_edid.h>
18 #include <drm/drm_encoder_slave.h>
19 #include <drm/bridge/dw_hdmi.h>
21 #include "rockchip_drm_drv.h"
22 #include "rockchip_drm_vop.h"
24 #define GRF_SOC_CON6 0x025c
25 #define HDMI_SEL_VOP_LIT (1 << 4)
27 struct rockchip_hdmi {
29 struct regmap *regmap;
30 struct drm_encoder encoder;
33 #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
35 static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
115 static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
116 /* pixelclk bpp8 bpp10 bpp12 */
118 40000000, { 0x0018, 0x0018, 0x0018 },
120 65000000, { 0x0028, 0x0028, 0x0028 },
122 66000000, { 0x0038, 0x0038, 0x0038 },
124 74250000, { 0x0028, 0x0038, 0x0038 },
126 83500000, { 0x0028, 0x0038, 0x0038 },
128 146250000, { 0x0038, 0x0038, 0x0038 },
130 148500000, { 0x0000, 0x0038, 0x0038 },
132 ~0UL, { 0x0000, 0x0000, 0x0000},
136 static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
137 /*pixelclk symbol term vlev*/
138 { 74250000, 0x8009, 0x0004, 0x0272},
139 { 148500000, 0x802b, 0x0004, 0x028d},
140 { 297000000, 0x8039, 0x0005, 0x028d},
141 { ~0UL, 0x0000, 0x0000, 0x0000}
144 static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
146 struct device_node *np = hdmi->dev->of_node;
148 hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
149 if (IS_ERR(hdmi->regmap)) {
150 dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
151 return PTR_ERR(hdmi->regmap);
157 static enum drm_mode_status
158 dw_hdmi_rockchip_mode_valid(struct drm_connector *connector,
159 struct drm_display_mode *mode)
161 const struct dw_hdmi_mpll_config *mpll_cfg = rockchip_mpll_cfg;
162 int pclk = mode->clock * 1000;
166 for (i = 0; mpll_cfg[i].mpixelclock != (~0UL); i++) {
167 if (pclk == mpll_cfg[i].mpixelclock) {
173 return (valid) ? MODE_OK : MODE_BAD;
176 static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs = {
177 .destroy = drm_encoder_cleanup,
180 static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
185 dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder *encoder,
186 const struct drm_display_mode *mode,
187 struct drm_display_mode *adj_mode)
192 static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder *encoder,
193 struct drm_display_mode *mode,
194 struct drm_display_mode *adj_mode)
198 static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
200 struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
204 mux = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
206 val = HDMI_SEL_VOP_LIT | (HDMI_SEL_VOP_LIT << 16);
208 val = HDMI_SEL_VOP_LIT << 16;
210 regmap_write(hdmi->regmap, GRF_SOC_CON6, val);
211 dev_dbg(hdmi->dev, "vop %s output to hdmi\n",
212 (mux) ? "LIT" : "BIG");
216 dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
217 struct drm_crtc_state *crtc_state,
218 struct drm_connector_state *conn_state)
220 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
222 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
223 s->output_type = DRM_MODE_CONNECTOR_HDMIA;
228 static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
229 .mode_fixup = dw_hdmi_rockchip_encoder_mode_fixup,
230 .mode_set = dw_hdmi_rockchip_encoder_mode_set,
231 .enable = dw_hdmi_rockchip_encoder_enable,
232 .disable = dw_hdmi_rockchip_encoder_disable,
233 .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
236 static const struct dw_hdmi_plat_data rockchip_hdmi_drv_data = {
237 .mode_valid = dw_hdmi_rockchip_mode_valid,
238 .mpll_cfg = rockchip_mpll_cfg,
239 .cur_ctr = rockchip_cur_ctr,
240 .phy_config = rockchip_phy_config,
241 .dev_type = RK3288_HDMI,
244 static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
245 { .compatible = "rockchip,rk3288-dw-hdmi",
246 .data = &rockchip_hdmi_drv_data
250 MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
252 static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
255 struct platform_device *pdev = to_platform_device(dev);
256 const struct dw_hdmi_plat_data *plat_data;
257 const struct of_device_id *match;
258 struct drm_device *drm = data;
259 struct drm_encoder *encoder;
260 struct rockchip_hdmi *hdmi;
261 struct resource *iores;
265 if (!pdev->dev.of_node)
268 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
272 match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
273 plat_data = match->data;
274 hdmi->dev = &pdev->dev;
275 encoder = &hdmi->encoder;
277 irq = platform_get_irq(pdev, 0);
281 iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
285 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
287 * If we failed to find the CRTC(s) which this encoder is
288 * supposed to be connected to, it's because the CRTC has
289 * not been registered yet. Defer probing, and hope that
290 * the required CRTC is added later.
292 if (encoder->possible_crtcs == 0)
293 return -EPROBE_DEFER;
295 ret = rockchip_hdmi_parse_dt(hdmi);
297 dev_err(hdmi->dev, "Unable to parse OF data\n");
301 drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
302 drm_encoder_init(drm, encoder, &dw_hdmi_rockchip_encoder_funcs,
303 DRM_MODE_ENCODER_TMDS, NULL);
305 ret = dw_hdmi_bind(dev, master, data, encoder, iores, irq, plat_data);
308 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
309 * which would have called the encoder cleanup. Do it manually.
312 drm_encoder_cleanup(encoder);
317 static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
320 return dw_hdmi_unbind(dev, master, data);
323 static const struct component_ops dw_hdmi_rockchip_ops = {
324 .bind = dw_hdmi_rockchip_bind,
325 .unbind = dw_hdmi_rockchip_unbind,
328 static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
330 return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
333 static int dw_hdmi_rockchip_remove(struct platform_device *pdev)
335 component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
340 static struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
341 .probe = dw_hdmi_rockchip_probe,
342 .remove = dw_hdmi_rockchip_remove,
344 .name = "dwhdmi-rockchip",
345 .of_match_table = dw_hdmi_rockchip_dt_ids,
349 module_platform_driver(dw_hdmi_rockchip_pltfm_driver);
351 MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
352 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
353 MODULE_DESCRIPTION("Rockchip Specific DW-HDMI Driver Extension");
354 MODULE_LICENSE("GPL");
355 MODULE_ALIAS("platform:dwhdmi-rockchip");