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