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_of.h> 18 #include <drm/drm_panel.h> 19 #include <drm/drm_probe_helper.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 int ret; 106 107 fsl_dev->connector.encoder = encoder; 108 109 ret = drm_connector_init(fsl_dev->drm, connector, 110 &fsl_dcu_drm_connector_funcs, 111 DRM_MODE_CONNECTOR_LVDS); 112 if (ret < 0) 113 return ret; 114 115 drm_connector_helper_add(connector, &connector_helper_funcs); 116 ret = drm_connector_register(connector); 117 if (ret < 0) 118 goto err_cleanup; 119 120 ret = drm_connector_attach_encoder(connector, encoder); 121 if (ret < 0) 122 goto err_sysfs; 123 124 ret = drm_panel_attach(panel, connector); 125 if (ret) { 126 dev_err(fsl_dev->dev, "failed to attach panel\n"); 127 goto err_sysfs; 128 } 129 130 return 0; 131 132 err_sysfs: 133 drm_connector_unregister(connector); 134 err_cleanup: 135 drm_connector_cleanup(connector); 136 return ret; 137 } 138 139 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev) 140 { 141 struct device_node *panel_node; 142 struct drm_panel *panel; 143 struct drm_bridge *bridge; 144 int ret; 145 146 /* This is for backward compatibility */ 147 panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0); 148 if (panel_node) { 149 fsl_dev->connector.panel = of_drm_find_panel(panel_node); 150 of_node_put(panel_node); 151 if (IS_ERR(fsl_dev->connector.panel)) 152 return PTR_ERR(fsl_dev->connector.panel); 153 154 return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel); 155 } 156 157 ret = drm_of_find_panel_or_bridge(fsl_dev->np, 0, 0, &panel, &bridge); 158 if (ret) 159 return ret; 160 161 if (panel) { 162 fsl_dev->connector.panel = panel; 163 return fsl_dcu_attach_panel(fsl_dev, panel); 164 } 165 166 return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL); 167 } 168