1 /* 2 * Copyright 2015 Freescale Semiconductor, Inc. 3 * 4 * Freescale DCU drm device driver 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 as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12 #include <linux/backlight.h> 13 14 #include <drm/drmP.h> 15 #include <drm/drm_atomic_helper.h> 16 #include <drm/drm_crtc_helper.h> 17 #include <drm/drm_panel.h> 18 19 #include "fsl_dcu_drm_drv.h" 20 #include "fsl_tcon.h" 21 22 static int 23 fsl_dcu_drm_encoder_atomic_check(struct drm_encoder *encoder, 24 struct drm_crtc_state *crtc_state, 25 struct drm_connector_state *conn_state) 26 { 27 return 0; 28 } 29 30 static void fsl_dcu_drm_encoder_disable(struct drm_encoder *encoder) 31 { 32 struct drm_device *dev = encoder->dev; 33 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 34 35 if (fsl_dev->tcon) 36 fsl_tcon_bypass_disable(fsl_dev->tcon); 37 } 38 39 static void fsl_dcu_drm_encoder_enable(struct drm_encoder *encoder) 40 { 41 struct drm_device *dev = encoder->dev; 42 struct fsl_dcu_drm_device *fsl_dev = dev->dev_private; 43 44 if (fsl_dev->tcon) 45 fsl_tcon_bypass_enable(fsl_dev->tcon); 46 } 47 48 static const struct drm_encoder_helper_funcs encoder_helper_funcs = { 49 .atomic_check = fsl_dcu_drm_encoder_atomic_check, 50 .disable = fsl_dcu_drm_encoder_disable, 51 .enable = fsl_dcu_drm_encoder_enable, 52 }; 53 54 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder) 55 { 56 drm_encoder_cleanup(encoder); 57 } 58 59 static const struct drm_encoder_funcs encoder_funcs = { 60 .destroy = fsl_dcu_drm_encoder_destroy, 61 }; 62 63 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev, 64 struct drm_crtc *crtc) 65 { 66 struct drm_encoder *encoder = &fsl_dev->encoder; 67 int ret; 68 69 encoder->possible_crtcs = 1; 70 ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs, 71 DRM_MODE_ENCODER_LVDS, NULL); 72 if (ret < 0) 73 return ret; 74 75 drm_encoder_helper_add(encoder, &encoder_helper_funcs); 76 77 return 0; 78 } 79 80 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector) 81 { 82 struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector); 83 84 drm_connector_unregister(connector); 85 drm_panel_detach(fsl_con->panel); 86 drm_connector_cleanup(connector); 87 } 88 89 static enum drm_connector_status 90 fsl_dcu_drm_connector_detect(struct drm_connector *connector, bool force) 91 { 92 return connector_status_connected; 93 } 94 95 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = { 96 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 97 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 98 .destroy = fsl_dcu_drm_connector_destroy, 99 .detect = fsl_dcu_drm_connector_detect, 100 .dpms = drm_atomic_helper_connector_dpms, 101 .fill_modes = drm_helper_probe_single_connector_modes, 102 .reset = drm_atomic_helper_connector_reset, 103 }; 104 105 static struct drm_encoder * 106 fsl_dcu_drm_connector_best_encoder(struct drm_connector *connector) 107 { 108 struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector); 109 110 return fsl_con->encoder; 111 } 112 113 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector) 114 { 115 struct fsl_dcu_drm_connector *fsl_connector; 116 int (*get_modes)(struct drm_panel *panel); 117 int num_modes = 0; 118 119 fsl_connector = to_fsl_dcu_connector(connector); 120 if (fsl_connector->panel && fsl_connector->panel->funcs && 121 fsl_connector->panel->funcs->get_modes) { 122 get_modes = fsl_connector->panel->funcs->get_modes; 123 num_modes = get_modes(fsl_connector->panel); 124 } 125 126 return num_modes; 127 } 128 129 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector, 130 struct drm_display_mode *mode) 131 { 132 if (mode->hdisplay & 0xf) 133 return MODE_ERROR; 134 135 return MODE_OK; 136 } 137 138 static const struct drm_connector_helper_funcs connector_helper_funcs = { 139 .best_encoder = fsl_dcu_drm_connector_best_encoder, 140 .get_modes = fsl_dcu_drm_connector_get_modes, 141 .mode_valid = fsl_dcu_drm_connector_mode_valid, 142 }; 143 144 int fsl_dcu_drm_connector_create(struct fsl_dcu_drm_device *fsl_dev, 145 struct drm_encoder *encoder) 146 { 147 struct drm_connector *connector = &fsl_dev->connector.base; 148 struct drm_mode_config *mode_config = &fsl_dev->drm->mode_config; 149 struct device_node *panel_node; 150 int ret; 151 152 fsl_dev->connector.encoder = encoder; 153 154 ret = drm_connector_init(fsl_dev->drm, connector, 155 &fsl_dcu_drm_connector_funcs, 156 DRM_MODE_CONNECTOR_LVDS); 157 if (ret < 0) 158 return ret; 159 160 drm_connector_helper_add(connector, &connector_helper_funcs); 161 ret = drm_connector_register(connector); 162 if (ret < 0) 163 goto err_cleanup; 164 165 ret = drm_mode_connector_attach_encoder(connector, encoder); 166 if (ret < 0) 167 goto err_sysfs; 168 169 drm_object_property_set_value(&connector->base, 170 mode_config->dpms_property, 171 DRM_MODE_DPMS_OFF); 172 173 panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0); 174 if (!panel_node) { 175 dev_err(fsl_dev->dev, "fsl,panel property not found\n"); 176 ret = -ENODEV; 177 goto err_sysfs; 178 } 179 180 fsl_dev->connector.panel = of_drm_find_panel(panel_node); 181 if (!fsl_dev->connector.panel) { 182 ret = -EPROBE_DEFER; 183 goto err_panel; 184 } 185 of_node_put(panel_node); 186 187 ret = drm_panel_attach(fsl_dev->connector.panel, connector); 188 if (ret) { 189 dev_err(fsl_dev->dev, "failed to attach panel\n"); 190 goto err_sysfs; 191 } 192 193 return 0; 194 195 err_panel: 196 of_node_put(panel_node); 197 err_sysfs: 198 drm_connector_unregister(connector); 199 err_cleanup: 200 drm_connector_cleanup(connector); 201 return ret; 202 } 203