sh_mobile_lcdcfb.c 42.9 KB
Newer Older
M
Magnus Damm 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * SuperH Mobile LCDC Framebuffer
 *
 * Copyright (c) 2008 Magnus Damm
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/clk.h>
16
#include <linux/pm_runtime.h>
M
Magnus Damm 已提交
17 18
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
19
#include <linux/interrupt.h>
20
#include <linux/vmalloc.h>
21
#include <linux/ioctl.h>
22
#include <linux/slab.h>
23
#include <linux/console.h>
24 25
#include <linux/backlight.h>
#include <linux/gpio.h>
26
#include <video/sh_mobile_lcdc.h>
A
Arun Sharma 已提交
27
#include <linux/atomic.h>
M
Magnus Damm 已提交
28

29
#include "sh_mobile_lcdcfb.h"
30
#include "sh_mobile_meram.h"
31

32 33
#define SIDE_B_OFFSET 0x1000
#define MIRROR_OFFSET 0x2000
M
Magnus Damm 已提交
34 35 36 37 38 39 40 41

/* shared registers */
#define _LDDCKR 0x410
#define _LDDCKSTPR 0x414
#define _LDINTR 0x468
#define _LDSR 0x46c
#define _LDCNT1R 0x470
#define _LDCNT2R 0x474
42
#define _LDRCNTR 0x478
M
Magnus Damm 已提交
43 44 45 46 47 48
#define _LDDDSR 0x47c
#define _LDDWD0R 0x800
#define _LDDRDR 0x840
#define _LDDWAR 0x900
#define _LDDRAR 0x904

49 50 51 52 53 54 55 56 57 58 59
/* shared registers and their order for context save/restore */
static int lcdc_shared_regs[] = {
	_LDDCKR,
	_LDDCKSTPR,
	_LDINTR,
	_LDDDSR,
	_LDCNT1R,
	_LDCNT2R,
};
#define NR_SHARED_REGS ARRAY_SIZE(lcdc_shared_regs)

60 61
#define MAX_XRES 1920
#define MAX_YRES 1080
M
Magnus Damm 已提交
62

63
static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
M
Magnus Damm 已提交
64 65 66 67 68 69 70
	[LDDCKPAT1R] = 0x400,
	[LDDCKPAT2R] = 0x404,
	[LDMT1R] = 0x418,
	[LDMT2R] = 0x41c,
	[LDMT3R] = 0x420,
	[LDDFR] = 0x424,
	[LDSM1R] = 0x428,
71
	[LDSM2R] = 0x42c,
M
Magnus Damm 已提交
72
	[LDSA1R] = 0x430,
73
	[LDSA2R] = 0x434,
M
Magnus Damm 已提交
74 75 76 77 78 79
	[LDMLSR] = 0x438,
	[LDHCNR] = 0x448,
	[LDHSYNR] = 0x44c,
	[LDVLNR] = 0x450,
	[LDVSYNR] = 0x454,
	[LDPMR] = 0x460,
80
	[LDHAJR] = 0x4a0,
M
Magnus Damm 已提交
81 82
};

83
static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
M
Magnus Damm 已提交
84 85 86 87 88 89 90
	[LDDCKPAT1R] = 0x408,
	[LDDCKPAT2R] = 0x40c,
	[LDMT1R] = 0x600,
	[LDMT2R] = 0x604,
	[LDMT3R] = 0x608,
	[LDDFR] = 0x60c,
	[LDSM1R] = 0x610,
91
	[LDSM2R] = 0x614,
M
Magnus Damm 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
	[LDSA1R] = 0x618,
	[LDMLSR] = 0x620,
	[LDHCNR] = 0x624,
	[LDHSYNR] = 0x628,
	[LDVLNR] = 0x62c,
	[LDVSYNR] = 0x630,
	[LDPMR] = 0x63c,
};

#define START_LCDC	0x00000001
#define LCDC_RESET	0x00000100
#define DISPLAY_BEU	0x00000008
#define LCDC_ENABLE	0x00000001
105
#define LDINTR_FE	0x00000400
106 107
#define LDINTR_VSE	0x00000200
#define LDINTR_VEE	0x00000100
108
#define LDINTR_FS	0x00000004
109 110
#define LDINTR_VSS	0x00000002
#define LDINTR_VES	0x00000001
111 112 113 114
#define LDRCNTR_SRS	0x00020000
#define LDRCNTR_SRC	0x00010000
#define LDRCNTR_MRS	0x00000002
#define LDRCNTR_MRC	0x00000001
115
#define LDSR_MRS	0x00000100
M
Magnus Damm 已提交
116

117 118 119 120 121
static const struct fb_videomode default_720p = {
	.name = "HDMI 720p",
	.xres = 1280,
	.yres = 720,

122 123 124
	.left_margin = 220,
	.right_margin = 110,
	.hsync_len = 40,
125 126 127 128 129 130

	.upper_margin = 20,
	.lower_margin = 5,
	.vsync_len = 5,

	.pixclock = 13468,
131
	.refresh = 60,
132
	.sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
133 134 135 136 137 138 139 140 141 142
};

struct sh_mobile_lcdc_priv {
	void __iomem *base;
	int irq;
	atomic_t hw_usecnt;
	struct device *dev;
	struct clk *dot_clk;
	unsigned long lddckr;
	struct sh_mobile_lcdc_chan ch[2];
143
	struct notifier_block notifier;
144 145
	unsigned long saved_shared_regs[NR_SHARED_REGS];
	int started;
146
	int forced_bpp; /* 2 channel LCDC must share bpp setting */
147
	struct sh_mobile_meram_info *meram_dev;
148 149
};

150 151 152 153 154 155 156 157 158
static bool banked(int reg_nr)
{
	switch (reg_nr) {
	case LDMT1R:
	case LDMT2R:
	case LDMT3R:
	case LDDFR:
	case LDSM1R:
	case LDSA1R:
159
	case LDSA2R:
160 161 162 163 164 165 166 167 168 169
	case LDMLSR:
	case LDHCNR:
	case LDHSYNR:
	case LDVLNR:
	case LDVSYNR:
		return true;
	}
	return false;
}

M
Magnus Damm 已提交
170 171 172 173
static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
			    int reg_nr, unsigned long data)
{
	iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
174 175 176 177 178 179 180 181 182 183
	if (banked(reg_nr))
		iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
			  SIDE_B_OFFSET);
}

static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
			    int reg_nr, unsigned long data)
{
	iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
		  MIRROR_OFFSET);
M
Magnus Damm 已提交
184 185 186 187 188 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 218 219 220 221 222 223
}

static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
				    int reg_nr)
{
	return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
}

static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
		       unsigned long reg_offs, unsigned long data)
{
	iowrite32(data, priv->base + reg_offs);
}

static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
			       unsigned long reg_offs)
{
	return ioread32(priv->base + reg_offs);
}

static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
			  unsigned long reg_offs,
			  unsigned long mask, unsigned long until)
{
	while ((lcdc_read(priv, reg_offs) & mask) != until)
		cpu_relax();
}

static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
{
	return chan->cfg.chan == LCDC_CHAN_SUBLCD;
}

static void lcdc_sys_write_index(void *handle, unsigned long data)
{
	struct sh_mobile_lcdc_chan *ch = handle;

	lcdc_write(ch->lcdc, _LDDWD0R, data | 0x10000000);
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
	lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
M
Magnus Damm 已提交
224
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
M
Magnus Damm 已提交
225 226 227 228 229 230 231 232 233
}

