imxfb.c 26.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 *  Freescale i.MX Frame Buffer device driver
 *
 *  Copyright (C) 2004 Sascha Hauer, Pengutronix
 *   Based on acornfb.c Copyright (C) Russell King.
 *
 * 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.
 *
 * Please direct your questions and comments on this driver to the following
 * email address:
 *
 *	linux-arm-kernel@lists.arm.linux.org.uk
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
23
#include <linux/mm.h>
24 25 26 27 28
#include <linux/fb.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/cpufreq.h>
S
Sascha Hauer 已提交
29
#include <linux/clk.h>
30
#include <linux/platform_device.h>
31
#include <linux/dma-mapping.h>
32
#include <linux/io.h>
S
Sascha Hauer 已提交
33
#include <linux/math64.h>
M
Markus Pargmann 已提交
34 35 36 37 38 39
#include <linux/of.h>
#include <linux/of_device.h>

#include <video/of_display_timing.h>
#include <video/of_videomode.h>
#include <video/videomode.h>
40

41
#include <linux/platform_data/video-imxfb.h>
42 43 44 45 46 47

/*
 * Complain if VAR is out of range.
 */
#define DEBUG_VAR 1

48 49 50 51 52 53
#if defined(CONFIG_BACKLIGHT_CLASS_DEVICE) || \
	(defined(CONFIG_BACKLIGHT_CLASS_DEVICE_MODULE) && \
		defined(CONFIG_FB_IMX_MODULE))
#define PWMR_BACKLIGHT_AVAILABLE
#endif

54 55 56 57 58 59
#define DRIVER_NAME "imx-fb"

#define LCDC_SSA	0x00

#define LCDC_SIZE	0x04
#define SIZE_XMAX(x)	((((x) >> 4) & 0x3f) << 20)
S
Sascha Hauer 已提交
60

61 62
#define YMAX_MASK_IMX1	0x1ff
#define YMAX_MASK_IMX21	0x3ff
63 64 65 66 67 68 69 70 71

#define LCDC_VPW	0x08
#define VPW_VPW(x)	((x) & 0x3ff)

#define LCDC_CPOS	0x0C
#define CPOS_CC1	(1<<31)
#define CPOS_CC0	(1<<30)
#define CPOS_OP		(1<<28)
#define CPOS_CXP(x)	(((x) & 3ff) << 16)
S
Sascha Hauer 已提交
72

73 74 75 76 77 78 79
#define LCDC_LCWHB	0x10
#define LCWHB_BK_EN	(1<<31)
#define LCWHB_CW(w)	(((w) & 0x1f) << 24)
#define LCWHB_CH(h)	(((h) & 0x1f) << 16)
#define LCWHB_BD(x)	((x) & 0xff)

#define LCDC_LCHCC	0x14
S
Sascha Hauer 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#define LCDC_PCR	0x18

#define LCDC_HCR	0x1C
#define HCR_H_WIDTH(x)	(((x) & 0x3f) << 26)
#define HCR_H_WAIT_1(x)	(((x) & 0xff) << 8)
#define HCR_H_WAIT_2(x)	((x) & 0xff)

#define LCDC_VCR	0x20
#define VCR_V_WIDTH(x)	(((x) & 0x3f) << 26)
#define VCR_V_WAIT_1(x)	(((x) & 0xff) << 8)
#define VCR_V_WAIT_2(x)	((x) & 0xff)

#define LCDC_POS	0x24
#define POS_POS(x)	((x) & 1f)

#define LCDC_LSCR1	0x28
/* bit fields in imxfb.h */

#define LCDC_PWMR	0x2C
/* bit fields in imxfb.h */

#define LCDC_DMACR	0x30
/* bit fields in imxfb.h */

#define LCDC_RMCR	0x34
S
Sascha Hauer 已提交
106

107
#define RMCR_LCDC_EN_MX1	(1<<1)
S
Sascha Hauer 已提交
108

109 110 111 112 113 114 115 116 117 118 119 120
#define RMCR_SELF_REF	(1<<0)

#define LCDC_LCDICR	0x38
#define LCDICR_INT_SYN	(1<<2)
#define LCDICR_INT_CON	(1)

#define LCDC_LCDISR	0x40
#define LCDISR_UDR_ERR	(1<<3)
#define LCDISR_ERR_RES	(1<<2)
#define LCDISR_EOF	(1<<1)
#define LCDISR_BOF	(1<<0)

M
Markus Pargmann 已提交
121 122
#define IMXFB_LSCR1_DEFAULT 0x00120300

123 124 125
/* Used fb-mode. Can be set on kernel command line, therefore file-static. */
static const char *fb_mode;

126 127 128 129 130 131 132 133 134 135 136
/*
 * These are the bitfields for each
 * display depth that we support.
 */
struct imxfb_rgb {
	struct fb_bitfield	red;
	struct fb_bitfield	green;
	struct fb_bitfield	blue;
	struct fb_bitfield	transp;
};

137 138 139 140 141
enum imxfb_type {
	IMX1_FB,
	IMX21_FB,
};

142 143 144
struct imxfb_info {
	struct platform_device  *pdev;
	void __iomem		*regs;
145 146 147
	struct clk		*clk_ipg;
	struct clk		*clk_ahb;
	struct clk		*clk_per;
148
	enum imxfb_type		devtype;
149
	bool			enabled;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	/*
	 * These are the addresses we mapped
	 * the framebuffer memory region to.
	 */
	dma_addr_t		map_dma;
	u_char			*map_cpu;
	u_int			map_size;

