sdi.c 8.6 KB
Newer Older
T
Tomi Valkeinen 已提交
1 2
/*
 * Copyright (C) 2009 Nokia Corporation
T
Tomi Valkeinen 已提交
3
 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
T
Tomi Valkeinen 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define DSS_SUBSYS_NAME "SDI"

#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/err.h>
23
#include <linux/regulator/consumer.h>
24
#include <linux/export.h>
25
#include <linux/platform_device.h>
26
#include <linux/string.h>
T
Tomi Valkeinen 已提交
27
#include <linux/of.h>
T
Tomi Valkeinen 已提交
28

29
#include "omapdss.h"
T
Tomi Valkeinen 已提交
30 31
#include "dss.h"

32
struct sdi_device {
33
	struct platform_device *pdev;
34
	struct dss_device *dss;
35

T
Tomi Valkeinen 已提交
36
	bool update_enabled;
37
	struct regulator *vdds_sdi_reg;
T
Tomi Valkeinen 已提交
38

39
	struct dss_lcd_mgr_config mgr_config;
40
	struct videomode vm;
41
	int datapairs;
42

43
	struct omap_dss_device output;
44
};
T
Tomi Valkeinen 已提交
45

46
#define dssdev_to_sdi(dssdev) container_of(dssdev, struct sdi_device, output)
47

48
struct sdi_clk_calc_ctx {
49
	struct sdi_device *sdi;
50 51
	unsigned long pck_min, pck_max;

T
Tomi Valkeinen 已提交
52
	unsigned long fck;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	struct dispc_clock_info dispc_cinfo;
};

static bool dpi_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
		unsigned long pck, void *data)
{
	struct sdi_clk_calc_ctx *ctx = data;

	ctx->dispc_cinfo.lck_div = lckd;
	ctx->dispc_cinfo.pck_div = pckd;
	ctx->dispc_cinfo.lck = lck;
	ctx->dispc_cinfo.pck = pck;

	return true;
}

69
static bool dpi_calc_dss_cb(unsigned long fck, void *data)
70 71 72
{
	struct sdi_clk_calc_ctx *ctx = data;

73
	ctx->fck = fck;
74

75
	return dispc_div_calc(ctx->sdi->dss->dispc, fck,
76 77
			      ctx->pck_min, ctx->pck_max,
			      dpi_calc_dispc_cb, ctx);
78 79
}

80 81 82
static int sdi_calc_clock_div(struct sdi_device *sdi, unsigned long pclk,
			      unsigned long *fck,
			      struct dispc_clock_info *dispc_cinfo)
83 84
{
	int i;
85
	struct sdi_clk_calc_ctx ctx;
86 87 88 89 90 91 92 93 94 95 96 97

	/*
	 * DSS fclk gives us very few possibilities, so finding a good pixel
	 * clock may not be possible. We try multiple times to find the clock,
	 * each time widening the pixel clock range we look for, up to
	 * +/- 1MHz.
	 */

	for (i = 0; i < 10; ++i) {
		bool ok;

		memset(&ctx, 0, sizeof(ctx));
98 99 100

		ctx.sdi = sdi;

101 102 103 104 105 106
		if (pclk > 1000 * i * i * i)
			ctx.pck_min = max(pclk - 1000 * i * i * i, 0lu);
		else
			ctx.pck_min = 0;
		ctx.pck_max = pclk + 1000 * i * i * i;

107
		ok = dss_div_calc(sdi->dss, pclk, ctx.pck_min,
108
				  dpi_calc_dss_cb, &ctx);
109
		if (ok) {
110
			*fck = ctx.fck;
111 112 113 114 115 116 117 118
			*dispc_cinfo = ctx.dispc_cinfo;
			return 0;
		}
	}

	return -EINVAL;
}

119
static void sdi_config_lcd_manager(struct sdi_device *sdi)
T
Tomi Valkeinen 已提交
120
{
121
	sdi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
122

123 124
	sdi->mgr_config.stallmode = false;
	sdi->mgr_config.fifohandcheck = false;
125

126 127
	sdi->mgr_config.video_port_width = 24;
	sdi->mgr_config.lcden_sig_polarity = 1;
128

129
	dss_mgr_set_lcd_config(&sdi->output, &sdi->mgr_config);
T
Tomi Valkeinen 已提交
130 131
}

132
static int sdi_display_enable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
133
{
134 135
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);
	struct videomode *vm = &sdi->vm;
136
	unsigned long fck;
T
Tomi Valkeinen 已提交
137 138 139 140
	struct dispc_clock_info dispc_cinfo;
	unsigned long pck;
	int r;

141
	if (!sdi->output.dispc_channel_connected) {
142
		DSSERR("failed to enable display: no output/manager\n");
143 144 145
		return -ENODEV;
	}

146
	r = regulator_enable(sdi->vdds_sdi_reg);
147
	if (r)
148
		goto err_reg_enable;
149

150
	r = dispc_runtime_get(sdi->dss->dispc);
151 152
	if (r)
		goto err_get_dispc;