static void lcdc_sys_write_data(void *handle, unsigned long data)
{
	struct sh_mobile_lcdc_chan *ch = handle;

	lcdc_write(ch->lcdc, _LDDWD0R, data | 0x11000000);
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
	lcdc_write(ch->lcdc, _LDDWAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
M
Magnus Damm 已提交
234
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
M
Magnus Damm 已提交
235 236 237 238 239 240 241 242 243 244
}

static unsigned long lcdc_sys_read_data(void *handle)
{
	struct sh_mobile_lcdc_chan *ch = handle;

	lcdc_write(ch->lcdc, _LDDRDR, 0x01000000);
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
	lcdc_write(ch->lcdc, _LDDRAR, 1 | (lcdc_chan_is_sublcd(ch) ? 2 : 0));
	udelay(1);
M
Magnus Damm 已提交
245
	lcdc_wait_bit(ch->lcdc, _LDSR, 2, 0);
M
Magnus Damm 已提交
246

247
	return lcdc_read(ch->lcdc, _LDDRDR) & 0x3ffff;
M
Magnus Damm 已提交
248 249 250 251 252 253 254 255
}

struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
	lcdc_sys_write_index,
	lcdc_sys_write_data,
	lcdc_sys_read_data,
};

256 257
static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
{
258
	if (atomic_inc_and_test(&priv->hw_usecnt)) {
259 260
		if (priv->dot_clk)
			clk_enable(priv->dot_clk);
261
		pm_runtime_get_sync(priv->dev);
262 263
		if (priv->meram_dev && priv->meram_dev->pdev)
			pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
264 265 266 267 268
	}
}

static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
{
269
	if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
270 271
		if (priv->meram_dev && priv->meram_dev->pdev)
			pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
272
		pm_runtime_put(priv->dev);
273 274
		if (priv->dot_clk)
			clk_disable(priv->dot_clk);
275 276 277
	}
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
static int sh_mobile_lcdc_sginit(struct fb_info *info,
				  struct list_head *pagelist)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
	unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
	struct page *page;
	int nr_pages = 0;

	sg_init_table(ch->sglist, nr_pages_max);

	list_for_each_entry(page, pagelist, lru)
		sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);

	return nr_pages;
}

294 295 296 297
static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
				       struct list_head *pagelist)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
298
	struct sh_mobile_lcdc_board_cfg	*bcfg = &ch->cfg.board_cfg;
299 300 301 302

	/* enable clocks before accessing hardware */
	sh_mobile_lcdc_clk_on(ch->lcdc);

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	/*
	 * It's possible to get here without anything on the pagelist via
	 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
	 * invocation. In the former case, the acceleration routines are
	 * stepped in to when using the framebuffer console causing the
	 * workqueue to be scheduled without any dirty pages on the list.
	 *
	 * Despite this, a panel update is still needed given that the
	 * acceleration routines have their own methods for writing in
	 * that still need to be updated.
	 *
	 * The fsync() and empty pagelist case could be optimized for,
	 * but we don't bother, as any application exhibiting such
	 * behaviour is fundamentally broken anyways.
	 */
	if (!list_empty(pagelist)) {
		unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);

		/* trigger panel update */
		dma_map_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
323 324 325
		if (bcfg->start_transfer)
			bcfg->start_transfer(bcfg->board_data, ch,
					     &sh_mobile_lcdc_sys_bus_ops);
326 327
		lcdc_write_chan(ch, LDSM2R, 1);
		dma_unmap_sg(info->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
328 329 330 331
	} else {
		if (bcfg->start_transfer)
			bcfg->start_transfer(bcfg->board_data, ch,
					     &sh_mobile_lcdc_sys_bus_ops);
332
		lcdc_write_chan(ch, LDSM2R, 1);
333
	}
334 335 336 337 338 339 340 341 342 343 344 345 346
}

static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
{
	struct fb_deferred_io *fbdefio = info->fbdefio;

	if (fbdefio)
		schedule_delayed_work(&info->deferred_work, fbdefio->delay);
}

static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
{
	struct sh_mobile_lcdc_priv *priv = data;
347
	struct sh_mobile_lcdc_chan *ch;
348
	unsigned long tmp;
349
	unsigned long ldintr;
350 351
	int is_sub;
	int k;
352 353

	/* acknowledge interrupt */
354 355 356 357 358 359
	ldintr = tmp = lcdc_read(priv, _LDINTR);
	/*
	 * disable further VSYNC End IRQs, preserve all other enabled IRQs,
	 * write 0 to bits 0-6 to ack all triggered IRQs.
	 */
	tmp &= 0xffffff00 & ~LDINTR_VEE;
360 361
	lcdc_write(priv, _LDINTR, tmp);

362 363 364
	/* figure out if this interrupt is for main or sub lcd */
	is_sub = (lcdc_read(priv, _LDSR) & (1 << 10)) ? 1 : 0;

365
	/* wake up channel and disable clocks */
366 367 368 369 370 371
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		ch = &priv->ch[k];

		if (!ch->enabled)
			continue;

372 373 374 375 376
		/* Frame Start */
		if (ldintr & LDINTR_FS) {
			if (is_sub == lcdc_chan_is_sublcd(ch)) {
				ch->frame_end = 1;
				wake_up(&ch->frame_end_wait);
377

378 379 380 381 382
				sh_mobile_lcdc_clk_off(priv);
			}
		}

		/* VSYNC End */
383 384
		if (ldintr & LDINTR_VES)
			complete(&ch->vsync_completion);
385 386
	}

387 388 389
	return IRQ_HANDLED;
}

M
Magnus Damm 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
				      int start)
{
	unsigned long tmp = lcdc_read(priv, _LDCNT2R);
	int k;

	/* start or stop the lcdc */
	if (start)
		lcdc_write(priv, _LDCNT2R, tmp | START_LCDC);
	else
		lcdc_write(priv, _LDCNT2R, tmp & ~START_LCDC);

	/* wait until power is applied/stopped on all channels */
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
		if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
			while (1) {
				tmp = lcdc_read_chan(&priv->ch[k], LDPMR) & 3;
				if (start && tmp == 3)
					break;
				if (!start && tmp == 0)
					break;
				cpu_relax();
			}

	if (!start)
		lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
}

418 419
static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
{
420 421
	struct fb_var_screeninfo *var = &ch->info->var, *display_var = &ch->display_var;
	unsigned long h_total, hsync_pos, display_h_total;
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
	u32 tmp;

	tmp = ch->ldmt1r_value;
	tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : 1 << 28;
	tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : 1 << 27;
	tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? 1 << 26 : 0;
	tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? 1 << 25 : 0;
	tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? 1 << 24 : 0;
	tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? 1 << 17 : 0;
	tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? 1 << 16 : 0;
	lcdc_write_chan(ch, LDMT1R, tmp);

	/* setup SYS bus */
	lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
	lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);

	/* horizontal configuration */
439 440
	h_total = display_var->xres + display_var->hsync_len +
		display_var->left_margin + display_var->right_margin;
441
	tmp = h_total / 8; /* HTCN */
442
	tmp |= (min(display_var->xres, var->xres) / 8) << 16; /* HDCN */