	u_char			*screen_cpu;
	dma_addr_t		screen_dma;
	u_int			palette_size;

	dma_addr_t		dbar1;
	dma_addr_t		dbar2;

	u_int			pcr;
	u_int			pwmr;
	u_int			lscr1;
	u_int			dmacr;
	u_int			cmap_inverse:1,
				cmap_static:1,
				unused:30;

174 175
	struct imx_fb_videomode *mode;
	int			num_modes;
176
#ifdef PWMR_BACKLIGHT_AVAILABLE
177
	struct backlight_device *bl;
178
#endif
179

180 181 182 183
	void (*lcd_power)(int);
	void (*backlight_power)(int);
};

184 185 186 187 188 189 190 191 192 193 194 195 196
static struct platform_device_id imxfb_devtype[] = {
	{
		.name = "imx1-fb",
		.driver_data = IMX1_FB,
	}, {
		.name = "imx21-fb",
		.driver_data = IMX21_FB,
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(platform, imxfb_devtype);

M
Markus Pargmann 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209
static struct of_device_id imxfb_of_dev_id[] = {
	{
		.compatible = "fsl,imx1-fb",
		.data = &imxfb_devtype[IMX1_FB],
	}, {
		.compatible = "fsl,imx21-fb",
		.data = &imxfb_devtype[IMX21_FB],
	}, {
		/* sentinel */
	}
};
MODULE_DEVICE_TABLE(of, imxfb_of_dev_id);

210 211 212 213 214
static inline int is_imx1_fb(struct imxfb_info *fbi)
{
	return fbi->devtype == IMX1_FB;
}

215 216 217 218 219 220 221 222
#define IMX_NAME	"IMX"

/*
 * Minimum X and Y resolutions
 */
#define MIN_XRES	64
#define MIN_YRES	64

S
Sascha Hauer 已提交
223 224 225 226 227 228 229 230 231 232 233
/* Actually this really is 18bit support, the lowest 2 bits of each colour
 * are unused in hardware. We claim to have 24bit support to make software
 * like X work, which does not support 18bit.
 */
static struct imxfb_rgb def_rgb_18 = {
	.red	= {.offset = 16, .length = 8,},
	.green	= {.offset = 8, .length = 8,},
	.blue	= {.offset = 0, .length = 8,},
	.transp = {.offset = 0, .length = 0,},
};

S
Sascha Hauer 已提交
234 235 236 237 238 239 240 241
static struct imxfb_rgb def_rgb_16_tft = {
	.red	= {.offset = 11, .length = 5,},
	.green	= {.offset = 5, .length = 6,},
	.blue	= {.offset = 0, .length = 5,},
	.transp = {.offset = 0, .length = 0,},
};

static struct imxfb_rgb def_rgb_16_stn = {
242 243 244 245
	.red	= {.offset = 8, .length = 4,},
	.green	= {.offset = 4, .length = 4,},
	.blue	= {.offset = 0, .length = 4,},
	.transp = {.offset = 0, .length = 0,},
246 247 248
};

static struct imxfb_rgb def_rgb_8 = {
249 250 251 252
	.red	= {.offset = 0, .length = 8,},
	.green	= {.offset = 0, .length = 8,},
	.blue	= {.offset = 0, .length = 8,},
	.transp = {.offset = 0, .length = 0,},
253 254
};

255 256
static int imxfb_activate_var(struct fb_var_screeninfo *var,
		struct fb_info *info);
257 258 259 260 261 262 263 264

static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
{
	chan &= 0xffff;
	chan >>= 16 - bf->length;
	return chan << bf->offset;
}

265 266
static int imxfb_setpalettereg(u_int regno, u_int red, u_int green, u_int blue,
		u_int trans, struct fb_info *info)
267 268 269 270 271 272 273 274 275 276
{
	struct imxfb_info *fbi = info->par;
	u_int val, ret = 1;

#define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
	if (regno < fbi->palette_size) {
		val = (CNVT_TOHW(red, 4) << 8) |
		      (CNVT_TOHW(green,4) << 4) |
		      CNVT_TOHW(blue,  4);

277
		writel(val, fbi->regs + 0x800 + (regno << 2));
278 279 280 281 282
		ret = 0;
	}
	return ret;
}

283
static int imxfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		   u_int trans, struct fb_info *info)
{
	struct imxfb_info *fbi = info->par;
	unsigned int val;
	int ret = 1;

	/*
	 * If inverse mode was selected, invert all the colours
	 * rather than the register number.  The register number
	 * is what you poke into the framebuffer to produce the
	 * colour you requested.
	 */
	if (fbi->cmap_inverse) {
		red   = 0xffff - red;
		green = 0xffff - green;
		blue  = 0xffff - blue;
	}

	/*
	 * If greyscale is true, then we convert the RGB value
	 * to greyscale no mater what visual we are using.
	 */
	if (info->var.grayscale)
		red = green = blue = (19595 * red + 38470 * green +
					7471 * blue) >> 16;

	switch (info->fix.visual) {
	case FB_VISUAL_TRUECOLOR:
		/*
		 * 12 or 16-bit True Colour.  We encode the RGB value
		 * according to the RGB bitfield information.
		 */
		if (regno < 16) {
			u32 *pal = info->pseudo_palette;

			val  = chan_to_field(red, &info->var.red);
			val |= chan_to_field(green, &info->var.green);
			val |= chan_to_field(blue, &info->var.blue);

			pal[regno] = val;
			ret = 0;
		}
		break;

	case FB_VISUAL_STATIC_PSEUDOCOLOR:
	case FB_VISUAL_PSEUDOCOLOR:
		ret = imxfb_setpalettereg(regno, red, green, blue, trans, info);
		break;
	}

	return ret;
}

337 338 339 340 341
static const struct imx_fb_videomode *imxfb_find_mode(struct imxfb_info *fbi)
{
	struct imx_fb_videomode *m;
	int i;

M
Markus Pargmann 已提交
342 343 344
	if (!fb_mode)
		return &fbi->mode[0];

345 346 347 348 349 350 351
	for (i = 0, m = &fbi->mode[0]; i < fbi->num_modes; i++, m++) {
		if (!strcmp(m->mode.name, fb_mode))
			return m;
	}
	return NULL;
}

352 353 354 355 356 357
/*
 *  imxfb_check_var():
 *    Round up in the following order: bits_per_pixel, xres,
 *    yres, xres_virtual, yres_virtual, xoffset, yoffset, grayscale,
 *    bitfields, horizontal timing, vertical timing.
 */
358
static int imxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
359 360
{
	struct imxfb_info *fbi = info->par;
S
Sascha Hauer 已提交
361
	struct imxfb_rgb *rgb;
362 363 364 365
	const struct imx_fb_videomode *imxfb_mode;
	unsigned long lcd_clk;
	unsigned long long tmp;
	u32 pcr = 0;
366 367 368 369 370

	if (var->xres < MIN_XRES)
		var->xres = MIN_XRES;
	if (var->yres < MIN_YRES)
		var->yres = MIN_YRES;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	imxfb_mode = imxfb_find_mode(fbi);
	if (!imxfb_mode)
		return -EINVAL;

	var->xres		= imxfb_mode->mode.xres;
	var->yres		= imxfb_mode->mode.yres;
	var->bits_per_pixel	= imxfb_mode->bpp;
	var->pixclock		= imxfb_mode->mode.pixclock;
	var->hsync_len		= imxfb_mode->mode.hsync_len;
	var->left_margin	= imxfb_mode->mode.left_margin;
	var->right_margin	= imxfb_mode->mode.right_margin;
	var->vsync_len		= imxfb_mode->mode.vsync_len;
	var->upper_margin	= imxfb_mode->mode.upper_margin;
	var->lower_margin	= imxfb_mode->mode.lower_margin;
	var->sync		= imxfb_mode->mode.sync;
	var->xres_virtual	= max(var->xres_virtual, var->xres);
	var->yres_virtual	= max(var->yres_virtual, var->yres);
389 390

	pr_debug("var->bits_per_pixel=%d\n", var->bits_per_pixel);
391

392
	lcd_clk = clk_get_rate(fbi->clk_per);
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

	tmp = var->pixclock * (unsigned long long)lcd_clk;

	do_div(tmp, 1000000);

	if (do_div(tmp, 1000000) > 500000)
		tmp++;

	pcr = (unsigned int)tmp;

	if (--pcr > 0x3F) {
		pcr = 0x3F;
		printk(KERN_WARNING "Must limit pixel clock to %luHz\n",
				lcd_clk / pcr);
	}

409
	switch (var->bits_per_pixel) {
S
Sascha Hauer 已提交
410
	case 32:
411
		pcr |= PCR_BPIX_18;
S
Sascha Hauer 已提交
412 413
		rgb = &def_rgb_18;
		break;
414
	case 16:
S
Sascha Hauer 已提交
415
	default:
416
		if (is_imx1_fb(fbi))
417 418 419 420 421
			pcr |= PCR_BPIX_12;
		else
			pcr |= PCR_BPIX_16;

		if (imxfb_mode->pcr & PCR_TFT)
S
Sascha Hauer 已提交
422 423 424
			rgb = &def_rgb_16_tft;
		else
			rgb = &def_rgb_16_stn;
425 426
		break;
	case 8:
427
		pcr |= PCR_BPIX_8;
S
Sascha Hauer 已提交
428
		rgb = &def_rgb_8;
429 430 431
		break;
	}

432 433 434 435 436
	/* add sync polarities */
	pcr |= imxfb_mode->pcr & ~(0x3f | (7 << 25));

	fbi->pcr = pcr;

437 438 439 440
	/*
	 * Copy the RGB parameters for this display
	 * from the machine specific parameters.
	 */
S
Sascha Hauer 已提交
441 442 443 444
	var->red    = rgb->red;
	var->green  = rgb->green;
	var->blue   = rgb->blue;
	var->transp = rgb->transp;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

	pr_debug("RGBT length = %d:%d:%d:%d\n",
		var->red.length, var->green.length, var->blue.length,
		var->transp.length);

	pr_debug("RGBT offset = %d:%d:%d:%d\n",
		var->red.offset, var->green.offset, var->blue.offset,
		var->transp.offset);

	return 0;
}

/*
 * imxfb_set_par():
 *	Set the user defined part of the display for the specified console
 */
static int imxfb_set_par(struct fb_info *info)
{
	struct imxfb_info *fbi = info->par;
	struct fb_var_screeninfo *var = &info->var;

S
Sascha Hauer 已提交
466
	if (var->bits_per_pixel == 16 || var->bits_per_pixel == 32)
467 468 469 470 471 472 473 474 475 476 477 478
		info->fix.visual = FB_VISUAL_TRUECOLOR;
	else if (!fbi->cmap_static)
		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
	else {
		/*
		 * Some people have weird ideas about wanting static
		 * pseudocolor maps.  I suspect their user space
		 * applications are broken.
		 */
		info->fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
	}

479
	info->fix.line_length = var->xres_virtual * var->bits_per_pixel / 8;
480 481 482 483 484 485 486
	fbi->palette_size = var->bits_per_pixel == 8 ? 256 : 16;

	imxfb_activate_var(var, info);

	return 0;
}

487
#ifdef PWMR_BACKLIGHT_AVAILABLE
488 489 490 491 492 493 494 495 496 497 498 499
static int imxfb_bl_get_brightness(struct backlight_device *bl)
{
	struct imxfb_info *fbi = bl_get_data(bl);

	return readl(fbi->regs + LCDC_PWMR) & 0xFF;
}

static int imxfb_bl_update_status(struct backlight_device *bl)
{
	struct imxfb_info *fbi = bl_get_data(bl);
	int brightness = bl->props.brightness;

M
Markus Pargmann 已提交
500 501 502
	if (!fbi->pwmr)
		return 0;

503 504 505 506 507 508 509
	if (bl->props.power != FB_BLANK_UNBLANK)
		brightness = 0;
	if (bl->props.fb_blank != FB_BLANK_UNBLANK)
		brightness = 0;

	fbi->pwmr = (fbi->pwmr & ~0xFF) | brightness;

510 511 512 513 514
	if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
		clk_prepare_enable(fbi->clk_ipg);
		clk_prepare_enable(fbi->clk_ahb);
		clk_prepare_enable(fbi->clk_per);
	}
515
	writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
516 517 518 519 520
	if (bl->props.fb_blank != FB_BLANK_UNBLANK) {
		clk_disable_unprepare(fbi->clk_per);
		clk_disable_unprepare(fbi->clk_ahb);
		clk_disable_unprepare(fbi->clk_ipg);
	}
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

