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/bridge/dw_hdmi.h>
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_vop.h"
23 #define GRF_SOC_CON6 0x025c
24 #define HDMI_SEL_VOP_LIT (1 << 4)
26 struct rockchip_hdmi {
28 struct regmap *regmap;
29 struct drm_encoder encoder;
32 #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
34 static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
114 static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
115 /* pixelclk bpp8 bpp10 bpp12 */
117 40000000, { 0x0018, 0x0018, 0x0018 },
119 65000000, { 0x0028, 0x0028, 0x0028 },
121 66000000, { 0x0038, 0x0038, 0x0038 },
123 74250000, { 0x0028, 0x0038, 0x0038 },
125 83500000, { 0x0028, 0x0038, 0x0038 },
127 146250000, { 0x0038, 0x0038, 0x0038 },
129 148500000, { 0x0000, 0x0038, 0x0038 },
131 ~0UL, { 0x0000, 0x0000, 0x0000},
135 static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
136 /*pixelclk symbol term vlev*/
137 { 74250000, 0x8009, 0x0004, 0x0272},
138 { 148500000, 0x802b, 0x0004, 0x028d},
139 { 297000000, 0x8039, 0x0005, 0x028d},
140 { ~0UL, 0x0000, 0x0000, 0x0000}
143 static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
145 struct device_node *np = hdmi->dev->of_node;
147 hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
148 if (IS_ERR(hdmi->regmap)) {
149 dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
150 return PTR_ERR(hdmi->regmap);
156 static enum drm_mode_status
157 dw_hdmi_rockchip_mode_valid(struct drm_connector *connector,
158 struct drm_display_mode *mode)
160 const struct dw_hdmi_mpll_config *mpll_cfg = rockchip_mpll_cfg;
161 int pclk = mode->clock * 1000;
165 for (i = 0; mpll_cfg[i].mpixelclock != (~0UL); i++) {
166 if (pclk == mpll_cfg[i].mpixelclock) {
172 return (valid) ? MODE_OK : MODE_BAD;
175 static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs = {
176 .destroy = drm_encoder_cleanup,
179 static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
184 dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder *encoder,
185 const struct drm_display_mode *mode,
186 struct drm_display_mode *adj_mode)
191 static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder *encoder,
192 struct drm_display_mode *mode,
193 struct drm_display_mode *adj_mode)
197 static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
199 struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
203 mux = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
205 val = HDMI_SEL_VOP_LIT | (HDMI_SEL_VOP_LIT << 16);
207 val = HDMI_SEL_VOP_LIT << 16;
209 regmap_write(hdmi->regmap, GRF_SOC_CON6, val);
210 dev_dbg(hdmi->dev, "vop %s output to hdmi\n",
211 (mux) ? "LIT" : "BIG");
215 dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
216 struct drm_crtc_state *crtc_state,
217 struct drm_connector_state *conn_state)
219 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
221 s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
222 s->output_type = DRM_MODE_CONNECTOR_HDMIA;
227 static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
228 .mode_fixup = dw_hdmi_rockchip_encoder_mode_fixup,
229 .mode_set = dw_hdmi_rockchip_encoder_mode_set,
230 .enable = dw_hdmi_rockchip_encoder_enable,
231 .disable = dw_hdmi_rockchip_encoder_disable,
232 .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
235 static const struct dw_hdmi_plat_data rockchip_hdmi_drv_data = {
236 .mode_valid = dw_hdmi_rockchip_mode_valid,
237 .mpll_cfg = rockchip_mpll_cfg,
238 .cur_ctr = rockchip_cur_ctr,
239 .phy_config = rockchip_phy_config,
242 static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
243 { .compatible = "rockchip,rk3288-dw-hdmi",
244 .data = &rockchip_hdmi_drv_data
248 MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
250 static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
253 struct platform_device *pdev = to_platform_device(dev);
254 const struct dw_hdmi_plat_data *plat_data;
255 const struct of_device_id *match;
256 struct drm_device *drm = data;
257 struct drm_encoder *encoder;
258 struct rockchip_hdmi *hdmi;
261 if (!pdev->dev.of_node)
264 hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
268 match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
269 plat_data = match->data;
270 hdmi->dev = &pdev->dev;
271 encoder = &hdmi->encoder;
273 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
275 * If we failed to find the CRTC(s) which this encoder is
276 * supposed to be connected to, it's because the CRTC has
277 * not been registered yet. Defer probing, and hope that
278 * the required CRTC is added later.
280 if (encoder->possible_crtcs == 0)
281 return -EPROBE_DEFER;
283 ret = rockchip_hdmi_parse_dt(hdmi);
285 dev_err(hdmi->dev, "Unable to parse OF data\n");
289 drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
290 drm_encoder_init(drm, encoder, &dw_hdmi_rockchip_encoder_funcs,
291 DRM_MODE_ENCODER_TMDS, NULL);
293 ret = dw_hdmi_bind(pdev, encoder, plat_data);
296 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
297 * which would have called the encoder cleanup. Do it manually.
300 drm_encoder_cleanup(encoder);
305 static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
308 return dw_hdmi_unbind(dev);
311 static const struct component_ops dw_hdmi_rockchip_ops = {
312 .bind = dw_hdmi_rockchip_bind,
313 .unbind = dw_hdmi_rockchip_unbind,
316 static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
318 return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
321 static int dw_hdmi_rockchip_remove(struct platform_device *pdev)
323 component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
328 struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
329 .probe = dw_hdmi_rockchip_probe,
330 .remove = dw_hdmi_rockchip_remove,
332 .name = "dwhdmi-rockchip",
333 .of_match_table = dw_hdmi_rockchip_dt_ids,