1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Avionic Design GmbH 4 * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved. 5 */ 6 7 #include <linux/clk.h> 8 #include <linux/of.h> 9 10 #include <drm/drm_atomic_helper.h> 11 #include <drm/drm_bridge_connector.h> 12 #include <drm/drm_encoder.h> 13 #include <drm/drm_panel.h> 14 #include <drm/drm_print.h> 15 16 #include "drm.h" 17 #include "dc.h" 18 19 struct tegra_rgb { 20 struct tegra_output output; 21 struct tegra_dc *dc; 22 23 struct clk *pll_d_out0; 24 struct clk *pll_d2_out0; 25 struct clk *clk_parent; 26 struct clk *clk; 27 }; 28 29 static inline struct tegra_rgb *to_rgb(struct tegra_output *output) 30 { 31 return container_of(output, struct tegra_rgb, output); 32 } 33 34 struct reg_entry { 35 unsigned long offset; 36 unsigned long value; 37 }; 38 39 static const struct reg_entry rgb_enable[] = { 40 { DC_COM_PIN_OUTPUT_ENABLE(0), 0x00000000 }, 41 { DC_COM_PIN_OUTPUT_ENABLE(1), 0x00000000 }, 42 { DC_COM_PIN_OUTPUT_ENABLE(2), 0x00000000 }, 43 { DC_COM_PIN_OUTPUT_ENABLE(3), 0x00000000 }, 44 { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 }, 45 { DC_COM_PIN_OUTPUT_POLARITY(1), 0x01000000 }, 46 { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 }, 47 { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 }, 48 { DC_COM_PIN_OUTPUT_DATA(0), 0x00000000 }, 49 { DC_COM_PIN_OUTPUT_DATA(1), 0x00000000 }, 50 { DC_COM_PIN_OUTPUT_DATA(2), 0x00000000 }, 51 { DC_COM_PIN_OUTPUT_DATA(3), 0x00000000 }, 52 { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 }, 53 { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 }, 54 { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 }, 55 { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 }, 56 { DC_COM_PIN_OUTPUT_SELECT(4), 0x00210222 }, 57 { DC_COM_PIN_OUTPUT_SELECT(5), 0x00002200 }, 58 { DC_COM_PIN_OUTPUT_SELECT(6), 0x00020000 }, 59 }; 60 61 static const struct reg_entry rgb_disable[] = { 62 { DC_COM_PIN_OUTPUT_SELECT(6), 0x00000000 }, 63 { DC_COM_PIN_OUTPUT_SELECT(5), 0x00000000 }, 64 { DC_COM_PIN_OUTPUT_SELECT(4), 0x00000000 }, 65 { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 }, 66 { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 }, 67 { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 }, 68 { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 }, 69 { DC_COM_PIN_OUTPUT_DATA(3), 0xaaaaaaaa }, 70 { DC_COM_PIN_OUTPUT_DATA(2), 0xaaaaaaaa }, 71 { DC_COM_PIN_OUTPUT_DATA(1), 0xaaaaaaaa }, 72 { DC_COM_PIN_OUTPUT_DATA(0), 0xaaaaaaaa }, 73 { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 }, 74 { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 }, 75 { DC_COM_PIN_OUTPUT_POLARITY(1), 0x00000000 }, 76 { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 }, 77 { DC_COM_PIN_OUTPUT_ENABLE(3), 0x55555555 }, 78 { DC_COM_PIN_OUTPUT_ENABLE(2), 0x55555555 }, 79 { DC_COM_PIN_OUTPUT_ENABLE(1), 0x55150005 }, 80 { DC_COM_PIN_OUTPUT_ENABLE(0), 0x55555555 }, 81 }; 82 83 static void tegra_dc_write_regs(struct tegra_dc *dc, 84 const struct reg_entry *table, 85 unsigned int num) 86 { 87 unsigned int i; 88 89 for (i = 0; i < num; i++) 90 tegra_dc_writel(dc, table[i].value, table[i].offset); 91 } 92 93 static void tegra_rgb_encoder_disable(struct drm_encoder *encoder) 94 { 95 struct tegra_output *output = encoder_to_output(encoder); 96 struct tegra_rgb *rgb = to_rgb(output); 97 98 tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable)); 99 tegra_dc_commit(rgb->dc); 100 } 101 102 static void tegra_rgb_encoder_enable(struct drm_encoder *encoder) 103 { 104 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; 105 struct tegra_output *output = encoder_to_output(encoder); 106 struct tegra_rgb *rgb = to_rgb(output); 107 u32 value; 108 109 tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable)); 110 111 value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL; 112 tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS); 113 114 /* configure H- and V-sync signal polarities */ 115 value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1)); 116 117 if (mode->flags & DRM_MODE_FLAG_NHSYNC) 118 value |= LHS_OUTPUT_POLARITY_LOW; 119 else 120 value &= ~LHS_OUTPUT_POLARITY_LOW; 121 122 if (mode->flags & DRM_MODE_FLAG_NVSYNC) 123 value |= LVS_OUTPUT_POLARITY_LOW; 124 else 125 value &= ~LVS_OUTPUT_POLARITY_LOW; 126 127 tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1)); 128 129 /* XXX: parameterize? */ 130 value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB | 131 DISP_ORDER_RED_BLUE; 132 tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL); 133 134 tegra_dc_commit(rgb->dc); 135 } 136 137 static bool tegra_rgb_pll_rate_change_allowed(struct tegra_rgb *rgb) 138 { 139 if (!rgb->pll_d2_out0) 140 return false; 141 142 if (!clk_is_match(rgb->clk_parent, rgb->pll_d_out0) && 143 !clk_is_match(rgb->clk_parent, rgb->pll_d2_out0)) 144 return false; 145 146 return true; 147 } 148 149 static int 150 tegra_rgb_encoder_atomic_check(struct drm_encoder *encoder, 151 struct drm_crtc_state *crtc_state, 152 struct drm_connector_state *conn_state) 153 { 154 struct tegra_output *output = encoder_to_output(encoder); 155 struct tegra_dc *dc = to_tegra_dc(conn_state->crtc); 156 unsigned long pclk = crtc_state->mode.clock * 1000; 157 struct tegra_rgb *rgb = to_rgb(output); 158 unsigned int div; 159 int err; 160 161 /* 162 * We may not want to change the frequency of the parent clock, since 163 * it may be a parent for other peripherals. This is due to the fact 164 * that on Tegra20 there's only a single clock dedicated to display 165 * (pll_d_out0), whereas later generations have a second one that can 166 * be used to independently drive a second output (pll_d2_out0). 167 * 168 * As a way to support multiple outputs on Tegra20 as well, pll_p is 169 * typically used as the parent clock for the display controllers. 170 * But this comes at a cost: pll_p is the parent of several other 171 * peripherals, so its frequency shouldn't change out of the blue. 172 * 173 * The best we can do at this point is to use the shift clock divider 174 * and hope that the desired frequency can be matched (or at least 175 * matched sufficiently close that the panel will still work). 176 */ 177 if (tegra_rgb_pll_rate_change_allowed(rgb)) { 178 /* 179 * Set display controller clock to x2 of PCLK in order to 180 * produce higher resolution pulse positions. 181 */ 182 div = 2; 183 pclk *= 2; 184 } else { 185 div = ((clk_get_rate(rgb->clk) * 2) / pclk) - 2; 186 pclk = 0; 187 } 188 189 err = tegra_dc_state_setup_clock(dc, crtc_state, rgb->clk_parent, 190 pclk, div); 191 if (err < 0) { 192 dev_err(output->dev, "failed to setup CRTC state: %d\n", err); 193 return err; 194 } 195 196 return err; 197 } 198 199 static const struct drm_encoder_funcs tegra_rgb_encoder_funcs = { 200 .destroy = drm_encoder_cleanup, 201 }; 202 203 static const struct drm_encoder_helper_funcs tegra_rgb_encoder_helper_funcs = { 204 .disable = tegra_rgb_encoder_disable, 205 .enable = tegra_rgb_encoder_enable, 206 .atomic_check = tegra_rgb_encoder_atomic_check, 207 }; 208 209 static void tegra_dc_of_node_put(void *data) 210 { 211 of_node_put(data); 212 } 213 214 int tegra_dc_rgb_probe(struct tegra_dc *dc) 215 { 216 struct device_node *np; 217 struct tegra_rgb *rgb; 218 int err; 219 220 np = of_get_child_by_name(dc->dev->of_node, "rgb"); 221 if (!np) 222 return -ENODEV; 223 224 err = devm_add_action_or_reset(dc->dev, tegra_dc_of_node_put, np); 225 if (err < 0) 226 return err; 227 228 if (!of_device_is_available(np)) 229 return -ENODEV; 230 231 rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL); 232 if (!rgb) 233 return -ENOMEM; 234 235 rgb->output.dev = dc->dev; 236 rgb->output.of_node = np; 237 rgb->dc = dc; 238 239 err = tegra_output_probe(&rgb->output); 240 if (err < 0) 241 return err; 242 243 rgb->clk = devm_clk_get(dc->dev, NULL); 244 if (IS_ERR(rgb->clk)) { 245 dev_err(dc->dev, "failed to get clock\n"); 246 err = PTR_ERR(rgb->clk); 247 goto remove; 248 } 249 250 rgb->clk_parent = devm_clk_get(dc->dev, "parent"); 251 if (IS_ERR(rgb->clk_parent)) { 252 dev_err(dc->dev, "failed to get parent clock\n"); 253 err = PTR_ERR(rgb->clk_parent); 254 goto remove; 255 } 256 257 err = clk_set_parent(rgb->clk, rgb->clk_parent); 258 if (err < 0) { 259 dev_err(dc->dev, "failed to set parent clock: %d\n", err); 260 goto remove; 261 } 262 263 rgb->pll_d_out0 = clk_get_sys(NULL, "pll_d_out0"); 264 if (IS_ERR(rgb->pll_d_out0)) { 265 err = PTR_ERR(rgb->pll_d_out0); 266 dev_err(dc->dev, "failed to get pll_d_out0: %d\n", err); 267 goto remove; 268 } 269 270 if (dc->soc->has_pll_d2_out0) { 271 rgb->pll_d2_out0 = clk_get_sys(NULL, "pll_d2_out0"); 272 if (IS_ERR(rgb->pll_d2_out0)) { 273 err = PTR_ERR(rgb->pll_d2_out0); 274 dev_err(dc->dev, "failed to get pll_d2_out0: %d\n", err); 275 goto put_pll; 276 } 277 } 278 279 dc->rgb = &rgb->output; 280 281 return 0; 282 283 put_pll: 284 clk_put(rgb->pll_d_out0); 285 remove: 286 tegra_output_remove(&rgb->output); 287 return err; 288 } 289 290 void tegra_dc_rgb_remove(struct tegra_dc *dc) 291 { 292 struct tegra_rgb *rgb; 293 294 if (!dc->rgb) 295 return; 296 297 rgb = to_rgb(dc->rgb); 298 clk_put(rgb->pll_d2_out0); 299 clk_put(rgb->pll_d_out0); 300 301 tegra_output_remove(dc->rgb); 302 dc->rgb = NULL; 303 } 304 305 int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc) 306 { 307 struct tegra_output *output = dc->rgb; 308 struct drm_connector *connector; 309 int err; 310 311 if (!dc->rgb) 312 return -ENODEV; 313 314 err = drm_encoder_init(drm, &output->encoder, &tegra_rgb_encoder_funcs, 315 DRM_MODE_ENCODER_LVDS, NULL); 316 if (err) { 317 drm_err(drm, "failed to initialize encoder: %d\n", err); 318 return err; 319 } 320 321 drm_encoder_helper_add(&output->encoder, 322 &tegra_rgb_encoder_helper_funcs); 323 324 /* 325 * Wrap directly-connected panel into DRM bridge in order to let 326 * DRM core to handle panel for us. 327 */ 328 if (output->panel) { 329 output->bridge = devm_drm_panel_bridge_add(output->dev, 330 output->panel); 331 if (IS_ERR(output->bridge)) { 332 dev_err(output->dev, 333 "failed to wrap panel into bridge: %pe\n", 334 output->bridge); 335 return PTR_ERR(output->bridge); 336 } 337 338 output->panel = NULL; 339 } 340 341 /* 342 * Tegra devices that have LVDS panel utilize LVDS encoder bridge 343 * for converting up to 28 LCD LVTTL lanes into 5/4 LVDS lanes that 344 * go to display panel's receiver. 345 * 346 * Encoder usually have a power-down control which needs to be enabled 347 * in order to transmit data to the panel. Historically devices that 348 * use an older device-tree version didn't model the bridge, assuming 349 * that encoder is turned ON by default, while today's DRM allows us 350 * to model LVDS encoder properly. 351 * 352 * Newer device-trees utilize LVDS encoder bridge, which provides 353 * us with a connector and handles the display panel. 354 * 355 * For older device-trees we wrapped panel into the panel-bridge. 356 */ 357 if (output->bridge) { 358 err = drm_bridge_attach(&output->encoder, output->bridge, 359 NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR); 360 if (err) 361 return err; 362 363 connector = drm_bridge_connector_init(drm, &output->encoder); 364 if (IS_ERR(connector)) { 365 dev_err(output->dev, 366 "failed to initialize bridge connector: %pe\n", 367 connector); 368 return PTR_ERR(connector); 369 } 370 } 371 372 err = tegra_output_init(drm, output); 373 if (err < 0) { 374 dev_err(output->dev, "failed to initialize output: %d\n", err); 375 return err; 376 } 377 378 /* 379 * Other outputs can be attached to either display controller. The RGB 380 * outputs are an exception and work only with their parent display 381 * controller. 382 */ 383 output->encoder.possible_crtcs = drm_crtc_mask(&dc->base); 384 385 return 0; 386 } 387 388 int tegra_dc_rgb_exit(struct tegra_dc *dc) 389 { 390 if (dc->rgb) 391 tegra_output_exit(dc->rgb); 392 393 return 0; 394 } 395