	return 0;
}

static const struct backlight_ops imxfb_lcdc_bl_ops = {
	.update_status = imxfb_bl_update_status,
	.get_brightness = imxfb_bl_get_brightness,
};

static void imxfb_init_backlight(struct imxfb_info *fbi)
{
	struct backlight_properties props;
	struct backlight_device	*bl;

	if (fbi->bl)
		return;

	memset(&props, 0, sizeof(struct backlight_properties));
	props.max_brightness = 0xff;
M
Matthew Garrett 已提交
540
	props.type = BACKLIGHT_RAW;
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	writel(fbi->pwmr, fbi->regs + LCDC_PWMR);

	bl = backlight_device_register("imxfb-bl", &fbi->pdev->dev, fbi,
				       &imxfb_lcdc_bl_ops, &props);
	if (IS_ERR(bl)) {
		dev_err(&fbi->pdev->dev, "error %ld on backlight register\n",
				PTR_ERR(bl));
		return;
	}

	fbi->bl = bl;
	bl->props.power = FB_BLANK_UNBLANK;
	bl->props.fb_blank = FB_BLANK_UNBLANK;
	bl->props.brightness = imxfb_bl_get_brightness(bl);
}

static void imxfb_exit_backlight(struct imxfb_info *fbi)
{
	if (fbi->bl)
		backlight_device_unregister(fbi->bl);
}
562
#endif
563

