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