129e57fabSMaxime Ripard /* 229e57fabSMaxime Ripard * Copyright (C) 2015 Free Electrons 329e57fabSMaxime Ripard * Copyright (C) 2015 NextThing Co 429e57fabSMaxime Ripard * 529e57fabSMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 629e57fabSMaxime Ripard * 729e57fabSMaxime Ripard * This program is free software; you can redistribute it and/or 829e57fabSMaxime Ripard * modify it under the terms of the GNU General Public License as 929e57fabSMaxime Ripard * published by the Free Software Foundation; either version 2 of 1029e57fabSMaxime Ripard * the License, or (at your option) any later version. 1129e57fabSMaxime Ripard */ 1229e57fabSMaxime Ripard 1329e57fabSMaxime Ripard #include <linux/clk.h> 1429e57fabSMaxime Ripard 1529e57fabSMaxime Ripard #include <drm/drmP.h> 1629e57fabSMaxime Ripard #include <drm/drm_atomic_helper.h> 1729e57fabSMaxime Ripard #include <drm/drm_crtc_helper.h> 1829e57fabSMaxime Ripard #include <drm/drm_panel.h> 1929e57fabSMaxime Ripard 2029e57fabSMaxime Ripard #include "sun4i_drv.h" 2129e57fabSMaxime Ripard #include "sun4i_tcon.h" 2229e57fabSMaxime Ripard 2329e57fabSMaxime Ripard struct sun4i_rgb { 2429e57fabSMaxime Ripard struct drm_connector connector; 2529e57fabSMaxime Ripard struct drm_encoder encoder; 2629e57fabSMaxime Ripard 2729e57fabSMaxime Ripard struct sun4i_drv *drv; 2829e57fabSMaxime Ripard }; 2929e57fabSMaxime Ripard 3029e57fabSMaxime Ripard static inline struct sun4i_rgb * 3129e57fabSMaxime Ripard drm_connector_to_sun4i_rgb(struct drm_connector *connector) 3229e57fabSMaxime Ripard { 3329e57fabSMaxime Ripard return container_of(connector, struct sun4i_rgb, 3429e57fabSMaxime Ripard connector); 3529e57fabSMaxime Ripard } 3629e57fabSMaxime Ripard 3729e57fabSMaxime Ripard static inline struct sun4i_rgb * 3829e57fabSMaxime Ripard drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder) 3929e57fabSMaxime Ripard { 4029e57fabSMaxime Ripard return container_of(encoder, struct sun4i_rgb, 4129e57fabSMaxime Ripard encoder); 4229e57fabSMaxime Ripard } 4329e57fabSMaxime Ripard 4429e57fabSMaxime Ripard static int sun4i_rgb_get_modes(struct drm_connector *connector) 4529e57fabSMaxime Ripard { 4629e57fabSMaxime Ripard struct sun4i_rgb *rgb = 4729e57fabSMaxime Ripard drm_connector_to_sun4i_rgb(connector); 4829e57fabSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 4929e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 5029e57fabSMaxime Ripard 5129e57fabSMaxime Ripard return drm_panel_get_modes(tcon->panel); 5229e57fabSMaxime Ripard } 5329e57fabSMaxime Ripard 5429e57fabSMaxime Ripard static int sun4i_rgb_mode_valid(struct drm_connector *connector, 5529e57fabSMaxime Ripard struct drm_display_mode *mode) 5629e57fabSMaxime Ripard { 57bb43d40dSMaxime Ripard struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector); 58bb43d40dSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 59bb43d40dSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 6029e57fabSMaxime Ripard u32 hsync = mode->hsync_end - mode->hsync_start; 6129e57fabSMaxime Ripard u32 vsync = mode->vsync_end - mode->vsync_start; 62bb43d40dSMaxime Ripard unsigned long rate = mode->clock * 1000; 63bb43d40dSMaxime Ripard long rounded_rate; 6429e57fabSMaxime Ripard 6529e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Validating modes...\n"); 6629e57fabSMaxime Ripard 6729e57fabSMaxime Ripard if (hsync < 1) 6829e57fabSMaxime Ripard return MODE_HSYNC_NARROW; 6929e57fabSMaxime Ripard 7029e57fabSMaxime Ripard if (hsync > 0x3ff) 7129e57fabSMaxime Ripard return MODE_HSYNC_WIDE; 7229e57fabSMaxime Ripard 7329e57fabSMaxime Ripard if ((mode->hdisplay < 1) || (mode->htotal < 1)) 7429e57fabSMaxime Ripard return MODE_H_ILLEGAL; 7529e57fabSMaxime Ripard 7629e57fabSMaxime Ripard if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff)) 7729e57fabSMaxime Ripard return MODE_BAD_HVALUE; 7829e57fabSMaxime Ripard 7929e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Horizontal parameters OK\n"); 8029e57fabSMaxime Ripard 8129e57fabSMaxime Ripard if (vsync < 1) 8229e57fabSMaxime Ripard return MODE_VSYNC_NARROW; 8329e57fabSMaxime Ripard 8429e57fabSMaxime Ripard if (vsync > 0x3ff) 8529e57fabSMaxime Ripard return MODE_VSYNC_WIDE; 8629e57fabSMaxime Ripard 8729e57fabSMaxime Ripard if ((mode->vdisplay < 1) || (mode->vtotal < 1)) 8829e57fabSMaxime Ripard return MODE_V_ILLEGAL; 8929e57fabSMaxime Ripard 9029e57fabSMaxime Ripard if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff)) 9129e57fabSMaxime Ripard return MODE_BAD_VVALUE; 9229e57fabSMaxime Ripard 9329e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Vertical parameters OK\n"); 9429e57fabSMaxime Ripard 95bb43d40dSMaxime Ripard rounded_rate = clk_round_rate(tcon->dclk, rate); 96bb43d40dSMaxime Ripard if (rounded_rate < rate) 97bb43d40dSMaxime Ripard return MODE_CLOCK_LOW; 98bb43d40dSMaxime Ripard 99bb43d40dSMaxime Ripard if (rounded_rate > rate) 100bb43d40dSMaxime Ripard return MODE_CLOCK_HIGH; 101bb43d40dSMaxime Ripard 102bb43d40dSMaxime Ripard DRM_DEBUG_DRIVER("Clock rate OK\n"); 103bb43d40dSMaxime Ripard 10429e57fabSMaxime Ripard return MODE_OK; 10529e57fabSMaxime Ripard } 10629e57fabSMaxime Ripard 10729e57fabSMaxime Ripard static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = { 10829e57fabSMaxime Ripard .get_modes = sun4i_rgb_get_modes, 10929e57fabSMaxime Ripard .mode_valid = sun4i_rgb_mode_valid, 11029e57fabSMaxime Ripard }; 11129e57fabSMaxime Ripard 11229e57fabSMaxime Ripard static enum drm_connector_status 11329e57fabSMaxime Ripard sun4i_rgb_connector_detect(struct drm_connector *connector, bool force) 11429e57fabSMaxime Ripard { 11529e57fabSMaxime Ripard return connector_status_connected; 11629e57fabSMaxime Ripard } 11729e57fabSMaxime Ripard 11829e57fabSMaxime Ripard static void 11929e57fabSMaxime Ripard sun4i_rgb_connector_destroy(struct drm_connector *connector) 12029e57fabSMaxime Ripard { 12129e57fabSMaxime Ripard struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector); 12229e57fabSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 12329e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 12429e57fabSMaxime Ripard 12529e57fabSMaxime Ripard drm_panel_detach(tcon->panel); 12629e57fabSMaxime Ripard drm_connector_cleanup(connector); 12729e57fabSMaxime Ripard } 12829e57fabSMaxime Ripard 12929e57fabSMaxime Ripard static struct drm_connector_funcs sun4i_rgb_con_funcs = { 13029e57fabSMaxime Ripard .dpms = drm_atomic_helper_connector_dpms, 13129e57fabSMaxime Ripard .detect = sun4i_rgb_connector_detect, 13229e57fabSMaxime Ripard .fill_modes = drm_helper_probe_single_connector_modes, 13329e57fabSMaxime Ripard .destroy = sun4i_rgb_connector_destroy, 13429e57fabSMaxime Ripard .reset = drm_atomic_helper_connector_reset, 13529e57fabSMaxime Ripard .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 13629e57fabSMaxime Ripard .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 13729e57fabSMaxime Ripard }; 13829e57fabSMaxime Ripard 13929e57fabSMaxime Ripard static int sun4i_rgb_atomic_check(struct drm_encoder *encoder, 14029e57fabSMaxime Ripard struct drm_crtc_state *crtc_state, 14129e57fabSMaxime Ripard struct drm_connector_state *conn_state) 14229e57fabSMaxime Ripard { 14329e57fabSMaxime Ripard return 0; 14429e57fabSMaxime Ripard } 14529e57fabSMaxime Ripard 14629e57fabSMaxime Ripard static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder) 14729e57fabSMaxime Ripard { 14829e57fabSMaxime Ripard struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder); 14929e57fabSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 15029e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 15129e57fabSMaxime Ripard 15229e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Enabling RGB output\n"); 15329e57fabSMaxime Ripard 154*894f5a9fSMaxime Ripard if (!IS_ERR(tcon->panel)) 15529e57fabSMaxime Ripard drm_panel_enable(tcon->panel); 156*894f5a9fSMaxime Ripard 157*894f5a9fSMaxime Ripard if (!IS_ERR(encoder->bridge)) 158*894f5a9fSMaxime Ripard drm_bridge_enable(encoder->bridge); 159*894f5a9fSMaxime Ripard 16029e57fabSMaxime Ripard sun4i_tcon_channel_enable(tcon, 0); 16129e57fabSMaxime Ripard } 16229e57fabSMaxime Ripard 16329e57fabSMaxime Ripard static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder) 16429e57fabSMaxime Ripard { 16529e57fabSMaxime Ripard struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder); 16629e57fabSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 16729e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 16829e57fabSMaxime Ripard 16929e57fabSMaxime Ripard DRM_DEBUG_DRIVER("Disabling RGB output\n"); 17029e57fabSMaxime Ripard 17129e57fabSMaxime Ripard sun4i_tcon_channel_disable(tcon, 0); 172*894f5a9fSMaxime Ripard 173*894f5a9fSMaxime Ripard if (!IS_ERR(encoder->bridge)) 174*894f5a9fSMaxime Ripard drm_bridge_disable(encoder->bridge); 175*894f5a9fSMaxime Ripard 176*894f5a9fSMaxime Ripard if (!IS_ERR(tcon->panel)) 17729e57fabSMaxime Ripard drm_panel_disable(tcon->panel); 17829e57fabSMaxime Ripard } 17929e57fabSMaxime Ripard 18029e57fabSMaxime Ripard static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder, 18129e57fabSMaxime Ripard struct drm_display_mode *mode, 18229e57fabSMaxime Ripard struct drm_display_mode *adjusted_mode) 18329e57fabSMaxime Ripard { 18429e57fabSMaxime Ripard struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder); 18529e57fabSMaxime Ripard struct sun4i_drv *drv = rgb->drv; 18629e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 18729e57fabSMaxime Ripard 18829e57fabSMaxime Ripard sun4i_tcon0_mode_set(tcon, mode); 18929e57fabSMaxime Ripard 19029e57fabSMaxime Ripard clk_set_rate(tcon->dclk, mode->crtc_clock * 1000); 19129e57fabSMaxime Ripard 19229e57fabSMaxime Ripard /* FIXME: This seems to be board specific */ 19329e57fabSMaxime Ripard clk_set_phase(tcon->dclk, 120); 19429e57fabSMaxime Ripard } 19529e57fabSMaxime Ripard 19629e57fabSMaxime Ripard static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = { 19729e57fabSMaxime Ripard .atomic_check = sun4i_rgb_atomic_check, 19829e57fabSMaxime Ripard .mode_set = sun4i_rgb_encoder_mode_set, 19929e57fabSMaxime Ripard .disable = sun4i_rgb_encoder_disable, 20029e57fabSMaxime Ripard .enable = sun4i_rgb_encoder_enable, 20129e57fabSMaxime Ripard }; 20229e57fabSMaxime Ripard 20329e57fabSMaxime Ripard static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder) 20429e57fabSMaxime Ripard { 20529e57fabSMaxime Ripard drm_encoder_cleanup(encoder); 20629e57fabSMaxime Ripard } 20729e57fabSMaxime Ripard 20829e57fabSMaxime Ripard static struct drm_encoder_funcs sun4i_rgb_enc_funcs = { 20929e57fabSMaxime Ripard .destroy = sun4i_rgb_enc_destroy, 21029e57fabSMaxime Ripard }; 21129e57fabSMaxime Ripard 21229e57fabSMaxime Ripard int sun4i_rgb_init(struct drm_device *drm) 21329e57fabSMaxime Ripard { 21429e57fabSMaxime Ripard struct sun4i_drv *drv = drm->dev_private; 21529e57fabSMaxime Ripard struct sun4i_tcon *tcon = drv->tcon; 216*894f5a9fSMaxime Ripard struct drm_encoder *encoder; 21729e57fabSMaxime Ripard struct sun4i_rgb *rgb; 21829e57fabSMaxime Ripard int ret; 21929e57fabSMaxime Ripard 22029e57fabSMaxime Ripard rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL); 22129e57fabSMaxime Ripard if (!rgb) 22229e57fabSMaxime Ripard return -ENOMEM; 22329e57fabSMaxime Ripard rgb->drv = drv; 224*894f5a9fSMaxime Ripard encoder = &rgb->encoder; 22529e57fabSMaxime Ripard 226a8444c7eSMaxime Ripard tcon->panel = sun4i_tcon_find_panel(tcon->dev->of_node); 227*894f5a9fSMaxime Ripard encoder->bridge = sun4i_tcon_find_bridge(tcon->dev->of_node); 228*894f5a9fSMaxime Ripard if (IS_ERR(tcon->panel) && IS_ERR(encoder->bridge)) { 229*894f5a9fSMaxime Ripard dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n"); 230a8444c7eSMaxime Ripard return 0; 231a8444c7eSMaxime Ripard } 232a8444c7eSMaxime Ripard 23329e57fabSMaxime Ripard drm_encoder_helper_add(&rgb->encoder, 23429e57fabSMaxime Ripard &sun4i_rgb_enc_helper_funcs); 23529e57fabSMaxime Ripard ret = drm_encoder_init(drm, 23629e57fabSMaxime Ripard &rgb->encoder, 23729e57fabSMaxime Ripard &sun4i_rgb_enc_funcs, 23829e57fabSMaxime Ripard DRM_MODE_ENCODER_NONE, 23929e57fabSMaxime Ripard NULL); 24029e57fabSMaxime Ripard if (ret) { 24129e57fabSMaxime Ripard dev_err(drm->dev, "Couldn't initialise the rgb encoder\n"); 24229e57fabSMaxime Ripard goto err_out; 24329e57fabSMaxime Ripard } 24429e57fabSMaxime Ripard 24529e57fabSMaxime Ripard /* The RGB encoder can only work with the TCON channel 0 */ 24629e57fabSMaxime Ripard rgb->encoder.possible_crtcs = BIT(0); 24729e57fabSMaxime Ripard 248*894f5a9fSMaxime Ripard if (!IS_ERR(tcon->panel)) { 24929e57fabSMaxime Ripard drm_connector_helper_add(&rgb->connector, 25029e57fabSMaxime Ripard &sun4i_rgb_con_helper_funcs); 25129e57fabSMaxime Ripard ret = drm_connector_init(drm, &rgb->connector, 25229e57fabSMaxime Ripard &sun4i_rgb_con_funcs, 25329e57fabSMaxime Ripard DRM_MODE_CONNECTOR_Unknown); 25429e57fabSMaxime Ripard if (ret) { 25529e57fabSMaxime Ripard dev_err(drm->dev, "Couldn't initialise the rgb connector\n"); 25629e57fabSMaxime Ripard goto err_cleanup_connector; 25729e57fabSMaxime Ripard } 25829e57fabSMaxime Ripard 259*894f5a9fSMaxime Ripard drm_mode_connector_attach_encoder(&rgb->connector, 260*894f5a9fSMaxime Ripard &rgb->encoder); 26129e57fabSMaxime Ripard 262*894f5a9fSMaxime Ripard ret = drm_panel_attach(tcon->panel, &rgb->connector); 263*894f5a9fSMaxime Ripard if (ret) { 264*894f5a9fSMaxime Ripard dev_err(drm->dev, "Couldn't attach our panel\n"); 265*894f5a9fSMaxime Ripard goto err_cleanup_connector; 266*894f5a9fSMaxime Ripard } 267*894f5a9fSMaxime Ripard } 268*894f5a9fSMaxime Ripard 269*894f5a9fSMaxime Ripard if (!IS_ERR(encoder->bridge)) { 270*894f5a9fSMaxime Ripard encoder->bridge->encoder = &rgb->encoder; 271*894f5a9fSMaxime Ripard 272*894f5a9fSMaxime Ripard ret = drm_bridge_attach(drm, encoder->bridge); 273*894f5a9fSMaxime Ripard if (ret) { 274*894f5a9fSMaxime Ripard dev_err(drm->dev, "Couldn't attach our bridge\n"); 275*894f5a9fSMaxime Ripard goto err_cleanup_connector; 276*894f5a9fSMaxime Ripard } 277*894f5a9fSMaxime Ripard } 27829e57fabSMaxime Ripard 27929e57fabSMaxime Ripard return 0; 28029e57fabSMaxime Ripard 28129e57fabSMaxime Ripard err_cleanup_connector: 28229e57fabSMaxime Ripard drm_encoder_cleanup(&rgb->encoder); 28329e57fabSMaxime Ripard err_out: 28429e57fabSMaxime Ripard return ret; 28529e57fabSMaxime Ripard } 28629e57fabSMaxime Ripard EXPORT_SYMBOL(sun4i_rgb_init); 287