564 565
static void imxfb_enable_controller(struct imxfb_info *fbi)
{
566 567 568 569

	if (fbi->enabled)
		return;

570 571
	pr_debug("Enabling LCD controller\n");

572
	writel(fbi->screen_dma, fbi->regs + LCDC_SSA);
573

574 575
	/* panning offset 0 (0 pixel offset)        */
	writel(0x00000000, fbi->regs + LCDC_POS);
576 577

	/* disable hardware cursor */
578 579
	writel(readl(fbi->regs + LCDC_CPOS) & ~(CPOS_CC0 | CPOS_CC1),
		fbi->regs + LCDC_CPOS);
580

581 582 583 584 585
	/*
	 * RMCR_LCDC_EN_MX1 is present on i.MX1 only, but doesn't hurt
	 * on other SoCs
	 */
	writel(RMCR_LCDC_EN_MX1, fbi->regs + LCDC_RMCR);
586

587 588 589
	clk_prepare_enable(fbi->clk_ipg);
	clk_prepare_enable(fbi->clk_ahb);
	clk_prepare_enable(fbi->clk_per);
590
	fbi->enabled = true;
S
Sascha Hauer 已提交
591

592
	if (fbi->backlight_power)
593
		fbi->backlight_power(1);
594
	if (fbi->lcd_power)
595 596 597 598 599
		fbi->lcd_power(1);
}

