Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 BayLibre, SAS |
| 3 | * Author: Neil Armstrong <narmstrong@baylibre.com> |
| 4 | * Copyright (C) 2015 Amlogic, Inc. All rights reserved. |
| 5 | * Copyright (C) 2014 Endless Mobile |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * Written by: |
| 21 | * Jasper St. Pierre <jstpierre@mecheye.net> |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/of_graph.h> |
| 27 | |
| 28 | #include <drm/drmP.h> |
| 29 | #include <drm/drm_edid.h> |
| 30 | #include <drm/drm_crtc_helper.h> |
| 31 | #include <drm/drm_atomic_helper.h> |
| 32 | |
| 33 | #include "meson_venc_cvbs.h" |
| 34 | #include "meson_venc.h" |
Neil Armstrong | 2f4c95d | 2017-04-04 14:15:25 +0200 | [diff] [blame] | 35 | #include "meson_vclk.h" |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 36 | #include "meson_registers.h" |
| 37 | |
| 38 | /* HHI VDAC Registers */ |
| 39 | #define HHI_VDAC_CNTL0 0x2F4 /* 0xbd offset in data sheet */ |
| 40 | #define HHI_VDAC_CNTL1 0x2F8 /* 0xbe offset in data sheet */ |
| 41 | |
| 42 | struct meson_venc_cvbs { |
| 43 | struct drm_encoder encoder; |
| 44 | struct drm_connector connector; |
| 45 | struct meson_drm *priv; |
| 46 | }; |
| 47 | #define encoder_to_meson_venc_cvbs(x) \ |
| 48 | container_of(x, struct meson_venc_cvbs, encoder) |
| 49 | |
| 50 | #define connector_to_meson_venc_cvbs(x) \ |
| 51 | container_of(x, struct meson_venc_cvbs, connector) |
| 52 | |
| 53 | /* Supported Modes */ |
| 54 | |
| 55 | struct meson_cvbs_mode meson_cvbs_modes[MESON_CVBS_MODES_COUNT] = { |
| 56 | { /* PAL */ |
| 57 | .enci = &meson_cvbs_enci_pal, |
| 58 | .mode = { |
| 59 | DRM_MODE("720x576i", DRM_MODE_TYPE_DRIVER, 13500, |
| 60 | 720, 732, 795, 864, 0, 576, 580, 586, 625, 0, |
| 61 | DRM_MODE_FLAG_INTERLACE), |
| 62 | .vrefresh = 50, |
| 63 | .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, |
| 64 | }, |
| 65 | }, |
| 66 | { /* NTSC */ |
| 67 | .enci = &meson_cvbs_enci_ntsc, |
| 68 | .mode = { |
| 69 | DRM_MODE("720x480i", DRM_MODE_TYPE_DRIVER, 13500, |
| 70 | 720, 739, 801, 858, 0, 480, 488, 494, 525, 0, |
| 71 | DRM_MODE_FLAG_INTERLACE), |
| 72 | .vrefresh = 60, |
| 73 | .picture_aspect_ratio = HDMI_PICTURE_ASPECT_4_3, |
| 74 | }, |
| 75 | }, |
| 76 | }; |
| 77 | |
| 78 | /* Connector */ |
| 79 | |
| 80 | static void meson_cvbs_connector_destroy(struct drm_connector *connector) |
| 81 | { |
| 82 | drm_connector_cleanup(connector); |
| 83 | } |
| 84 | |
| 85 | static enum drm_connector_status |
| 86 | meson_cvbs_connector_detect(struct drm_connector *connector, bool force) |
| 87 | { |
| 88 | /* FIXME: Add load-detect or jack-detect if possible */ |
| 89 | return connector_status_connected; |
| 90 | } |
| 91 | |
| 92 | static int meson_cvbs_connector_get_modes(struct drm_connector *connector) |
| 93 | { |
| 94 | struct drm_device *dev = connector->dev; |
| 95 | struct drm_display_mode *mode; |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < MESON_CVBS_MODES_COUNT; ++i) { |
| 99 | struct meson_cvbs_mode *meson_mode = &meson_cvbs_modes[i]; |
| 100 | |
| 101 | mode = drm_mode_duplicate(dev, &meson_mode->mode); |
| 102 | if (!mode) { |
| 103 | DRM_ERROR("Failed to create a new display mode\n"); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | drm_mode_probed_add(connector, mode); |
| 108 | } |
| 109 | |
| 110 | return i; |
| 111 | } |
| 112 | |
| 113 | static int meson_cvbs_connector_mode_valid(struct drm_connector *connector, |
| 114 | struct drm_display_mode *mode) |
| 115 | { |
| 116 | /* Validate the modes added in get_modes */ |
| 117 | return MODE_OK; |
| 118 | } |
| 119 | |
| 120 | static const struct drm_connector_funcs meson_cvbs_connector_funcs = { |
| 121 | .dpms = drm_atomic_helper_connector_dpms, |
| 122 | .detect = meson_cvbs_connector_detect, |
| 123 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 124 | .destroy = meson_cvbs_connector_destroy, |
| 125 | .reset = drm_atomic_helper_connector_reset, |
| 126 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 127 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 128 | }; |
| 129 | |
| 130 | static const |
| 131 | struct drm_connector_helper_funcs meson_cvbs_connector_helper_funcs = { |
| 132 | .get_modes = meson_cvbs_connector_get_modes, |
| 133 | .mode_valid = meson_cvbs_connector_mode_valid, |
| 134 | }; |
| 135 | |
| 136 | /* Encoder */ |
| 137 | |
| 138 | static void meson_venc_cvbs_encoder_destroy(struct drm_encoder *encoder) |
| 139 | { |
| 140 | drm_encoder_cleanup(encoder); |
| 141 | } |
| 142 | |
| 143 | static const struct drm_encoder_funcs meson_venc_cvbs_encoder_funcs = { |
| 144 | .destroy = meson_venc_cvbs_encoder_destroy, |
| 145 | }; |
| 146 | |
| 147 | static int meson_venc_cvbs_encoder_atomic_check(struct drm_encoder *encoder, |
| 148 | struct drm_crtc_state *crtc_state, |
| 149 | struct drm_connector_state *conn_state) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | for (i = 0; i < MESON_CVBS_MODES_COUNT; ++i) { |
| 154 | struct meson_cvbs_mode *meson_mode = &meson_cvbs_modes[i]; |
| 155 | |
| 156 | if (drm_mode_equal(&crtc_state->mode, &meson_mode->mode)) |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | static void meson_venc_cvbs_encoder_disable(struct drm_encoder *encoder) |
| 164 | { |
| 165 | struct meson_venc_cvbs *meson_venc_cvbs = |
| 166 | encoder_to_meson_venc_cvbs(encoder); |
| 167 | struct meson_drm *priv = meson_venc_cvbs->priv; |
| 168 | |
| 169 | /* Disable CVBS VDAC */ |
| 170 | regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0); |
Neil Armstrong | 5db60ea | 2017-01-04 10:51:02 +0100 | [diff] [blame] | 171 | regmap_write(priv->hhi, HHI_VDAC_CNTL1, 8); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void meson_venc_cvbs_encoder_enable(struct drm_encoder *encoder) |
| 175 | { |
| 176 | struct meson_venc_cvbs *meson_venc_cvbs = |
| 177 | encoder_to_meson_venc_cvbs(encoder); |
| 178 | struct meson_drm *priv = meson_venc_cvbs->priv; |
| 179 | |
| 180 | /* VDAC0 source is not from ATV */ |
| 181 | writel_bits_relaxed(BIT(5), 0, priv->io_base + _REG(VENC_VDAC_DACSEL0)); |
| 182 | |
| 183 | if (meson_vpu_is_compatible(priv, "amlogic,meson-gxbb-vpu")) |
| 184 | regmap_write(priv->hhi, HHI_VDAC_CNTL0, 1); |
| 185 | else if (meson_vpu_is_compatible(priv, "amlogic,meson-gxm-vpu") || |
| 186 | meson_vpu_is_compatible(priv, "amlogic,meson-gxl-vpu")) |
| 187 | regmap_write(priv->hhi, HHI_VDAC_CNTL0, 0xf0001); |
| 188 | |
| 189 | regmap_write(priv->hhi, HHI_VDAC_CNTL1, 0); |
| 190 | } |
| 191 | |
| 192 | static void meson_venc_cvbs_encoder_mode_set(struct drm_encoder *encoder, |
| 193 | struct drm_display_mode *mode, |
| 194 | struct drm_display_mode *adjusted_mode) |
| 195 | { |
| 196 | struct meson_venc_cvbs *meson_venc_cvbs = |
| 197 | encoder_to_meson_venc_cvbs(encoder); |
Neil Armstrong | 2f4c95d | 2017-04-04 14:15:25 +0200 | [diff] [blame] | 198 | struct meson_drm *priv = meson_venc_cvbs->priv; |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < MESON_CVBS_MODES_COUNT; ++i) { |
| 202 | struct meson_cvbs_mode *meson_mode = &meson_cvbs_modes[i]; |
| 203 | |
| 204 | if (drm_mode_equal(mode, &meson_mode->mode)) { |
Neil Armstrong | 2f4c95d | 2017-04-04 14:15:25 +0200 | [diff] [blame] | 205 | meson_venci_cvbs_mode_set(priv, |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 206 | meson_mode->enci); |
Neil Armstrong | 2f4c95d | 2017-04-04 14:15:25 +0200 | [diff] [blame] | 207 | |
| 208 | /* Setup 27MHz vclk2 for ENCI and VDAC */ |
| 209 | meson_vclk_setup(priv, MESON_VCLK_TARGET_CVBS, |
| 210 | MESON_VCLK_CVBS, MESON_VCLK_CVBS, |
| 211 | MESON_VCLK_CVBS, true); |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static const struct drm_encoder_helper_funcs |
| 218 | meson_venc_cvbs_encoder_helper_funcs = { |
| 219 | .atomic_check = meson_venc_cvbs_encoder_atomic_check, |
| 220 | .disable = meson_venc_cvbs_encoder_disable, |
| 221 | .enable = meson_venc_cvbs_encoder_enable, |
| 222 | .mode_set = meson_venc_cvbs_encoder_mode_set, |
| 223 | }; |
| 224 | |
| 225 | static bool meson_venc_cvbs_connector_is_available(struct meson_drm *priv) |
| 226 | { |
Rob Herring | 86418f9 | 2017-03-22 08:26:06 -0500 | [diff] [blame] | 227 | struct device_node *remote; |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 228 | |
Rob Herring | 86418f9 | 2017-03-22 08:26:06 -0500 | [diff] [blame] | 229 | remote = of_graph_get_remote_node(priv->dev->of_node, 0, 0); |
| 230 | if (!remote) |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 231 | return false; |
| 232 | |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 233 | of_node_put(remote); |
Rob Herring | 86418f9 | 2017-03-22 08:26:06 -0500 | [diff] [blame] | 234 | return true; |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | int meson_venc_cvbs_create(struct meson_drm *priv) |
| 238 | { |
| 239 | struct drm_device *drm = priv->drm; |
| 240 | struct meson_venc_cvbs *meson_venc_cvbs; |
| 241 | struct drm_connector *connector; |
| 242 | struct drm_encoder *encoder; |
| 243 | int ret; |
| 244 | |
| 245 | if (!meson_venc_cvbs_connector_is_available(priv)) { |
| 246 | dev_info(drm->dev, "CVBS Output connector not available\n"); |
Neil Armstrong | cb110b6 | 2017-04-04 14:15:24 +0200 | [diff] [blame] | 247 | return 0; |
Neil Armstrong | bbbe775 | 2016-11-10 15:29:37 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | meson_venc_cvbs = devm_kzalloc(priv->dev, sizeof(*meson_venc_cvbs), |
| 251 | GFP_KERNEL); |
| 252 | if (!meson_venc_cvbs) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | meson_venc_cvbs->priv = priv; |
| 256 | encoder = &meson_venc_cvbs->encoder; |
| 257 | connector = &meson_venc_cvbs->connector; |
| 258 | |
| 259 | /* Connector */ |
| 260 | |
| 261 | drm_connector_helper_add(connector, |
| 262 | &meson_cvbs_connector_helper_funcs); |
| 263 | |
| 264 | ret = drm_connector_init(drm, connector, &meson_cvbs_connector_funcs, |
| 265 | DRM_MODE_CONNECTOR_Composite); |
| 266 | if (ret) { |
| 267 | dev_err(priv->dev, "Failed to init CVBS connector\n"); |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | connector->interlace_allowed = 1; |
| 272 | |
| 273 | /* Encoder */ |
| 274 | |
| 275 | drm_encoder_helper_add(encoder, &meson_venc_cvbs_encoder_helper_funcs); |
| 276 | |
| 277 | ret = drm_encoder_init(drm, encoder, &meson_venc_cvbs_encoder_funcs, |
| 278 | DRM_MODE_ENCODER_TVDAC, "meson_venc_cvbs"); |
| 279 | if (ret) { |
| 280 | dev_err(priv->dev, "Failed to init CVBS encoder\n"); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | encoder->possible_crtcs = BIT(0); |
| 285 | |
| 286 | drm_mode_connector_attach_encoder(connector, encoder); |
| 287 | |
| 288 | return 0; |
| 289 | } |