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