static void imxfb_disable_controller(struct imxfb_info *fbi)
{
600 601 602
	if (!fbi->enabled)
		return;

603 604
	pr_debug("Disabling LCD controller\n");

605
	if (fbi->backlight_power)
606
		fbi->backlight_power(0);
607
	if (fbi->lcd_power)
608 609
		fbi->lcd_power(0);

610 611 612
	clk_disable_unprepare(fbi->clk_per);
	clk_disable_unprepare(fbi->clk_ipg);
	clk_disable_unprepare(fbi->clk_ahb);
613
	fbi->enabled = false;
S
Sascha Hauer 已提交
614

615
	writel(0, fbi->regs + LCDC_RMCR);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
}

static int imxfb_blank(int blank, struct fb_info *info)
{
	struct imxfb_info *fbi = info->par;

	pr_debug("imxfb_blank: blank=%d\n", blank);

	switch (blank) {
	case FB_BLANK_POWERDOWN:
	case FB_BLANK_VSYNC_SUSPEND:
	case FB_BLANK_HSYNC_SUSPEND:
	case FB_BLANK_NORMAL:
		imxfb_disable_controller(fbi);
		break;

	case FB_BLANK_UNBLANK:
		imxfb_enable_controller(fbi);
		break;
	}
	return 0;
}

static struct fb_ops imxfb_ops = {
	.owner		= THIS_MODULE,
	.fb_check_var	= imxfb_check_var,
	.fb_set_par	= imxfb_set_par,
	.fb_setcolreg	= imxfb_setcolreg,
	.fb_fillrect	= cfb_fillrect,
	.fb_copyarea	= cfb_copyarea,
	.fb_imageblit	= cfb_imageblit,
	.fb_blank	= imxfb_blank,
};

/*
 * imxfb_activate_var():
 *	Configures LCD Controller based on entries in var parameter.  Settings are
 *	only written to the controller if changes were made.
 */
static int imxfb_activate_var(struct fb_var_screeninfo *var, struct fb_info *info)
{
	struct imxfb_info *fbi = info->par;
658
	u32 ymax_mask = is_imx1_fb(fbi) ? YMAX_MASK_IMX1 : YMAX_MASK_IMX21;
S
Sascha Hauer 已提交
659

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	pr_debug("var: xres=%d hslen=%d lm=%d rm=%d\n",
		var->xres, var->hsync_len,
		var->left_margin, var->right_margin);
	pr_debug("var: yres=%d vslen=%d um=%d bm=%d\n",
		var->yres, var->vsync_len,
		var->upper_margin, var->lower_margin);

#if DEBUG_VAR
	if (var->xres < 16        || var->xres > 1024)
		printk(KERN_ERR "%s: invalid xres %d\n",
			info->fix.id, var->xres);
	if (var->hsync_len < 1    || var->hsync_len > 64)
		printk(KERN_ERR "%s: invalid hsync_len %d\n",
			info->fix.id, var->hsync_len);
	if (var->left_margin > 255)
		printk(KERN_ERR "%s: invalid left_margin %d\n",
			info->fix.id, var->left_margin);
	if (var->right_margin > 255)
		printk(KERN_ERR "%s: invalid right_margin %d\n",
			info->fix.id, var->right_margin);
680
	if (var->yres < 1 || var->yres > ymax_mask)
681 682 683 684 685 686 687 688 689 690 691 692 693
		printk(KERN_ERR "%s: invalid yres %d\n",
			info->fix.id, var->yres);
	if (var->vsync_len > 100)
		printk(KERN_ERR "%s: invalid vsync_len %d\n",
			info->fix.id, var->vsync_len);
	if (var->upper_margin > 63)
		printk(KERN_ERR "%s: invalid upper_margin %d\n",
			info->fix.id, var->upper_margin);
	if (var->lower_margin > 255)
		printk(KERN_ERR "%s: invalid lower_margin %d\n",
			info->fix.id, var->lower_margin);
#endif

694 695 696 697
	/* physical screen start address	    */
	writel(VPW_VPW(var->xres * var->bits_per_pixel / 8 / 4),
		fbi->regs + LCDC_VPW);

S
Sascha Hauer 已提交
698 699 700
	writel(HCR_H_WIDTH(var->hsync_len - 1) |
		HCR_H_WAIT_1(var->right_margin - 1) |
		HCR_H_WAIT_2(var->left_margin - 3),
701
		fbi->regs + LCDC_HCR);
702

703
	writel(VCR_V_WIDTH(var->vsync_len) |
S
Sascha Hauer 已提交
704 705
		VCR_V_WAIT_1(var->lower_margin) |
		VCR_V_WAIT_2(var->upper_margin),
706
		fbi->regs + LCDC_VCR);
707

708
	writel(SIZE_XMAX(var->xres) | (var->yres & ymax_mask),
709
			fbi->regs + LCDC_SIZE);
S
Sascha Hauer 已提交
710

711
	writel(fbi->pcr, fbi->regs + LCDC_PCR);
712
#ifndef PWMR_BACKLIGHT_AVAILABLE
M
Markus Pargmann 已提交
713 714
	if (fbi->pwmr)
		writel(fbi->pwmr, fbi->regs + LCDC_PWMR);
715
#endif
716
	writel(fbi->lscr1, fbi->regs + LCDC_LSCR1);
M
Markus Pargmann 已提交
717 718 719 720

	/* dmacr = 0 is no valid value, as we need DMA control marks. */
	if (fbi->dmacr)
		writel(fbi->dmacr, fbi->regs + LCDC_DMACR);
721 722 723 724 725 726 727 728 729

	return 0;
}

