1 /*
2 * Copyright (C) 2014 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
4 * Author: Vinay Simha <vinaysimha@inforcecomputing.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <linux/gpio.h>
20
21 #include "mdp4_kms.h"
22
23 struct mdp4_lvds_connector {
24 struct drm_connector base;
25 struct drm_encoder *encoder;
26 struct device_node *panel_node;
27 struct drm_panel *panel;
28 };
29 #define to_mdp4_lvds_connector(x) container_of(x, struct mdp4_lvds_connector, base)
30
mdp4_lvds_connector_detect(struct drm_connector * connector,bool force)31 static enum drm_connector_status mdp4_lvds_connector_detect(
32 struct drm_connector *connector, bool force)
33 {
34 struct mdp4_lvds_connector *mdp4_lvds_connector =
35 to_mdp4_lvds_connector(connector);
36
37 if (!mdp4_lvds_connector->panel) {
38 mdp4_lvds_connector->panel =
39 of_drm_find_panel(mdp4_lvds_connector->panel_node);
40 if (IS_ERR(mdp4_lvds_connector->panel))
41 mdp4_lvds_connector->panel = NULL;
42 }
43
44 return mdp4_lvds_connector->panel ?
45 connector_status_connected :
46 connector_status_disconnected;
47 }
48
mdp4_lvds_connector_destroy(struct drm_connector * connector)49 static void mdp4_lvds_connector_destroy(struct drm_connector *connector)
50 {
51 struct mdp4_lvds_connector *mdp4_lvds_connector =
52 to_mdp4_lvds_connector(connector);
53
54 drm_connector_cleanup(connector);
55
56 kfree(mdp4_lvds_connector);
57 }
58
mdp4_lvds_connector_get_modes(struct drm_connector * connector)59 static int mdp4_lvds_connector_get_modes(struct drm_connector *connector)
60 {
61 struct mdp4_lvds_connector *mdp4_lvds_connector =
62 to_mdp4_lvds_connector(connector);
63 struct drm_panel *panel = mdp4_lvds_connector->panel;
64 int ret = 0;
65
66 if (panel) {
67 drm_panel_attach(panel, connector);
68
69 ret = panel->funcs->get_modes(panel);
70
71 drm_panel_detach(panel);
72 }
73
74 return ret;
75 }
76
mdp4_lvds_connector_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)77 static int mdp4_lvds_connector_mode_valid(struct drm_connector *connector,
78 struct drm_display_mode *mode)
79 {
80 struct mdp4_lvds_connector *mdp4_lvds_connector =
81 to_mdp4_lvds_connector(connector);
82 struct drm_encoder *encoder = mdp4_lvds_connector->encoder;
83 long actual, requested;
84
85 requested = 1000 * mode->clock;
86 actual = mdp4_lcdc_round_pixclk(encoder, requested);
87
88 DBG("requested=%ld, actual=%ld", requested, actual);
89
90 if (actual != requested)
91 return MODE_CLOCK_RANGE;
92
93 return MODE_OK;
94 }
95
96 static const struct drm_connector_funcs mdp4_lvds_connector_funcs = {
97 .detect = mdp4_lvds_connector_detect,
98 .fill_modes = drm_helper_probe_single_connector_modes,
99 .destroy = mdp4_lvds_connector_destroy,
100 .reset = drm_atomic_helper_connector_reset,
101 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
102 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
103 };
104
105 static const struct drm_connector_helper_funcs mdp4_lvds_connector_helper_funcs = {
106 .get_modes = mdp4_lvds_connector_get_modes,
107 .mode_valid = mdp4_lvds_connector_mode_valid,
108 };
109
110 /* initialize connector */
mdp4_lvds_connector_init(struct drm_device * dev,struct device_node * panel_node,struct drm_encoder * encoder)111 struct drm_connector *mdp4_lvds_connector_init(struct drm_device *dev,
112 struct device_node *panel_node, struct drm_encoder *encoder)
113 {
114 struct drm_connector *connector = NULL;
115 struct mdp4_lvds_connector *mdp4_lvds_connector;
116
117 mdp4_lvds_connector = kzalloc(sizeof(*mdp4_lvds_connector), GFP_KERNEL);
118 if (!mdp4_lvds_connector)
119 return ERR_PTR(-ENOMEM);
120
121 mdp4_lvds_connector->encoder = encoder;
122 mdp4_lvds_connector->panel_node = panel_node;
123
124 connector = &mdp4_lvds_connector->base;
125
126 drm_connector_init(dev, connector, &mdp4_lvds_connector_funcs,
127 DRM_MODE_CONNECTOR_LVDS);
128 drm_connector_helper_add(connector, &mdp4_lvds_connector_helper_funcs);
129
130 connector->polled = 0;
131
132 connector->interlace_allowed = 0;
133 connector->doublescan_allowed = 0;
134
135 drm_connector_attach_encoder(connector, encoder);
136
137 return connector;
138 }
139