T
Tomi Valkeinen 已提交
153 154

	/* 15.5.9.1.2 */
155
	vm->flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE | DISPLAY_FLAGS_SYNC_POSEDGE;
156

157
	r = sdi_calc_clock_div(sdi, vm->pixelclock, &fck, &dispc_cinfo);
T
Tomi Valkeinen 已提交
158
	if (r)
159
		goto err_calc_clock_div;
T
Tomi Valkeinen 已提交
160

161
	sdi->mgr_config.clock_info = dispc_cinfo;
T
Tomi Valkeinen 已提交
162

163
	pck = fck / dispc_cinfo.lck_div / dispc_cinfo.pck_div;
T
Tomi Valkeinen 已提交
164

165
	if (pck != vm->pixelclock) {
166
		DSSWARN("Could not find exact pixel clock. Requested %lu Hz, got %lu Hz\n",
167
			vm->pixelclock, pck);
T
Tomi Valkeinen 已提交
168

169
		vm->pixelclock = pck;
T
Tomi Valkeinen 已提交
170 171 172
	}


173
	dss_mgr_set_timings(&sdi->output, vm);
T
Tomi Valkeinen 已提交
174

175
	r = dss_set_fck_rate(sdi->dss, fck);
T
Tomi Valkeinen 已提交
176
	if (r)
177
		goto err_set_dss_clock_div;
T
Tomi Valkeinen 已提交
178

179
	sdi_config_lcd_manager(sdi);
T
Tomi Valkeinen 已提交
180

T
Tomi Valkeinen 已提交
181 182 183 184 185 186 187 188 189 190 191
	/*
	 * LCLK and PCLK divisors are located in shadow registers, and we
	 * normally write them to DISPC registers when enabling the output.
	 * However, SDI uses pck-free as source clock for its PLL, and pck-free
	 * is affected by the divisors. And as we need the PLL before enabling
	 * the output, we need to write the divisors early.
	 *
	 * It seems just writing to the DISPC register is enough, and we don't
	 * need to care about the shadow register mechanism for pck-free. The
	 * exact reason for this is unknown.
	 */
192 193
	dispc_mgr_set_clock_div(sdi->dss->dispc, sdi->output.dispc_channel,
				&sdi->mgr_config.clock_info);
194

195 196
	dss_sdi_init(sdi->dss, sdi->datapairs);
	r = dss_sdi_enable(sdi->dss);
197
	if (r)
198
		goto err_sdi_enable;
199
	mdelay(2);
T
Tomi Valkeinen 已提交
200

201
	r = dss_mgr_enable(&sdi->output);
202 203
	if (r)
		goto err_mgr_enable;
T
Tomi Valkeinen 已提交
204 205

	return 0;
206

207
err_mgr_enable:
208
	dss_sdi_disable(sdi->dss);
209 210 211
err_sdi_enable:
err_set_dss_clock_div:
err_calc_clock_div:
212
	dispc_runtime_put(sdi->dss->dispc);
213
err_get_dispc:
214
	regulator_disable(sdi->vdds_sdi_reg);
215
err_reg_enable:
T
Tomi Valkeinen 已提交
216 217 218
	return r;
}

219
static void sdi_display_disable(struct omap_dss_device *dssdev)
T
Tomi Valkeinen 已提交
220
{
221 222 223
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);

	dss_mgr_disable(&sdi->output);
T
Tomi Valkeinen 已提交
224

225
	dss_sdi_disable(sdi->dss);
T
Tomi Valkeinen 已提交
226

227
	dispc_runtime_put(sdi->dss->dispc);
T
Tomi Valkeinen 已提交
228

229
	regulator_disable(sdi->vdds_sdi_reg);
T
Tomi Valkeinen 已提交
230 231
}

232
static void sdi_set_timings(struct omap_dss_device *dssdev,
233
			    struct videomode *vm)
234
{
235 236 237
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);

	sdi->vm = *vm;
238 239
}

T
Tomi Valkeinen 已提交
240
static int sdi_check_timings(struct omap_dss_device *dssdev,
241
			     struct videomode *vm)
T
Tomi Valkeinen 已提交
242
{
243
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);
244
	enum omap_channel channel = dssdev->dispc_channel;
T
Tomi Valkeinen 已提交
245

246
	if (!dispc_mgr_timings_ok(sdi->dss->dispc, channel, vm))
T
Tomi Valkeinen 已提交
247 248
		return -EINVAL;

249
	if (vm->pixelclock == 0)
T
Tomi Valkeinen 已提交
250 251 252 253 254
		return -EINVAL;

	return 0;
}

255
static int sdi_init_regulator(struct sdi_device *sdi)
T
Tomi Valkeinen 已提交
256
{
257
	struct regulator *vdds_sdi;
T
Tomi Valkeinen 已提交
258

259
	if (sdi->vdds_sdi_reg)
260
		return 0;
261

262
	vdds_sdi = devm_regulator_get(&sdi->pdev->dev, "vdds_sdi");
263
	if (IS_ERR(vdds_sdi)) {
264 265
		if (PTR_ERR(vdds_sdi) != -EPROBE_DEFER)
			DSSERR("can't get VDDS_SDI regulator\n");
266
		return PTR_ERR(vdds_sdi);
267 268
	}

269
	sdi->vdds_sdi_reg = vdds_sdi;
270

T
Tomi Valkeinen 已提交
271 272 273
	return 0;
}