443 444
	lcdc_write_chan(ch, LDHCNR, tmp);

445
	hsync_pos = display_var->xres + display_var->right_margin;
446
	tmp = hsync_pos / 8; /* HSYNP */
447
	tmp |= (display_var->hsync_len / 8) << 16; /* HSYNW */
448 449 450
	lcdc_write_chan(ch, LDHSYNR, tmp);

	/* vertical configuration */
451 452 453
	tmp = display_var->yres + display_var->vsync_len +
		display_var->upper_margin + display_var->lower_margin; /* VTLN */
	tmp |= min(display_var->yres, var->yres) << 16; /* VDLN */
454 455
	lcdc_write_chan(ch, LDVLNR, tmp);

456 457
	tmp = display_var->yres + display_var->lower_margin; /* VSYNP */
	tmp |= display_var->vsync_len << 16; /* VSYNW */
458 459 460
	lcdc_write_chan(ch, LDVSYNR, tmp);

	/* Adjust horizontal synchronisation for HDMI */
461 462 463 464 465
	display_h_total = display_var->xres + display_var->hsync_len +
		display_var->left_margin + display_var->right_margin;
	tmp = ((display_var->xres & 7) << 24) |
		((display_h_total & 7) << 16) |
		((display_var->hsync_len & 7) << 8) |
466 467 468 469
		hsync_pos;
	lcdc_write_chan(ch, LDHAJR, tmp);
}

M
Magnus Damm 已提交
470 471 472 473 474
static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
{
	struct sh_mobile_lcdc_chan *ch;
	struct sh_mobile_lcdc_board_cfg	*board_cfg;
	unsigned long tmp;
475
	int bpp = 0;
476
	unsigned long ldddsr;
477
	int k, m, ret;
M
Magnus Damm 已提交
478

479
	/* enable clocks before accessing the hardware */
480 481
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		if (priv->ch[k].enabled) {
482
			sh_mobile_lcdc_clk_on(priv);
483 484 485 486
			if (!bpp)
				bpp = priv->ch[k].info->var.bits_per_pixel;
		}
	}
487

M
Magnus Damm 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	/* reset */
	lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
	lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);

	/* enable LCDC channels */
	tmp = lcdc_read(priv, _LDCNT2R);
	tmp |= priv->ch[0].enabled;
	tmp |= priv->ch[1].enabled;
	lcdc_write(priv, _LDCNT2R, tmp);

	/* read data from external memory, avoid using the BEU for now */
	lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);

	/* stop the lcdc first */
	sh_mobile_lcdc_start_stop(priv, 0);

	/* configure clocks */
	tmp = priv->lddckr;
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		ch = &priv->ch[k];

		if (!priv->ch[k].enabled)
			continue;

		m = ch->cfg.clock_divider;
		if (!m)
			continue;

		if (m == 1)
			m = 1 << 6;
		tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);

520
		/* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
521
		lcdc_write_chan(ch, LDDCKPAT1R, 0);
M
Magnus Damm 已提交
522 523 524 525 526 527 528 529 530
		lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
	}

	lcdc_write(priv, _LDDCKR, tmp);

	/* start dotclock again */
	lcdc_write(priv, _LDDCKSTPR, 0);
	lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);

531
	/* interrupts are disabled to begin with */
M
Magnus Damm 已提交
532 533 534 535 536 537 538 539
	lcdc_write(priv, _LDINTR, 0);

	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		ch = &priv->ch[k];

		if (!ch->enabled)
			continue;

540
		sh_mobile_lcdc_geometry(ch);
M
Magnus Damm 已提交
541 542 543 544 545

		/* power supply */
		lcdc_write_chan(ch, LDPMR, 0);

		board_cfg = &ch->cfg.board_cfg;
546
		if (board_cfg->setup_sys) {
547
			ret = board_cfg->setup_sys(board_cfg->board_data,
548 549 550 551
						ch, &sh_mobile_lcdc_sys_bus_ops);
			if (ret)
				return ret;
		}
M
Magnus Damm 已提交
552 553 554
	}

	/* word and long word swap */
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	ldddsr = lcdc_read(priv, _LDDDSR);
	if  (priv->ch[0].info->var.nonstd)
		lcdc_write(priv, _LDDDSR, ldddsr | 7);
	else {
		switch (bpp) {
		case 16:
			lcdc_write(priv, _LDDDSR, ldddsr | 6);
			break;
		case 24:
			lcdc_write(priv, _LDDDSR, ldddsr | 7);
			break;
		case 32:
			lcdc_write(priv, _LDDDSR, ldddsr | 4);
			break;
		}
570
	}
M
Magnus Damm 已提交
571 572

	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
573 574 575
		unsigned long base_addr_y;
		unsigned long base_addr_c = 0;
		int pitch;
M
Magnus Damm 已提交
576 577 578 579 580 581 582
		ch = &priv->ch[k];

		if (!priv->ch[k].enabled)
			continue;

		/* set bpp format in PKF[4:0] */
		tmp = lcdc_read_chan(ch, LDDFR);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		tmp &= ~0x0003031f;
		if (ch->info->var.nonstd) {
			tmp |= (ch->info->var.nonstd << 16);
			switch (ch->info->var.bits_per_pixel) {
			case 12:
				break;
			case 16:
				tmp |= (0x1 << 8);
				break;
			case 24:
				tmp |= (0x2 << 8);
				break;
			}
		} else {
			switch (ch->info->var.bits_per_pixel) {
			case 16:
				tmp |= 0x03;
				break;
			case 24:
				tmp |= 0x0b;
				break;
			case 32:
				break;
			}
607
		}
M
Magnus Damm 已提交
608 609
		lcdc_write_chan(ch, LDDFR, tmp);

610 611 612 613 614 615 616
		base_addr_y = ch->info->fix.smem_start;
		base_addr_c = base_addr_y +
				ch->info->var.xres *
				ch->info->var.yres_virtual;
		pitch = ch->info->fix.line_length;

		/* test if we can enable meram */
617 618
		if (ch->cfg.meram_cfg && priv->meram_dev &&
				priv->meram_dev->ops) {
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
			struct sh_mobile_meram_cfg *cfg;
			struct sh_mobile_meram_info *mdev;
			unsigned long icb_addr_y, icb_addr_c;
			int icb_pitch;
			int pf;

			cfg = ch->cfg.meram_cfg;
			mdev = priv->meram_dev;
			/* we need to de-init configured ICBs before we
			 * we can re-initialize them.
			 */
			if (ch->meram_enabled)
				mdev->ops->meram_unregister(mdev, cfg);

			ch->meram_enabled = 0;

635 636 637 638 639 640
			if (ch->info->var.nonstd) {
				if (ch->info->var.bits_per_pixel == 24)
					pf = SH_MOBILE_MERAM_PF_NV24;
				else
					pf = SH_MOBILE_MERAM_PF_NV;
			} else {
641
				pf = SH_MOBILE_MERAM_PF_RGB;
642
			}
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

			ret = mdev->ops->meram_register(mdev, cfg, pitch,
						ch->info->var.yres,
						pf,
						base_addr_y,
						base_addr_c,
						&icb_addr_y,
						&icb_addr_c,
						&icb_pitch);
			if (!ret)  {
				/* set LDSA1R value */
				base_addr_y = icb_addr_y;
				pitch = icb_pitch;

				/* set LDSA2R value if required */
				if (base_addr_c)
					base_addr_c = icb_addr_c;

				ch->meram_enabled = 1;
			}
		}

