Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Traphandler |
| 3 | * Copyright (C) 2014 Free Electrons |
| 4 | * Copyright (C) 2014 Atmel |
| 5 | * |
| 6 | * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com> |
| 7 | * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License version 2 as published by |
| 11 | * the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/of_graph.h> |
| 23 | |
| 24 | #include <drm/drmP.h> |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 25 | #include <drm/drm_of.h> |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 26 | |
| 27 | #include "atmel_hlcdc_dc.h" |
| 28 | |
| 29 | /** |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 30 | * Atmel HLCDC RGB connector structure |
| 31 | * |
| 32 | * This structure stores RGB slave device information. |
| 33 | * |
| 34 | * @connector: DRM connector |
| 35 | * @encoder: DRM encoder |
| 36 | * @dc: pointer to the atmel_hlcdc_dc structure |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 37 | * @panel: panel connected on the RGB output |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 38 | */ |
| 39 | struct atmel_hlcdc_rgb_output { |
| 40 | struct drm_connector connector; |
| 41 | struct drm_encoder encoder; |
| 42 | struct atmel_hlcdc_dc *dc; |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 43 | struct drm_panel *panel; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static inline struct atmel_hlcdc_rgb_output * |
| 47 | drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector) |
| 48 | { |
| 49 | return container_of(connector, struct atmel_hlcdc_rgb_output, |
| 50 | connector); |
| 51 | } |
| 52 | |
| 53 | static inline struct atmel_hlcdc_rgb_output * |
| 54 | drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder) |
| 55 | { |
| 56 | return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder); |
| 57 | } |
| 58 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 59 | static void atmel_hlcdc_rgb_encoder_enable(struct drm_encoder *encoder) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 60 | { |
| 61 | struct atmel_hlcdc_rgb_output *rgb = |
| 62 | drm_encoder_to_atmel_hlcdc_rgb_output(encoder); |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 63 | |
| 64 | if (rgb->panel) { |
| 65 | drm_panel_prepare(rgb->panel); |
| 66 | drm_panel_enable(rgb->panel); |
| 67 | } |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 68 | } |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 69 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 70 | static void atmel_hlcdc_rgb_encoder_disable(struct drm_encoder *encoder) |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 71 | { |
| 72 | struct atmel_hlcdc_rgb_output *rgb = |
| 73 | drm_encoder_to_atmel_hlcdc_rgb_output(encoder); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 74 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 75 | if (rgb->panel) { |
| 76 | drm_panel_disable(rgb->panel); |
| 77 | drm_panel_unprepare(rgb->panel); |
| 78 | } |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Ville Syrjälä | e9becd4 | 2015-12-15 12:21:00 +0100 | [diff] [blame] | 81 | static const struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = { |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 82 | .disable = atmel_hlcdc_rgb_encoder_disable, |
| 83 | .enable = atmel_hlcdc_rgb_encoder_enable, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder) |
| 87 | { |
| 88 | drm_encoder_cleanup(encoder); |
| 89 | memset(encoder, 0, sizeof(*encoder)); |
| 90 | } |
| 91 | |
| 92 | static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = { |
| 93 | .destroy = atmel_hlcdc_rgb_encoder_destroy, |
| 94 | }; |
| 95 | |
| 96 | static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector) |
| 97 | { |
| 98 | struct atmel_hlcdc_rgb_output *rgb = |
| 99 | drm_connector_to_atmel_hlcdc_rgb_output(connector); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 100 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 101 | if (rgb->panel) |
| 102 | return rgb->panel->funcs->get_modes(rgb->panel); |
| 103 | |
| 104 | return 0; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector, |
| 108 | struct drm_display_mode *mode) |
| 109 | { |
| 110 | struct atmel_hlcdc_rgb_output *rgb = |
| 111 | drm_connector_to_atmel_hlcdc_rgb_output(connector); |
| 112 | |
| 113 | return atmel_hlcdc_dc_mode_valid(rgb->dc, mode); |
| 114 | } |
| 115 | |
Ville Syrjälä | e9becd4 | 2015-12-15 12:21:00 +0100 | [diff] [blame] | 116 | static const struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = { |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 117 | .get_modes = atmel_hlcdc_panel_get_modes, |
| 118 | .mode_valid = atmel_hlcdc_rgb_mode_valid, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static enum drm_connector_status |
| 122 | atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force) |
| 123 | { |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 124 | struct atmel_hlcdc_rgb_output *rgb = |
| 125 | drm_connector_to_atmel_hlcdc_rgb_output(connector); |
| 126 | |
| 127 | if (rgb->panel) |
| 128 | return connector_status_connected; |
| 129 | |
| 130 | return connector_status_disconnected; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void |
| 134 | atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector) |
| 135 | { |
| 136 | struct atmel_hlcdc_rgb_output *rgb = |
| 137 | drm_connector_to_atmel_hlcdc_rgb_output(connector); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 138 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 139 | if (rgb->panel) |
| 140 | drm_panel_detach(rgb->panel); |
| 141 | |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 142 | drm_connector_cleanup(connector); |
| 143 | } |
| 144 | |
| 145 | static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = { |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 146 | .dpms = drm_atomic_helper_connector_dpms, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 147 | .detect = atmel_hlcdc_panel_connector_detect, |
| 148 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 149 | .destroy = atmel_hlcdc_panel_connector_destroy, |
Boris Brezillon | 2389fc1 | 2015-02-05 16:32:33 +0100 | [diff] [blame] | 150 | .reset = drm_atomic_helper_connector_reset, |
| 151 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 152 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 153 | }; |
| 154 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 155 | static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 156 | const struct device_node *np) |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 157 | { |
| 158 | struct atmel_hlcdc_dc *dc = dev->dev_private; |
| 159 | struct atmel_hlcdc_rgb_output *output; |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 160 | struct drm_panel *panel; |
| 161 | struct drm_bridge *bridge; |
| 162 | int ret; |
| 163 | |
| 164 | output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL); |
| 165 | if (!output) |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 166 | return -EINVAL; |
| 167 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 168 | output->dc = dc; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 169 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 170 | drm_encoder_helper_add(&output->encoder, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 171 | &atmel_hlcdc_panel_encoder_helper_funcs); |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 172 | ret = drm_encoder_init(dev, &output->encoder, |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 173 | &atmel_hlcdc_panel_encoder_funcs, |
Boris Brezillon | abfc936 | 2015-12-31 18:24:09 +0100 | [diff] [blame] | 174 | DRM_MODE_ENCODER_NONE, NULL); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 175 | if (ret) |
| 176 | return ret; |
| 177 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 178 | output->encoder.possible_crtcs = 0x1; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 179 | |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 180 | ret = drm_of_find_panel_or_bridge(np, 0, 0, &panel, &bridge); |
| 181 | if (ret) |
| 182 | return ret; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 183 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 184 | if (panel) { |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 185 | output->connector.dpms = DRM_MODE_DPMS_OFF; |
| 186 | output->connector.polled = DRM_CONNECTOR_POLL_CONNECT; |
| 187 | drm_connector_helper_add(&output->connector, |
| 188 | &atmel_hlcdc_panel_connector_helper_funcs); |
| 189 | ret = drm_connector_init(dev, &output->connector, |
| 190 | &atmel_hlcdc_panel_connector_funcs, |
| 191 | DRM_MODE_CONNECTOR_Unknown); |
| 192 | if (ret) |
| 193 | goto err_encoder_cleanup; |
| 194 | |
| 195 | drm_mode_connector_attach_encoder(&output->connector, |
| 196 | &output->encoder); |
| 197 | |
| 198 | ret = drm_panel_attach(panel, &output->connector); |
| 199 | if (ret) { |
| 200 | drm_connector_cleanup(&output->connector); |
| 201 | goto err_encoder_cleanup; |
| 202 | } |
| 203 | |
| 204 | output->panel = panel; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 209 | if (bridge) { |
Laurent Pinchart | 3bb80f2 | 2016-11-28 17:59:08 +0200 | [diff] [blame] | 210 | ret = drm_bridge_attach(&output->encoder, bridge, NULL); |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 211 | if (!ret) |
| 212 | return 0; |
| 213 | } |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 214 | |
| 215 | err_encoder_cleanup: |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 216 | drm_encoder_cleanup(&output->encoder); |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | int atmel_hlcdc_create_outputs(struct drm_device *dev) |
| 222 | { |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 223 | struct device_node *remote; |
Dan Carpenter | 418d59e | 2017-04-15 22:21:42 +0300 | [diff] [blame] | 224 | int ret = -ENODEV; |
| 225 | int endpoint = 0; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 226 | |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 227 | while (true) { |
| 228 | /* Loop thru possible multiple connections to the output */ |
| 229 | remote = of_graph_get_remote_node(dev->dev->of_node, 0, |
| 230 | endpoint++); |
| 231 | if (!remote) |
| 232 | break; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 233 | |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 234 | ret = atmel_hlcdc_attach_endpoint(dev, remote); |
| 235 | of_node_put(remote); |
| 236 | if (ret) |
Boris Brezillon | 17a8e03 | 2016-01-06 10:16:32 +0100 | [diff] [blame] | 237 | return ret; |
| 238 | } |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 239 | |
Rob Herring | ebc9446 | 2017-03-29 13:55:46 -0500 | [diff] [blame] | 240 | return ret; |
Boris Brezillon | 1a39678 | 2015-01-06 11:13:28 +0100 | [diff] [blame] | 241 | } |