xref: /openbmc/linux/drivers/gpu/drm/hisilicon/hibmc/hibmc_drm_vdac.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25294967fSRongrong Zou /* Hisilicon Hibmc SoC drm driver
35294967fSRongrong Zou  *
45294967fSRongrong Zou  * Based on the bochs drm driver.
55294967fSRongrong Zou  *
65294967fSRongrong Zou  * Copyright (c) 2016 Huawei Limited.
75294967fSRongrong Zou  *
85294967fSRongrong Zou  * Author:
95294967fSRongrong Zou  *	Rongrong Zou <zourongrong@huawei.com>
105294967fSRongrong Zou  *	Rongrong Zou <zourongrong@gmail.com>
115294967fSRongrong Zou  *	Jianhua Li <lijianhua@huawei.com>
125294967fSRongrong Zou  */
135294967fSRongrong Zou 
14*d5c7533aSThomas Zimmermann #include <linux/io.h>
15*d5c7533aSThomas Zimmermann 
165294967fSRongrong Zou #include <drm/drm_atomic_helper.h>
17fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
1840b4db43SSam Ravnborg #include <drm/drm_print.h>
194992a7f7STian Tao #include <drm/drm_simple_kms_helper.h>
205294967fSRongrong Zou 
215294967fSRongrong Zou #include "hibmc_drm_drv.h"
225294967fSRongrong Zou #include "hibmc_drm_regs.h"
235294967fSRongrong Zou 
hibmc_connector_get_modes(struct drm_connector * connector)245294967fSRongrong Zou static int hibmc_connector_get_modes(struct drm_connector *connector)
255294967fSRongrong Zou {
2629b7216bSTian Tao 	int count;
27a0d078d0STian Tao 	void *edid;
28a0d078d0STian Tao 	struct hibmc_connector *hibmc_connector = to_hibmc_connector(connector);
29a0d078d0STian Tao 
30a0d078d0STian Tao 	edid = drm_get_edid(connector, &hibmc_connector->adapter);
31a0d078d0STian Tao 	if (edid) {
32a0d078d0STian Tao 		drm_connector_update_edid_property(connector, edid);
33a0d078d0STian Tao 		count = drm_add_edid_modes(connector, edid);
34a0d078d0STian Tao 		if (count)
35a0d078d0STian Tao 			goto out;
36a0d078d0STian Tao 	}
3729b7216bSTian Tao 
3829b7216bSTian Tao 	count = drm_add_modes_noedid(connector,
3929b7216bSTian Tao 				     connector->dev->mode_config.max_width,
4029b7216bSTian Tao 				     connector->dev->mode_config.max_height);
4129b7216bSTian Tao 	drm_set_preferred_mode(connector, 1024, 768);
4229b7216bSTian Tao 
43a0d078d0STian Tao out:
44a0d078d0STian Tao 	kfree(edid);
4529b7216bSTian Tao 	return count;
465294967fSRongrong Zou }
475294967fSRongrong Zou 
hibmc_connector_destroy(struct drm_connector * connector)48a0d078d0STian Tao static void hibmc_connector_destroy(struct drm_connector *connector)
49a0d078d0STian Tao {
50a0d078d0STian Tao 	struct hibmc_connector *hibmc_connector = to_hibmc_connector(connector);
51a0d078d0STian Tao 
52a0d078d0STian Tao 	i2c_del_adapter(&hibmc_connector->adapter);
53a0d078d0STian Tao 	drm_connector_cleanup(connector);
54a0d078d0STian Tao }
55a0d078d0STian Tao 
565294967fSRongrong Zou static const struct drm_connector_helper_funcs
575294967fSRongrong Zou 	hibmc_connector_helper_funcs = {
585294967fSRongrong Zou 	.get_modes = hibmc_connector_get_modes,
595294967fSRongrong Zou };
605294967fSRongrong Zou 
615294967fSRongrong Zou static const struct drm_connector_funcs hibmc_connector_funcs = {
625294967fSRongrong Zou 	.fill_modes = drm_helper_probe_single_connector_modes,
63a0d078d0STian Tao 	.destroy = hibmc_connector_destroy,
645294967fSRongrong Zou 	.reset = drm_atomic_helper_connector_reset,
655294967fSRongrong Zou 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
665294967fSRongrong Zou 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
675294967fSRongrong Zou };
685294967fSRongrong Zou 
hibmc_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adj_mode)695294967fSRongrong Zou static void hibmc_encoder_mode_set(struct drm_encoder *encoder,
705294967fSRongrong Zou 				   struct drm_display_mode *mode,
715294967fSRongrong Zou 				   struct drm_display_mode *adj_mode)
725294967fSRongrong Zou {
735294967fSRongrong Zou 	u32 reg;
745294967fSRongrong Zou 	struct drm_device *dev = encoder->dev;
75c7a39927STian Tao 	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
765294967fSRongrong Zou 
775294967fSRongrong Zou 	reg = readl(priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
785294967fSRongrong Zou 	reg |= HIBMC_DISPLAY_CONTROL_FPVDDEN(1);
795294967fSRongrong Zou 	reg |= HIBMC_DISPLAY_CONTROL_PANELDATE(1);
805294967fSRongrong Zou 	reg |= HIBMC_DISPLAY_CONTROL_FPEN(1);
815294967fSRongrong Zou 	reg |= HIBMC_DISPLAY_CONTROL_VBIASEN(1);
825294967fSRongrong Zou 	writel(reg, priv->mmio + HIBMC_DISPLAY_CONTROL_HISILE);
835294967fSRongrong Zou }
845294967fSRongrong Zou 
855294967fSRongrong Zou static const struct drm_encoder_helper_funcs hibmc_encoder_helper_funcs = {
865294967fSRongrong Zou 	.mode_set = hibmc_encoder_mode_set,
875294967fSRongrong Zou };
885294967fSRongrong Zou 
hibmc_vdac_init(struct hibmc_drm_private * priv)895294967fSRongrong Zou int hibmc_vdac_init(struct hibmc_drm_private *priv)
905294967fSRongrong Zou {
91770729f7STian Tao 	struct drm_device *dev = &priv->dev;
92a0d078d0STian Tao 	struct hibmc_connector *hibmc_connector = &priv->connector;
93be8c8403STian Tao 	struct drm_encoder *encoder = &priv->encoder;
949254cd1dSTian Tao 	struct drm_crtc *crtc = &priv->crtc;
95a0d078d0STian Tao 	struct drm_connector *connector = &hibmc_connector->base;
965294967fSRongrong Zou 	int ret;
975294967fSRongrong Zou 
98a0d078d0STian Tao 	ret = hibmc_ddc_create(dev, hibmc_connector);
99a0d078d0STian Tao 	if (ret) {
100a0d078d0STian Tao 		drm_err(dev, "failed to create ddc: %d\n", ret);
101a0d078d0STian Tao 		return ret;
102a0d078d0STian Tao 	}
103a0d078d0STian Tao 
1049254cd1dSTian Tao 	encoder->possible_crtcs = drm_crtc_mask(crtc);
1054992a7f7STian Tao 	ret = drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_DAC);
1065294967fSRongrong Zou 	if (ret) {
107ae34c15bSTian Tao 		drm_err(dev, "failed to init encoder: %d\n", ret);
1085294967fSRongrong Zou 		return ret;
1095294967fSRongrong Zou 	}
1105294967fSRongrong Zou 
1115294967fSRongrong Zou 	drm_encoder_helper_add(encoder, &hibmc_encoder_helper_funcs);
112be8c8403STian Tao 
113a0d078d0STian Tao 	ret = drm_connector_init_with_ddc(dev, connector,
114a0d078d0STian Tao 					  &hibmc_connector_funcs,
115a0d078d0STian Tao 					  DRM_MODE_CONNECTOR_VGA,
116a0d078d0STian Tao 					  &hibmc_connector->adapter);
117be8c8403STian Tao 	if (ret) {
118ae34c15bSTian Tao 		drm_err(dev, "failed to init connector: %d\n", ret);
119be8c8403STian Tao 		return ret;
120be8c8403STian Tao 	}
121a0d078d0STian Tao 
122be8c8403STian Tao 	drm_connector_helper_add(connector, &hibmc_connector_helper_funcs);
123be8c8403STian Tao 
124cde4c44dSDaniel Vetter 	drm_connector_attach_encoder(connector, encoder);
1255294967fSRongrong Zou 
1265294967fSRongrong Zou 	return 0;
1275294967fSRongrong Zou }
128