M
Magnus Damm 已提交
665
		/* point out our frame buffer */
666
		lcdc_write_chan(ch, LDSA1R, base_addr_y);
667
		if (ch->info->var.nonstd)
668
			lcdc_write_chan(ch, LDSA2R, base_addr_c);
M
Magnus Damm 已提交
669 670

		/* set line size */
671
		lcdc_write_chan(ch, LDMLSR, pitch);
M
Magnus Damm 已提交
672

673 674 675 676 677
		/* setup deferred io if SYS bus */
		tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
		if (ch->ldmt1r_value & (1 << 12) && tmp) {
			ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
			ch->defio.delay = msecs_to_jiffies(tmp);
678 679
			ch->info->fbdefio = &ch->defio;
			fb_deferred_io_init(ch->info);
680 681 682 683 684 685 686 687 688 689 690

			/* one-shot mode */
			lcdc_write_chan(ch, LDSM1R, 1);

			/* enable "Frame End Interrupt Enable" bit */
			lcdc_write(priv, _LDINTR, LDINTR_FE);

		} else {
			/* continuous read mode */
			lcdc_write_chan(ch, LDSM1R, 0);
		}
M
Magnus Damm 已提交
691 692 693 694 695 696 697
	}

	/* display output */
	lcdc_write(priv, _LDCNT1R, LCDC_ENABLE);

	/* start the lcdc */
	sh_mobile_lcdc_start_stop(priv, 1);
698
	priv->started = 1;
M
Magnus Damm 已提交
699 700 701 702

	/* tell the board code to enable the panel */
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		ch = &priv->ch[k];
M
Magnus Damm 已提交
703 704 705
		if (!ch->enabled)
			continue;

M
Magnus Damm 已提交
706
		board_cfg = &ch->cfg.board_cfg;
707
		if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
708
			board_cfg->display_on(board_cfg->board_data, ch->info);
709 710
			module_put(board_cfg->owner);
		}
711 712 713 714 715

		if (ch->bl) {
			ch->bl->props.power = FB_BLANK_UNBLANK;
			backlight_update_status(ch->bl);
		}
M
Magnus Damm 已提交
716 717 718 719 720 721 722 723 724 725 726
	}

	return 0;
}

static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
{
	struct sh_mobile_lcdc_chan *ch;
	struct sh_mobile_lcdc_board_cfg	*board_cfg;
	int k;

727
	/* clean up deferred io and ask board code to disable panel */
M
Magnus Damm 已提交
728 729
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
		ch = &priv->ch[k];
M
Magnus Damm 已提交
730 731
		if (!ch->enabled)
			continue;
732

733 734 735 736
		/* deferred io mode:
		 * flush frame, and wait for frame end interrupt
		 * clean up deferred io and enable clock
		 */
737
		if (ch->info && ch->info->fbdefio) {
738
			ch->frame_end = 0;
739
			schedule_delayed_work(&ch->info->deferred_work, 0);
740
			wait_event(ch->frame_end_wait, ch->frame_end);
741 742
			fb_deferred_io_cleanup(ch->info);
			ch->info->fbdefio = NULL;
743
			sh_mobile_lcdc_clk_on(priv);
744
		}
745

746 747 748 749 750
		if (ch->bl) {
			ch->bl->props.power = FB_BLANK_POWERDOWN;
			backlight_update_status(ch->bl);
		}

751
		board_cfg = &ch->cfg.board_cfg;
752
		if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
753
			board_cfg->display_off(board_cfg->board_data);
754 755
			module_put(board_cfg->owner);
		}
756 757 758 759 760 761 762 763 764 765 766

		/* disable the meram */
		if (ch->meram_enabled) {
			struct sh_mobile_meram_cfg *cfg;
			struct sh_mobile_meram_info *mdev;
			cfg = ch->cfg.meram_cfg;
			mdev = priv->meram_dev;
			mdev->ops->meram_unregister(mdev, cfg);
			ch->meram_enabled = 0;
		}

M
Magnus Damm 已提交
767 768 769
	}

	/* stop the lcdc */
770 771 772 773
	if (priv->started) {
		sh_mobile_lcdc_start_stop(priv, 0);
		priv->started = 0;
	}
774

775 776 777 778
	/* stop clocks */
	for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
		if (priv->ch[k].enabled)
			sh_mobile_lcdc_clk_off(priv);
M
Magnus Damm 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
}

static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
{
	int ifm, miftyp;

	switch (ch->cfg.interface_type) {
	case RGB8: ifm = 0; miftyp = 0; break;
	case RGB9: ifm = 0; miftyp = 4; break;
	case RGB12A: ifm = 0; miftyp = 5; break;
	case RGB12B: ifm = 0; miftyp = 6; break;
	case RGB16: ifm = 0; miftyp = 7; break;
	case RGB18: ifm = 0; miftyp = 10; break;
	case RGB24: ifm = 0; miftyp = 11; break;
	case SYS8A: ifm = 1; miftyp = 0; break;
	case SYS8B: ifm = 1; miftyp = 1; break;
	case SYS8C: ifm = 1; miftyp = 2; break;
	case SYS8D: ifm = 1; miftyp = 3; break;
	case SYS9: ifm = 1; miftyp = 4; break;
	case SYS12: ifm = 1; miftyp = 5; break;
	case SYS16A: ifm = 1; miftyp = 7; break;
	case SYS16B: ifm = 1; miftyp = 8; break;
	case SYS16C: ifm = 1; miftyp = 9; break;
	case SYS18: ifm = 1; miftyp = 10; break;
	case SYS24: ifm = 1; miftyp = 11; break;
	default: goto bad;
	}

	/* SUBLCD only supports SYS interface */
	if (lcdc_chan_is_sublcd(ch)) {
		if (ifm == 0)
			goto bad;
		else
			ifm = 0;
	}

	ch->ldmt1r_value = (ifm << 12) | miftyp;
	return 0;
 bad:
	return -EINVAL;
}

821 822
static int sh_mobile_lcdc_setup_clocks(struct platform_device *pdev,
				       int clock_source,
M
Magnus Damm 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
				       struct sh_mobile_lcdc_priv *priv)
{
	char *str;
	int icksel;

	switch (clock_source) {
	case LCDC_CLK_BUS: str = "bus_clk"; icksel = 0; break;
	case LCDC_CLK_PERIPHERAL: str = "peripheral_clk"; icksel = 1; break;
	case LCDC_CLK_EXTERNAL: str = NULL; icksel = 2; break;
	default:
		return -EINVAL;
	}

	priv->lddckr = icksel << 16;

	if (str) {
839 840 841 842
		priv->dot_clk = clk_get(&pdev->dev, str);
		if (IS_ERR(priv->dot_clk)) {
			dev_err(&pdev->dev, "cannot get dot clock %s\n", str);
			return PTR_ERR(priv->dot_clk);
M
Magnus Damm 已提交
843 844
		}
	}
845 846 847 848 849

	/* Runtime PM support involves two step for this driver:
	 * 1) Enable Runtime PM
	 * 2) Force Runtime PM Resume since hardware is accessed from probe()
	 */
850
	priv->dev = &pdev->dev;
851 852
	pm_runtime_enable(priv->dev);
	pm_runtime_resume(priv->dev);
M
Magnus Damm 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	return 0;
}

