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 #include <linux/of_graph.h> 14 15 #include <drm/drmP.h> 16 #include <drm/drm_atomic_helper.h> 17 #include <drm/drm_crtc_helper.h> 18 #include <drm/drm_of.h> 19 #include <drm/drm_panel.h> 20 21 #include "fsl_dcu_drm_drv.h" 22 #include "fsl_tcon.h" 23 24 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder) 25 { 26 drm_encoder_cleanup(encoder); 27 } 28 29 static const struct drm_encoder_funcs encoder_funcs = { 30 .destroy = fsl_dcu_drm_encoder_destroy, 31 }; 32 33 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev, 34 struct drm_crtc *crtc) 35 { 36 struct drm_encoder *encoder = &fsl_dev->encoder; 37 int ret; 38 39 encoder->possible_crtcs = 1; 40 41 /* Use bypass mode for parallel RGB/LVDS encoder */ 42 if (fsl_dev->tcon) 43 fsl_tcon_bypass_enable(fsl_dev->tcon); 44 45 ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs, 46 DRM_MODE_ENCODER_LVDS, NULL); 47 if (ret < 0) 48 return ret; 49 50 return 0; 51 } 52 53 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector) 54 { 55 struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector); 56 57 drm_connector_unregister(connector); 58 drm_panel_detach(fsl_con->panel); 59 drm_connector_cleanup(connector); 60 } 61 62 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = { 63 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 64 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 65 .destroy = fsl_dcu_drm_connector_destroy, 66 .fill_modes = drm_helper_probe_single_connector_modes, 67 .reset = drm_atomic_helper_connector_reset, 68 }; 69 70 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector) 71 { 72 struct fsl_dcu_drm_connector *fsl_connector; 73 int (*get_modes)(struct drm_panel *panel); 74 int num_modes = 0; 75 76 fsl_connector = to_fsl_dcu_connector(connector); 77 if (fsl_connector->panel && fsl_connector->panel->funcs && 78 fsl_connector->panel->funcs->get_modes) { 79 get_modes = fsl_connector->panel->funcs->get_modes; 80 num_modes = get_modes(fsl_connector->panel); 81 } 82 83 return num_modes; 84 } 85 86 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector, 87 struct drm_display_mode *mode) 88 { 89 if (mode->hdisplay & 0xf) 90 return MODE_ERROR; 91 92 return MODE_OK; 93 } 94 95 static const struct drm_connector_helper_funcs connector_helper_funcs = { 96 .get_modes = fsl_dcu_drm_connector_get_modes, 97 .mode_valid = fsl_dcu_drm_connector_mode_valid, 98 }; 99 100 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev, 101 struct drm_panel *panel) 102 { 103 struct drm_encoder *encoder = &fsl_dev->encoder; 104 struct drm_connector *connector = &fsl_dev->connector.base; 105 struct drm_mode_config *mode_config = &fsl_dev->drm->mode_config; 106 int ret; 107 108 fsl_dev->connector.encoder = encoder; 109 110 ret = drm_connector_init(fsl_dev->drm, connector, 111 &fsl_dcu_drm_connector_funcs, 112 DRM_MODE_CONNECTOR_LVDS); 113 if (ret < 0) 114 return ret; 115 116 drm_connector_helper_add(connector, &connector_helper_funcs); 117 ret = drm_connector_register(connector); 118 if (ret < 0) 119 goto err_cleanup; 120 121 ret = drm_mode_connector_attach_encoder(connector, encoder); 122 if (ret < 0) 123 goto err_sysfs; 124 125 drm_object_property_set_value(&connector->base, 126 mode_config->dpms_property, 127 DRM_MODE_DPMS_OFF); 128 129 ret = drm_panel_attach(panel, connector); 130 if (ret) { 131 dev_err(fsl_dev->dev, "failed to attach panel\n"); 132 goto err_sysfs; 133 } 134 135 return 0; 136 137 err_sysfs: 138 drm_connector_unregister(connector); 139 err_cleanup: 140 drm_connector_cleanup(connector); 141 return ret; 142 } 143 144 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev) 145 { 146 struct device_node *panel_node; 147 struct drm_panel *panel; 148 struct drm_bridge *bridge; 149 int ret; 150 151 /* This is for backward compatibility */ 152 panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0); 153 if (panel_node) { 154 fsl_dev->connector.panel = of_drm_find_panel(panel_node); 155 of_node_put(panel_node); 156 if (!fsl_dev->connector.panel) 157 return -EPROBE_DEFER; 158 return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel); 159 } 160 161 ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge); 162 if (ret) 163 return ret; 164 165 if (panel) { 166 fsl_dev->connector.panel = panel; 167 return fsl_dcu_attach_panel(fsl_dev, panel); 168 } 169 170 return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL); 171 } 172