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