1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2014 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  * Author: Vinay Simha <vinaysimha@inforcecomputing.com>
6  */
7 
8 #include "mdp4_kms.h"
9 
10 struct mdp4_lvds_connector {
11 	struct drm_connector base;
12 	struct drm_encoder *encoder;
13 	struct device_node *panel_node;
14 	struct drm_panel *panel;
15 };
16 #define to_mdp4_lvds_connector(x) container_of(x, struct mdp4_lvds_connector, base)
17 
mdp4_lvds_connector_detect(struct drm_connector * connector,bool force)18 static enum drm_connector_status mdp4_lvds_connector_detect(
19 		struct drm_connector *connector, bool force)
20 {
21 	struct mdp4_lvds_connector *mdp4_lvds_connector =
22 			to_mdp4_lvds_connector(connector);
23 
24 	if (!mdp4_lvds_connector->panel) {
25 		mdp4_lvds_connector->panel =
26 			of_drm_find_panel(mdp4_lvds_connector->panel_node);
27 		if (IS_ERR(mdp4_lvds_connector->panel))
28 			mdp4_lvds_connector->panel = NULL;
29 	}
30 
31 	return mdp4_lvds_connector->panel ?
32 			connector_status_connected :
33 			connector_status_disconnected;
34 }
35 
mdp4_lvds_connector_destroy(struct drm_connector * connector)36 static void mdp4_lvds_connector_destroy(struct drm_connector *connector)
37 {
38 	struct mdp4_lvds_connector *mdp4_lvds_connector =
39 			to_mdp4_lvds_connector(connector);
40 
41 	drm_connector_cleanup(connector);
42 
43 	kfree(mdp4_lvds_connector);
44 }
45 
mdp4_lvds_connector_get_modes(struct drm_connector * connector)46 static int mdp4_lvds_connector_get_modes(struct drm_connector *connector)
47 {
48 	struct mdp4_lvds_connector *mdp4_lvds_connector =
49 			to_mdp4_lvds_connector(connector);
50 	struct drm_panel *panel = mdp4_lvds_connector->panel;
51 	int ret = 0;
52 
53 	if (panel)
54 		ret = drm_panel_get_modes(panel, connector);
55 
56 	return ret;
57 }
58 
59 static enum drm_mode_status
mdp4_lvds_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)60 mdp4_lvds_connector_mode_valid(struct drm_connector *connector,
61 			       struct drm_display_mode *mode)
62 {
63 	struct mdp4_lvds_connector *mdp4_lvds_connector =
64 			to_mdp4_lvds_connector(connector);
65 	struct drm_encoder *encoder = mdp4_lvds_connector->encoder;
66 	long actual, requested;
67 
68 	requested = 1000 * mode->clock;
69 	actual = mdp4_lcdc_round_pixclk(encoder, requested);
70 
71 	DBG("requested=%ld, actual=%ld", requested, actual);
72 
73 	if (actual != requested)
74 		return MODE_CLOCK_RANGE;
75 
76 	return MODE_OK;
77 }
78 
79 static const struct drm_connector_funcs mdp4_lvds_connector_funcs = {
80 	.detect = mdp4_lvds_connector_detect,
81 	.fill_modes = drm_helper_probe_single_connector_modes,
82 	.destroy = mdp4_lvds_connector_destroy,
83 	.reset = drm_atomic_helper_connector_reset,
84 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
85 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
86 };
87 
88 static const struct drm_connector_helper_funcs mdp4_lvds_connector_helper_funcs = {
89 	.get_modes = mdp4_lvds_connector_get_modes,
90 	.mode_valid = mdp4_lvds_connector_mode_valid,
91 };
92 
93 /* initialize connector */
mdp4_lvds_connector_init(struct drm_device * dev,struct device_node * panel_node,struct drm_encoder * encoder)94 struct drm_connector *mdp4_lvds_connector_init(struct drm_device *dev,
95 		struct device_node *panel_node, struct drm_encoder *encoder)
96 {
97 	struct drm_connector *connector = NULL;
98 	struct mdp4_lvds_connector *mdp4_lvds_connector;
99 
100 	mdp4_lvds_connector = kzalloc(sizeof(*mdp4_lvds_connector), GFP_KERNEL);
101 	if (!mdp4_lvds_connector)
102 		return ERR_PTR(-ENOMEM);
103 
104 	mdp4_lvds_connector->encoder = encoder;
105 	mdp4_lvds_connector->panel_node = panel_node;
106 
107 	connector = &mdp4_lvds_connector->base;
108 
109 	drm_connector_init(dev, connector, &mdp4_lvds_connector_funcs,
110 			DRM_MODE_CONNECTOR_LVDS);
111 	drm_connector_helper_add(connector, &mdp4_lvds_connector_helper_funcs);
112 
113 	connector->polled = 0;
114 
115 	connector->interlace_allowed = 0;
116 	connector->doublescan_allowed = 0;
117 
118 	drm_connector_attach_encoder(connector, encoder);
119 
120 	return connector;
121 }
122