T
Tomi Valkeinen 已提交
274 275 276
static int sdi_connect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
277
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);
T
Tomi Valkeinen 已提交
278 279
	int r;

280
	r = sdi_init_regulator(sdi);
T
Tomi Valkeinen 已提交
281 282 283
	if (r)
		return r;

284
	r = dss_mgr_connect(&sdi->output, dssdev);
T
Tomi Valkeinen 已提交
285 286 287 288 289 290 291
	if (r)
		return r;

	r = omapdss_output_set_device(dssdev, dst);
	if (r) {
		DSSERR("failed to connect output to new device: %s\n",
				dst->name);
292
		dss_mgr_disconnect(&sdi->output, dssdev);
T
Tomi Valkeinen 已提交
293 294 295 296 297 298 299 300 301
		return r;
	}

	return 0;
}

static void sdi_disconnect(struct omap_dss_device *dssdev,
		struct omap_dss_device *dst)
{
302 303
	struct sdi_device *sdi = dssdev_to_sdi(dssdev);

304
	WARN_ON(dst != dssdev->dst);
T
Tomi Valkeinen 已提交
305

306
	if (dst != dssdev->dst)
T
Tomi Valkeinen 已提交
307 308 309 310
		return;

	omapdss_output_unset_device(dssdev);

311
	dss_mgr_disconnect(&sdi->output, dssdev);
T
Tomi Valkeinen 已提交
312 313 314 315 316 317
}

static const struct omapdss_sdi_ops sdi_ops = {
	.connect = sdi_connect,
	.disconnect = sdi_disconnect,

318 319
	.enable = sdi_display_enable,
	.disable = sdi_display_disable,
T
Tomi Valkeinen 已提交
320 321

	.check_timings = sdi_check_timings,
322
	.set_timings = sdi_set_timings,
T
Tomi Valkeinen 已提交
323 324
};

325
static void sdi_init_output(struct sdi_device *sdi)
326
{
327
	struct omap_dss_device *out = &sdi->output;
328

329
	out->dev = &sdi->pdev->dev;
330
	out->id = OMAP_DSS_OUTPUT_SDI;
331
	out->output_type = OMAP_DISPLAY_TYPE_SDI;
T
Tomi Valkeinen 已提交
332
	out->name = "sdi.0";
333
	out->dispc_channel = OMAP_DSS_CHANNEL_LCD;
T
Tomi Valkeinen 已提交
334 335
	/* We have SDI only on OMAP3, where it's on port 1 */
	out->port_num = 1;
T
Tomi Valkeinen 已提交
336
	out->ops.sdi = &sdi_ops;
337
	out->owner = THIS_MODULE;
338

339
	omapdss_register_output(out);
340 341
}

342
static void sdi_uninit_output(struct sdi_device *sdi)
343
{
344
	omapdss_unregister_output(&sdi->output);
345 346
}

347 348
int sdi_init_port(struct dss_device *dss, struct platform_device *pdev,
		  struct device_node *port)
T
Tomi Valkeinen 已提交
349
{
350
	struct sdi_device *sdi;
T
Tomi Valkeinen 已提交
351 352 353 354
	struct device_node *ep;
	u32 datapairs;
	int r;

355 356 357 358
	sdi = kzalloc(sizeof(*sdi), GFP_KERNEL);
	if (!sdi)
		return -ENOMEM;

359
	ep = of_get_next_child(port, NULL);
360 361 362 363
	if (!ep) {
		r = 0;
		goto err_free;
	}
T
Tomi Valkeinen 已提交
364 365 366 367 368 369 370

	r = of_property_read_u32(ep, "datapairs", &datapairs);
	if (r) {
		DSSERR("failed to parse datapairs\n");
		goto err_datapairs;
	}

371 372
	sdi->datapairs = datapairs;
	sdi->dss = dss;
T
Tomi Valkeinen 已提交
373 374 375

	of_node_put(ep);

376 377
	sdi->pdev = pdev;
	port->data = sdi;
T
Tomi Valkeinen 已提交
378

379
	sdi_init_output(sdi);
T
Tomi Valkeinen 已提交
380 381 382 383 384

	return 0;

err_datapairs:
	of_node_put(ep);
385 386
err_free:
	kfree(sdi);
T
Tomi Valkeinen 已提交
387 388 389 390

	return r;
}

391
void sdi_uninit_port(struct device_node *port)
T
Tomi Valkeinen 已提交
392
{
393 394 395
	struct sdi_device *sdi = port->data;

	if (!sdi)
T
Tomi Valkeinen 已提交
396 397
		return;

398 399
	sdi_uninit_output(sdi);
	kfree(sdi);
T
Tomi Valkeinen 已提交
400
}