Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * rcar_du_encoder.c -- R-Car Display Unit Encoder |
| 3 | * |
Laurent Pinchart | 36d5046 | 2014-02-06 18:13:52 +0100 | [diff] [blame] | 4 | * Copyright (C) 2013-2014 Renesas Electronics Corporation |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 5 | * |
| 6 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 14 | #include <linux/export.h> |
| 15 | |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 16 | #include <drm/drmP.h> |
| 17 | #include <drm/drm_crtc.h> |
| 18 | #include <drm/drm_crtc_helper.h> |
| 19 | |
| 20 | #include "rcar_du_drv.h" |
| 21 | #include "rcar_du_encoder.h" |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 22 | #include "rcar_du_hdmicon.h" |
| 23 | #include "rcar_du_hdmienc.h" |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 24 | #include "rcar_du_kms.h" |
| 25 | #include "rcar_du_lvdscon.h" |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 26 | #include "rcar_du_lvdsenc.h" |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 27 | #include "rcar_du_vgacon.h" |
| 28 | |
| 29 | /* ----------------------------------------------------------------------------- |
| 30 | * Common connector functions |
| 31 | */ |
| 32 | |
| 33 | struct drm_encoder * |
| 34 | rcar_du_connector_best_encoder(struct drm_connector *connector) |
| 35 | { |
| 36 | struct rcar_du_connector *rcon = to_rcar_connector(connector); |
| 37 | |
Laurent Pinchart | 4b96b70 | 2014-03-31 01:50:16 +0200 | [diff] [blame] | 38 | return rcar_encoder_to_drm_encoder(rcon->encoder); |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* ----------------------------------------------------------------------------- |
| 42 | * Encoder |
| 43 | */ |
| 44 | |
Laurent Pinchart | f6638b6 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 45 | static void rcar_du_encoder_disable(struct drm_encoder *encoder) |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 46 | { |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 47 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
| 48 | |
| 49 | if (renc->lvds) |
Laurent Pinchart | f6638b6 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 50 | rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, false); |
| 51 | } |
| 52 | |
| 53 | static void rcar_du_encoder_enable(struct drm_encoder *encoder) |
| 54 | { |
| 55 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
| 56 | |
| 57 | if (renc->lvds) |
| 58 | rcar_du_lvdsenc_enable(renc->lvds, encoder->crtc, true); |
| 59 | } |
| 60 | |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 61 | static int rcar_du_encoder_atomic_check(struct drm_encoder *encoder, |
| 62 | struct drm_crtc_state *crtc_state, |
| 63 | struct drm_connector_state *conn_state) |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 64 | { |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 65 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 66 | struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; |
| 67 | const struct drm_display_mode *mode = &crtc_state->mode; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 68 | const struct drm_display_mode *panel_mode; |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 69 | struct drm_connector *connector = conn_state->connector; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 70 | struct drm_device *dev = encoder->dev; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 71 | |
| 72 | /* DAC encoders have currently no restriction on the mode. */ |
| 73 | if (encoder->encoder_type == DRM_MODE_ENCODER_DAC) |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 74 | return 0; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 75 | |
| 76 | if (list_empty(&connector->modes)) { |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 77 | dev_dbg(dev->dev, "encoder: empty modes list\n"); |
| 78 | return -EINVAL; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | panel_mode = list_first_entry(&connector->modes, |
| 82 | struct drm_display_mode, head); |
| 83 | |
| 84 | /* We're not allowed to modify the resolution. */ |
| 85 | if (mode->hdisplay != panel_mode->hdisplay || |
| 86 | mode->vdisplay != panel_mode->vdisplay) |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 87 | return -EINVAL; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 88 | |
| 89 | /* The flat panel mode is fixed, just copy it to the adjusted mode. */ |
| 90 | drm_mode_copy(adjusted_mode, panel_mode); |
| 91 | |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 92 | /* The internal LVDS encoder has a clock frequency operating range of |
| 93 | * 30MHz to 150MHz. Clamp the clock accordingly. |
| 94 | */ |
| 95 | if (renc->lvds) |
| 96 | adjusted_mode->clock = clamp(adjusted_mode->clock, |
| 97 | 30000, 150000); |
| 98 | |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 99 | return 0; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 102 | static void rcar_du_encoder_mode_set(struct drm_encoder *encoder, |
| 103 | struct drm_display_mode *mode, |
| 104 | struct drm_display_mode *adjusted_mode) |
| 105 | { |
| 106 | struct rcar_du_encoder *renc = to_rcar_encoder(encoder); |
| 107 | |
| 108 | rcar_du_crtc_route_output(encoder->crtc, renc->output); |
| 109 | } |
| 110 | |
| 111 | static const struct drm_encoder_helper_funcs encoder_helper_funcs = { |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 112 | .mode_set = rcar_du_encoder_mode_set, |
Laurent Pinchart | f6638b6 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 113 | .disable = rcar_du_encoder_disable, |
| 114 | .enable = rcar_du_encoder_enable, |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 115 | .atomic_check = rcar_du_encoder_atomic_check, |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static const struct drm_encoder_funcs encoder_funcs = { |
| 119 | .destroy = drm_encoder_cleanup, |
| 120 | }; |
| 121 | |
| 122 | int rcar_du_encoder_init(struct rcar_du_device *rcdu, |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 123 | enum rcar_du_encoder_type type, |
| 124 | enum rcar_du_output output, |
Laurent Pinchart | 3ea4d5e | 2014-09-17 14:41:57 +0300 | [diff] [blame] | 125 | struct device_node *enc_node, |
| 126 | struct device_node *con_node) |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 127 | { |
| 128 | struct rcar_du_encoder *renc; |
Laurent Pinchart | 4b96b70 | 2014-03-31 01:50:16 +0200 | [diff] [blame] | 129 | struct drm_encoder *encoder; |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 130 | unsigned int encoder_type; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 131 | int ret; |
| 132 | |
| 133 | renc = devm_kzalloc(rcdu->dev, sizeof(*renc), GFP_KERNEL); |
| 134 | if (renc == NULL) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | renc->output = output; |
Laurent Pinchart | 4b96b70 | 2014-03-31 01:50:16 +0200 | [diff] [blame] | 138 | encoder = rcar_encoder_to_drm_encoder(renc); |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 139 | |
Laurent Pinchart | 90374b5 | 2013-06-17 13:48:27 +0200 | [diff] [blame] | 140 | switch (output) { |
| 141 | case RCAR_DU_OUTPUT_LVDS0: |
| 142 | renc->lvds = rcdu->lvds[0]; |
| 143 | break; |
| 144 | |
| 145 | case RCAR_DU_OUTPUT_LVDS1: |
| 146 | renc->lvds = rcdu->lvds[1]; |
| 147 | break; |
| 148 | |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 153 | switch (type) { |
| 154 | case RCAR_DU_ENCODER_VGA: |
| 155 | encoder_type = DRM_MODE_ENCODER_DAC; |
| 156 | break; |
| 157 | case RCAR_DU_ENCODER_LVDS: |
| 158 | encoder_type = DRM_MODE_ENCODER_LVDS; |
| 159 | break; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 160 | case RCAR_DU_ENCODER_HDMI: |
| 161 | encoder_type = DRM_MODE_ENCODER_TMDS; |
| 162 | break; |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 163 | case RCAR_DU_ENCODER_NONE: |
| 164 | default: |
| 165 | /* No external encoder, use the internal encoder type. */ |
| 166 | encoder_type = rcdu->info->routes[output].encoder_type; |
| 167 | break; |
| 168 | } |
| 169 | |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 170 | if (type == RCAR_DU_ENCODER_HDMI) { |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 171 | ret = rcar_du_hdmienc_init(rcdu, renc, enc_node); |
| 172 | if (ret < 0) |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 173 | goto done; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 174 | } else { |
| 175 | ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs, |
| 176 | encoder_type); |
| 177 | if (ret < 0) |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 178 | goto done; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 179 | |
| 180 | drm_encoder_helper_add(encoder, &encoder_helper_funcs); |
| 181 | } |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 182 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 183 | switch (encoder_type) { |
Laurent Pinchart | 2378ad1 | 2014-09-17 02:07:52 +0300 | [diff] [blame] | 184 | case DRM_MODE_ENCODER_LVDS: |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 185 | ret = rcar_du_lvds_connector_init(rcdu, renc, con_node); |
| 186 | break; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 187 | |
Laurent Pinchart | ef67a90 | 2013-06-17 03:13:11 +0200 | [diff] [blame] | 188 | case DRM_MODE_ENCODER_DAC: |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 189 | ret = rcar_du_vga_connector_init(rcdu, renc); |
| 190 | break; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 191 | |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 192 | case DRM_MODE_ENCODER_TMDS: |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 193 | ret = rcar_du_hdmi_connector_init(rcdu, renc); |
| 194 | break; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 195 | |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 196 | default: |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 197 | ret = -EINVAL; |
| 198 | break; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 199 | } |
Laurent Pinchart | 347d761 | 2014-12-11 01:26:04 +0200 | [diff] [blame] | 200 | |
| 201 | done: |
| 202 | if (ret < 0) { |
| 203 | if (encoder->name) |
| 204 | encoder->funcs->destroy(encoder); |
| 205 | devm_kfree(rcdu->dev, renc); |
| 206 | } |
| 207 | |
| 208 | return ret; |
Laurent Pinchart | 6978f12 | 2013-06-15 15:02:12 +0200 | [diff] [blame] | 209 | } |