static int sh_mobile_lcdc_setcolreg(u_int regno,
				    u_int red, u_int green, u_int blue,
				    u_int transp, struct fb_info *info)
{
	u32 *palette = info->pseudo_palette;

	if (regno >= PALETTE_NR)
		return -EINVAL;

	/* only FB_VISUAL_TRUECOLOR supported */

	red >>= 16 - info->var.red.length;
	green >>= 16 - info->var.green.length;
	blue >>= 16 - info->var.blue.length;
	transp >>= 16 - info->var.transp.length;

	palette[regno] = (red << info->var.red.offset) |
	  (green << info->var.green.offset) |
	  (blue << info->var.blue.offset) |
	  (transp << info->var.transp.offset);

	return 0;
}

static struct fb_fix_screeninfo sh_mobile_lcdc_fix  = {
	.id =		"SH Mobile LCDC",
	.type =		FB_TYPE_PACKED_PIXELS,
	.visual =	FB_VISUAL_TRUECOLOR,
	.accel =	FB_ACCEL_NONE,
885 886 887
	.xpanstep =	0,
	.ypanstep =	1,
	.ywrapstep =	0,
M
Magnus Damm 已提交
888 889
};

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
static void sh_mobile_lcdc_fillrect(struct fb_info *info,
				    const struct fb_fillrect *rect)
{
	sys_fillrect(info, rect);
	sh_mobile_lcdc_deferred_io_touch(info);
}

static void sh_mobile_lcdc_copyarea(struct fb_info *info,
				    const struct fb_copyarea *area)
{
	sys_copyarea(info, area);
	sh_mobile_lcdc_deferred_io_touch(info);
}

static void sh_mobile_lcdc_imageblit(struct fb_info *info,
				     const struct fb_image *image)
{
	sys_imageblit(info, image);
	sh_mobile_lcdc_deferred_io_touch(info);
}

911 912 913 914
static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
				     struct fb_info *info)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
915 916 917
	struct sh_mobile_lcdc_priv *priv = ch->lcdc;
	unsigned long ldrcntr;
	unsigned long new_pan_offset;
918 919
	unsigned long base_addr_y, base_addr_c;
	unsigned long c_offset;
920

921 922 923 924 925 926
	if (!var->nonstd)
		new_pan_offset = (var->yoffset * info->fix.line_length) +
			(var->xoffset * (info->var.bits_per_pixel / 8));
	else
		new_pan_offset = (var->yoffset * info->fix.line_length) +
			(var->xoffset);
927

928
	if (new_pan_offset == ch->pan_offset)
929 930
		return 0;	/* No change, do nothing */

931
	ldrcntr = lcdc_read(priv, _LDRCNTR);
932

933
	/* Set the source address for the next refresh */
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
	base_addr_y = ch->dma_handle + new_pan_offset;
	if (var->nonstd) {
		/* Set y offset */
		c_offset = (var->yoffset *
			info->fix.line_length *
			(info->var.bits_per_pixel - 8)) / 8;
		base_addr_c = ch->dma_handle + var->xres * var->yres_virtual +
			c_offset;
		/* Set x offset */
		if (info->var.bits_per_pixel == 24)
			base_addr_c += 2 * var->xoffset;
		else
			base_addr_c += var->xoffset;
	} else
		base_addr_c = 0;

950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
	if (!ch->meram_enabled) {
		lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
		if (base_addr_c)
			lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
	} else {
		struct sh_mobile_meram_cfg *cfg;
		struct sh_mobile_meram_info *mdev;
		unsigned long icb_addr_y, icb_addr_c;
		int ret;

		cfg = ch->cfg.meram_cfg;
		mdev = priv->meram_dev;
		ret = mdev->ops->meram_update(mdev, cfg,
					base_addr_y, base_addr_c,
					&icb_addr_y, &icb_addr_c);
		if (ret)
			return ret;

		lcdc_write_chan_mirror(ch, LDSA1R, icb_addr_y);
		if (icb_addr_c)
			lcdc_write_chan_mirror(ch, LDSA2R, icb_addr_c);

	}
973

974 975 976 977 978 979 980 981
	if (lcdc_chan_is_sublcd(ch))
		lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
	else
		lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);

	ch->pan_offset = new_pan_offset;

	sh_mobile_lcdc_deferred_io_touch(info);
982 983 984 985

	return 0;
}

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
static int sh_mobile_wait_for_vsync(struct fb_info *info)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
	unsigned long ldintr;
	int ret;

	/* Enable VSync End interrupt */
	ldintr = lcdc_read(ch->lcdc, _LDINTR);
	ldintr |= LDINTR_VEE;
	lcdc_write(ch->lcdc, _LDINTR, ldintr);

	ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
							msecs_to_jiffies(100));
	if (!ret)
		return -ETIMEDOUT;

	return 0;
}

static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
		       unsigned long arg)
{
	int retval;

	switch (cmd) {
	case FBIO_WAITFORVSYNC:
		retval = sh_mobile_wait_for_vsync(info);
		break;

	default:
		retval = -ENOIOCTLCMD;
		break;
	}
	return retval;
}

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
static void sh_mobile_fb_reconfig(struct fb_info *info)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
	struct fb_videomode mode1, mode2;
	struct fb_event event;
	int evnt = FB_EVENT_MODE_CHANGE_ALL;

	if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
		/* More framebuffer users are active */
		return;

	fb_var_to_videomode(&mode1, &ch->display_var);
	fb_var_to_videomode(&mode2, &info->var);

	if (fb_mode_is_equal(&mode1, &mode2))
		return;

	/* Display has been re-plugged, framebuffer is free now, reconfigure */
	if (fb_set_var(info, &ch->display_var) < 0)
		/* Couldn't reconfigure, hopefully, can continue as before */
		return;

1044 1045 1046 1047
	if (info->var.nonstd)
		info->fix.line_length = mode1.xres;
	else
		info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
1048 1049 1050 1051 1052 1053 1054

	/*
	 * fb_set_var() calls the notifier change internally, only if
	 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
	 * user event, we have to call the chain ourselves.
	 */
	event.info = info;
1055
	event.data = &mode1;
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	fb_notifier_call_chain(evnt, &event);
}

/*
 * Locking: both .fb_release() and .fb_open() are called with info->lock held if
 * user == 1, or with console sem held, if user == 0.
 */
static int sh_mobile_release(struct fb_info *info, int user)
{
	struct sh_mobile_lcdc_chan *ch = info->par;

	mutex_lock(&ch->open_lock);
	dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);

	ch->use_count--;

	/* Nothing to reconfigure, when called from fbcon */
	if (user) {
1074
		console_lock();
1075
		sh_mobile_fb_reconfig(info);
1076
		console_unlock();
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	}

	mutex_unlock(&ch->open_lock);

	return 0;
}

static int sh_mobile_open(struct fb_info *info, int user)
{
	struct sh_mobile_lcdc_chan *ch = info->par;

	mutex_lock(&ch->open_lock);
	ch->use_count++;

	dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
	mutex_unlock(&ch->open_lock);

	return 0;
}

static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
1100
	struct sh_mobile_lcdc_priv *p = ch->lcdc;
1101

1102
	if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
1103
	    var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
1104
		dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
