rgb.c 10.0 KB
Newer Older
T
Thierry Reding 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2012 Avionic Design GmbH
 * Copyright (C) 2012 NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/clk.h>

12
#include <drm/drm_atomic_helper.h>
T
Thierry Reding 已提交
13 14
#include <drm/drm_panel.h>

T
Thierry Reding 已提交
15 16 17 18 19
#include "drm.h"
#include "dc.h"

struct tegra_rgb {
	struct tegra_output output;
20
	struct tegra_dc *dc;
21
	bool enabled;
22

T
Thierry Reding 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	struct clk *clk_parent;
	struct clk *clk;
};

static inline struct tegra_rgb *to_rgb(struct tegra_output *output)
{
	return container_of(output, struct tegra_rgb, output);
}

struct reg_entry {
	unsigned long offset;
	unsigned long value;
};

static const struct reg_entry rgb_enable[] = {
	{ DC_COM_PIN_OUTPUT_ENABLE(0),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_ENABLE(1),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_ENABLE(2),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_ENABLE(3),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(1), 0x01000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_DATA(0),     0x00000000 },
	{ DC_COM_PIN_OUTPUT_DATA(1),     0x00000000 },
	{ DC_COM_PIN_OUTPUT_DATA(2),     0x00000000 },
	{ DC_COM_PIN_OUTPUT_DATA(3),     0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(0),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(1),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(2),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(3),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(4),   0x00210222 },
	{ DC_COM_PIN_OUTPUT_SELECT(5),   0x00002200 },
	{ DC_COM_PIN_OUTPUT_SELECT(6),   0x00020000 },
};

