Linux Framebuffer Layer development
 help / color / mirror / Atom feed
* [PATCH/RFC v2 0/3] fbdev: Add FOURCC-based format configuration API
From: Laurent Pinchart @ 2011-08-19  9:37 UTC (permalink / raw)
  To: linux-fbdev; +Cc: linux-media, magnus.damm

Hi everybody,

Here's the second version of the fbdev FOURCC-based format configuration API.

Compared to the previous version, I've removed the FB_VMODE_FOURCC bit (FOURCC
mode is now selected by a grayscale value > 1), reorganized the union in the
fb_var_screeninfo structure, and added a colorspace field used for YUV modes.

This patch set also contains an implementation of the FOURCC-based format API
for the sh_mobile_lcdc driver, based on top of the latest patches I've sent to
the list. You can find a consolidated version that includes this patch set at
http://git.linuxtv.org/pinchartl/fbdev.git/shortlog/refs/heads/fbdev-yuv.
YUV support when MERAM is enabled is currently broken, I'm working on fixing
that.

I've updated the fbdev-test tool to add FOURCC support. The code is available
in the fbdev-test yuv branch at
http://git.ideasonboard.org/?pûdev-test.git;a=shortlog;h=refs/heads/yuv.

Laurent Pinchart (3):
  fbdev: Add FOURCC-based format configuration API
  v4l: Add V4L2_PIX_FMT_NV24 and V4L2_PIX_FMT_NV42 formats
  fbdev: sh_mobile_lcdc: Support FOURCC-based format API

 Documentation/DocBook/media/v4l/pixfmt-nv24.xml |  128 +++++++++
 Documentation/DocBook/media/v4l/pixfmt.xml      |    1 +
 Documentation/fb/api.txt                        |  299 ++++++++++++++++++++
 arch/arm/mach-shmobile/board-ag5evm.c           |    2 +-
 arch/arm/mach-shmobile/board-ap4evb.c           |    4 +-
 arch/arm/mach-shmobile/board-mackerel.c         |    4 +-
 drivers/video/sh_mobile_lcdcfb.c                |  342 +++++++++++++++--------
 include/linux/fb.h                              |   27 ++-
 include/linux/videodev2.h                       |    2 +
 include/video/sh_mobile_lcdc.h                  |    4 +-
 10 files changed, 681 insertions(+), 132 deletions(-)
 create mode 100644 Documentation/DocBook/media/v4l/pixfmt-nv24.xml
 create mode 100644 Documentation/fb/api.txt

-- 
Regards,

Laurent Pinchart


^ permalink raw reply

* [PATCH 7/7] fbdev: sh_mobile_lcdc: Remove sh_mobile_lcdc_set_bpp()
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

The function duplicates code found in sh_mobile_check_var(). Remove
sh_mobile_lcdc_set_bpp() and call sh_mobile_check_var() instead.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |   87 +++++++------------------------------
 1 files changed, 17 insertions(+), 70 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index d1576e2..97ab8ba 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -1291,66 +1291,6 @@ static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
 	backlight_device_unregister(bdev);
 }
 
-static int sh_mobile_lcdc_set_bpp(struct fb_var_screeninfo *var, int bpp,
-				   int nonstd)
-{
-	if (nonstd) {
-		switch (bpp) {
-		case 12:
-		case 16:
-		case 24:
-			var->bits_per_pixel = bpp;
-			var->nonstd = nonstd;
-			return 0;
-		default:
-			return -EINVAL;
-		}
-	}
-
-	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;
-
-	case 24: /* PKF[4:0] = 01011 - RGB 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 = 0;
-		var->transp.length = 0;
-		break;
-
-	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;
-	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;
-}
-
 static int sh_mobile_lcdc_suspend(struct device *dev)
 {
 	struct platform_device *pdev = to_platform_device(dev);
@@ -1499,6 +1439,9 @@ static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
 	int ret;
 	int i;
 
+	mutex_init(&ch->open_lock);
+
+	/* Allocate the frame buffer device. */
 	ch->info = framebuffer_alloc(0, dev);
 	if (!ch->info) {
 		dev_err(dev, "unable to allocate fb_info\n");
@@ -1506,11 +1449,10 @@ static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
 	}
 
 	info = ch->info;
-	var = &info->var;
 	info->fbops = &sh_mobile_lcdc_ops;
 	info->par = ch;
-
-	mutex_init(&ch->open_lock);
+	info->pseudo_palette = &ch->pseudo_palette;
+	info->flags = FBINFO_FLAG_DEFAULT;
 
 	/* Iterate through the modes to validate them and find the highest
 	 * resolution.
@@ -1541,13 +1483,15 @@ static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
 		dev_dbg(dev, "Found largest videomode %ux%u\n",
 			max_mode->xres, max_mode->yres);
 
+	/* Initialize fixed screen information. Restrict pan to 2 lines steps
+	 * for NV12.
+	 */
 	info->fix = sh_mobile_lcdc_fix;
 	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;
 
+	/* Create the mode list. */
 	if (cfg->lcd_cfg = NULL) {
 		mode = &default_720p;
 		num_cfg = 1;
@@ -1558,17 +1502,23 @@ static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
 
 	fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
 
+	/* Initialize variable screen information using the first mode as
+	 * default. The default Y virtual resolution is twice the panel size to
+	 * allow for double-buffering.
+	 */
+	var = &info->var;
 	fb_videomode_to_var(var, mode);
+	var->bits_per_pixel = cfg->bpp;
 	var->width = cfg->lcd_size_cfg.width;
 	var->height = cfg->lcd_size_cfg.height;
-	/* Default Y virtual resolution is 2x panel size */
 	var->yres_virtual = var->yres * 2;
 	var->activate = FB_ACTIVATE_NOW;
 
-	ret = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
+	ret = sh_mobile_check_var(var, info);
 	if (ret)
 		return ret;
 
+	/* Allocate frame buffer memory and color map. */
 	buf = dma_alloc_coherent(dev, info->fix.smem_len, &ch->dma_handle,
 				 GFP_KERNEL);
 	if (!buf) {
@@ -1576,9 +1526,6 @@ static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
 		return -ENOMEM;
 	}
 
-	info->pseudo_palette = &ch->pseudo_palette;
-	info->flags = FBINFO_FLAG_DEFAULT;
-
 	ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
 	if (ret < 0) {
 		dev_err(dev, "unable to allocate cmap\n");
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 6/7] fbdev: sh_mobile_lcdc: Split channel initialization from probe function
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Move channel initialization to sh_mobile_lcdc_channel_init() and call
the function from sh_mobile_lcdc_probe(). This makes the code more
readable and prepares it for fix/var initialization rework.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |  247 ++++++++++++++++++++------------------
 1 files changed, 129 insertions(+), 118 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index 366315b..d1576e2 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -1485,15 +1485,129 @@ static int sh_mobile_lcdc_remove(struct platform_device *pdev)
 	return 0;
 }
 
-static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
+static int __devinit sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch,
+						 struct device *dev)
 {
+	struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
+	const struct fb_videomode *max_mode;
+	const struct fb_videomode *mode;
+	struct fb_var_screeninfo *var;
 	struct fb_info *info;
-	struct sh_mobile_lcdc_priv *priv;
+	unsigned int max_size;
+	int num_cfg;
+	void *buf;
+	int ret;
+	int i;
+
+	ch->info = framebuffer_alloc(0, dev);
+	if (!ch->info) {
+		dev_err(dev, "unable to allocate fb_info\n");
+		return -ENOMEM;
+	}
+
+	info = ch->info;
+	var = &info->var;
+	info->fbops = &sh_mobile_lcdc_ops;
+	info->par = ch;
+
+	mutex_init(&ch->open_lock);
+
+	/* Iterate through the modes to validate them and find the highest
+	 * resolution.
+	 */
+	max_mode = NULL;
+	max_size = 0;
+
+	for (i = 0, mode = cfg->lcd_cfg; i < cfg->num_cfg; i++, mode++) {
+		unsigned int size = mode->yres * mode->xres;
+
+		/* NV12 buffers must have even number of lines */
+		if ((cfg->nonstd) && cfg->bpp = 12 &&
+				(mode->yres & 0x1)) {
+			dev_err(dev, "yres must be multiple of 2 for YCbCr420 "
+				"mode.\n");
+			return -EINVAL;
+		}
+
+		if (size > max_size) {
+			max_mode = mode;
+			max_size = size;
+		}
+	}
+
+	if (!max_size)
+		max_size = MAX_XRES * MAX_YRES;
+	else
+		dev_dbg(dev, "Found largest videomode %ux%u\n",
+			max_mode->xres, max_mode->yres);
+
+	info->fix = sh_mobile_lcdc_fix;
+	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;
+
+	if (cfg->lcd_cfg = NULL) {
+		mode = &default_720p;
+		num_cfg = 1;
+	} else {
+		mode = cfg->lcd_cfg;
+		num_cfg = cfg->num_cfg;
+	}
+
+	fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
+
+	fb_videomode_to_var(var, mode);
+	var->width = cfg->lcd_size_cfg.width;
+	var->height = cfg->lcd_size_cfg.height;
+	/* Default Y virtual resolution is 2x panel size */
+	var->yres_virtual = var->yres * 2;
+	var->activate = FB_ACTIVATE_NOW;
+
+	ret = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
+	if (ret)
+		return ret;
+
+	buf = dma_alloc_coherent(dev, info->fix.smem_len, &ch->dma_handle,
+				 GFP_KERNEL);
+	if (!buf) {
+		dev_err(dev, "unable to allocate buffer\n");
+		return -ENOMEM;
+	}
+
+	info->pseudo_palette = &ch->pseudo_palette;
+	info->flags = FBINFO_FLAG_DEFAULT;
+
+	ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
+	if (ret < 0) {
+		dev_err(dev, "unable to allocate cmap\n");
+		dma_free_coherent(dev, info->fix.smem_len,
+				  buf, ch->dma_handle);
+		return ret;
+	}
+
+	info->fix.smem_start = ch->dma_handle;
+	if (var->nonstd)
+		info->fix.line_length = var->xres;
+	else
+		info->fix.line_length = var->xres * (cfg->bpp / 8);
+
+	info->screen_base = buf;
+	info->device = dev;
+	ch->display_var = *var;
+
+	return 0;
+}
+
+static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
+{
 	struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
+	struct sh_mobile_lcdc_priv *priv;
 	struct resource *res;
+	int num_channels;
 	int error;
-	void *buf;
-	int i, j;
+	int i;
 
 	if (!pdata) {
 		dev_err(&pdev->dev, "no platform data defined\n");
@@ -1525,9 +1639,8 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 	priv->irq = i;
 	atomic_set(&priv->hw_usecnt, -1);
 
-	j = 0;
-	for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
-		struct sh_mobile_lcdc_chan *ch = priv->ch + j;
+	for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
+		struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
 
 		ch->lcdc = priv;
 		memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
@@ -1549,24 +1662,24 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 		case LCDC_CHAN_MAINLCD:
 			ch->enabled = LDCNT2R_ME;
 			ch->reg_offs = lcdc_offs_mainlcd;
-			j++;
+			num_channels++;
 			break;
 		case LCDC_CHAN_SUBLCD:
 			ch->enabled = LDCNT2R_SE;
 			ch->reg_offs = lcdc_offs_sublcd;
-			j++;
+			num_channels++;
 			break;
 		}
 	}
 
-	if (!j) {
+	if (!num_channels) {
 		dev_err(&pdev->dev, "no channels defined\n");
 		error = -EINVAL;
 		goto err1;
 	}
 
 	/* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
-	if (j = 2)
+	if (num_channels = 2)
 		priv->forced_bpp = pdata->ch[0].bpp;
 
 	priv->base = ioremap_nocache(res->start, resource_size(res));
@@ -1581,125 +1694,23 @@ static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 
 	priv->meram_dev = pdata->meram_dev;
 
-	for (i = 0; i < j; i++) {
-		struct fb_var_screeninfo *var;
-		const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
+	for (i = 0; i < num_channels; i++) {
 		struct sh_mobile_lcdc_chan *ch = priv->ch + i;
-		struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
-		const struct fb_videomode *mode = cfg->lcd_cfg;
-		unsigned long max_size = 0;
-		int k;
-		int num_cfg;
-
-		ch->info = framebuffer_alloc(0, &pdev->dev);
-		if (!ch->info) {
-			dev_err(&pdev->dev, "unable to allocate fb_info\n");
-			error = -ENOMEM;
-			break;
-		}
-
-		info = ch->info;
-		var = &info->var;
-		info->fbops = &sh_mobile_lcdc_ops;
-		info->par = ch;
-
-		mutex_init(&ch->open_lock);
-
-		for (k = 0, lcd_cfg = mode;
-		     k < cfg->num_cfg && lcd_cfg;
-		     k++, lcd_cfg++) {
-			unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
-			/* 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;
-			}
-
-			if (size > max_size) {
-				max_cfg = lcd_cfg;
-				max_size = size;
-			}
-		}
-
-		if (!mode)
-			max_size = MAX_XRES * MAX_YRES;
-		else if (max_cfg)
-			dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
-				max_cfg->xres, max_cfg->yres);
-
-		info->fix = sh_mobile_lcdc_fix;
-		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;
-
-		if (!mode) {
-			mode = &default_720p;
-			num_cfg = 1;
-		} else {
-			num_cfg = cfg->num_cfg;
-		}
-
-		fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
 
-		fb_videomode_to_var(var, mode);
-		var->width = cfg->lcd_size_cfg.width;
-		var->height = cfg->lcd_size_cfg.height;
-		/* Default Y virtual resolution is 2x panel size */
-		var->yres_virtual = var->yres * 2;
-		var->activate = FB_ACTIVATE_NOW;
-
-		error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
+		error = sh_mobile_lcdc_channel_init(ch, &pdev->dev);
 		if (error)
-			break;
-
-		buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
-					 &ch->dma_handle, GFP_KERNEL);
-		if (!buf) {
-			dev_err(&pdev->dev, "unable to allocate buffer\n");
-			error = -ENOMEM;
-			break;
-		}
-
-		info->pseudo_palette = &ch->pseudo_palette;
-		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,
-					  buf, ch->dma_handle);
-			break;
-		}
-
-		info->fix.smem_start = ch->dma_handle;
-		if (var->nonstd)
-			info->fix.line_length = var->xres;
-		else
-			info->fix.line_length = var->xres * (cfg->bpp / 8);
-
-		info->screen_base = buf;
-		info->device = &pdev->dev;
-		ch->display_var = *var;
+			goto err1;
 	}
 
-	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++) {
+	for (i = 0; i < num_channels; i++) {
 		struct sh_mobile_lcdc_chan *ch = priv->ch + i;
-
-		info = ch->info;
+		struct fb_info *info = ch->info;
 
 		if (info->fbdefio) {
 			ch->sglist = vmalloc(sizeof(struct scatterlist) *
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 5/7] fbdev: sh_mobile_lcdc: Avoid forward declarations
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Reorder probe/remove functions to avoid forward declarations.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |  102 ++++++++++++++++++-------------------
 1 files changed, 50 insertions(+), 52 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index b6da1d6..366315b 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -1434,7 +1434,56 @@ static int sh_mobile_lcdc_notify(struct notifier_block *nb,
 	return NOTIFY_OK;
 }
 
-static int sh_mobile_lcdc_remove(struct platform_device *pdev);
+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;
+
+	fb_unregister_client(&priv->notifier);
+
+	for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
+		if (priv->ch[i].info && priv->ch[i].info->dev)
+			unregister_framebuffer(priv->ch[i].info);
+
+	sh_mobile_lcdc_stop(priv);
+
+	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
+		info = priv->ch[i].info;
+
+		if (!info || !info->device)
+			continue;
+
+		if (priv->ch[i].sglist)
+			vfree(priv->ch[i].sglist);
+
+		if (info->screen_base)
+			dma_free_coherent(&pdev->dev, info->fix.smem_len,
+					  info->screen_base,
+					  priv->ch[i].dma_handle);
+		fb_dealloc_cmap(&info->cmap);
+		framebuffer_release(info);
+	}
+
+	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
+		if (priv->ch[i].bl)
+			sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
+	}
+
+	if (priv->dot_clk)
+		clk_put(priv->dot_clk);
+
+	if (priv->dev)
+		pm_runtime_disable(priv->dev);
+
+	if (priv->base)
+		iounmap(priv->base);
+
+	if (priv->irq)
+		free_irq(priv->irq, priv);
+	kfree(priv);
+	return 0;
+}
 
 static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
 {
@@ -1691,57 +1740,6 @@ err1:
 	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;
-
-	fb_unregister_client(&priv->notifier);
-
-	for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
-		if (priv->ch[i].info && priv->ch[i].info->dev)
-			unregister_framebuffer(priv->ch[i].info);
-
-	sh_mobile_lcdc_stop(priv);
-
-	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
-		info = priv->ch[i].info;
-
-		if (!info || !info->device)
-			continue;
-
-		if (priv->ch[i].sglist)
-			vfree(priv->ch[i].sglist);
-
-		if (info->screen_base)
-			dma_free_coherent(&pdev->dev, info->fix.smem_len,
-					  info->screen_base,
-					  priv->ch[i].dma_handle);
-		fb_dealloc_cmap(&info->cmap);
-		framebuffer_release(info);
-	}
-
-	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
-		if (priv->ch[i].bl)
-			sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
-	}
-
-	if (priv->dot_clk)
-		clk_put(priv->dot_clk);
-
-	if (priv->dev)
-		pm_runtime_disable(priv->dev);
-
-	if (priv->base)
-		iounmap(priv->base);
-
-	if (priv->irq)
-		free_irq(priv->irq, priv);
-	kfree(priv);
-	return 0;
-}
-
 static struct platform_driver sh_mobile_lcdc_driver = {
 	.driver		= {
 		.name		= "sh_mobile_lcdc_fb",
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 4/7] fbdev: sh_mobile_lcdc: Update fix.line_length in .fb_set_par()
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Instead of updating the fixed screen information line length manually
after calling fb_set_var() in sh_mobile_fb_reconfig(), update the field
in the .fb_set_par() operation handler.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |   17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index 1ff215c..b6da1d6 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -998,11 +998,6 @@ static void sh_mobile_fb_reconfig(struct fb_info *info)
 		/* Couldn't reconfigure, hopefully, can continue as before */
 		return;
 
-	if (info->var.nonstd)
-		info->fix.line_length = mode1.xres;
-	else
-		info->fix.line_length = mode1.xres * (ch->cfg.bpp / 8);
-
 	/*
 	 * fb_set_var() calls the notifier change internally, only if
 	 * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
@@ -1157,12 +1152,22 @@ static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *in
 static int sh_mobile_set_par(struct fb_info *info)
 {
 	struct sh_mobile_lcdc_chan *ch = info->par;
+	u32 line_length = info->fix.line_length;
 	int ret;
 
 	sh_mobile_lcdc_stop(ch->lcdc);
+
+	if (info->var.nonstd)
+		info->fix.line_length = info->var.xres;
+	else
+		info->fix.line_length = info->var.xres
+				      * info->var.bits_per_pixel / 8;
+
 	ret = sh_mobile_lcdc_start(ch->lcdc);
-	if (ret < 0)
+	if (ret < 0) {
 		dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
+		info->fix.line_length = line_length;
+	}
 
 	return ret;
 }
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 3/7] fbdev: sh_mobile_lcdc: use display information in info for panning
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

We must not use any information in the passed var besides xoffset,
yoffset and vmode as otherwise applications might abuse it.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |   24 ++++++++++++------------
 1 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index f9f420d..1ff215c 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -877,12 +877,12 @@ static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
 	unsigned long base_addr_y, base_addr_c;
 	unsigned long c_offset;
 
-	if (!var->nonstd)
-		new_pan_offset = (var->yoffset * info->fix.line_length) +
-			(var->xoffset * (info->var.bits_per_pixel / 8));
+	if (!info->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);
+		new_pan_offset = var->yoffset * info->fix.line_length
+			       + var->xoffset;
 
 	if (new_pan_offset = ch->pan_offset)
 		return 0;	/* No change, do nothing */
@@ -891,13 +891,13 @@ static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
 
 	/* Set the source address for the next refresh */
 	base_addr_y = ch->dma_handle + new_pan_offset;
-	if (var->nonstd) {
+	if (info->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;
+		c_offset = var->yoffset * info->fix.line_length
+			 * (info->var.bits_per_pixel - 8) / 8;
+		base_addr_c = ch->dma_handle
+			    + info->var.xres * info->var.yres_virtual
+			    + c_offset;
 		/* Set x offset */
 		if (info->var.bits_per_pixel = 24)
 			base_addr_c += 2 * var->xoffset;
@@ -923,7 +923,7 @@ static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
 	ch->base_addr_c = base_addr_c;
 
 	lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
-	if (var->nonstd)
+	if (info->var.nonstd)
 		lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
 
 	if (lcdc_chan_is_sublcd(ch))
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 2/7] fbdev: sh_mobile_lcdc: Add support for format changes at runtime
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Implement .fb_set_par to support frame buffer format changes at runtime.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index 33b0ff8..f9f420d 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -1154,6 +1154,19 @@ static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *in
 	return 0;
 }
 
+static int sh_mobile_set_par(struct fb_info *info)
+{
+	struct sh_mobile_lcdc_chan *ch = info->par;
+	int ret;
+
+	sh_mobile_lcdc_stop(ch->lcdc);
+	ret = sh_mobile_lcdc_start(ch->lcdc);
+	if (ret < 0)
+		dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
+
+	return ret;
+}
+
 /*
  * Screen blanking. Behavior is as follows:
  * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
@@ -1211,6 +1224,7 @@ static struct fb_ops sh_mobile_lcdc_ops = {
 	.fb_open	= sh_mobile_open,
 	.fb_release	= sh_mobile_release,
 	.fb_check_var	= sh_mobile_check_var,
+	.fb_set_par	= sh_mobile_set_par,
 };
 
 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 1/7] fbdev: sh_mobile_lcdc: Adjust requested parameters in .fb_check_var
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Instead of failing when the requested fb_var_screeninfo parameters are
not supported, adjust the parameters according to the hardware
capabilities.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 drivers/video/sh_mobile_lcdcfb.c |  103 ++++++++++++++++++++++++++++++++------
 1 files changed, 88 insertions(+), 15 deletions(-)

diff --git a/drivers/video/sh_mobile_lcdcfb.c b/drivers/video/sh_mobile_lcdcfb.c
index 088cb17..33b0ff8 100644
--- a/drivers/video/sh_mobile_lcdcfb.c
+++ b/drivers/video/sh_mobile_lcdcfb.c
@@ -1055,28 +1055,101 @@ static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *in
 {
 	struct sh_mobile_lcdc_chan *ch = info->par;
 	struct sh_mobile_lcdc_priv *p = ch->lcdc;
+	unsigned int best_dist = (unsigned int)-1;
+	unsigned int best_xres = 0;
+	unsigned int best_yres = 0;
+	unsigned int i;
 
-	if (var->xres > MAX_XRES || var->yres > MAX_YRES ||
-	    var->xres * var->yres * (ch->cfg.bpp / 8) * 2 > info->fix.smem_len) {
-		dev_warn(info->dev, "Invalid info: %u-%u-%u-%u x %u-%u-%u-%u @ %lukHz!\n",
-			 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));
+	if (var->xres > MAX_XRES || var->yres > MAX_YRES)
 		return -EINVAL;
+
+	/* If board code provides us with a list of available modes, make sure
+	 * we use one of them. Find the mode closest to the requested one. The
+	 * distance between two modes is defined as the size of the
+	 * non-overlapping parts of the two rectangles.
+	 */
+	for (i = 0; i < ch->cfg.num_cfg; ++i) {
+		const struct fb_videomode *mode = &ch->cfg.lcd_cfg[i];
+		unsigned int dist;
+
+		/* We can only round up. */
+		if (var->xres > mode->xres || var->yres > mode->yres)
+			continue;
+
+		dist = var->xres * var->yres + mode->xres * mode->yres
+		     - 2 * min(var->xres, mode->xres)
+			 * min(var->yres, mode->yres);
+
+		if (dist < best_dist) {
+			best_xres = mode->xres;
+			best_yres = mode->yres;
+			best_dist = dist;
+		}
 	}
 
-	/* only accept the forced_bpp for dual channel configurations */
-	if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
+	/* If no available mode can be used, return an error. */
+	if (ch->cfg.num_cfg != 0) {
+		if (best_dist = (unsigned int)-1)
+			return -EINVAL;
+
+		var->xres = best_xres;
+		var->yres = best_yres;
+	}
+
+	/* Make sure the virtual resolution is at least as big as the visible
+	 * resolution.
+	 */
+	if (var->xres_virtual < var->xres)
+		var->xres_virtual = var->xres;
+	if (var->yres_virtual < var->yres)
+		var->yres_virtual = var->yres;
+
+	if (var->bits_per_pixel <= 16) {		/* RGB 565 */
+		var->bits_per_pixel = 16;
+		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;
+	} else if (var->bits_per_pixel <= 24) {		/* RGB 888 */
+		var->bits_per_pixel = 24;
+		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 = 0;
+		var->transp.length = 0;
+	} else if (var->bits_per_pixel <= 32) {		/* RGBA 888 */
+		var->bits_per_pixel = 32;
+		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;
+	} else
 		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:
+	var->red.msb_right = 0;
+	var->green.msb_right = 0;
+	var->blue.msb_right = 0;
+	var->transp.msb_right = 0;
+
+	/* Make sure we don't exceed our allocated memory. */
+	if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
+	    info->fix.smem_len)
+		return -EINVAL;
+
+	/* only accept the forced_bpp for dual channel configurations */
+	if (p->forced_bpp && p->forced_bpp != var->bits_per_pixel)
 		return -EINVAL;
-	}
 
 	return 0;
 }
-- 
1.7.3.4


^ permalink raw reply related

* [PATCH 0/7] sh_mobile_lcdc: Support format changes at runtime
From: Laurent Pinchart @ 2011-08-19  9:16 UTC (permalink / raw)
  To: linux-fbdev

Hi everybody,

This patch set add supports for the .fb_set_par() operation in the
sh_mobile_lcdc driver, allowing userspace to change the frame buffer format
and size at runtime. It applies on top of the latest SH mobile LCDC cleanups
and fixes patches posted to the linux-fbdev list and available at
http://git.linuxtv.org/pinchartl/fbdev.git/shortlog/refs/heads/sh-mobile-lcdc.

Frame buffer memory reallocation is currently not supported. You will need to
make sure platform data sets the maximum size and bpp to high enough values
for all formats and sizes you want to support at runtime.