1105 1106 1107
			 var->left_margin, var->xres, var->right_margin, var->hsync_len,
			 var->upper_margin, var->yres, var->lower_margin, var->vsync_len,
			 PICOS2KHZ(var->pixclock));
1108 1109
		return -EINVAL;
	}
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	/* only accept the forced_bpp for dual channel configurations */
	if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
		return -EINVAL;

	switch (var->bits_per_pixel) {
	case 16: /* PKF[4:0] = 00011 - RGB 565 */
	case 24: /* PKF[4:0] = 01011 - RGB 888 */
	case 32: /* PKF[4:0] = 00000 - RGBA 888 */
		break;
	default:
		return -EINVAL;
	}

1124 1125
	return 0;
}
1126

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
/*
 * Screen blanking. Behavior is as follows:
 * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
 * FB_BLANK_NORMAL: screen blanked, clocks enabled
 * FB_BLANK_VSYNC,
 * FB_BLANK_HSYNC,
 * FB_BLANK_POWEROFF: screen blanked, clocks disabled
 */
static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
{
	struct sh_mobile_lcdc_chan *ch = info->par;
	struct sh_mobile_lcdc_priv *p = ch->lcdc;

	/* blank the screen? */
	if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
		struct fb_fillrect rect = {
			.width = info->var.xres,
			.height = info->var.yres,
		};
		sh_mobile_lcdc_fillrect(info, &rect);
	}
	/* turn clocks on? */
	if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
		sh_mobile_lcdc_clk_on(p);
	}
	/* turn clocks off? */
	if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
		/* make sure the screen is updated with the black fill before
		 * switching the clocks off. one vsync is not enough since
		 * blanking may occur in the middle of a refresh. deferred io
		 * mode will reenable the clocks and update the screen in time,
		 * so it does not need this. */
		if (!info->fbdefio) {
			sh_mobile_wait_for_vsync(info);
			sh_mobile_wait_for_vsync(info);
		}
		sh_mobile_lcdc_clk_off(p);
	}

	ch->blank_status = blank;
	return 0;
}

M
Magnus Damm 已提交
1170
static struct fb_ops sh_mobile_lcdc_ops = {
1171
	.owner          = THIS_MODULE,
M
Magnus Damm 已提交
1172
	.fb_setcolreg	= sh_mobile_lcdc_setcolreg,
1173 1174
	.fb_read        = fb_sys_read,
	.fb_write       = fb_sys_write,
1175 1176 1177
	.fb_fillrect	= sh_mobile_lcdc_fillrect,
	.fb_copyarea	= sh_mobile_lcdc_copyarea,
	.fb_imageblit	= sh_mobile_lcdc_imageblit,
1178
	.fb_blank	= sh_mobile_lcdc_blank,
1179
	.fb_pan_display = sh_mobile_fb_pan_display,
1180
	.fb_ioctl       = sh_mobile_ioctl,
1181 1182 1183
	.fb_open	= sh_mobile_open,
	.fb_release	= sh_mobile_release,
	.fb_check_var	= sh_mobile_check_var,
M
Magnus Damm 已提交
1184 1185
};

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
{
	struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
	struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;
	int brightness = bdev->props.brightness;

	if (bdev->props.power != FB_BLANK_UNBLANK ||
	    bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
		brightness = 0;

	return cfg->set_brightness(cfg->board_data, brightness);
}

static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
{
	struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
	struct sh_mobile_lcdc_board_cfg *cfg = &ch->cfg.board_cfg;

	return cfg->get_brightness(cfg->board_data);
}

static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
				   struct fb_info *info)
{
	return (info->bl_dev == bdev);
}

static struct backlight_ops sh_mobile_lcdc_bl_ops = {
	.options	= BL_CORE_SUSPENDRESUME,
	.update_status	= sh_mobile_lcdc_update_bl,
	.get_brightness	= sh_mobile_lcdc_get_brightness,
	.check_fb	= sh_mobile_lcdc_check_fb,
};

static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
					       struct sh_mobile_lcdc_chan *ch)
{
	struct backlight_device *bl;

	bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
				       &sh_mobile_lcdc_bl_ops, NULL);
1227 1228 1229
	if (IS_ERR(bl)) {
		dev_err(parent, "unable to register backlight device: %ld\n",
			PTR_ERR(bl));
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		return NULL;
	}

	bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
	bl->props.brightness = bl->props.max_brightness;
	backlight_update_status(bl);

	return bl;
}

static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
{
	backlight_device_unregister(bdev);
}

1245 1246
static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
				   int nonstd)
M
Magnus Damm 已提交
1247
{
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	if (nonstd) {
		switch (bpp) {
		case 12:
		case 16:
		case 24:
			var->bits_per_pixel = bpp;
			var->nonstd = nonstd;
			return 0;
		default:
			return -EINVAL;
		}
	}

M
Magnus Damm 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	switch (bpp) {
	case 16: /* PKF[4:0] = 00011 - RGB 565 */
		var->red.offset = 11;
		var->red.length = 5;
		var->green.offset = 5;
		var->green.length = 6;
		var->blue.offset = 0;
		var->blue.length = 5;
		var->transp.offset = 0;
		var->transp.length = 0;
		break;

1273 1274
	case 24: /* PKF[4:0] = 01011 - RGB 888 */
		var->red.offset = 16;
M
Magnus Damm 已提交
1275
		var->red.length = 8;
1276
		var->green.offset = 8;
M
Magnus Damm 已提交
1277
		var->green.length = 8;
1278
		var->blue.offset = 0;
M
Magnus Damm 已提交
1279 1280 1281 1282
		var->blue.length = 8;
		var->transp.offset = 0;
		var->transp.length = 0;
		break;
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

	case 32: /* PKF[4:0] = 00000 - RGBA 888 */
		var->red.offset = 16;
		var->red.length = 8;
		var->green.offset = 8;
		var->green.length = 8;
		var->blue.offset = 0;
		var->blue.length = 8;
		var->transp.offset = 24;
		var->transp.length = 8;
		break;
M
Magnus Damm 已提交
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	default:
		return -EINVAL;
	}
	var->bits_per_pixel = bpp;
	var->red.msb_right = 0;
	var->green.msb_right = 0;
	var->blue.msb_right = 0;
	var->transp.msb_right = 0;
	return 0;
}

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
static int sh_mobile_lcdc_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);

	sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
	return 0;
}

static int sh_mobile_lcdc_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);

	return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
}

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
	struct sh_mobile_lcdc_chan *ch;
	int k, n;

	/* save per-channel registers */
	for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
		ch = &p->ch[k];
		if (!ch->enabled)
			continue;
		for (n = 0; n < NR_CH_REGS; n++)
			ch->saved_ch_regs[n] = lcdc_read_chan(ch, n);
	}

	/* save shared registers */
	for (n = 0; n < NR_SHARED_REGS; n++)
		p->saved_shared_regs[n] = lcdc_read(p, lcdc_shared_regs[n]);

	/* turn off LCDC hardware */
	lcdc_write(p, _LDCNT1R, 0);
	return 0;
}