static const struct reg_entry rgb_disable[] = {
	{ DC_COM_PIN_OUTPUT_SELECT(6),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(5),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(4),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(3),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(2),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(1),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_SELECT(0),   0x00000000 },
	{ DC_COM_PIN_OUTPUT_DATA(3),     0xaaaaaaaa },
	{ DC_COM_PIN_OUTPUT_DATA(2),     0xaaaaaaaa },
	{ DC_COM_PIN_OUTPUT_DATA(1),     0xaaaaaaaa },
	{ DC_COM_PIN_OUTPUT_DATA(0),     0xaaaaaaaa },
	{ DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(1), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
	{ DC_COM_PIN_OUTPUT_ENABLE(3),   0x55555555 },
	{ DC_COM_PIN_OUTPUT_ENABLE(2),   0x55555555 },
	{ DC_COM_PIN_OUTPUT_ENABLE(1),   0x55150005 },
	{ DC_COM_PIN_OUTPUT_ENABLE(0),   0x55555555 },
};

static void tegra_dc_write_regs(struct tegra_dc *dc,
				const struct reg_entry *table,
				unsigned int num)
{
	unsigned int i;

	for (i = 0; i < num; i++)
		tegra_dc_writel(dc, table[i].value, table[i].offset);
}

T
Thierry Reding 已提交
91 92 93 94 95 96 97
static void tegra_rgb_connector_dpms(struct drm_connector *connector,
				     int mode)
{
}

static const struct drm_connector_funcs tegra_rgb_connector_funcs = {
	.dpms = tegra_rgb_connector_dpms,
98
	.reset = drm_atomic_helper_connector_reset,
T
Thierry Reding 已提交
99 100 101
	.detect = tegra_output_connector_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = tegra_output_connector_destroy,
102
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
103
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
T
Thierry Reding 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
};

static enum drm_mode_status
tegra_rgb_connector_mode_valid(struct drm_connector *connector,
			       struct drm_display_mode *mode)
{
	/*
	 * FIXME: For now, always assume that the mode is okay. There are
	 * unresolved issues with clk_round_rate(), which doesn't always
	 * reliably report whether a frequency can be set or not.
	 */
	return MODE_OK;
}

static const struct drm_connector_helper_funcs tegra_rgb_connector_helper_funcs = {
	.get_modes = tegra_output_connector_get_modes,
	.mode_valid = tegra_rgb_connector_mode_valid,
	.best_encoder = tegra_output_connector_best_encoder,
};

static const struct drm_encoder_funcs tegra_rgb_encoder_funcs = {
	.destroy = tegra_output_encoder_destroy,
};

static void tegra_rgb_encoder_dpms(struct drm_encoder *encoder, int mode)
{
}

static bool tegra_rgb_encoder_mode_fixup(struct drm_encoder *encoder,
					 const struct drm_display_mode *mode,
					 struct drm_display_mode *adjusted)
T
Thierry Reding 已提交
135
{
T
Thierry Reding 已提交
136 137
	struct tegra_output *output = encoder_to_output(encoder);
	unsigned long pclk = mode->clock * 1000;
138
	struct tegra_rgb *rgb = to_rgb(output);
T
Thierry Reding 已提交
139 140
	unsigned int div;
	int err;
T
Thierry Reding 已提交
141

T
Thierry Reding 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	/*
	 * We may not want to change the frequency of the parent clock, since
	 * it may be a parent for other peripherals. This is due to the fact
	 * that on Tegra20 there's only a single clock dedicated to display
	 * (pll_d_out0), whereas later generations have a second one that can
	 * be used to independently drive a second output (pll_d2_out0).
	 *
	 * As a way to support multiple outputs on Tegra20 as well, pll_p is
	 * typically used as the parent clock for the display controllers.
	 * But this comes at a cost: pll_p is the parent of several other
	 * peripherals, so its frequency shouldn't change out of the blue.
	 *
	 * The best we can do at this point is to use the shift clock divider
	 * and hope that the desired frequency can be matched (or at least
	 * matched sufficiently close that the panel will still work).
	 */
	div = ((clk_get_rate(rgb->clk) * 2) / pclk) - 2;

	err = tegra_dc_setup_clock(rgb->dc, rgb->clk_parent, pclk, div);
	if (err < 0) {
		dev_err(output->dev, "failed to setup DC clock: %d\n", err);
		return false;
	}

	return true;
}

static void tegra_rgb_encoder_prepare(struct drm_encoder *encoder)
{
}

static void tegra_rgb_encoder_commit(struct drm_encoder *encoder)
{
}

static void tegra_rgb_encoder_mode_set(struct drm_encoder *encoder,
				       struct drm_display_mode *mode,
				       struct drm_display_mode *adjusted)
{
	struct tegra_output *output = encoder_to_output(encoder);
	struct tegra_rgb *rgb = to_rgb(output);
	u32 value;

	if (output->panel)
		drm_panel_prepare(output->panel);
187

188
	tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
T
Thierry Reding 已提交
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
	tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);

	/* XXX: parameterize? */
	value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
	value &= ~LVS_OUTPUT_POLARITY_LOW;
	value &= ~LHS_OUTPUT_POLARITY_LOW;
	tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));

	/* XXX: parameterize? */
	value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
		DISP_ORDER_RED_BLUE;
	tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);

	/* XXX: parameterize? */
	value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
	tegra_dc_writel(rgb->dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);

	value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_COMMAND);
	value &= ~DISP_CTRL_MODE_MASK;
	value |= DISP_CTRL_MODE_C_DISPLAY;
	tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_COMMAND);

	value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_POWER_CONTROL);
	value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
		 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
	tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_POWER_CONTROL);

218
	tegra_dc_commit(rgb->dc);
219

T
Thierry Reding 已提交
220 221
	if (output->panel)
		drm_panel_enable(output->panel);
T
Thierry Reding 已提交
222 223
}

T
Thierry Reding 已提交
224
static void tegra_rgb_encoder_disable(struct drm_encoder *encoder)
T
Thierry Reding 已提交
225
{
T
Thierry Reding 已提交
226
	struct tegra_output *output = encoder_to_output(encoder);
227
	struct tegra_rgb *rgb = to_rgb(output);
228

T
Thierry Reding 已提交
229 230
	if (output->panel)
		drm_panel_disable(output->panel);
231

232
	tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
T
Thierry Reding 已提交
233

T
Thierry Reding 已提交
234 235
	if (output->panel)
		drm_panel_unprepare(output->panel);
T
Thierry Reding 已提交
236 237
}