#ifdef CONFIG_PM
/*
 * Power management hooks.  Note that we won't be called from IRQ context,
 * unlike the blank functions above, so we may sleep.
 */
730
static int imxfb_suspend(struct platform_device *dev, pm_message_t state)
731
{
732 733
	struct fb_info *info = platform_get_drvdata(dev);
	struct imxfb_info *fbi = info->par;
734 735

	pr_debug("%s\n", __func__);
736

737
	imxfb_disable_controller(fbi);
738 739 740
	return 0;
}

741
static int imxfb_resume(struct platform_device *dev)
742
{
743 744
	struct fb_info *info = platform_get_drvdata(dev);
	struct imxfb_info *fbi = info->par;
745 746

	pr_debug("%s\n", __func__);
747

748
	imxfb_enable_controller(fbi);
749 750 751 752 753 754 755
	return 0;
}
#else
#define imxfb_suspend	NULL
#define imxfb_resume	NULL
#endif

M
Markus Pargmann 已提交
756
static int imxfb_init_fbinfo(struct platform_device *pdev)
757
{
758
	struct imx_fb_platform_data *pdata = dev_get_platdata(&pdev->dev);
759
	struct fb_info *info = dev_get_drvdata(&pdev->dev);
760
	struct imxfb_info *fbi = info->par;
M
Markus Pargmann 已提交
761
	struct device_node *np;
762

763
	pr_debug("%s\n",__func__);
764

765
	info->pseudo_palette = kmalloc(sizeof(u32) * 16, GFP_KERNEL);
766 767 768 769 770
	if (!info->pseudo_palette)
		return -ENOMEM;

	memset(fbi, 0, sizeof(struct imxfb_info));

771 772
	fbi->devtype = pdev->id_entry->driver_data;

773 774
	strlcpy(info->fix.id, IMX_NAME, sizeof(info->fix.id));

775
	info->fix.type			= FB_TYPE_PACKED_PIXELS;
776 777 778 779
	info->fix.type_aux		= 0;
	info->fix.xpanstep		= 0;
	info->fix.ypanstep		= 0;
	info->fix.ywrapstep		= 0;
780
	info->fix.accel			= FB_ACCEL_NONE;
781 782 783 784 785 786

	info->var.nonstd		= 0;
	info->var.activate		= FB_ACTIVATE_NOW;
	info->var.height		= -1;
	info->var.width	= -1;
	info->var.accel_flags		= 0;
787
	info->var.vmode			= FB_VMODE_NONINTERLACED;
788 789

	info->fbops			= &imxfb_ops;
790 791
	info->flags			= FBINFO_FLAG_DEFAULT |
					  FBINFO_READS_FAST;
M
Markus Pargmann 已提交
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 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	if (pdata) {
		info->var.grayscale		= pdata->cmap_greyscale;
		fbi->cmap_inverse		= pdata->cmap_inverse;
		fbi->cmap_static		= pdata->cmap_static;
		fbi->lscr1			= pdata->lscr1;
		fbi->dmacr			= pdata->dmacr;
		fbi->pwmr			= pdata->pwmr;
		fbi->lcd_power			= pdata->lcd_power;
		fbi->backlight_power		= pdata->backlight_power;
	} else {
		np = pdev->dev.of_node;
		info->var.grayscale = of_property_read_bool(np,
						"cmap-greyscale");
		fbi->cmap_inverse = of_property_read_bool(np, "cmap-inverse");
		fbi->cmap_static = of_property_read_bool(np, "cmap-static");

		fbi->lscr1 = IMXFB_LSCR1_DEFAULT;
		of_property_read_u32(np, "fsl,lscr1", &fbi->lscr1);

		of_property_read_u32(np, "fsl,dmacr", &fbi->dmacr);

		/* These two function pointers could be used by some specific
		 * platforms. */
		fbi->lcd_power = NULL;
		fbi->backlight_power = NULL;
	}

	return 0;
}

static int imxfb_of_read_mode(struct device *dev, struct device_node *np,
		struct imx_fb_videomode *imxfb_mode)
{
	int ret;
	struct fb_videomode *of_mode = &imxfb_mode->mode;
	u32 bpp;
	u32 pcr;

	ret = of_property_read_string(np, "model", &of_mode->name);
	if (ret)
		of_mode->name = NULL;

	ret = of_get_fb_videomode(np, of_mode, OF_USE_NATIVE_MODE);
	if (ret) {
		dev_err(dev, "Failed to get videomode from DT\n");
		return ret;
	}

	ret = of_property_read_u32(np, "bits-per-pixel", &bpp);
	ret |= of_property_read_u32(np, "fsl,pcr", &pcr);

	if (ret) {
		dev_err(dev, "Failed to read bpp and pcr from DT\n");
		return -EINVAL;
	}

	if (bpp < 1 || bpp > 255) {
		dev_err(dev, "Bits per pixel have to be between 1 and 255\n");
		return -EINVAL;
	}

	imxfb_mode->bpp = bpp;
	imxfb_mode->pcr = pcr;
855 856 857 858

	return 0;
}