static int sh_mobile_lcdc_runtime_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct sh_mobile_lcdc_priv *p = platform_get_drvdata(pdev);
	struct sh_mobile_lcdc_chan *ch;
	int k, n;

	/* restore per-channel registers */
	for (k = 0; k < ARRAY_SIZE(p->ch); k++) {
		ch = &p->ch[k];
		if (!ch->enabled)
			continue;
		for (n = 0; n < NR_CH_REGS; n++)
			lcdc_write_chan(ch, n, ch->saved_ch_regs[n]);
	}

	/* restore shared registers */
	for (n = 0; n < NR_SHARED_REGS; n++)
		lcdc_write(p, lcdc_shared_regs[n], p->saved_shared_regs[n]);

	return 0;
}

1368
static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
1369 1370
	.suspend = sh_mobile_lcdc_suspend,
	.resume = sh_mobile_lcdc_resume,
1371 1372
	.runtime_suspend = sh_mobile_lcdc_runtime_suspend,
	.runtime_resume = sh_mobile_lcdc_runtime_resume,
1373 1374
};

1375
/* locking: called with info->lock held */
1376 1377 1378 1379 1380 1381 1382 1383 1384
static int sh_mobile_lcdc_notify(struct notifier_block *nb,
				 unsigned long action, void *data)
{
	struct fb_event *event = data;
	struct fb_info *info = event->info;
	struct sh_mobile_lcdc_chan *ch = info->par;
	struct sh_mobile_lcdc_board_cfg	*board_cfg = &ch->cfg.board_cfg;

	if (&ch->lcdc->notifier != nb)
1385
		return NOTIFY_DONE;
1386 1387 1388 1389 1390 1391

	dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
		__func__, action, event->data);

	switch(action) {
	case FB_EVENT_SUSPEND:
1392
		if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
1393
			board_cfg->display_off(board_cfg->board_data);
1394 1395
			module_put(board_cfg->owner);
		}
1396
		sh_mobile_lcdc_stop(ch->lcdc);
1397 1398
		break;
	case FB_EVENT_RESUME:
1399 1400 1401
		mutex_lock(&ch->open_lock);
		sh_mobile_fb_reconfig(info);
		mutex_unlock(&ch->open_lock);
1402 1403

		/* HDMI must be enabled before LCDC configuration */
1404
		if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
1405
			board_cfg->display_on(board_cfg->board_data, info);
1406
			module_put(board_cfg->owner);
1407 1408
		}

1409
		sh_mobile_lcdc_start(ch->lcdc);
1410 1411
	}

1412
	return NOTIFY_OK;
1413 1414
}

M
Magnus Damm 已提交
1415 1416
static int sh_mobile_lcdc_remove(struct platform_device *pdev);

1417
static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
M
Magnus Damm 已提交
1418 1419 1420
{
	struct fb_info *info;
	struct sh_mobile_lcdc_priv *priv;
1421
	struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
M
Magnus Damm 已提交
1422 1423 1424 1425 1426
	struct resource *res;
	int error;
	void *buf;
	int i, j;

1427
	if (!pdata) {
M
Magnus Damm 已提交
1428
		dev_err(&pdev->dev, "no platform data defined\n");
1429
		return -EINVAL;
M
Magnus Damm 已提交
1430 1431 1432
	}

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1433 1434 1435
	i = platform_get_irq(pdev, 0);
	if (!res || i < 0) {
		dev_err(&pdev->dev, "cannot get platform resources\n");
1436
		return -ENOENT;
M
Magnus Damm 已提交
1437 1438 1439 1440 1441
	}

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv) {
		dev_err(&pdev->dev, "cannot allocate device data\n");
1442
		return -ENOMEM;
M
Magnus Damm 已提交
1443 1444
	}

1445 1446
	platform_set_drvdata(pdev, priv);

1447
	error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
1448
			    dev_name(&pdev->dev), priv);
1449 1450 1451 1452 1453 1454
	if (error) {
		dev_err(&pdev->dev, "unable to request irq\n");
		goto err1;
	}

	priv->irq = i;
1455
	atomic_set(&priv->hw_usecnt, -1);
M
Magnus Damm 已提交
1456 1457 1458

	j = 0;
	for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
1459
		struct sh_mobile_lcdc_chan *ch = priv->ch + j;
M
Magnus Damm 已提交
1460

1461 1462
		ch->lcdc = priv;
		memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
M
Magnus Damm 已提交
1463

1464
		error = sh_mobile_lcdc_check_interface(ch);
M
Magnus Damm 已提交
1465 1466 1467 1468
		if (error) {
			dev_err(&pdev->dev, "unsupported interface type\n");
			goto err1;
		}
1469 1470 1471
		init_waitqueue_head(&ch->frame_end_wait);
		init_completion(&ch->vsync_completion);
		ch->pan_offset = 0;
M
Magnus Damm 已提交
1472

1473 1474 1475 1476
		/* probe the backlight is there is one defined */
		if (ch->cfg.bl_info.max_brightness)
			ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);

M
Magnus Damm 已提交
1477 1478
		switch (pdata->ch[i].chan) {
		case LCDC_CHAN_MAINLCD:
1479 1480
			ch->enabled = 1 << 1;
			ch->reg_offs = lcdc_offs_mainlcd;
M
Magnus Damm 已提交
1481 1482 1483
			j++;
			break;
		case LCDC_CHAN_SUBLCD:
1484 1485
			ch->enabled = 1 << 2;
			ch->reg_offs = lcdc_offs_sublcd;
M
Magnus Damm 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
			j++;
			break;
		}
	}

	if (!j) {
		dev_err(&pdev->dev, "no channels defined\n");
		error = -EINVAL;
		goto err1;
	}

1497 1498 1499 1500
	/* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
	if (j == 2)
		priv->forced_bpp = pdata->ch[0].bpp;

1501 1502 1503 1504
	priv->base = ioremap_nocache(res->start, resource_size(res));
	if (!priv->base)
		goto err1;

1505
	error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
M
Magnus Damm 已提交
1506 1507 1508 1509 1510
	if (error) {
		dev_err(&pdev->dev, "unable to setup clocks\n");
		goto err1;
	}

1511 1512
	priv->meram_dev = pdata->meram_dev;

M
Magnus Damm 已提交
1513
	for (i = 0; i < j; i++) {
1514
		struct fb_var_screeninfo *var;
1515
		const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
1516
		struct sh_mobile_lcdc_chan *ch = priv->ch + i;
1517 1518
		struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
		const struct fb_videomode *mode = cfg->lcd_cfg;
1519 1520
		unsigned long max_size = 0;
		int k;
1521
		int num_cfg;
M
Magnus Damm 已提交
1522

1523 1524
		ch->info = framebuffer_alloc(0, &pdev->dev);
		if (!ch->info) {
1525 1526 1527 1528 1529
			dev_err(&pdev->dev, "unable to allocate fb_info\n");
			error = -ENOMEM;
			break;
		}

1530
		info = ch->info;
1531
		var = &info->var;
M
Magnus Damm 已提交
1532
		info->fbops = &sh_mobile_lcdc_ops;
1533
		info->par = ch;
1534 1535 1536

		mutex_init(&ch->open_lock);

1537 1538
		for (k = 0, lcd_cfg = mode;
		     k < cfg->num_cfg && lcd_cfg;
1539 1540
		     k++, lcd_cfg++) {
			unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
1541 1542 1543 1544 1545 1546 1547 1548
			/* NV12 buffers must have even number of lines */
			if ((cfg->nonstd) && cfg->bpp == 12 &&
					(lcd_cfg->yres & 0x1)) {
				dev_err(&pdev->dev, "yres must be multiple of 2"
						" for YCbCr420 mode.\n");
				error = -EINVAL;
				goto err1;
			}
