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
,
240 .dev_type
= RK3288_HDMI
,
243 static const struct of_device_id dw_hdmi_rockchip_dt_ids
[] = {
244 { .compatible
= "rockchip,rk3288-dw-hdmi",
245 .data
= &rockchip_hdmi_drv_data
249 MODULE_DEVICE_TABLE(of
, dw_hdmi_rockchip_dt_ids
);
251 static int dw_hdmi_rockchip_bind(struct device
*dev
, struct device
*master
,
254 struct platform_device
*pdev
= to_platform_device(dev
);
255 const struct dw_hdmi_plat_data
*plat_data
;
256 const struct of_device_id
*match
;
257 struct drm_device
*drm
= data
;
258 struct drm_encoder
*encoder
;
259 struct rockchip_hdmi
*hdmi
;
262 if (!pdev
->dev
.of_node
)
265 hdmi
= devm_kzalloc(&pdev
->dev
, sizeof(*hdmi
), GFP_KERNEL
);
269 match
= of_match_node(dw_hdmi_rockchip_dt_ids
, pdev
->dev
.of_node
);
270 plat_data
= match
->data
;
271 hdmi
->dev
= &pdev
->dev
;
272 encoder
= &hdmi
->encoder
;
274 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm
, dev
->of_node
);
276 * If we failed to find the CRTC(s) which this encoder is
277 * supposed to be connected to, it's because the CRTC has
278 * not been registered yet. Defer probing, and hope that
279 * the required CRTC is added later.
281 if (encoder
->possible_crtcs
== 0)
282 return -EPROBE_DEFER
;
284 ret
= rockchip_hdmi_parse_dt(hdmi
);
286 dev_err(hdmi
->dev
, "Unable to parse OF data\n");
290 drm_encoder_helper_add(encoder
, &dw_hdmi_rockchip_encoder_helper_funcs
);
291 drm_encoder_init(drm
, encoder
, &dw_hdmi_rockchip_encoder_funcs
,
292 DRM_MODE_ENCODER_TMDS
, NULL
);
294 ret
= dw_hdmi_bind(pdev
, encoder
, plat_data
);
297 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
298 * which would have called the encoder cleanup. Do it manually.
301 drm_encoder_cleanup(encoder
);
306 static void dw_hdmi_rockchip_unbind(struct device
*dev
, struct device
*master
,
309 return dw_hdmi_unbind(dev
);
312 static const struct component_ops dw_hdmi_rockchip_ops
= {
313 .bind
= dw_hdmi_rockchip_bind
,
314 .unbind
= dw_hdmi_rockchip_unbind
,
317 static int dw_hdmi_rockchip_probe(struct platform_device
*pdev
)
319 return component_add(&pdev
->dev
, &dw_hdmi_rockchip_ops
);
322 static int dw_hdmi_rockchip_remove(struct platform_device
*pdev
)
324 component_del(&pdev
->dev
, &dw_hdmi_rockchip_ops
);
329 static struct platform_driver dw_hdmi_rockchip_pltfm_driver
= {
330 .probe
= dw_hdmi_rockchip_probe
,
331 .remove
= dw_hdmi_rockchip_remove
,
333 .name
= "dwhdmi-rockchip",
334 .of_match_table
= dw_hdmi_rockchip_dt_ids
,
338 module_platform_driver(dw_hdmi_rockchip_pltfm_driver
);
340 MODULE_AUTHOR("Andy Yan <andy.yan@rock-chips.com>");
341 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
342 MODULE_DESCRIPTION("Rockchip Specific DW-HDMI Driver Extension");
343 MODULE_LICENSE("GPL");
344 MODULE_ALIAS("platform:dwhdmi-rockchip");