M
Markus Pargmann 已提交
859
static int imxfb_probe(struct platform_device *pdev)
860 861 862
{
	struct imxfb_info *fbi;
	struct fb_info *info;
863
	struct imx_fb_platform_data *pdata;
864
	struct resource *res;
M
Markus Pargmann 已提交
865 866
	struct imx_fb_videomode *m;
	const struct of_device_id *of_id;
867
	int ret, i;
M
Markus Pargmann 已提交
868
	int bytes_per_pixel;
869

S
Sascha Hauer 已提交
870
	dev_info(&pdev->dev, "i.MX Framebuffer driver\n");
871

M
Markus Pargmann 已提交
872 873 874 875
	of_id = of_match_device(imxfb_of_dev_id, &pdev->dev);
	if (of_id)
		pdev->id_entry = of_id->data;

876
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
877
	if (!res)
878 879
		return -ENODEV;

880
	pdata = dev_get_platdata(&pdev->dev);
881

882
	info = framebuffer_alloc(sizeof(struct imxfb_info), &pdev->dev);
883
	if (!info)
884 885 886 887
		return -ENOMEM;

	fbi = info->par;

888
	platform_set_drvdata(pdev, info);
889

890
	ret = imxfb_init_fbinfo(pdev);
891
	if (ret < 0)
892 893
		goto failed_init;

M
Markus Pargmann 已提交
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
	if (pdata) {
		if (!fb_mode)
			fb_mode = pdata->mode[0].mode.name;

		fbi->mode = pdata->mode;
		fbi->num_modes = pdata->num_modes;
	} else {
		struct device_node *display_np;
		fb_mode = NULL;

		display_np = of_parse_phandle(pdev->dev.of_node, "display", 0);
		if (!display_np) {
			dev_err(&pdev->dev, "No display defined in devicetree\n");
			ret = -EINVAL;
			goto failed_of_parse;
		}

		/*
		 * imxfb does not support more modes, we choose only the native
		 * mode.
		 */
		fbi->num_modes = 1;

		fbi->mode = devm_kzalloc(&pdev->dev,
				sizeof(struct imx_fb_videomode), GFP_KERNEL);
		if (!fbi->mode) {
			ret = -ENOMEM;
			goto failed_of_parse;
		}

		ret = imxfb_of_read_mode(&pdev->dev, display_np, fbi->mode);
		if (ret)
			goto failed_of_parse;
	}

	/* Calculate maximum bytes used per pixel. In most cases this should
	 * be the same as m->bpp/8 */
	m = &fbi->mode[0];
	bytes_per_pixel = (m->bpp + 7) / 8;
	for (i = 0; i < fbi->num_modes; i++, m++)
		info->fix.smem_len = max_t(size_t, info->fix.smem_len,
				m->mode.xres * m->mode.yres * bytes_per_pixel);

937 938
	res = request_mem_region(res->start, resource_size(res),
				DRIVER_NAME);
939 940
	if (!res) {
		ret = -EBUSY;
941 942 943
		goto failed_req;
	}

944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	fbi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
	if (IS_ERR(fbi->clk_ipg)) {
		ret = PTR_ERR(fbi->clk_ipg);
		goto failed_getclock;
	}

	fbi->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
	if (IS_ERR(fbi->clk_ahb)) {
		ret = PTR_ERR(fbi->clk_ahb);
		goto failed_getclock;
	}

	fbi->clk_per = devm_clk_get(&pdev->dev, "per");
	if (IS_ERR(fbi->clk_per)) {
		ret = PTR_ERR(fbi->clk_per);
S
Sascha Hauer 已提交
959 960 961
		goto failed_getclock;
	}

962 963
	fbi->regs = ioremap(res->start, resource_size(res));
	if (fbi->regs == NULL) {
S
Sascha Hauer 已提交
964
		dev_err(&pdev->dev, "Cannot map frame buffer registers\n");
965
		ret = -ENOMEM;
966
		goto failed_ioremap;
967 968
	}

M
Markus Pargmann 已提交
969 970
	/* Seems not being used by anyone, so no support for oftree */
	if (!pdata || !pdata->fixed_screen_cpu) {
971 972 973 974 975
		fbi->map_size = PAGE_ALIGN(info->fix.smem_len);
		fbi->map_cpu = dma_alloc_writecombine(&pdev->dev,
				fbi->map_size, &fbi->map_dma, GFP_KERNEL);

		if (!fbi->map_cpu) {
976
			dev_err(&pdev->dev, "Failed to allocate video RAM: %d\n", ret);
977 978 979
			ret = -ENOMEM;
			goto failed_map;
		}
980 981 982 983 984

		info->screen_base = fbi->map_cpu;
		fbi->screen_cpu = fbi->map_cpu;
		fbi->screen_dma = fbi->map_dma;
		info->fix.smem_start = fbi->screen_dma;
985 986
	} else {
		/* Fixed framebuffer mapping enables location of the screen in eSRAM */
987 988
		fbi->map_cpu = pdata->fixed_screen_cpu;
		fbi->map_dma = pdata->fixed_screen_dma;
989 990 991 992 993 994
		info->screen_base = fbi->map_cpu;
		fbi->screen_cpu = fbi->map_cpu;
		fbi->screen_dma = fbi->map_dma;
		info->fix.smem_start = fbi->screen_dma;
	}

M
Markus Pargmann 已提交
995
	if (pdata && pdata->init) {
996 997 998 999 1000
		ret = pdata->init(fbi->pdev);
		if (ret)
			goto failed_platform_init;
	}

1001 1002

	INIT_LIST_HEAD(&info->modelist);
M
Markus Pargmann 已提交
1003 1004
	for (i = 0; i < fbi->num_modes; i++)
		fb_add_videomode(&fbi->mode[i].mode, &info->modelist);
1005

1006 1007 1008 1009 1010 1011
	/*
	 * This makes sure that our colour bitfield
	 * descriptors are correctly initialised.
	 */
	imxfb_check_var(&info->var, info);

1012
	ret = fb_alloc_cmap(&info->cmap, 1 << info->var.bits_per_pixel, 0);
1013 1014 1015 1016 1017 1018
	if (ret < 0)
		goto failed_cmap;

	imxfb_set_par(info);
	ret = register_framebuffer(info);
	if (ret < 0) {
1019
		dev_err(&pdev->dev, "failed to register framebuffer\n");
1020 1021 1022 1023
		goto failed_register;
	}

	imxfb_enable_controller(fbi);
1024
	fbi->pdev = pdev;
1025
#ifdef PWMR_BACKLIGHT_AVAILABLE
1026
	imxfb_init_backlight(fbi);
1027
#endif
1028 1029 1030 1031 1032 1033

	return 0;

failed_register:
	fb_dealloc_cmap(&info->cmap);
failed_cmap:
M
Markus Pargmann 已提交
1034
	if (pdata && pdata->exit)
1035 1036
		pdata->exit(fbi->pdev);
failed_platform_init:
M
Markus Pargmann 已提交
1037
	if (pdata && !pdata->fixed_screen_cpu)
1038
		dma_free_writecombine(&pdev->dev,fbi->map_size,fbi->map_cpu,
1039
			fbi->map_dma);
1040
failed_map:
1041 1042
	iounmap(fbi->regs);
failed_ioremap:
1043
failed_getclock:
S
Sascha Hauer 已提交
1044
	release_mem_region(res->start, resource_size(res));
1045
failed_req:
M
Markus Pargmann 已提交
1046
failed_of_parse:
1047
	kfree(info->pseudo_palette);
1048 1049 1050 1051 1052
failed_init:
	framebuffer_release(info);
	return ret;
}

