Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 1 | /* |
Andrew F. Davis | bb5cdf8 | 2017-12-05 14:29:31 -0600 | [diff] [blame] | 2 | * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 3 | * Author: Rob Clark <rob@ti.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Laurent Pinchart | 2d278f5 | 2015-03-05 21:31:37 +0200 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | |
| 20 | #include <drm/drm_crtc.h> |
| 21 | #include <drm/drm_crtc_helper.h> |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 22 | #include <drm/drm_edid.h> |
| 23 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 24 | #include "omap_drv.h" |
| 25 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 26 | /* |
| 27 | * encoder funcs |
| 28 | */ |
| 29 | |
| 30 | #define to_omap_encoder(x) container_of(x, struct omap_encoder, base) |
| 31 | |
Rob Clark | f5f9454 | 2012-12-04 13:59:12 -0600 | [diff] [blame] | 32 | /* The encoder and connector both map to same dssdev.. the encoder |
| 33 | * handles the 'active' parts, ie. anything the modifies the state |
| 34 | * of the hw, and the connector handles the 'read-only' parts, like |
| 35 | * detecting connection and reading edid. |
| 36 | */ |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 37 | struct omap_encoder { |
| 38 | struct drm_encoder base; |
Laurent Pinchart | d96aaad | 2018-05-31 23:14:43 +0300 | [diff] [blame] | 39 | struct omap_dss_device *output; |
| 40 | struct omap_dss_device *display; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static void omap_encoder_destroy(struct drm_encoder *encoder) |
| 44 | { |
| 45 | struct omap_encoder *omap_encoder = to_omap_encoder(encoder); |
Archit Taneja | ec72a81 | 2014-01-02 14:49:53 +0530 | [diff] [blame] | 46 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 47 | drm_encoder_cleanup(encoder); |
| 48 | kfree(omap_encoder); |
| 49 | } |
| 50 | |
Rob Clark | f5f9454 | 2012-12-04 13:59:12 -0600 | [diff] [blame] | 51 | static const struct drm_encoder_funcs omap_encoder_funcs = { |
| 52 | .destroy = omap_encoder_destroy, |
| 53 | }; |
| 54 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 55 | static void omap_encoder_mode_set(struct drm_encoder *encoder, |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 56 | struct drm_display_mode *mode, |
| 57 | struct drm_display_mode *adjusted_mode) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 58 | { |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 59 | struct drm_device *dev = encoder->dev; |
| 60 | struct omap_encoder *omap_encoder = to_omap_encoder(encoder); |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 61 | struct omap_dss_device *display = omap_encoder->display; |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 62 | struct drm_connector *connector; |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 63 | struct omap_dss_device *dssdev; |
| 64 | struct videomode vm = { 0 }; |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 65 | bool hdmi_mode; |
| 66 | int r; |
| 67 | |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 68 | drm_display_mode_to_videomode(adjusted_mode, &vm); |
| 69 | |
| 70 | /* |
| 71 | * HACK: This fixes the vm flags. |
| 72 | * struct drm_display_mode does not contain the VSYNC/HSYNC/DE flags and |
| 73 | * they get lost when converting back and forth between struct |
| 74 | * drm_display_mode and struct videomode. The hack below goes and |
| 75 | * fetches the missing flags. |
| 76 | * |
| 77 | * A better solution is to use DRM's bus-flags through the whole driver. |
| 78 | */ |
| 79 | for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) { |
| 80 | unsigned long bus_flags = dssdev->bus_flags; |
| 81 | |
| 82 | if (!(vm.flags & (DISPLAY_FLAGS_DE_LOW | |
| 83 | DISPLAY_FLAGS_DE_HIGH))) { |
| 84 | if (bus_flags & DRM_BUS_FLAG_DE_LOW) |
| 85 | vm.flags |= DISPLAY_FLAGS_DE_LOW; |
| 86 | else if (bus_flags & DRM_BUS_FLAG_DE_HIGH) |
| 87 | vm.flags |= DISPLAY_FLAGS_DE_HIGH; |
| 88 | } |
| 89 | |
| 90 | if (!(vm.flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE | |
| 91 | DISPLAY_FLAGS_PIXDATA_NEGEDGE))) { |
| 92 | if (bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE) |
| 93 | vm.flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE; |
| 94 | else if (bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE) |
| 95 | vm.flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 96 | } |
| 97 | |
| 98 | if (!(vm.flags & (DISPLAY_FLAGS_SYNC_POSEDGE | |
| 99 | DISPLAY_FLAGS_SYNC_NEGEDGE))) { |
| 100 | if (bus_flags & DRM_BUS_FLAG_SYNC_POSEDGE) |
| 101 | vm.flags |= DISPLAY_FLAGS_SYNC_POSEDGE; |
| 102 | else if (bus_flags & DRM_BUS_FLAG_SYNC_NEGEDGE) |
| 103 | vm.flags |= DISPLAY_FLAGS_SYNC_NEGEDGE; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * HACK: Call the .set_timings() operation if available, this will |
| 109 | * eventually store timings in the CRTC. Otherwise (for DSI outputs) |
| 110 | * store the timings directly. |
| 111 | * |
| 112 | * All outputs should be brought in sync to operate similarly. |
| 113 | */ |
| 114 | if (display->ops->set_timings) |
| 115 | display->ops->set_timings(display, &vm); |
| 116 | else |
| 117 | *omap_crtc_timings(encoder->crtc) = vm; |
| 118 | |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 119 | hdmi_mode = false; |
| 120 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 121 | if (connector->encoder == encoder) { |
| 122 | hdmi_mode = omap_connector_get_hdmi_mode(connector); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 127 | dssdev = omap_encoder->output; |
| 128 | |
Laurent Pinchart | 83910ad | 2018-06-01 19:45:01 +0300 | [diff] [blame] | 129 | if (dssdev->ops->hdmi.set_hdmi_mode) |
| 130 | dssdev->ops->hdmi.set_hdmi_mode(dssdev, hdmi_mode); |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 131 | |
Laurent Pinchart | 83910ad | 2018-06-01 19:45:01 +0300 | [diff] [blame] | 132 | if (hdmi_mode && dssdev->ops->hdmi.set_infoframe) { |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 133 | struct hdmi_avi_infoframe avi; |
| 134 | |
Shashank Sharma | 0c1f528 | 2017-07-13 21:03:07 +0530 | [diff] [blame] | 135 | r = drm_hdmi_avi_infoframe_from_display_mode(&avi, adjusted_mode, |
| 136 | false); |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 137 | if (r == 0) |
Laurent Pinchart | 83910ad | 2018-06-01 19:45:01 +0300 | [diff] [blame] | 138 | dssdev->ops->hdmi.set_infoframe(dssdev, &avi); |
Tomi Valkeinen | 4f930c0 | 2014-06-18 14:19:48 +0300 | [diff] [blame] | 139 | } |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 140 | } |
| 141 | |
Laurent Pinchart | 68dc039 | 2015-03-07 00:22:39 +0200 | [diff] [blame] | 142 | static void omap_encoder_disable(struct drm_encoder *encoder) |
| 143 | { |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 144 | struct omap_encoder *omap_encoder = to_omap_encoder(encoder); |
Laurent Pinchart | d96aaad | 2018-05-31 23:14:43 +0300 | [diff] [blame] | 145 | struct omap_dss_device *dssdev = omap_encoder->display; |
Rob Clark | f5f9454 | 2012-12-04 13:59:12 -0600 | [diff] [blame] | 146 | |
Laurent Pinchart | 83910ad | 2018-06-01 19:45:01 +0300 | [diff] [blame] | 147 | dssdev->ops->disable(dssdev); |
Rob Clark | f5f9454 | 2012-12-04 13:59:12 -0600 | [diff] [blame] | 148 | } |
| 149 | |
Laurent Pinchart | 4029755e | 2015-05-28 02:34:05 +0300 | [diff] [blame] | 150 | static void omap_encoder_enable(struct drm_encoder *encoder) |
| 151 | { |
| 152 | struct omap_encoder *omap_encoder = to_omap_encoder(encoder); |
Laurent Pinchart | d96aaad | 2018-05-31 23:14:43 +0300 | [diff] [blame] | 153 | struct omap_dss_device *dssdev = omap_encoder->display; |
Tomi Valkeinen | 8d83bbd | 2016-01-05 11:43:16 +0200 | [diff] [blame] | 154 | int r; |
Laurent Pinchart | 4029755e | 2015-05-28 02:34:05 +0300 | [diff] [blame] | 155 | |
Laurent Pinchart | 83910ad | 2018-06-01 19:45:01 +0300 | [diff] [blame] | 156 | r = dssdev->ops->enable(dssdev); |
Tomi Valkeinen | 8d83bbd | 2016-01-05 11:43:16 +0200 | [diff] [blame] | 157 | if (r) |
| 158 | dev_err(encoder->dev->dev, |
| 159 | "Failed to enable display '%s': %d\n", |
| 160 | dssdev->name, r); |
Laurent Pinchart | 4029755e | 2015-05-28 02:34:05 +0300 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int omap_encoder_atomic_check(struct drm_encoder *encoder, |
| 164 | struct drm_crtc_state *crtc_state, |
| 165 | struct drm_connector_state *conn_state) |
| 166 | { |
Laurent Pinchart | 3fbda31 | 2018-06-07 17:58:57 +0300 | [diff] [blame^] | 167 | struct omap_encoder *omap_encoder = to_omap_encoder(encoder); |
| 168 | struct drm_device *dev = encoder->dev; |
| 169 | struct omap_dss_device *dssdev; |
| 170 | struct videomode vm = { 0 }; |
| 171 | int ret; |
| 172 | |
| 173 | drm_display_mode_to_videomode(&crtc_state->mode, &vm); |
| 174 | |
| 175 | for (dssdev = omap_encoder->output; dssdev; dssdev = dssdev->next) { |
| 176 | if (!dssdev->ops->check_timings) |
| 177 | continue; |
| 178 | |
| 179 | ret = dssdev->ops->check_timings(dssdev, &vm); |
| 180 | if (ret) { |
| 181 | dev_err(dev->dev, "invalid timings: %d\n", ret); |
| 182 | return ret; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | drm_display_mode_from_videomode(&vm, &crtc_state->adjusted_mode); |
| 187 | |
Laurent Pinchart | 4029755e | 2015-05-28 02:34:05 +0300 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static const struct drm_encoder_helper_funcs omap_encoder_helper_funcs = { |
| 192 | .mode_set = omap_encoder_mode_set, |
| 193 | .disable = omap_encoder_disable, |
| 194 | .enable = omap_encoder_enable, |
| 195 | .atomic_check = omap_encoder_atomic_check, |
| 196 | }; |
| 197 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 198 | /* initialize encoder */ |
| 199 | struct drm_encoder *omap_encoder_init(struct drm_device *dev, |
Laurent Pinchart | d96aaad | 2018-05-31 23:14:43 +0300 | [diff] [blame] | 200 | struct omap_dss_device *output, |
| 201 | struct omap_dss_device *display) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 202 | { |
| 203 | struct drm_encoder *encoder = NULL; |
| 204 | struct omap_encoder *omap_encoder; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 205 | |
| 206 | omap_encoder = kzalloc(sizeof(*omap_encoder), GFP_KERNEL); |
Joe Perches | 78110bb | 2013-02-11 09:41:29 -0800 | [diff] [blame] | 207 | if (!omap_encoder) |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 208 | goto fail; |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 209 | |
Laurent Pinchart | d96aaad | 2018-05-31 23:14:43 +0300 | [diff] [blame] | 210 | omap_encoder->output = output; |
| 211 | omap_encoder->display = display; |
Rob Clark | f5f9454 | 2012-12-04 13:59:12 -0600 | [diff] [blame] | 212 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 213 | encoder = &omap_encoder->base; |
| 214 | |
| 215 | drm_encoder_init(dev, encoder, &omap_encoder_funcs, |
Ville Syrjälä | 13a3d91 | 2015-12-09 16:20:18 +0200 | [diff] [blame] | 216 | DRM_MODE_ENCODER_TMDS, NULL); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 217 | drm_encoder_helper_add(encoder, &omap_encoder_helper_funcs); |
| 218 | |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 219 | return encoder; |
| 220 | |
| 221 | fail: |
YAMANE Toshiaki | 582bc28 | 2012-11-14 19:31:27 +0900 | [diff] [blame] | 222 | if (encoder) |
Rob Clark | 65b0bd0 | 2011-12-09 23:26:07 -0600 | [diff] [blame] | 223 | omap_encoder_destroy(encoder); |
Rob Clark | cd5351f | 2011-11-12 12:09:40 -0600 | [diff] [blame] | 224 | |
| 225 | return NULL; |
| 226 | } |