The patches have been tested with the fbdev-test utility
(http://git.ideasonboard.org/?pûdev-test.git;a=summary) on a Mackerel board.

Laurent Pinchart (7):
  fbdev: sh_mobile_lcdc: Adjust requested parameters in .fb_check_var
  fbdev: sh_mobile_lcdc: Add support for format changes at runtime
  fbdev: sh_mobile_lcdc: use display information in info for panning
  fbdev: sh_mobile_lcdc: Update fix.line_length in .fb_set_par()
  fbdev: sh_mobile_lcdc: Avoid forward declarations
  fbdev: sh_mobile_lcdc: Split channel initialization from probe function
  fbdev: sh_mobile_lcdc: Remove sh_mobile_lcdc_set_bpp()

 drivers/video/sh_mobile_lcdcfb.c |  570 +++++++++++++++++++++-----------------
 1 files changed, 309 insertions(+), 261 deletions(-)

-- 
Regards,

Laurent Pinchart


^ permalink raw reply

* [PULL] SH mobile LCDC cleanups and fixes
From: Laurent Pinchart @ 2011-08-19  8:44 UTC (permalink / raw)
  To: linux-fbdev

Hi Florian,

Here are the latest SH mobile LCDC and MERAM cleanups and fixes based on top
of v3.1-rc2. All the patches have been posted to the list. I've incorporated
acked-by and tested-by lines when available, but most of the patches received
no comment.

The following changes since commit 93ee7a9340d64f20295aacc3fb6a22b759323280:

  Linux 3.1-rc2 (2011-08-14 15:09:08 -0700)

are available in the git repository at:
  git://linuxtv.org/pinchartl/fbdev.git sh-mobile-lcdc

Damian Hobson-Garcia (5):
      fbdev: sh_mobile_meram: Enable runtime PM
      fbdev: sh_mobile_meram: Enable/disable MERAM along with LCDC
      fbdev: sh_mobile_meram: Move private data from .h to .c
      fbdev: sh_mobile_meram: Backup/restore device registers on shutdown/resume
      fbdev: sh_mobile_meram: Assign meram to the SH7372_A4LC power domain

Laurent Pinchart (11):
      fbdev: sh_mobile_lcdc: Turn dot clock on before resuming from runtime PM
      fbdev: sh_mobile_lcdc: Replace hardcoded register values with macros
      fbdev: sh_mobile_lcdc: Don't acknowlege interrupts unintentionally
      fbdev: sh_mobile_lcdc: Compute clock pattern using divider denominator
      fbdev: sh_mobile_lcdc: Split LCDC start code from sh_mobile_lcdc_start
      fbdev: sh_mobile_lcdc: Store the frame buffer base address when panning
      fbdev: sh_mobile_lcdc: Restart LCDC in runtime PM resume handler
      fbdev: sh_mobile_meram: Replace hardcoded register values with macros
      fbdev: sh_mobile_meram: Validate ICB configuration outside mutex
      fbdev: sh_mobile_meram: Fix MExxCTL register save on runtime PM suspend
      fbdev: sh_mobile_meram: Remove unneeded sh_mobile_meram.h

 arch/arm/mach-shmobile/board-mackerel.c |    1 +
 drivers/video/sh_mobile_lcdcfb.c        |  592 ++++++++++++++-----------------
 drivers/video/sh_mobile_lcdcfb.h        |   12 +-
 drivers/video/sh_mobile_meram.c         |  202 +++++++++--
 drivers/video/sh_mobile_meram.h         |   41 ---
 include/video/sh_mobile_lcdc.h          |  135 ++++++-
 6 files changed, 556 insertions(+), 427 deletions(-)
 delete mode 100644 drivers/video/sh_mobile_meram.h

-- 
Regards,

Laurent Pinchart

^ permalink raw reply

* [PULL] Use display information in info not in var for panning
From: Laurent Pinchart @ 2011-08-19  8:38 UTC (permalink / raw)
  To: linux-fbdev

Hi Florian (or should I call you Florian Tobias ?),

Here is the latest version of the pan patches based on top of v3.1-rc2. Apart 
from being rebased, they're identical to the v3 version posted to the linux-
fbdev mailing list.

The following changes since commit 93ee7a9340d64f20295aacc3fb6a22b759323280:

  Linux 3.1-rc2 (2011-08-14 15:09:08 -0700)

are available in the git repository at:
  git://linuxtv.org/pinchartl/fbdev.git pan-fixes

Laurent Pinchart (29):
      68328fb: use display information in info not in var for panning
      acornfb: Dont BUG() on invalid pan parameters
      acornfb: use display information in info not in var for panning
      arkfb: use display information in info not in var for panning
      atmel_lcdfb: use display information in info not in var for panning
      radeonfb: use display information in info not in var for panning
      fbdev: da8xx: use display information in info not in var for panning
      fbdev: unicore32: use display information in info not in var for panning
      g364fb: use display information in info not in var for panning
      gxt4500: use display information in info not in var for panning
      hgafb: use display information in info not in var for panning
      imsttfb: use display information in info not in var for panning
      intelfb: use display information in info not in var for panning
      mb862xxfb: use display information in info not in var for panning
      mx3fb: use display information in info not in var for panning
      neofb: use display information in info not in var for panning
      pm2fb: use display information in info not in var for panning
      pm3fb: use display information in info not in var for panning
      s3c-fb: use display information in info not in var for panning
      s3fb: use display information in info not in var for panning
      savagefb: use display information in info not in var for panning
      sisfb: use display information in info not in var for panning
      sm501fb: use display information in info not in var for panning
      tridentfb: use display information in info not in var for panning
      vfb: use display information in info not in var for panning
      vga16fb: use display information in info not in var for panning
      vt8500lcdfb: use display information in info not in var for panning
      vt8623fb: use display information in info not in var for panning
      staging: xgifb: use display information in info not in var for panning

 drivers/staging/xgifb/XGI_main_26.c    |   22 ++++++----------------
 drivers/video/68328fb.c                |    4 ++--
 drivers/video/acornfb.c                |    5 +++--
 drivers/video/arkfb.c                  |    9 +++++----
 drivers/video/atmel_lcdfb.c            |   15 +++++++++------
 drivers/video/aty/radeon_base.c        |   10 +++++-----
 drivers/video/da8xx-fb.c               |    4 ++--
 drivers/video/fb-puv3.c                |    4 ++--
 drivers/video/g364fb.c                 |    5 +++--
 drivers/video/gxt4500.c                |    4 ++--
 drivers/video/hgafb.c                  |    4 ++--
 drivers/video/imsttfb.c                |    2 +-
 drivers/video/intelfb/intelfbhw.c      |    6 +++---
 drivers/video/mb862xx/mb862xxfbdrv.c   |    2 +-
 drivers/video/mx3fb.c                  |    6 +++---
 drivers/video/neofb.c                  |    4 ++--
 drivers/video/pm2fb.c                  |    4 ++--
 drivers/video/pm3fb.c                  |    4 ++--
 drivers/video/s3c-fb.c                 |    2 +-
 drivers/video/s3fb.c                   |    7 ++++---
 drivers/video/savage/savagefb_driver.c |   16 +++++++---------
 drivers/video/sis/sis_main.c           |   30 ++++++++++--------------------
 drivers/video/sm501fb.c                |    6 +++---
 drivers/video/tridentfb.c              |    4 ++--
 drivers/video/vfb.c                    |    4 ++--
 drivers/video/vga16fb.c                |    2 +-
 drivers/video/vt8500lcdfb.c            |    4 ++--
 drivers/video/vt8623fb.c               |    9 +++++----
 28 files changed, 92 insertions(+), 106 deletions(-)

-- 
Regards,

Laurent Pinchart

^ permalink raw reply

* RE: [PATCH] backlight: l4f00242t03: Use gpio_request_one to
From: Estevam Fabio-R49496 @ 2011-08-19  3:58 UTC (permalink / raw)
  To: linux-fbdev

Richard,

Ping?

-----Original Message-----
From: Fabio Estevam [mailto:festevam@gmail.com] 
Sent: Friday, June 24, 2011 3:26 PM
To: linux-fbdev@vger.kernel.org
Cc: rpurdie@rpsys.net; Fabio Estevam; Estevam Fabio-R49496
Subject: [PATCH] backlight: l4f00242t03: Use gpio_request_one to simplify error handling

Using gpio_request_one can make the error handling simpler.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
---
 drivers/video/backlight/l4f00242t03.c |   17 +++++------------
 1 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/drivers/video/backlight/l4f00242t03.c b/drivers/video/backlight/l4f00242t03.c
index 98ad3e5..d6b0812 100644
--- a/drivers/video/backlight/l4f00242t03.c
+++ b/drivers/video/backlight/l4f00242t03.c
@@ -178,29 +178,22 @@ static int __devinit l4f00242t03_probe(struct spi_device *spi)
 
 	priv->spi = spi;
 
-	ret = gpio_request(pdata->reset_gpio, "lcd l4f00242t03 reset");
+	ret = gpio_request_one(pdata->reset_gpio, GPIOF_OUT_INIT_HIGH,
+						"lcd l4f00242t03 reset");
 	if (ret) {
 		dev_err(&spi->dev,
 			"Unable to get the lcd l4f00242t03 reset gpio.\n");
 		goto err;
 	}
 
-	ret = gpio_direction_output(pdata->reset_gpio, 1);
-	if (ret)
-		goto err2;
-
-	ret = gpio_request(pdata->data_enable_gpio,
-				"lcd l4f00242t03 data enable");
+	ret = gpio_request_one(pdata->data_enable_gpio, GPIOF_OUT_INIT_LOW,
+						"lcd l4f00242t03 data enable");
 	if (ret) {
 		dev_err(&spi->dev,
 			"Unable to get the lcd l4f00242t03 data en gpio.\n");
 		goto err2;
 	}
-
-	ret = gpio_direction_output(pdata->data_enable_gpio, 0);
-	if (ret)
-		goto err3;
-
+
 	if (pdata->io_supply) {
 		priv->io_reg = regulator_get(NULL, pdata->io_supply);
 
-- 
1.7.1




^ permalink raw reply related

* Re: [PATCH] backlight: Fix broken regulator API usage in l4f00242t03
From: Fabio Estevam @ 2011-08-19  3:50 UTC (permalink / raw)
  To: linux-arm-kernel
In-Reply-To: <1313419838-12150-1-git-send-email-broonie@opensource.wolfsonmicro.com>

On Mon, Aug 15, 2011 at 11:50 AM, Mark Brown
<broonie@opensource.wolfsonmicro.com> wrote:
> The regulator support in the l4f00242t03 is very non-idiomatic, rather
> than requesting the regulators based on the device name and the supply
> names used by the device the driver requires boards to pass system
> specific supply names around through platform data. The driver also
> conditionally requests the regulators based on this platform data, adding
> unneeded conditional code to the driver.
>
> Fix this by removing the platform data and converting to the standard idiom,
> also updating all in tree users of the driver. As no datasheet appears to
> be available for the LCD I'm guessing the names for the supplies based on
> the existing users and I've no ability to do anything more than compile
> test.
>
> The use of regulator_set_voltage() in the driver is also problematic, since
> fixed voltages are required the expectation would be that the voltages
> would be fixed in the constraints set by the machines rather than manually
> configured by the driver, but is less problematic.
>
> Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>

On a mx27_3ds:

Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

^ permalink raw reply

* [PATCH] [v2] video: miscellaneous minor changes to the Freescale DIU driver
From: Timur Tabi @ 2011-08-19  0:38 UTC (permalink / raw)
  To: linux-fbdev

Make several minor, miscellaneous changes to the Freescale DIU framebuffer
driver.  These changes "lighten" the code by removing crud, fixing small
bugs, and fixing some coding style problems.  These changes will make it
easier to make more substantial fixes in the future.

1. Fix incorrect indentation and spacing with some code.
2. Remove debug printks (they don't actually help in debugging the code).
3. Clean up some other printks (e.g. use pr_xxx, clean up the text, etc).
4. Remove the "default" videomode object since it's just a dupe of the
   first element in the videomode array.
5. Remove some superfluous local variables.
6. Rename ofdev to pdev, since it's a platform device not an OF device.
7. Fix some device tree operations.
8. Fix some build warnings.
9. Removed some unused structures from the header file.
10. Other minor bug fixes and changes.

Signed-off-by: Timur Tabi <timur@freescale.com>
---

v2: added a couple more changes that I missed.

 drivers/video/fsl-diu-fb.c |  362 +++++++++++++++-----------------------------
 include/linux/fsl-diu-fb.h |   12 +--
 2 files changed, 125 insertions(+), 249 deletions(-)

diff --git a/drivers/video/fsl-diu-fb.c b/drivers/video/fsl-diu-fb.c
index 0f1933b..1470581 100644
--- a/drivers/video/fsl-diu-fb.c
+++ b/drivers/video/fsl-diu-fb.c
@@ -36,26 +36,10 @@
 #include "edid.h"
 
 /*
- * These parameters give default parameters
- * for video output 1024x768,
- * FIXME - change timing to proper amounts
- * hsync 31.5kHz, vsync 60Hz
+ * List of supported video modes
+ *
+ * The first entry is the default video mode
  */
-static struct fb_videomode __devinitdata fsl_diu_default_mode = {
-	.refresh	= 60,
-	.xres		= 1024,
-	.yres		= 768,
-	.pixclock	= 15385,
-	.left_margin	= 160,
-	.right_margin	= 24,
-	.upper_margin	= 29,
-	.lower_margin	= 3,
-	.hsync_len	= 136,
-	.vsync_len	= 6,
-	.sync		= FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
-	.vmode		= FB_VMODE_NONINTERLACED
-};
-
 static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
 	{
 		.name		= "1024x768-60",
@@ -217,59 +201,59 @@ struct mfb_info {
 	int x_aoi_d;		/* aoi display x offset to physical screen */
 	int y_aoi_d;		/* aoi display y offset to physical screen */
 	struct fsl_diu_data *parent;
-	u8 *edid_data;
+	void *edid_data;
 };
 
 
 static struct mfb_info mfb_template[] = {
 	{		/* AOI 0 for plane 0 */
-	.index = 0,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel0",
-	.registered = 0,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 0,
+		.index = 0,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel0",
+		.registered = 0,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 0 for plane 1 */
-	.index = 1,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel1 AOI0",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 0,
+		.index = 1,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel1 AOI0",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 1 for plane 1 */
-	.index = 2,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel1 AOI1",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 480,
+		.index = 2,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel1 AOI1",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 480,
 	},
 	{		/* AOI 0 for plane 2 */
-	.index = 3,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel2 AOI0",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 640,
-	.y_aoi_d = 0,
+		.index = 3,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel2 AOI0",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 640,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 1 for plane 2 */
-	.index = 4,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel2 AOI1",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 640,
-	.y_aoi_d = 480,
+		.index = 4,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel2 AOI1",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 640,
+		.y_aoi_d = 480,
 	},
 };
 
@@ -322,14 +306,9 @@ static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
 {
 	void *virt;
 
-	pr_debug("size=%zu\n", size);
-
 	virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
-	if (virt) {
+	if (virt)
 		*phys = virt_to_phys(virt);
-		pr_debug("virt=%p phys=%llx\n", virt,
-			(unsigned long long)*phys);
-	}
 
 	return virt;
 }
@@ -343,8 +322,6 @@ static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
  */
 static void fsl_diu_free(void *virt, size_t size)
 {
-	pr_debug("virt=%p size=%zu\n", virt, size);
-
 	if (virt && size)
 		free_pages_exact(virt, size);
 }
@@ -368,7 +345,6 @@ static int fsl_diu_enable_panel(struct fb_info *info)
 	struct fsl_diu_data *machine_data = mfbi->parent;
 	int res = 0;
 
-	pr_debug("enable_panel index %d\n", mfbi->index);
 	if (mfbi->type != MFB_TYPE_OFF) {
 		switch (mfbi->index) {
 		case 0:				/* plane 0 */
@@ -585,9 +561,6 @@ static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
 static int fsl_diu_check_var(struct fb_var_screeninfo *var,
 				struct fb_info *info)
 {
-	pr_debug("check_var xres: %d\n", var->xres);
-	pr_debug("check_var yres: %d\n", var->yres);
-
 	if (var->xres_virtual < var->xres)
 		var->xres_virtual = var->xres;
 	if (var->yres_virtual < var->yres)
@@ -682,7 +655,7 @@ static void set_fix(struct fb_info *info)
 	struct fb_var_screeninfo *var = &info->var;
 	struct mfb_info *mfbi = info->par;
 
-	strncpy(fix->id, mfbi->id, strlen(mfbi->id));
+	strncpy(fix->id, mfbi->id, sizeof(fix->id));
 	fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
 	fix->type = FB_TYPE_PACKED_PIXELS;
 	fix->accel = FB_ACCEL_NONE;
@@ -715,12 +688,11 @@ static void update_lcdc(struct fb_info *info)
 	/* Prep for DIU init  - gamma table, cursor table */
 
 	for (i = 0; i <= 2; i++)
-	   for (j = 0; j <= 255; j++)
-	      *gamma_table_base++ = j;
+		for (j = 0; j <= 255; j++)
+			*gamma_table_base++ = j;
 
 	diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
 
-	pr_debug("update-lcdc: HW - %p\n Disabling DIU\n", hw);
 	disable_lcdc(info);
 
 	/* Program DIU registers */
@@ -732,9 +704,6 @@ static void update_lcdc(struct fb_info *info)
 	out_be32(&hw->bgnd_wb, 0); 		/* BGND_WB */
 	out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
 						/* DISP SIZE */
-	pr_debug("DIU xres: %d\n", var->xres);
-	pr_debug("DIU yres: %d\n", var->yres);
-
 	out_be32(&hw->wb_size, 0); /* WB SIZE */
 	out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
 
@@ -751,15 +720,6 @@ static void update_lcdc(struct fb_info *info)
 
 	out_be32(&hw->vsyn_para, temp);
 
-	pr_debug("DIU right_margin - %d\n", var->right_margin);
-	pr_debug("DIU left_margin - %d\n", var->left_margin);
-	pr_debug("DIU hsync_len - %d\n", var->hsync_len);
-	pr_debug("DIU upper_margin - %d\n", var->upper_margin);
-	pr_debug("DIU lower_margin - %d\n", var->lower_margin);
-	pr_debug("DIU vsync_len - %d\n", var->vsync_len);
-	pr_debug("DIU HSYNC - 0x%08x\n", hw->hsyn_para);
-	pr_debug("DIU VSYNC - 0x%08x\n", hw->vsyn_para);
-
 	diu_ops.set_pixel_clock(var->pixclock);
 
 	out_be32(&hw->syn_pol, 0);	/* SYNC SIGNALS POLARITY */
@@ -776,14 +736,9 @@ static int map_video_memory(struct fb_info *info)
 	phys_addr_t phys;
 	u32 smem_len = info->fix.line_length * info->var.yres_virtual;
 
-	pr_debug("info->var.xres_virtual = %d\n", info->var.xres_virtual);
-	pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
-	pr_debug("info->fix.line_length  = %d\n", info->fix.line_length);
-	pr_debug("MAP_VIDEO_MEMORY: smem_len = %u\n", smem_len);
-
 	info->screen_base = fsl_diu_alloc(smem_len, &phys);
 	if (info->screen_base = NULL) {
-		printk(KERN_ERR "Unable to allocate fb memory\n");
+		dev_err(info->dev, "unable to allocate fb memory\n");
 		return -ENOMEM;
 	}
 	mutex_lock(&info->mm_lock);
@@ -792,10 +747,6 @@ static int map_video_memory(struct fb_info *info)
 	mutex_unlock(&info->mm_lock);
 	info->screen_size = info->fix.smem_len;
 
-	pr_debug("Allocated fb @ paddr=0x%08lx, size=%d.\n",
-		 info->fix.smem_start, info->fix.smem_len);
-	pr_debug("screen base %p\n", info->screen_base);
-
 	return 0;
 }
 
@@ -852,11 +803,10 @@ static int fsl_diu_set_par(struct fb_info *info)
 	if (len != info->fix.smem_len) {
 		if (info->fix.smem_start)
 			unmap_video_memory(info);
-		pr_debug("SET PAR: smem_len = %d\n", info->fix.smem_len);
 
 		/* Memory allocation for framebuffer */
 		if (map_video_memory(info)) {
-			printk(KERN_ERR "Unable to allocate fb memory 1\n");
+			dev_err(info->dev, "unable to allocate fb memory 1\n");
 			return -ENOMEM;
 		}
 	}
@@ -887,7 +837,7 @@ static int fsl_diu_set_par(struct fb_info *info)
 
 static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
 {
-	return ((val<<width) + 0x7FFF - val)>>16;
+	return ((val << width) + 0x7FFF - val) >> 16;
 }
 
 /*
@@ -899,8 +849,9 @@ static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
  * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
  * color palette.
  */
-static int fsl_diu_setcolreg(unsigned regno, unsigned red, unsigned green,
-			   unsigned blue, unsigned transp, struct fb_info *info)
+static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
+			     unsigned int green, unsigned int blue,
+			     unsigned int transp, struct fb_info *info)
 {
 	int ret = 1;
 
@@ -935,9 +886,6 @@ static int fsl_diu_setcolreg(unsigned regno, unsigned red, unsigned green,
 			ret = 0;
 		}
 		break;
-	case FB_VISUAL_STATIC_PSEUDOCOLOR:
-	case FB_VISUAL_PSEUDOCOLOR:
-		break;
 	}
 
 	return ret;
@@ -1022,21 +970,17 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
 			return -EFAULT;
 		ad->pix_fmt = pix_fmt;
-		pr_debug("Set pixel format to 0x%08x\n", ad->pix_fmt);
 		break;
 	case MFB_GET_PIXFMT:
 		pix_fmt = ad->pix_fmt;
 		if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
 			return -EFAULT;
-		pr_debug("get pixel format 0x%08x\n", ad->pix_fmt);
 		break;
 	case MFB_SET_AOID:
 		if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
 			return -EFAULT;
 		mfbi->x_aoi_d = aoi_d.x_aoi_d;
 		mfbi->y_aoi_d = aoi_d.y_aoi_d;
-		pr_debug("set AOI display offset of index %d to (%d,%d)\n",
-				 mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
 		fsl_diu_check_var(&info->var, info);
 		fsl_diu_set_aoi(info);
 		break;
@@ -1045,14 +989,11 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		aoi_d.y_aoi_d = mfbi->y_aoi_d;
 		if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
 			return -EFAULT;
-		pr_debug("get AOI display offset of index %d (%d,%d)\n",
-				mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
 		break;
 	case MFB_GET_ALPHA:
 		global_alpha = mfbi->g_alpha;
 		if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
 			return -EFAULT;
-		pr_debug("get global alpha of index %d\n", mfbi->index);
 		break;
 	case MFB_SET_ALPHA:
 		/* set panel information */
@@ -1061,7 +1002,6 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
 							(global_alpha & 0xff);
 		mfbi->g_alpha = global_alpha;
-		pr_debug("set global alpha for index %d\n", mfbi->index);
 		break;
 	case MFB_SET_CHROMA_KEY:
 		/* set panel winformation */
@@ -1089,7 +1029,6 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 			ad->ckmin_g = ck.green_min;
 			ad->ckmin_b = ck.blue_min;
 		}
-		pr_debug("set chroma key\n");
 		break;
 	case FBIOGET_GWINFO:
 		if (mfbi->type = MFB_TYPE_OFF)
@@ -1098,18 +1037,9 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		if (copy_to_user(buf, ad, sizeof(*ad)))
 			return -EFAULT;
 		break;
-	case FBIOGET_HWCINFO:
-		pr_debug("FBIOGET_HWCINFO:0x%08x\n", FBIOGET_HWCINFO);
-		break;
-	case FBIOPUT_MODEINFO:
-		pr_debug("FBIOPUT_MODEINFO:0x%08x\n", FBIOPUT_MODEINFO);
-		break;
-	case FBIOGET_DISPINFO:
-		pr_debug("FBIOGET_DISPINFO:0x%08x\n", FBIOGET_DISPINFO);
-		break;
 
 	default:
-		printk(KERN_ERR "Unknown ioctl command (0x%08X)\n", cmd);
+		dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
 		return -ENOIOCTLCMD;
 	}
 
@@ -1130,7 +1060,6 @@ static int fsl_diu_open(struct fb_info *info, int user)
 	spin_lock(&diu_lock);
 	mfbi->count++;
 	if (mfbi->count = 1) {
-		pr_debug("open plane index %d\n", mfbi->index);
 		fsl_diu_check_var(&info->var, info);
 		res = fsl_diu_set_par(info);
 		if (res < 0)
@@ -1156,7 +1085,6 @@ static int fsl_diu_release(struct fb_info *info, int user)
 	spin_lock(&diu_lock);
 	mfbi->count--;
 	if (mfbi->count = 0) {
-		pr_debug("release plane index %d\n", mfbi->index);
 		res = fsl_diu_disable_panel(info);
 		if (res < 0)
 			mfbi->count++;
@@ -1221,26 +1149,9 @@ static int __devinit install_fb(struct fb_info *info)
 	} else {
 		aoi_mode = init_aoi_mode;
 	}
-	pr_debug("mode used = %s\n", aoi_mode);
-	rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize,
-			  &fsl_diu_default_mode, default_bpp);
-	switch (rc) {
-	case 1:
-		pr_debug("using mode specified in @mode\n");
-		break;
-	case 2:
-		pr_debug("using mode specified in @mode "
-			"with ignored refresh rate\n");
-		break;
-	case 3:
-		pr_debug("using mode default mode\n");
-		break;
-	case 4:
-		pr_debug("using mode from list\n");
-		break;
-	default:
-		pr_debug("rc = %d\n", rc);
-		pr_debug("failed to find mode\n");
+	rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
+			  default_bpp);
+	if (!rc) {
 		/*
 		 * For plane 0 we continue and look into
 		 * driver's internal modedb.
@@ -1249,15 +1160,12 @@ static int __devinit install_fb(struct fb_info *info)
 			has_default_mode = 0;
 		else
 			return -EINVAL;
-		break;
 	}
 
 	if (!has_default_mode) {
 		rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
-				  ARRAY_SIZE(fsl_diu_mode_db),
-				  &fsl_diu_default_mode,
-				  default_bpp);
-		if (rc > 0 && rc < 5)
+			ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
+		if (rc)
 			has_default_mode = 1;
 	}
 
@@ -1285,33 +1193,27 @@ static int __devinit install_fb(struct fb_info *info)
 		fb_videomode_to_var(&info->var, modedb);
 	}
 
-	pr_debug("xres_virtual %d\n", info->var.xres_virtual);
-	pr_debug("bits_per_pixel %d\n", info->var.bits_per_pixel);
-
-	pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
-	pr_debug("info->fix.line_length = %d\n", info->fix.line_length);
-
 	if (mfbi->type = MFB_TYPE_OFF)
 		mfbi->blank = FB_BLANK_NORMAL;
 	else
 		mfbi->blank = FB_BLANK_UNBLANK;
 
 	if (fsl_diu_check_var(&info->var, info)) {
-		printk(KERN_ERR "fb_check_var failed");
+		dev_err(info->dev, "fsl_diu_check_var failed\n");
+		unmap_video_memory(info);
 		fb_dealloc_cmap(&info->cmap);
 		return -EINVAL;
 	}
 
 	if (register_framebuffer(info) < 0) {
-		printk(KERN_ERR "register_framebuffer failed");
+		dev_err(info->dev, "register_framebuffer failed\n");
 		unmap_video_memory(info);
 		fb_dealloc_cmap(&info->cmap);
 		return -EINVAL;
 	}
 
 	mfbi->registered = 1;
-	printk(KERN_INFO "fb%d: %s fb device registered successfully.\n",
-		 info->node, info->fix.id);
+	dev_info(info->dev, "%s registered successfully\n", mfbi->id);
 
 	return 0;
 }
@@ -1343,13 +1245,13 @@ static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
 		/* This is the workaround for underrun */
 		if (status & INT_UNDRUN) {
 			out_be32(&hw->diu_mode, 0);
-			pr_debug("Err: DIU occurs underrun!\n");
 			udelay(1);
 			out_be32(&hw->diu_mode, 1);
 		}
 #if defined(CONFIG_NOT_COHERENT_CACHE)
 		else if (status & INT_VSYNC) {
 			unsigned int i;
+
 			for (i = 0; i < coherence_data_size;
 				i += d_cache_line_size)
 				__asm__ __volatile__ (
@@ -1364,30 +1266,30 @@ static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
 
 static int request_irq_local(int irq)
 {
-	unsigned long status, ints;
+	u32 ints;
 	struct diu *hw;
 	int ret;
 
 	hw = dr.diu_reg;
 
 	/* Read to clear the status */
-	status = in_be32(&hw->int_status);
+	in_be32(&hw->int_status);
 
-	ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
-	if (ret)
-		pr_info("Request diu IRQ failed.\n");
-	else {
+	ret = request_irq(irq, fsl_diu_isr, 0, "fsl-diu-fb", NULL);
+	if (!ret) {
 		ints = INT_PARERR | INT_LS_BF_VS;
 #if !defined(CONFIG_NOT_COHERENT_CACHE)
 		ints |=	INT_VSYNC;
 #endif
+
 		if (dr.mode = MFB_MODE2 || dr.mode = MFB_MODE3)
 			ints |= INT_VSYNC_WB;
 
 		/* Read to clear the status */
-		status = in_be32(&hw->int_status);
+		in_be32(&hw->int_status);
 		out_be32(&hw->int_mask, ints);
 	}
+
 	return ret;
 }
 
@@ -1435,34 +1337,31 @@ static int fsl_diu_resume(struct platform_device *ofdev)
 static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
 			u32 bytes_align)
 {
-	u32 offset, ssize;
-	u32 mask;
-	dma_addr_t paddr = 0;
+	u32 offset;
+	dma_addr_t mask;
 
-	ssize = size + bytes_align;
-	buf->vaddr = dma_alloc_coherent(dev, ssize, &paddr, GFP_DMA |
-							     __GFP_ZERO);
+	buf->vaddr +		dma_alloc_coherent(dev, size + bytes_align, &buf->paddr,
+				   GFP_DMA | __GFP_ZERO);
 	if (!buf->vaddr)
 		return -ENOMEM;
 
-	buf->paddr = (__u32) paddr;
-
 	mask = bytes_align - 1;
-	offset = (u32)buf->paddr & mask;
+	offset = buf->paddr & mask;
 	if (offset) {
 		buf->offset = bytes_align - offset;
-		buf->paddr = (u32)buf->paddr + offset;
+		buf->paddr = buf->paddr + offset;
 	} else
 		buf->offset = 0;
+
 	return 0;
 }
 
 static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
 		     u32 bytes_align)
 {
-	dma_free_coherent(dev, size + bytes_align,
-				buf->vaddr, (buf->paddr - buf->offset));
-	return;
+	dma_free_coherent(dev, size + bytes_align, buf->vaddr,
+			  buf->paddr - buf->offset);
 }
 
 static ssize_t store_monitor(struct device *device,
@@ -1506,13 +1405,12 @@ static ssize_t show_monitor(struct device *device,
 	return 0;
 }
 
-static int __devinit fsl_diu_probe(struct platform_device *ofdev)
+static int __devinit fsl_diu_probe(struct platform_device *pdev)
 {
-	struct device_node *np = ofdev->dev.of_node;
+	struct device_node *np = pdev->dev.of_node;
 	struct mfb_info *mfbi;
-	phys_addr_t dummy_ad_addr;
+	phys_addr_t dummy_ad_addr = 0;
 	int ret, i, error = 0;
-	struct resource res;
 	struct fsl_diu_data *machine_data;
 	int diu_mode;
 
@@ -1522,9 +1420,9 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 
 	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
 		machine_data->fsl_diu_info[i] -			framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
+			framebuffer_alloc(sizeof(struct mfb_info), &pdev->dev);
 		if (!machine_data->fsl_diu_info[i]) {
-			dev_err(&ofdev->dev, "cannot allocate memory\n");
+			dev_err(&pdev->dev, "cannot allocate memory\n");
 			ret = -ENOMEM;
 			goto error2;
 		}
@@ -1544,20 +1442,9 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 		}
 	}
 
-	ret = of_address_to_resource(np, 0, &res);
-	if (ret) {
-		dev_err(&ofdev->dev, "could not obtain DIU address\n");
-		goto error;
-	}
-	if (!res.start) {
-		dev_err(&ofdev->dev, "invalid DIU address\n");
-		goto error;
-	}
-	dev_dbg(&ofdev->dev, "%s, res.start: 0x%08x\n", __func__, res.start);
-
-	dr.diu_reg = ioremap(res.start, sizeof(struct diu));
+	dr.diu_reg = of_iomap(np, 0);
 	if (!dr.diu_reg) {
-		dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
+		dev_err(&pdev->dev, "cannot map DIU registers\n");
 		ret = -EFAULT;
 		goto error2;
 	}
@@ -1570,25 +1457,25 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 	machine_data->irq = irq_of_parse_and_map(np, 0);
 
 	if (!machine_data->irq) {
-		dev_err(&ofdev->dev, "could not get DIU IRQ\n");
+		dev_err(&pdev->dev, "could not get DIU IRQ\n");
 		ret = -EINVAL;
 		goto error;
 	}
 	machine_data->monitor_port = monitor_port;
 
 	/* Area descriptor memory pool aligns to 64-bit boundary */
-	if (allocate_buf(&ofdev->dev, &pool.ad,
+	if (allocate_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
 		return -ENOMEM;
 
 	/* Get memory for Gamma Table  - 32-byte aligned memory */
-	if (allocate_buf(&ofdev->dev, &pool.gamma, 768, 32)) {
+	if (allocate_buf(&pdev->dev, &pool.gamma, 768, 32)) {
 		ret = -ENOMEM;
 		goto error;
 	}
 
 	/* For performance, cursor bitmap buffer aligns to 32-byte boundary */
-	if (allocate_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
+	if (allocate_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
 			 32)) {
 		ret = -ENOMEM;
 		goto error;
@@ -1630,16 +1517,13 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 		mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
 		ret = install_fb(machine_data->fsl_diu_info[i]);
 		if (ret) {
-			dev_err(&ofdev->dev,
-				"Failed to register framebuffer %d\n",
-				i);
+			dev_err(&pdev->dev, "could not register fb %d\n", i);
 			goto error;
 		}
 	}
 
 	if (request_irq_local(machine_data->irq)) {
-		dev_err(machine_data->fsl_diu_info[0]->dev,
-			"could not request irq for diu.");
+		dev_err(&pdev->dev, "could not claim irq\n");
 		goto error;
 	}
 
@@ -1651,25 +1535,24 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 	error = device_create_file(machine_data->fsl_diu_info[0]->dev,
 				  &machine_data->dev_attr);
 	if (error) {
-		dev_err(machine_data->fsl_diu_info[0]->dev,
-			"could not create sysfs %s file\n",
+		dev_err(&pdev->dev, "could not create sysfs file %s\n",
 			machine_data->dev_attr.attr.name);
 	}
 
-	dev_set_drvdata(&ofdev->dev, machine_data);
+	dev_set_drvdata(&pdev->dev, machine_data);
 	return 0;
 
 error:
-	for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
-		i > 0; i--)
-		uninstall_fb(machine_data->fsl_diu_info[i - 1]);
+	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
+		uninstall_fb(machine_data->fsl_diu_info[i]);
+
 	if (pool.ad.vaddr)
-		free_buf(&ofdev->dev, &pool.ad,
+		free_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
 	if (pool.gamma.vaddr)
-		free_buf(&ofdev->dev, &pool.gamma, 768, 32);
+		free_buf(&pdev->dev, &pool.gamma, 768, 32);
 	if (pool.cursor.vaddr)
-		free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
+		free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
 			 32);
 	if (machine_data->dummy_aoi_virt)
 		fsl_diu_free(machine_data->dummy_aoi_virt, 64);
@@ -1684,25 +1567,23 @@ error2:
 	return ret;
 }
 
-
-static int fsl_diu_remove(struct platform_device *ofdev)
+static int fsl_diu_remove(struct platform_device *pdev)
 {
 	struct fsl_diu_data *machine_data;
 	int i;
 
-	machine_data = dev_get_drvdata(&ofdev->dev);
+	machine_data = dev_get_drvdata(&pdev->dev);
 	disable_lcdc(machine_data->fsl_diu_info[0]);
 	free_irq_local(machine_data->irq);
-	for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
-		uninstall_fb(machine_data->fsl_diu_info[i - 1]);
+	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
+		uninstall_fb(machine_data->fsl_diu_info[i]);
 	if (pool.ad.vaddr)
-		free_buf(&ofdev->dev, &pool.ad,
+		free_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
 	if (pool.gamma.vaddr)
-		free_buf(&ofdev->dev, &pool.gamma, 768, 32);
+		free_buf(&pdev->dev, &pool.gamma, 768, 32);
 	if (pool.cursor.vaddr)
-		free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
-			 32);
+		free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
 	if (machine_data->dummy_aoi_virt)
 		fsl_diu_free(machine_data->dummy_aoi_virt, 64);
 	iounmap(dr.diu_reg);
@@ -1754,7 +1635,7 @@ MODULE_DEVICE_TABLE(of, fsl_diu_match);
 
 static struct platform_driver fsl_diu_driver = {
 	.driver = {
-		.name = "fsl_diu",
+		.name = "fsl-diu-fb",
 		.owner = THIS_MODULE,
 		.of_match_table = fsl_diu_match,
 	},
@@ -1783,43 +1664,48 @@ static int __init fsl_diu_init(void)
 #else
 	monitor_port = fsl_diu_name_to_port(monitor_string);
 #endif
-	printk(KERN_INFO "Freescale DIU driver\n");
+	pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
 
 #ifdef CONFIG_NOT_COHERENT_CACHE
 	np = of_find_node_by_type(NULL, "cpu");
 	if (!np) {
-		printk(KERN_ERR "Err: can't find device node 'cpu'\n");
+		pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
 		return -ENODEV;
 	}
 
 	prop = of_get_property(np, "d-cache-size", NULL);
 	if (prop = NULL) {
+		pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
+		       "in 'cpu' node\n");
 		of_node_put(np);
 		return -ENODEV;
 	}
 
-	/* Freescale PLRU requires 13/8 times the cache size to do a proper
-	   displacement flush
+	/*
+	 * Freescale PLRU requires 13/8 times the cache size to do a proper
+	 * displacement flush
 	 */
-	coherence_data_size = *prop * 13;
+	coherence_data_size = be32_to_cpup(prop) * 13;
 	coherence_data_size /= 8;
 
 	prop = of_get_property(np, "d-cache-line-size", NULL);
 	if (prop = NULL) {
+		pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
+		       "in 'cpu' node\n");
 		of_node_put(np);
 		return -ENODEV;
 	}
-	d_cache_line_size = *prop;
+	d_cache_line_size = be32_to_cpup(prop);
 
 	of_node_put(np);
 	coherence_data = vmalloc(coherence_data_size);
 	if (!coherence_data)
 		return -ENOMEM;
 #endif
+
 	ret = platform_driver_register(&fsl_diu_driver);
 	if (ret) {
-		printk(KERN_ERR
-			"fsl-diu: failed to register platform driver\n");
+		pr_err("fsl-diu-fb: failed to register platform driver\n");
 #if defined(CONFIG_NOT_COHERENT_CACHE)
 		vfree(coherence_data);
 #endif
@@ -1847,7 +1733,7 @@ module_param_named(mode, fb_mode, charp, 0);
 MODULE_PARM_DESC(mode,
 	"Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
 module_param_named(bpp, default_bpp, ulong, 0);
-MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified mode");
+MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in \"mode\"");
 module_param_named(monitor, monitor_string, charp, 0);
 MODULE_PARM_DESC(monitor, "Specify the monitor port "
 	"(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
diff --git a/include/linux/fsl-diu-fb.h b/include/linux/fsl-diu-fb.h
index daa9952..df23f59 100644
--- a/include/linux/fsl-diu-fb.h
+++ b/include/linux/fsl-diu-fb.h
@@ -20,18 +20,8 @@
 #ifndef __FSL_DIU_FB_H__
 #define __FSL_DIU_FB_H__
 
-/* Arbitrary threshold to determine the allocation method
- * See mpc8610fb_set_par(), map_video_memory(), and unmap_video_memory()
- */
-#define MEM_ALLOC_THRESHOLD (1024*768*4+32)
-
 #include <linux/types.h>
 
-struct mfb_alpha {
-	int enable;
-	int alpha;
-};
-
 struct mfb_chroma_key {
 	int enable;
 	__u8  red_max;
@@ -167,7 +157,7 @@ struct diu_hw {
 };
 
 struct diu_addr {
-	__u8 __iomem *vaddr;	/* Virtual address */
+	void *vaddr;		/* Virtual address */
 	dma_addr_t paddr;	/* Physical address */
 	__u32 	   offset;
 };
-- 
1.7.4.4



^ permalink raw reply related

* [PATCH] video: miscellaneous minor changes to the Freescale DIU driver
From: Timur Tabi @ 2011-08-18 18:10 UTC (permalink / raw)
  To: linux-fbdev

Make several minor, miscellaneous changes to the Freescale DIU framebuffer
driver.  These changes "lighten" the code by removing crud, fixing small
bugs, and fixing some coding style problems.  These changes will make it
easier to make more substantial fixes in the future.

1. Fix incorrect indentation and spacing with some code.
2. Remove debug printks (they don't actually help in debugging the code).
3. Clean up some other printks (e.g. use pr_xxx, clean up the text, etc).
4. Remove the "default" videomode object since it's just a dupe of the
   first element in the videomode array.
5. Remove some superfluous local variables.
6. Rename ofdev to pdev, since it's a platform device not an OF device.
7. Fix endian-unsafe device tree references.
8. Fix some build warnings.
9. Other minor bug fixes and changes.

Signed-off-by: Timur Tabi <timur@freescale.com>
---
 drivers/video/fsl-diu-fb.c |  343 ++++++++++++++++----------------------------
 1 files changed, 121 insertions(+), 222 deletions(-)

diff --git a/drivers/video/fsl-diu-fb.c b/drivers/video/fsl-diu-fb.c
index 0f1933b..be1930b 100644
--- a/drivers/video/fsl-diu-fb.c
+++ b/drivers/video/fsl-diu-fb.c
@@ -36,26 +36,10 @@
 #include "edid.h"
 
 /*
- * These parameters give default parameters
- * for video output 1024x768,
- * FIXME - change timing to proper amounts
- * hsync 31.5kHz, vsync 60Hz
+ * List of supported video modes
+ *
+ * The first entry is the default video mode
  */
-static struct fb_videomode __devinitdata fsl_diu_default_mode = {
-	.refresh	= 60,
-	.xres		= 1024,
-	.yres		= 768,
-	.pixclock	= 15385,
-	.left_margin	= 160,
-	.right_margin	= 24,
-	.upper_margin	= 29,
-	.lower_margin	= 3,
-	.hsync_len	= 136,
-	.vsync_len	= 6,
-	.sync		= FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
-	.vmode		= FB_VMODE_NONINTERLACED
-};
-
 static struct fb_videomode __devinitdata fsl_diu_mode_db[] = {
 	{
 		.name		= "1024x768-60",
@@ -223,53 +207,53 @@ struct mfb_info {
 
 static struct mfb_info mfb_template[] = {
 	{		/* AOI 0 for plane 0 */
-	.index = 0,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel0",
-	.registered = 0,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 0,
+		.index = 0,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel0",
+		.registered = 0,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 0 for plane 1 */
-	.index = 1,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel1 AOI0",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 0,
+		.index = 1,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel1 AOI0",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 1 for plane 1 */
-	.index = 2,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel1 AOI1",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 0,
-	.y_aoi_d = 480,
+		.index = 2,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel1 AOI1",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 0,
+		.y_aoi_d = 480,
 	},
 	{		/* AOI 0 for plane 2 */
-	.index = 3,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel2 AOI0",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 640,
-	.y_aoi_d = 0,
+		.index = 3,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel2 AOI0",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 640,
+		.y_aoi_d = 0,
 	},
 	{		/* AOI 1 for plane 2 */
-	.index = 4,
-	.type = MFB_TYPE_OUTPUT,
-	.id = "Panel2 AOI1",
-	.registered = 0,
-	.g_alpha = 0xff,
-	.count = 0,
-	.x_aoi_d = 640,
-	.y_aoi_d = 480,
+		.index = 4,
+		.type = MFB_TYPE_OUTPUT,
+		.id = "Panel2 AOI1",
+		.registered = 0,
+		.g_alpha = 0xff,
+		.count = 0,
+		.x_aoi_d = 640,
+		.y_aoi_d = 480,
 	},
 };
 
@@ -322,14 +306,9 @@ static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
 {
 	void *virt;
 
-	pr_debug("size=%zu\n", size);
-
 	virt = alloc_pages_exact(size, GFP_DMA | __GFP_ZERO);
-	if (virt) {
+	if (virt)
 		*phys = virt_to_phys(virt);
-		pr_debug("virt=%p phys=%llx\n", virt,
-			(unsigned long long)*phys);
-	}
 
 	return virt;
 }
@@ -343,8 +322,6 @@ static void *fsl_diu_alloc(size_t size, phys_addr_t *phys)
  */
 static void fsl_diu_free(void *virt, size_t size)
 {
-	pr_debug("virt=%p size=%zu\n", virt, size);
-
 	if (virt && size)
 		free_pages_exact(virt, size);
 }
@@ -368,7 +345,6 @@ static int fsl_diu_enable_panel(struct fb_info *info)
 	struct fsl_diu_data *machine_data = mfbi->parent;
 	int res = 0;
 
-	pr_debug("enable_panel index %d\n", mfbi->index);
 	if (mfbi->type != MFB_TYPE_OFF) {
 		switch (mfbi->index) {
 		case 0:				/* plane 0 */
@@ -585,9 +561,6 @@ static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
 static int fsl_diu_check_var(struct fb_var_screeninfo *var,
 				struct fb_info *info)
 {
-	pr_debug("check_var xres: %d\n", var->xres);
-	pr_debug("check_var yres: %d\n", var->yres);
-
 	if (var->xres_virtual < var->xres)
 		var->xres_virtual = var->xres;
 	if (var->yres_virtual < var->yres)
@@ -682,7 +655,7 @@ static void set_fix(struct fb_info *info)
 	struct fb_var_screeninfo *var = &info->var;
 	struct mfb_info *mfbi = info->par;
 
-	strncpy(fix->id, mfbi->id, strlen(mfbi->id));
+	strncpy(fix->id, mfbi->id, sizeof(fix->id));
 	fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
 	fix->type = FB_TYPE_PACKED_PIXELS;
 	fix->accel = FB_ACCEL_NONE;
@@ -715,12 +688,11 @@ static void update_lcdc(struct fb_info *info)
 	/* Prep for DIU init  - gamma table, cursor table */
 
 	for (i = 0; i <= 2; i++)
-	   for (j = 0; j <= 255; j++)
-	      *gamma_table_base++ = j;
+		for (j = 0; j <= 255; j++)
+			*gamma_table_base++ = j;
 
 	diu_ops.set_gamma_table(machine_data->monitor_port, pool.gamma.vaddr);
 
-	pr_debug("update-lcdc: HW - %p\n Disabling DIU\n", hw);
 	disable_lcdc(info);
 
 	/* Program DIU registers */
@@ -732,9 +704,6 @@ static void update_lcdc(struct fb_info *info)
 	out_be32(&hw->bgnd_wb, 0); 		/* BGND_WB */
 	out_be32(&hw->disp_size, (var->yres << 16 | var->xres));
 						/* DISP SIZE */
-	pr_debug("DIU xres: %d\n", var->xres);
-	pr_debug("DIU yres: %d\n", var->yres);
-
 	out_be32(&hw->wb_size, 0); /* WB SIZE */
 	out_be32(&hw->wb_mem_addr, 0); /* WB MEM ADDR */
 
@@ -751,15 +720,6 @@ static void update_lcdc(struct fb_info *info)
 
 	out_be32(&hw->vsyn_para, temp);
 
-	pr_debug("DIU right_margin - %d\n", var->right_margin);
-	pr_debug("DIU left_margin - %d\n", var->left_margin);
-	pr_debug("DIU hsync_len - %d\n", var->hsync_len);
-	pr_debug("DIU upper_margin - %d\n", var->upper_margin);
-	pr_debug("DIU lower_margin - %d\n", var->lower_margin);
-	pr_debug("DIU vsync_len - %d\n", var->vsync_len);
-	pr_debug("DIU HSYNC - 0x%08x\n", hw->hsyn_para);
-	pr_debug("DIU VSYNC - 0x%08x\n", hw->vsyn_para);
-
 	diu_ops.set_pixel_clock(var->pixclock);
 
 	out_be32(&hw->syn_pol, 0);	/* SYNC SIGNALS POLARITY */
@@ -776,14 +736,9 @@ static int map_video_memory(struct fb_info *info)
 	phys_addr_t phys;
 	u32 smem_len = info->fix.line_length * info->var.yres_virtual;
 
-	pr_debug("info->var.xres_virtual = %d\n", info->var.xres_virtual);
-	pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
-	pr_debug("info->fix.line_length  = %d\n", info->fix.line_length);
-	pr_debug("MAP_VIDEO_MEMORY: smem_len = %u\n", smem_len);
-
 	info->screen_base = fsl_diu_alloc(smem_len, &phys);
 	if (info->screen_base = NULL) {
-		printk(KERN_ERR "Unable to allocate fb memory\n");
+		dev_err(info->dev, "unable to allocate fb memory\n");
 		return -ENOMEM;
 	}
 	mutex_lock(&info->mm_lock);
@@ -792,10 +747,6 @@ static int map_video_memory(struct fb_info *info)
 	mutex_unlock(&info->mm_lock);
 	info->screen_size = info->fix.smem_len;
 
-	pr_debug("Allocated fb @ paddr=0x%08lx, size=%d.\n",
-		 info->fix.smem_start, info->fix.smem_len);
-	pr_debug("screen base %p\n", info->screen_base);
-
 	return 0;
 }
 
@@ -852,11 +803,10 @@ static int fsl_diu_set_par(struct fb_info *info)
 	if (len != info->fix.smem_len) {
 		if (info->fix.smem_start)
 			unmap_video_memory(info);
-		pr_debug("SET PAR: smem_len = %d\n", info->fix.smem_len);
 
 		/* Memory allocation for framebuffer */
 		if (map_video_memory(info)) {
-			printk(KERN_ERR "Unable to allocate fb memory 1\n");
+			dev_err(info->dev, "unable to allocate fb memory 1\n");
 			return -ENOMEM;
 		}
 	}
@@ -887,7 +837,7 @@ static int fsl_diu_set_par(struct fb_info *info)
 
 static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
 {
-	return ((val<<width) + 0x7FFF - val)>>16;
+	return ((val << width) + 0x7FFF - val) >> 16;
 }
 
 /*
@@ -1022,21 +972,17 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
 			return -EFAULT;
 		ad->pix_fmt = pix_fmt;
-		pr_debug("Set pixel format to 0x%08x\n", ad->pix_fmt);
 		break;
 	case MFB_GET_PIXFMT:
 		pix_fmt = ad->pix_fmt;
 		if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
 			return -EFAULT;
-		pr_debug("get pixel format 0x%08x\n", ad->pix_fmt);
 		break;
 	case MFB_SET_AOID:
 		if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
 			return -EFAULT;
 		mfbi->x_aoi_d = aoi_d.x_aoi_d;
 		mfbi->y_aoi_d = aoi_d.y_aoi_d;
-		pr_debug("set AOI display offset of index %d to (%d,%d)\n",
-				 mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
 		fsl_diu_check_var(&info->var, info);
 		fsl_diu_set_aoi(info);
 		break;
@@ -1045,14 +991,11 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		aoi_d.y_aoi_d = mfbi->y_aoi_d;
 		if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
 			return -EFAULT;
-		pr_debug("get AOI display offset of index %d (%d,%d)\n",
-				mfbi->index, aoi_d.x_aoi_d, aoi_d.y_aoi_d);
 		break;
 	case MFB_GET_ALPHA:
 		global_alpha = mfbi->g_alpha;
 		if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
 			return -EFAULT;
-		pr_debug("get global alpha of index %d\n", mfbi->index);
 		break;
 	case MFB_SET_ALPHA:
 		/* set panel information */
@@ -1061,7 +1004,6 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
 							(global_alpha & 0xff);
 		mfbi->g_alpha = global_alpha;
-		pr_debug("set global alpha for index %d\n", mfbi->index);
 		break;
 	case MFB_SET_CHROMA_KEY:
 		/* set panel winformation */
@@ -1089,7 +1031,6 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 			ad->ckmin_g = ck.green_min;
 			ad->ckmin_b = ck.blue_min;
 		}
-		pr_debug("set chroma key\n");
 		break;
 	case FBIOGET_GWINFO:
 		if (mfbi->type = MFB_TYPE_OFF)
@@ -1098,18 +1039,9 @@ static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
 		if (copy_to_user(buf, ad, sizeof(*ad)))
 			return -EFAULT;
 		break;
-	case FBIOGET_HWCINFO:
-		pr_debug("FBIOGET_HWCINFO:0x%08x\n", FBIOGET_HWCINFO);
-		break;
-	case FBIOPUT_MODEINFO:
-		pr_debug("FBIOPUT_MODEINFO:0x%08x\n", FBIOPUT_MODEINFO);
-		break;
-	case FBIOGET_DISPINFO:
-		pr_debug("FBIOGET_DISPINFO:0x%08x\n", FBIOGET_DISPINFO);
-		break;
 
 	default:
-		printk(KERN_ERR "Unknown ioctl command (0x%08X)\n", cmd);
+		dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
 		return -ENOIOCTLCMD;
 	}
 
@@ -1130,7 +1062,6 @@ static int fsl_diu_open(struct fb_info *info, int user)
 	spin_lock(&diu_lock);
 	mfbi->count++;
 	if (mfbi->count = 1) {
-		pr_debug("open plane index %d\n", mfbi->index);
 		fsl_diu_check_var(&info->var, info);
 		res = fsl_diu_set_par(info);
 		if (res < 0)
@@ -1156,7 +1087,6 @@ static int fsl_diu_release(struct fb_info *info, int user)
 	spin_lock(&diu_lock);
 	mfbi->count--;
 	if (mfbi->count = 0) {
-		pr_debug("release plane index %d\n", mfbi->index);
 		res = fsl_diu_disable_panel(info);
 		if (res < 0)
 			mfbi->count++;
@@ -1221,26 +1151,9 @@ static int __devinit install_fb(struct fb_info *info)
 	} else {
 		aoi_mode = init_aoi_mode;
 	}
-	pr_debug("mode used = %s\n", aoi_mode);
-	rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize,
-			  &fsl_diu_default_mode, default_bpp);
-	switch (rc) {
-	case 1:
-		pr_debug("using mode specified in @mode\n");
-		break;
-	case 2:
-		pr_debug("using mode specified in @mode "
-			"with ignored refresh rate\n");
-		break;
-	case 3:
-		pr_debug("using mode default mode\n");
-		break;
-	case 4:
-		pr_debug("using mode from list\n");
-		break;
-	default:
-		pr_debug("rc = %d\n", rc);
-		pr_debug("failed to find mode\n");
+	rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
+			  default_bpp);
+	if (!rc) {
 		/*
 		 * For plane 0 we continue and look into
 		 * driver's internal modedb.
@@ -1249,15 +1162,12 @@ static int __devinit install_fb(struct fb_info *info)
 			has_default_mode = 0;
 		else
 			return -EINVAL;
-		break;
 	}
 
 	if (!has_default_mode) {
 		rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
-				  ARRAY_SIZE(fsl_diu_mode_db),
-				  &fsl_diu_default_mode,
-				  default_bpp);
-		if (rc > 0 && rc < 5)
+			ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
+		if (rc)
 			has_default_mode = 1;
 	}
 
@@ -1285,33 +1195,27 @@ static int __devinit install_fb(struct fb_info *info)
 		fb_videomode_to_var(&info->var, modedb);
 	}
 
-	pr_debug("xres_virtual %d\n", info->var.xres_virtual);
-	pr_debug("bits_per_pixel %d\n", info->var.bits_per_pixel);
-
-	pr_debug("info->var.yres_virtual = %d\n", info->var.yres_virtual);
-	pr_debug("info->fix.line_length = %d\n", info->fix.line_length);
-
 	if (mfbi->type = MFB_TYPE_OFF)
 		mfbi->blank = FB_BLANK_NORMAL;
 	else
 		mfbi->blank = FB_BLANK_UNBLANK;
 
 	if (fsl_diu_check_var(&info->var, info)) {
-		printk(KERN_ERR "fb_check_var failed");
+		dev_err(info->dev, "fsl_diu_check_var failed\n");
+		unmap_video_memory(info);
 		fb_dealloc_cmap(&info->cmap);
 		return -EINVAL;
 	}
 
 	if (register_framebuffer(info) < 0) {
-		printk(KERN_ERR "register_framebuffer failed");
+		dev_err(info->dev, "register_framebuffer failed\n");
 		unmap_video_memory(info);
 		fb_dealloc_cmap(&info->cmap);
 		return -EINVAL;
 	}
 
 	mfbi->registered = 1;
-	printk(KERN_INFO "fb%d: %s fb device registered successfully.\n",
-		 info->node, info->fix.id);
+	dev_info(info->dev, "%s registered successfully\n", mfbi->id);
 
 	return 0;
 }
@@ -1343,13 +1247,13 @@ static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
 		/* This is the workaround for underrun */
 		if (status & INT_UNDRUN) {
 			out_be32(&hw->diu_mode, 0);
-			pr_debug("Err: DIU occurs underrun!\n");
 			udelay(1);
 			out_be32(&hw->diu_mode, 1);
 		}
 #if defined(CONFIG_NOT_COHERENT_CACHE)
 		else if (status & INT_VSYNC) {
 			unsigned int i;
+
 			for (i = 0; i < coherence_data_size;
 				i += d_cache_line_size)
 				__asm__ __volatile__ (
@@ -1364,30 +1268,30 @@ static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
 
 static int request_irq_local(int irq)
 {
-	unsigned long status, ints;
+	u32 ints;
 	struct diu *hw;
 	int ret;
 
 	hw = dr.diu_reg;
 
 	/* Read to clear the status */
-	status = in_be32(&hw->int_status);
+	in_be32(&hw->int_status);
 
-	ret = request_irq(irq, fsl_diu_isr, 0, "diu", NULL);
-	if (ret)
-		pr_info("Request diu IRQ failed.\n");
-	else {
+	ret = request_irq(irq, fsl_diu_isr, 0, "fsl-diu-fb", NULL);
+	if (!ret) {
 		ints = INT_PARERR | INT_LS_BF_VS;
 #if !defined(CONFIG_NOT_COHERENT_CACHE)
 		ints |=	INT_VSYNC;
 #endif
+
 		if (dr.mode = MFB_MODE2 || dr.mode = MFB_MODE3)
 			ints |= INT_VSYNC_WB;
 
 		/* Read to clear the status */
-		status = in_be32(&hw->int_status);
+		in_be32(&hw->int_status);
 		out_be32(&hw->int_mask, ints);
 	}
+
 	return ret;
 }
 
@@ -1435,34 +1339,31 @@ static int fsl_diu_resume(struct platform_device *ofdev)
 static int allocate_buf(struct device *dev, struct diu_addr *buf, u32 size,
 			u32 bytes_align)
 {
-	u32 offset, ssize;
-	u32 mask;
-	dma_addr_t paddr = 0;
+	u32 offset;
+	dma_addr_t mask;
 
-	ssize = size + bytes_align;
-	buf->vaddr = dma_alloc_coherent(dev, ssize, &paddr, GFP_DMA |
-							     __GFP_ZERO);
+	buf->vaddr +		dma_alloc_coherent(dev, size + bytes_align, &buf->paddr,
+				   GFP_DMA | __GFP_ZERO);
 	if (!buf->vaddr)
 		return -ENOMEM;
 
-	buf->paddr = (__u32) paddr;
-
 	mask = bytes_align - 1;
-	offset = (u32)buf->paddr & mask;
+	offset = buf->paddr & mask;
 	if (offset) {
 		buf->offset = bytes_align - offset;
-		buf->paddr = (u32)buf->paddr + offset;
+		buf->paddr = buf->paddr + offset;
 	} else
 		buf->offset = 0;
+
 	return 0;
 }
 
 static void free_buf(struct device *dev, struct diu_addr *buf, u32 size,
 		     u32 bytes_align)
 {
-	dma_free_coherent(dev, size + bytes_align,
-				buf->vaddr, (buf->paddr - buf->offset));
-	return;
+	dma_free_coherent(dev, size + bytes_align, buf->vaddr,
+			  buf->paddr - buf->offset);
 }
 
 static ssize_t store_monitor(struct device *device,
@@ -1506,11 +1407,11 @@ static ssize_t show_monitor(struct device *device,
 	return 0;
 }
 
-static int __devinit fsl_diu_probe(struct platform_device *ofdev)
+static int __devinit fsl_diu_probe(struct platform_device *pdev)
 {
-	struct device_node *np = ofdev->dev.of_node;
+	struct device_node *np = pdev->dev.of_node;
 	struct mfb_info *mfbi;
-	phys_addr_t dummy_ad_addr;
+	phys_addr_t dummy_ad_addr = 0;
 	int ret, i, error = 0;
 	struct resource res;
 	struct fsl_diu_data *machine_data;
@@ -1522,9 +1423,9 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 
 	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++) {
 		machine_data->fsl_diu_info[i] -			framebuffer_alloc(sizeof(struct mfb_info), &ofdev->dev);
+			framebuffer_alloc(sizeof(struct mfb_info), &pdev->dev);
 		if (!machine_data->fsl_diu_info[i]) {
-			dev_err(&ofdev->dev, "cannot allocate memory\n");
+			dev_err(&pdev->dev, "cannot allocate memory\n");
 			ret = -ENOMEM;
 			goto error2;
 		}
@@ -1546,18 +1447,17 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 
 	ret = of_address_to_resource(np, 0, &res);
 	if (ret) {
-		dev_err(&ofdev->dev, "could not obtain DIU address\n");
+		dev_err(&pdev->dev, "could not obtain DIU address\n");
 		goto error;
 	}
 	if (!res.start) {
-		dev_err(&ofdev->dev, "invalid DIU address\n");
+		dev_err(&pdev->dev, "invalid DIU address\n");
 		goto error;
 	}
-	dev_dbg(&ofdev->dev, "%s, res.start: 0x%08x\n", __func__, res.start);
 
 	dr.diu_reg = ioremap(res.start, sizeof(struct diu));
 	if (!dr.diu_reg) {
-		dev_err(&ofdev->dev, "Err: can't map DIU registers!\n");
+		dev_err(&pdev->dev, "cannot map DIU registers\n");
 		ret = -EFAULT;
 		goto error2;
 	}
@@ -1570,25 +1470,25 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 	machine_data->irq = irq_of_parse_and_map(np, 0);
 
 	if (!machine_data->irq) {
-		dev_err(&ofdev->dev, "could not get DIU IRQ\n");
+		dev_err(&pdev->dev, "could not get DIU IRQ\n");
 		ret = -EINVAL;
 		goto error;
 	}
 	machine_data->monitor_port = monitor_port;
 
 	/* Area descriptor memory pool aligns to 64-bit boundary */
-	if (allocate_buf(&ofdev->dev, &pool.ad,
+	if (allocate_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8))
 		return -ENOMEM;
 
 	/* Get memory for Gamma Table  - 32-byte aligned memory */
-	if (allocate_buf(&ofdev->dev, &pool.gamma, 768, 32)) {
+	if (allocate_buf(&pdev->dev, &pool.gamma, 768, 32)) {
 		ret = -ENOMEM;
 		goto error;
 	}
 
 	/* For performance, cursor bitmap buffer aligns to 32-byte boundary */
-	if (allocate_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
+	if (allocate_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
 			 32)) {
 		ret = -ENOMEM;
 		goto error;
@@ -1630,16 +1530,13 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 		mfbi->ad->paddr = pool.ad.paddr + i * sizeof(struct diu_ad);
 		ret = install_fb(machine_data->fsl_diu_info[i]);
 		if (ret) {
-			dev_err(&ofdev->dev,
-				"Failed to register framebuffer %d\n",
-				i);
+			dev_err(&pdev->dev, "could not register fb %d\n", i);
 			goto error;
 		}
 	}
 
 	if (request_irq_local(machine_data->irq)) {
-		dev_err(machine_data->fsl_diu_info[0]->dev,
-			"could not request irq for diu.");
+		dev_err(&pdev->dev, "could not claim irq\n");
 		goto error;
 	}
 
@@ -1651,25 +1548,24 @@ static int __devinit fsl_diu_probe(struct platform_device *ofdev)
 	error = device_create_file(machine_data->fsl_diu_info[0]->dev,
 				  &machine_data->dev_attr);
 	if (error) {
-		dev_err(machine_data->fsl_diu_info[0]->dev,
-			"could not create sysfs %s file\n",
+		dev_err(&pdev->dev, "could not create sysfs file %s\n",
 			machine_data->dev_attr.attr.name);
 	}
 
-	dev_set_drvdata(&ofdev->dev, machine_data);
+	dev_set_drvdata(&pdev->dev, machine_data);
 	return 0;
 
 error:
-	for (i = ARRAY_SIZE(machine_data->fsl_diu_info);
-		i > 0; i--)
-		uninstall_fb(machine_data->fsl_diu_info[i - 1]);
+	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
+		uninstall_fb(machine_data->fsl_diu_info[i]);
+
 	if (pool.ad.vaddr)
-		free_buf(&ofdev->dev, &pool.ad,
+		free_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
 	if (pool.gamma.vaddr)
-		free_buf(&ofdev->dev, &pool.gamma, 768, 32);
+		free_buf(&pdev->dev, &pool.gamma, 768, 32);
 	if (pool.cursor.vaddr)
-		free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
+		free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
 			 32);
 	if (machine_data->dummy_aoi_virt)
 		fsl_diu_free(machine_data->dummy_aoi_virt, 64);
@@ -1684,25 +1580,23 @@ error2:
 	return ret;
 }
 
-
-static int fsl_diu_remove(struct platform_device *ofdev)
+static int fsl_diu_remove(struct platform_device *pdev)
 {
 	struct fsl_diu_data *machine_data;
 	int i;
 
-	machine_data = dev_get_drvdata(&ofdev->dev);
+	machine_data = dev_get_drvdata(&pdev->dev);
 	disable_lcdc(machine_data->fsl_diu_info[0]);
 	free_irq_local(machine_data->irq);
-	for (i = ARRAY_SIZE(machine_data->fsl_diu_info); i > 0; i--)
-		uninstall_fb(machine_data->fsl_diu_info[i - 1]);
+	for (i = 0; i < ARRAY_SIZE(machine_data->fsl_diu_info); i++)
+		uninstall_fb(machine_data->fsl_diu_info[i]);
 	if (pool.ad.vaddr)
-		free_buf(&ofdev->dev, &pool.ad,
+		free_buf(&pdev->dev, &pool.ad,
 			 sizeof(struct diu_ad) * FSL_AOI_NUM, 8);
 	if (pool.gamma.vaddr)
-		free_buf(&ofdev->dev, &pool.gamma, 768, 32);
+		free_buf(&pdev->dev, &pool.gamma, 768, 32);
 	if (pool.cursor.vaddr)
-		free_buf(&ofdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2,
-			 32);
+		free_buf(&pdev->dev, &pool.cursor, MAX_CURS * MAX_CURS * 2, 32);
 	if (machine_data->dummy_aoi_virt)
 		fsl_diu_free(machine_data->dummy_aoi_virt, 64);
 	iounmap(dr.diu_reg);
@@ -1754,7 +1648,7 @@ MODULE_DEVICE_TABLE(of, fsl_diu_match);
 
 static struct platform_driver fsl_diu_driver = {
 	.driver = {
-		.name = "fsl_diu",
+		.name = "fsl-diu-fb",
 		.owner = THIS_MODULE,
 		.of_match_table = fsl_diu_match,
 	},
@@ -1783,43 +1677,48 @@ static int __init fsl_diu_init(void)
 #else
 	monitor_port = fsl_diu_name_to_port(monitor_string);
 #endif
-	printk(KERN_INFO "Freescale DIU driver\n");
+	pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
 
 #ifdef CONFIG_NOT_COHERENT_CACHE
 	np = of_find_node_by_type(NULL, "cpu");
 	if (!np) {
-		printk(KERN_ERR "Err: can't find device node 'cpu'\n");
+		pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
 		return -ENODEV;
 	}
 
 	prop = of_get_property(np, "d-cache-size", NULL);
 	if (prop = NULL) {
+		pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
+		       "in 'cpu' node\n");
 		of_node_put(np);
 		return -ENODEV;
 	}
 
-	/* Freescale PLRU requires 13/8 times the cache size to do a proper
-	   displacement flush
+	/*
+	 * Freescale PLRU requires 13/8 times the cache size to do a proper
+	 * displacement flush
 	 */
-	coherence_data_size = *prop * 13;
+	coherence_data_size = be32_to_cpup(prop) * 13;
 	coherence_data_size /= 8;
 
 	prop = of_get_property(np, "d-cache-line-size", NULL);
 	if (prop = NULL) {
+		pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
+		       "in 'cpu' node\n");
 		of_node_put(np);
 		return -ENODEV;
 	}
-	d_cache_line_size = *prop;
+	d_cache_line_size = be32_to_cpup(prop);
 
 	of_node_put(np);
 	coherence_data = vmalloc(coherence_data_size);
 	if (!coherence_data)
 		return -ENOMEM;
 #endif
+
 	ret = platform_driver_register(&fsl_diu_driver);
 	if (ret) {
-		printk(KERN_ERR
-			"fsl-diu: failed to register platform driver\n");
+		pr_err("fsl-diu-fb: failed to register platform driver\n");
 #if defined(CONFIG_NOT_COHERENT_CACHE)
 		vfree(coherence_data);
 #endif
@@ -1847,7 +1746,7 @@ module_param_named(mode, fb_mode, charp, 0);
 MODULE_PARM_DESC(mode,
 	"Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
 module_param_named(bpp, default_bpp, ulong, 0);
-MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified mode");
+MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in \"mode\"");
 module_param_named(monitor, monitor_string, charp, 0);
 MODULE_PARM_DESC(monitor, "Specify the monitor port "
 	"(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");
-- 
1.7.4.4



^ permalink raw reply related

* [PATCH] Add support for SMSC UFX6000/7000 USB display adapters
From: Steve Glendinning @ 2011-08-18 14:20 UTC (permalink / raw)
  To: linux-fbdev
In-Reply-To: <1313150823-2527-1-git-send-email-steve.glendinning@smsc.com>

This patch adds framebuffer suport for SMSC's UFX6000 (USB 2.0) and
UFX7000 (USB 3.0) display adapters.

Signed-off-by: Steve Glendinning <steve.glendinning@smsc.com>
---
 MAINTAINERS             |    6 +
 drivers/video/Kconfig   |   16 +
 drivers/video/Makefile  |    1 +
 drivers/video/smscufx.c | 1994 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 2017 insertions(+), 0 deletions(-)
 create mode 100644 drivers/video/smscufx.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 1d445f5..e7b6b6d 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5920,6 +5920,12 @@ L:	netdev@vger.kernel.org
 S:	Supported
 F:	drivers/net/smsc9420.*
 
+SMSC UFX6000 and UFX7000 USB to VGA DRIVER
+M:	Steve Glendinning <steve.glendinning@smsc.com>
+L:	linux-fbdev@vger.kernel.org
+S:	Supported
+F:	drivers/video/smscufx.c
+
 SN-IA64 (Itanium) SUB-PLATFORM
 M:	Jes Sorensen <jes@sgi.com>
 L:	linux-altix@sgi.com
diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
index 549b960..8bfb387 100644
--- a/drivers/video/Kconfig
+++ b/drivers/video/Kconfig
@@ -2110,6 +2110,22 @@ config FB_SM501
 
 	  If unsure, say N.
 
+config FB_SMSCUFX
+	tristate "SMSC UFX6000/7000 USB Framebuffer support"
+	depends on FB && USB
+	select FB_MODE_HELPERS
+	select FB_SYS_FILLRECT
+	select FB_SYS_COPYAREA
+	select FB_SYS_IMAGEBLIT
+	select FB_SYS_FOPS
+	select FB_DEFERRED_IO
+	---help---
+	  This is a kernel framebuffer driver for SMSC UFX USB devices.
+	  Supports fbdev clients like xf86-video-fbdev, kdrive, fbi, and
+	  mplayer -vo fbdev. Supports both UFX6000 (USB 2.0) and UFX7000
+	  (USB 3.0) devices.
+	  To compile as a module, choose M here: the module name is smscufx.
+
 config FB_UDL
 	tristate "Displaylink USB Framebuffer support"
 	depends on FB && USB
diff --git a/drivers/video/Makefile b/drivers/video/Makefile
index 8b83129..f3546ec 100644
--- a/drivers/video/Makefile
+++ b/drivers/video/Makefile
@@ -127,6 +127,7 @@ obj-$(CONFIG_FB_IBM_GXT4500)	  += gxt4500.o
 obj-$(CONFIG_FB_PS3)		  += ps3fb.o
 obj-$(CONFIG_FB_SM501)            += sm501fb.o
 obj-$(CONFIG_FB_UDL)		  += udlfb.o
+obj-$(CONFIG_FB_SMSCUFX)	  += smscufx.o
 obj-$(CONFIG_FB_XILINX)           += xilinxfb.o
 obj-$(CONFIG_SH_MIPI_DSI)	  += sh_mipi_dsi.o
 obj-$(CONFIG_FB_SH_MOBILE_HDMI)	  += sh_mobile_hdmi.o
diff --git a/drivers/video/smscufx.c b/drivers/video/smscufx.c
new file mode 100644
index 0000000..c6b86e7
--- /dev/null
+++ b/drivers/video/smscufx.c
@@ -0,0 +1,1994 @@
+/*
+ * smscufx.c -- Framebuffer driver for SMSC UFX USB controller
+ *
+ * Copyright (C) 2011 Steve Glendinning <steve.glendinning@smsc.com>
+ * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
+ * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
+ * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License v2. See the file COPYING in the main directory of this archive for
+ * more details.
+ *
+ * Based on udlfb, with work from Florian Echtler, Henrik Bjerregaard Pedersen,
+ * and others.
+ *
+ * Works well with Bernie Thompson's X DAMAGE patch to xf86-video-fbdev
+ * available from http://git.plugable.com
+ *
+ * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
+ * usb-skeleton by GregKH.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/usb.h>
+#include <linux/uaccess.h>
+#include <linux/mm.h>
+#include <linux/fb.h>
+#include <linux/vmalloc.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+#include "edid.h"
+
+#define check_warn(status, fmt, args...) \
+	({ if (status < 0) pr_warn(fmt, ##args); })
+
+#define check_warn_return(status, fmt, args...) \
+	({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
+
+#define check_warn_goto_error(status, fmt, args...) \
+	({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
+
+#define all_bits_set(x, bits) (((x) & (bits)) = (bits))
+
+#define USB_VENDOR_REQUEST_WRITE_REGISTER	0xA0
+#define USB_VENDOR_REQUEST_READ_REGISTER	0xA1
+
+/*
+ * TODO: Propose standard fb.h ioctl for reporting damage,
+ * using _IOWR() and one of the existing area structs from fb.h
+ * Consider these ioctls deprecated, but they're still used by the
+ * DisplayLink X server as yet - need both to be modified in tandem
+ * when new ioctl(s) are ready.
+ */
+#define UFX_IOCTL_RETURN_EDID	(0xAD)
+#define UFX_IOCTL_REPORT_DAMAGE	(0xAA)
+
+/* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
+#define BULK_SIZE		(512)
+#define MAX_TRANSFER		(PAGE_SIZE*16 - BULK_SIZE)
+#define WRITES_IN_FLIGHT	(4)
+
+#define GET_URB_TIMEOUT		(HZ)
+#define FREE_URB_TIMEOUT	(HZ*2)
+
+#define BPP			2
+
+#define UFX_DEFIO_WRITE_DELAY	5 /* fb_deferred_io.delay in jiffies */
+#define UFX_DEFIO_WRITE_DISABLE	(HZ*60) /* "disable" with long delay */
+
+struct dloarea {
+	int x, y;
+	int w, h;
+};
+
+struct urb_node {
+	struct list_head entry;
+	struct ufx_data *dev;
+	struct delayed_work release_urb_work;
+	struct urb *urb;
+};
+
+struct urb_list {
+	struct list_head list;
+	spinlock_t lock;
+	struct semaphore limit_sem;
+	int available;
+	int count;
+	size_t size;
+};
+
+struct ufx_data {
+	struct usb_device *udev;
+	struct device *gdev; /* &udev->dev */
+	struct fb_info *info;
+	struct urb_list urbs;
+	struct kref kref;
+	int fb_count;
+	bool virtualized; /* true when physical usb device not present */
+	struct delayed_work free_framebuffer_work;
+	atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */
+	atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */
+	char *edid; /* null until we read edid from hw or get from sysfs */
+	size_t edid_size;
+	u32 pseudo_palette[256];
+};
+
+static struct fb_fix_screeninfo ufx_fix = {
+	.id =           "smscufx",
+	.type =         FB_TYPE_PACKED_PIXELS,
+	.visual =       FB_VISUAL_TRUECOLOR,
+	.xpanstep =     0,
+	.ypanstep =     0,
+	.ywrapstep =    0,
+	.accel =        FB_ACCEL_NONE,
+};
+
+static const u32 smscufx_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
+	FBINFO_VIRTFB |	FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
+	FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
+
+static struct usb_device_id id_table[] = {
+	{USB_DEVICE(0x0424, 0x9d00),},
+	{USB_DEVICE(0x0424, 0x9d01),},
+	{},
+};
+MODULE_DEVICE_TABLE(usb, id_table);
+
+/* module options */
+static int console;   /* Optionally allow fbcon to consume first framebuffer */
+static int fb_defio = true;  /* Optionally enable fb_defio mmap support */
+
+/* ufx keeps a list of urbs for efficient bulk transfers */
+static void ufx_urb_completion(struct urb *urb);
+static struct urb *ufx_get_urb(struct ufx_data *dev);
+static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
+static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
+static void ufx_free_urb_list(struct ufx_data *dev);
+
+/* reads a control register */
+static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
+{
+	u32 *buf = kmalloc(4, GFP_KERNEL);
+	int ret;
+
+	BUG_ON(!dev);
+
+	if (!buf)
+		return -ENOMEM;
+
+	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
+		USB_VENDOR_REQUEST_READ_REGISTER,
+		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+		00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
+
+	le32_to_cpus(buf);
+	*data = *buf;
+	kfree(buf);
+
+	if (unlikely(ret < 0))
+		pr_warn("Failed to read register index 0x%08x\n", index);
+
+	return ret;
+}
+
+/* writes a control register */
+static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
+{
+	u32 *buf = kmalloc(4, GFP_KERNEL);
+	int ret;
+
+	BUG_ON(!dev);
+
+	if (!buf)
+		return -ENOMEM;
+
+	*buf = data;
+	cpu_to_le32s(buf);
+
+	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
+		USB_VENDOR_REQUEST_WRITE_REGISTER,
+		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+		00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
+
+	kfree(buf);
+
+	if (unlikely(ret < 0))
+		pr_warn("Failed to write register index 0x%08x with value "
+			"0x%08x\n", index, data);
+
+	return ret;
+}
+
+static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
+	u32 bits_to_clear, u32 bits_to_set)
+{
+	u32 data;
+	int status = ufx_reg_read(dev, index, &data);
+	check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
+		"0x%x", index);
+
+	data &= (~bits_to_clear);
+	data |= bits_to_set;
+
+	status = ufx_reg_write(dev, index, data);
+	check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
+		"0x%x", index);
+
+	return 0;
+}
+
+static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
+{
+	return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
+}
+
+static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
+{
+	return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
+}
+
+static int ufx_lite_reset(struct ufx_data *dev)
+{
+	int status;
+	u32 value;
+
+	status = ufx_reg_write(dev, 0x3008, 0x00000001);
+	check_warn_return(status, "ufx_lite_reset error writing 0x3008");
+
+	status = ufx_reg_read(dev, 0x3008, &value);
+	check_warn_return(status, "ufx_lite_reset error reading 0x3008");
+
+	return (value = 0) ? 0 : -EIO;
+}
+
+/* If display is unblanked, then blank it */
+static int ufx_blank(struct ufx_data *dev, bool wait)
+{
+	u32 dc_ctrl, dc_sts;
+	int i;
+
+	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
+	check_warn_return(status, "ufx_blank error reading 0x2004");
+
+	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
+	check_warn_return(status, "ufx_blank error reading 0x2000");
+
+	/* return success if display is already blanked */
+	if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
+		return 0;
+
+	/* request the DC to blank the display */
+	dc_ctrl |= 0x00000100;
+	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
+	check_warn_return(status, "ufx_blank error writing 0x2000");
+
+	/* return success immediately if we don't have to wait */
+	if (!wait)
+		return 0;
+
+	for (i = 0; i < 250; i++) {
+		status = ufx_reg_read(dev, 0x2004, &dc_sts);
+		check_warn_return(status, "ufx_blank error reading 0x2004");
+
+		if (dc_sts & 0x00000100)
+			return 0;
+	}
+
+	/* timed out waiting for display to blank */
+	return -EIO;
+}
+
+/* If display is blanked, then unblank it */
+static int ufx_unblank(struct ufx_data *dev, bool wait)
+{
+	u32 dc_ctrl, dc_sts;
+	int i;
+
+	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
+	check_warn_return(status, "ufx_unblank error reading 0x2004");
+
+	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
+	check_warn_return(status, "ufx_unblank error reading 0x2000");
+
+	/* return success if display is already unblanked */
+	if (((dc_sts & 0x00000100) = 0) || ((dc_ctrl & 0x00000100) = 0))
+		return 0;
+
+	/* request the DC to unblank the display */
+	dc_ctrl &= ~0x00000100;
+	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
+	check_warn_return(status, "ufx_unblank error writing 0x2000");
+
+	/* return success immediately if we don't have to wait */
+	if (!wait)
+		return 0;
+
+	for (i = 0; i < 250; i++) {
+		status = ufx_reg_read(dev, 0x2004, &dc_sts);
+		check_warn_return(status, "ufx_unblank error reading 0x2004");
+
+		if ((dc_sts & 0x00000100) = 0)
+			return 0;
+	}
+
+	/* timed out waiting for display to unblank */
+	return -EIO;
+}
+
+/* If display is enabled, then disable it */
+static int ufx_disable(struct ufx_data *dev, bool wait)
+{
+	u32 dc_ctrl, dc_sts;
+	int i;
+
+	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
+	check_warn_return(status, "ufx_disable error reading 0x2004");
+
+	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
+	check_warn_return(status, "ufx_disable error reading 0x2000");
+
+	/* return success if display is already disabled */
+	if (((dc_sts & 0x00000001) = 0) || ((dc_ctrl & 0x00000001) = 0))
+		return 0;
+
+	/* request the DC to disable the display */
+	dc_ctrl &= ~(0x00000001);
+	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
+	check_warn_return(status, "ufx_disable error writing 0x2000");
+
+	/* return success immediately if we don't have to wait */
+	if (!wait)
+		return 0;
+
+	for (i = 0; i < 250; i++) {
+		status = ufx_reg_read(dev, 0x2004, &dc_sts);
+		check_warn_return(status, "ufx_disable error reading 0x2004");
+
+		if ((dc_sts & 0x00000001) = 0)
+			return 0;
+	}
+
+	/* timed out waiting for display to disable */
+	return -EIO;
+}
+
+/* If display is disabled, then enable it */
+static int ufx_enable(struct ufx_data *dev, bool wait)
+{
+	u32 dc_ctrl, dc_sts;
+	int i;
+
+	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
+	check_warn_return(status, "ufx_enable error reading 0x2004");
+
+	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
+	check_warn_return(status, "ufx_enable error reading 0x2000");
+
+	/* return success if display is already enabled */
+	if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
+		return 0;
+
+	/* request the DC to enable the display */
+	dc_ctrl |= 0x00000001;
+	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
+	check_warn_return(status, "ufx_enable error writing 0x2000");
+
+	/* return success immediately if we don't have to wait */
+	if (!wait)
+		return 0;
+
+	for (i = 0; i < 250; i++) {
+		status = ufx_reg_read(dev, 0x2004, &dc_sts);
+		check_warn_return(status, "ufx_enable error reading 0x2004");
+
+		if (dc_sts & 0x00000001)
+			return 0;
+	}
+
+	/* timed out waiting for display to enable */
+	return -EIO;
+}
+
+static int ufx_config_sys_clk(struct ufx_data *dev)
+{
+	int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
+	check_warn_return(status, "error writing 0x700C");
+
+	status = ufx_reg_write(dev, 0x7014, 0x0010024F);
+	check_warn_return(status, "error writing 0x7014");
+
+	status = ufx_reg_write(dev, 0x7010, 0x00000000);
+	check_warn_return(status, "error writing 0x7010");
+
+	status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
+	check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
+	msleep(1);
+
+	status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
+	check_warn_return(status, "error clearing output gate in 0x700C");
+
+	return 0;
+}
+
+static int ufx_config_ddr2(struct ufx_data *dev)
+{
+	int status, i = 0;
+	u32 tmp;
+
+	status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
+	check_warn_return(status, "error writing 0x0004");
+
+	status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
+	check_warn_return(status, "error writing 0x0008");
+
+	status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
+	check_warn_return(status, "error writing 0x000C");
+
+	status = ufx_reg_write(dev, 0x0010, 0x00030814);
+	check_warn_return(status, "error writing 0x0010");
+
+	status = ufx_reg_write(dev, 0x0014, 0x00500019);
+	check_warn_return(status, "error writing 0x0014");
+
+	status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
+	check_warn_return(status, "error writing 0x0018");
+
+	status = ufx_reg_write(dev, 0x001C, 0x02532305);
+	check_warn_return(status, "error writing 0x001C");
+
+	status = ufx_reg_write(dev, 0x0020, 0x0B030905);
+	check_warn_return(status, "error writing 0x0020");
+
+	status = ufx_reg_write(dev, 0x0024, 0x00000827);
+	check_warn_return(status, "error writing 0x0024");
+
+	status = ufx_reg_write(dev, 0x0028, 0x00000000);
+	check_warn_return(status, "error writing 0x0028");
+
+	status = ufx_reg_write(dev, 0x002C, 0x00000042);
+	check_warn_return(status, "error writing 0x002C");
+
+	status = ufx_reg_write(dev, 0x0030, 0x09520000);
+	check_warn_return(status, "error writing 0x0030");
+
+	status = ufx_reg_write(dev, 0x0034, 0x02223314);
+	check_warn_return(status, "error writing 0x0034");
+
+	status = ufx_reg_write(dev, 0x0038, 0x00430043);
+	check_warn_return(status, "error writing 0x0038");
+
+	status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
+	check_warn_return(status, "error writing 0x003C");
+
+	status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
+	check_warn_return(status, "error writing 0x0040");
+
+	status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
+	check_warn_return(status, "error writing 0x0044");
+
+	status = ufx_reg_write(dev, 0x0048, 0x03080406);
+	check_warn_return(status, "error writing 0x0048");
+
+	status = ufx_reg_write(dev, 0x004C, 0x00001000);
+	check_warn_return(status, "error writing 0x004C");
+
+	status = ufx_reg_write(dev, 0x005C, 0x00000007);
+	check_warn_return(status, "error writing 0x005C");
+
+	status = ufx_reg_write(dev, 0x0100, 0x54F00012);
+	check_warn_return(status, "error writing 0x0100");
+
+	status = ufx_reg_write(dev, 0x0104, 0x00004012);
+	check_warn_return(status, "error writing 0x0104");
+
+	status = ufx_reg_write(dev, 0x0118, 0x40404040);
+	check_warn_return(status, "error writing 0x0118");
+
+	status = ufx_reg_write(dev, 0x0000, 0x00000001);
+	check_warn_return(status, "error writing 0x0000");
+
+	while (i++ < 500) {
+		status = ufx_reg_read(dev, 0x0000, &tmp);
+		check_warn_return(status, "error reading 0x0000");
+
+		if (all_bits_set(tmp, 0xC0000000))
+			return 0;
+	}
+
+	pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
+	return -ETIMEDOUT;
+}
+
+struct pll_values {
+	u32 div_r0;
+	u32 div_f0;
+	u32 div_q0;
+	u32 range0;
+	u32 div_r1;
+	u32 div_f1;
+	u32 div_q1;
+	u32 range1;
+};
+
+static u32 ufx_calc_range(u32 ref_freq)
+{
+	if (ref_freq >= 88000000)
+		return 7;
+
+	if (ref_freq >= 54000000)
+		return 6;
+
+	if (ref_freq >= 34000000)
+		return 5;
+
+	if (ref_freq >= 21000000)
+		return 4;
+
+	if (ref_freq >= 13000000)
+		return 3;
+
+	if (ref_freq >= 8000000)
+		return 2;
+
+	return 1;
+}
+
+/* calculates PLL divider settings for a desired target frequency */
+static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
+{
+	const u32 ref_clk = 25000000;
+	u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
+	u32 min_error = clk_pixel_pll;
+
+	for (div_r0 = 1; div_r0 <= 32; div_r0++) {
+		u32 ref_freq0 = ref_clk / div_r0;
+		if (ref_freq0 < 5000000)
+			break;
+
+		if (ref_freq0 > 200000000)
+			continue;
+
+		for (div_f0 = 1; div_f0 <= 256; div_f0++) {
+			u32 vco_freq0 = ref_freq0 * div_f0;
+
+			if (vco_freq0 < 350000000)
+				continue;
+
+			if (vco_freq0 > 700000000)
+				break;
+
+			for (div_q0 = 0; div_q0 < 7; div_q0++) {
+				u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
+
+				if (pllout_freq0 < 5000000)
+					break;
+
+				if (pllout_freq0 > 200000000)
+					continue;
+
+				for (div_r1 = 1; div_r1 <= 32; div_r1++) {
+					u32 ref_freq1 = pllout_freq0 / div_r1;
+
+					if (ref_freq1 < 5000000)
+						break;
+
+					for (div_f1 = 1; div_f1 <= 256; div_f1++) {
+						u32 vco_freq1 = ref_freq1 * div_f1;
+
+						if (vco_freq1 < 350000000)
+							continue;
+
+						if (vco_freq1 > 700000000)
+							break;
+
+						for (div_q1 = 0; div_q1 < 7; div_q1++) {
+							u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
+							int error = abs(pllout_freq1 - clk_pixel_pll);
+
+							if (pllout_freq1 < 5000000)
+								break;
+
+							if (pllout_freq1 > 700000000)
+								continue;
+
+							if (error < min_error) {
+								min_error = error;
+
+								/* final returned value is equal to calculated value - 1
+								 * because a value of 0 = divide by 1 */
+								asic_pll->div_r0 = div_r0 - 1;
+								asic_pll->div_f0 = div_f0 - 1;
+								asic_pll->div_q0 = div_q0;
+								asic_pll->div_r1 = div_r1 - 1;
+								asic_pll->div_f1 = div_f1 - 1;
+								asic_pll->div_q1 = div_q1;
+
+								asic_pll->range0 = ufx_calc_range(ref_freq0);
+								asic_pll->range1 = ufx_calc_range(ref_freq1);
+
+								if (min_error = 0)
+									return;
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+}
+
+/* sets analog bit PLL configuration values */
+static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
+{
+	struct pll_values asic_pll = {0};
+	u32 value, clk_pixel, clk_pixel_pll;
+	int status;
+
+	/* convert pixclock (in ps) to frequency (in Hz) */
+	clk_pixel = PICOS2KHZ(pixclock) * 1000;
+	pr_debug("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
+
+	/* clk_pixel = 1/2 clk_pixel_pll */
+	clk_pixel_pll = clk_pixel * 2;
+
+	ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
+
+	/* Keep BYPASS and RESET signals asserted until configured */
+	status = ufx_reg_write(dev, 0x7000, 0x8000000F);
+	check_warn_return(status, "error writing 0x7000");
+
+	value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
+		(asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
+	status = ufx_reg_write(dev, 0x7008, value);
+	check_warn_return(status, "error writing 0x7008");
+
+	value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
+		(asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
+	status = ufx_reg_write(dev, 0x7004, value);
+	check_warn_return(status, "error writing 0x7004");
+
+	status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
+	check_warn_return(status,
+		"error clearing PLL0 bypass bits in 0x7000");
+	msleep(1);
+
+	status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
+	check_warn_return(status,
+		"error clearing PLL1 bypass bits in 0x7000");
+	msleep(1);
+
+	status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
+	check_warn_return(status, "error clearing gate bits in 0x7000");
+
+	return 0;
+}
+
+static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
+{
+	u32 temp;
+	u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
+	u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
+
+	int status = ufx_reg_write(dev, 0x8028, 0);
+	check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
+
+	status = ufx_reg_write(dev, 0x8024, 0);
+	check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
+
+	/* shut everything down before changing timing */
+	status = ufx_blank(dev, true);
+	check_warn_return(status, "ufx_set_vid_mode error blanking display");
+
+	status = ufx_disable(dev, true);
+	check_warn_return(status, "ufx_set_vid_mode error disabling display");
+
+	status = ufx_config_pix_clk(dev, var->pixclock);
+	check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
+
+	status = ufx_reg_write(dev, 0x2000, 0x00000104);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
+
+	/* set horizontal timings */
+	h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
+	h_active = var->xres;
+	h_blank_start = var->xres + var->right_margin;
+	h_blank_end = var->xres + var->right_margin + var->hsync_len;
+	h_sync_start = var->xres + var->right_margin;
+	h_sync_end = var->xres + var->right_margin + var->hsync_len;
+
+	temp = ((h_total - 1) << 16) | (h_active - 1);
+	status = ufx_reg_write(dev, 0x2008, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
+
+	temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
+	status = ufx_reg_write(dev, 0x200C, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
+
+	temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
+	status = ufx_reg_write(dev, 0x2010, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
+
+	/* set vertical timings */
+	v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
+	v_active = var->yres;
+	v_blank_start = var->yres + var->lower_margin;
+	v_blank_end = var->yres + var->lower_margin + var->vsync_len;
+	v_sync_start = var->yres + var->lower_margin;
+	v_sync_end = var->yres + var->lower_margin + var->vsync_len;
+
+	temp = ((v_total - 1) << 16) | (v_active - 1);
+	status = ufx_reg_write(dev, 0x2014, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
+
+	temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
+	status = ufx_reg_write(dev, 0x2018, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
+
+	temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
+	status = ufx_reg_write(dev, 0x201C, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
+
+	status = ufx_reg_write(dev, 0x2020, 0x00000000);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
+
+	status = ufx_reg_write(dev, 0x2024, 0x00000000);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
+
+	/* Set the frame length register (#pix * 2 bytes/pixel) */
+	temp = var->xres * var->yres * 2;
+	temp = (temp + 7) & (~0x7);
+	status = ufx_reg_write(dev, 0x2028, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
+
+	/* enable desired output interface & disable others */
+	status = ufx_reg_write(dev, 0x2040, 0);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
+
+	status = ufx_reg_write(dev, 0x2044, 0);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
+
+	status = ufx_reg_write(dev, 0x2048, 0);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
+
+	/* set the sync polarities & enable bit */
+	temp = 0x00000001;
+	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
+		temp |= 0x00000010;
+
+	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
+		temp |= 0x00000008;
+
+	status = ufx_reg_write(dev, 0x2040, temp);
+	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
+
+	/* start everything back up */
+	status = ufx_enable(dev, true);
+	check_warn_return(status, "ufx_set_vid_mode error enabling display");
+
+	/* Unblank the display */
+	status = ufx_unblank(dev, true);
+	check_warn_return(status, "ufx_set_vid_mode error unblanking display");
+
+	/* enable RGB pad */
+	status = ufx_reg_write(dev, 0x8028, 0x00000003);
+	check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
+
+	/* enable VDAC */
+	status = ufx_reg_write(dev, 0x8024, 0x00000007);
+	check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
+
+	return 0;
+}
+
+static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
+{
+	unsigned long start = vma->vm_start;
+	unsigned long size = vma->vm_end - vma->vm_start;
+	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
+	unsigned long page, pos;
+
+	if (offset + size > info->fix.smem_len)
+		return -EINVAL;
+
+	pos = (unsigned long)info->fix.smem_start + offset;
+
+	pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
+		  pos, size);
+
+	while (size > 0) {
+		page = vmalloc_to_pfn((void *)pos);
+		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
+			return -EAGAIN;
+
+		start += PAGE_SIZE;
+		pos += PAGE_SIZE;
+		if (size > PAGE_SIZE)
+			size -= PAGE_SIZE;
+		else
+			size = 0;
+	}
+
+	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
+	return 0;
+}
+
+static void ufx_raw_rect(struct ufx_data *dev, char *cmd, int x, int y,
+	int width, int height)
+{
+	size_t packed_line_len = ALIGN((width * 2), 4);
+	size_t packed_rect_len = packed_line_len * height;
+	int line;
+
+	BUG_ON(!dev);
+	BUG_ON(!dev->info);
+
+	/* command word */
+	*((u32 *)&cmd[0]) = cpu_to_le32(0x01);
+
+	/* length word */
+	*((u32 *)&cmd[4]) = cpu_to_le32(packed_rect_len + 16);
+
+	*((u16 *)&cmd[8]) = cpu_to_le16(x);
+	*((u16 *)&cmd[10]) = cpu_to_le16(y);
+	*((u16 *)&cmd[12]) = cpu_to_le16(width);
+	*((u16 *)&cmd[14]) = cpu_to_le16(height);
+
+	/* frame base address */
+	*((u32 *)&cmd[16]) = cpu_to_le32(0 & 0xffffff80);
+
+	/* color mode and horizontal resolution */
+	*((u16 *)&cmd[20]) = cpu_to_le16(0x4000 | dev->info->var.xres);
+
+	/* vertical resolution */
+	*((u16 *)&cmd[22]) = cpu_to_le16(dev->info->var.yres);
+
+	/* packed data */
+	for (line = 0; line < height; line++) {
+		const int line_offset = dev->info->fix.line_length * (y + line);
+		const int byte_offset = line_offset + (x * BPP);
+		memcpy(&cmd[24 + (packed_line_len * line)],
+			(char *)dev->info->fix.smem_start + byte_offset, width * BPP);
+	}
+}
+
+int ufx_handle_damage(struct ufx_data *dev, int x, int y,
+	int width, int height)
+{
+	size_t packed_line_len = ALIGN((width * 2), 4);
+	int len, status, urb_lines, start_line = 0;
+
+	if ((width <= 0) || (height <= 0) ||
+	    (x + width > dev->info->var.xres) ||
+	    (y + height > dev->info->var.yres))
+		return -EINVAL;
+
+	if (!atomic_read(&dev->usb_active))
+		return 0;
+
+	while (start_line < height) {
+		struct urb *urb = ufx_get_urb(dev);
+		if (!urb) {
+			pr_warn("ufx_handle_damage unable to get urb");
+			return 0;
+		}
+
+		/* assume we have enough space to transfer at least one line */
+		BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
+
+		/* calculate the maximum number of lines we could fit in */
+		urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
+
+		/* but we might not need this many */
+		urb_lines = min(urb_lines, (height - start_line));
+
+		memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
+
+		ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
+		len = 24 + (packed_line_len * urb_lines);
+
+		status = ufx_submit_urb(dev, urb, len);
+		check_warn_return(status, "Error submitting URB");
+
+		start_line += urb_lines;
+	}
+
+	return 0;
+}
+
+/* Path triggered by usermode clients who write to filesystem
+ * e.g. cat filename > /dev/fb1
+ * Not used by X Windows or text-mode console. But useful for testing.
+ * Slow because of extra copy and we must assume all pixels dirty. */
+static ssize_t ufx_ops_write(struct fb_info *info, const char __user *buf,
+			  size_t count, loff_t *ppos)
+{
+	ssize_t result;
+	struct ufx_data *dev = info->par;
+	u32 offset = (u32) *ppos;
+
+	result = fb_sys_write(info, buf, count, ppos);
+
+	if (result > 0) {
+		int start = max((int)(offset / info->fix.line_length) - 1, 0);
+		int lines = min((u32)((result / info->fix.line_length) + 1),
+				(u32)info->var.yres);
+
+		ufx_handle_damage(dev, 0, start, info->var.xres, lines);
+	}
+
+	return result;
+}
+
+static void ufx_ops_copyarea(struct fb_info *info,
+				const struct fb_copyarea *area)
+{
+
+	struct ufx_data *dev = info->par;
+
+	sys_copyarea(info, area);
+
+	ufx_handle_damage(dev, area->dx, area->dy,
+			area->width, area->height);
+}
+
+static void ufx_ops_imageblit(struct fb_info *info,
+				const struct fb_image *image)
+{
+	struct ufx_data *dev = info->par;
+
+	sys_imageblit(info, image);
+
+	ufx_handle_damage(dev, image->dx, image->dy,
+			image->width, image->height);
+}
+
+static void ufx_ops_fillrect(struct fb_info *info,
+			  const struct fb_fillrect *rect)
+{
+	struct ufx_data *dev = info->par;
+
+	sys_fillrect(info, rect);
+
+	ufx_handle_damage(dev, rect->dx, rect->dy, rect->width,
+			      rect->height);
+}
+
+/* NOTE: fb_defio.c is holding info->fbdefio.mutex
+ *   Touching ANY framebuffer memory that triggers a page fault
+ *   in fb_defio will cause a deadlock, when it also tries to
+ *   grab the same mutex. */
+static void ufx_dpy_deferred_io(struct fb_info *info,
+				struct list_head *pagelist)
+{
+	struct page *cur;
+	struct fb_deferred_io *fbdefio = info->fbdefio;
+	struct ufx_data *dev = info->par;
+
+	if (!fb_defio)
+		return;
+
+	if (!atomic_read(&dev->usb_active))
+		return;
+
+	/* walk the written page list and render each to device */
+	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
+		/* create a rectangle of full screen width that encloses the
+		 * entire dirty framebuffer page */
+		const int x = 0;
+		const int width = dev->info->var.xres;
+		const int y = (cur->index << PAGE_SHIFT) / (width * 2);
+		int height = (PAGE_SIZE / (width * 2)) + 1;
+		height = min(height, (int)(dev->info->var.yres - y));
+
+		BUG_ON(y >= dev->info->var.yres);
+		BUG_ON((y + height) > dev->info->var.yres);
+
+		ufx_handle_damage(dev, x, y, width, height);
+	}
+}
+
+static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
+			 unsigned long arg)
+{
+	struct ufx_data *dev = info->par;
+	struct dloarea *area = NULL;
+
+	if (!atomic_read(&dev->usb_active))
+		return 0;
+
+	/* TODO: Update X server to get this from sysfs instead */
+	if (cmd = UFX_IOCTL_RETURN_EDID) {
+		char *edid = (char *)arg;
+		if (copy_to_user(edid, dev->edid, dev->edid_size))
+			return -EFAULT;
+		return 0;
+	}
+
+	/* TODO: Help propose a standard fb.h ioctl to report mmap damage */
+	if (cmd = UFX_IOCTL_REPORT_DAMAGE) {
+		/* If we have a damage-aware client, turn fb_defio "off"
+		 * To avoid perf imact of unecessary page fault handling.
+		 * Done by resetting the delay for this fb_info to a very
+		 * long period. Pages will become writable and stay that way.
+		 * Reset to normal value when all clients have closed this fb.
+		 */
+		if (info->fbdefio)
+			info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
+
+		area = (struct dloarea *)arg;
+
+		if (area->x < 0)
+			area->x = 0;
+
+		if (area->x > info->var.xres)
+			area->x = info->var.xres;
+
+		if (area->y < 0)
+			area->y = 0;
+
+		if (area->y > info->var.yres)
+			area->y = info->var.yres;
+
+		ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
+	}
+
+	return 0;
+}
+
+/* taken from vesafb */
+static int
+ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
+	       unsigned blue, unsigned transp, struct fb_info *info)
+{
+	int err = 0;
+
+	if (regno >= info->cmap.len)
+		return 1;
+
+	if (regno < 16) {
+		if (info->var.red.offset = 10) {
+			/* 1:5:5:5 */
+			((u32 *) (info->pseudo_palette))[regno] +			    ((red & 0xf800) >> 1) |
+			    ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
+		} else {
+			/* 0:5:6:5 */
+			((u32 *) (info->pseudo_palette))[regno] +			    ((red & 0xf800)) |
+			    ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
+		}
+	}
+
+	return err;
+}
+
+/* It's common for several clients to have framebuffer open simultaneously.
+ * e.g. both fbcon and X. Makes things interesting.
+ * Assumes caller is holding info->lock (for open and release at least) */
+static int ufx_ops_open(struct fb_info *info, int user)
+{
+	struct ufx_data *dev = info->par;
+
+	/* fbcon aggressively connects to first framebuffer it finds,
+	 * preventing other clients (X) from working properly. Usually
+	 * not what the user wants. Fail by default with option to enable. */
+	if (user = 0 && !console)
+		return -EBUSY;
+
+	/* If the USB device is gone, we don't accept new opens */
+	if (dev->virtualized)
+		return -ENODEV;
+
+	dev->fb_count++;
+
+	kref_get(&dev->kref);
+
+	if (fb_defio && (info->fbdefio = NULL)) {
+		/* enable defio at last moment if not disabled by client */
+
+		struct fb_deferred_io *fbdefio;
+
+		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
+
+		if (fbdefio) {
+			fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
+			fbdefio->deferred_io = ufx_dpy_deferred_io;
+		}
+
+		info->fbdefio = fbdefio;
+		fb_deferred_io_init(info);
+	}
+
+	pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d",
+		info->node, user, info, dev->fb_count);
+
+	return 0;
+}
+
+/*
+ * Called when all client interfaces to start transactions have been disabled,
+ * and all references to our device instance (ufx_data) are released.
+ * Every transaction must have a reference, so we know are fully spun down
+ */
+static void ufx_free(struct kref *kref)
+{
+	struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
+
+	/* this function will wait for all in-flight urbs to complete */
+	if (dev->urbs.count > 0)
+		ufx_free_urb_list(dev);
+
+	pr_debug("freeing ufx_data %p", dev);
+
+	kfree(dev);
+}
+
+static void ufx_release_urb_work(struct work_struct *work)
+{
+	struct urb_node *unode = container_of(work, struct urb_node,
+					      release_urb_work.work);
+
+	up(&unode->dev->urbs.limit_sem);
+}
+
+static void ufx_free_framebuffer_work(struct work_struct *work)
+{
+	struct ufx_data *dev = container_of(work, struct ufx_data,
+					    free_framebuffer_work.work);
+	struct fb_info *info = dev->info;
+	int node = info->node;
+
+	unregister_framebuffer(info);
+
+	if (info->cmap.len != 0)
+		fb_dealloc_cmap(&info->cmap);
+	if (info->monspecs.modedb)
+		fb_destroy_modedb(info->monspecs.modedb);
+	if (info->screen_base)
+		vfree(info->screen_base);
+
+	fb_destroy_modelist(&info->modelist);
+
+	dev->info = 0;
+
+	/* Assume info structure is freed after this point */
+	framebuffer_release(info);
+
+	pr_debug("fb_info for /dev/fb%d has been freed", node);
+
+	/* ref taken in probe() as part of registering framebfufer */
+	kref_put(&dev->kref, ufx_free);
+}
+
+/*
+ * Assumes caller is holding info->lock mutex (for open and release at least)
+ */
+static int ufx_ops_release(struct fb_info *info, int user)
+{
+	struct ufx_data *dev = info->par;
+
+	dev->fb_count--;
+
+	/* We can't free fb_info here - fbmem will touch it when we return */
+	if (dev->virtualized && (dev->fb_count = 0))
+		schedule_delayed_work(&dev->free_framebuffer_work, HZ);
+
+	if ((dev->fb_count = 0) && (info->fbdefio)) {
+		fb_deferred_io_cleanup(info);
+		kfree(info->fbdefio);
+		info->fbdefio = NULL;
+		info->fbops->fb_mmap = ufx_ops_mmap;
+	}
+
+	pr_debug("released /dev/fb%d user=%d count=%d",
+		  info->node, user, dev->fb_count);
+
+	kref_put(&dev->kref, ufx_free);
+
+	return 0;
+}
+
+/* Check whether a video mode is supported by the chip
+ * We start from monitor's modes, so don't need to filter that here */
+static int ufx_is_valid_mode(struct fb_videomode *mode,
+		struct fb_info *info)
+{
+	if ((mode->xres * mode->yres) > (2048 * 1152)) {
+		pr_debug("%dx%d too many pixels",
+		       mode->xres, mode->yres);
+		return 0;
+	}
+
+	if (mode->pixclock < 5000) {
+		pr_debug("%dx%d %dps pixel clock too fast",
+		       mode->xres, mode->yres, mode->pixclock);
+		return 0;
+	}
+
+	pr_debug("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
+		mode->pixclock, (1000000 / mode->pixclock));
+	return 1;
+}
+
+static void ufx_var_color_format(struct fb_var_screeninfo *var)
+{
+	const struct fb_bitfield red = { 11, 5, 0 };
+	const struct fb_bitfield green = { 5, 6, 0 };
+	const struct fb_bitfield blue = { 0, 5, 0 };
+
+	var->bits_per_pixel = 16;
+	var->red = red;
+	var->green = green;
+	var->blue = blue;
+}
+
+static int ufx_ops_check_var(struct fb_var_screeninfo *var,
+				struct fb_info *info)
+{
+	struct fb_videomode mode;
+
+	/* TODO: support dynamically changing framebuffer size */
+	if ((var->xres * var->yres * 2) > info->fix.smem_len)
+		return -EINVAL;
+
+	/* set device-specific elements of var unrelated to mode */
+	ufx_var_color_format(var);
+
+	fb_var_to_videomode(&mode, var);
+
+	if (!ufx_is_valid_mode(&mode, info))
+		return -EINVAL;
+
+	return 0;
+}
+
+static int ufx_ops_set_par(struct fb_info *info)
+{
+	struct ufx_data *dev = info->par;
+	int result;
+	u16 *pix_framebuffer;
+	int i;
+
+	pr_debug("set_par mode %dx%d", info->var.xres, info->var.yres);
+	result = ufx_set_vid_mode(dev, &info->var);
+
+	if ((result = 0) && (dev->fb_count = 0)) {
+		/* paint greenscreen */
+		pix_framebuffer = (u16 *) info->screen_base;
+		for (i = 0; i < info->fix.smem_len / 2; i++)
+			pix_framebuffer[i] = 0x37e6;
+
+		ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
+	}
+
+	/* re-enable defio if previously disabled by damage tracking */
+	if (info->fbdefio)
+		info->fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
+
+	return result;
+}
+
+/* In order to come back from full DPMS off, we need to set the mode again */
+static int ufx_ops_blank(int blank_mode, struct fb_info *info)
+{
+	struct ufx_data *dev = info->par;
+	ufx_set_vid_mode(dev, &info->var);
+	return 0;
+}
+
+static struct fb_ops ufx_ops = {
+	.owner = THIS_MODULE,
+	.fb_read = fb_sys_read,
+	.fb_write = ufx_ops_write,
+	.fb_setcolreg = ufx_ops_setcolreg,
+	.fb_fillrect = ufx_ops_fillrect,
+	.fb_copyarea = ufx_ops_copyarea,
+	.fb_imageblit = ufx_ops_imageblit,
+	.fb_mmap = ufx_ops_mmap,
+	.fb_ioctl = ufx_ops_ioctl,
+	.fb_open = ufx_ops_open,
+	.fb_release = ufx_ops_release,
+	.fb_blank = ufx_ops_blank,
+	.fb_check_var = ufx_ops_check_var,
+	.fb_set_par = ufx_ops_set_par,
+};
+
+/* Assumes &info->lock held by caller
+ * Assumes no active clients have framebuffer open */
+static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
+{
+	int retval = -ENOMEM;
+	int old_len = info->fix.smem_len;
+	int new_len;
+	unsigned char *old_fb = info->screen_base;
+	unsigned char *new_fb;
+
+	pr_debug("Reallocating framebuffer. Addresses will change!");
+
+	new_len = info->fix.line_length * info->var.yres;
+
+	if (PAGE_ALIGN(new_len) > old_len) {
+		/*
+		 * Alloc system memory for virtual framebuffer
+		 */
+		new_fb = vmalloc(new_len);
+		if (!new_fb) {
+			pr_err("Virtual framebuffer alloc failed");
+			goto error;
+		}
+
+		if (info->screen_base) {
+			memcpy(new_fb, old_fb, old_len);
+			vfree(info->screen_base);
+		}
+
+		info->screen_base = new_fb;
+		info->fix.smem_len = PAGE_ALIGN(new_len);
+		info->fix.smem_start = (unsigned long) new_fb;
+		info->flags = smscufx_info_flags;
+	}
+
+	retval = 0;
+
+error:
+	return retval;
+}
+
+/* sets up I2C Controller for 100 Kbps, std. speed, 7-bit addr, master,
+ * restart enabled, but no start byte, enable controller */
+static int ufx_i2c_init(struct ufx_data *dev)
+{
+	u32 tmp;
+
+	/* disable the controller before it can be reprogrammed */
+	int status = ufx_reg_write(dev, 0x106C, 0x00);
+	check_warn_return(status, "failed to disable I2C");
+
+	/* Setup the clock count registers
+	 * (12+1) = 13 clks @ 2.5 MHz = 5.2 uS */
+	status = ufx_reg_write(dev, 0x1018, 12);
+	check_warn_return(status, "error writing 0x1018");
+
+	/* (6+8) = 14 clks @ 2.5 MHz = 5.6 uS */
+	status = ufx_reg_write(dev, 0x1014, 6);
+	check_warn_return(status, "error writing 0x1014");
+
+	status = ufx_reg_read(dev, 0x1000, &tmp);
+	check_warn_return(status, "error reading 0x1000");
+
+	/* set speed to std mode */
+	tmp &= ~(0x06);
+	tmp |= 0x02;
+
+	/* 7-bit (not 10-bit) addressing */
+	tmp &= ~(0x10);
+
+	/* enable restart conditions and master mode */
+	tmp |= 0x21;
+
+	status = ufx_reg_write(dev, 0x1000, tmp);
+	check_warn_return(status, "error writing 0x1000");
+
+	/* Set normal tx using target address 0 */
+	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
+	check_warn_return(status, "error setting TX mode bits in 0x1004");
+
+	/* Enable the controller */
+	status = ufx_reg_write(dev, 0x106C, 0x01);
+	check_warn_return(status, "failed to enable I2C");
+
+	return 0;
+}
+
+/* sets the I2C port mux and target address */
+static int ufx_i2c_configure(struct ufx_data *dev)
+{
+	int status = ufx_reg_write(dev, 0x106C, 0x00);
+	check_warn_return(status, "failed to disable I2C");
+
+	status = ufx_reg_write(dev, 0x3010, 0x00000000);
+	check_warn_return(status, "failed to write 0x3010");
+
+	/* A0h is std for any EDID, right shifted by one */
+	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF,	(0xA0 >> 1));
+	check_warn_return(status, "failed to set TAR bits in 0x1004");
+
+	status = ufx_reg_write(dev, 0x106C, 0x01);
+	check_warn_return(status, "failed to enable I2C");
+
+	return 0;
+}
+
+/* wait for BUSY to clear, with a timeout of 50ms with 10ms sleeps. if no
+ * monitor is connected, there is no error except for timeout */
+static int ufx_i2c_wait_busy(struct ufx_data *dev)
+{
+	u32 tmp;
+	int i, status;
+
+	for (i = 0; i < 15; i++) {
+		status = ufx_reg_read(dev, 0x1100, &tmp);
+		check_warn_return(status, "0x1100 read failed");
+
+		/* if BUSY is clear, check for error */
+		if ((tmp & 0x80000000) = 0) {
+			if (tmp & 0x20000000) {
+				pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
+				return -EIO;
+			}
+
+			return 0;
+		}
+
+		/* perform the first 10 retries without delay */
+		if (i >= 10)
+			msleep(10);
+	}
+
+	pr_warn("I2C access timed out, resetting I2C hardware");
+	status =  ufx_reg_write(dev, 0x1100, 0x40000000);
+	check_warn_return(status, "0x1100 write failed");
+
+	return -ETIMEDOUT;
+}
+
+/* reads a 128-byte EDID block from the currently selected port and TAR */
+static int ufx_read_edid(struct ufx_data *dev, char *edid, int edid_len)
+{
+	int i, j, status;
+	u32 *edid_u32 = (u32 *)edid;
+
+	BUG_ON(edid_len != EDID_LENGTH);
+
+	status = ufx_i2c_configure(dev);
+	if (status < 0) {
+		pr_err("ufx_i2c_configure failed");
+		return status;
+	}
+
+	memset(edid, 0xff, EDID_LENGTH);
+
+	/* Read the 128-byte EDID as 2 bursts of 64 bytes */
+	for (i = 0; i < 2; i++) {
+		u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
+		status = ufx_reg_write(dev, 0x1100, temp);
+		check_warn_return(status, "Failed to write 0x1100");
+
+		temp |= 0x80000000;
+		status = ufx_reg_write(dev, 0x1100, temp);
+		check_warn_return(status, "Failed to write 0x1100");
+
+		status = ufx_i2c_wait_busy(dev);
+		check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
+
+		for (j = 0; j < 16; j++) {
+			u32 data_reg_addr = 0x1110 + (j * 4);
+			status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
+			check_warn_return(status, "Error reading i2c data");
+		}
+	}
+
+	/* all FF's in the first 16 bytes indicates nothing is connected */
+	for (i = 0; i < 16; i++) {
+		if (edid[i] != 0xFF) {
+			pr_debug("edid data read succesfully");
+			return EDID_LENGTH;
+		}
+	}
+
+	pr_warn("edid data contains all 0xff");
+	return -ETIMEDOUT;
+}
+
+/* 1) use sw default
+ * 2) Parse into various fb_info structs
+ * 3) Allocate virtual framebuffer memory to back highest res mode
+ *
+ * Parses EDID into three places used by various parts of fbdev:
+ * fb_var_screeninfo contains the timing of the monitor's preferred mode
+ * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
+ * fb_info.modelist is a linked list of all monitor & VESA modes which work
+ *
+ * If EDID is not readable/valid, then modelist is all VESA modes,
+ * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
+ * Returns 0 if successful */
+static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
+	char *default_edid, size_t default_edid_size)
+{
+	const struct fb_videomode *default_vmode = NULL;
+	char *edid;
+	int i, result = 0, tries = 3;
+
+	if (info->dev) /* only use mutex if info has been registered */
+		mutex_lock(&info->lock);
+
+	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
+	if (!edid) {
+		result = -ENOMEM;
+		goto error;
+	}
+
+	fb_destroy_modelist(&info->modelist);
+	memset(&info->monspecs, 0, sizeof(info->monspecs));
+
+	/* Try to (re)read EDID from hardware first
+	 * EDID data may return, but not parse as valid
+	 * Try again a few times, in case of e.g. analog cable noise */
+	while (tries--) {
+		i = ufx_read_edid(dev, edid, EDID_LENGTH);
+
+		if (i >= EDID_LENGTH)
+			fb_edid_to_monspecs(edid, &info->monspecs);
+
+		if (info->monspecs.modedb_len > 0) {
+			dev->edid = edid;
+			dev->edid_size = i;
+			break;
+		}
+	}
+
+	/* If that fails, use a previously returned EDID if available */
+	if (info->monspecs.modedb_len = 0) {
+		pr_err("Unable to get valid EDID from device/display\n");
+
+		if (dev->edid) {
+			fb_edid_to_monspecs(dev->edid, &info->monspecs);
+			if (info->monspecs.modedb_len > 0)
+				pr_err("Using previously queried EDID\n");
+		}
+	}
+
+	/* If that fails, use the default EDID we were handed */
+	if (info->monspecs.modedb_len = 0) {
+		if (default_edid_size >= EDID_LENGTH) {
+			fb_edid_to_monspecs(default_edid, &info->monspecs);
+			if (info->monspecs.modedb_len > 0) {
+				memcpy(edid, default_edid, default_edid_size);
+				dev->edid = edid;
+				dev->edid_size = default_edid_size;
+				pr_err("Using default/backup EDID\n");
+			}
+		}
+	}
+
+	/* If we've got modes, let's pick a best default mode */
+	if (info->monspecs.modedb_len > 0) {
+
+		for (i = 0; i < info->monspecs.modedb_len; i++) {
+			if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
+				fb_add_videomode(&info->monspecs.modedb[i],
+					&info->modelist);
+			else /* if we've removed top/best mode */
+				info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
+		}
+
+		default_vmode = fb_find_best_display(&info->monspecs,
+						     &info->modelist);
+	}
+
+	/* If everything else has failed, fall back to safe default mode */
+	if (default_vmode = NULL) {
+
+		struct fb_videomode fb_vmode = {0};
+
+		/* Add the standard VESA modes to our modelist
+		 * Since we don't have EDID, there may be modes that
+		 * overspec monitor and/or are incorrect aspect ratio, etc.
+		 * But at least the user has a chance to choose
+		 */
+		for (i = 0; i < VESA_MODEDB_SIZE; i++) {
+			if (ufx_is_valid_mode((struct fb_videomode *)
+						&vesa_modes[i], info))
+				fb_add_videomode(&vesa_modes[i],
+						 &info->modelist);
+		}
+
+		/* default to resolution safe for projectors
+		 * (since they are most common case without EDID)
+		 */
+		fb_vmode.xres = 800;
+		fb_vmode.yres = 600;
+		fb_vmode.refresh = 60;
+		default_vmode = fb_find_nearest_mode(&fb_vmode,
+						     &info->modelist);
+	}
+
+	/* If we have good mode and no active clients */
+	if ((default_vmode != NULL) && (dev->fb_count = 0)) {
+
+		fb_videomode_to_var(&info->var, default_vmode);
+		ufx_var_color_format(&info->var);
+
+		/* with mode size info, we can now alloc our framebuffer */
+		memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
+		info->fix.line_length = info->var.xres *
+			(info->var.bits_per_pixel / 8);
+
+		result = ufx_realloc_framebuffer(dev, info);
+
+	} else
+		result = -EINVAL;
+
+error:
+	if (edid && (dev->edid != edid))
+		kfree(edid);
+
+	if (info->dev)
+		mutex_unlock(&info->lock);
+
+	return result;
+}
+
+static int ufx_usb_probe(struct usb_interface *interface,
+			const struct usb_device_id *id)
+{
+	struct usb_device *usbdev;
+	struct ufx_data *dev;
+	struct fb_info *info = 0;
+	int retval = -ENOMEM;
+	u32 id_rev, fpga_rev;
+
+	/* usb initialization */
+	usbdev = interface_to_usbdev(interface);
+	BUG_ON(!usbdev);
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (dev = NULL) {
+		dev_err(&usbdev->dev, "ufx_usb_probe: failed alloc of dev struct\n");
+		goto error;
+	}
+
+	/* we need to wait for both usb and fbdev to spin down on disconnect */
+	kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
+	kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
+
+	dev->udev = usbdev;
+	dev->gdev = &usbdev->dev; /* our generic struct device * */
+	usb_set_intfdata(interface, dev);
+
+	dev_dbg(dev->gdev, "%s %s - serial #%s\n",
+		usbdev->manufacturer, usbdev->product, usbdev->serial);
+	dev_dbg(dev->gdev, "vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
+		usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
+		usbdev->descriptor.bcdDevice, dev);
+	dev_dbg(dev->gdev, "console enable=%d\n", console);
+	dev_dbg(dev->gdev, "fb_defio enable=%d\n", fb_defio);
+
+	if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
+		retval = -ENOMEM;
+		dev_err(dev->gdev, "ufx_alloc_urb_list failed\n");
+		goto error;
+	}
+
+	/* We don't register a new USB class. Our client interface is fbdev */
+
+	/* allocates framebuffer driver structure, not framebuffer memory */
+	info = framebuffer_alloc(0, &usbdev->dev);
+	if (!info) {
+		retval = -ENOMEM;
+		dev_err(dev->gdev, "framebuffer_alloc failed\n");
+		goto error;
+	}
+
+	dev->info = info;
+	info->par = dev;
+	info->pseudo_palette = dev->pseudo_palette;
+	info->fbops = &ufx_ops;
+
+	retval = fb_alloc_cmap(&info->cmap, 256, 0);
+	if (retval < 0) {
+		dev_err(dev->gdev, "fb_alloc_cmap failed %x\n", retval);
+		goto error;
+	}
+
+	INIT_DELAYED_WORK(&dev->free_framebuffer_work,
+			  ufx_free_framebuffer_work);
+
+	INIT_LIST_HEAD(&info->modelist);
+
+	retval = ufx_reg_read(dev, 0x3000, &id_rev);
+	check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
+	dev_dbg(dev->gdev, "ID_REV register value 0x%08x", id_rev);
+
+	retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
+	check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
+	dev_dbg(dev->gdev, "FPGA_REV register value 0x%08x", fpga_rev);
+
+	dev_dbg(dev->gdev, "resetting device");
+	retval = ufx_lite_reset(dev);
+	check_warn_goto_error(retval, "error %d resetting device", retval);
+
+	dev_dbg(dev->gdev, "configuring system clock");
+	retval = ufx_config_sys_clk(dev);
+	check_warn_goto_error(retval, "error %d configuring system clock", retval);
+
+	dev_dbg(dev->gdev, "configuring DDR2 controller");
+	retval = ufx_config_ddr2(dev);
+	check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
+
+	dev_dbg(dev->gdev, "configuring I2C controller");
+	retval = ufx_i2c_init(dev);
+	check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
+
+	dev_dbg(dev->gdev, "selecting display mode");
+	retval = ufx_setup_modes(dev, info, NULL, 0);
+	check_warn_goto_error(retval, "unable to find common mode for display and adapter");
+
+	retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
+	check_warn_goto_error(retval, "error %d enabling graphics engine", retval);
+
+	/* ready to begin using device */
+	atomic_set(&dev->usb_active, 1);
+
+	dev_dbg(dev->gdev, "checking var");
+	retval = ufx_ops_check_var(&info->var, info);
+	check_warn_goto_error(retval, "error %d ufx_ops_check_var", retval);
+
+	dev_dbg(dev->gdev, "setting par");
+	retval = ufx_ops_set_par(info);
+	check_warn_goto_error(retval, "error %d ufx_ops_set_par", retval);
+
+	dev_dbg(dev->gdev, "registering framebuffer");
+	retval = register_framebuffer(info);
+	check_warn_goto_error(retval, "error %d register_framebuffer", retval);
+
+	dev_info(dev->gdev, "SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
+		" Using %dK framebuffer memory\n", info->node,
+		info->var.xres, info->var.yres, info->fix.smem_len >> 10);
+
+	return 0;
+
+error:
+	if (dev) {
+		if (info) {
+			if (info->cmap.len != 0)
+				fb_dealloc_cmap(&info->cmap);
+			if (info->monspecs.modedb)
+				fb_destroy_modedb(info->monspecs.modedb);
+			if (info->screen_base)
+				vfree(info->screen_base);
+
+			fb_destroy_modelist(&info->modelist);
+
+			framebuffer_release(info);
+		}
+
+		kref_put(&dev->kref, ufx_free); /* ref for framebuffer */
+		kref_put(&dev->kref, ufx_free); /* last ref from kref_init */
+
+		/* dev has been deallocated. Do not dereference */
+	}
+
+	return retval;
+}
+
+static void ufx_usb_disconnect(struct usb_interface *interface)
+{
+	struct ufx_data *dev;
+	struct fb_info *info;
+
+	dev = usb_get_intfdata(interface);
+	info = dev->info;
+
+	pr_debug("USB disconnect starting\n");
+
+	/* we virtualize until all fb clients release. Then we free */
+	dev->virtualized = true;
+
+	/* When non-active we'll update virtual framebuffer, but no new urbs */
+	atomic_set(&dev->usb_active, 0);
+
+	usb_set_intfdata(interface, NULL);
+
+	/* if clients still have us open, will be freed on last close */
+	if (dev->fb_count = 0)
+		schedule_delayed_work(&dev->free_framebuffer_work, 0);
+
+	/* release reference taken by kref_init in probe() */
+	kref_put(&dev->kref, ufx_free);
+
+	/* consider ufx_data freed */
+}
+
+static struct usb_driver ufx_driver = {
+	.name = "smscufx",
+	.probe = ufx_usb_probe,
+	.disconnect = ufx_usb_disconnect,
+	.id_table = id_table,
+};
+
+static int __init ufx_module_init(void)
+{
+	int res;
+
+	res = usb_register(&ufx_driver);
+	if (res)
+		err("usb_register failed. Error number %d", res);
+
+	return res;
+}
+
+static void __exit ufx_module_exit(void)
+{
+	usb_deregister(&ufx_driver);
+}
+
+module_init(ufx_module_init);
+module_exit(ufx_module_exit);
+
+static void ufx_urb_completion(struct urb *urb)
+{
+	struct urb_node *unode = urb->context;
+	struct ufx_data *dev = unode->dev;
+	unsigned long flags;
+
+	/* sync/async unlink faults aren't errors */
+	if (urb->status) {
+		if (!(urb->status = -ENOENT ||
+		    urb->status = -ECONNRESET ||
+		    urb->status = -ESHUTDOWN)) {
+			pr_err("%s - nonzero write bulk status received: %d\n",
+				__func__, urb->status);
+			atomic_set(&dev->lost_pixels, 1);
+		}
+	}
+
+	urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
+
+	spin_lock_irqsave(&dev->urbs.lock, flags);
+	list_add_tail(&unode->entry, &dev->urbs.list);
+	dev->urbs.available++;
+	spin_unlock_irqrestore(&dev->urbs.lock, flags);
+
+	/* When using fb_defio, we deadlock if up() is called
+	 * while another is waiting. So queue to another process */
+	if (fb_defio)
+		schedule_delayed_work(&unode->release_urb_work, 0);
+	else
+		up(&dev->urbs.limit_sem);
+}
+
+static void ufx_free_urb_list(struct ufx_data *dev)
+{
+	int count = dev->urbs.count;
+	struct list_head *node;
+	struct urb_node *unode;
+	struct urb *urb;
+	int ret;
+	unsigned long flags;
+
+	pr_debug("Waiting for completes and freeing all render urbs\n");
+
+	/* keep waiting and freeing, until we've got 'em all */
+	while (count--) {
+		/* Getting interrupted means a leak, but ok at shutdown*/
+		ret = down_interruptible(&dev->urbs.limit_sem);
+		if (ret)
+			break;
+
+		spin_lock_irqsave(&dev->urbs.lock, flags);
+
+		node = dev->urbs.list.next; /* have reserved one with sem */
+		list_del_init(node);
+
+		spin_unlock_irqrestore(&dev->urbs.lock, flags);
+
+		unode = list_entry(node, struct urb_node, entry);
+		urb = unode->urb;
+
+		/* Free each separately allocated piece */
+		usb_free_coherent(urb->dev, dev->urbs.size,
+				  urb->transfer_buffer, urb->transfer_dma);
+		usb_free_urb(urb);
+		kfree(node);
+	}
+}
+
+static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
+{
+	int i = 0;
+	struct urb *urb;
+	struct urb_node *unode;
+	char *buf;
+
+	spin_lock_init(&dev->urbs.lock);
+
+	dev->urbs.size = size;
+	INIT_LIST_HEAD(&dev->urbs.list);
+
+	while (i < count) {
+		unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
+		if (!unode)
+			break;
+		unode->dev = dev;
+
+		INIT_DELAYED_WORK(&unode->release_urb_work,
+			  ufx_release_urb_work);
+
+		urb = usb_alloc_urb(0, GFP_KERNEL);
+		if (!urb) {
+			kfree(unode);
+			break;
+		}
+		unode->urb = urb;
+
+		buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
+					 &urb->transfer_dma);
+		if (!buf) {
+			kfree(unode);
+			usb_free_urb(urb);
+			break;
+		}
+
+		/* urb->transfer_buffer_length set to actual before submit */
+		usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
+			buf, size, ufx_urb_completion, unode);
+		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
+
+		list_add_tail(&unode->entry, &dev->urbs.list);
+
+		i++;
+	}
+
+	sema_init(&dev->urbs.limit_sem, i);
+	dev->urbs.count = i;
+	dev->urbs.available = i;
+
+	pr_debug("allocated %d %d byte urbs\n", i, (int) size);
+
+	return i;
+}
+
+static struct urb *ufx_get_urb(struct ufx_data *dev)
+{
+	int ret = 0;
+	struct list_head *entry;
+	struct urb_node *unode;
+	struct urb *urb = NULL;
+	unsigned long flags;
+
+	/* Wait for an in-flight buffer to complete and get re-queued */
+	ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
+	if (ret) {
+		atomic_set(&dev->lost_pixels, 1);
+		pr_warn("wait for urb interrupted: %x available: %d\n",
+		       ret, dev->urbs.available);
+		goto error;
+	}
+
+	spin_lock_irqsave(&dev->urbs.lock, flags);
+
+	BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
+	entry = dev->urbs.list.next;
+	list_del_init(entry);
+	dev->urbs.available--;
+
+	spin_unlock_irqrestore(&dev->urbs.lock, flags);
+
+	unode = list_entry(entry, struct urb_node, entry);
+	urb = unode->urb;
+
+error:
+	return urb;
+}
+
+static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
+{
+	int ret;
+
+	BUG_ON(len > dev->urbs.size);
+
+	urb->transfer_buffer_length = len; /* set to actual payload len */
+	ret = usb_submit_urb(urb, GFP_KERNEL);
+	if (ret) {
+		ufx_urb_completion(urb); /* because no one else will */
+		atomic_set(&dev->lost_pixels, 1);
+		pr_err("usb_submit_urb error %x\n", ret);
+	}
+	return ret;
+}
+
+module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
+MODULE_PARM_DESC(console, "Allow fbcon to be used on this display");
+
+module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
+MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
+
+MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
+MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
+MODULE_LICENSE("GPL");
-- 
1.7.4.1


^ permalink raw reply related

* [PATCH v2] Add support for SMSC UFX6000/7000 USB display adapters
From: Steve Glendinning @ 2011-08-18 14:20 UTC (permalink / raw)
  To: linux-fbdev

Updated following feedback from Greg K-H and Florian Schandinat.

I haven't made the changes to fb.h in this patch, so it still uses
the same hardcoded ioctl as udlfb.  The patch that adds this could
also update both udlfb and smscufx to use it.


Steve Glendinning (1):
  Add support for SMSC UFX6000/7000 USB display adapters

 MAINTAINERS             |    6 +
 drivers/video/Kconfig   |   16 +
 drivers/video/Makefile  |    1 +
 drivers/video/smscufx.c | 1994 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 2017 insertions(+), 0 deletions(-)
 create mode 100644 drivers/video/smscufx.c

-- 
1.7.4.1


^ permalink raw reply

* [PATCH] fbdev: fix indentation in modedb.c
From: Timur Tabi @ 2011-08-17 15:56 UTC (permalink / raw)
  To: linux-fbdev

Fix the incorrect indentation in functions fb_try_mode() and fb_find_mode().

Signed-off-by: Timur Tabi <timur@freescale.com>
---
 drivers/video/modedb.c |  444 ++++++++++++++++++++++++------------------------
 1 files changed, 225 insertions(+), 219 deletions(-)

diff --git a/drivers/video/modedb.c b/drivers/video/modedb.c
index cb175fe..a9a907c 100644
--- a/drivers/video/modedb.c
+++ b/drivers/video/modedb.c
@@ -491,55 +491,56 @@ EXPORT_SYMBOL(vesa_modes);
 static int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
 		       const struct fb_videomode *mode, unsigned int bpp)
 {
-    int err = 0;
-
-    DPRINTK("Trying mode %s %dx%d-%d@%d\n", mode->name ? mode->name : "noname",
-	    mode->xres, mode->yres, bpp, mode->refresh);
-    var->xres = mode->xres;
-    var->yres = mode->yres;
-    var->xres_virtual = mode->xres;
-    var->yres_virtual = mode->yres;
-    var->xoffset = 0;
-    var->yoffset = 0;
-    var->bits_per_pixel = bpp;
-    var->activate |= FB_ACTIVATE_TEST;
-    var->pixclock = mode->pixclock;
-    var->left_margin = mode->left_margin;
-    var->right_margin = mode->right_margin;
-    var->upper_margin = mode->upper_margin;
-    var->lower_margin = mode->lower_margin;
-    var->hsync_len = mode->hsync_len;
-    var->vsync_len = mode->vsync_len;
-    var->sync = mode->sync;
-    var->vmode = mode->vmode;
-    if (info->fbops->fb_check_var)
-    	err = info->fbops->fb_check_var(var, info);
-    var->activate &= ~FB_ACTIVATE_TEST;
-    return err;
+	int err = 0;
+
+	DPRINTK("Trying mode %s %dx%d-%d@%d\n",
+		mode->name ? mode->name : "noname",
+		mode->xres, mode->yres, bpp, mode->refresh);
+	var->xres = mode->xres;
+	var->yres = mode->yres;
+	var->xres_virtual = mode->xres;
+	var->yres_virtual = mode->yres;
+	var->xoffset = 0;
+	var->yoffset = 0;
+	var->bits_per_pixel = bpp;
+	var->activate |= FB_ACTIVATE_TEST;
+	var->pixclock = mode->pixclock;
+	var->left_margin = mode->left_margin;
+	var->right_margin = mode->right_margin;
+	var->upper_margin = mode->upper_margin;
+	var->lower_margin = mode->lower_margin;
+	var->hsync_len = mode->hsync_len;
+	var->vsync_len = mode->vsync_len;
+	var->sync = mode->sync;
+	var->vmode = mode->vmode;
+	if (info->fbops->fb_check_var)
+		err = info->fbops->fb_check_var(var, info);
+	var->activate &= ~FB_ACTIVATE_TEST;
+	return err;
 }
 
 /**
- *	fb_find_mode - finds a valid video mode
- *	@var: frame buffer user defined part of display
- *	@info: frame buffer info structure
- *	@mode_option: string video mode to find
- *	@db: video mode database
- *	@dbsize: size of @db
- *	@default_mode: default video mode to fall back to
- *	@default_bpp: default color depth in bits per pixel
+ *     fb_find_mode - finds a valid video mode
+ *     @var: frame buffer user defined part of display
+ *     @info: frame buffer info structure
+ *     @mode_option: string video mode to find
+ *     @db: video mode database
+ *     @dbsize: size of @db
+ *     @default_mode: default video mode to fall back to
+ *     @default_bpp: default color depth in bits per pixel
  *
- *	Finds a suitable video mode, starting with the specified mode
- *	in @mode_option with fallback to @default_mode.  If
- *	@default_mode fails, all modes in the video mode database will
- *	be tried.
+ *     Finds a suitable video mode, starting with the specified mode
+ *     in @mode_option with fallback to @default_mode.  If
+ *     @default_mode fails, all modes in the video mode database will
+ *     be tried.
  *
- *	Valid mode specifiers for @mode_option:
+ *     Valid mode specifiers for @mode_option:
  *
- *	<xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m] or
- *	<name>[-<bpp>][@<refresh>]
+ *     <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m] or
+ *     <name>[-<bpp>][@<refresh>]
  *
- *	with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
- *	<name> a string.
+ *     with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
+ *     <name> a string.
  *
  *      If 'M' is present after yres (and before refresh/bpp if present),
  *      the function will compute the timings using VESA(tm) Coordinated
@@ -551,12 +552,12 @@ static int fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
  *
  *      1024x768MR-8@60m - Reduced blank with margins at 60Hz.
  *
- *	NOTE: The passed struct @var is _not_ cleared!  This allows you
- *	to supply values for e.g. the grayscale and accel_flags fields.
+ *     NOTE: The passed struct @var is _not_ cleared!  This allows you
+ *     to supply values for e.g. the grayscale and accel_flags fields.
  *
- *	Returns zero for failure, 1 if using specified @mode_option,
- *	2 if using specified @mode_option with an ignored refresh rate,
- *	3 if default mode is used, 4 if fall back to any valid mode.
+ *     Returns zero for failure, 1 if using specified @mode_option,
+ *     2 if using specified @mode_option with an ignored refresh rate,
+ *     3 if default mode is used, 4 if fall back to any valid mode.
  *
  */
 
@@ -566,198 +567,203 @@ int fb_find_mode(struct fb_var_screeninfo *var,
 		 const struct fb_videomode *default_mode,
 		 unsigned int default_bpp)
 {
-    int i;
-
-    /* Set up defaults */
-    if (!db) {
-	db = modedb;
-	dbsize = ARRAY_SIZE(modedb);
-    }
-
-    if (!default_mode)
-	default_mode = &db[0];
-
-    if (!default_bpp)
-	default_bpp = 8;
-
-    /* Did the user specify a video mode? */
-    if (!mode_option)
-	mode_option = fb_mode_option;
-    if (mode_option) {
-	const char *name = mode_option;
-	unsigned int namelen = strlen(name);
-	int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
-	unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
-	int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
-	u32 best, diff, tdiff;
-
-	for (i = namelen-1; i >= 0; i--) {
-	    switch (name[i]) {
-		case '@':
-		    namelen = i;
-		    if (!refresh_specified && !bpp_specified &&
-			!yres_specified) {
-			refresh = simple_strtol(&name[i+1], NULL, 10);
-			refresh_specified = 1;
-			if (cvt || rb)
-			    cvt = 0;
-		    } else
-			goto done;
-		    break;
-		case '-':
-		    namelen = i;
-		    if (!bpp_specified && !yres_specified) {
-			bpp = simple_strtol(&name[i+1], NULL, 10);
-			bpp_specified = 1;
-			if (cvt || rb)
-			    cvt = 0;
-		    } else
-			goto done;
-		    break;
-		case 'x':
-		    if (!yres_specified) {
-			yres = simple_strtol(&name[i+1], NULL, 10);
-			yres_specified = 1;
-		    } else
-			goto done;
-		    break;
-		case '0' ... '9':
-		    break;
-		case 'M':
-		    if (!yres_specified)
-			cvt = 1;
-		    break;
-		case 'R':
-		    if (!cvt)
-			rb = 1;
-		    break;
-		case 'm':
-		    if (!cvt)
-			margins = 1;
-		    break;
-		case 'i':
-		    if (!cvt)
-			interlace = 1;
-		    break;
-		default:
-		    goto done;
-	    }
-	}
-	if (i < 0 && yres_specified) {
-	    xres = simple_strtol(name, NULL, 10);
-	    res_specified = 1;
-	}
-done:
-	if (cvt) {
-	    struct fb_videomode cvt_mode;
-	    int ret;
-
-	    DPRINTK("CVT mode %dx%d@%dHz%s%s%s\n", xres, yres,
-		    (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
-		    "", (margins) ? " with margins" : "", (interlace) ?
-		    " interlaced" : "");
-
-	    memset(&cvt_mode, 0, sizeof(cvt_mode));
-	    cvt_mode.xres = xres;
-	    cvt_mode.yres = yres;
-	    cvt_mode.refresh = (refresh) ? refresh : 60;
+	int i;
 
-	    if (interlace)
-		cvt_mode.vmode |= FB_VMODE_INTERLACED;
-	    else
-		cvt_mode.vmode &= ~FB_VMODE_INTERLACED;
+	/* Set up defaults */
+	if (!db) {
+		db = modedb;
+		dbsize = ARRAY_SIZE(modedb);
+	}
 
-	    ret = fb_find_mode_cvt(&cvt_mode, margins, rb);
+	if (!default_mode)
+		default_mode = &db[0];
+
+	if (!default_bpp)
+		default_bpp = 8;
+
+	/* Did the user specify a video mode? */
+	if (!mode_option)
+		mode_option = fb_mode_option;
+	if (mode_option) {
+		const char *name = mode_option;
+		unsigned int namelen = strlen(name);
+		int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
+		unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
+		int yres_specified = 0, cvt = 0, rb = 0, interlace = 0;
+		int margins = 0;
+		u32 best, diff, tdiff;
+
+		for (i = namelen-1; i >= 0; i--) {
+			switch (name[i]) {
+			case '@':
+				namelen = i;
+				if (!refresh_specified && !bpp_specified &&
+				    !yres_specified) {
+					refresh = simple_strtol(&name[i+1], NULL,
+								10);
+					refresh_specified = 1;
+					if (cvt || rb)
+						cvt = 0;
+				} else
+					goto done;
+				break;
+			case '-':
+				namelen = i;
+				if (!bpp_specified && !yres_specified) {
+					bpp = simple_strtol(&name[i+1], NULL,
+							    10);
+					bpp_specified = 1;
+					if (cvt || rb)
+						cvt = 0;
+				} else
+					goto done;
+				break;
+			case 'x':
+				if (!yres_specified) {
+					yres = simple_strtol(&name[i+1], NULL,
+							     10);
+					yres_specified = 1;
+				} else
+					goto done;
+				break;
+			case '0' ... '9':
+				break;
+			case 'M':
+				if (!yres_specified)
+					cvt = 1;
+				break;
+			case 'R':
+				if (!cvt)
+					rb = 1;
+				break;
+			case 'm':
+				if (!cvt)
+					margins = 1;
+				break;
+			case 'i':
+				if (!cvt)
+					interlace = 1;
+				break;
+			default:
+				goto done;
+			}
+		}
+		if (i < 0 && yres_specified) {
+			xres = simple_strtol(name, NULL, 10);
+			res_specified = 1;
+		}
+done:
+		if (cvt) {
+			struct fb_videomode cvt_mode;
+			int ret;
+
+			DPRINTK("CVT mode %dx%d@%dHz%s%s%s\n", xres, yres,
+				(refresh) ? refresh : 60,
+				(rb) ? " reduced blanking" : "",
+				(margins) ? " with margins" : "",
+				(interlace) ? " interlaced" : "");
+
+			memset(&cvt_mode, 0, sizeof(cvt_mode));
+			cvt_mode.xres = xres;
+			cvt_mode.yres = yres;
+			cvt_mode.refresh = (refresh) ? refresh : 60;
+
+			if (interlace)
+				cvt_mode.vmode |= FB_VMODE_INTERLACED;
+			else
+				cvt_mode.vmode &= ~FB_VMODE_INTERLACED;
+
+			ret = fb_find_mode_cvt(&cvt_mode, margins, rb);
+
+			if (!ret && !fb_try_mode(var, info, &cvt_mode, bpp)) {
+				DPRINTK("modedb CVT: CVT mode ok\n");
+				return 1;
+			}
 
-	    if (!ret && !fb_try_mode(var, info, &cvt_mode, bpp)) {
-		DPRINTK("modedb CVT: CVT mode ok\n");
-		return 1;
-	    }
+			DPRINTK("CVT mode invalid, getting mode from database\n");
+		}
 
-	    DPRINTK("CVT mode invalid, getting mode from database\n");
-	}
+		DPRINTK("Trying specified video mode%s %ix%i\n",
+			refresh_specified ? "" : " (ignoring refresh rate)",
+			xres, yres);
 
-	DPRINTK("Trying specified video mode%s %ix%i\n",
-	    refresh_specified ? "" : " (ignoring refresh rate)", xres, yres);
-
-	if (!refresh_specified) {
-		/*
-		 * If the caller has provided a custom mode database and a
-		 * valid monspecs structure, we look for the mode with the
-		 * highest refresh rate.  Otherwise we play it safe it and
-		 * try to find a mode with a refresh rate closest to the
-		 * standard 60 Hz.
-		 */
-		if (db != modedb &&
-		    info->monspecs.vfmin && info->monspecs.vfmax &&
-		    info->monspecs.hfmin && info->monspecs.hfmax &&
-		    info->monspecs.dclkmax) {
-			refresh = 1000;
-		} else {
-			refresh = 60;
+		if (!refresh_specified) {
+			/*
+			 * If the caller has provided a custom mode database and
+			 * a valid monspecs structure, we look for the mode with
+			 * the highest refresh rate.  Otherwise we play it safe
+			 * it and try to find a mode with a refresh rate closest
+			 * to the standard 60 Hz.
+			 */
+			if (db != modedb &&
+			    info->monspecs.vfmin && info->monspecs.vfmax &&
+			    info->monspecs.hfmin && info->monspecs.hfmax &&
+			    info->monspecs.dclkmax) {
+				refresh = 1000;
+			} else {
+				refresh = 60;
+			}
 		}
-	}
 
-	diff = -1;
-	best = -1;
-	for (i = 0; i < dbsize; i++) {
-		if ((name_matches(db[i], name, namelen) ||
-		    (res_specified && res_matches(db[i], xres, yres))) &&
-		    !fb_try_mode(var, info, &db[i], bpp)) {
-			if (refresh_specified && db[i].refresh = refresh) {
-				return 1;
-			} else {
+		diff = -1;
+		best = -1;
+		for (i = 0; i < dbsize; i++) {
+			if ((name_matches(db[i], name, namelen) ||
+			     (res_specified && res_matches(db[i], xres, yres))) &&
+			    !fb_try_mode(var, info, &db[i], bpp)) {
+				if (refresh_specified && db[i].refresh = refresh)
+					return 1;
+
 				if (abs(db[i].refresh - refresh) < diff) {
 					diff = abs(db[i].refresh - refresh);
 					best = i;
 				}
 			}
 		}
-	}
-	if (best != -1) {
-		fb_try_mode(var, info, &db[best], bpp);
-		return (refresh_specified) ? 2 : 1;
-	}
-
-	diff = 2 * (xres + yres);
-	best = -1;
-	DPRINTK("Trying best-fit modes\n");
-	for (i = 0; i < dbsize; i++) {
-		DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
-		if (!fb_try_mode(var, info, &db[i], bpp)) {
-			tdiff = abs(db[i].xres - xres) +
-				abs(db[i].yres - yres);
-
-			/*
-			 * Penalize modes with resolutions smaller
-			 * than requested.
-			 */
-			if (xres > db[i].xres || yres > db[i].yres)
-				tdiff += xres + yres;
+		if (best != -1) {
+			fb_try_mode(var, info, &db[best], bpp);
+			return (refresh_specified) ? 2 : 1;
+		}
 
-			if (diff > tdiff) {
-				diff = tdiff;
-				best = i;
+		diff = 2 * (xres + yres);
+		best = -1;
+		DPRINTK("Trying best-fit modes\n");
+		for (i = 0; i < dbsize; i++) {
+			DPRINTK("Trying %ix%i\n", db[i].xres, db[i].yres);
+			if (!fb_try_mode(var, info, &db[i], bpp)) {
+				tdiff = abs(db[i].xres - xres) +
+					abs(db[i].yres - yres);
+
+				/*
+				 * Penalize modes with resolutions smaller
+				 * than requested.
+				 */
+				if (xres > db[i].xres || yres > db[i].yres)
+					tdiff += xres + yres;
+
+				if (diff > tdiff) {
+					diff = tdiff;
+					best = i;
+				}
 			}
 		}
+		if (best != -1) {
+			fb_try_mode(var, info, &db[best], bpp);
+			return 5;
+		}
 	}
-	if (best != -1) {
-	    fb_try_mode(var, info, &db[best], bpp);
-	    return 5;
-	}
-    }
 
-    DPRINTK("Trying default video mode\n");
-    if (!fb_try_mode(var, info, default_mode, default_bpp))
-	return 3;
+	DPRINTK("Trying default video mode\n");
+	if (!fb_try_mode(var, info, default_mode, default_bpp))
+		return 3;
 
-    DPRINTK("Trying all modes\n");
-    for (i = 0; i < dbsize; i++)
-	if (!fb_try_mode(var, info, &db[i], default_bpp))
-	    return 4;
+	DPRINTK("Trying all modes\n");
+	for (i = 0; i < dbsize; i++)
+		if (!fb_try_mode(var, info, &db[i], default_bpp))
+			return 4;
 
-    DPRINTK("No valid mode found\n");
-    return 0;
+	DPRINTK("No valid mode found\n");
+	return 0;
 }
 
 /**
-- 
1.7.4.4



^ permalink raw reply related

* Re: [PATCH] Add support for SMSC UFX6000/7000 USB display adapters
From: Florian Tobias Schandinat @ 2011-08-16 17:32 UTC (permalink / raw)
  To: linux-fbdev
In-Reply-To: <1313150823-2527-1-git-send-email-steve.glendinning@smsc.com>

Hi Bernie,

On 08/16/2011 04:53 PM, Bernie Thompson wrote:
> Hi Florian,
> 
> On Tue, Aug 16, 2011 at 3:17 AM, Florian Tobias Schandinat
> <FlorianSchandinat@gmx.de> wrote:
>>> + * TODO: Propose standard fb.h ioctl for reporting damage,
>>> + * using _IOWR() and one of the existing area structs from fb.h
>>> + * Consider these ioctls deprecated, but they're still used by the
>>> + * DisplayLink X server as yet - need both to be modified in tandem
>>> + * when new ioctl(s) are ready.
>>> + */
>>> +#define UFX_IOCTL_RETURN_EDID        (0xAD)
>>> +#define UFX_IOCTL_REPORT_DAMAGE      (0xAA)
>>
>> Why don't we solve this now? So what do you need?
> 
> It would be wonderful to solve this now.  This capability is needed
> for any device which doesn't have a directly mmapable hardware
> framebuffer (e.g. any USB graphics device, but also remote
> framebuffers, devices like eink controllers, etc.)
> 
> If we define the standard interface, we'll also modify udlfb (the
> DisplayLink driver) to match, so we'll have several clients of the
> interface in the tree from day one.
> 
> And we also have a patch to the xorg fbdev driver to be a client of
> this interface -- to pass X DAMAGE protocol notifications to the
> kernel framebuffer, allowing all these devices to work with common
> clients (whereas they can't today, without fb_defio or some other
> trick to detect framebuffer writes).
> 
> Here is the xorg fbdev patch that can be updated to the new interface:
> http://git.plugable.com/gitphp/index.php?p=xf86-video-fbdev&a=commitdiff&hÛfe3f839ee00135a7cccf13db5926fb1019abde
> 
>> An IOCTL, a struct like
>> struct dloarea {
>>        int x, y;
>>        int w, h;
>> };
>> and I don't think any of the existing structs in fb.h would fit. I'd just prefer
>> to have those int's replaced by something of known size, probably __u32 to make
>> it consistent with fb_var_screeninfo?
> 
> Yes. that'd be perfect.   Could be as simple as
> 
> struct fb_rect {
> 	__u32_t x;
> 	__u32_t y;
> 	__u32_t width;
> 	__u32_t height;
> };
> #define FBIOPUT_DAMAGE _IOW('F', 0x1A, struct fb_rect)
> 
> (with the 1A ordinal being out of date now).  But anything similarly
> simple is fine.

Yes, I think we can add this to fb.h without any problems.

>> Maybe this should be also reported by the capability field that Laurent's API
>> extension will likely add.
> 
> Definitely.  In addition to the above, the only other thing needed is
> capability negotiation between the fbdev client (say, xorg) and the
> fbdev driver.  The negotiation has to be two-step:
> 
> The fbdev client tells the driver that it's damage-aware (A)
> The fbdev driver returns to the client whether it requires damage (B)
> 
> If A and B, then the client will send damage and the driver will rely
> on it (and not use fb_defio, timers, or other backup)
> If A but not B, then the client should avoid unncessary overhead of
> sending damage notifications that the driver isn't listening to.
> If not A, then the driver must use fb_defio, timers, or some of the
> other (less-efficient) backup mechanisms to make the mmap client's
> framebuffer writes get noticed.
> 
> Timing: When the driver doesn't have a damage-aware client, they often
> fall back to using fb_defio.  fb_defio requires special setup
> (framebuffer mmap with pages set to fault on writes), that has
> complexities that are best avoided if possible.  So we want step (A)
> to happen before mmap, at least, and we should define the API to make
> it happen at the earliest point we can define.
> 
> You can see the xorg driver patch above, and the smsc and displaylink
> drivers doing this today, but in not the prettiest way (as soon as the
> driver gets the first damage notification, it tries to stop using
> fb_defio).  But an explicit capability negotiation, defined to happen
> at the earliest point, would be cleaner.

What's the problem with the way you do it now? (aside that you should IMHO reset
the flag whether the client does use damage or not on set_par/mode change)
The extension I proposed and is part of Laurent's patches is a capability
reporting from the driver to the client, that would solve (B). Sure we could
think of anything to make the other way (A) possible but I doubt that it would
be worth it as I think you have to keep defio support around anyway for
applications that do not support damage reporting. I think implementing (A)
would just needlessly add complexity to the drivers as it would be some sort of
alternative mmap IOCTL. Or we could try to find yet another flag inside var that
is unused for it but that is difficult.

> All the pieces are close. It would be great to get this standardized!
> Thanks for stepping in to help lead this.

Sure, all the nice features will just be used if they are easy usable from user
space, so providing an API instead of requiring user space to know individual
drivers is a good thing.


Best regards,

Florian Tobias Schandinat

^ permalink raw reply

* Re: [PATCH] Add support for SMSC UFX6000/7000 USB display adapters
From: Bernie Thompson @ 2011-08-16 16:53 UTC (permalink / raw)
  To: linux-fbdev
In-Reply-To: <1313150823-2527-1-git-send-email-steve.glendinning@smsc.com>

Hi Florian,

On Tue, Aug 16, 2011 at 3:17 AM, Florian Tobias Schandinat
<FlorianSchandinat@gmx.de> wrote:
>> + * TODO: Propose standard fb.h ioctl for reporting damage,
>> + * using _IOWR() and one of the existing area structs from fb.h
>> + * Consider these ioctls deprecated, but they're still used by the
>> + * DisplayLink X server as yet - need both to be modified in tandem
>> + * when new ioctl(s) are ready.
>> + */
>> +#define UFX_IOCTL_RETURN_EDID        (0xAD)
>> +#define UFX_IOCTL_REPORT_DAMAGE      (0xAA)
>
> Why don't we solve this now? So what do you need?

It would be wonderful to solve this now.  This capability is needed
for any device which doesn't have a directly mmapable hardware
framebuffer (e.g. any USB graphics device, but also remote
framebuffers, devices like eink controllers, etc.)

If we define the standard interface, we'll also modify udlfb (the
DisplayLink driver) to match, so we'll have several clients of the
interface in the tree from day one.

And we also have a patch to the xorg fbdev driver to be a client of
this interface -- to pass X DAMAGE protocol notifications to the
kernel framebuffer, allowing all these devices to work with common
clients (whereas they can't today, without fb_defio or some other
trick to detect framebuffer writes).

Here is the xorg fbdev patch that can be updated to the new interface:
http://git.plugable.com/gitphp/index.php?p=xf86-video-fbdev&a=commitdiff&hÛfe3f839ee00135a7cccf13db5926fb1019abde

> An IOCTL, a struct like
> struct dloarea {
>        int x, y;
>        int w, h;
> };
> and I don't think any of the existing structs in fb.h would fit. I'd just prefer
> to have those int's replaced by something of known size, probably __u32 to make
> it consistent with fb_var_screeninfo?

Yes. that'd be perfect.   Could be as simple as

struct fb_rect {
	__u32_t x;
	__u32_t y;
	__u32_t width;
	__u32_t height;
};
#define FBIOPUT_DAMAGE _IOW('F', 0x1A, struct fb_rect)

(with the 1A ordinal being out of date now).  But anything similarly
simple is fine.

> Maybe this should be also reported by the capability field that Laurent's API
> extension will likely add.

Definitely.  In addition to the above, the only other thing needed is
capability negotiation between the fbdev client (say, xorg) and the
fbdev driver.  The negotiation has to be two-step:

The fbdev client tells the driver that it's damage-aware (A)
The fbdev driver returns to the client whether it requires damage (B)

If A and B, then the client will send damage and the driver will rely
on it (and not use fb_defio, timers, or other backup)
If A but not B, then the client should avoid unncessary overhead of
sending damage notifications that the driver isn't listening to.
If not A, then the driver must use fb_defio, timers, or some of the
other (less-efficient) backup mechanisms to make the mmap client's
framebuffer writes get noticed.

Timing: When the driver doesn't have a damage-aware client, they often
fall back to using fb_defio.  fb_defio requires special setup
(framebuffer mmap with pages set to fault on writes), that has
complexities that are best avoided if possible.  So we want step (A)
to happen before mmap, at least, and we should define the API to make
it happen at the earliest point we can define.

You can see the xorg driver patch above, and the smsc and displaylink
drivers doing this today, but in not the prettiest way (as soon as the
driver gets the first damage notification, it tries to stop using
fb_defio).  But an explicit capability negotiation, defined to happen
at the earliest point, would be cleaner.

I don't know Laurent's API.  Do we have the tools in the interface to
do a two-step negotiation to figure out whether (both) client and
driver support this new damage reporting interface?

All the pieces are close. It would be great to get this standardized!
Thanks for stepping in to help lead this.

Best wishes,
Bernie
http://plugable.com/

^ permalink raw reply

* Re: [PATCH] Add support for SMSC UFX6000/7000 USB display adapters
From: Florian Tobias Schandinat @ 2011-08-16 10:17 UTC (permalink / raw)
  To: linux-fbdev
In-Reply-To: <1313150823-2527-1-git-send-email-steve.glendinning@smsc.com>

Hi,

On 08/12/2011 12:07 PM, Steve Glendinning wrote:
> This patch adds framebuffer suport for SMSC's UFX6000 (USB 2.0) and
> UFX7000 (USB 3.0) display adapters.
> 
> Signed-off-by: Steve Glendinning <steve.glendinning@smsc.com>
> ---
>  MAINTAINERS             |    6 +
>  drivers/video/Kconfig   |   16 +
>  drivers/video/Makefile  |    1 +
>  drivers/video/smscufx.c | 2009 +++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 2032 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/video/smscufx.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 1e55e1e..80d5555 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -5920,6 +5920,12 @@ L:	netdev@vger.kernel.org
>  S:	Supported
>  F:	drivers/net/smsc9420.*
>  
> +SMSC UFX6000 and UFX7000 USB to VGA DRIVER
> +M:	Steve Glendinning <steve.glendinning@smsc.com>
> +L:	linux-fbdev@vger.kernel.org
> +S:	Supported
> +F:	drivers/video/smscufx.c
> +
>  SN-IA64 (Itanium) SUB-PLATFORM
>  M:	Jes Sorensen <jes@sgi.com>
>  L:	linux-altix@sgi.com
> diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
> index 549b960..8bfb387 100644
> --- a/drivers/video/Kconfig
> +++ b/drivers/video/Kconfig
> @@ -2110,6 +2110,22 @@ config FB_SM501
>  
>  	  If unsure, say N.
>  
> +config FB_SMSCUFX
> +	tristate "SMSC UFX6000/7000 USB Framebuffer support"
> +	depends on FB && USB
> +	select FB_MODE_HELPERS
> +	select FB_SYS_FILLRECT
> +	select FB_SYS_COPYAREA
> +	select FB_SYS_IMAGEBLIT
> +	select FB_SYS_FOPS
> +	select FB_DEFERRED_IO
> +	---help---
> +	  This is a kernel framebuffer driver for SMSC UFX USB devices.
> +	  Supports fbdev clients like xf86-video-fbdev, kdrive, fbi, and
> +	  mplayer -vo fbdev. Supports both UFX6000 (USB 2.0) and UFX7000
> +	  (USB 3.0) devices.
> +	  To compile as a module, choose M here: the module name is smscufx.
> +
>  config FB_UDL
>  	tristate "Displaylink USB Framebuffer support"
>  	depends on FB && USB
> diff --git a/drivers/video/Makefile b/drivers/video/Makefile
> index 8b83129..f3546ec 100644
> --- a/drivers/video/Makefile
> +++ b/drivers/video/Makefile
> @@ -127,6 +127,7 @@ obj-$(CONFIG_FB_IBM_GXT4500)	  += gxt4500.o
>  obj-$(CONFIG_FB_PS3)		  += ps3fb.o
>  obj-$(CONFIG_FB_SM501)            += sm501fb.o
>  obj-$(CONFIG_FB_UDL)		  += udlfb.o
> +obj-$(CONFIG_FB_SMSCUFX)	  += smscufx.o
>  obj-$(CONFIG_FB_XILINX)           += xilinxfb.o
>  obj-$(CONFIG_SH_MIPI_DSI)	  += sh_mipi_dsi.o
>  obj-$(CONFIG_FB_SH_MOBILE_HDMI)	  += sh_mobile_hdmi.o
> diff --git a/drivers/video/smscufx.c b/drivers/video/smscufx.c
> new file mode 100644
> index 0000000..2d1a7b2
> --- /dev/null
> +++ b/drivers/video/smscufx.c
> @@ -0,0 +1,2009 @@
> +/*
> + * smscufx.c -- Framebuffer driver for SMSC UFX USB controller
> + *
> + * Copyright (C) 2011 Steve Glendinning <steve.glendinning@smsc.com>
> + * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
> + * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
> + * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
> + *
> + * This file is subject to the terms and conditions of the GNU General Public
> + * License v2. See the file COPYING in the main directory of this archive for
> + * more details.
> + *
> + * Based on udlfb, with work from Florian Echtler, Henrik Bjerregaard Pedersen,
> + * and others.
> + *
> + * Works well with Bernie Thompson's X DAMAGE patch to xf86-video-fbdev
> + * available from http://git.plugable.com
> + *
> + * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
> + * usb-skeleton by GregKH.
> + */
> +
> +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/init.h>
> +#include <linux/usb.h>
> +#include <linux/uaccess.h>
> +#include <linux/mm.h>
> +#include <linux/fb.h>
> +#include <linux/vmalloc.h>
> +#include <linux/slab.h>
> +#include <linux/delay.h>
> +#include "edid.h"
> +
> +#define check_warn(status, fmt, args...) \
> +	({ if (status < 0) pr_warn(fmt, ##args); })
> +
> +#define check_warn_return(status, fmt, args...) \
> +	({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
> +
> +#define check_warn_goto_error(status, fmt, args...) \
> +	({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
> +
> +#define all_bits_set(x, bits) (((x) & (bits)) = (bits))
> +
> +#define USB_VENDOR_REQUEST_WRITE_REGISTER	0xA0
> +#define USB_VENDOR_REQUEST_READ_REGISTER	0xA1
> +
> +/*
> + * TODO: Propose standard fb.h ioctl for reporting damage,
> + * using _IOWR() and one of the existing area structs from fb.h
> + * Consider these ioctls deprecated, but they're still used by the
> + * DisplayLink X server as yet - need both to be modified in tandem
> + * when new ioctl(s) are ready.
> + */
> +#define UFX_IOCTL_RETURN_EDID	(0xAD)
> +#define UFX_IOCTL_REPORT_DAMAGE	(0xAA)

Why don't we solve this now? So what do you need?
An IOCTL, a struct like
struct dloarea {
	int x, y;
	int w, h;
};
and I don't think any of the existing structs in fb.h would fit. I'd just prefer
to have those int's replaced by something of known size, probably __u32 to make
it consistent with fb_var_screeninfo?
Maybe this should be also reported by the capability field that Laurent's API
extension will likely add.

> +
> +/* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
> +#define BULK_SIZE		(512)
> +#define MAX_TRANSFER		(PAGE_SIZE*16 - BULK_SIZE)
> +#define WRITES_IN_FLIGHT	(4)
> +
> +#define GET_URB_TIMEOUT		(HZ)
> +#define FREE_URB_TIMEOUT	(HZ*2)
> +
> +#define BPP			2
> +
> +#define UFX_DEFIO_WRITE_DELAY	5 /* fb_deferred_io.delay in jiffies */
> +#define UFX_DEFIO_WRITE_DISABLE	(HZ*60) /* "disable" with long delay */
> +
> +struct dloarea {
> +	int x, y;
> +	int w, h;
> +};
> +
> +struct urb_node {
> +	struct list_head entry;
> +	struct ufx_data *dev;
> +	struct delayed_work release_urb_work;
> +	struct urb *urb;
> +};
> +
> +struct urb_list {
> +	struct list_head list;
> +	spinlock_t lock;
> +	struct semaphore limit_sem;
> +	int available;
> +	int count;
> +	size_t size;
> +};
> +
> +struct ufx_data {
> +	struct usb_device *udev;
> +	struct device *gdev; /* &udev->dev */
> +	struct fb_info *info;
> +	struct urb_list urbs;
> +	struct kref kref;
> +	int fb_count;
> +	bool virtualized; /* true when physical usb device not present */
> +	struct delayed_work free_framebuffer_work;
> +	atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */
> +	atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */
> +	char *edid; /* null until we read edid from hw or get from sysfs */
> +	size_t edid_size;
> +	u32 pseudo_palette[256];
> +};
> +
> +static struct fb_fix_screeninfo ufx_fix = {
> +	.id =           "smscufx",
> +	.type =         FB_TYPE_PACKED_PIXELS,
> +	.visual =       FB_VISUAL_TRUECOLOR,
> +	.xpanstep =     0,
> +	.ypanstep =     0,
> +	.ywrapstep =    0,
> +	.accel =        FB_ACCEL_NONE,
> +};
> +
> +static const u32 smscufx_info_flags = FBINFO_DEFAULT | FBINFO_READS_FAST |
> +	FBINFO_VIRTFB |	FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
> +	FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
> +
> +static struct usb_device_id id_table[] = {
> +	{USB_DEVICE(0x0424, 0x9d00),},
> +	{USB_DEVICE(0x0424, 0x9d01),},
> +	{},
> +};
> +MODULE_DEVICE_TABLE(usb, id_table);
> +
> +/* module options */
> +static int console;   /* Optionally allow fbcon to consume first framebuffer */
> +static int fb_defio = true;  /* Optionally enable fb_defio mmap support */
> +
> +/* ufx keeps a list of urbs for efficient bulk transfers */
> +static void ufx_urb_completion(struct urb *urb);
> +static struct urb *ufx_get_urb(struct ufx_data *dev);
> +static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
> +static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
> +static void ufx_free_urb_list(struct ufx_data *dev);
> +
> +/* reads a control register */
> +static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
> +{
> +	u32 *buf = kmalloc(4, GFP_KERNEL);
> +	int ret;
> +
> +	BUG_ON(!dev);
> +
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
> +		USB_VENDOR_REQUEST_READ_REGISTER,
> +		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
> +		00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
> +
> +	le32_to_cpus(buf);
> +	*data = *buf;
> +	kfree(buf);
> +
> +	if (unlikely(ret < 0))
> +		pr_warn("Failed to read register index 0x%08x\n", index);
> +
> +	return ret;
> +}
> +
> +/* writes a control register */
> +static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
> +{
> +	u32 *buf = kmalloc(4, GFP_KERNEL);
> +	int ret;
> +
> +	BUG_ON(!dev);
> +
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	*buf = data;
> +	cpu_to_le32s(buf);
> +
> +	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
> +		USB_VENDOR_REQUEST_WRITE_REGISTER,
> +		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
> +		00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
> +
> +	kfree(buf);
> +
> +	if (unlikely(ret < 0))
> +		pr_warn("Failed to write register index 0x%08x with value "
> +			"0x%08x\n", index, data);
> +
> +	return ret;
> +}
> +
> +static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
> +	u32 bits_to_clear, u32 bits_to_set)
> +{
> +	u32 data;
> +	int status = ufx_reg_read(dev, index, &data);
> +	check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
> +		"0x%x", index);
> +
> +	data &= (~bits_to_clear);
> +	data |= bits_to_set;
> +
> +	status = ufx_reg_write(dev, index, data);
> +	check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
> +		"0x%x", index);
> +
> +	return 0;
> +}
> +
> +static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
> +{
> +	return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
> +}
> +
> +static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
> +{
> +	return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
> +}
> +
> +static int ufx_lite_reset(struct ufx_data *dev)
> +{
> +	int status;
> +	u32 value;
> +
> +	status = ufx_reg_write(dev, 0x3008, 0x00000001);
> +	check_warn_return(status, "ufx_lite_reset error writing 0x3008");
> +
> +	status = ufx_reg_read(dev, 0x3008, &value);
> +	check_warn_return(status, "ufx_lite_reset error reading 0x3008");
> +
> +	return (value = 0) ? 0 : -EIO;
> +}
> +
> +/* If display is unblanked, then blank it */
> +static int ufx_blank(struct ufx_data *dev, bool wait)
> +{
> +	u32 dc_ctrl, dc_sts;
> +	int i;
> +
> +	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +	check_warn_return(status, "ufx_blank error reading 0x2004");
> +
> +	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
> +	check_warn_return(status, "ufx_blank error reading 0x2000");
> +
> +	/* return success if display is already blanked */
> +	if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
> +		return 0;
> +
> +	/* request the DC to blank the display */
> +	dc_ctrl |= 0x00000100;
> +	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
> +	check_warn_return(status, "ufx_blank error writing 0x2000");
> +
> +	/* return success immediately if we don't have to wait */
> +	if (!wait)
> +		return 0;
> +
> +	for (i = 0; i < 250; i++) {
> +		status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +		check_warn_return(status, "ufx_blank error reading 0x2004");
> +
> +		if (dc_sts & 0x00000100)
> +			return 0;
> +	}
> +
> +	/* timed out waiting for display to blank */
> +	return -EIO;
> +}
> +
> +/* If display is blanked, then unblank it */
> +static int ufx_unblank(struct ufx_data *dev, bool wait)
> +{
> +	u32 dc_ctrl, dc_sts;
> +	int i;
> +
> +	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +	check_warn_return(status, "ufx_unblank error reading 0x2004");
> +
> +	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
> +	check_warn_return(status, "ufx_unblank error reading 0x2000");
> +
> +	/* return success if display is already unblanked */
> +	if (((dc_sts & 0x00000100) = 0) || ((dc_ctrl & 0x00000100) = 0))
> +		return 0;
> +
> +	/* request the DC to unblank the display */
> +	dc_ctrl &= ~0x00000100;
> +	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
> +	check_warn_return(status, "ufx_unblank error writing 0x2000");
> +
> +	/* return success immediately if we don't have to wait */
> +	if (!wait)
> +		return 0;
> +
> +	for (i = 0; i < 250; i++) {
> +		status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +		check_warn_return(status, "ufx_unblank error reading 0x2004");
> +
> +		if ((dc_sts & 0x00000100) = 0)
> +			return 0;
> +	}
> +
> +	/* timed out waiting for display to unblank */
> +	return -EIO;
> +}
> +
> +/* If display is enabled, then disable it */
> +static int ufx_disable(struct ufx_data *dev, bool wait)
> +{
> +	u32 dc_ctrl, dc_sts;
> +	int i;
> +
> +	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +	check_warn_return(status, "ufx_disable error reading 0x2004");
> +
> +	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
> +	check_warn_return(status, "ufx_disable error reading 0x2000");
> +
> +	/* return success if display is already disabled */
> +	if (((dc_sts & 0x00000001) = 0) || ((dc_ctrl & 0x00000001) = 0))
> +		return 0;
> +
> +	/* request the DC to disable the display */
> +	dc_ctrl &= ~(0x00000001);
> +	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
> +	check_warn_return(status, "ufx_disable error writing 0x2000");
> +
> +	/* return success immediately if we don't have to wait */
> +	if (!wait)
> +		return 0;
> +
> +	for (i = 0; i < 250; i++) {
> +		status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +		check_warn_return(status, "ufx_disable error reading 0x2004");
> +
> +		if ((dc_sts & 0x00000001) = 0)
> +			return 0;
> +	}
> +
> +	/* timed out waiting for display to disable */
> +	return -EIO;
> +}
> +
> +/* If display is disabled, then enable it */
> +static int ufx_enable(struct ufx_data *dev, bool wait)
> +{
> +	u32 dc_ctrl, dc_sts;
> +	int i;
> +
> +	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +	check_warn_return(status, "ufx_enable error reading 0x2004");
> +
> +	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
> +	check_warn_return(status, "ufx_enable error reading 0x2000");
> +
> +	/* return success if display is already enabled */
> +	if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
> +		return 0;
> +
> +	/* request the DC to enable the display */
> +	dc_ctrl |= 0x00000001;
> +	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
> +	check_warn_return(status, "ufx_enable error writing 0x2000");
> +
> +	/* return success immediately if we don't have to wait */
> +	if (!wait)
> +		return 0;
> +
> +	for (i = 0; i < 250; i++) {
> +		status = ufx_reg_read(dev, 0x2004, &dc_sts);
> +		check_warn_return(status, "ufx_enable error reading 0x2004");
> +
> +		if (dc_sts & 0x00000001)
> +			return 0;
> +	}
> +
> +	/* timed out waiting for display to enable */
> +	return -EIO;
> +}
> +
> +static int ufx_config_sys_clk(struct ufx_data *dev)
> +{
> +	int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
> +	check_warn_return(status, "error writing 0x700C");
> +
> +	status = ufx_reg_write(dev, 0x7014, 0x0010024F);
> +	check_warn_return(status, "error writing 0x7014");
> +
> +	status = ufx_reg_write(dev, 0x7010, 0x00000000);
> +	check_warn_return(status, "error writing 0x7010");
> +
> +	status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
> +	check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
> +	msleep(1);
> +
> +	status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
> +	check_warn_return(status, "error clearing output gate in 0x700C");
> +
> +	return 0;
> +}
> +
> +static int ufx_config_ddr2(struct ufx_data *dev)
> +{
> +	int status, i = 0;
> +	u32 tmp;
> +
> +	status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
> +	check_warn_return(status, "error writing 0x0004");
> +
> +	status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
> +	check_warn_return(status, "error writing 0x0008");
> +
> +	status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
> +	check_warn_return(status, "error writing 0x000C");
> +
> +	status = ufx_reg_write(dev, 0x0010, 0x00030814);
> +	check_warn_return(status, "error writing 0x0010");
> +
> +	status = ufx_reg_write(dev, 0x0014, 0x00500019);
> +	check_warn_return(status, "error writing 0x0014");
> +
> +	status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
> +	check_warn_return(status, "error writing 0x0018");
> +
> +	status = ufx_reg_write(dev, 0x001C, 0x02532305);
> +	check_warn_return(status, "error writing 0x001C");
> +
> +	status = ufx_reg_write(dev, 0x0020, 0x0B030905);
> +	check_warn_return(status, "error writing 0x0020");
> +
> +	status = ufx_reg_write(dev, 0x0024, 0x00000827);
> +	check_warn_return(status, "error writing 0x0024");
> +
> +	status = ufx_reg_write(dev, 0x0028, 0x00000000);
> +	check_warn_return(status, "error writing 0x0028");
> +
> +	status = ufx_reg_write(dev, 0x002C, 0x00000042);
> +	check_warn_return(status, "error writing 0x002C");
> +
> +	status = ufx_reg_write(dev, 0x0030, 0x09520000);
> +	check_warn_return(status, "error writing 0x0030");
> +
> +	status = ufx_reg_write(dev, 0x0034, 0x02223314);
> +	check_warn_return(status, "error writing 0x0034");
> +
> +	status = ufx_reg_write(dev, 0x0038, 0x00430043);
> +	check_warn_return(status, "error writing 0x0038");
> +
> +	status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
> +	check_warn_return(status, "error writing 0x003C");
> +
> +	status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
> +	check_warn_return(status, "error writing 0x0040");
> +
> +	status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
> +	check_warn_return(status, "error writing 0x0044");
> +
> +	status = ufx_reg_write(dev, 0x0048, 0x03080406);
> +	check_warn_return(status, "error writing 0x0048");
> +
> +	status = ufx_reg_write(dev, 0x004C, 0x00001000);
> +	check_warn_return(status, "error writing 0x004C");
> +
> +	status = ufx_reg_write(dev, 0x005C, 0x00000007);
> +	check_warn_return(status, "error writing 0x005C");
> +
> +	status = ufx_reg_write(dev, 0x0100, 0x54F00012);
> +	check_warn_return(status, "error writing 0x0100");
> +
> +	status = ufx_reg_write(dev, 0x0104, 0x00004012);
> +	check_warn_return(status, "error writing 0x0104");
> +
> +	status = ufx_reg_write(dev, 0x0118, 0x40404040);
> +	check_warn_return(status, "error writing 0x0118");
> +
> +	status = ufx_reg_write(dev, 0x0000, 0x00000001);
> +	check_warn_return(status, "error writing 0x0000");

These are a lot of magic values above (including ufx_config_sys_clk). If they
have any useful meaning and are not just magic please document what they do to
allow others to understand it.

> +
> +	while (i++ < 500) {
> +		status = ufx_reg_read(dev, 0x0000, &tmp);
> +		check_warn_return(status, "error reading 0x0000");
> +
> +		if (all_bits_set(tmp, 0xC0000000))
> +			return 0;
> +	}
> +
> +	pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
> +	return -ETIMEDOUT;
> +}
> +
> +struct pll_values {
> +	u32 div_r0;
> +	u32 div_f0;
> +	u32 div_q0;
> +	u32 range0;
> +	u32 div_r1;
> +	u32 div_f1;
> +	u32 div_q1;
> +	u32 range1;
> +};
> +
> +static u32 ufx_calc_range(u32 ref_freq)
> +{
> +	if (ref_freq >= 88000000)
> +		return 7;
> +
> +	if (ref_freq >= 54000000)
> +		return 6;
> +
> +	if (ref_freq >= 34000000)
> +		return 5;
> +
> +	if (ref_freq >= 21000000)
> +		return 4;
> +
> +	if (ref_freq >= 13000000)
> +		return 3;
> +
> +	if (ref_freq >= 8000000)
> +		return 2;
> +
> +	return 1;
> +}
> +
> +/* calculates PLL divider settings for a desired target frequency */
> +static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
> +{
> +	const u32 ref_clk = 25000000;
> +	u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
> +	u32 min_error = clk_pixel_pll;
> +
> +	for (div_r0 = 1; div_r0 <= 32; div_r0++) {
> +		u32 ref_freq0 = ref_clk / div_r0;
> +		if (ref_freq0 < 5000000)
> +			break;
> +
> +		if (ref_freq0 > 200000000)
> +			continue;
> +
> +		for (div_f0 = 1; div_f0 <= 256; div_f0++) {
> +			u32 vco_freq0 = ref_freq0 * div_f0;
> +
> +			if (vco_freq0 < 350000000)
> +				continue;
> +
> +			if (vco_freq0 > 700000000)
> +				break;
> +
> +			for (div_q0 = 0; div_q0 < 7; div_q0++) {
> +				u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
> +
> +				if (pllout_freq0 < 5000000)
> +					break;
> +
> +				if (pllout_freq0 > 200000000)
> +					continue;
> +
> +				for (div_r1 = 1; div_r1 <= 32; div_r1++) {
> +					u32 ref_freq1 = pllout_freq0 / div_r1;
> +
> +					if (ref_freq1 < 5000000)
> +						break;
> +
> +					for (div_f1 = 1; div_f1 <= 256; div_f1++) {
> +						u32 vco_freq1 = ref_freq1 * div_f1;
> +
> +						if (vco_freq1 < 350000000)
> +							continue;
> +
> +						if (vco_freq1 > 700000000)
> +							break;
> +
> +						for (div_q1 = 0; div_q1 < 7; div_q1++) {
> +							u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
> +							int error = abs(pllout_freq1 - clk_pixel_pll);
> +
> +							if (pllout_freq1 < 5000000)
> +								break;
> +
> +							if (pllout_freq1 > 700000000)
> +								continue;
> +
> +							if (error < min_error) {
> +								min_error = error;
> +
> +								/* final returned value is equal to calculated value - 1
> +								 * because a value of 0 = divide by 1 */
> +								asic_pll->div_r0 = div_r0 - 1;
> +								asic_pll->div_f0 = div_f0 - 1;
> +								asic_pll->div_q0 = div_q0;
> +								asic_pll->div_r1 = div_r1 - 1;
> +								asic_pll->div_f1 = div_f1 - 1;
> +								asic_pll->div_q1 = div_q1;
> +
> +								asic_pll->range0 = ufx_calc_range(ref_freq0);
> +								asic_pll->range1 = ufx_calc_range(ref_freq1);
> +
> +								if (min_error = 0)
> +									return;

Ugh, 6 nested loops. It's good that you do the calculation of PLL vlaues so
transparent but isn't there a way to do it more efficient than trying all
possible combinations (in theory 2^32 if I calculated correct, probably much
less due to the breaks and continues). At least it would be an improvement, I
think, if you calculated the min and max value for each loop instead of relying
on break and continue.

> +							}
> +						}
> +					}
> +				}
> +			}
> +		}
> +	}
> +}
> +
> +/* sets analog bit PLL configuration values */
> +static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
> +{
> +	struct pll_values asic_pll = {0};
> +	u32 value, clk_pixel, clk_pixel_pll;
> +	int status;
> +
> +	/* convert pixclock (in ps) to frequency (in Hz) */
> +	clk_pixel = PICOS2KHZ(pixclock) * 1000;
> +	pr_info("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
> +
> +	/* clk_pixel = 1/2 clk_pixel_pll */
> +	clk_pixel_pll = clk_pixel * 2;
> +
> +	ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
> +
> +	/* Keep BYPASS and RESET signals asserted until configured */
> +	status = ufx_reg_write(dev, 0x7000, 0x8000000F);
> +	check_warn_return(status, "error writing 0x7000");
> +
> +	value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
> +		(asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
> +	status = ufx_reg_write(dev, 0x7008, value);
> +	check_warn_return(status, "error writing 0x7008");
> +
> +	value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
> +		(asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
> +	status = ufx_reg_write(dev, 0x7004, value);
> +	check_warn_return(status, "error writing 0x7004");
> +
> +	status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
> +	check_warn_return(status,
> +		"error clearing PLL0 bypass bits in 0x7000");
> +	msleep(1);
> +
> +	status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
> +	check_warn_return(status,
> +		"error clearing PLL1 bypass bits in 0x7000");
> +	msleep(1);
> +
> +	status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
> +	check_warn_return(status, "error clearing gate bits in 0x7000");
> +
> +	return 0;
> +}
> +
> +static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
> +{
> +	u32 temp;
> +	u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
> +	u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
> +
> +	int status = ufx_reg_write(dev, 0x8028, 0);
> +	check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
> +
> +	status = ufx_reg_write(dev, 0x8024, 0);
> +	check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
> +
> +	/* shut everything down before changing timing */
> +	status = ufx_blank(dev, true);
> +	check_warn_return(status, "ufx_set_vid_mode error blanking display");
> +
> +	status = ufx_disable(dev, true);
> +	check_warn_return(status, "ufx_set_vid_mode error disabling display");
> +
> +	status = ufx_config_pix_clk(dev, var->pixclock);
> +	check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
> +
> +	status = ufx_reg_write(dev, 0x2000, 0x00000104);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
> +
> +	/* set horizontal timings */
> +	h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
> +	h_active = var->xres;
> +	h_blank_start = var->xres + var->right_margin;
> +	h_blank_end = var->xres + var->right_margin + var->hsync_len;
> +	h_sync_start = var->xres + var->right_margin;
> +	h_sync_end = var->xres + var->right_margin + var->hsync_len;
> +
> +	temp = ((h_total - 1) << 16) | (h_active - 1);
> +	status = ufx_reg_write(dev, 0x2008, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
> +
> +	temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
> +	status = ufx_reg_write(dev, 0x200C, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
> +
> +	temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
> +	status = ufx_reg_write(dev, 0x2010, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
> +
> +	/* set vertical timings */
> +	v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
> +	v_active = var->yres;
> +	v_blank_start = var->yres + var->lower_margin;
> +	v_blank_end = var->yres + var->lower_margin + var->vsync_len;
> +	v_sync_start = var->yres + var->lower_margin;
> +	v_sync_end = var->yres + var->lower_margin + var->vsync_len;
> +
> +	temp = ((v_total - 1) << 16) | (v_active - 1);
> +	status = ufx_reg_write(dev, 0x2014, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
> +
> +	temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
> +	status = ufx_reg_write(dev, 0x2018, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
> +
> +	temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
> +	status = ufx_reg_write(dev, 0x201C, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
> +
> +	status = ufx_reg_write(dev, 0x2020, 0x00000000);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
> +
> +	status = ufx_reg_write(dev, 0x2024, 0x00000000);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
> +
> +	/* Set the frame length register (#pix * 2 bytes/pixel) */
> +	temp = var->xres * var->yres * 2;
> +	temp = (temp + 7) & (~0x7);
> +	status = ufx_reg_write(dev, 0x2028, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
> +
> +	/* enable desired output interface & disable others */
> +	status = ufx_reg_write(dev, 0x2040, 0);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
> +
> +	status = ufx_reg_write(dev, 0x2044, 0);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
> +
> +	status = ufx_reg_write(dev, 0x2048, 0);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
> +
> +	/* set the sync polarities & enable bit */
> +	temp = 0x00000001;
> +	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
> +		temp |= 0x00000010;
> +
> +	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
> +		temp |= 0x00000008;
> +
> +	status = ufx_reg_write(dev, 0x2040, temp);
> +	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
> +
> +	/* start everything back up */
> +	status = ufx_enable(dev, true);
> +	check_warn_return(status, "ufx_set_vid_mode error enabling display");
> +
> +	/* Unblank the display */
> +	status = ufx_unblank(dev, true);
> +	check_warn_return(status, "ufx_set_vid_mode error unblanking display");
> +
> +	/* enable RGB pad */
> +	status = ufx_reg_write(dev, 0x8028, 0x00000003);
> +	check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
> +
> +	/* enable VDAC */
> +	status = ufx_reg_write(dev, 0x8024, 0x00000007);
> +	check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
> +
> +	return 0;
> +}
> +
> +static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
> +{
> +	unsigned long start = vma->vm_start;
> +	unsigned long size = vma->vm_end - vma->vm_start;
> +	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
> +	unsigned long page, pos;
> +
> +	if (offset + size > info->fix.smem_len)
> +		return -EINVAL;
> +
> +	pos = (unsigned long)info->fix.smem_start + offset;
> +
> +	pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
> +		  pos, size);
> +
> +	while (size > 0) {
> +		page = vmalloc_to_pfn((void *)pos);
> +		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
> +			return -EAGAIN;
> +
> +		start += PAGE_SIZE;
> +		pos += PAGE_SIZE;
> +		if (size > PAGE_SIZE)
> +			size -= PAGE_SIZE;
> +		else
> +			size = 0;
> +	}
> +
> +	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
> +	return 0;
> +}
> +
> +static char *ufx_raw_rect(struct ufx_data *dev, char *cmd, int x, int y,
> +	int width, int height)
> +{
> +	size_t packed_line_len = ALIGN((width * 2), 4);
> +	size_t packed_rect_len = packed_line_len * height;
> +	int i;
> +
> +	BUG_ON(!dev);
> +	BUG_ON(!dev->info);
> +
> +	/* command word */
> +	*cmd = 0x01;
> +	cmd += 4;

That looks strange. You dereference a char* (1 byte) but add 4? So the 3 bytes
after the one written contain old data, maybe this is intentional, just asking.

> +
> +	/* length word */
> +	*((u32 *)cmd) = cpu_to_le32(packed_rect_len + 16);
> +	cmd += 4;
> +
> +	*((u16 *)cmd) = cpu_to_le16(x);
> +	cmd += 2;
> +
> +	*((u16 *)cmd) = cpu_to_le16(y);
> +	cmd += 2;
> +
> +	*((u16 *)cmd) = cpu_to_le16(width);
> +	cmd += 2;
> +
> +	*((u16 *)cmd) = cpu_to_le16(height);
> +	cmd += 2;
> +
> +	/* frame base address */
> +	*((u32 *)cmd) = cpu_to_le32(0 & 0xffffff80);
> +	cmd += 4;
> +
> +	/* color mode and horizontal resolution */
> +	*((u16 *)cmd) = cpu_to_le16(0x4000 | dev->info->var.xres);
> +	cmd += 2;
> +
> +	/* vertical resolution */
> +	*((u16 *)cmd) = cpu_to_le16(dev->info->var.yres);
> +	cmd += 2;

Isn't there a way to avoid this amount of casts and pointer increments?
I guess it could help if you used u16* as base type and then
*((u32*)&cmd[2]) cmd[4] cmd[5] 
> +
> +	/* packed data */
> +	for (i = y; i < y + height ; i++) {
> +		const int line_offset = dev->info->fix.line_length * i;
> +		const int byte_offset = line_offset + (x * BPP);
> +		memcpy(cmd, (char *) dev->info->fix.smem_start + byte_offset, (width * BPP));
> +
> +		/* include line padding */
> +		cmd += packed_line_len;
> +	}
> +
> +	return cmd;
> +}
> +
> +int ufx_handle_damage(struct ufx_data *dev, int x, int y,
> +	int width, int height)
> +{
> +	size_t packed_line_len = ALIGN((width * 2), 4);
> +	int len, status, urb_lines, start_line = 0;
> +	char *cmd;
> +
> +	if ((width <= 0) || (height <= 0) ||
> +	    (x + width > dev->info->var.xres) ||
> +	    (y + height > dev->info->var.yres))
> +		return -EINVAL;
> +
> +	if (!atomic_read(&dev->usb_active))
> +		return 0;
> +
> +	while (start_line < height) {
> +		struct urb *urb = ufx_get_urb(dev);
> +		if (!urb) {
> +			pr_warn("ufx_handle_damage unable to get urb");
> +			return 0;
> +		}
> +
> +		/* assume we have enough space to transfer at least one line */
> +		BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
> +
> +		/* calculate the maximum number of lines we could fit in */
> +		urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
> +
> +		/* but we might not need this many */
> +		urb_lines = min(urb_lines, (height - start_line));
> +
> +		memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
> +
> +		cmd = ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
> +
> +		len = cmd - (char *) urb->transfer_buffer;
> +		BUG_ON(len != (24 + (packed_line_len * urb_lines)));
> +
> +		status = ufx_submit_urb(dev, urb, len);
> +		check_warn_return(status, "Error submitting URB");
> +
> +		start_line += urb_lines;
> +	}
> +
> +	return 0;
> +}
> +
> +/* Path triggered by usermode clients who write to filesystem
> + * e.g. cat filename > /dev/fb1
> + * Not used by X Windows or text-mode console. But useful for testing.
> + * Slow because of extra copy and we must assume all pixels dirty. */
> +static ssize_t ufx_ops_write(struct fb_info *info, const char __user *buf,
> +			  size_t count, loff_t *ppos)
> +{
> +	ssize_t result;
> +	struct ufx_data *dev = info->par;
> +	u32 offset = (u32) *ppos;
> +
> +	result = fb_sys_write(info, buf, count, ppos);
> +
> +	if (result > 0) {
> +		int start = max((int)(offset / info->fix.line_length) - 1, 0);
> +		int lines = min((u32)((result / info->fix.line_length) + 1),
> +				(u32)info->var.yres);
> +
> +		ufx_handle_damage(dev, 0, start, info->var.xres, lines);
> +	}
> +
> +	return result;
> +}
> +
> +static void ufx_ops_copyarea(struct fb_info *info,
> +				const struct fb_copyarea *area)
> +{
> +
> +	struct ufx_data *dev = info->par;
> +
> +	sys_copyarea(info, area);
> +
> +	ufx_handle_damage(dev, area->dx, area->dy,
> +			area->width, area->height);
> +}
> +
> +static void ufx_ops_imageblit(struct fb_info *info,
> +				const struct fb_image *image)
> +{
> +	struct ufx_data *dev = info->par;
> +
> +	sys_imageblit(info, image);
> +
> +	ufx_handle_damage(dev, image->dx, image->dy,
> +			image->width, image->height);
> +}
> +
> +static void ufx_ops_fillrect(struct fb_info *info,
> +			  const struct fb_fillrect *rect)
> +{
> +	struct ufx_data *dev = info->par;
> +
> +	sys_fillrect(info, rect);
> +
> +	ufx_handle_damage(dev, rect->dx, rect->dy, rect->width,
> +			      rect->height);
> +}
> +
> +/* NOTE: fb_defio.c is holding info->fbdefio.mutex
> + *   Touching ANY framebuffer memory that triggers a page fault
> + *   in fb_defio will cause a deadlock, when it also tries to
> + *   grab the same mutex. */
> +static void ufx_dpy_deferred_io(struct fb_info *info,
> +				struct list_head *pagelist)
> +{
> +	struct page *cur;
> +	struct fb_deferred_io *fbdefio = info->fbdefio;
> +	struct ufx_data *dev = info->par;
> +
> +	if (!fb_defio)
> +		return;
> +
> +	if (!atomic_read(&dev->usb_active))
> +		return;
> +
> +	/* walk the written page list and render each to device */
> +	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
> +		/* create a rectangle of full screen width that encloses the
> +		 * entire dirty framebuffer page */
> +		const int x = 0;
> +		const int width = dev->info->var.xres;
> +		const int y = (cur->index << PAGE_SHIFT) / (width * 2);
> +		int height = (PAGE_SIZE / (width * 2)) + 1;
> +		height = min(height, (int)(dev->info->var.yres - y));
> +
> +		BUG_ON(y >= dev->info->var.yres);
> +		BUG_ON((y + height) > dev->info->var.yres);
> +
> +		ufx_handle_damage(dev, x, y, width, height);
> +	}
> +}
> +
> +static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
> +			 unsigned long arg)
> +{
> +	struct ufx_data *dev = info->par;
> +	struct dloarea *area = NULL;
> +
> +	if (!atomic_read(&dev->usb_active))
> +		return 0;
> +
> +	/* TODO: Update X server to get this from sysfs instead */
> +	if (cmd = UFX_IOCTL_RETURN_EDID) {
> +		char *edid = (char *)arg;
> +		if (copy_to_user(edid, dev->edid, dev->edid_size))
> +			return -EFAULT;
> +		return 0;
> +	}
> +
> +	/* TODO: Help propose a standard fb.h ioctl to report mmap damage */
> +	if (cmd = UFX_IOCTL_REPORT_DAMAGE) {
> +		/* If we have a damage-aware client, turn fb_defio "off"
> +		 * To avoid perf imact of unecessary page fault handling.
> +		 * Done by resetting the delay for this fb_info to a very
> +		 * long period. Pages will become writable and stay that way.
> +		 * Reset to normal value when all clients have closed this fb.
> +		 */

I think you should reset when fb_set_par is called as probably all users do when
they are activated. Otherwise things get broken if any client that supports this
extension and any other is run at the same time so it won't be resetted if you
switch between them.

> +		if (info->fbdefio)
> +			info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
> +
> +		area = (struct dloarea *)arg;
> +
> +		if (area->x < 0)
> +			area->x = 0;
> +
> +		if (area->x > info->var.xres)
> +			area->x = info->var.xres;
> +
> +		if (area->y < 0)
> +			area->y = 0;
> +
> +		if (area->y > info->var.yres)
> +			area->y = info->var.yres;
> +
> +		ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
> +	}
> +
> +	return 0;
> +}
> +
> +/* taken from vesafb */
> +static int
> +ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
> +	       unsigned blue, unsigned transp, struct fb_info *info)
> +{
> +	int err = 0;
> +
> +	if (regno >= info->cmap.len)
> +		return 1;
> +
> +	if (regno < 16) {
> +		if (info->var.red.offset = 10) {
> +			/* 1:5:5:5 */
> +			((u32 *) (info->pseudo_palette))[regno] > +			    ((red & 0xf800) >> 1) |
> +			    ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
> +		} else {
> +			/* 0:5:6:5 */
> +			((u32 *) (info->pseudo_palette))[regno] > +			    ((red & 0xf800)) |
> +			    ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
> +		}
> +	}
> +
> +	return err;
> +}
> +
> +/* It's common for several clients to have framebuffer open simultaneously.
> + * e.g. both fbcon and X. Makes things interesting.
> + * Assumes caller is holding info->lock (for open and release at least) */
> +static int ufx_ops_open(struct fb_info *info, int user)
> +{
> +	struct ufx_data *dev = info->par;
> +
> +	/* fbcon aggressively connects to first framebuffer it finds,
> +	 * preventing other clients (X) from working properly. Usually
> +	 * not what the user wants. Fail by default with option to enable. */

Not really, otherwise we'd see this problem more often. If an application
changed the VT to KD_GRAPHICS it works well. I assume that if this problem
occured X was stared with --shared-vt (or what the option was called) which does
not do this correctly.

> +	if ((user = 0) & (!console))

You want a && here and you can also drop the inner ().

> +		return -EBUSY;
> +
> +	/* If the USB device is gone, we don't accept new opens */
> +	if (dev->virtualized)
> +		return -ENODEV;
> +
> +	dev->fb_count++;
> +
> +	kref_get(&dev->kref);
> +
> +	if (fb_defio && (info->fbdefio = NULL)) {
> +		/* enable defio at last moment if not disabled by client */
> +
> +		struct fb_deferred_io *fbdefio;
> +
> +		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
> +
> +		if (fbdefio) {
> +			fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
> +			fbdefio->deferred_io = ufx_dpy_deferred_io;
> +		}
> +
> +		info->fbdefio = fbdefio;
> +		fb_deferred_io_init(info);
> +	}
> +
> +	pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d",
> +		info->node, user, info, dev->fb_count);
> +
> +	return 0;
> +}
> +
> +/*
> + * Called when all client interfaces to start transactions have been disabled,
> + * and all references to our device instance (ufx_data) are released.
> + * Every transaction must have a reference, so we know are fully spun down
> + */
> +static void ufx_free(struct kref *kref)
> +{
> +	struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
> +
> +	/* this function will wait for all in-flight urbs to complete */
> +	if (dev->urbs.count > 0)
> +		ufx_free_urb_list(dev);
> +
> +	pr_warn("freeing ufx_data %p", dev);
> +
> +	kfree(dev);
> +}
> +
> +static void ufx_release_urb_work(struct work_struct *work)
> +{
> +	struct urb_node *unode = container_of(work, struct urb_node,
> +					      release_urb_work.work);
> +
> +	up(&unode->dev->urbs.limit_sem);
> +}
> +
> +static void ufx_free_framebuffer_work(struct work_struct *work)
> +{
> +	struct ufx_data *dev = container_of(work, struct ufx_data,
> +					    free_framebuffer_work.work);
> +	struct fb_info *info = dev->info;
> +	int node = info->node;
> +
> +	unregister_framebuffer(info);
> +
> +	if (info->cmap.len != 0)
> +		fb_dealloc_cmap(&info->cmap);
> +	if (info->monspecs.modedb)
> +		fb_destroy_modedb(info->monspecs.modedb);
> +	if (info->screen_base)
> +		vfree(info->screen_base);
> +
> +	fb_destroy_modelist(&info->modelist);
> +
> +	dev->info = 0;
> +
> +	/* Assume info structure is freed after this point */
> +	framebuffer_release(info);
> +
> +	pr_warn("fb_info for /dev/fb%d has been freed", node);
> +
> +	/* ref taken in probe() as part of registering framebfufer */
> +	kref_put(&dev->kref, ufx_free);
> +}
> +
> +/*
> + * Assumes caller is holding info->lock mutex (for open and release at least)
> + */
> +static int ufx_ops_release(struct fb_info *info, int user)
> +{
> +	struct ufx_data *dev = info->par;
> +
> +	dev->fb_count--;
> +
> +	/* We can't free fb_info here - fbmem will touch it when we return */
> +	if (dev->virtualized && (dev->fb_count = 0))
> +		schedule_delayed_work(&dev->free_framebuffer_work, HZ);
> +
> +	if ((dev->fb_count = 0) && (info->fbdefio)) {
> +		fb_deferred_io_cleanup(info);
> +		kfree(info->fbdefio);
> +		info->fbdefio = NULL;
> +		info->fbops->fb_mmap = ufx_ops_mmap;
> +	}
> +
> +	pr_warn("released /dev/fb%d user=%d count=%d",
> +		  info->node, user, dev->fb_count);
> +
> +	kref_put(&dev->kref, ufx_free);
> +
> +	return 0;
> +}
> +
> +/* Check whether a video mode is supported by the chip
> + * We start from monitor's modes, so don't need to filter that here */
> +static int ufx_is_valid_mode(struct fb_videomode *mode,
> +		struct fb_info *info)
> +{
> +	if ((mode->xres * mode->yres) > (2048 * 1152)) {
> +		pr_warn("%dx%d too many pixels",
> +		       mode->xres, mode->yres);
> +		return 0;
> +	}
> +
> +	if (mode->pixclock < 5000) {
> +		pr_warn("%dx%d %dps pixel clock too fast",
> +		       mode->xres, mode->yres, mode->pixclock);
> +		return 0;
> +	}
> +
> +	pr_info("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
> +		mode->pixclock, (1000000 / mode->pixclock));
> +	return 1;
> +}
> +
> +static void ufx_var_color_format(struct fb_var_screeninfo *var)
> +{
> +	const struct fb_bitfield red = { 11, 5, 0 };
> +	const struct fb_bitfield green = { 5, 6, 0 };
> +	const struct fb_bitfield blue = { 0, 5, 0 };
> +
> +	var->bits_per_pixel = 16;
> +	var->red = red;
> +	var->green = green;
> +	var->blue = blue;
> +}
> +
> +static int ufx_ops_check_var(struct fb_var_screeninfo *var,
> +				struct fb_info *info)
> +{
> +	struct fb_videomode mode;
> +
> +	/* TODO: support dynamically changing framebuffer size */
> +	if ((var->xres * var->yres * 2) > info->fix.smem_len)
> +		return -EINVAL;
> +
> +	/* set device-specific elements of var unrelated to mode */
> +	ufx_var_color_format(var);
> +
> +	fb_var_to_videomode(&mode, var);
> +
> +	if (!ufx_is_valid_mode(&mode, info))
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
> +static int ufx_ops_set_par(struct fb_info *info)
> +{
> +	struct ufx_data *dev = info->par;
> +	int result;
> +	u16 *pix_framebuffer;
> +	int i;
> +
> +	pr_notice("set_par mode %dx%d", info->var.xres, info->var.yres);
> +	result = ufx_set_vid_mode(dev, &info->var);
> +
> +	if ((result = 0) && (dev->fb_count = 0)) {
> +		/* paint greenscreen */
> +		pix_framebuffer = (u16 *) info->screen_base;
> +		for (i = 0; i < info->fix.smem_len / 2; i++)
> +			pix_framebuffer[i] = 0x37e6;
> +
> +		ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
> +	}
> +
> +	return result;
> +}
> +
> +/* In order to come back from full DPMS off, we need to set the mode again */
> +static int ufx_ops_blank(int blank_mode, struct fb_info *info)
> +{
> +	struct ufx_data *dev = info->par;
> +	ufx_set_vid_mode(dev, &info->var);
> +	return 0;
> +}
> +
> +static struct fb_ops ufx_ops = {
> +	.owner = THIS_MODULE,
> +	.fb_read = fb_sys_read,
> +	.fb_write = ufx_ops_write,
> +	.fb_setcolreg = ufx_ops_setcolreg,
> +	.fb_fillrect = ufx_ops_fillrect,
> +	.fb_copyarea = ufx_ops_copyarea,
> +	.fb_imageblit = ufx_ops_imageblit,
> +	.fb_mmap = ufx_ops_mmap,
> +	.fb_ioctl = ufx_ops_ioctl,
> +	.fb_open = ufx_ops_open,
> +	.fb_release = ufx_ops_release,
> +	.fb_blank = ufx_ops_blank,
> +	.fb_check_var = ufx_ops_check_var,
> +	.fb_set_par = ufx_ops_set_par,
> +};
> +
> +/* Assumes &info->lock held by caller
> + * Assumes no active clients have framebuffer open */
> +static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
> +{
> +	int retval = -ENOMEM;
> +	int old_len = info->fix.smem_len;
> +	int new_len;
> +	unsigned char *old_fb = info->screen_base;
> +	unsigned char *new_fb;
> +
> +	pr_warn("Reallocating framebuffer. Addresses will change!");
> +
> +	new_len = info->fix.line_length * info->var.yres;
> +
> +	if (PAGE_ALIGN(new_len) > old_len) {
> +		/*
> +		 * Alloc system memory for virtual framebuffer
> +		 */
> +		new_fb = vmalloc(new_len);
> +		if (!new_fb) {
> +			pr_err("Virtual framebuffer alloc failed");
> +			goto error;
> +		}
> +
> +		if (info->screen_base) {
> +			memcpy(new_fb, old_fb, old_len);
> +			vfree(info->screen_base);
> +		}
> +
> +		info->screen_base = new_fb;
> +		info->fix.smem_len = PAGE_ALIGN(new_len);
> +		info->fix.smem_start = (unsigned long) new_fb;
> +		info->flags = smscufx_info_flags;
> +	}
> +
> +	retval = 0;
> +
> +error:
> +	return retval;
> +}
> +
> +/* sets up I2C Controller for 100 Kbps, std. speed, 7-bit addr, master,
> + * restart enabled, but no start byte, enable controller */
> +static int ufx_i2c_init(struct ufx_data *dev)
> +{
> +	u32 tmp;
> +
> +	/* disable the controller before it can be reprogrammed */
> +	int status = ufx_reg_write(dev, 0x106C, 0x00);
> +	check_warn_return(status, "failed to disable I2C");
> +
> +	/* Setup the clock count registers
> +	 * (12+1) = 13 clks @ 2.5 MHz = 5.2 uS */
> +	status = ufx_reg_write(dev, 0x1018, 12);
> +	check_warn_return(status, "error writing 0x1018");
> +
> +	/* (6+8) = 14 clks @ 2.5 MHz = 5.6 uS */
> +	status = ufx_reg_write(dev, 0x1014, 6);
> +	check_warn_return(status, "error writing 0x1014");
> +
> +	status = ufx_reg_read(dev, 0x1000, &tmp);
> +	check_warn_return(status, "error reading 0x1000");
> +
> +	/* set speed to std mode */
> +	tmp &= ~(0x06);
> +	tmp |= 0x02;
> +
> +	/* 7-bit (not 10-bit) addressing */
> +	tmp &= ~(0x10);
> +
> +	/* enable restart conditions and master mode */
> +	tmp |= 0x21;
> +
> +	status = ufx_reg_write(dev, 0x1000, tmp);
> +	check_warn_return(status, "error writing 0x1000");
> +
> +	/* Set normal tx using target address 0 */
> +	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
> +	check_warn_return(status, "error setting TX mode bits in 0x1004");
> +
> +	/* Enable the controller */
> +	status = ufx_reg_write(dev, 0x106C, 0x01);
> +	check_warn_return(status, "failed to enable I2C");
> +
> +	return 0;
> +}
> +
> +/* sets the I2C port mux and target address */
> +static int ufx_i2c_configure(struct ufx_data *dev)
> +{
> +	int status = ufx_reg_write(dev, 0x106C, 0x00);
> +	check_warn_return(status, "failed to disable I2C");
> +
> +	status = ufx_reg_write(dev, 0x3010, 0x00000000);
> +	check_warn_return(status, "failed to write 0x3010");
> +
> +	/* A0h is std for any EDID, right shifted by one */
> +	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF,	(0xA0 >> 1));
> +	check_warn_return(status, "failed to set TAR bits in 0x1004");
> +
> +	status = ufx_reg_write(dev, 0x106C, 0x01);
> +	check_warn_return(status, "failed to enable I2C");
> +
> +	return 0;
> +}
> +
> +/* wait for BUSY to clear, with a timeout of 50ms with 10ms sleeps. if no
> + * monitor is connected, there is no error except for timeout */
> +static int ufx_i2c_wait_busy(struct ufx_data *dev)
> +{
> +	u32 tmp;
> +	int i, status;
> +
> +	for (i = 0; i < 15; i++) {
> +		status = ufx_reg_read(dev, 0x1100, &tmp);
> +		check_warn_return(status, "0x1100 read failed");
> +
> +		/* if BUSY is clear, check for error */
> +		if ((tmp & 0x80000000) = 0) {
> +			if (tmp & 0x20000000) {
> +				pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
> +				return -EIO;
> +			}
> +
> +			return 0;
> +		}
> +
> +		/* perform the first 10 retries without delay */
> +		if (i >= 10)
> +			msleep(10);
> +	}
> +
> +	pr_warn("I2C access timed out, resetting I2C hardware");
> +	status =  ufx_reg_write(dev, 0x1100, 0x40000000);
> +	check_warn_return(status, "0x1100 write failed");
> +
> +	return -ETIMEDOUT;
> +}
> +
> +/* reads a 128-byte EDID block from the currently selected port and TAR */
> +static int ufx_read_edid(struct ufx_data *dev, char *edid, int edid_len)
> +{
> +	int i, j, status;
> +	u32 *edid_u32 = (u32 *)edid;
> +
> +	BUG_ON(edid_len != EDID_LENGTH);
> +
> +	status = ufx_i2c_configure(dev);
> +	if (status < 0) {
> +		pr_err("ufx_i2c_configure failed");
> +		return status;
> +	}
> +
> +	memset(edid, 0xff, EDID_LENGTH);
> +
> +	/* Read the 128-byte EDID as 2 bursts of 64 bytes */
> +	for (i = 0; i < 2; i++) {
> +		u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
> +		status = ufx_reg_write(dev, 0x1100, temp);
> +		check_warn_return(status, "Failed to write 0x1100");
> +
> +		temp |= 0x80000000;
> +		status = ufx_reg_write(dev, 0x1100, temp);
> +		check_warn_return(status, "Failed to write 0x1100");
> +
> +		status = ufx_i2c_wait_busy(dev);
> +		check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
> +
> +		for (j = 0; j < 16; j++) {
> +			u32 data_reg_addr = 0x1110 + (j * 4);
> +			status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
> +			check_warn_return(status, "Error reading i2c data");
> +		}
> +	}
> +
> +	/* all FF's in the first 16 bytes indicates nothing is connected */
> +	for (i = 0; i < 16; i++) {
> +		if (edid[i] != 0xFF) {
> +			pr_info("edid data read succesfully");
> +			return EDID_LENGTH;
> +		}
> +	}
> +
> +	pr_warn("edid data contains all 0xff");
> +	return -ETIMEDOUT;
> +}
> +
> +/* 1) use sw default
> + * 2) Parse into various fb_info structs
> + * 3) Allocate virtual framebuffer memory to back highest res mode
> + *
> + * Parses EDID into three places used by various parts of fbdev:
> + * fb_var_screeninfo contains the timing of the monitor's preferred mode
> + * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
> + * fb_info.modelist is a linked list of all monitor & VESA modes which work
> + *
> + * If EDID is not readable/valid, then modelist is all VESA modes,
> + * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
> + * Returns 0 if successful */
> +static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
> +	char *default_edid, size_t default_edid_size)
> +{
> +	const struct fb_videomode *default_vmode = NULL;
> +	char *edid;
> +	int i, result = 0, tries = 3;
> +
> +	if (info->dev) /* only use mutex if info has been registered */
> +		mutex_lock(&info->lock);
> +
> +	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
> +	if (!edid) {
> +		result = -ENOMEM;
> +		goto error;
> +	}
> +
> +	fb_destroy_modelist(&info->modelist);
> +	memset(&info->monspecs, 0, sizeof(info->monspecs));
> +
> +	/* Try to (re)read EDID from hardware first
> +	 * EDID data may return, but not parse as valid
> +	 * Try again a few times, in case of e.g. analog cable noise */
> +	while (tries--) {
> +		i = ufx_read_edid(dev, edid, EDID_LENGTH);
> +
> +		if (i >= EDID_LENGTH)
> +			fb_edid_to_monspecs(edid, &info->monspecs);
> +
> +		if (info->monspecs.modedb_len > 0) {
> +			dev->edid = edid;
> +			dev->edid_size = i;
> +			break;
> +		}
> +	}
> +
> +	/* If that fails, use a previously returned EDID if available */
> +	if (info->monspecs.modedb_len = 0) {
> +
> +		pr_err("Unable to get valid EDID from device/display\n");
> +
> +		if (dev->edid) {
> +			fb_edid_to_monspecs(dev->edid, &info->monspecs);
> +			if (info->monspecs.modedb_len > 0)
> +				pr_err("Using previously queried EDID\n");
> +		}
> +	}
> +
> +	/* If that fails, use the default EDID we were handed */
> +	if (info->monspecs.modedb_len = 0) {
> +		if (default_edid_size >= EDID_LENGTH) {
> +			fb_edid_to_monspecs(default_edid, &info->monspecs);
> +			if (info->monspecs.modedb_len > 0) {
> +				memcpy(edid, default_edid, default_edid_size);
> +				dev->edid = edid;
> +				dev->edid_size = default_edid_size;
> +				pr_err("Using default/backup EDID\n");
> +			}
> +		}
> +	}
> +
> +	/* If we've got modes, let's pick a best default mode */
> +	if (info->monspecs.modedb_len > 0) {
> +
> +		for (i = 0; i < info->monspecs.modedb_len; i++) {
> +			if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
> +				fb_add_videomode(&info->monspecs.modedb[i],
> +					&info->modelist);
> +			else /* if we've removed top/best mode */
> +				info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
> +		}
> +
> +		default_vmode = fb_find_best_display(&info->monspecs,
> +						     &info->modelist);
> +	}
> +
> +	/* If everything else has failed, fall back to safe default mode */
> +	if (default_vmode = NULL) {
> +
> +		struct fb_videomode fb_vmode = {0};
> +
> +		/* Add the standard VESA modes to our modelist
> +		 * Since we don't have EDID, there may be modes that
> +		 * overspec monitor and/or are incorrect aspect ratio, etc.
> +		 * But at least the user has a chance to choose
> +		 */
> +		for (i = 0; i < VESA_MODEDB_SIZE; i++) {
> +			if (ufx_is_valid_mode((struct fb_videomode *)
> +						&vesa_modes[i], info))
> +				fb_add_videomode(&vesa_modes[i],
> +						 &info->modelist);
> +		}
> +
> +		/* default to resolution safe for projectors
> +		 * (since they are most common case without EDID)
> +		 */
> +		fb_vmode.xres = 800;
> +		fb_vmode.yres = 600;
> +		fb_vmode.refresh = 60;
> +		default_vmode = fb_find_nearest_mode(&fb_vmode,
> +						     &info->modelist);
> +	}
> +
> +	/* If we have good mode and no active clients */
> +	if ((default_vmode != NULL) && (dev->fb_count = 0)) {
> +
> +		fb_videomode_to_var(&info->var, default_vmode);
> +		ufx_var_color_format(&info->var);
> +
> +		/* with mode size info, we can now alloc our framebuffer */
> +		memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
> +		info->fix.line_length = info->var.xres *
> +			(info->var.bits_per_pixel / 8);
> +
> +		result = ufx_realloc_framebuffer(dev, info);
> +
> +	} else
> +		result = -EINVAL;
> +
> +error:
> +	if (edid && (dev->edid != edid))
> +		kfree(edid);
> +
> +	if (info->dev)
> +		mutex_unlock(&info->lock);
> +
> +	return result;
> +}
> +
> +static int ufx_usb_probe(struct usb_interface *interface,
> +			const struct usb_device_id *id)
> +{
> +	struct usb_device *usbdev;
> +	struct ufx_data *dev = 0;
> +	struct fb_info *info = 0;
> +	int retval = -ENOMEM;
> +	u32 id_rev, fpga_rev;
> +
> +	/* usb initialization */
> +	usbdev = interface_to_usbdev(interface);
> +
> +	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> +	if (dev = NULL) {
> +		err("ufx_usb_probe: failed alloc of dev struct\n");
> +		goto error;
> +	}
> +
> +	/* we need to wait for both usb and fbdev to spin down on disconnect */
> +	kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
> +	kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
> +
> +	dev->udev = usbdev;
> +	dev->gdev = &usbdev->dev; /* our generic struct device * */
> +	usb_set_intfdata(interface, dev);
> +
> +	pr_info("%s %s - serial #%s\n",
> +		usbdev->manufacturer, usbdev->product, usbdev->serial);
> +	pr_info("vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
> +		usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
> +		usbdev->descriptor.bcdDevice, dev);
> +	pr_info("console enable=%d\n", console);
> +	pr_info("fb_defio enable=%d\n", fb_defio);
> +
> +	if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
> +		retval = -ENOMEM;
> +		pr_err("ufx_alloc_urb_list failed\n");
> +		goto error;
> +	}
> +
> +	/* We don't register a new USB class. Our client interface is fbdev */
> +
> +	/* allocates framebuffer driver structure, not framebuffer memory */
> +	info = framebuffer_alloc(0, &usbdev->dev);
> +	if (!info) {
> +		retval = -ENOMEM;
> +		pr_err("framebuffer_alloc failed\n");
> +		goto error;
> +	}
> +
> +	dev->info = info;
> +	info->par = dev;
> +	info->pseudo_palette = dev->pseudo_palette;
> +	info->fbops = &ufx_ops;
> +
> +	retval = fb_alloc_cmap(&info->cmap, 256, 0);
> +	if (retval < 0) {
> +		pr_err("fb_alloc_cmap failed %x\n", retval);
> +		goto error;
> +	}
> +
> +	INIT_DELAYED_WORK(&dev->free_framebuffer_work,
> +			  ufx_free_framebuffer_work);
> +
> +	INIT_LIST_HEAD(&info->modelist);
> +
> +	retval = ufx_reg_read(dev, 0x3000, &id_rev);
> +	check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
> +	pr_info("ID_REV register value 0x%08x", id_rev);
> +
> +	retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
> +	check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
> +	pr_info("FPGA_REV register value 0x%08x", fpga_rev);
> +
> +	pr_info("resetting device");
> +	retval = ufx_lite_reset(dev);
> +	check_warn_goto_error(retval, "error %d resetting device", retval);
> +
> +	pr_info("configuring system clock");
> +	retval = ufx_config_sys_clk(dev);
> +	check_warn_goto_error(retval, "error %d configuring system clock", retval);
> +
> +	pr_info("configuring DDR2 controller");
> +	retval = ufx_config_ddr2(dev);
> +	check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
> +
> +	pr_info("configuring I2C controller");
> +	retval = ufx_i2c_init(dev);
> +	check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
> +
> +	pr_info("selecting display mode");
> +	retval = ufx_setup_modes(dev, info, NULL, 0);
> +	check_warn_goto_error(retval, "unable to find common mode for display and adapter");
> +
> +	retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
> +	check_warn_goto_error(retval, "error %d enabling graphics engine", retval);
> +
> +	/* ready to begin using device */
> +	atomic_set(&dev->usb_active, 1);
> +
> +	pr_info("checking var");
> +	retval = ufx_ops_check_var(&info->var, info);
> +	check_warn_goto_error(retval, "error %d ufx_ops_check_var", retval);
> +
> +	pr_info("setting par");
> +	retval = ufx_ops_set_par(info);
> +	check_warn_goto_error(retval, "error %d ufx_ops_set_par", retval);
> +
> +	pr_info("registering framebuffer");
> +	retval = register_framebuffer(info);
> +	check_warn_goto_error(retval, "error %d register_framebuffer", retval);
> +
> +	pr_info("SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
> +		" Using %dK framebuffer memory\n", info->node,
> +		info->var.xres, info->var.yres, info->fix.smem_len >> 10);
> +
> +	return 0;
> +
> +error:
> +	if (dev) {
> +		if (info) {
> +			if (info->cmap.len != 0)
> +				fb_dealloc_cmap(&info->cmap);
> +			if (info->monspecs.modedb)
> +				fb_destroy_modedb(info->monspecs.modedb);
> +			if (info->screen_base)
> +				vfree(info->screen_base);
> +
> +			fb_destroy_modelist(&info->modelist);
> +
> +			framebuffer_release(info);
> +		}
> +
> +		kref_put(&dev->kref, ufx_free); /* ref for framebuffer */
> +		kref_put(&dev->kref, ufx_free); /* last ref from kref_init */
> +
> +		/* dev has been deallocated. Do not dereference */
> +	}
> +
> +	return retval;
> +}
> +
> +static void ufx_usb_disconnect(struct usb_interface *interface)
> +{
> +	struct ufx_data *dev;
> +	struct fb_info *info;
> +
> +	dev = usb_get_intfdata(interface);
> +	info = dev->info;
> +
> +	pr_info("USB disconnect starting\n");
> +
> +	/* we virtualize until all fb clients release. Then we free */
> +	dev->virtualized = true;
> +
> +	/* When non-active we'll update virtual framebuffer, but no new urbs */
> +	atomic_set(&dev->usb_active, 0);
> +
> +	usb_set_intfdata(interface, NULL);
> +
> +	/* if clients still have us open, will be freed on last close */
> +	if (dev->fb_count = 0)
> +		schedule_delayed_work(&dev->free_framebuffer_work, 0);
> +
> +	/* release reference taken by kref_init in probe() */
> +	kref_put(&dev->kref, ufx_free);
> +
> +	/* consider ufx_data freed */
> +}
> +
> +static struct usb_driver ufx_driver = {
> +	.name = "smscufx",
> +	.probe = ufx_usb_probe,
> +	.disconnect = ufx_usb_disconnect,
> +	.id_table = id_table,
> +};
> +
> +static int __init ufx_module_init(void)
> +{
> +	int res;
> +
> +	res = usb_register(&ufx_driver);
> +	if (res)
> +		err("usb_register failed. Error number %d", res);
> +
> +	return res;
> +}
> +
> +static void __exit ufx_module_exit(void)
> +{
> +	usb_deregister(&ufx_driver);
> +}
> +
> +module_init(ufx_module_init);
> +module_exit(ufx_module_exit);
> +
> +static void ufx_urb_completion(struct urb *urb)
> +{
> +	struct urb_node *unode = urb->context;
> +	struct ufx_data *dev = unode->dev;
> +	unsigned long flags;
> +
> +	/* sync/async unlink faults aren't errors */
> +	if (urb->status) {
> +		if (!(urb->status = -ENOENT ||
> +		    urb->status = -ECONNRESET ||
> +		    urb->status = -ESHUTDOWN)) {
> +			pr_err("%s - nonzero write bulk status received: %d\n",
> +				__func__, urb->status);
> +			atomic_set(&dev->lost_pixels, 1);
> +		}
> +	}
> +
> +	urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
> +
> +	spin_lock_irqsave(&dev->urbs.lock, flags);
> +	list_add_tail(&unode->entry, &dev->urbs.list);
> +	dev->urbs.available++;
> +	spin_unlock_irqrestore(&dev->urbs.lock, flags);
> +
> +	/* When using fb_defio, we deadlock if up() is called
> +	 * while another is waiting. So queue to another process */
> +	if (fb_defio)
> +		schedule_delayed_work(&unode->release_urb_work, 0);
> +	else
> +		up(&dev->urbs.limit_sem);
> +}
> +
> +static void ufx_free_urb_list(struct ufx_data *dev)
> +{
> +	int count = dev->urbs.count;
> +	struct list_head *node;
> +	struct urb_node *unode;
> +	struct urb *urb;
> +	int ret;
> +	unsigned long flags;
> +
> +	pr_notice("Waiting for completes and freeing all render urbs\n");
> +
> +	/* keep waiting and freeing, until we've got 'em all */
> +	while (count--) {
> +		/* Getting interrupted means a leak, but ok at shutdown*/
> +		ret = down_interruptible(&dev->urbs.limit_sem);
> +		if (ret)
> +			break;
> +
> +		spin_lock_irqsave(&dev->urbs.lock, flags);
> +
> +		node = dev->urbs.list.next; /* have reserved one with sem */
> +		list_del_init(node);
> +
> +		spin_unlock_irqrestore(&dev->urbs.lock, flags);
> +
> +		unode = list_entry(node, struct urb_node, entry);
> +		urb = unode->urb;
> +
> +		/* Free each separately allocated piece */
> +		usb_free_coherent(urb->dev, dev->urbs.size,
> +				  urb->transfer_buffer, urb->transfer_dma);
> +		usb_free_urb(urb);
> +		kfree(node);
> +	}
> +}
> +
> +static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
> +{
> +	int i = 0;
> +	struct urb *urb;
> +	struct urb_node *unode;
> +	char *buf;
> +
> +	spin_lock_init(&dev->urbs.lock);
> +
> +	dev->urbs.size = size;
> +	INIT_LIST_HEAD(&dev->urbs.list);
> +
> +	while (i < count) {
> +		unode = kzalloc(sizeof(struct urb_node), GFP_KERNEL);
> +		if (!unode)
> +			break;
> +		unode->dev = dev;
> +
> +		INIT_DELAYED_WORK(&unode->release_urb_work,
> +			  ufx_release_urb_work);
> +
> +		urb = usb_alloc_urb(0, GFP_KERNEL);
> +		if (!urb) {
> +			kfree(unode);
> +			break;
> +		}
> +		unode->urb = urb;
> +
> +		buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
> +					 &urb->transfer_dma);
> +		if (!buf) {
> +			kfree(unode);
> +			usb_free_urb(urb);
> +			break;
> +		}
> +
> +		/* urb->transfer_buffer_length set to actual before submit */
> +		usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
> +			buf, size, ufx_urb_completion, unode);
> +		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
> +
> +		list_add_tail(&unode->entry, &dev->urbs.list);
> +
> +		i++;
> +	}
> +
> +	sema_init(&dev->urbs.limit_sem, i);
> +	dev->urbs.count = i;
> +	dev->urbs.available = i;
> +
> +	pr_notice("allocated %d %d byte urbs\n", i, (int) size);
> +
> +	return i;
> +}
> +
> +static struct urb *ufx_get_urb(struct ufx_data *dev)
> +{
> +	int ret = 0;
> +	struct list_head *entry;
> +	struct urb_node *unode;
> +	struct urb *urb = NULL;
> +	unsigned long flags;
> +
> +	/* Wait for an in-flight buffer to complete and get re-queued */
> +	ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
> +	if (ret) {
> +		atomic_set(&dev->lost_pixels, 1);
> +		pr_warn("wait for urb interrupted: %x available: %d\n",
> +		       ret, dev->urbs.available);
> +		goto error;
> +	}
> +
> +	spin_lock_irqsave(&dev->urbs.lock, flags);
> +
> +	BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
> +	entry = dev->urbs.list.next;
> +	list_del_init(entry);
> +	dev->urbs.available--;
> +
> +	spin_unlock_irqrestore(&dev->urbs.lock, flags);
> +
> +	unode = list_entry(entry, struct urb_node, entry);
> +	urb = unode->urb;
> +
> +error:
> +	return urb;
> +}
> +
> +static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
> +{
> +	int ret;
> +
> +	BUG_ON(len > dev->urbs.size);
> +
> +	urb->transfer_buffer_length = len; /* set to actual payload len */
> +	ret = usb_submit_urb(urb, GFP_KERNEL);
> +	if (ret) {
> +		ufx_urb_completion(urb); /* because no one else will */
> +		atomic_set(&dev->lost_pixels, 1);
> +		pr_err("usb_submit_urb error %x\n", ret);
> +	}
> +	return ret;
> +}
> +
> +module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
> +MODULE_PARM_DESC(console, "Allow fbcon to consume first framebuffer found");

The description is not really correct. It's more like
Allow fbcon to be used on this display.
Whether or not your module is the first framebuffer and therefore the one fbcon
tries to use with default settings depends on module load order and such things.

> +
> +module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
> +MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
> +
> +MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
> +MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
> +MODULE_LICENSE("GPL");

Otherwise it looks good to me.


Thanks,

Florian Tobias Schandinat

^ permalink raw reply

* [ANNOUNCE] fbdev maintainer change
From: Florian Tobias Schandinat @ 2011-08-16  7:22 UTC (permalink / raw)
  To: linux-fbdev@vger.kernel.org, LKML
  Cc: Paul Mundt, Linus Torvalds, Andrew Morton

Hi all,

as Paul hasn't been able to catch up with the patches lately and as already 
about half of the development cycle passed I decided to take over maintaining 
the fbdev subsystem, at least for now. So please send all patches you want to go 
mainline via fbdev also directly to me in addition to sending them to linux-fbdev.
Please help me to avoid picking up outdated, buggy versions of your patches by 
resending your final, bug-free ;) version if they are not already included in 
3.1-rc2. I'll be on vacation for a week now, so do not expect immediate 
response, I just wanted to give you some time to polish your patches. When I'm 
back I'll start setting up a fbdev-next branch, probably based on 3.1-rc3, and 
adding your patches to it. So feel free to drown me in patches.
I want to say thanks to Paul for his work, as well as all those people who made 
the fbdev subsystem such a great thing during the past decades and to those who 
are still working to make it even better.


Thanks and happy coding,

Florian Tobias Schandinat

^ permalink raw reply

* Re: [RFC] fbdev: allow multiple concurrent visible consoles
From: Florian Tobias Schandinat @ 2011-08-16  6:41 UTC (permalink / raw)
  To: linux-fbdev
In-Reply-To: <1312799864-3468-1-git-send-email-FlorianSchandinat@gmx.de>

Hello Martin,

On 08/11/2011 10:57 AM, Martin Decky wrote:
> Hello Florian,
>
>> This patch allows having multiple visible consoles that receive
>> display updates. For example one can have running "top" to monitor
>> the system on fb0 and at the same time work on a shell on fb1.
>
> I hate to report this, because I would really appreciate this feature,
> but your patch (applied against kernel 3.0) breaks my machine.

Thank you for testing it. And you do not need to worry, the more feedback I get 
the more motivated I am to get it mainline.

> I have an x86-64 machine with a dual-GPU ATI Radeon HD 4850 X2 (which
> acts essentially as two discrete R700s) driven by the "radeon" KMS driver.
>
> If I use the "fbcon=map:01" boot option to map the even-numbered VCs to
> the first GPU and the odd-numbered VCs to the second GPU, the kernel
> patched with your patch encounters some sort of soft-lockup when
> initializing the second GPU (i.e. creating the fb1) while booting. The
> is no panic or oops message and I can reset the machine by hitting
> ctrl+alt+del, but there is no further progress. I can provide further
> observations if you would like to.

Interesting. With your fbcon option I was able to reproduce it (without it, it 
boots as expected). Unfortunately I was not yet able too figure out what causes 
this behavior. I guess it'll take me some time to get it right, probably not for 
the next merge window, but I'll continue to work on it.


Thanks,

Florian Tobias Schandinat

^ permalink raw reply

* Re: [PATCH] video/atmel_lcdfb: Fix build failure/typo for avr32 defconfig
From: Laurent Pinchart @ 2011-08-15 21:29 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Peter Hüwe, Nicolas Ferre, Paul Mundt, linux-fbdev,
	linux-kernel, Stephen Rothwell
In-Reply-To: <20110815212659.GA18198@pengutronix.de>

On Monday 15 August 2011 23:26:59 Wolfram Sang wrote:
> On Mon, Aug 15, 2011 at 07:43:07PM +0200, Peter Hüwe wrote:
> > Am Montag 15 August 2011, 19:31:27 schrieb Laurent Pinchart:
> > > Thanks for the patch. I've already submitted the same patch
> > > (https://patchwork.kernel.org/patch/990792/) nearly a month ago to the
> > > linux- fbdev mailing list. Paul, can you please pick it up ?
> > 
> > Ah okay, didn't see it. I usually look on the lkml if the problem was
> > already fixed lately but somehow missed your patch.
> > 
> > Since you've done exactly what my patch did you get my
> > Reviewed-by: Peter Huewe <peterhuewe@gmx.de>
> 
> This bug was spotted during the review phase, too:
> 
> https://patchwork.kernel.org/patch/878382/
> 
> Dunno if the wrong patch was picked up or the follow up patch never came
> around. Doesn't matter:

I've sent a v3 but v2 was picked up. I've then sent a fix on top of v2, but it 
hasn't made it through (yet).

> Acked-by: Wolfram Sang <w.sang@pengutronix.de>

-- 
Regards,

Laurent Pinchart

^ permalink raw reply

* Re: [PATCH] video/atmel_lcdfb: Fix build failure/typo for avr32
From: Wolfram Sang @ 2011-08-15 21:26 UTC (permalink / raw)
  To: Peter Hüwe
  Cc: Laurent Pinchart, Nicolas Ferre, Paul Mundt, linux-fbdev,
	linux-kernel, Stephen Rothwell
In-Reply-To: <201108151943.08347.PeterHuewe@gmx.de>

[-- Attachment #1: Type: text/plain, Size: 1012 bytes --]

On Mon, Aug 15, 2011 at 07:43:07PM +0200, Peter Hüwe wrote:
> Am Montag 15 August 2011, 19:31:27 schrieb Laurent Pinchart:
> > Thanks for the patch. I've already submitted the same patch
> > (https://patchwork.kernel.org/patch/990792/) nearly a month ago to the
> > linux- fbdev mailing list. Paul, can you please pick it up ?
> 
> Ah okay, didn't see it. I usually look on the lkml if the problem was already 
> fixed lately but somehow missed your patch.
> 
> Since you've done exactly what my patch did you get my 
> Reviewed-by: Peter Huewe <peterhuewe@gmx.de>

This bug was spotted during the review phase, too:

https://patchwork.kernel.org/patch/878382/

Dunno if the wrong patch was picked up or the follow up patch never came around.
Doesn't matter:

Acked-by: Wolfram Sang <w.sang@pengutronix.de>

Regards,

   Wolfram

-- 
Pengutronix e.K.                           | Wolfram Sang                |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox