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