xref: /linux/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_vdac.c (revision 55d0969c451159cff86949b38c39171cab962069)
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 <linux/io.h>
15 
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_edid.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_simple_kms_helper.h>
21 
22 #include "hibmc_drm_drv.h"
23 #include "hibmc_drm_regs.h"
24 
25 static int hibmc_connector_get_modes(struct drm_connector *connector)
26 {
27 	struct hibmc_connector *hibmc_connector = to_hibmc_connector(connector);
28 	const struct drm_edid *drm_edid;
29 	int count;
30 
31 	drm_edid = drm_edid_read_ddc(connector, &hibmc_connector->adapter);
32 
33 	drm_edid_connector_update(connector, drm_edid);
34 
35 	if (drm_edid) {
36 		count = drm_edid_connector_add_modes(connector);
37 		if (count)
38 			goto out;
39 	}
40 
41 	count = drm_add_modes_noedid(connector,
42 				     connector->dev->mode_config.max_width,
43 				     connector->dev->mode_config.max_height);
44 	drm_set_preferred_mode(connector, 1024, 768);
45 
46 out:
47 	drm_edid_free(drm_edid);
48 
49 	return count;
50 }
51 
52 static void hibmc_connector_destroy(struct drm_connector *connector)
53 {
54 	struct hibmc_connector *hibmc_connector = to_hibmc_connector(connector);
55 
56 	i2c_del_adapter(&hibmc_connector->adapter);
57 	drm_connector_cleanup(connector);
58 }
59 
60 static const struct drm_connector_helper_funcs
61 	hibmc_connector_helper_funcs = {
62 	.get_modes = hibmc_connector_get_modes,
63 };
64 
65 static const struct drm_connector_funcs hibmc_connector_funcs = {
66 	.fill_modes = drm_helper_probe_single_connector_modes,
67 	.destroy = hibmc_connector_destroy,
68 	.reset = drm_atomic_helper_connector_reset,
69 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
70 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
71 };
72 
73 static void hibmc_encoder_mode_set(struct drm_encoder *encoder,
74 				   struct drm_display_mode *mode,
75 				   struct drm_display_mode *adj_mode)
76 {
77 	u32 reg;
78 	struct drm_device *dev = encoder->dev;
79 	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
80 
81 	reg = readl(priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
82 	reg |= HIBMC_DISPLAY_CONTROL_FPVDDEN(1);
83 	reg |= HIBMC_DISPLAY_CONTROL_PANELDATE(1);
84 	reg |= HIBMC_DISPLAY_CONTROL_FPEN(1);
85 	reg |= HIBMC_DISPLAY_CONTROL_VBIASEN(1);
86 	writel(reg, priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
87 }
88 
89 static const struct drm_encoder_helper_funcs hibmc_encoder_helper_funcs = {
90 	.mode_set = hibmc_encoder_mode_set,
91 };
92 
93 int hibmc_vdac_init(struct hibmc_drm_private *priv)
94 {
95 	struct drm_device *dev = &priv->dev;
96 	struct hibmc_connector *hibmc_connector = &priv->connector;
97 	struct drm_encoder *encoder = &priv->encoder;
98 	struct drm_crtc *crtc = &priv->crtc;
99 	struct drm_connector *connector = &hibmc_connector->base;
100 	int ret;
101 
102 	ret = hibmc_ddc_create(dev, hibmc_connector);
103 	if (ret) {
104 		drm_err(dev, "failed to create ddc: %d\n", ret);
105 		return ret;
106 	}
107 
108 	encoder->possible_crtcs = drm_crtc_mask(crtc);
109 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
110 	if (ret) {
111 		drm_err(dev, "failed to init encoder: %d\n", ret);
112 		return ret;
113 	}
114 
115 	drm_encoder_helper_add(encoder, &hibmc_encoder_helper_funcs);
116 
117 	ret = drm_connector_init_with_ddc(dev, connector,
118 					  &hibmc_connector_funcs,
119 					  DRM_MODE_CONNECTOR_VGA,
120 					  &hibmc_connector->adapter);
121 	if (ret) {
122 		drm_err(dev, "failed to init connector: %d\n", ret);
123 		return ret;
124 	}
125 
126 	drm_connector_helper_add(connector, &hibmc_connector_helper_funcs);
127 
128 	drm_connector_attach_encoder(connector, encoder);
129 
130 	return 0;
131 }
132