1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Hisilicon Hibmc SoC drm driver
3  *
4  * Based on the bochs drm driver.
5  *
6  * Copyright (c) 2016 Huawei Limited.
7  *
8  * Author:
9  *	Rongrong Zou <zourongrong@huawei.com>
10  *	Rongrong Zou <zourongrong@gmail.com>
11  *	Jianhua Li <lijianhua@huawei.com>
12  */
13 
14 #include <drm/drm_gem_vram_helper.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_print.h>
19 
20 #include "hibmc_drm_drv.h"
21 #include "hibmc_drm_regs.h"
22 
23 static int hibmc_connector_get_modes(struct drm_connector *connector)
24 {
25 	int count;
26 
27 	count = drm_add_modes_noedid(connector,
28 				     connector->dev->mode_config.max_width,
29 				     connector->dev->mode_config.max_height);
30 	drm_set_preferred_mode(connector, 1024, 768);
31 
32 	return count;
33 }
34 
35 static enum drm_mode_status hibmc_connector_mode_valid(struct drm_connector *connector,
36 				      struct drm_display_mode *mode)
37 {
38 	return MODE_OK;
39 }
40 
41 static const struct drm_connector_helper_funcs
42 	hibmc_connector_helper_funcs = {
43 	.get_modes = hibmc_connector_get_modes,
44 	.mode_valid = hibmc_connector_mode_valid,
45 };
46 
47 static const struct drm_connector_funcs hibmc_connector_funcs = {
48 	.fill_modes = drm_helper_probe_single_connector_modes,
49 	.destroy = drm_connector_cleanup,
50 	.reset = drm_atomic_helper_connector_reset,
51 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
52 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
53 };
54 
55 static void hibmc_encoder_mode_set(struct drm_encoder *encoder,
56 				   struct drm_display_mode *mode,
57 				   struct drm_display_mode *adj_mode)
58 {
59 	u32 reg;
60 	struct drm_device *dev = encoder->dev;
61 	struct hibmc_drm_private *priv = dev->dev_private;
62 
63 	reg = readl(priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
64 	reg |= HIBMC_DISPLAY_CONTROL_FPVDDEN(1);
65 	reg |= HIBMC_DISPLAY_CONTROL_PANELDATE(1);
66 	reg |= HIBMC_DISPLAY_CONTROL_FPEN(1);
67 	reg |= HIBMC_DISPLAY_CONTROL_VBIASEN(1);
68 	writel(reg, priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
69 }
70 
71 static const struct drm_encoder_helper_funcs hibmc_encoder_helper_funcs = {
72 	.mode_set = hibmc_encoder_mode_set,
73 };
74 
75 static const struct drm_encoder_funcs hibmc_encoder_funcs = {
76 	.destroy = drm_encoder_cleanup,
77 };
78 
79 int hibmc_vdac_init(struct hibmc_drm_private *priv)
80 {
81 	struct drm_device *dev = priv->dev;
82 	struct drm_encoder *encoder = &priv->encoder;
83 	struct drm_connector *connector = &priv->connector;
84 	int ret;
85 
86 	encoder->possible_crtcs = 0x1;
87 	ret = drm_encoder_init(dev, encoder, &hibmc_encoder_funcs,
88 			       DRM_MODE_ENCODER_DAC, NULL);
89 	if (ret) {
90 		DRM_ERROR("failed to init encoder: %d\n", ret);
91 		return ret;
92 	}
93 
94 	drm_encoder_helper_add(encoder, &hibmc_encoder_helper_funcs);
95 
96 	ret = drm_connector_init(dev, connector, &hibmc_connector_funcs,
97 				 DRM_MODE_CONNECTOR_VGA);
98 	if (ret) {
99 		DRM_ERROR("failed to init connector: %d\n", ret);
100 		return ret;
101 	}
102 	drm_connector_helper_add(connector, &hibmc_connector_helper_funcs);
103 
104 	drm_connector_attach_encoder(connector, encoder);
105 
106 	return 0;
107 }
108