Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * R-Car Display Unit HDMI Encoder |
| 3 | * |
| 4 | * Copyright (C) 2014 Renesas Electronics Corporation |
| 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 | |
| 14 | #include <linux/slab.h> |
| 15 | |
| 16 | #include <drm/drmP.h> |
| 17 | #include <drm/drm_crtc.h> |
| 18 | #include <drm/drm_crtc_helper.h> |
| 19 | #include <drm/drm_encoder_slave.h> |
| 20 | |
| 21 | #include "rcar_du_drv.h" |
| 22 | #include "rcar_du_encoder.h" |
| 23 | #include "rcar_du_hdmienc.h" |
Laurent Pinchart | 5e433b6 | 2014-10-26 01:14:27 +0300 | [diff] [blame] | 24 | #include "rcar_du_lvdsenc.h" |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 25 | |
| 26 | struct rcar_du_hdmienc { |
| 27 | struct rcar_du_encoder *renc; |
| 28 | struct device *dev; |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 29 | bool enabled; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | #define to_rcar_hdmienc(e) (to_rcar_encoder(e)->hdmi) |
| 33 | #define to_slave_funcs(e) (to_rcar_encoder(e)->slave.slave_funcs) |
| 34 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 35 | static void rcar_du_hdmienc_disable(struct drm_encoder *encoder) |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 36 | { |
| 37 | struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder); |
| 38 | struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder); |
| 39 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 40 | if (sfuncs->dpms) |
| 41 | sfuncs->dpms(encoder, DRM_MODE_DPMS_OFF); |
Laurent Pinchart | 3dbf11e | 2014-12-09 13:19:10 +0200 | [diff] [blame] | 42 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 43 | if (hdmienc->renc->lvds) |
| 44 | rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc, |
| 45 | false); |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 46 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 47 | hdmienc->enabled = false; |
| 48 | } |
| 49 | |
| 50 | static void rcar_du_hdmienc_enable(struct drm_encoder *encoder) |
| 51 | { |
| 52 | struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder); |
| 53 | struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder); |
| 54 | |
| 55 | if (hdmienc->renc->lvds) |
Laurent Pinchart | 44ef7ed | 2015-02-20 14:59:58 +0200 | [diff] [blame] | 56 | rcar_du_lvdsenc_enable(hdmienc->renc->lvds, encoder->crtc, |
| 57 | true); |
Laurent Pinchart | 5e433b6 | 2014-10-26 01:14:27 +0300 | [diff] [blame] | 58 | |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 59 | if (sfuncs->dpms) |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 60 | sfuncs->dpms(encoder, DRM_MODE_DPMS_ON); |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 61 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 62 | hdmienc->enabled = true; |
| 63 | } |
Laurent Pinchart | 5e433b6 | 2014-10-26 01:14:27 +0300 | [diff] [blame] | 64 | |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 65 | static int rcar_du_hdmienc_atomic_check(struct drm_encoder *encoder, |
| 66 | struct drm_crtc_state *crtc_state, |
| 67 | struct drm_connector_state *conn_state) |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 68 | { |
Laurent Pinchart | 5e433b6 | 2014-10-26 01:14:27 +0300 | [diff] [blame] | 69 | struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder); |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 70 | struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder); |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 71 | struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode; |
| 72 | const struct drm_display_mode *mode = &crtc_state->mode; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 73 | |
Laurent Pinchart | 5e433b6 | 2014-10-26 01:14:27 +0300 | [diff] [blame] | 74 | /* The internal LVDS encoder has a clock frequency operating range of |
| 75 | * 30MHz to 150MHz. Clamp the clock accordingly. |
| 76 | */ |
| 77 | if (hdmienc->renc->lvds) |
| 78 | adjusted_mode->clock = clamp(adjusted_mode->clock, |
| 79 | 30000, 150000); |
| 80 | |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 81 | if (sfuncs->mode_fixup == NULL) |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 82 | return 0; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 83 | |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 84 | return sfuncs->mode_fixup(encoder, mode, adjusted_mode) ? 0 : -EINVAL; |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 87 | static void rcar_du_hdmienc_mode_set(struct drm_encoder *encoder, |
| 88 | struct drm_display_mode *mode, |
| 89 | struct drm_display_mode *adjusted_mode) |
| 90 | { |
| 91 | struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder); |
| 92 | struct drm_encoder_slave_funcs *sfuncs = to_slave_funcs(encoder); |
| 93 | |
| 94 | if (sfuncs->mode_set) |
| 95 | sfuncs->mode_set(encoder, mode, adjusted_mode); |
| 96 | |
| 97 | rcar_du_crtc_route_output(encoder->crtc, hdmienc->renc->output); |
| 98 | } |
| 99 | |
| 100 | static const struct drm_encoder_helper_funcs encoder_helper_funcs = { |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 101 | .mode_set = rcar_du_hdmienc_mode_set, |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 102 | .disable = rcar_du_hdmienc_disable, |
| 103 | .enable = rcar_du_hdmienc_enable, |
Laurent Pinchart | ede7714 | 2015-02-22 21:02:39 +0200 | [diff] [blame] | 104 | .atomic_check = rcar_du_hdmienc_atomic_check, |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | static void rcar_du_hdmienc_cleanup(struct drm_encoder *encoder) |
| 108 | { |
| 109 | struct rcar_du_hdmienc *hdmienc = to_rcar_hdmienc(encoder); |
| 110 | |
Laurent Pinchart | 58706b8 | 2015-02-20 14:05:21 +0200 | [diff] [blame] | 111 | if (hdmienc->enabled) |
| 112 | rcar_du_hdmienc_disable(encoder); |
Laurent Pinchart | 637e619 | 2014-03-30 21:55:38 +0200 | [diff] [blame] | 113 | |
| 114 | drm_encoder_cleanup(encoder); |
| 115 | put_device(hdmienc->dev); |
| 116 | } |
| 117 | |
| 118 | static const struct drm_encoder_funcs encoder_funcs = { |
| 119 | .destroy = rcar_du_hdmienc_cleanup, |
| 120 | }; |
| 121 | |
| 122 | int rcar_du_hdmienc_init(struct rcar_du_device *rcdu, |
| 123 | struct rcar_du_encoder *renc, struct device_node *np) |
| 124 | { |
| 125 | struct drm_encoder *encoder = rcar_encoder_to_drm_encoder(renc); |
| 126 | struct drm_i2c_encoder_driver *driver; |
| 127 | struct i2c_client *i2c_slave; |
| 128 | struct rcar_du_hdmienc *hdmienc; |
| 129 | int ret; |
| 130 | |
| 131 | hdmienc = devm_kzalloc(rcdu->dev, sizeof(*hdmienc), GFP_KERNEL); |
| 132 | if (hdmienc == NULL) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | /* Locate the slave I2C device and driver. */ |
| 136 | i2c_slave = of_find_i2c_device_by_node(np); |
| 137 | if (!i2c_slave || !i2c_get_clientdata(i2c_slave)) |
| 138 | return -EPROBE_DEFER; |
| 139 | |
| 140 | hdmienc->dev = &i2c_slave->dev; |
| 141 | |
| 142 | if (hdmienc->dev->driver == NULL) { |
| 143 | ret = -EPROBE_DEFER; |
| 144 | goto error; |
| 145 | } |
| 146 | |
| 147 | /* Initialize the slave encoder. */ |
| 148 | driver = to_drm_i2c_encoder_driver(to_i2c_driver(hdmienc->dev->driver)); |
| 149 | ret = driver->encoder_init(i2c_slave, rcdu->ddev, &renc->slave); |
| 150 | if (ret < 0) |
| 151 | goto error; |
| 152 | |
| 153 | ret = drm_encoder_init(rcdu->ddev, encoder, &encoder_funcs, |
| 154 | DRM_MODE_ENCODER_TMDS); |
| 155 | if (ret < 0) |
| 156 | goto error; |
| 157 | |
| 158 | drm_encoder_helper_add(encoder, &encoder_helper_funcs); |
| 159 | |
| 160 | renc->hdmi = hdmienc; |
| 161 | hdmienc->renc = renc; |
| 162 | |
| 163 | return 0; |
| 164 | |
| 165 | error: |
| 166 | put_device(hdmienc->dev); |
| 167 | return ret; |
| 168 | } |