1549 1550 1551 1552 1553 1554 1555

			if (size > max_size) {
				max_cfg = lcd_cfg;
				max_size = size;
			}
		}

1556
		if (!mode)
1557
			max_size = MAX_XRES * MAX_YRES;
1558 1559 1560
		else if (max_cfg)
			dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
				max_cfg->xres, max_cfg->yres);
1561

M
Magnus Damm 已提交
1562
		info->fix = sh_mobile_lcdc_fix;
1563 1564 1565 1566 1567
		info->fix.smem_len = max_size * 2 * cfg->bpp / 8;

		 /* Only pan in 2 line steps for NV12 */
		if (cfg->nonstd && cfg->bpp == 12)
			info->fix.ypanstep = 2;
M
Magnus Damm 已提交
1568

1569
		if (!mode) {
1570
			mode = &default_720p;
1571 1572
			num_cfg = 1;
		} else {
1573
			num_cfg = cfg->num_cfg;
1574 1575 1576
		}

		fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
1577 1578

		fb_videomode_to_var(var, mode);
1579 1580
		var->width = cfg->lcd_size_cfg.width;
		var->height = cfg->lcd_size_cfg.height;
1581
		/* Default Y virtual resolution is 2x panel size */
1582 1583 1584
		var->yres_virtual = var->yres * 2;
		var->activate = FB_ACTIVATE_NOW;

1585
		error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
M
Magnus Damm 已提交
1586 1587 1588 1589
		if (error)
			break;

		buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
1590
					 &ch->dma_handle, GFP_KERNEL);
M
Magnus Damm 已提交
1591 1592 1593 1594 1595 1596
		if (!buf) {
			dev_err(&pdev->dev, "unable to allocate buffer\n");
			error = -ENOMEM;
			break;
		}

1597
		info->pseudo_palette = &ch->pseudo_palette;
M
Magnus Damm 已提交
1598 1599 1600 1601 1602 1603
		info->flags = FBINFO_FLAG_DEFAULT;

		error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
		if (error < 0) {
			dev_err(&pdev->dev, "unable to allocate cmap\n");
			dma_free_coherent(&pdev->dev, info->fix.smem_len,
1604
					  buf, ch->dma_handle);
M
Magnus Damm 已提交
1605 1606 1607
			break;
		}

1608
		info->fix.smem_start = ch->dma_handle;
1609 1610 1611 1612 1613
		if (var->nonstd)
			info->fix.line_length = var->xres;
		else
			info->fix.line_length = var->xres * (cfg->bpp / 8);

M
Magnus Damm 已提交
1614 1615
		info->screen_base = buf;
		info->device = &pdev->dev;
1616
		ch->display_var = *var;
M
Magnus Damm 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
	}

	if (error)
		goto err1;

	error = sh_mobile_lcdc_start(priv);
	if (error) {
		dev_err(&pdev->dev, "unable to start hardware\n");
		goto err1;
	}

	for (i = 0; i < j; i++) {
1629 1630
		struct sh_mobile_lcdc_chan *ch = priv->ch + i;

1631
		info = ch->info;
1632 1633

		if (info->fbdefio) {
1634
			ch->sglist = vmalloc(sizeof(struct scatterlist) *
1635
					info->fix.smem_len >> PAGE_SHIFT);
1636
			if (!ch->sglist) {
1637 1638 1639 1640 1641
				dev_err(&pdev->dev, "cannot allocate sglist\n");
				goto err1;
			}
		}

1642 1643
		info->bl_dev = ch->bl;

1644
		error = register_framebuffer(info);
M
Magnus Damm 已提交
1645 1646 1647 1648 1649 1650
		if (error < 0)
			goto err1;

		dev_info(info->dev,
			 "registered %s/%s as %dx%d %dbpp.\n",
			 pdev->name,
1651
			 (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
M
Magnus Damm 已提交
1652
			 "mainlcd" : "sublcd",
1653
			 info->var.xres, info->var.yres,
1654
			 ch->cfg.bpp);
1655 1656

		/* deferred io mode: disable clock to save power */
1657
		if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
1658
			sh_mobile_lcdc_clk_off(priv);
M
Magnus Damm 已提交
1659 1660
	}

1661 1662 1663 1664
	/* Failure ignored */
	priv->notifier.notifier_call = sh_mobile_lcdc_notify;
	fb_register_client(&priv->notifier);

M
Magnus Damm 已提交
1665
	return 0;
1666
err1:
M
Magnus Damm 已提交
1667
	sh_mobile_lcdc_remove(pdev);
1668

M
Magnus Damm 已提交
1669 1670 1671 1672 1673 1674 1675 1676 1677
	return error;
}

static int sh_mobile_lcdc_remove(struct platform_device *pdev)
{
	struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
	struct fb_info *info;
	int i;

1678 1679
	fb_unregister_client(&priv->notifier);

M
Magnus Damm 已提交
1680
	for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
1681
		if (priv->ch[i].info && priv->ch[i].info->dev)
1682
			unregister_framebuffer(priv->ch[i].info);
M
Magnus Damm 已提交
1683 1684 1685 1686

	sh_mobile_lcdc_stop(priv);

	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
1687
		info = priv->ch[i].info;
M
Magnus Damm 已提交
1688

1689
		if (!info || !info->device)
M
Magnus Damm 已提交
1690 1691
			continue;

1692 1693 1694
		if (priv->ch[i].sglist)
			vfree(priv->ch[i].sglist);

1695 1696 1697 1698
		if (info->screen_base)
			dma_free_coherent(&pdev->dev, info->fix.smem_len,
					  info->screen_base,
					  priv->ch[i].dma_handle);
M
Magnus Damm 已提交
1699
		fb_dealloc_cmap(&info->cmap);
1700
		framebuffer_release(info);
M
Magnus Damm 已提交
1701 1702
	}

1703 1704 1705 1706 1707
	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
		if (priv->ch[i].bl)
			sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
	}

1708 1709
	if (priv->dot_clk)
		clk_put(priv->dot_clk);
1710

1711 1712
	if (priv->dev)
		pm_runtime_disable(priv->dev);
M
Magnus Damm 已提交
1713 1714 1715 1716

	if (priv->base)
		iounmap(priv->base);

1717 1718
	if (priv->irq)
		free_irq(priv->irq, priv);
M
Magnus Damm 已提交
1719 1720 1721 1722 1723 1724 1725 1726
	kfree(priv);
	return 0;
}

static struct platform_driver sh_mobile_lcdc_driver = {
	.driver		= {
		.name		= "sh_mobile_lcdc_fb",
		.owner		= THIS_MODULE,
1727
		.pm		= &sh_mobile_lcdc_dev_pm_ops,
M
Magnus Damm 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
	},
	.probe		= sh_mobile_lcdc_probe,
	.remove		= sh_mobile_lcdc_remove,
};

static int __init sh_mobile_lcdc_init(void)
{
	return platform_driver_register(&sh_mobile_lcdc_driver);
}

static void __exit sh_mobile_lcdc_exit(void)
{
	platform_driver_unregister(&sh_mobile_lcdc_driver);
}

module_init(sh_mobile_lcdc_init);
module_exit(sh_mobile_lcdc_exit);

MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
MODULE_LICENSE("GPL v2");