T
Thierry Reding 已提交
238 239 240 241 242 243 244
static const struct drm_encoder_helper_funcs tegra_rgb_encoder_helper_funcs = {
	.dpms = tegra_rgb_encoder_dpms,
	.mode_fixup = tegra_rgb_encoder_mode_fixup,
	.prepare = tegra_rgb_encoder_prepare,
	.commit = tegra_rgb_encoder_commit,
	.mode_set = tegra_rgb_encoder_mode_set,
	.disable = tegra_rgb_encoder_disable,
T
Thierry Reding 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
};

int tegra_dc_rgb_probe(struct tegra_dc *dc)
{
	struct device_node *np;
	struct tegra_rgb *rgb;
	int err;

	np = of_get_child_by_name(dc->dev->of_node, "rgb");
	if (!np || !of_device_is_available(np))
		return -ENODEV;

	rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
	if (!rgb)
		return -ENOMEM;

261 262
	rgb->output.dev = dc->dev;
	rgb->output.of_node = np;
263
	rgb->dc = dc;
264

265
	err = tegra_output_probe(&rgb->output);
266 267 268
	if (err < 0)
		return err;

T
Thierry Reding 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	rgb->clk = devm_clk_get(dc->dev, NULL);
	if (IS_ERR(rgb->clk)) {
		dev_err(dc->dev, "failed to get clock\n");
		return PTR_ERR(rgb->clk);
	}

	rgb->clk_parent = devm_clk_get(dc->dev, "parent");
	if (IS_ERR(rgb->clk_parent)) {
		dev_err(dc->dev, "failed to get parent clock\n");
		return PTR_ERR(rgb->clk_parent);
	}

	err = clk_set_parent(rgb->clk, rgb->clk_parent);
	if (err < 0) {
		dev_err(dc->dev, "failed to set parent clock: %d\n", err);
		return err;
	}

	dc->rgb = &rgb->output;

	return 0;
}

292 293 294 295 296
int tegra_dc_rgb_remove(struct tegra_dc *dc)
{
	if (!dc->rgb)
		return 0;

297
	tegra_output_remove(dc->rgb);
T
Thierry Reding 已提交
298 299
	dc->rgb = NULL;

300 301 302
	return 0;
}

T
Thierry Reding 已提交
303 304
int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
{
T
Thierry Reding 已提交
305
	struct tegra_output *output = dc->rgb;
T
Thierry Reding 已提交
306 307 308 309 310
	int err;

	if (!dc->rgb)
		return -ENODEV;

T
Thierry Reding 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	drm_connector_init(drm, &output->connector, &tegra_rgb_connector_funcs,
			   DRM_MODE_CONNECTOR_LVDS);
	drm_connector_helper_add(&output->connector,
				 &tegra_rgb_connector_helper_funcs);
	output->connector.dpms = DRM_MODE_DPMS_OFF;

	drm_encoder_init(drm, &output->encoder, &tegra_rgb_encoder_funcs,
			 DRM_MODE_ENCODER_LVDS);
	drm_encoder_helper_add(&output->encoder,
			       &tegra_rgb_encoder_helper_funcs);

	drm_mode_connector_attach_encoder(&output->connector,
					  &output->encoder);
	drm_connector_register(&output->connector);

326 327 328 329 330 331
	err = tegra_output_init(drm, output);
	if (err < 0) {
		dev_err(output->dev, "failed to initialize output: %d\n", err);
		return err;
	}

T
Thierry Reding 已提交
332
	/*
T
Thierry Reding 已提交
333 334 335
	 * Other outputs can be attached to either display controller. The RGB
	 * outputs are an exception and work only with their parent display
	 * controller.
T
Thierry Reding 已提交
336
	 */
T
Thierry Reding 已提交
337
	output->encoder.possible_crtcs = drm_crtc_mask(&dc->base);
T
Thierry Reding 已提交
338 339 340 341 342 343

	return 0;
}

int tegra_dc_rgb_exit(struct tegra_dc *dc)
{
344 345
	if (dc->rgb)
		tegra_output_exit(dc->rgb);
T
Thierry Reding 已提交
346

347
	return 0;
T
Thierry Reding 已提交
348
}