1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
7 #include <linux/mfd/syscon.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/phy/phy.h>
11 #include <linux/regmap.h>
13 #include <drm/bridge/dw_hdmi.h>
14 #include <drm/drm_edid.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_probe_helper.h>
18 #include "rockchip_drm_drv.h"
19 #include "rockchip_drm_vop.h"
21 #define RK3228_GRF_SOC_CON2 0x0408
22 #define RK3228_HDMI_SDAIN_MSK BIT(14)
23 #define RK3228_HDMI_SCLIN_MSK BIT(13)
24 #define RK3228_GRF_SOC_CON6 0x0418
25 #define RK3228_HDMI_HPD_VSEL BIT(6)
26 #define RK3228_HDMI_SDA_VSEL BIT(5)
27 #define RK3228_HDMI_SCL_VSEL BIT(4)
29 #define RK3288_GRF_SOC_CON6 0x025C
30 #define RK3288_HDMI_LCDC_SEL BIT(4)
31 #define RK3328_GRF_SOC_CON2 0x0408
33 #define RK3328_HDMI_SDAIN_MSK BIT(11)
34 #define RK3328_HDMI_SCLIN_MSK BIT(10)
35 #define RK3328_HDMI_HPD_IOE BIT(2)
36 #define RK3328_GRF_SOC_CON3 0x040c
37 /* need to be unset if hdmi or i2c should control voltage */
38 #define RK3328_HDMI_SDA5V_GRF BIT(15)
39 #define RK3328_HDMI_SCL5V_GRF BIT(14)
40 #define RK3328_HDMI_HPD5V_GRF BIT(13)
41 #define RK3328_HDMI_CEC5V_GRF BIT(12)
42 #define RK3328_GRF_SOC_CON4 0x0410
43 #define RK3328_HDMI_HPD_SARADC BIT(13)
44 #define RK3328_HDMI_CEC_5V BIT(11)
45 #define RK3328_HDMI_SDA_5V BIT(10)
46 #define RK3328_HDMI_SCL_5V BIT(9)
47 #define RK3328_HDMI_HPD_5V BIT(8)
49 #define RK3399_GRF_SOC_CON20 0x6250
50 #define RK3399_HDMI_LCDC_SEL BIT(6)
52 #define HIWORD_UPDATE(val, mask) (val | (mask) << 16)
55 * struct rockchip_hdmi_chip_data - splite the grf setting of kind of chips
56 * @lcdsel_grf_reg: grf register offset of lcdc select
57 * @lcdsel_big: reg value of selecting vop big for HDMI
58 * @lcdsel_lit: reg value of selecting vop little for HDMI
60 struct rockchip_hdmi_chip_data
{
66 struct rockchip_hdmi
{
68 struct regmap
*regmap
;
69 struct drm_encoder encoder
;
70 const struct rockchip_hdmi_chip_data
*chip_data
;
77 #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
79 static const struct dw_hdmi_mpll_config rockchip_mpll_cfg
[] = {
159 static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr
[] = {
160 /* pixelclk bpp8 bpp10 bpp12 */
162 40000000, { 0x0018, 0x0018, 0x0018 },
164 65000000, { 0x0028, 0x0028, 0x0028 },
166 66000000, { 0x0038, 0x0038, 0x0038 },
168 74250000, { 0x0028, 0x0038, 0x0038 },
170 83500000, { 0x0028, 0x0038, 0x0038 },
172 146250000, { 0x0038, 0x0038, 0x0038 },
174 148500000, { 0x0000, 0x0038, 0x0038 },
176 ~0UL, { 0x0000, 0x0000, 0x0000},
180 static const struct dw_hdmi_phy_config rockchip_phy_config
[] = {
181 /*pixelclk symbol term vlev*/
182 { 74250000, 0x8009, 0x0004, 0x0272},
183 { 148500000, 0x802b, 0x0004, 0x028d},
184 { 297000000, 0x8039, 0x0005, 0x028d},
185 { ~0UL, 0x0000, 0x0000, 0x0000}
188 static int rockchip_hdmi_parse_dt(struct rockchip_hdmi
*hdmi
)
190 struct device_node
*np
= hdmi
->dev
->of_node
;
192 hdmi
->regmap
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
193 if (IS_ERR(hdmi
->regmap
)) {
194 DRM_DEV_ERROR(hdmi
->dev
, "Unable to get rockchip,grf\n");
195 return PTR_ERR(hdmi
->regmap
);
198 hdmi
->vpll_clk
= devm_clk_get(hdmi
->dev
, "vpll");
199 if (PTR_ERR(hdmi
->vpll_clk
) == -ENOENT
) {
200 hdmi
->vpll_clk
= NULL
;
201 } else if (PTR_ERR(hdmi
->vpll_clk
) == -EPROBE_DEFER
) {
202 return -EPROBE_DEFER
;
203 } else if (IS_ERR(hdmi
->vpll_clk
)) {
204 DRM_DEV_ERROR(hdmi
->dev
, "failed to get grf clock\n");
205 return PTR_ERR(hdmi
->vpll_clk
);
208 hdmi
->grf_clk
= devm_clk_get(hdmi
->dev
, "grf");
209 if (PTR_ERR(hdmi
->grf_clk
) == -ENOENT
) {
210 hdmi
->grf_clk
= NULL
;
211 } else if (PTR_ERR(hdmi
->grf_clk
) == -EPROBE_DEFER
) {
212 return -EPROBE_DEFER
;
213 } else if (IS_ERR(hdmi
->grf_clk
)) {
214 DRM_DEV_ERROR(hdmi
->dev
, "failed to get grf clock\n");
215 return PTR_ERR(hdmi
->grf_clk
);
221 static enum drm_mode_status
222 dw_hdmi_rockchip_mode_valid(struct drm_connector
*connector
,
223 const struct drm_display_mode
*mode
)
225 const struct dw_hdmi_mpll_config
*mpll_cfg
= rockchip_mpll_cfg
;
226 int pclk
= mode
->clock
* 1000;
230 for (i
= 0; mpll_cfg
[i
].mpixelclock
!= (~0UL); i
++) {
231 if (pclk
== mpll_cfg
[i
].mpixelclock
) {
237 return (valid
) ? MODE_OK
: MODE_BAD
;
240 static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs
= {
241 .destroy
= drm_encoder_cleanup
,
244 static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder
*encoder
)
249 dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder
*encoder
,
250 const struct drm_display_mode
*mode
,
251 struct drm_display_mode
*adj_mode
)
256 static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder
*encoder
,
257 struct drm_display_mode
*mode
,
258 struct drm_display_mode
*adj_mode
)
260 struct rockchip_hdmi
*hdmi
= to_rockchip_hdmi(encoder
);
262 clk_set_rate(hdmi
->vpll_clk
, adj_mode
->clock
* 1000);
265 static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder
*encoder
)
267 struct rockchip_hdmi
*hdmi
= to_rockchip_hdmi(encoder
);
271 if (hdmi
->chip_data
->lcdsel_grf_reg
< 0)
274 ret
= drm_of_encoder_active_endpoint_id(hdmi
->dev
->of_node
, encoder
);
276 val
= hdmi
->chip_data
->lcdsel_lit
;
278 val
= hdmi
->chip_data
->lcdsel_big
;
280 ret
= clk_prepare_enable(hdmi
->grf_clk
);
282 DRM_DEV_ERROR(hdmi
->dev
, "failed to enable grfclk %d\n", ret
);
286 ret
= regmap_write(hdmi
->regmap
, hdmi
->chip_data
->lcdsel_grf_reg
, val
);
288 DRM_DEV_ERROR(hdmi
->dev
, "Could not write to GRF: %d\n", ret
);
290 clk_disable_unprepare(hdmi
->grf_clk
);
291 DRM_DEV_DEBUG(hdmi
->dev
, "vop %s output to hdmi\n",
292 ret
? "LIT" : "BIG");
296 dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder
*encoder
,
297 struct drm_crtc_state
*crtc_state
,
298 struct drm_connector_state
*conn_state
)
300 struct rockchip_crtc_state
*s
= to_rockchip_crtc_state(crtc_state
);
302 s
->output_mode
= ROCKCHIP_OUT_MODE_AAAA
;
303 s
->output_type
= DRM_MODE_CONNECTOR_HDMIA
;
308 static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs
= {
309 .mode_fixup
= dw_hdmi_rockchip_encoder_mode_fixup
,
310 .mode_set
= dw_hdmi_rockchip_encoder_mode_set
,
311 .enable
= dw_hdmi_rockchip_encoder_enable
,
312 .disable
= dw_hdmi_rockchip_encoder_disable
,
313 .atomic_check
= dw_hdmi_rockchip_encoder_atomic_check
,
316 static int dw_hdmi_rockchip_genphy_init(struct dw_hdmi
*dw_hdmi
, void *data
,
317 struct drm_display_mode
*mode
)
319 struct rockchip_hdmi
*hdmi
= (struct rockchip_hdmi
*)data
;
321 return phy_power_on(hdmi
->phy
);
324 static void dw_hdmi_rockchip_genphy_disable(struct dw_hdmi
*dw_hdmi
, void *data
)
326 struct rockchip_hdmi
*hdmi
= (struct rockchip_hdmi
*)data
;
328 phy_power_off(hdmi
->phy
);
331 static void dw_hdmi_rk3228_setup_hpd(struct dw_hdmi
*dw_hdmi
, void *data
)
333 struct rockchip_hdmi
*hdmi
= (struct rockchip_hdmi
*)data
;
335 dw_hdmi_phy_setup_hpd(dw_hdmi
, data
);
337 regmap_write(hdmi
->regmap
,
339 HIWORD_UPDATE(RK3228_HDMI_HPD_VSEL
| RK3228_HDMI_SDA_VSEL
|
340 RK3228_HDMI_SCL_VSEL
,
341 RK3228_HDMI_HPD_VSEL
| RK3228_HDMI_SDA_VSEL
|
342 RK3228_HDMI_SCL_VSEL
));
344 regmap_write(hdmi
->regmap
,
346 HIWORD_UPDATE(RK3228_HDMI_SDAIN_MSK
| RK3228_HDMI_SCLIN_MSK
,
347 RK3228_HDMI_SDAIN_MSK
| RK3228_HDMI_SCLIN_MSK
));
350 static enum drm_connector_status
351 dw_hdmi_rk3328_read_hpd(struct dw_hdmi
*dw_hdmi
, void *data
)
353 struct rockchip_hdmi
*hdmi
= (struct rockchip_hdmi
*)data
;
354 enum drm_connector_status status
;
356 status
= dw_hdmi_phy_read_hpd(dw_hdmi
, data
);
358 if (status
== connector_status_connected
)
359 regmap_write(hdmi
->regmap
,
361 HIWORD_UPDATE(RK3328_HDMI_SDA_5V
| RK3328_HDMI_SCL_5V
,
362 RK3328_HDMI_SDA_5V
| RK3328_HDMI_SCL_5V
));
364 regmap_write(hdmi
->regmap
,
366 HIWORD_UPDATE(0, RK3328_HDMI_SDA_5V
|
367 RK3328_HDMI_SCL_5V
));
371 static void dw_hdmi_rk3328_setup_hpd(struct dw_hdmi
*dw_hdmi
, void *data
)
373 struct rockchip_hdmi
*hdmi
= (struct rockchip_hdmi
*)data
;
375 dw_hdmi_phy_setup_hpd(dw_hdmi
, data
);
377 /* Enable and map pins to 3V grf-controlled io-voltage */
378 regmap_write(hdmi
->regmap
,
380 HIWORD_UPDATE(0, RK3328_HDMI_HPD_SARADC
| RK3328_HDMI_CEC_5V
|
381 RK3328_HDMI_SDA_5V
| RK3328_HDMI_SCL_5V
|
382 RK3328_HDMI_HPD_5V
));
383 regmap_write(hdmi
->regmap
,
385 HIWORD_UPDATE(0, RK3328_HDMI_SDA5V_GRF
| RK3328_HDMI_SCL5V_GRF
|
386 RK3328_HDMI_HPD5V_GRF
|
387 RK3328_HDMI_CEC5V_GRF
));
388 regmap_write(hdmi
->regmap
,
390 HIWORD_UPDATE(RK3328_HDMI_SDAIN_MSK
| RK3328_HDMI_SCLIN_MSK
,
391 RK3328_HDMI_SDAIN_MSK
| RK3328_HDMI_SCLIN_MSK
|
392 RK3328_HDMI_HPD_IOE
));
395 static const struct dw_hdmi_phy_ops rk3228_hdmi_phy_ops
= {
396 .init
= dw_hdmi_rockchip_genphy_init
,
397 .disable
= dw_hdmi_rockchip_genphy_disable
,
398 .read_hpd
= dw_hdmi_phy_read_hpd
,
399 .update_hpd
= dw_hdmi_phy_update_hpd
,
400 .setup_hpd
= dw_hdmi_rk3228_setup_hpd
,
403 static struct rockchip_hdmi_chip_data rk3228_chip_data
= {
404 .lcdsel_grf_reg
= -1,
407 static const struct dw_hdmi_plat_data rk3228_hdmi_drv_data
= {
408 .mode_valid
= dw_hdmi_rockchip_mode_valid
,
409 .mpll_cfg
= rockchip_mpll_cfg
,
410 .cur_ctr
= rockchip_cur_ctr
,
411 .phy_config
= rockchip_phy_config
,
412 .phy_data
= &rk3228_chip_data
,
413 .phy_ops
= &rk3228_hdmi_phy_ops
,
414 .phy_name
= "inno_dw_hdmi_phy2",
415 .phy_force_vendor
= true,
418 static struct rockchip_hdmi_chip_data rk3288_chip_data
= {
419 .lcdsel_grf_reg
= RK3288_GRF_SOC_CON6
,
420 .lcdsel_big
= HIWORD_UPDATE(0, RK3288_HDMI_LCDC_SEL
),
421 .lcdsel_lit
= HIWORD_UPDATE(RK3288_HDMI_LCDC_SEL
, RK3288_HDMI_LCDC_SEL
),
424 static const struct dw_hdmi_plat_data rk3288_hdmi_drv_data
= {
425 .mode_valid
= dw_hdmi_rockchip_mode_valid
,
426 .mpll_cfg
= rockchip_mpll_cfg
,
427 .cur_ctr
= rockchip_cur_ctr
,
428 .phy_config
= rockchip_phy_config
,
429 .phy_data
= &rk3288_chip_data
,
432 static const struct dw_hdmi_phy_ops rk3328_hdmi_phy_ops
= {
433 .init
= dw_hdmi_rockchip_genphy_init
,
434 .disable
= dw_hdmi_rockchip_genphy_disable
,
435 .read_hpd
= dw_hdmi_rk3328_read_hpd
,
436 .update_hpd
= dw_hdmi_phy_update_hpd
,
437 .setup_hpd
= dw_hdmi_rk3328_setup_hpd
,
440 static struct rockchip_hdmi_chip_data rk3328_chip_data
= {
441 .lcdsel_grf_reg
= -1,
444 static const struct dw_hdmi_plat_data rk3328_hdmi_drv_data
= {
445 .mode_valid
= dw_hdmi_rockchip_mode_valid
,
446 .mpll_cfg
= rockchip_mpll_cfg
,
447 .cur_ctr
= rockchip_cur_ctr
,
448 .phy_config
= rockchip_phy_config
,
449 .phy_data
= &rk3328_chip_data
,
450 .phy_ops
= &rk3328_hdmi_phy_ops
,
451 .phy_name
= "inno_dw_hdmi_phy2",
452 .phy_force_vendor
= true,
453 .use_drm_infoframe
= true,
456 static struct rockchip_hdmi_chip_data rk3399_chip_data
= {
457 .lcdsel_grf_reg
= RK3399_GRF_SOC_CON20
,
458 .lcdsel_big
= HIWORD_UPDATE(0, RK3399_HDMI_LCDC_SEL
),
459 .lcdsel_lit
= HIWORD_UPDATE(RK3399_HDMI_LCDC_SEL
, RK3399_HDMI_LCDC_SEL
),
462 static const struct dw_hdmi_plat_data rk3399_hdmi_drv_data
= {
463 .mode_valid
= dw_hdmi_rockchip_mode_valid
,
464 .mpll_cfg
= rockchip_mpll_cfg
,
465 .cur_ctr
= rockchip_cur_ctr
,
466 .phy_config
= rockchip_phy_config
,
467 .phy_data
= &rk3399_chip_data
,
468 .use_drm_infoframe
= true,
471 static const struct of_device_id dw_hdmi_rockchip_dt_ids
[] = {
472 { .compatible
= "rockchip,rk3228-dw-hdmi",
473 .data
= &rk3228_hdmi_drv_data
475 { .compatible
= "rockchip,rk3288-dw-hdmi",
476 .data
= &rk3288_hdmi_drv_data
478 { .compatible
= "rockchip,rk3328-dw-hdmi",
479 .data
= &rk3328_hdmi_drv_data
481 { .compatible
= "rockchip,rk3399-dw-hdmi",
482 .data
= &rk3399_hdmi_drv_data
486 MODULE_DEVICE_TABLE(of
, dw_hdmi_rockchip_dt_ids
);
488 static int dw_hdmi_rockchip_bind(struct device
*dev
, struct device
*master
,
491 struct platform_device
*pdev
= to_platform_device(dev
);
492 struct dw_hdmi_plat_data
*plat_data
;
493 const struct of_device_id
*match
;
494 struct drm_device
*drm
= data
;
495 struct drm_encoder
*encoder
;
496 struct rockchip_hdmi
*hdmi
;
499 if (!pdev
->dev
.of_node
)
502 hdmi
= devm_kzalloc(&pdev
->dev
, sizeof(*hdmi
), GFP_KERNEL
);
506 match
= of_match_node(dw_hdmi_rockchip_dt_ids
, pdev
->dev
.of_node
);
507 plat_data
= devm_kmemdup(&pdev
->dev
, match
->data
,
508 sizeof(*plat_data
), GFP_KERNEL
);
512 hdmi
->dev
= &pdev
->dev
;
513 hdmi
->chip_data
= plat_data
->phy_data
;
514 plat_data
->phy_data
= hdmi
;
515 encoder
= &hdmi
->encoder
;
517 encoder
->possible_crtcs
= drm_of_find_possible_crtcs(drm
, dev
->of_node
);
519 * If we failed to find the CRTC(s) which this encoder is
520 * supposed to be connected to, it's because the CRTC has
521 * not been registered yet. Defer probing, and hope that
522 * the required CRTC is added later.
524 if (encoder
->possible_crtcs
== 0)
525 return -EPROBE_DEFER
;
527 ret
= rockchip_hdmi_parse_dt(hdmi
);
529 DRM_DEV_ERROR(hdmi
->dev
, "Unable to parse OF data\n");
533 ret
= clk_prepare_enable(hdmi
->vpll_clk
);
535 DRM_DEV_ERROR(hdmi
->dev
, "Failed to enable HDMI vpll: %d\n",
540 hdmi
->phy
= devm_phy_optional_get(dev
, "hdmi");
541 if (IS_ERR(hdmi
->phy
)) {
542 ret
= PTR_ERR(hdmi
->phy
);
543 if (ret
!= -EPROBE_DEFER
)
544 DRM_DEV_ERROR(hdmi
->dev
, "failed to get phy\n");
548 drm_encoder_helper_add(encoder
, &dw_hdmi_rockchip_encoder_helper_funcs
);
549 drm_encoder_init(drm
, encoder
, &dw_hdmi_rockchip_encoder_funcs
,
550 DRM_MODE_ENCODER_TMDS
, NULL
);
552 platform_set_drvdata(pdev
, hdmi
);
554 hdmi
->hdmi
= dw_hdmi_bind(pdev
, encoder
, plat_data
);
557 * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
558 * which would have called the encoder cleanup. Do it manually.
560 if (IS_ERR(hdmi
->hdmi
)) {
561 ret
= PTR_ERR(hdmi
->hdmi
);
562 drm_encoder_cleanup(encoder
);
563 clk_disable_unprepare(hdmi
->vpll_clk
);
569 static void dw_hdmi_rockchip_unbind(struct device
*dev
, struct device
*master
,
572 struct rockchip_hdmi
*hdmi
= dev_get_drvdata(dev
);
574 dw_hdmi_unbind(hdmi
->hdmi
);
575 clk_disable_unprepare(hdmi
->vpll_clk
);
578 static const struct component_ops dw_hdmi_rockchip_ops
= {
579 .bind
= dw_hdmi_rockchip_bind
,
580 .unbind
= dw_hdmi_rockchip_unbind
,
583 static int dw_hdmi_rockchip_probe(struct platform_device
*pdev
)
585 return component_add(&pdev
->dev
, &dw_hdmi_rockchip_ops
);
588 static int dw_hdmi_rockchip_remove(struct platform_device
*pdev
)
590 component_del(&pdev
->dev
, &dw_hdmi_rockchip_ops
);
595 static int __maybe_unused
dw_hdmi_rockchip_resume(struct device
*dev
)
597 struct rockchip_hdmi
*hdmi
= dev_get_drvdata(dev
);
599 dw_hdmi_resume(hdmi
->hdmi
);
604 static const struct dev_pm_ops dw_hdmi_rockchip_pm
= {
605 SET_SYSTEM_SLEEP_PM_OPS(NULL
, dw_hdmi_rockchip_resume
)
608 struct platform_driver dw_hdmi_rockchip_pltfm_driver
= {
609 .probe
= dw_hdmi_rockchip_probe
,
610 .remove
= dw_hdmi_rockchip_remove
,
612 .name
= "dwhdmi-rockchip",
613 .pm
= &dw_hdmi_rockchip_pm
,
614 .of_match_table
= dw_hdmi_rockchip_dt_ids
,