1053
static int imxfb_remove(struct platform_device *pdev)
1054
{
1055
	struct imx_fb_platform_data *pdata;
1056
	struct fb_info *info = platform_get_drvdata(pdev);
1057
	struct imxfb_info *fbi = info->par;
1058 1059 1060 1061
	struct resource *res;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);

1062
	imxfb_disable_controller(fbi);
1063

1064
#ifdef PWMR_BACKLIGHT_AVAILABLE
1065
	imxfb_exit_backlight(fbi);
1066
#endif
1067 1068
	unregister_framebuffer(info);

1069
	pdata = dev_get_platdata(&pdev->dev);
M
Markus Pargmann 已提交
1070
	if (pdata && pdata->exit)
1071 1072
		pdata->exit(fbi->pdev);

1073 1074 1075 1076
	fb_dealloc_cmap(&info->cmap);
	kfree(info->pseudo_palette);
	framebuffer_release(info);

1077
	iounmap(fbi->regs);
S
Sascha Hauer 已提交
1078
	release_mem_region(res->start, resource_size(res));
S
Sascha Hauer 已提交
1079

1080 1081 1082
	return 0;
}

1083
static void imxfb_shutdown(struct platform_device *dev)
1084
{
1085
	struct fb_info *info = platform_get_drvdata(dev);
1086 1087
	struct imxfb_info *fbi = info->par;
	imxfb_disable_controller(fbi);
1088 1089
}

1090
static struct platform_driver imxfb_driver = {
1091 1092
	.suspend	= imxfb_suspend,
	.resume		= imxfb_resume,
1093
	.remove		= imxfb_remove,
1094
	.shutdown	= imxfb_shutdown,
1095
	.driver		= {
1096
		.name	= DRIVER_NAME,
M
Markus Pargmann 已提交
1097
		.of_match_table = imxfb_of_dev_id,
1098
	},
1099
	.id_table	= imxfb_devtype,
1100 1101
};

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
static int imxfb_setup(void)
{
#ifndef MODULE
	char *opt, *options = NULL;

	if (fb_get_options("imxfb", &options))
		return -ENODEV;

	if (!options || !*options)
		return 0;

	while ((opt = strsep(&options, ",")) != NULL) {
		if (!*opt)
			continue;
		else
			fb_mode = opt;
	}
#endif
	return 0;
}

1123
static int __init imxfb_init(void)
1124
{
1125 1126 1127 1128 1129
	int ret = imxfb_setup();

	if (ret < 0)
		return ret;

1130
	return platform_driver_probe(&imxfb_driver, imxfb_probe);
1131 1132 1133 1134
}

static void __exit imxfb_cleanup(void)
{
1135
	platform_driver_unregister(&imxfb_driver);
1136 1137 1138 1139 1140
}

module_init(imxfb_init);
module_exit(imxfb_cleanup);

1141
MODULE_DESCRIPTION("Freescale i.MX framebuffer driver");
1142 1143
MODULE_AUTHOR("Sascha Hauer, Pengutronix");
MODULE_LICENSE("GPL");