Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ARC PGU DRM driver. |
| 3 | * |
| 4 | * Copyright (C) 2016 Synopsys, Inc. (www.synopsys.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <drm/drm_crtc_helper.h> |
| 18 | #include <drm/drm_encoder_slave.h> |
| 19 | #include <drm/drm_atomic_helper.h> |
| 20 | |
| 21 | #include "arcpgu.h" |
| 22 | |
| 23 | struct arcpgu_drm_connector { |
| 24 | struct drm_connector connector; |
| 25 | struct drm_encoder_slave *encoder_slave; |
| 26 | }; |
| 27 | |
| 28 | static int arcpgu_drm_connector_get_modes(struct drm_connector *connector) |
| 29 | { |
| 30 | const struct drm_encoder_slave_funcs *sfuncs; |
| 31 | struct drm_encoder_slave *slave; |
| 32 | struct arcpgu_drm_connector *con = |
| 33 | container_of(connector, struct arcpgu_drm_connector, connector); |
| 34 | |
| 35 | slave = con->encoder_slave; |
| 36 | if (slave == NULL) { |
| 37 | dev_err(connector->dev->dev, |
| 38 | "connector_get_modes: cannot find slave encoder for connector\n"); |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | sfuncs = slave->slave_funcs; |
| 43 | if (sfuncs->get_modes == NULL) |
| 44 | return 0; |
| 45 | |
| 46 | return sfuncs->get_modes(&slave->base, connector); |
| 47 | } |
| 48 | |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 49 | static enum drm_connector_status |
| 50 | arcpgu_drm_connector_detect(struct drm_connector *connector, bool force) |
| 51 | { |
| 52 | enum drm_connector_status status = connector_status_unknown; |
| 53 | const struct drm_encoder_slave_funcs *sfuncs; |
| 54 | struct drm_encoder_slave *slave; |
| 55 | |
| 56 | struct arcpgu_drm_connector *con = |
| 57 | container_of(connector, struct arcpgu_drm_connector, connector); |
| 58 | |
| 59 | slave = con->encoder_slave; |
| 60 | if (slave == NULL) { |
| 61 | dev_err(connector->dev->dev, |
| 62 | "connector_detect: cannot find slave encoder for connector\n"); |
| 63 | return status; |
| 64 | } |
| 65 | |
| 66 | sfuncs = slave->slave_funcs; |
| 67 | if (sfuncs && sfuncs->detect) |
| 68 | return sfuncs->detect(&slave->base, connector); |
| 69 | |
| 70 | dev_err(connector->dev->dev, "connector_detect: could not detect slave funcs\n"); |
| 71 | return status; |
| 72 | } |
| 73 | |
| 74 | static void arcpgu_drm_connector_destroy(struct drm_connector *connector) |
| 75 | { |
| 76 | drm_connector_unregister(connector); |
| 77 | drm_connector_cleanup(connector); |
| 78 | } |
| 79 | |
| 80 | static const struct drm_connector_helper_funcs |
| 81 | arcpgu_drm_connector_helper_funcs = { |
| 82 | .get_modes = arcpgu_drm_connector_get_modes, |
Carlos Palminha | 51dacf2 | 2016-02-19 15:30:26 +0300 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static const struct drm_connector_funcs arcpgu_drm_connector_funcs = { |
| 86 | .dpms = drm_helper_connector_dpms, |
| 87 | .reset = drm_atomic_helper_connector_reset, |
| 88 | .detect = arcpgu_drm_connector_detect, |
| 89 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 90 | .destroy = arcpgu_drm_connector_destroy, |
| 91 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 92 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 93 | }; |
| 94 | |
| 95 | static struct drm_encoder_helper_funcs arcpgu_drm_encoder_helper_funcs = { |
| 96 | .dpms = drm_i2c_encoder_dpms, |
| 97 | .mode_fixup = drm_i2c_encoder_mode_fixup, |
| 98 | .mode_set = drm_i2c_encoder_mode_set, |
| 99 | .prepare = drm_i2c_encoder_prepare, |
| 100 | .commit = drm_i2c_encoder_commit, |
| 101 | .detect = drm_i2c_encoder_detect, |
| 102 | }; |
| 103 | |
| 104 | static struct drm_encoder_funcs arcpgu_drm_encoder_funcs = { |
| 105 | .destroy = drm_encoder_cleanup, |
| 106 | }; |
| 107 | |
| 108 | int arcpgu_drm_hdmi_init(struct drm_device *drm, struct device_node *np) |
| 109 | { |
| 110 | struct arcpgu_drm_connector *arcpgu_connector; |
| 111 | struct drm_i2c_encoder_driver *driver; |
| 112 | struct drm_encoder_slave *encoder; |
| 113 | struct drm_connector *connector; |
| 114 | struct i2c_client *i2c_slave; |
| 115 | int ret; |
| 116 | |
| 117 | encoder = devm_kzalloc(drm->dev, sizeof(*encoder), GFP_KERNEL); |
| 118 | if (encoder == NULL) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | i2c_slave = of_find_i2c_device_by_node(np); |
| 122 | if (!i2c_slave || !i2c_get_clientdata(i2c_slave)) { |
| 123 | dev_err(drm->dev, "failed to find i2c slave encoder\n"); |
| 124 | return -EPROBE_DEFER; |
| 125 | } |
| 126 | |
| 127 | if (i2c_slave->dev.driver == NULL) { |
| 128 | dev_err(drm->dev, "failed to find i2c slave driver\n"); |
| 129 | return -EPROBE_DEFER; |
| 130 | } |
| 131 | |
| 132 | driver = |
| 133 | to_drm_i2c_encoder_driver(to_i2c_driver(i2c_slave->dev.driver)); |
| 134 | ret = driver->encoder_init(i2c_slave, drm, encoder); |
| 135 | if (ret) { |
| 136 | dev_err(drm->dev, "failed to initialize i2c encoder slave\n"); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | encoder->base.possible_crtcs = 1; |
| 141 | encoder->base.possible_clones = 0; |
| 142 | ret = drm_encoder_init(drm, &encoder->base, &arcpgu_drm_encoder_funcs, |
| 143 | DRM_MODE_ENCODER_TMDS, NULL); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | drm_encoder_helper_add(&encoder->base, |
| 148 | &arcpgu_drm_encoder_helper_funcs); |
| 149 | |
| 150 | arcpgu_connector = devm_kzalloc(drm->dev, sizeof(*arcpgu_connector), |
| 151 | GFP_KERNEL); |
| 152 | if (!arcpgu_connector) { |
| 153 | ret = -ENOMEM; |
| 154 | goto error_encoder_cleanup; |
| 155 | } |
| 156 | |
| 157 | connector = &arcpgu_connector->connector; |
| 158 | drm_connector_helper_add(connector, &arcpgu_drm_connector_helper_funcs); |
| 159 | ret = drm_connector_init(drm, connector, &arcpgu_drm_connector_funcs, |
| 160 | DRM_MODE_CONNECTOR_HDMIA); |
| 161 | if (ret < 0) { |
| 162 | dev_err(drm->dev, "failed to initialize drm connector\n"); |
| 163 | goto error_encoder_cleanup; |
| 164 | } |
| 165 | |
| 166 | ret = drm_mode_connector_attach_encoder(connector, &encoder->base); |
| 167 | if (ret < 0) { |
| 168 | dev_err(drm->dev, "could not attach connector to encoder\n"); |
| 169 | drm_connector_unregister(connector); |
| 170 | goto error_connector_cleanup; |
| 171 | } |
| 172 | |
| 173 | arcpgu_connector->encoder_slave = encoder; |
| 174 | |
| 175 | return 0; |
| 176 | |
| 177 | error_connector_cleanup: |
| 178 | drm_connector_cleanup(connector); |
| 179 | |
| 180 | error_encoder_cleanup: |
| 181 | drm_encoder_cleanup(&encoder->base); |
| 182 | return ret; |
| 183 | } |