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 
mdp4_lvds_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)59 static int mdp4_lvds_connector_mode_valid(struct drm_connector *connector,
60 				 struct drm_display_mode *mode)
61 {
62 	struct mdp4_lvds_connector *mdp4_lvds_connector =
63 			to_mdp4_lvds_connector(connector);
64 	struct drm_encoder *encoder = mdp4_lvds_connector->encoder;
65 	long actual, requested;
66 
67 	requested = 1000 * mode->clock;
68 	actual = mdp4_lcdc_round_pixclk(encoder, requested);
69 
70 	DBG("requested=%ld, actual=%ld", requested, actual);
71 
72 	if (actual != requested)
73 		return MODE_CLOCK_RANGE;
74 
75 	return MODE_OK;
76 }
77 
78 static const struct drm_connector_funcs mdp4_lvds_connector_funcs = {
79 	.detect = mdp4_lvds_connector_detect,
80 	.fill_modes = drm_helper_probe_single_connector_modes,
81 	.destroy = mdp4_lvds_connector_destroy,
82 	.reset = drm_atomic_helper_connector_reset,
83 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
84 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
85 };
86 
87 static const struct drm_connector_helper_funcs mdp4_lvds_connector_helper_funcs = {
88 	.get_modes = mdp4_lvds_connector_get_modes,
89 	.mode_valid = mdp4_lvds_connector_mode_valid,
90 };
91 
92 /* initialize connector */
mdp4_lvds_connector_init(struct drm_device * dev,struct device_node * panel_node,struct drm_encoder * encoder)93 struct drm_connector *mdp4_lvds_connector_init(struct drm_device *dev,
94 		struct device_node *panel_node, struct drm_encoder *encoder)
95 {
96 	struct drm_connector *connector = NULL;
97 	struct mdp4_lvds_connector *mdp4_lvds_connector;
98 
99 	mdp4_lvds_connector = kzalloc(sizeof(*mdp4_lvds_connector), GFP_KERNEL);
100 	if (!mdp4_lvds_connector)
101 		return ERR_PTR(-ENOMEM);
102 
103 	mdp4_lvds_connector->encoder = encoder;
104 	mdp4_lvds_connector->panel_node = panel_node;
105 
106 	connector = &mdp4_lvds_connector->base;
107 
108 	drm_connector_init(dev, connector, &mdp4_lvds_connector_funcs,
109 			DRM_MODE_CONNECTOR_LVDS);
110 	drm_connector_helper_add(connector, &mdp4_lvds_connector_helper_funcs);
111 
112 	connector->polled = 0;
113 
114 	connector->interlace_allowed = 0;
115 	connector->doublescan_allowed = 0;
116 
117 	drm_connector_attach_encoder(connector, encoder);
118 
119 	return connector;
120 }
121