1 /* 2 * Rockchip SoC DP (Display Port) interface driver. 3 * 4 * Copyright (C) Fuzhou Rockchip Electronics Co., Ltd. 5 * Author: Andy Yan <andy.yan@rock-chips.com> 6 * Yakir Yang <ykk@rock-chips.com> 7 * Jeff Chen <jeff.chen@rock-chips.com> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 */ 14 15 #include <linux/component.h> 16 #include <linux/mfd/syscon.h> 17 #include <linux/of_graph.h> 18 #include <linux/regmap.h> 19 #include <linux/reset.h> 20 #include <linux/clk.h> 21 22 #include <drm/drmP.h> 23 #include <drm/drm_crtc_helper.h> 24 #include <drm/drm_dp_helper.h> 25 #include <drm/drm_of.h> 26 #include <drm/drm_panel.h> 27 28 #include <video/of_videomode.h> 29 #include <video/videomode.h> 30 31 #include <drm/bridge/analogix_dp.h> 32 33 #include "rockchip_drm_drv.h" 34 #include "rockchip_drm_vop.h" 35 36 #define to_dp(nm) container_of(nm, struct rockchip_dp_device, nm) 37 38 /* dp grf register offset */ 39 #define GRF_SOC_CON6 0x025c 40 #define GRF_EDP_LCD_SEL_MASK BIT(5) 41 #define GRF_EDP_SEL_VOP_LIT BIT(5) 42 #define GRF_EDP_SEL_VOP_BIG 0 43 44 struct rockchip_dp_device { 45 struct drm_device *drm_dev; 46 struct device *dev; 47 struct drm_encoder encoder; 48 struct drm_display_mode mode; 49 50 struct clk *pclk; 51 struct regmap *grf; 52 struct reset_control *rst; 53 54 struct analogix_dp_plat_data plat_data; 55 }; 56 57 static int rockchip_dp_pre_init(struct rockchip_dp_device *dp) 58 { 59 reset_control_assert(dp->rst); 60 usleep_range(10, 20); 61 reset_control_deassert(dp->rst); 62 63 return 0; 64 } 65 66 static int rockchip_dp_poweron(struct analogix_dp_plat_data *plat_data) 67 { 68 struct rockchip_dp_device *dp = to_dp(plat_data); 69 int ret; 70 71 ret = clk_prepare_enable(dp->pclk); 72 if (ret < 0) { 73 dev_err(dp->dev, "failed to enable pclk %d\n", ret); 74 return ret; 75 } 76 77 ret = rockchip_dp_pre_init(dp); 78 if (ret < 0) { 79 dev_err(dp->dev, "failed to dp pre init %d\n", ret); 80 return ret; 81 } 82 83 return 0; 84 } 85 86 static int rockchip_dp_powerdown(struct analogix_dp_plat_data *plat_data) 87 { 88 struct rockchip_dp_device *dp = to_dp(plat_data); 89 90 clk_disable_unprepare(dp->pclk); 91 92 return 0; 93 } 94 95 static bool 96 rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder *encoder, 97 const struct drm_display_mode *mode, 98 struct drm_display_mode *adjusted_mode) 99 { 100 /* do nothing */ 101 return true; 102 } 103 104 static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder *encoder, 105 struct drm_display_mode *mode, 106 struct drm_display_mode *adjusted) 107 { 108 /* do nothing */ 109 } 110 111 static void rockchip_dp_drm_encoder_enable(struct drm_encoder *encoder) 112 { 113 struct rockchip_dp_device *dp = to_dp(encoder); 114 int ret; 115 u32 val; 116 117 ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder); 118 if (ret < 0) 119 return; 120 121 if (ret) 122 val = GRF_EDP_SEL_VOP_LIT | (GRF_EDP_LCD_SEL_MASK << 16); 123 else 124 val = GRF_EDP_SEL_VOP_BIG | (GRF_EDP_LCD_SEL_MASK << 16); 125 126 dev_dbg(dp->dev, "vop %s output to dp\n", (ret) ? "LIT" : "BIG"); 127 128 ret = regmap_write(dp->grf, GRF_SOC_CON6, val); 129 if (ret != 0) { 130 dev_err(dp->dev, "Could not write to GRF: %d\n", ret); 131 return; 132 } 133 } 134 135 static void rockchip_dp_drm_encoder_nop(struct drm_encoder *encoder) 136 { 137 /* do nothing */ 138 } 139 140 static int 141 rockchip_dp_drm_encoder_atomic_check(struct drm_encoder *encoder, 142 struct drm_crtc_state *crtc_state, 143 struct drm_connector_state *conn_state) 144 { 145 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state); 146 147 /* 148 * FIXME(Yakir): driver should configure the CRTC output video 149 * mode with the display information which indicated the monitor 150 * support colorimetry. 151 * 152 * But don't know why the CRTC driver seems could only output the 153 * RGBaaa rightly. For example, if connect the "innolux,n116bge" 154 * eDP screen, EDID would indicated that screen only accepted the 155 * 6bpc mode. But if I configure CRTC to RGB666 output, then eDP 156 * screen would show a blue picture (RGB888 show a green picture). 157 * But if I configure CTRC to RGBaaa, and eDP driver still keep 158 * RGB666 input video mode, then screen would works prefect. 159 */ 160 s->output_mode = ROCKCHIP_OUT_MODE_AAAA; 161 s->output_type = DRM_MODE_CONNECTOR_eDP; 162 163 return 0; 164 } 165 166 static struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs = { 167 .mode_fixup = rockchip_dp_drm_encoder_mode_fixup, 168 .mode_set = rockchip_dp_drm_encoder_mode_set, 169 .enable = rockchip_dp_drm_encoder_enable, 170 .disable = rockchip_dp_drm_encoder_nop, 171 .atomic_check = rockchip_dp_drm_encoder_atomic_check, 172 }; 173 174 static void rockchip_dp_drm_encoder_destroy(struct drm_encoder *encoder) 175 { 176 drm_encoder_cleanup(encoder); 177 } 178 179 static struct drm_encoder_funcs rockchip_dp_encoder_funcs = { 180 .destroy = rockchip_dp_drm_encoder_destroy, 181 }; 182 183 static int rockchip_dp_init(struct rockchip_dp_device *dp) 184 { 185 struct device *dev = dp->dev; 186 struct device_node *np = dev->of_node; 187 int ret; 188 189 dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); 190 if (IS_ERR(dp->grf)) { 191 dev_err(dev, "failed to get rockchip,grf property\n"); 192 return PTR_ERR(dp->grf); 193 } 194 195 dp->pclk = devm_clk_get(dev, "pclk"); 196 if (IS_ERR(dp->pclk)) { 197 dev_err(dev, "failed to get pclk property\n"); 198 return PTR_ERR(dp->pclk); 199 } 200 201 dp->rst = devm_reset_control_get(dev, "dp"); 202 if (IS_ERR(dp->rst)) { 203 dev_err(dev, "failed to get dp reset control\n"); 204 return PTR_ERR(dp->rst); 205 } 206 207 ret = clk_prepare_enable(dp->pclk); 208 if (ret < 0) { 209 dev_err(dp->dev, "failed to enable pclk %d\n", ret); 210 return ret; 211 } 212 213 ret = rockchip_dp_pre_init(dp); 214 if (ret < 0) { 215 dev_err(dp->dev, "failed to pre init %d\n", ret); 216 return ret; 217 } 218 219 return 0; 220 } 221 222 static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device *dp) 223 { 224 struct drm_encoder *encoder = &dp->encoder; 225 struct drm_device *drm_dev = dp->drm_dev; 226 struct device *dev = dp->dev; 227 int ret; 228 229 encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev, 230 dev->of_node); 231 DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs); 232 233 ret = drm_encoder_init(drm_dev, encoder, &rockchip_dp_encoder_funcs, 234 DRM_MODE_ENCODER_TMDS, NULL); 235 if (ret) { 236 DRM_ERROR("failed to initialize encoder with drm\n"); 237 return ret; 238 } 239 240 drm_encoder_helper_add(encoder, &rockchip_dp_encoder_helper_funcs); 241 242 return 0; 243 } 244 245 static int rockchip_dp_bind(struct device *dev, struct device *master, 246 void *data) 247 { 248 struct rockchip_dp_device *dp = dev_get_drvdata(dev); 249 struct drm_device *drm_dev = data; 250 int ret; 251 252 /* 253 * Just like the probe function said, we don't need the 254 * device drvrate anymore, we should leave the charge to 255 * analogix dp driver, set the device drvdata to NULL. 256 */ 257 dev_set_drvdata(dev, NULL); 258 259 ret = rockchip_dp_init(dp); 260 if (ret < 0) 261 return ret; 262 263 dp->drm_dev = drm_dev; 264 265 ret = rockchip_dp_drm_create_encoder(dp); 266 if (ret) { 267 DRM_ERROR("failed to create drm encoder\n"); 268 return ret; 269 } 270 271 dp->plat_data.encoder = &dp->encoder; 272 273 dp->plat_data.dev_type = RK3288_DP; 274 dp->plat_data.power_on = rockchip_dp_poweron; 275 dp->plat_data.power_off = rockchip_dp_powerdown; 276 277 return analogix_dp_bind(dev, dp->drm_dev, &dp->plat_data); 278 } 279 280 static void rockchip_dp_unbind(struct device *dev, struct device *master, 281 void *data) 282 { 283 return analogix_dp_unbind(dev, master, data); 284 } 285 286 static const struct component_ops rockchip_dp_component_ops = { 287 .bind = rockchip_dp_bind, 288 .unbind = rockchip_dp_unbind, 289 }; 290 291 static int rockchip_dp_probe(struct platform_device *pdev) 292 { 293 struct device *dev = &pdev->dev; 294 struct device_node *panel_node, *port, *endpoint; 295 struct rockchip_dp_device *dp; 296 struct drm_panel *panel; 297 298 port = of_graph_get_port_by_id(dev->of_node, 1); 299 if (!port) { 300 dev_err(dev, "can't find output port\n"); 301 return -EINVAL; 302 } 303 304 endpoint = of_get_child_by_name(port, "endpoint"); 305 of_node_put(port); 306 if (!endpoint) { 307 dev_err(dev, "no output endpoint found\n"); 308 return -EINVAL; 309 } 310 311 panel_node = of_graph_get_remote_port_parent(endpoint); 312 of_node_put(endpoint); 313 if (!panel_node) { 314 dev_err(dev, "no output node found\n"); 315 return -EINVAL; 316 } 317 318 panel = of_drm_find_panel(panel_node); 319 if (!panel) { 320 DRM_ERROR("failed to find panel\n"); 321 of_node_put(panel_node); 322 return -EPROBE_DEFER; 323 } 324 325 of_node_put(panel_node); 326 327 dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL); 328 if (!dp) 329 return -ENOMEM; 330 331 dp->dev = dev; 332 333 dp->plat_data.panel = panel; 334 335 /* 336 * We just use the drvdata until driver run into component 337 * add function, and then we would set drvdata to null, so 338 * that analogix dp driver could take charge of the drvdata. 339 */ 340 platform_set_drvdata(pdev, dp); 341 342 return component_add(dev, &rockchip_dp_component_ops); 343 } 344 345 static int rockchip_dp_remove(struct platform_device *pdev) 346 { 347 component_del(&pdev->dev, &rockchip_dp_component_ops); 348 349 return 0; 350 } 351 352 #ifdef CONFIG_PM_SLEEP 353 static int rockchip_dp_suspend(struct device *dev) 354 { 355 return analogix_dp_suspend(dev); 356 } 357 358 static int rockchip_dp_resume(struct device *dev) 359 { 360 return analogix_dp_resume(dev); 361 } 362 #endif 363 364 static const struct dev_pm_ops rockchip_dp_pm_ops = { 365 SET_SYSTEM_SLEEP_PM_OPS(rockchip_dp_suspend, rockchip_dp_resume) 366 }; 367 368 static const struct of_device_id rockchip_dp_dt_ids[] = { 369 {.compatible = "rockchip,rk3288-dp",}, 370 {} 371 }; 372 MODULE_DEVICE_TABLE(of, rockchip_dp_dt_ids); 373 374 static struct platform_driver rockchip_dp_driver = { 375 .probe = rockchip_dp_probe, 376 .remove = rockchip_dp_remove, 377 .driver = { 378 .name = "rockchip-dp", 379 .owner = THIS_MODULE, 380 .pm = &rockchip_dp_pm_ops, 381 .of_match_table = of_match_ptr(rockchip_dp_dt_ids), 382 }, 383 }; 384 385 module_platform_driver(rockchip_dp_driver); 386 387 MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>"); 388 MODULE_AUTHOR("Jeff chen <jeff.chen@rock-chips.com>"); 389 MODULE_DESCRIPTION("Rockchip Specific Analogix-DP Driver Extension"); 390 MODULE_LICENSE("GPL v2"); 391