xref: /linux/drivers/video/fbdev/smscufx.c (revision c771600c6af14749609b49565ffb4cac2959710d)
112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23c8a63e2SSteve Glendinning /*
33c8a63e2SSteve Glendinning  * smscufx.c -- Framebuffer driver for SMSC UFX USB controller
43c8a63e2SSteve Glendinning  *
590b24cfbSSteve Glendinning  * Copyright (C) 2011 Steve Glendinning <steve.glendinning@shawell.net>
63c8a63e2SSteve Glendinning  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
73c8a63e2SSteve Glendinning  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
83c8a63e2SSteve Glendinning  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
93c8a63e2SSteve Glendinning  *
103c8a63e2SSteve Glendinning  * Based on udlfb, with work from Florian Echtler, Henrik Bjerregaard Pedersen,
113c8a63e2SSteve Glendinning  * and others.
123c8a63e2SSteve Glendinning  *
133c8a63e2SSteve Glendinning  * Works well with Bernie Thompson's X DAMAGE patch to xf86-video-fbdev
143c8a63e2SSteve Glendinning  * available from http://git.plugable.com
153c8a63e2SSteve Glendinning  *
163c8a63e2SSteve Glendinning  * Layout is based on skeletonfb by James Simmons and Geert Uytterhoeven,
173c8a63e2SSteve Glendinning  * usb-skeleton by GregKH.
183c8a63e2SSteve Glendinning  */
193c8a63e2SSteve Glendinning 
203c8a63e2SSteve Glendinning #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
213c8a63e2SSteve Glendinning 
223c8a63e2SSteve Glendinning #include <linux/module.h>
233c8a63e2SSteve Glendinning #include <linux/kernel.h>
243c8a63e2SSteve Glendinning #include <linux/init.h>
253c8a63e2SSteve Glendinning #include <linux/usb.h>
263c8a63e2SSteve Glendinning #include <linux/uaccess.h>
273c8a63e2SSteve Glendinning #include <linux/mm.h>
283c8a63e2SSteve Glendinning #include <linux/fb.h>
293c8a63e2SSteve Glendinning #include <linux/vmalloc.h>
303c8a63e2SSteve Glendinning #include <linux/slab.h>
313c8a63e2SSteve Glendinning #include <linux/delay.h>
323c8a63e2SSteve Glendinning #include "edid.h"
333c8a63e2SSteve Glendinning 
343c8a63e2SSteve Glendinning #define check_warn(status, fmt, args...) \
353c8a63e2SSteve Glendinning 	({ if (status < 0) pr_warn(fmt, ##args); })
363c8a63e2SSteve Glendinning 
373c8a63e2SSteve Glendinning #define check_warn_return(status, fmt, args...) \
383c8a63e2SSteve Glendinning 	({ if (status < 0) { pr_warn(fmt, ##args); return status; } })
393c8a63e2SSteve Glendinning 
403c8a63e2SSteve Glendinning #define check_warn_goto_error(status, fmt, args...) \
413c8a63e2SSteve Glendinning 	({ if (status < 0) { pr_warn(fmt, ##args); goto error; } })
423c8a63e2SSteve Glendinning 
433c8a63e2SSteve Glendinning #define all_bits_set(x, bits) (((x) & (bits)) == (bits))
443c8a63e2SSteve Glendinning 
453c8a63e2SSteve Glendinning #define USB_VENDOR_REQUEST_WRITE_REGISTER	0xA0
463c8a63e2SSteve Glendinning #define USB_VENDOR_REQUEST_READ_REGISTER	0xA1
473c8a63e2SSteve Glendinning 
483c8a63e2SSteve Glendinning /*
493c8a63e2SSteve Glendinning  * TODO: Propose standard fb.h ioctl for reporting damage,
503c8a63e2SSteve Glendinning  * using _IOWR() and one of the existing area structs from fb.h
513c8a63e2SSteve Glendinning  * Consider these ioctls deprecated, but they're still used by the
523c8a63e2SSteve Glendinning  * DisplayLink X server as yet - need both to be modified in tandem
533c8a63e2SSteve Glendinning  * when new ioctl(s) are ready.
543c8a63e2SSteve Glendinning  */
553c8a63e2SSteve Glendinning #define UFX_IOCTL_RETURN_EDID	(0xAD)
563c8a63e2SSteve Glendinning #define UFX_IOCTL_REPORT_DAMAGE	(0xAA)
573c8a63e2SSteve Glendinning 
583c8a63e2SSteve Glendinning /* -BULK_SIZE as per usb-skeleton. Can we get full page and avoid overhead? */
593c8a63e2SSteve Glendinning #define BULK_SIZE		(512)
603c8a63e2SSteve Glendinning #define MAX_TRANSFER		(PAGE_SIZE*16 - BULK_SIZE)
613c8a63e2SSteve Glendinning #define WRITES_IN_FLIGHT	(4)
623c8a63e2SSteve Glendinning 
633c8a63e2SSteve Glendinning #define GET_URB_TIMEOUT		(HZ)
643c8a63e2SSteve Glendinning #define FREE_URB_TIMEOUT	(HZ*2)
653c8a63e2SSteve Glendinning 
663c8a63e2SSteve Glendinning #define BPP			2
673c8a63e2SSteve Glendinning 
683c8a63e2SSteve Glendinning #define UFX_DEFIO_WRITE_DELAY	5 /* fb_deferred_io.delay in jiffies */
693c8a63e2SSteve Glendinning #define UFX_DEFIO_WRITE_DISABLE	(HZ*60) /* "disable" with long delay */
703c8a63e2SSteve Glendinning 
713c8a63e2SSteve Glendinning struct dloarea {
723c8a63e2SSteve Glendinning 	int x, y;
733c8a63e2SSteve Glendinning 	int w, h;
743c8a63e2SSteve Glendinning };
753c8a63e2SSteve Glendinning 
763c8a63e2SSteve Glendinning struct urb_node {
773c8a63e2SSteve Glendinning 	struct list_head entry;
783c8a63e2SSteve Glendinning 	struct ufx_data *dev;
793c8a63e2SSteve Glendinning 	struct delayed_work release_urb_work;
803c8a63e2SSteve Glendinning 	struct urb *urb;
813c8a63e2SSteve Glendinning };
823c8a63e2SSteve Glendinning 
833c8a63e2SSteve Glendinning struct urb_list {
843c8a63e2SSteve Glendinning 	struct list_head list;
853c8a63e2SSteve Glendinning 	spinlock_t lock;
863c8a63e2SSteve Glendinning 	struct semaphore limit_sem;
873c8a63e2SSteve Glendinning 	int available;
883c8a63e2SSteve Glendinning 	int count;
893c8a63e2SSteve Glendinning 	size_t size;
903c8a63e2SSteve Glendinning };
913c8a63e2SSteve Glendinning 
923c8a63e2SSteve Glendinning struct ufx_data {
933c8a63e2SSteve Glendinning 	struct usb_device *udev;
943c8a63e2SSteve Glendinning 	struct device *gdev; /* &udev->dev */
953c8a63e2SSteve Glendinning 	struct fb_info *info;
963c8a63e2SSteve Glendinning 	struct urb_list urbs;
973c8a63e2SSteve Glendinning 	struct kref kref;
983c8a63e2SSteve Glendinning 	int fb_count;
993c8a63e2SSteve Glendinning 	bool virtualized; /* true when physical usb device not present */
1003c8a63e2SSteve Glendinning 	atomic_t usb_active; /* 0 = update virtual buffer, but no usb traffic */
1013c8a63e2SSteve Glendinning 	atomic_t lost_pixels; /* 1 = a render op failed. Need screen refresh */
102261e7676SDan Carpenter 	u8 *edid; /* null until we read edid from hw or get from sysfs */
1033c8a63e2SSteve Glendinning 	size_t edid_size;
1043c8a63e2SSteve Glendinning 	u32 pseudo_palette[256];
1053c8a63e2SSteve Glendinning };
1063c8a63e2SSteve Glendinning 
1073c8a63e2SSteve Glendinning static struct fb_fix_screeninfo ufx_fix = {
1083c8a63e2SSteve Glendinning 	.id =           "smscufx",
1093c8a63e2SSteve Glendinning 	.type =         FB_TYPE_PACKED_PIXELS,
1103c8a63e2SSteve Glendinning 	.visual =       FB_VISUAL_TRUECOLOR,
1113c8a63e2SSteve Glendinning 	.xpanstep =     0,
1123c8a63e2SSteve Glendinning 	.ypanstep =     0,
1133c8a63e2SSteve Glendinning 	.ywrapstep =    0,
1143c8a63e2SSteve Glendinning 	.accel =        FB_ACCEL_NONE,
1153c8a63e2SSteve Glendinning };
1163c8a63e2SSteve Glendinning 
117b3e148d7SThomas Zimmermann static const u32 smscufx_info_flags = FBINFO_READS_FAST |
1183c8a63e2SSteve Glendinning 	FBINFO_VIRTFB |	FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT |
1193c8a63e2SSteve Glendinning 	FBINFO_HWACCEL_COPYAREA | FBINFO_MISC_ALWAYS_SETPAR;
1203c8a63e2SSteve Glendinning 
1218290d78eSArvind Yadav static const struct usb_device_id id_table[] = {
1223c8a63e2SSteve Glendinning 	{USB_DEVICE(0x0424, 0x9d00),},
1233c8a63e2SSteve Glendinning 	{USB_DEVICE(0x0424, 0x9d01),},
1243c8a63e2SSteve Glendinning 	{},
1253c8a63e2SSteve Glendinning };
1263c8a63e2SSteve Glendinning MODULE_DEVICE_TABLE(usb, id_table);
1273c8a63e2SSteve Glendinning 
1283c8a63e2SSteve Glendinning /* module options */
12990ab5ee9SRusty Russell static bool console;   /* Optionally allow fbcon to consume first framebuffer */
13090ab5ee9SRusty Russell static bool fb_defio = true;  /* Optionally enable fb_defio mmap support */
1313c8a63e2SSteve Glendinning 
1323c8a63e2SSteve Glendinning /* ufx keeps a list of urbs for efficient bulk transfers */
1333c8a63e2SSteve Glendinning static void ufx_urb_completion(struct urb *urb);
1343c8a63e2SSteve Glendinning static struct urb *ufx_get_urb(struct ufx_data *dev);
1353c8a63e2SSteve Glendinning static int ufx_submit_urb(struct ufx_data *dev, struct urb * urb, size_t len);
1363c8a63e2SSteve Glendinning static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size);
1373c8a63e2SSteve Glendinning static void ufx_free_urb_list(struct ufx_data *dev);
1383c8a63e2SSteve Glendinning 
1395610bcfeSHyunwoo Kim static DEFINE_MUTEX(disconnect_mutex);
1405610bcfeSHyunwoo Kim 
1413c8a63e2SSteve Glendinning /* reads a control register */
ufx_reg_read(struct ufx_data * dev,u32 index,u32 * data)1423c8a63e2SSteve Glendinning static int ufx_reg_read(struct ufx_data *dev, u32 index, u32 *data)
1433c8a63e2SSteve Glendinning {
1443c8a63e2SSteve Glendinning 	u32 *buf = kmalloc(4, GFP_KERNEL);
1453c8a63e2SSteve Glendinning 	int ret;
1463c8a63e2SSteve Glendinning 
1473c8a63e2SSteve Glendinning 	BUG_ON(!dev);
1483c8a63e2SSteve Glendinning 
1493c8a63e2SSteve Glendinning 	if (!buf)
1503c8a63e2SSteve Glendinning 		return -ENOMEM;
1513c8a63e2SSteve Glendinning 
1523c8a63e2SSteve Glendinning 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
1533c8a63e2SSteve Glendinning 		USB_VENDOR_REQUEST_READ_REGISTER,
1543c8a63e2SSteve Glendinning 		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1553c8a63e2SSteve Glendinning 		00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
1563c8a63e2SSteve Glendinning 
1573c8a63e2SSteve Glendinning 	le32_to_cpus(buf);
1583c8a63e2SSteve Glendinning 	*data = *buf;
1593c8a63e2SSteve Glendinning 	kfree(buf);
1603c8a63e2SSteve Glendinning 
1613c8a63e2SSteve Glendinning 	if (unlikely(ret < 0))
1623c8a63e2SSteve Glendinning 		pr_warn("Failed to read register index 0x%08x\n", index);
1633c8a63e2SSteve Glendinning 
1643c8a63e2SSteve Glendinning 	return ret;
1653c8a63e2SSteve Glendinning }
1663c8a63e2SSteve Glendinning 
1673c8a63e2SSteve Glendinning /* writes a control register */
ufx_reg_write(struct ufx_data * dev,u32 index,u32 data)1683c8a63e2SSteve Glendinning static int ufx_reg_write(struct ufx_data *dev, u32 index, u32 data)
1693c8a63e2SSteve Glendinning {
1703c8a63e2SSteve Glendinning 	u32 *buf = kmalloc(4, GFP_KERNEL);
1713c8a63e2SSteve Glendinning 	int ret;
1723c8a63e2SSteve Glendinning 
1733c8a63e2SSteve Glendinning 	BUG_ON(!dev);
1743c8a63e2SSteve Glendinning 
1753c8a63e2SSteve Glendinning 	if (!buf)
1763c8a63e2SSteve Glendinning 		return -ENOMEM;
1773c8a63e2SSteve Glendinning 
1783c8a63e2SSteve Glendinning 	*buf = data;
1793c8a63e2SSteve Glendinning 	cpu_to_le32s(buf);
1803c8a63e2SSteve Glendinning 
1813c8a63e2SSteve Glendinning 	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
1823c8a63e2SSteve Glendinning 		USB_VENDOR_REQUEST_WRITE_REGISTER,
1833c8a63e2SSteve Glendinning 		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1843c8a63e2SSteve Glendinning 		00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
1853c8a63e2SSteve Glendinning 
1863c8a63e2SSteve Glendinning 	kfree(buf);
1873c8a63e2SSteve Glendinning 
1883c8a63e2SSteve Glendinning 	if (unlikely(ret < 0))
1893c8a63e2SSteve Glendinning 		pr_warn("Failed to write register index 0x%08x with value "
1903c8a63e2SSteve Glendinning 			"0x%08x\n", index, data);
1913c8a63e2SSteve Glendinning 
1923c8a63e2SSteve Glendinning 	return ret;
1933c8a63e2SSteve Glendinning }
1943c8a63e2SSteve Glendinning 
ufx_reg_clear_and_set_bits(struct ufx_data * dev,u32 index,u32 bits_to_clear,u32 bits_to_set)1953c8a63e2SSteve Glendinning static int ufx_reg_clear_and_set_bits(struct ufx_data *dev, u32 index,
1963c8a63e2SSteve Glendinning 	u32 bits_to_clear, u32 bits_to_set)
1973c8a63e2SSteve Glendinning {
1983c8a63e2SSteve Glendinning 	u32 data;
1993c8a63e2SSteve Glendinning 	int status = ufx_reg_read(dev, index, &data);
2003c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_reg_clear_and_set_bits error reading "
2013c8a63e2SSteve Glendinning 		"0x%x", index);
2023c8a63e2SSteve Glendinning 
2033c8a63e2SSteve Glendinning 	data &= (~bits_to_clear);
2043c8a63e2SSteve Glendinning 	data |= bits_to_set;
2053c8a63e2SSteve Glendinning 
2063c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, index, data);
2073c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_reg_clear_and_set_bits error writing "
2083c8a63e2SSteve Glendinning 		"0x%x", index);
2093c8a63e2SSteve Glendinning 
2103c8a63e2SSteve Glendinning 	return 0;
2113c8a63e2SSteve Glendinning }
2123c8a63e2SSteve Glendinning 
ufx_reg_set_bits(struct ufx_data * dev,u32 index,u32 bits)2133c8a63e2SSteve Glendinning static int ufx_reg_set_bits(struct ufx_data *dev, u32 index, u32 bits)
2143c8a63e2SSteve Glendinning {
2153c8a63e2SSteve Glendinning 	return ufx_reg_clear_and_set_bits(dev, index, 0, bits);
2163c8a63e2SSteve Glendinning }
2173c8a63e2SSteve Glendinning 
ufx_reg_clear_bits(struct ufx_data * dev,u32 index,u32 bits)2183c8a63e2SSteve Glendinning static int ufx_reg_clear_bits(struct ufx_data *dev, u32 index, u32 bits)
2193c8a63e2SSteve Glendinning {
2203c8a63e2SSteve Glendinning 	return ufx_reg_clear_and_set_bits(dev, index, bits, 0);
2213c8a63e2SSteve Glendinning }
2223c8a63e2SSteve Glendinning 
ufx_lite_reset(struct ufx_data * dev)2233c8a63e2SSteve Glendinning static int ufx_lite_reset(struct ufx_data *dev)
2243c8a63e2SSteve Glendinning {
2253c8a63e2SSteve Glendinning 	int status;
2263c8a63e2SSteve Glendinning 	u32 value;
2273c8a63e2SSteve Glendinning 
2283c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x3008, 0x00000001);
2293c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_lite_reset error writing 0x3008");
2303c8a63e2SSteve Glendinning 
2313c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x3008, &value);
2323c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_lite_reset error reading 0x3008");
2333c8a63e2SSteve Glendinning 
2343c8a63e2SSteve Glendinning 	return (value == 0) ? 0 : -EIO;
2353c8a63e2SSteve Glendinning }
2363c8a63e2SSteve Glendinning 
2373c8a63e2SSteve Glendinning /* If display is unblanked, then blank it */
ufx_blank(struct ufx_data * dev,bool wait)2383c8a63e2SSteve Glendinning static int ufx_blank(struct ufx_data *dev, bool wait)
2393c8a63e2SSteve Glendinning {
2403c8a63e2SSteve Glendinning 	u32 dc_ctrl, dc_sts;
2413c8a63e2SSteve Glendinning 	int i;
2423c8a63e2SSteve Glendinning 
2433c8a63e2SSteve Glendinning 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
2443c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_blank error reading 0x2004");
2453c8a63e2SSteve Glendinning 
2463c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
2473c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_blank error reading 0x2000");
2483c8a63e2SSteve Glendinning 
2493c8a63e2SSteve Glendinning 	/* return success if display is already blanked */
2503c8a63e2SSteve Glendinning 	if ((dc_sts & 0x00000100) || (dc_ctrl & 0x00000100))
2513c8a63e2SSteve Glendinning 		return 0;
2523c8a63e2SSteve Glendinning 
2533c8a63e2SSteve Glendinning 	/* request the DC to blank the display */
2543c8a63e2SSteve Glendinning 	dc_ctrl |= 0x00000100;
2553c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
2563c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_blank error writing 0x2000");
2573c8a63e2SSteve Glendinning 
2583c8a63e2SSteve Glendinning 	/* return success immediately if we don't have to wait */
2593c8a63e2SSteve Glendinning 	if (!wait)
2603c8a63e2SSteve Glendinning 		return 0;
2613c8a63e2SSteve Glendinning 
2623c8a63e2SSteve Glendinning 	for (i = 0; i < 250; i++) {
2633c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
2643c8a63e2SSteve Glendinning 		check_warn_return(status, "ufx_blank error reading 0x2004");
2653c8a63e2SSteve Glendinning 
2663c8a63e2SSteve Glendinning 		if (dc_sts & 0x00000100)
2673c8a63e2SSteve Glendinning 			return 0;
2683c8a63e2SSteve Glendinning 	}
2693c8a63e2SSteve Glendinning 
2703c8a63e2SSteve Glendinning 	/* timed out waiting for display to blank */
2713c8a63e2SSteve Glendinning 	return -EIO;
2723c8a63e2SSteve Glendinning }
2733c8a63e2SSteve Glendinning 
2743c8a63e2SSteve Glendinning /* If display is blanked, then unblank it */
ufx_unblank(struct ufx_data * dev,bool wait)2753c8a63e2SSteve Glendinning static int ufx_unblank(struct ufx_data *dev, bool wait)
2763c8a63e2SSteve Glendinning {
2773c8a63e2SSteve Glendinning 	u32 dc_ctrl, dc_sts;
2783c8a63e2SSteve Glendinning 	int i;
2793c8a63e2SSteve Glendinning 
2803c8a63e2SSteve Glendinning 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
2813c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_unblank error reading 0x2004");
2823c8a63e2SSteve Glendinning 
2833c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
2843c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_unblank error reading 0x2000");
2853c8a63e2SSteve Glendinning 
2863c8a63e2SSteve Glendinning 	/* return success if display is already unblanked */
2873c8a63e2SSteve Glendinning 	if (((dc_sts & 0x00000100) == 0) || ((dc_ctrl & 0x00000100) == 0))
2883c8a63e2SSteve Glendinning 		return 0;
2893c8a63e2SSteve Glendinning 
2903c8a63e2SSteve Glendinning 	/* request the DC to unblank the display */
2913c8a63e2SSteve Glendinning 	dc_ctrl &= ~0x00000100;
2923c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
2933c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_unblank error writing 0x2000");
2943c8a63e2SSteve Glendinning 
2953c8a63e2SSteve Glendinning 	/* return success immediately if we don't have to wait */
2963c8a63e2SSteve Glendinning 	if (!wait)
2973c8a63e2SSteve Glendinning 		return 0;
2983c8a63e2SSteve Glendinning 
2993c8a63e2SSteve Glendinning 	for (i = 0; i < 250; i++) {
3003c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
3013c8a63e2SSteve Glendinning 		check_warn_return(status, "ufx_unblank error reading 0x2004");
3023c8a63e2SSteve Glendinning 
3033c8a63e2SSteve Glendinning 		if ((dc_sts & 0x00000100) == 0)
3043c8a63e2SSteve Glendinning 			return 0;
3053c8a63e2SSteve Glendinning 	}
3063c8a63e2SSteve Glendinning 
3073c8a63e2SSteve Glendinning 	/* timed out waiting for display to unblank */
3083c8a63e2SSteve Glendinning 	return -EIO;
3093c8a63e2SSteve Glendinning }
3103c8a63e2SSteve Glendinning 
3113c8a63e2SSteve Glendinning /* If display is enabled, then disable it */
ufx_disable(struct ufx_data * dev,bool wait)3123c8a63e2SSteve Glendinning static int ufx_disable(struct ufx_data *dev, bool wait)
3133c8a63e2SSteve Glendinning {
3143c8a63e2SSteve Glendinning 	u32 dc_ctrl, dc_sts;
3153c8a63e2SSteve Glendinning 	int i;
3163c8a63e2SSteve Glendinning 
3173c8a63e2SSteve Glendinning 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
3183c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_disable error reading 0x2004");
3193c8a63e2SSteve Glendinning 
3203c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
3213c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_disable error reading 0x2000");
3223c8a63e2SSteve Glendinning 
3233c8a63e2SSteve Glendinning 	/* return success if display is already disabled */
3243c8a63e2SSteve Glendinning 	if (((dc_sts & 0x00000001) == 0) || ((dc_ctrl & 0x00000001) == 0))
3253c8a63e2SSteve Glendinning 		return 0;
3263c8a63e2SSteve Glendinning 
3273c8a63e2SSteve Glendinning 	/* request the DC to disable the display */
3283c8a63e2SSteve Glendinning 	dc_ctrl &= ~(0x00000001);
3293c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
3303c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_disable error writing 0x2000");
3313c8a63e2SSteve Glendinning 
3323c8a63e2SSteve Glendinning 	/* return success immediately if we don't have to wait */
3333c8a63e2SSteve Glendinning 	if (!wait)
3343c8a63e2SSteve Glendinning 		return 0;
3353c8a63e2SSteve Glendinning 
3363c8a63e2SSteve Glendinning 	for (i = 0; i < 250; i++) {
3373c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
3383c8a63e2SSteve Glendinning 		check_warn_return(status, "ufx_disable error reading 0x2004");
3393c8a63e2SSteve Glendinning 
3403c8a63e2SSteve Glendinning 		if ((dc_sts & 0x00000001) == 0)
3413c8a63e2SSteve Glendinning 			return 0;
3423c8a63e2SSteve Glendinning 	}
3433c8a63e2SSteve Glendinning 
3443c8a63e2SSteve Glendinning 	/* timed out waiting for display to disable */
3453c8a63e2SSteve Glendinning 	return -EIO;
3463c8a63e2SSteve Glendinning }
3473c8a63e2SSteve Glendinning 
3483c8a63e2SSteve Glendinning /* If display is disabled, then enable it */
ufx_enable(struct ufx_data * dev,bool wait)3493c8a63e2SSteve Glendinning static int ufx_enable(struct ufx_data *dev, bool wait)
3503c8a63e2SSteve Glendinning {
3513c8a63e2SSteve Glendinning 	u32 dc_ctrl, dc_sts;
3523c8a63e2SSteve Glendinning 	int i;
3533c8a63e2SSteve Glendinning 
3543c8a63e2SSteve Glendinning 	int status = ufx_reg_read(dev, 0x2004, &dc_sts);
3553c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_enable error reading 0x2004");
3563c8a63e2SSteve Glendinning 
3573c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x2000, &dc_ctrl);
3583c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_enable error reading 0x2000");
3593c8a63e2SSteve Glendinning 
3603c8a63e2SSteve Glendinning 	/* return success if display is already enabled */
3613c8a63e2SSteve Glendinning 	if ((dc_sts & 0x00000001) || (dc_ctrl & 0x00000001))
3623c8a63e2SSteve Glendinning 		return 0;
3633c8a63e2SSteve Glendinning 
3643c8a63e2SSteve Glendinning 	/* request the DC to enable the display */
3653c8a63e2SSteve Glendinning 	dc_ctrl |= 0x00000001;
3663c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2000, dc_ctrl);
3673c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_enable error writing 0x2000");
3683c8a63e2SSteve Glendinning 
3693c8a63e2SSteve Glendinning 	/* return success immediately if we don't have to wait */
3703c8a63e2SSteve Glendinning 	if (!wait)
3713c8a63e2SSteve Glendinning 		return 0;
3723c8a63e2SSteve Glendinning 
3733c8a63e2SSteve Glendinning 	for (i = 0; i < 250; i++) {
3743c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x2004, &dc_sts);
3753c8a63e2SSteve Glendinning 		check_warn_return(status, "ufx_enable error reading 0x2004");
3763c8a63e2SSteve Glendinning 
3773c8a63e2SSteve Glendinning 		if (dc_sts & 0x00000001)
3783c8a63e2SSteve Glendinning 			return 0;
3793c8a63e2SSteve Glendinning 	}
3803c8a63e2SSteve Glendinning 
3813c8a63e2SSteve Glendinning 	/* timed out waiting for display to enable */
3823c8a63e2SSteve Glendinning 	return -EIO;
3833c8a63e2SSteve Glendinning }
3843c8a63e2SSteve Glendinning 
ufx_config_sys_clk(struct ufx_data * dev)3853c8a63e2SSteve Glendinning static int ufx_config_sys_clk(struct ufx_data *dev)
3863c8a63e2SSteve Glendinning {
3873c8a63e2SSteve Glendinning 	int status = ufx_reg_write(dev, 0x700C, 0x8000000F);
3883c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x700C");
3893c8a63e2SSteve Glendinning 
3903c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x7014, 0x0010024F);
3913c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x7014");
3923c8a63e2SSteve Glendinning 
3933c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x7010, 0x00000000);
3943c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x7010");
3953c8a63e2SSteve Glendinning 
3963c8a63e2SSteve Glendinning 	status = ufx_reg_clear_bits(dev, 0x700C, 0x0000000A);
3973c8a63e2SSteve Glendinning 	check_warn_return(status, "error clearing PLL1 bypass in 0x700C");
3983c8a63e2SSteve Glendinning 	msleep(1);
3993c8a63e2SSteve Glendinning 
4003c8a63e2SSteve Glendinning 	status = ufx_reg_clear_bits(dev, 0x700C, 0x80000000);
4013c8a63e2SSteve Glendinning 	check_warn_return(status, "error clearing output gate in 0x700C");
4023c8a63e2SSteve Glendinning 
4033c8a63e2SSteve Glendinning 	return 0;
4043c8a63e2SSteve Glendinning }
4053c8a63e2SSteve Glendinning 
ufx_config_ddr2(struct ufx_data * dev)4063c8a63e2SSteve Glendinning static int ufx_config_ddr2(struct ufx_data *dev)
4073c8a63e2SSteve Glendinning {
4083c8a63e2SSteve Glendinning 	int status, i = 0;
4093c8a63e2SSteve Glendinning 	u32 tmp;
4103c8a63e2SSteve Glendinning 
4113c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0004, 0x001F0F77);
4123c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0004");
4133c8a63e2SSteve Glendinning 
4143c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0008, 0xFFF00000);
4153c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0008");
4163c8a63e2SSteve Glendinning 
4173c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x000C, 0x0FFF2222);
4183c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x000C");
4193c8a63e2SSteve Glendinning 
4203c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0010, 0x00030814);
4213c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0010");
4223c8a63e2SSteve Glendinning 
4233c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0014, 0x00500019);
4243c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0014");
4253c8a63e2SSteve Glendinning 
4263c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0018, 0x020D0F15);
4273c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0018");
4283c8a63e2SSteve Glendinning 
4293c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x001C, 0x02532305);
4303c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x001C");
4313c8a63e2SSteve Glendinning 
4323c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0020, 0x0B030905);
4333c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0020");
4343c8a63e2SSteve Glendinning 
4353c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0024, 0x00000827);
4363c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0024");
4373c8a63e2SSteve Glendinning 
4383c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0028, 0x00000000);
4393c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0028");
4403c8a63e2SSteve Glendinning 
4413c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x002C, 0x00000042);
4423c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x002C");
4433c8a63e2SSteve Glendinning 
4443c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0030, 0x09520000);
4453c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0030");
4463c8a63e2SSteve Glendinning 
4473c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0034, 0x02223314);
4483c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0034");
4493c8a63e2SSteve Glendinning 
4503c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0038, 0x00430043);
4513c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0038");
4523c8a63e2SSteve Glendinning 
4533c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x003C, 0xF00F000F);
4543c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x003C");
4553c8a63e2SSteve Glendinning 
4563c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0040, 0xF380F00F);
4573c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0040");
4583c8a63e2SSteve Glendinning 
4593c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0044, 0xF00F0496);
4603c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0044");
4613c8a63e2SSteve Glendinning 
4623c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0048, 0x03080406);
4633c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0048");
4643c8a63e2SSteve Glendinning 
4653c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x004C, 0x00001000);
4663c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x004C");
4673c8a63e2SSteve Glendinning 
4683c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x005C, 0x00000007);
4693c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x005C");
4703c8a63e2SSteve Glendinning 
4713c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0100, 0x54F00012);
4723c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0100");
4733c8a63e2SSteve Glendinning 
4743c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0104, 0x00004012);
4753c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0104");
4763c8a63e2SSteve Glendinning 
4773c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0118, 0x40404040);
4783c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0118");
4793c8a63e2SSteve Glendinning 
4803c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x0000, 0x00000001);
4813c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x0000");
4823c8a63e2SSteve Glendinning 
4833c8a63e2SSteve Glendinning 	while (i++ < 500) {
4843c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x0000, &tmp);
4853c8a63e2SSteve Glendinning 		check_warn_return(status, "error reading 0x0000");
4863c8a63e2SSteve Glendinning 
4873c8a63e2SSteve Glendinning 		if (all_bits_set(tmp, 0xC0000000))
4883c8a63e2SSteve Glendinning 			return 0;
4893c8a63e2SSteve Glendinning 	}
4903c8a63e2SSteve Glendinning 
4913c8a63e2SSteve Glendinning 	pr_err("DDR2 initialisation timed out, reg 0x0000=0x%08x", tmp);
4923c8a63e2SSteve Glendinning 	return -ETIMEDOUT;
4933c8a63e2SSteve Glendinning }
4943c8a63e2SSteve Glendinning 
4953c8a63e2SSteve Glendinning struct pll_values {
4963c8a63e2SSteve Glendinning 	u32 div_r0;
4973c8a63e2SSteve Glendinning 	u32 div_f0;
4983c8a63e2SSteve Glendinning 	u32 div_q0;
4993c8a63e2SSteve Glendinning 	u32 range0;
5003c8a63e2SSteve Glendinning 	u32 div_r1;
5013c8a63e2SSteve Glendinning 	u32 div_f1;
5023c8a63e2SSteve Glendinning 	u32 div_q1;
5033c8a63e2SSteve Glendinning 	u32 range1;
5043c8a63e2SSteve Glendinning };
5053c8a63e2SSteve Glendinning 
ufx_calc_range(u32 ref_freq)5063c8a63e2SSteve Glendinning static u32 ufx_calc_range(u32 ref_freq)
5073c8a63e2SSteve Glendinning {
5083c8a63e2SSteve Glendinning 	if (ref_freq >= 88000000)
5093c8a63e2SSteve Glendinning 		return 7;
5103c8a63e2SSteve Glendinning 
5113c8a63e2SSteve Glendinning 	if (ref_freq >= 54000000)
5123c8a63e2SSteve Glendinning 		return 6;
5133c8a63e2SSteve Glendinning 
5143c8a63e2SSteve Glendinning 	if (ref_freq >= 34000000)
5153c8a63e2SSteve Glendinning 		return 5;
5163c8a63e2SSteve Glendinning 
5173c8a63e2SSteve Glendinning 	if (ref_freq >= 21000000)
5183c8a63e2SSteve Glendinning 		return 4;
5193c8a63e2SSteve Glendinning 
5203c8a63e2SSteve Glendinning 	if (ref_freq >= 13000000)
5213c8a63e2SSteve Glendinning 		return 3;
5223c8a63e2SSteve Glendinning 
5233c8a63e2SSteve Glendinning 	if (ref_freq >= 8000000)
5243c8a63e2SSteve Glendinning 		return 2;
5253c8a63e2SSteve Glendinning 
5263c8a63e2SSteve Glendinning 	return 1;
5273c8a63e2SSteve Glendinning }
5283c8a63e2SSteve Glendinning 
5293c8a63e2SSteve Glendinning /* calculates PLL divider settings for a desired target frequency */
ufx_calc_pll_values(const u32 clk_pixel_pll,struct pll_values * asic_pll)5303c8a63e2SSteve Glendinning static void ufx_calc_pll_values(const u32 clk_pixel_pll, struct pll_values *asic_pll)
5313c8a63e2SSteve Glendinning {
5323c8a63e2SSteve Glendinning 	const u32 ref_clk = 25000000;
5333c8a63e2SSteve Glendinning 	u32 div_r0, div_f0, div_q0, div_r1, div_f1, div_q1;
5343c8a63e2SSteve Glendinning 	u32 min_error = clk_pixel_pll;
5353c8a63e2SSteve Glendinning 
5363c8a63e2SSteve Glendinning 	for (div_r0 = 1; div_r0 <= 32; div_r0++) {
5373c8a63e2SSteve Glendinning 		u32 ref_freq0 = ref_clk / div_r0;
5383c8a63e2SSteve Glendinning 		if (ref_freq0 < 5000000)
5393c8a63e2SSteve Glendinning 			break;
5403c8a63e2SSteve Glendinning 
5413c8a63e2SSteve Glendinning 		if (ref_freq0 > 200000000)
5423c8a63e2SSteve Glendinning 			continue;
5433c8a63e2SSteve Glendinning 
5443c8a63e2SSteve Glendinning 		for (div_f0 = 1; div_f0 <= 256; div_f0++) {
5453c8a63e2SSteve Glendinning 			u32 vco_freq0 = ref_freq0 * div_f0;
5463c8a63e2SSteve Glendinning 
5473c8a63e2SSteve Glendinning 			if (vco_freq0 < 350000000)
5483c8a63e2SSteve Glendinning 				continue;
5493c8a63e2SSteve Glendinning 
5503c8a63e2SSteve Glendinning 			if (vco_freq0 > 700000000)
5513c8a63e2SSteve Glendinning 				break;
5523c8a63e2SSteve Glendinning 
5533c8a63e2SSteve Glendinning 			for (div_q0 = 0; div_q0 < 7; div_q0++) {
5543c8a63e2SSteve Glendinning 				u32 pllout_freq0 = vco_freq0 / (1 << div_q0);
5553c8a63e2SSteve Glendinning 
5563c8a63e2SSteve Glendinning 				if (pllout_freq0 < 5000000)
5573c8a63e2SSteve Glendinning 					break;
5583c8a63e2SSteve Glendinning 
5593c8a63e2SSteve Glendinning 				if (pllout_freq0 > 200000000)
5603c8a63e2SSteve Glendinning 					continue;
5613c8a63e2SSteve Glendinning 
5623c8a63e2SSteve Glendinning 				for (div_r1 = 1; div_r1 <= 32; div_r1++) {
5633c8a63e2SSteve Glendinning 					u32 ref_freq1 = pllout_freq0 / div_r1;
5643c8a63e2SSteve Glendinning 
5653c8a63e2SSteve Glendinning 					if (ref_freq1 < 5000000)
5663c8a63e2SSteve Glendinning 						break;
5673c8a63e2SSteve Glendinning 
5683c8a63e2SSteve Glendinning 					for (div_f1 = 1; div_f1 <= 256; div_f1++) {
5693c8a63e2SSteve Glendinning 						u32 vco_freq1 = ref_freq1 * div_f1;
5703c8a63e2SSteve Glendinning 
5713c8a63e2SSteve Glendinning 						if (vco_freq1 < 350000000)
5723c8a63e2SSteve Glendinning 							continue;
5733c8a63e2SSteve Glendinning 
5743c8a63e2SSteve Glendinning 						if (vco_freq1 > 700000000)
5753c8a63e2SSteve Glendinning 							break;
5763c8a63e2SSteve Glendinning 
5773c8a63e2SSteve Glendinning 						for (div_q1 = 0; div_q1 < 7; div_q1++) {
5783c8a63e2SSteve Glendinning 							u32 pllout_freq1 = vco_freq1 / (1 << div_q1);
5793c8a63e2SSteve Glendinning 							int error = abs(pllout_freq1 - clk_pixel_pll);
5803c8a63e2SSteve Glendinning 
5813c8a63e2SSteve Glendinning 							if (pllout_freq1 < 5000000)
5823c8a63e2SSteve Glendinning 								break;
5833c8a63e2SSteve Glendinning 
5843c8a63e2SSteve Glendinning 							if (pllout_freq1 > 700000000)
5853c8a63e2SSteve Glendinning 								continue;
5863c8a63e2SSteve Glendinning 
5873c8a63e2SSteve Glendinning 							if (error < min_error) {
5883c8a63e2SSteve Glendinning 								min_error = error;
5893c8a63e2SSteve Glendinning 
5903c8a63e2SSteve Glendinning 								/* final returned value is equal to calculated value - 1
5913c8a63e2SSteve Glendinning 								 * because a value of 0 = divide by 1 */
5923c8a63e2SSteve Glendinning 								asic_pll->div_r0 = div_r0 - 1;
5933c8a63e2SSteve Glendinning 								asic_pll->div_f0 = div_f0 - 1;
5943c8a63e2SSteve Glendinning 								asic_pll->div_q0 = div_q0;
5953c8a63e2SSteve Glendinning 								asic_pll->div_r1 = div_r1 - 1;
5963c8a63e2SSteve Glendinning 								asic_pll->div_f1 = div_f1 - 1;
5973c8a63e2SSteve Glendinning 								asic_pll->div_q1 = div_q1;
5983c8a63e2SSteve Glendinning 
5993c8a63e2SSteve Glendinning 								asic_pll->range0 = ufx_calc_range(ref_freq0);
6003c8a63e2SSteve Glendinning 								asic_pll->range1 = ufx_calc_range(ref_freq1);
6013c8a63e2SSteve Glendinning 
6023c8a63e2SSteve Glendinning 								if (min_error == 0)
6033c8a63e2SSteve Glendinning 									return;
6043c8a63e2SSteve Glendinning 							}
6053c8a63e2SSteve Glendinning 						}
6063c8a63e2SSteve Glendinning 					}
6073c8a63e2SSteve Glendinning 				}
6083c8a63e2SSteve Glendinning 			}
6093c8a63e2SSteve Glendinning 		}
6103c8a63e2SSteve Glendinning 	}
6113c8a63e2SSteve Glendinning }
6123c8a63e2SSteve Glendinning 
6133c8a63e2SSteve Glendinning /* sets analog bit PLL configuration values */
ufx_config_pix_clk(struct ufx_data * dev,u32 pixclock)6143c8a63e2SSteve Glendinning static int ufx_config_pix_clk(struct ufx_data *dev, u32 pixclock)
6153c8a63e2SSteve Glendinning {
6163c8a63e2SSteve Glendinning 	struct pll_values asic_pll = {0};
6173c8a63e2SSteve Glendinning 	u32 value, clk_pixel, clk_pixel_pll;
6183c8a63e2SSteve Glendinning 	int status;
6193c8a63e2SSteve Glendinning 
6203c8a63e2SSteve Glendinning 	/* convert pixclock (in ps) to frequency (in Hz) */
6213c8a63e2SSteve Glendinning 	clk_pixel = PICOS2KHZ(pixclock) * 1000;
6223c8a63e2SSteve Glendinning 	pr_debug("pixclock %d ps = clk_pixel %d Hz", pixclock, clk_pixel);
6233c8a63e2SSteve Glendinning 
6243c8a63e2SSteve Glendinning 	/* clk_pixel = 1/2 clk_pixel_pll */
6253c8a63e2SSteve Glendinning 	clk_pixel_pll = clk_pixel * 2;
6263c8a63e2SSteve Glendinning 
6273c8a63e2SSteve Glendinning 	ufx_calc_pll_values(clk_pixel_pll, &asic_pll);
6283c8a63e2SSteve Glendinning 
6293c8a63e2SSteve Glendinning 	/* Keep BYPASS and RESET signals asserted until configured */
6303c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x7000, 0x8000000F);
6313c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x7000");
6323c8a63e2SSteve Glendinning 
6333c8a63e2SSteve Glendinning 	value = (asic_pll.div_f1 | (asic_pll.div_r1 << 8) |
6343c8a63e2SSteve Glendinning 		(asic_pll.div_q1 << 16) | (asic_pll.range1 << 20));
6353c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x7008, value);
6363c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x7008");
6373c8a63e2SSteve Glendinning 
6383c8a63e2SSteve Glendinning 	value = (asic_pll.div_f0 | (asic_pll.div_r0 << 8) |
6393c8a63e2SSteve Glendinning 		(asic_pll.div_q0 << 16) | (asic_pll.range0 << 20));
6403c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x7004, value);
6413c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x7004");
6423c8a63e2SSteve Glendinning 
6433c8a63e2SSteve Glendinning 	status = ufx_reg_clear_bits(dev, 0x7000, 0x00000005);
6443c8a63e2SSteve Glendinning 	check_warn_return(status,
6453c8a63e2SSteve Glendinning 		"error clearing PLL0 bypass bits in 0x7000");
6463c8a63e2SSteve Glendinning 	msleep(1);
6473c8a63e2SSteve Glendinning 
6483c8a63e2SSteve Glendinning 	status = ufx_reg_clear_bits(dev, 0x7000, 0x0000000A);
6493c8a63e2SSteve Glendinning 	check_warn_return(status,
6503c8a63e2SSteve Glendinning 		"error clearing PLL1 bypass bits in 0x7000");
6513c8a63e2SSteve Glendinning 	msleep(1);
6523c8a63e2SSteve Glendinning 
6533c8a63e2SSteve Glendinning 	status = ufx_reg_clear_bits(dev, 0x7000, 0x80000000);
6543c8a63e2SSteve Glendinning 	check_warn_return(status, "error clearing gate bits in 0x7000");
6553c8a63e2SSteve Glendinning 
6563c8a63e2SSteve Glendinning 	return 0;
6573c8a63e2SSteve Glendinning }
6583c8a63e2SSteve Glendinning 
ufx_set_vid_mode(struct ufx_data * dev,struct fb_var_screeninfo * var)6593c8a63e2SSteve Glendinning static int ufx_set_vid_mode(struct ufx_data *dev, struct fb_var_screeninfo *var)
6603c8a63e2SSteve Glendinning {
6613c8a63e2SSteve Glendinning 	u32 temp;
6623c8a63e2SSteve Glendinning 	u16 h_total, h_active, h_blank_start, h_blank_end, h_sync_start, h_sync_end;
6633c8a63e2SSteve Glendinning 	u16 v_total, v_active, v_blank_start, v_blank_end, v_sync_start, v_sync_end;
6643c8a63e2SSteve Glendinning 
6653c8a63e2SSteve Glendinning 	int status = ufx_reg_write(dev, 0x8028, 0);
6663c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error disabling RGB pad");
6673c8a63e2SSteve Glendinning 
6683c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x8024, 0);
6693c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error disabling VDAC");
6703c8a63e2SSteve Glendinning 
6713c8a63e2SSteve Glendinning 	/* shut everything down before changing timing */
6723c8a63e2SSteve Glendinning 	status = ufx_blank(dev, true);
6733c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error blanking display");
6743c8a63e2SSteve Glendinning 
6753c8a63e2SSteve Glendinning 	status = ufx_disable(dev, true);
6763c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error disabling display");
6773c8a63e2SSteve Glendinning 
6783c8a63e2SSteve Glendinning 	status = ufx_config_pix_clk(dev, var->pixclock);
6793c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error configuring pixclock");
6803c8a63e2SSteve Glendinning 
6813c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2000, 0x00000104);
6823c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2000");
6833c8a63e2SSteve Glendinning 
6843c8a63e2SSteve Glendinning 	/* set horizontal timings */
6853c8a63e2SSteve Glendinning 	h_total = var->xres + var->right_margin + var->hsync_len + var->left_margin;
6863c8a63e2SSteve Glendinning 	h_active = var->xres;
6873c8a63e2SSteve Glendinning 	h_blank_start = var->xres + var->right_margin;
6883c8a63e2SSteve Glendinning 	h_blank_end = var->xres + var->right_margin + var->hsync_len;
6893c8a63e2SSteve Glendinning 	h_sync_start = var->xres + var->right_margin;
6903c8a63e2SSteve Glendinning 	h_sync_end = var->xres + var->right_margin + var->hsync_len;
6913c8a63e2SSteve Glendinning 
6923c8a63e2SSteve Glendinning 	temp = ((h_total - 1) << 16) | (h_active - 1);
6933c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2008, temp);
6943c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2008");
6953c8a63e2SSteve Glendinning 
6963c8a63e2SSteve Glendinning 	temp = ((h_blank_start - 1) << 16) | (h_blank_end - 1);
6973c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x200C, temp);
6983c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x200C");
6993c8a63e2SSteve Glendinning 
7003c8a63e2SSteve Glendinning 	temp = ((h_sync_start - 1) << 16) | (h_sync_end - 1);
7013c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2010, temp);
7023c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2010");
7033c8a63e2SSteve Glendinning 
7043c8a63e2SSteve Glendinning 	/* set vertical timings */
7053c8a63e2SSteve Glendinning 	v_total = var->upper_margin + var->yres + var->lower_margin + var->vsync_len;
7063c8a63e2SSteve Glendinning 	v_active = var->yres;
7073c8a63e2SSteve Glendinning 	v_blank_start = var->yres + var->lower_margin;
7083c8a63e2SSteve Glendinning 	v_blank_end = var->yres + var->lower_margin + var->vsync_len;
7093c8a63e2SSteve Glendinning 	v_sync_start = var->yres + var->lower_margin;
7103c8a63e2SSteve Glendinning 	v_sync_end = var->yres + var->lower_margin + var->vsync_len;
7113c8a63e2SSteve Glendinning 
7123c8a63e2SSteve Glendinning 	temp = ((v_total - 1) << 16) | (v_active - 1);
7133c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2014, temp);
7143c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2014");
7153c8a63e2SSteve Glendinning 
7163c8a63e2SSteve Glendinning 	temp = ((v_blank_start - 1) << 16) | (v_blank_end - 1);
7173c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2018, temp);
7183c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2018");
7193c8a63e2SSteve Glendinning 
7203c8a63e2SSteve Glendinning 	temp = ((v_sync_start - 1) << 16) | (v_sync_end - 1);
7213c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x201C, temp);
7223c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x201C");
7233c8a63e2SSteve Glendinning 
7243c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2020, 0x00000000);
7253c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2020");
7263c8a63e2SSteve Glendinning 
7273c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2024, 0x00000000);
7283c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2024");
7293c8a63e2SSteve Glendinning 
7303c8a63e2SSteve Glendinning 	/* Set the frame length register (#pix * 2 bytes/pixel) */
7313c8a63e2SSteve Glendinning 	temp = var->xres * var->yres * 2;
7323c8a63e2SSteve Glendinning 	temp = (temp + 7) & (~0x7);
7333c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2028, temp);
7343c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2028");
7353c8a63e2SSteve Glendinning 
7363c8a63e2SSteve Glendinning 	/* enable desired output interface & disable others */
7373c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2040, 0);
7383c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
7393c8a63e2SSteve Glendinning 
7403c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2044, 0);
7413c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2044");
7423c8a63e2SSteve Glendinning 
7433c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2048, 0);
7443c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2048");
7453c8a63e2SSteve Glendinning 
7463c8a63e2SSteve Glendinning 	/* set the sync polarities & enable bit */
7473c8a63e2SSteve Glendinning 	temp = 0x00000001;
7483c8a63e2SSteve Glendinning 	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
7493c8a63e2SSteve Glendinning 		temp |= 0x00000010;
7503c8a63e2SSteve Glendinning 
7513c8a63e2SSteve Glendinning 	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
7523c8a63e2SSteve Glendinning 		temp |= 0x00000008;
7533c8a63e2SSteve Glendinning 
7543c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x2040, temp);
7553c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error writing 0x2040");
7563c8a63e2SSteve Glendinning 
7573c8a63e2SSteve Glendinning 	/* start everything back up */
7583c8a63e2SSteve Glendinning 	status = ufx_enable(dev, true);
7593c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error enabling display");
7603c8a63e2SSteve Glendinning 
7613c8a63e2SSteve Glendinning 	/* Unblank the display */
7623c8a63e2SSteve Glendinning 	status = ufx_unblank(dev, true);
7633c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error unblanking display");
7643c8a63e2SSteve Glendinning 
7653c8a63e2SSteve Glendinning 	/* enable RGB pad */
7663c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x8028, 0x00000003);
7673c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error enabling RGB pad");
7683c8a63e2SSteve Glendinning 
7693c8a63e2SSteve Glendinning 	/* enable VDAC */
7703c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x8024, 0x00000007);
7713c8a63e2SSteve Glendinning 	check_warn_return(status, "ufx_set_vid_mode error enabling VDAC");
7723c8a63e2SSteve Glendinning 
7733c8a63e2SSteve Glendinning 	return 0;
7743c8a63e2SSteve Glendinning }
7753c8a63e2SSteve Glendinning 
ufx_ops_mmap(struct fb_info * info,struct vm_area_struct * vma)7763c8a63e2SSteve Glendinning static int ufx_ops_mmap(struct fb_info *info, struct vm_area_struct *vma)
7773c8a63e2SSteve Glendinning {
7783c8a63e2SSteve Glendinning 	unsigned long start = vma->vm_start;
7793c8a63e2SSteve Glendinning 	unsigned long size = vma->vm_end - vma->vm_start;
7803c8a63e2SSteve Glendinning 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
7813c8a63e2SSteve Glendinning 	unsigned long page, pos;
7823c8a63e2SSteve Glendinning 
78359055851SThomas Zimmermann 	if (info->fbdefio)
78459055851SThomas Zimmermann 		return fb_deferred_io_mmap(info, vma);
78559055851SThomas Zimmermann 
78676f92201SThomas Zimmermann 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
78776f92201SThomas Zimmermann 
78804f8afbeSTomi Valkeinen 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
78904f8afbeSTomi Valkeinen 		return -EINVAL;
79004f8afbeSTomi Valkeinen 	if (size > info->fix.smem_len)
79104f8afbeSTomi Valkeinen 		return -EINVAL;
79204f8afbeSTomi Valkeinen 	if (offset > info->fix.smem_len - size)
7933c8a63e2SSteve Glendinning 		return -EINVAL;
7943c8a63e2SSteve Glendinning 
7953c8a63e2SSteve Glendinning 	pos = (unsigned long)info->fix.smem_start + offset;
7963c8a63e2SSteve Glendinning 
7973c8a63e2SSteve Glendinning 	pr_debug("mmap() framebuffer addr:%lu size:%lu\n",
7983c8a63e2SSteve Glendinning 		  pos, size);
7993c8a63e2SSteve Glendinning 
8003c8a63e2SSteve Glendinning 	while (size > 0) {
8013c8a63e2SSteve Glendinning 		page = vmalloc_to_pfn((void *)pos);
8023c8a63e2SSteve Glendinning 		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
8033c8a63e2SSteve Glendinning 			return -EAGAIN;
8043c8a63e2SSteve Glendinning 
8053c8a63e2SSteve Glendinning 		start += PAGE_SIZE;
8063c8a63e2SSteve Glendinning 		pos += PAGE_SIZE;
8073c8a63e2SSteve Glendinning 		if (size > PAGE_SIZE)
8083c8a63e2SSteve Glendinning 			size -= PAGE_SIZE;
8093c8a63e2SSteve Glendinning 		else
8103c8a63e2SSteve Glendinning 			size = 0;
8113c8a63e2SSteve Glendinning 	}
8123c8a63e2SSteve Glendinning 
8133c8a63e2SSteve Glendinning 	return 0;
8143c8a63e2SSteve Glendinning }
8153c8a63e2SSteve Glendinning 
ufx_raw_rect(struct ufx_data * dev,u16 * cmd,int x,int y,int width,int height)816be444890SSteve Glendinning static void ufx_raw_rect(struct ufx_data *dev, u16 *cmd, int x, int y,
8173c8a63e2SSteve Glendinning 	int width, int height)
8183c8a63e2SSteve Glendinning {
8193c8a63e2SSteve Glendinning 	size_t packed_line_len = ALIGN((width * 2), 4);
8203c8a63e2SSteve Glendinning 	size_t packed_rect_len = packed_line_len * height;
8213c8a63e2SSteve Glendinning 	int line;
8223c8a63e2SSteve Glendinning 
8233c8a63e2SSteve Glendinning 	BUG_ON(!dev);
8243c8a63e2SSteve Glendinning 	BUG_ON(!dev->info);
8253c8a63e2SSteve Glendinning 
8263c8a63e2SSteve Glendinning 	/* command word */
8273c8a63e2SSteve Glendinning 	*((u32 *)&cmd[0]) = cpu_to_le32(0x01);
8283c8a63e2SSteve Glendinning 
8293c8a63e2SSteve Glendinning 	/* length word */
830be444890SSteve Glendinning 	*((u32 *)&cmd[2]) = cpu_to_le32(packed_rect_len + 16);
8313c8a63e2SSteve Glendinning 
832be444890SSteve Glendinning 	cmd[4] = cpu_to_le16(x);
833be444890SSteve Glendinning 	cmd[5] = cpu_to_le16(y);
834be444890SSteve Glendinning 	cmd[6] = cpu_to_le16(width);
835be444890SSteve Glendinning 	cmd[7] = cpu_to_le16(height);
8363c8a63e2SSteve Glendinning 
8373c8a63e2SSteve Glendinning 	/* frame base address */
838be444890SSteve Glendinning 	*((u32 *)&cmd[8]) = cpu_to_le32(0);
8393c8a63e2SSteve Glendinning 
8403c8a63e2SSteve Glendinning 	/* color mode and horizontal resolution */
841be444890SSteve Glendinning 	cmd[10] = cpu_to_le16(0x4000 | dev->info->var.xres);
8423c8a63e2SSteve Glendinning 
8433c8a63e2SSteve Glendinning 	/* vertical resolution */
844be444890SSteve Glendinning 	cmd[11] = cpu_to_le16(dev->info->var.yres);
8453c8a63e2SSteve Glendinning 
8463c8a63e2SSteve Glendinning 	/* packed data */
8473c8a63e2SSteve Glendinning 	for (line = 0; line < height; line++) {
8483c8a63e2SSteve Glendinning 		const int line_offset = dev->info->fix.line_length * (y + line);
8493c8a63e2SSteve Glendinning 		const int byte_offset = line_offset + (x * BPP);
850be444890SSteve Glendinning 		memcpy(&cmd[(24 + (packed_line_len * line)) / 2],
8513c8a63e2SSteve Glendinning 			(char *)dev->info->fix.smem_start + byte_offset, width * BPP);
8523c8a63e2SSteve Glendinning 	}
8533c8a63e2SSteve Glendinning }
8543c8a63e2SSteve Glendinning 
ufx_handle_damage(struct ufx_data * dev,int x,int y,int width,int height)85535f3ec1cSH Hartley Sweeten static int ufx_handle_damage(struct ufx_data *dev, int x, int y,
8563c8a63e2SSteve Glendinning 	int width, int height)
8573c8a63e2SSteve Glendinning {
8583c8a63e2SSteve Glendinning 	size_t packed_line_len = ALIGN((width * 2), 4);
8593c8a63e2SSteve Glendinning 	int len, status, urb_lines, start_line = 0;
8603c8a63e2SSteve Glendinning 
8613c8a63e2SSteve Glendinning 	if ((width <= 0) || (height <= 0) ||
8623c8a63e2SSteve Glendinning 	    (x + width > dev->info->var.xres) ||
8633c8a63e2SSteve Glendinning 	    (y + height > dev->info->var.yres))
8643c8a63e2SSteve Glendinning 		return -EINVAL;
8653c8a63e2SSteve Glendinning 
8663c8a63e2SSteve Glendinning 	if (!atomic_read(&dev->usb_active))
8673c8a63e2SSteve Glendinning 		return 0;
8683c8a63e2SSteve Glendinning 
8693c8a63e2SSteve Glendinning 	while (start_line < height) {
8703c8a63e2SSteve Glendinning 		struct urb *urb = ufx_get_urb(dev);
8713c8a63e2SSteve Glendinning 		if (!urb) {
8723c8a63e2SSteve Glendinning 			pr_warn("ufx_handle_damage unable to get urb");
8733c8a63e2SSteve Glendinning 			return 0;
8743c8a63e2SSteve Glendinning 		}
8753c8a63e2SSteve Glendinning 
8763c8a63e2SSteve Glendinning 		/* assume we have enough space to transfer at least one line */
8773c8a63e2SSteve Glendinning 		BUG_ON(urb->transfer_buffer_length < (24 + (width * 2)));
8783c8a63e2SSteve Glendinning 
8793c8a63e2SSteve Glendinning 		/* calculate the maximum number of lines we could fit in */
8803c8a63e2SSteve Glendinning 		urb_lines = (urb->transfer_buffer_length - 24) / packed_line_len;
8813c8a63e2SSteve Glendinning 
8823c8a63e2SSteve Glendinning 		/* but we might not need this many */
8833c8a63e2SSteve Glendinning 		urb_lines = min(urb_lines, (height - start_line));
8843c8a63e2SSteve Glendinning 
8853c8a63e2SSteve Glendinning 		memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
8863c8a63e2SSteve Glendinning 
8873c8a63e2SSteve Glendinning 		ufx_raw_rect(dev, urb->transfer_buffer, x, (y + start_line), width, urb_lines);
8883c8a63e2SSteve Glendinning 		len = 24 + (packed_line_len * urb_lines);
8893c8a63e2SSteve Glendinning 
8903c8a63e2SSteve Glendinning 		status = ufx_submit_urb(dev, urb, len);
8913c8a63e2SSteve Glendinning 		check_warn_return(status, "Error submitting URB");
8923c8a63e2SSteve Glendinning 
8933c8a63e2SSteve Glendinning 		start_line += urb_lines;
8943c8a63e2SSteve Glendinning 	}
8953c8a63e2SSteve Glendinning 
8963c8a63e2SSteve Glendinning 	return 0;
8973c8a63e2SSteve Glendinning }
8983c8a63e2SSteve Glendinning 
8993c8a63e2SSteve Glendinning /* NOTE: fb_defio.c is holding info->fbdefio.mutex
9003c8a63e2SSteve Glendinning  *   Touching ANY framebuffer memory that triggers a page fault
9013c8a63e2SSteve Glendinning  *   in fb_defio will cause a deadlock, when it also tries to
9023c8a63e2SSteve Glendinning  *   grab the same mutex. */
ufx_dpy_deferred_io(struct fb_info * info,struct list_head * pagereflist)903e80eec1bSThomas Zimmermann static void ufx_dpy_deferred_io(struct fb_info *info, struct list_head *pagereflist)
9043c8a63e2SSteve Glendinning {
9053c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
906e80eec1bSThomas Zimmermann 	struct fb_deferred_io_pageref *pageref;
9073c8a63e2SSteve Glendinning 
9083c8a63e2SSteve Glendinning 	if (!fb_defio)
9093c8a63e2SSteve Glendinning 		return;
9103c8a63e2SSteve Glendinning 
9113c8a63e2SSteve Glendinning 	if (!atomic_read(&dev->usb_active))
9123c8a63e2SSteve Glendinning 		return;
9133c8a63e2SSteve Glendinning 
9143c8a63e2SSteve Glendinning 	/* walk the written page list and render each to device */
915e80eec1bSThomas Zimmermann 	list_for_each_entry(pageref, pagereflist, list) {
9163c8a63e2SSteve Glendinning 		/* create a rectangle of full screen width that encloses the
9173c8a63e2SSteve Glendinning 		 * entire dirty framebuffer page */
9183c8a63e2SSteve Glendinning 		const int x = 0;
9193c8a63e2SSteve Glendinning 		const int width = dev->info->var.xres;
920e2d8b428SThomas Zimmermann 		const int y = pageref->offset / (width * 2);
9213c8a63e2SSteve Glendinning 		int height = (PAGE_SIZE / (width * 2)) + 1;
9223c8a63e2SSteve Glendinning 		height = min(height, (int)(dev->info->var.yres - y));
9233c8a63e2SSteve Glendinning 
9243c8a63e2SSteve Glendinning 		BUG_ON(y >= dev->info->var.yres);
9253c8a63e2SSteve Glendinning 		BUG_ON((y + height) > dev->info->var.yres);
9263c8a63e2SSteve Glendinning 
9273c8a63e2SSteve Glendinning 		ufx_handle_damage(dev, x, y, width, height);
9283c8a63e2SSteve Glendinning 	}
9293c8a63e2SSteve Glendinning }
9303c8a63e2SSteve Glendinning 
ufx_ops_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)9313c8a63e2SSteve Glendinning static int ufx_ops_ioctl(struct fb_info *info, unsigned int cmd,
9323c8a63e2SSteve Glendinning 			 unsigned long arg)
9333c8a63e2SSteve Glendinning {
9343c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
9353c8a63e2SSteve Glendinning 	struct dloarea *area = NULL;
9363c8a63e2SSteve Glendinning 
9373c8a63e2SSteve Glendinning 	if (!atomic_read(&dev->usb_active))
9383c8a63e2SSteve Glendinning 		return 0;
9393c8a63e2SSteve Glendinning 
9403c8a63e2SSteve Glendinning 	/* TODO: Update X server to get this from sysfs instead */
9413c8a63e2SSteve Glendinning 	if (cmd == UFX_IOCTL_RETURN_EDID) {
942261e7676SDan Carpenter 		u8 __user *edid = (u8 __user *)arg;
9433c8a63e2SSteve Glendinning 		if (copy_to_user(edid, dev->edid, dev->edid_size))
9443c8a63e2SSteve Glendinning 			return -EFAULT;
9453c8a63e2SSteve Glendinning 		return 0;
9463c8a63e2SSteve Glendinning 	}
9473c8a63e2SSteve Glendinning 
9483c8a63e2SSteve Glendinning 	/* TODO: Help propose a standard fb.h ioctl to report mmap damage */
9493c8a63e2SSteve Glendinning 	if (cmd == UFX_IOCTL_REPORT_DAMAGE) {
9503c8a63e2SSteve Glendinning 		/* If we have a damage-aware client, turn fb_defio "off"
951ff0c2642SMasanari Iida 		 * To avoid perf imact of unnecessary page fault handling.
9523c8a63e2SSteve Glendinning 		 * Done by resetting the delay for this fb_info to a very
9533c8a63e2SSteve Glendinning 		 * long period. Pages will become writable and stay that way.
9543c8a63e2SSteve Glendinning 		 * Reset to normal value when all clients have closed this fb.
9553c8a63e2SSteve Glendinning 		 */
9563c8a63e2SSteve Glendinning 		if (info->fbdefio)
9573c8a63e2SSteve Glendinning 			info->fbdefio->delay = UFX_DEFIO_WRITE_DISABLE;
9583c8a63e2SSteve Glendinning 
9593c8a63e2SSteve Glendinning 		area = (struct dloarea *)arg;
9603c8a63e2SSteve Glendinning 
9613c8a63e2SSteve Glendinning 		if (area->x < 0)
9623c8a63e2SSteve Glendinning 			area->x = 0;
9633c8a63e2SSteve Glendinning 
9643c8a63e2SSteve Glendinning 		if (area->x > info->var.xres)
9653c8a63e2SSteve Glendinning 			area->x = info->var.xres;
9663c8a63e2SSteve Glendinning 
9673c8a63e2SSteve Glendinning 		if (area->y < 0)
9683c8a63e2SSteve Glendinning 			area->y = 0;
9693c8a63e2SSteve Glendinning 
9703c8a63e2SSteve Glendinning 		if (area->y > info->var.yres)
9713c8a63e2SSteve Glendinning 			area->y = info->var.yres;
9723c8a63e2SSteve Glendinning 
9733c8a63e2SSteve Glendinning 		ufx_handle_damage(dev, area->x, area->y, area->w, area->h);
9743c8a63e2SSteve Glendinning 	}
9753c8a63e2SSteve Glendinning 
9763c8a63e2SSteve Glendinning 	return 0;
9773c8a63e2SSteve Glendinning }
9783c8a63e2SSteve Glendinning 
9793c8a63e2SSteve Glendinning /* taken from vesafb */
9803c8a63e2SSteve Glendinning static int
ufx_ops_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)9813c8a63e2SSteve Glendinning ufx_ops_setcolreg(unsigned regno, unsigned red, unsigned green,
9823c8a63e2SSteve Glendinning 	       unsigned blue, unsigned transp, struct fb_info *info)
9833c8a63e2SSteve Glendinning {
9843c8a63e2SSteve Glendinning 	int err = 0;
9853c8a63e2SSteve Glendinning 
9863c8a63e2SSteve Glendinning 	if (regno >= info->cmap.len)
9873c8a63e2SSteve Glendinning 		return 1;
9883c8a63e2SSteve Glendinning 
9893c8a63e2SSteve Glendinning 	if (regno < 16) {
9903c8a63e2SSteve Glendinning 		if (info->var.red.offset == 10) {
9913c8a63e2SSteve Glendinning 			/* 1:5:5:5 */
9923c8a63e2SSteve Glendinning 			((u32 *) (info->pseudo_palette))[regno] =
9933c8a63e2SSteve Glendinning 			    ((red & 0xf800) >> 1) |
9943c8a63e2SSteve Glendinning 			    ((green & 0xf800) >> 6) | ((blue & 0xf800) >> 11);
9953c8a63e2SSteve Glendinning 		} else {
9963c8a63e2SSteve Glendinning 			/* 0:5:6:5 */
9973c8a63e2SSteve Glendinning 			((u32 *) (info->pseudo_palette))[regno] =
9983c8a63e2SSteve Glendinning 			    ((red & 0xf800)) |
9993c8a63e2SSteve Glendinning 			    ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
10003c8a63e2SSteve Glendinning 		}
10013c8a63e2SSteve Glendinning 	}
10023c8a63e2SSteve Glendinning 
10033c8a63e2SSteve Glendinning 	return err;
10043c8a63e2SSteve Glendinning }
10053c8a63e2SSteve Glendinning 
10063c8a63e2SSteve Glendinning /* It's common for several clients to have framebuffer open simultaneously.
10073c8a63e2SSteve Glendinning  * e.g. both fbcon and X. Makes things interesting.
10083c8a63e2SSteve Glendinning  * Assumes caller is holding info->lock (for open and release at least) */
ufx_ops_open(struct fb_info * info,int user)10093c8a63e2SSteve Glendinning static int ufx_ops_open(struct fb_info *info, int user)
10103c8a63e2SSteve Glendinning {
10113c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
10123c8a63e2SSteve Glendinning 
10133c8a63e2SSteve Glendinning 	/* fbcon aggressively connects to first framebuffer it finds,
10143c8a63e2SSteve Glendinning 	 * preventing other clients (X) from working properly. Usually
10153c8a63e2SSteve Glendinning 	 * not what the user wants. Fail by default with option to enable. */
10163c8a63e2SSteve Glendinning 	if (user == 0 && !console)
10173c8a63e2SSteve Glendinning 		return -EBUSY;
10183c8a63e2SSteve Glendinning 
10195610bcfeSHyunwoo Kim 	mutex_lock(&disconnect_mutex);
10205610bcfeSHyunwoo Kim 
10213c8a63e2SSteve Glendinning 	/* If the USB device is gone, we don't accept new opens */
10225610bcfeSHyunwoo Kim 	if (dev->virtualized) {
10235610bcfeSHyunwoo Kim 		mutex_unlock(&disconnect_mutex);
10243c8a63e2SSteve Glendinning 		return -ENODEV;
10255610bcfeSHyunwoo Kim 	}
10263c8a63e2SSteve Glendinning 
10273c8a63e2SSteve Glendinning 	dev->fb_count++;
10283c8a63e2SSteve Glendinning 
10293c8a63e2SSteve Glendinning 	kref_get(&dev->kref);
10303c8a63e2SSteve Glendinning 
10313c8a63e2SSteve Glendinning 	if (fb_defio && (info->fbdefio == NULL)) {
10323c8a63e2SSteve Glendinning 		/* enable defio at last moment if not disabled by client */
10333c8a63e2SSteve Glendinning 
10343c8a63e2SSteve Glendinning 		struct fb_deferred_io *fbdefio;
10353c8a63e2SSteve Glendinning 
1036defddeffSMarkus Elfring 		fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL);
10373c8a63e2SSteve Glendinning 		if (fbdefio) {
10383c8a63e2SSteve Glendinning 			fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
10393c8a63e2SSteve Glendinning 			fbdefio->deferred_io = ufx_dpy_deferred_io;
10403c8a63e2SSteve Glendinning 		}
10413c8a63e2SSteve Glendinning 
10423c8a63e2SSteve Glendinning 		info->fbdefio = fbdefio;
10433c8a63e2SSteve Glendinning 		fb_deferred_io_init(info);
10443c8a63e2SSteve Glendinning 	}
10453c8a63e2SSteve Glendinning 
10463c8a63e2SSteve Glendinning 	pr_debug("open /dev/fb%d user=%d fb_info=%p count=%d",
10473c8a63e2SSteve Glendinning 		info->node, user, info, dev->fb_count);
10483c8a63e2SSteve Glendinning 
10495610bcfeSHyunwoo Kim 	mutex_unlock(&disconnect_mutex);
10505610bcfeSHyunwoo Kim 
10513c8a63e2SSteve Glendinning 	return 0;
10523c8a63e2SSteve Glendinning }
10533c8a63e2SSteve Glendinning 
10543c8a63e2SSteve Glendinning /*
10553c8a63e2SSteve Glendinning  * Called when all client interfaces to start transactions have been disabled,
10563c8a63e2SSteve Glendinning  * and all references to our device instance (ufx_data) are released.
10573c8a63e2SSteve Glendinning  * Every transaction must have a reference, so we know are fully spun down
10583c8a63e2SSteve Glendinning  */
ufx_free(struct kref * kref)10593c8a63e2SSteve Glendinning static void ufx_free(struct kref *kref)
10603c8a63e2SSteve Glendinning {
10613c8a63e2SSteve Glendinning 	struct ufx_data *dev = container_of(kref, struct ufx_data, kref);
10623c8a63e2SSteve Glendinning 
10633c8a63e2SSteve Glendinning 	kfree(dev);
10643c8a63e2SSteve Glendinning }
10653c8a63e2SSteve Glendinning 
ufx_ops_destory(struct fb_info * info)1066cc67482cSHyunwoo Kim static void ufx_ops_destory(struct fb_info *info)
1067cc67482cSHyunwoo Kim {
1068cc67482cSHyunwoo Kim 	struct ufx_data *dev = info->par;
1069cc67482cSHyunwoo Kim 	int node = info->node;
1070cc67482cSHyunwoo Kim 
1071cc67482cSHyunwoo Kim 	/* Assume info structure is freed after this point */
1072cc67482cSHyunwoo Kim 	framebuffer_release(info);
1073cc67482cSHyunwoo Kim 
1074cc67482cSHyunwoo Kim 	pr_debug("fb_info for /dev/fb%d has been freed", node);
1075cc67482cSHyunwoo Kim 
1076cc67482cSHyunwoo Kim 	/* release reference taken by kref_init in probe() */
1077cc67482cSHyunwoo Kim 	kref_put(&dev->kref, ufx_free);
1078cc67482cSHyunwoo Kim }
1079cc67482cSHyunwoo Kim 
1080cc67482cSHyunwoo Kim 
ufx_release_urb_work(struct work_struct * work)10813c8a63e2SSteve Glendinning static void ufx_release_urb_work(struct work_struct *work)
10823c8a63e2SSteve Glendinning {
10833c8a63e2SSteve Glendinning 	struct urb_node *unode = container_of(work, struct urb_node,
10843c8a63e2SSteve Glendinning 					      release_urb_work.work);
10853c8a63e2SSteve Glendinning 
10863c8a63e2SSteve Glendinning 	up(&unode->dev->urbs.limit_sem);
10873c8a63e2SSteve Glendinning }
10883c8a63e2SSteve Glendinning 
ufx_free_framebuffer(struct ufx_data * dev)1089cc67482cSHyunwoo Kim static void ufx_free_framebuffer(struct ufx_data *dev)
10903c8a63e2SSteve Glendinning {
10913c8a63e2SSteve Glendinning 	struct fb_info *info = dev->info;
10923c8a63e2SSteve Glendinning 
10933c8a63e2SSteve Glendinning 	if (info->cmap.len != 0)
10943c8a63e2SSteve Glendinning 		fb_dealloc_cmap(&info->cmap);
10953c8a63e2SSteve Glendinning 	if (info->monspecs.modedb)
10963c8a63e2SSteve Glendinning 		fb_destroy_modedb(info->monspecs.modedb);
10976ca49268SThomas Zimmermann 	vfree(info->screen_buffer);
10983c8a63e2SSteve Glendinning 
10993c8a63e2SSteve Glendinning 	fb_destroy_modelist(&info->modelist);
11003c8a63e2SSteve Glendinning 
11019aae307cSSachin Kamat 	dev->info = NULL;
11023c8a63e2SSteve Glendinning 
11033c8a63e2SSteve Glendinning 	/* ref taken in probe() as part of registering framebfufer */
11043c8a63e2SSteve Glendinning 	kref_put(&dev->kref, ufx_free);
11053c8a63e2SSteve Glendinning }
11063c8a63e2SSteve Glendinning 
11073c8a63e2SSteve Glendinning /*
11083c8a63e2SSteve Glendinning  * Assumes caller is holding info->lock mutex (for open and release at least)
11093c8a63e2SSteve Glendinning  */
ufx_ops_release(struct fb_info * info,int user)11103c8a63e2SSteve Glendinning static int ufx_ops_release(struct fb_info *info, int user)
11113c8a63e2SSteve Glendinning {
11123c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
11133c8a63e2SSteve Glendinning 
1114cc67482cSHyunwoo Kim 	mutex_lock(&disconnect_mutex);
1115cc67482cSHyunwoo Kim 
11163c8a63e2SSteve Glendinning 	dev->fb_count--;
11173c8a63e2SSteve Glendinning 
11183c8a63e2SSteve Glendinning 	/* We can't free fb_info here - fbmem will touch it when we return */
11193c8a63e2SSteve Glendinning 	if (dev->virtualized && (dev->fb_count == 0))
1120cc67482cSHyunwoo Kim 		ufx_free_framebuffer(dev);
11213c8a63e2SSteve Glendinning 
11223c8a63e2SSteve Glendinning 	if ((dev->fb_count == 0) && (info->fbdefio)) {
11233c8a63e2SSteve Glendinning 		fb_deferred_io_cleanup(info);
11243c8a63e2SSteve Glendinning 		kfree(info->fbdefio);
11253c8a63e2SSteve Glendinning 		info->fbdefio = NULL;
11263c8a63e2SSteve Glendinning 	}
11273c8a63e2SSteve Glendinning 
11283c8a63e2SSteve Glendinning 	pr_debug("released /dev/fb%d user=%d count=%d",
11293c8a63e2SSteve Glendinning 		  info->node, user, dev->fb_count);
11303c8a63e2SSteve Glendinning 
11313c8a63e2SSteve Glendinning 	kref_put(&dev->kref, ufx_free);
11323c8a63e2SSteve Glendinning 
1133cc67482cSHyunwoo Kim 	mutex_unlock(&disconnect_mutex);
1134cc67482cSHyunwoo Kim 
11353c8a63e2SSteve Glendinning 	return 0;
11363c8a63e2SSteve Glendinning }
11373c8a63e2SSteve Glendinning 
11383c8a63e2SSteve Glendinning /* Check whether a video mode is supported by the chip
11393c8a63e2SSteve Glendinning  * We start from monitor's modes, so don't need to filter that here */
ufx_is_valid_mode(struct fb_videomode * mode,struct fb_info * info)11403c8a63e2SSteve Glendinning static int ufx_is_valid_mode(struct fb_videomode *mode,
11413c8a63e2SSteve Glendinning 		struct fb_info *info)
11423c8a63e2SSteve Glendinning {
11433c8a63e2SSteve Glendinning 	if ((mode->xres * mode->yres) > (2048 * 1152)) {
11443c8a63e2SSteve Glendinning 		pr_debug("%dx%d too many pixels",
11453c8a63e2SSteve Glendinning 		       mode->xres, mode->yres);
11463c8a63e2SSteve Glendinning 		return 0;
11473c8a63e2SSteve Glendinning 	}
11483c8a63e2SSteve Glendinning 
11493c8a63e2SSteve Glendinning 	if (mode->pixclock < 5000) {
11503c8a63e2SSteve Glendinning 		pr_debug("%dx%d %dps pixel clock too fast",
11513c8a63e2SSteve Glendinning 		       mode->xres, mode->yres, mode->pixclock);
11523c8a63e2SSteve Glendinning 		return 0;
11533c8a63e2SSteve Glendinning 	}
11543c8a63e2SSteve Glendinning 
11553c8a63e2SSteve Glendinning 	pr_debug("%dx%d (pixclk %dps %dMHz) valid mode", mode->xres, mode->yres,
11563c8a63e2SSteve Glendinning 		mode->pixclock, (1000000 / mode->pixclock));
11573c8a63e2SSteve Glendinning 	return 1;
11583c8a63e2SSteve Glendinning }
11593c8a63e2SSteve Glendinning 
ufx_var_color_format(struct fb_var_screeninfo * var)11603c8a63e2SSteve Glendinning static void ufx_var_color_format(struct fb_var_screeninfo *var)
11613c8a63e2SSteve Glendinning {
11623c8a63e2SSteve Glendinning 	const struct fb_bitfield red = { 11, 5, 0 };
11633c8a63e2SSteve Glendinning 	const struct fb_bitfield green = { 5, 6, 0 };
11643c8a63e2SSteve Glendinning 	const struct fb_bitfield blue = { 0, 5, 0 };
11653c8a63e2SSteve Glendinning 
11663c8a63e2SSteve Glendinning 	var->bits_per_pixel = 16;
11673c8a63e2SSteve Glendinning 	var->red = red;
11683c8a63e2SSteve Glendinning 	var->green = green;
11693c8a63e2SSteve Glendinning 	var->blue = blue;
11703c8a63e2SSteve Glendinning }
11713c8a63e2SSteve Glendinning 
ufx_ops_check_var(struct fb_var_screeninfo * var,struct fb_info * info)11723c8a63e2SSteve Glendinning static int ufx_ops_check_var(struct fb_var_screeninfo *var,
11733c8a63e2SSteve Glendinning 				struct fb_info *info)
11743c8a63e2SSteve Glendinning {
11753c8a63e2SSteve Glendinning 	struct fb_videomode mode;
11763c8a63e2SSteve Glendinning 
11773c8a63e2SSteve Glendinning 	/* TODO: support dynamically changing framebuffer size */
11783c8a63e2SSteve Glendinning 	if ((var->xres * var->yres * 2) > info->fix.smem_len)
11793c8a63e2SSteve Glendinning 		return -EINVAL;
11803c8a63e2SSteve Glendinning 
11813c8a63e2SSteve Glendinning 	/* set device-specific elements of var unrelated to mode */
11823c8a63e2SSteve Glendinning 	ufx_var_color_format(var);
11833c8a63e2SSteve Glendinning 
11843c8a63e2SSteve Glendinning 	fb_var_to_videomode(&mode, var);
11853c8a63e2SSteve Glendinning 
11863c8a63e2SSteve Glendinning 	if (!ufx_is_valid_mode(&mode, info))
11873c8a63e2SSteve Glendinning 		return -EINVAL;
11883c8a63e2SSteve Glendinning 
11893c8a63e2SSteve Glendinning 	return 0;
11903c8a63e2SSteve Glendinning }
11913c8a63e2SSteve Glendinning 
ufx_ops_set_par(struct fb_info * info)11923c8a63e2SSteve Glendinning static int ufx_ops_set_par(struct fb_info *info)
11933c8a63e2SSteve Glendinning {
11943c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
11953c8a63e2SSteve Glendinning 	int result;
11963c8a63e2SSteve Glendinning 	u16 *pix_framebuffer;
11973c8a63e2SSteve Glendinning 	int i;
11983c8a63e2SSteve Glendinning 
11993c8a63e2SSteve Glendinning 	pr_debug("set_par mode %dx%d", info->var.xres, info->var.yres);
12003c8a63e2SSteve Glendinning 	result = ufx_set_vid_mode(dev, &info->var);
12013c8a63e2SSteve Glendinning 
12023c8a63e2SSteve Glendinning 	if ((result == 0) && (dev->fb_count == 0)) {
12033c8a63e2SSteve Glendinning 		/* paint greenscreen */
12046ca49268SThomas Zimmermann 		pix_framebuffer = (u16 *)info->screen_buffer;
12053c8a63e2SSteve Glendinning 		for (i = 0; i < info->fix.smem_len / 2; i++)
12063c8a63e2SSteve Glendinning 			pix_framebuffer[i] = 0x37e6;
12073c8a63e2SSteve Glendinning 
12083c8a63e2SSteve Glendinning 		ufx_handle_damage(dev, 0, 0, info->var.xres, info->var.yres);
12093c8a63e2SSteve Glendinning 	}
12103c8a63e2SSteve Glendinning 
12113c8a63e2SSteve Glendinning 	/* re-enable defio if previously disabled by damage tracking */
12123c8a63e2SSteve Glendinning 	if (info->fbdefio)
12133c8a63e2SSteve Glendinning 		info->fbdefio->delay = UFX_DEFIO_WRITE_DELAY;
12143c8a63e2SSteve Glendinning 
12153c8a63e2SSteve Glendinning 	return result;
12163c8a63e2SSteve Glendinning }
12173c8a63e2SSteve Glendinning 
12183c8a63e2SSteve Glendinning /* In order to come back from full DPMS off, we need to set the mode again */
ufx_ops_blank(int blank_mode,struct fb_info * info)12193c8a63e2SSteve Glendinning static int ufx_ops_blank(int blank_mode, struct fb_info *info)
12203c8a63e2SSteve Glendinning {
12213c8a63e2SSteve Glendinning 	struct ufx_data *dev = info->par;
12223c8a63e2SSteve Glendinning 	ufx_set_vid_mode(dev, &info->var);
12233c8a63e2SSteve Glendinning 	return 0;
12243c8a63e2SSteve Glendinning }
12253c8a63e2SSteve Glendinning 
ufx_ops_damage_range(struct fb_info * info,off_t off,size_t len)12262519fcebSThomas Zimmermann static void ufx_ops_damage_range(struct fb_info *info, off_t off, size_t len)
12272519fcebSThomas Zimmermann {
12282519fcebSThomas Zimmermann 	struct ufx_data *dev = info->par;
12292519fcebSThomas Zimmermann 	int start = max((int)(off / info->fix.line_length), 0);
12302519fcebSThomas Zimmermann 	int lines = min((u32)((len / info->fix.line_length) + 1), (u32)info->var.yres);
12312519fcebSThomas Zimmermann 
12322519fcebSThomas Zimmermann 	ufx_handle_damage(dev, 0, start, info->var.xres, lines);
12332519fcebSThomas Zimmermann }
12342519fcebSThomas Zimmermann 
ufx_ops_damage_area(struct fb_info * info,u32 x,u32 y,u32 width,u32 height)12352519fcebSThomas Zimmermann static void ufx_ops_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
12362519fcebSThomas Zimmermann {
12372519fcebSThomas Zimmermann 	struct ufx_data *dev = info->par;
12382519fcebSThomas Zimmermann 
12392519fcebSThomas Zimmermann 	ufx_handle_damage(dev, x, y, width, height);
12402519fcebSThomas Zimmermann }
12412519fcebSThomas Zimmermann 
12422519fcebSThomas Zimmermann FB_GEN_DEFAULT_DEFERRED_SYSMEM_OPS(ufx_ops,
12432519fcebSThomas Zimmermann 				   ufx_ops_damage_range,
12442519fcebSThomas Zimmermann 				   ufx_ops_damage_area)
12452519fcebSThomas Zimmermann 
12468a48ac33SJani Nikula static const struct fb_ops ufx_ops = {
12473c8a63e2SSteve Glendinning 	.owner = THIS_MODULE,
12482519fcebSThomas Zimmermann 	__FB_DEFAULT_DEFERRED_OPS_RDWR(ufx_ops),
12493c8a63e2SSteve Glendinning 	.fb_setcolreg = ufx_ops_setcolreg,
12502519fcebSThomas Zimmermann 	__FB_DEFAULT_DEFERRED_OPS_DRAW(ufx_ops),
12513c8a63e2SSteve Glendinning 	.fb_mmap = ufx_ops_mmap,
12523c8a63e2SSteve Glendinning 	.fb_ioctl = ufx_ops_ioctl,
12533c8a63e2SSteve Glendinning 	.fb_open = ufx_ops_open,
12543c8a63e2SSteve Glendinning 	.fb_release = ufx_ops_release,
12553c8a63e2SSteve Glendinning 	.fb_blank = ufx_ops_blank,
12563c8a63e2SSteve Glendinning 	.fb_check_var = ufx_ops_check_var,
12573c8a63e2SSteve Glendinning 	.fb_set_par = ufx_ops_set_par,
1258cc67482cSHyunwoo Kim 	.fb_destroy = ufx_ops_destory,
12593c8a63e2SSteve Glendinning };
12603c8a63e2SSteve Glendinning 
12613c8a63e2SSteve Glendinning /* Assumes &info->lock held by caller
12623c8a63e2SSteve Glendinning  * Assumes no active clients have framebuffer open */
ufx_realloc_framebuffer(struct ufx_data * dev,struct fb_info * info)12633c8a63e2SSteve Glendinning static int ufx_realloc_framebuffer(struct ufx_data *dev, struct fb_info *info)
12643c8a63e2SSteve Glendinning {
12653c8a63e2SSteve Glendinning 	int old_len = info->fix.smem_len;
12663c8a63e2SSteve Glendinning 	int new_len;
12676ca49268SThomas Zimmermann 	unsigned char *old_fb = info->screen_buffer;
12683c8a63e2SSteve Glendinning 	unsigned char *new_fb;
12693c8a63e2SSteve Glendinning 
12703c8a63e2SSteve Glendinning 	pr_debug("Reallocating framebuffer. Addresses will change!");
12713c8a63e2SSteve Glendinning 
12723c8a63e2SSteve Glendinning 	new_len = info->fix.line_length * info->var.yres;
12733c8a63e2SSteve Glendinning 
12743c8a63e2SSteve Glendinning 	if (PAGE_ALIGN(new_len) > old_len) {
12753c8a63e2SSteve Glendinning 		/*
12763c8a63e2SSteve Glendinning 		 * Alloc system memory for virtual framebuffer
12773c8a63e2SSteve Glendinning 		 */
12783c8a63e2SSteve Glendinning 		new_fb = vmalloc(new_len);
127966891254SMarkus Elfring 		if (!new_fb)
12804befd0cfSMarkus Elfring 			return -ENOMEM;
12813c8a63e2SSteve Glendinning 
12826ca49268SThomas Zimmermann 		if (info->screen_buffer) {
12833c8a63e2SSteve Glendinning 			memcpy(new_fb, old_fb, old_len);
12846ca49268SThomas Zimmermann 			vfree(info->screen_buffer);
12853c8a63e2SSteve Glendinning 		}
12863c8a63e2SSteve Glendinning 
12876ca49268SThomas Zimmermann 		info->screen_buffer = new_fb;
12883c8a63e2SSteve Glendinning 		info->fix.smem_len = PAGE_ALIGN(new_len);
12893c8a63e2SSteve Glendinning 		info->fix.smem_start = (unsigned long) new_fb;
12903c8a63e2SSteve Glendinning 		info->flags = smscufx_info_flags;
12913c8a63e2SSteve Glendinning 	}
12924befd0cfSMarkus Elfring 	return 0;
12933c8a63e2SSteve Glendinning }
12943c8a63e2SSteve Glendinning 
12953cf12cabSEaswar Hariharan /* sets up DDC channel for 100 Kbps, std. speed, 7-bit addr, controller mode,
12963c8a63e2SSteve Glendinning  * restart enabled, but no start byte, enable controller */
ufx_i2c_init(struct ufx_data * dev)12973c8a63e2SSteve Glendinning static int ufx_i2c_init(struct ufx_data *dev)
12983c8a63e2SSteve Glendinning {
12993c8a63e2SSteve Glendinning 	u32 tmp;
13003c8a63e2SSteve Glendinning 
13013c8a63e2SSteve Glendinning 	/* disable the controller before it can be reprogrammed */
13023c8a63e2SSteve Glendinning 	int status = ufx_reg_write(dev, 0x106C, 0x00);
13033c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to disable I2C");
13043c8a63e2SSteve Glendinning 
13053c8a63e2SSteve Glendinning 	/* Setup the clock count registers
13063c8a63e2SSteve Glendinning 	 * (12+1) = 13 clks @ 2.5 MHz = 5.2 uS */
13073c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x1018, 12);
13083c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x1018");
13093c8a63e2SSteve Glendinning 
13103c8a63e2SSteve Glendinning 	/* (6+8) = 14 clks @ 2.5 MHz = 5.6 uS */
13113c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x1014, 6);
13123c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x1014");
13133c8a63e2SSteve Glendinning 
13143c8a63e2SSteve Glendinning 	status = ufx_reg_read(dev, 0x1000, &tmp);
13153c8a63e2SSteve Glendinning 	check_warn_return(status, "error reading 0x1000");
13163c8a63e2SSteve Glendinning 
13173c8a63e2SSteve Glendinning 	/* set speed to std mode */
13183c8a63e2SSteve Glendinning 	tmp &= ~(0x06);
13193c8a63e2SSteve Glendinning 	tmp |= 0x02;
13203c8a63e2SSteve Glendinning 
13213c8a63e2SSteve Glendinning 	/* 7-bit (not 10-bit) addressing */
13223c8a63e2SSteve Glendinning 	tmp &= ~(0x10);
13233c8a63e2SSteve Glendinning 
13243cf12cabSEaswar Hariharan 	/* enable restart conditions and controller mode */
13253c8a63e2SSteve Glendinning 	tmp |= 0x21;
13263c8a63e2SSteve Glendinning 
13273c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x1000, tmp);
13283c8a63e2SSteve Glendinning 	check_warn_return(status, "error writing 0x1000");
13293c8a63e2SSteve Glendinning 
13303c8a63e2SSteve Glendinning 	/* Set normal tx using target address 0 */
13313c8a63e2SSteve Glendinning 	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0xC00, 0x000);
13323c8a63e2SSteve Glendinning 	check_warn_return(status, "error setting TX mode bits in 0x1004");
13333c8a63e2SSteve Glendinning 
13343c8a63e2SSteve Glendinning 	/* Enable the controller */
13353c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x106C, 0x01);
13363c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to enable I2C");
13373c8a63e2SSteve Glendinning 
13383c8a63e2SSteve Glendinning 	return 0;
13393c8a63e2SSteve Glendinning }
13403c8a63e2SSteve Glendinning 
13413c8a63e2SSteve Glendinning /* sets the I2C port mux and target address */
ufx_i2c_configure(struct ufx_data * dev)13423c8a63e2SSteve Glendinning static int ufx_i2c_configure(struct ufx_data *dev)
13433c8a63e2SSteve Glendinning {
13443c8a63e2SSteve Glendinning 	int status = ufx_reg_write(dev, 0x106C, 0x00);
13453c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to disable I2C");
13463c8a63e2SSteve Glendinning 
13473c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x3010, 0x00000000);
13483c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to write 0x3010");
13493c8a63e2SSteve Glendinning 
13503c8a63e2SSteve Glendinning 	/* A0h is std for any EDID, right shifted by one */
13513c8a63e2SSteve Glendinning 	status = ufx_reg_clear_and_set_bits(dev, 0x1004, 0x3FF,	(0xA0 >> 1));
13523c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to set TAR bits in 0x1004");
13533c8a63e2SSteve Glendinning 
13543c8a63e2SSteve Glendinning 	status = ufx_reg_write(dev, 0x106C, 0x01);
13553c8a63e2SSteve Glendinning 	check_warn_return(status, "failed to enable I2C");
13563c8a63e2SSteve Glendinning 
13573c8a63e2SSteve Glendinning 	return 0;
13583c8a63e2SSteve Glendinning }
13593c8a63e2SSteve Glendinning 
13603c8a63e2SSteve Glendinning /* wait for BUSY to clear, with a timeout of 50ms with 10ms sleeps. if no
13613c8a63e2SSteve Glendinning  * monitor is connected, there is no error except for timeout */
ufx_i2c_wait_busy(struct ufx_data * dev)13623c8a63e2SSteve Glendinning static int ufx_i2c_wait_busy(struct ufx_data *dev)
13633c8a63e2SSteve Glendinning {
13643c8a63e2SSteve Glendinning 	u32 tmp;
13653c8a63e2SSteve Glendinning 	int i, status;
13663c8a63e2SSteve Glendinning 
13673c8a63e2SSteve Glendinning 	for (i = 0; i < 15; i++) {
13683c8a63e2SSteve Glendinning 		status = ufx_reg_read(dev, 0x1100, &tmp);
13693c8a63e2SSteve Glendinning 		check_warn_return(status, "0x1100 read failed");
13703c8a63e2SSteve Glendinning 
13713c8a63e2SSteve Glendinning 		/* if BUSY is clear, check for error */
13723c8a63e2SSteve Glendinning 		if ((tmp & 0x80000000) == 0) {
13733c8a63e2SSteve Glendinning 			if (tmp & 0x20000000) {
13743c8a63e2SSteve Glendinning 				pr_warn("I2C read failed, 0x1100=0x%08x", tmp);
13753c8a63e2SSteve Glendinning 				return -EIO;
13763c8a63e2SSteve Glendinning 			}
13773c8a63e2SSteve Glendinning 
13783c8a63e2SSteve Glendinning 			return 0;
13793c8a63e2SSteve Glendinning 		}
13803c8a63e2SSteve Glendinning 
13813c8a63e2SSteve Glendinning 		/* perform the first 10 retries without delay */
13823c8a63e2SSteve Glendinning 		if (i >= 10)
13833c8a63e2SSteve Glendinning 			msleep(10);
13843c8a63e2SSteve Glendinning 	}
13853c8a63e2SSteve Glendinning 
13863c8a63e2SSteve Glendinning 	pr_warn("I2C access timed out, resetting I2C hardware");
13873c8a63e2SSteve Glendinning 	status =  ufx_reg_write(dev, 0x1100, 0x40000000);
13883c8a63e2SSteve Glendinning 	check_warn_return(status, "0x1100 write failed");
13893c8a63e2SSteve Glendinning 
13903c8a63e2SSteve Glendinning 	return -ETIMEDOUT;
13913c8a63e2SSteve Glendinning }
13923c8a63e2SSteve Glendinning 
13933c8a63e2SSteve Glendinning /* reads a 128-byte EDID block from the currently selected port and TAR */
ufx_read_edid(struct ufx_data * dev,u8 * edid,int edid_len)1394261e7676SDan Carpenter static int ufx_read_edid(struct ufx_data *dev, u8 *edid, int edid_len)
13953c8a63e2SSteve Glendinning {
13963c8a63e2SSteve Glendinning 	int i, j, status;
13973c8a63e2SSteve Glendinning 	u32 *edid_u32 = (u32 *)edid;
13983c8a63e2SSteve Glendinning 
13993c8a63e2SSteve Glendinning 	BUG_ON(edid_len != EDID_LENGTH);
14003c8a63e2SSteve Glendinning 
14013c8a63e2SSteve Glendinning 	status = ufx_i2c_configure(dev);
14023c8a63e2SSteve Glendinning 	if (status < 0) {
14033c8a63e2SSteve Glendinning 		pr_err("ufx_i2c_configure failed");
14043c8a63e2SSteve Glendinning 		return status;
14053c8a63e2SSteve Glendinning 	}
14063c8a63e2SSteve Glendinning 
14073c8a63e2SSteve Glendinning 	memset(edid, 0xff, EDID_LENGTH);
14083c8a63e2SSteve Glendinning 
14093c8a63e2SSteve Glendinning 	/* Read the 128-byte EDID as 2 bursts of 64 bytes */
14103c8a63e2SSteve Glendinning 	for (i = 0; i < 2; i++) {
14113c8a63e2SSteve Glendinning 		u32 temp = 0x28070000 | (63 << 20) | (((u32)(i * 64)) << 8);
14123c8a63e2SSteve Glendinning 		status = ufx_reg_write(dev, 0x1100, temp);
14133c8a63e2SSteve Glendinning 		check_warn_return(status, "Failed to write 0x1100");
14143c8a63e2SSteve Glendinning 
14153c8a63e2SSteve Glendinning 		temp |= 0x80000000;
14163c8a63e2SSteve Glendinning 		status = ufx_reg_write(dev, 0x1100, temp);
14173c8a63e2SSteve Glendinning 		check_warn_return(status, "Failed to write 0x1100");
14183c8a63e2SSteve Glendinning 
14193c8a63e2SSteve Glendinning 		status = ufx_i2c_wait_busy(dev);
14203c8a63e2SSteve Glendinning 		check_warn_return(status, "Timeout waiting for I2C BUSY to clear");
14213c8a63e2SSteve Glendinning 
14223c8a63e2SSteve Glendinning 		for (j = 0; j < 16; j++) {
14233c8a63e2SSteve Glendinning 			u32 data_reg_addr = 0x1110 + (j * 4);
14243c8a63e2SSteve Glendinning 			status = ufx_reg_read(dev, data_reg_addr, edid_u32++);
14253c8a63e2SSteve Glendinning 			check_warn_return(status, "Error reading i2c data");
14263c8a63e2SSteve Glendinning 		}
14273c8a63e2SSteve Glendinning 	}
14283c8a63e2SSteve Glendinning 
14293c8a63e2SSteve Glendinning 	/* all FF's in the first 16 bytes indicates nothing is connected */
14303c8a63e2SSteve Glendinning 	for (i = 0; i < 16; i++) {
14313c8a63e2SSteve Glendinning 		if (edid[i] != 0xFF) {
1432ff0c2642SMasanari Iida 			pr_debug("edid data read successfully");
14333c8a63e2SSteve Glendinning 			return EDID_LENGTH;
14343c8a63e2SSteve Glendinning 		}
14353c8a63e2SSteve Glendinning 	}
14363c8a63e2SSteve Glendinning 
14373c8a63e2SSteve Glendinning 	pr_warn("edid data contains all 0xff");
14383c8a63e2SSteve Glendinning 	return -ETIMEDOUT;
14393c8a63e2SSteve Glendinning }
14403c8a63e2SSteve Glendinning 
14413c8a63e2SSteve Glendinning /* 1) use sw default
14423c8a63e2SSteve Glendinning  * 2) Parse into various fb_info structs
14433c8a63e2SSteve Glendinning  * 3) Allocate virtual framebuffer memory to back highest res mode
14443c8a63e2SSteve Glendinning  *
14453c8a63e2SSteve Glendinning  * Parses EDID into three places used by various parts of fbdev:
14463c8a63e2SSteve Glendinning  * fb_var_screeninfo contains the timing of the monitor's preferred mode
14473c8a63e2SSteve Glendinning  * fb_info.monspecs is full parsed EDID info, including monspecs.modedb
14483c8a63e2SSteve Glendinning  * fb_info.modelist is a linked list of all monitor & VESA modes which work
14493c8a63e2SSteve Glendinning  *
14503c8a63e2SSteve Glendinning  * If EDID is not readable/valid, then modelist is all VESA modes,
14513c8a63e2SSteve Glendinning  * monspecs is NULL, and fb_var_screeninfo is set to safe VESA mode
14523c8a63e2SSteve Glendinning  * Returns 0 if successful */
ufx_setup_modes(struct ufx_data * dev,struct fb_info * info,char * default_edid,size_t default_edid_size)14533c8a63e2SSteve Glendinning static int ufx_setup_modes(struct ufx_data *dev, struct fb_info *info,
14543c8a63e2SSteve Glendinning 	char *default_edid, size_t default_edid_size)
14553c8a63e2SSteve Glendinning {
14563c8a63e2SSteve Glendinning 	const struct fb_videomode *default_vmode = NULL;
1457261e7676SDan Carpenter 	u8 *edid;
14583c8a63e2SSteve Glendinning 	int i, result = 0, tries = 3;
14593c8a63e2SSteve Glendinning 
1460f5c6291fSThomas Zimmermann 	if (refcount_read(&info->count)) /* only use mutex if info has been registered */
14613c8a63e2SSteve Glendinning 		mutex_lock(&info->lock);
14623c8a63e2SSteve Glendinning 
14633c8a63e2SSteve Glendinning 	edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
14643c8a63e2SSteve Glendinning 	if (!edid) {
14653c8a63e2SSteve Glendinning 		result = -ENOMEM;
14663c8a63e2SSteve Glendinning 		goto error;
14673c8a63e2SSteve Glendinning 	}
14683c8a63e2SSteve Glendinning 
14693c8a63e2SSteve Glendinning 	fb_destroy_modelist(&info->modelist);
14703c8a63e2SSteve Glendinning 	memset(&info->monspecs, 0, sizeof(info->monspecs));
14713c8a63e2SSteve Glendinning 
14723c8a63e2SSteve Glendinning 	/* Try to (re)read EDID from hardware first
14733c8a63e2SSteve Glendinning 	 * EDID data may return, but not parse as valid
14743c8a63e2SSteve Glendinning 	 * Try again a few times, in case of e.g. analog cable noise */
14753c8a63e2SSteve Glendinning 	while (tries--) {
14763c8a63e2SSteve Glendinning 		i = ufx_read_edid(dev, edid, EDID_LENGTH);
14773c8a63e2SSteve Glendinning 
14783c8a63e2SSteve Glendinning 		if (i >= EDID_LENGTH)
14793c8a63e2SSteve Glendinning 			fb_edid_to_monspecs(edid, &info->monspecs);
14803c8a63e2SSteve Glendinning 
14813c8a63e2SSteve Glendinning 		if (info->monspecs.modedb_len > 0) {
14823c8a63e2SSteve Glendinning 			dev->edid = edid;
14833c8a63e2SSteve Glendinning 			dev->edid_size = i;
14843c8a63e2SSteve Glendinning 			break;
14853c8a63e2SSteve Glendinning 		}
14863c8a63e2SSteve Glendinning 	}
14873c8a63e2SSteve Glendinning 
14883c8a63e2SSteve Glendinning 	/* If that fails, use a previously returned EDID if available */
14893c8a63e2SSteve Glendinning 	if (info->monspecs.modedb_len == 0) {
14903c8a63e2SSteve Glendinning 		pr_err("Unable to get valid EDID from device/display\n");
14913c8a63e2SSteve Glendinning 
14923c8a63e2SSteve Glendinning 		if (dev->edid) {
14933c8a63e2SSteve Glendinning 			fb_edid_to_monspecs(dev->edid, &info->monspecs);
14943c8a63e2SSteve Glendinning 			if (info->monspecs.modedb_len > 0)
14953c8a63e2SSteve Glendinning 				pr_err("Using previously queried EDID\n");
14963c8a63e2SSteve Glendinning 		}
14973c8a63e2SSteve Glendinning 	}
14983c8a63e2SSteve Glendinning 
14993c8a63e2SSteve Glendinning 	/* If that fails, use the default EDID we were handed */
15003c8a63e2SSteve Glendinning 	if (info->monspecs.modedb_len == 0) {
15013c8a63e2SSteve Glendinning 		if (default_edid_size >= EDID_LENGTH) {
15023c8a63e2SSteve Glendinning 			fb_edid_to_monspecs(default_edid, &info->monspecs);
15033c8a63e2SSteve Glendinning 			if (info->monspecs.modedb_len > 0) {
15043c8a63e2SSteve Glendinning 				memcpy(edid, default_edid, default_edid_size);
15053c8a63e2SSteve Glendinning 				dev->edid = edid;
15063c8a63e2SSteve Glendinning 				dev->edid_size = default_edid_size;
15073c8a63e2SSteve Glendinning 				pr_err("Using default/backup EDID\n");
15083c8a63e2SSteve Glendinning 			}
15093c8a63e2SSteve Glendinning 		}
15103c8a63e2SSteve Glendinning 	}
15113c8a63e2SSteve Glendinning 
15123c8a63e2SSteve Glendinning 	/* If we've got modes, let's pick a best default mode */
15133c8a63e2SSteve Glendinning 	if (info->monspecs.modedb_len > 0) {
15143c8a63e2SSteve Glendinning 
15153c8a63e2SSteve Glendinning 		for (i = 0; i < info->monspecs.modedb_len; i++) {
15163c8a63e2SSteve Glendinning 			if (ufx_is_valid_mode(&info->monspecs.modedb[i], info))
15173c8a63e2SSteve Glendinning 				fb_add_videomode(&info->monspecs.modedb[i],
15183c8a63e2SSteve Glendinning 					&info->modelist);
15193c8a63e2SSteve Glendinning 			else /* if we've removed top/best mode */
15203c8a63e2SSteve Glendinning 				info->monspecs.misc &= ~FB_MISC_1ST_DETAIL;
15213c8a63e2SSteve Glendinning 		}
15223c8a63e2SSteve Glendinning 
15233c8a63e2SSteve Glendinning 		default_vmode = fb_find_best_display(&info->monspecs,
15243c8a63e2SSteve Glendinning 						     &info->modelist);
15253c8a63e2SSteve Glendinning 	}
15263c8a63e2SSteve Glendinning 
15273c8a63e2SSteve Glendinning 	/* If everything else has failed, fall back to safe default mode */
15283c8a63e2SSteve Glendinning 	if (default_vmode == NULL) {
15293c8a63e2SSteve Glendinning 
15303c8a63e2SSteve Glendinning 		struct fb_videomode fb_vmode = {0};
15313c8a63e2SSteve Glendinning 
15323c8a63e2SSteve Glendinning 		/* Add the standard VESA modes to our modelist
15333c8a63e2SSteve Glendinning 		 * Since we don't have EDID, there may be modes that
15343c8a63e2SSteve Glendinning 		 * overspec monitor and/or are incorrect aspect ratio, etc.
15353c8a63e2SSteve Glendinning 		 * But at least the user has a chance to choose
15363c8a63e2SSteve Glendinning 		 */
15373c8a63e2SSteve Glendinning 		for (i = 0; i < VESA_MODEDB_SIZE; i++) {
15383c8a63e2SSteve Glendinning 			if (ufx_is_valid_mode((struct fb_videomode *)
15393c8a63e2SSteve Glendinning 						&vesa_modes[i], info))
15403c8a63e2SSteve Glendinning 				fb_add_videomode(&vesa_modes[i],
15413c8a63e2SSteve Glendinning 						 &info->modelist);
15423c8a63e2SSteve Glendinning 		}
15433c8a63e2SSteve Glendinning 
15443c8a63e2SSteve Glendinning 		/* default to resolution safe for projectors
15453c8a63e2SSteve Glendinning 		 * (since they are most common case without EDID)
15463c8a63e2SSteve Glendinning 		 */
15473c8a63e2SSteve Glendinning 		fb_vmode.xres = 800;
15483c8a63e2SSteve Glendinning 		fb_vmode.yres = 600;
15493c8a63e2SSteve Glendinning 		fb_vmode.refresh = 60;
15503c8a63e2SSteve Glendinning 		default_vmode = fb_find_nearest_mode(&fb_vmode,
15513c8a63e2SSteve Glendinning 						     &info->modelist);
15523c8a63e2SSteve Glendinning 	}
15533c8a63e2SSteve Glendinning 
15543c8a63e2SSteve Glendinning 	/* If we have good mode and no active clients */
15553c8a63e2SSteve Glendinning 	if ((default_vmode != NULL) && (dev->fb_count == 0)) {
15563c8a63e2SSteve Glendinning 
15573c8a63e2SSteve Glendinning 		fb_videomode_to_var(&info->var, default_vmode);
15583c8a63e2SSteve Glendinning 		ufx_var_color_format(&info->var);
15593c8a63e2SSteve Glendinning 
15603c8a63e2SSteve Glendinning 		/* with mode size info, we can now alloc our framebuffer */
15613c8a63e2SSteve Glendinning 		memcpy(&info->fix, &ufx_fix, sizeof(ufx_fix));
15623c8a63e2SSteve Glendinning 		info->fix.line_length = info->var.xres *
15633c8a63e2SSteve Glendinning 			(info->var.bits_per_pixel / 8);
15643c8a63e2SSteve Glendinning 
15653c8a63e2SSteve Glendinning 		result = ufx_realloc_framebuffer(dev, info);
15663c8a63e2SSteve Glendinning 
15673c8a63e2SSteve Glendinning 	} else
15683c8a63e2SSteve Glendinning 		result = -EINVAL;
15693c8a63e2SSteve Glendinning 
15703c8a63e2SSteve Glendinning error:
15713c8a63e2SSteve Glendinning 	if (edid && (dev->edid != edid))
15723c8a63e2SSteve Glendinning 		kfree(edid);
15733c8a63e2SSteve Glendinning 
1574f5c6291fSThomas Zimmermann 	if (refcount_read(&info->count))
15753c8a63e2SSteve Glendinning 		mutex_unlock(&info->lock);
15763c8a63e2SSteve Glendinning 
15773c8a63e2SSteve Glendinning 	return result;
15783c8a63e2SSteve Glendinning }
15793c8a63e2SSteve Glendinning 
ufx_usb_probe(struct usb_interface * interface,const struct usb_device_id * id)15803c8a63e2SSteve Glendinning static int ufx_usb_probe(struct usb_interface *interface,
15813c8a63e2SSteve Glendinning 			const struct usb_device_id *id)
15823c8a63e2SSteve Glendinning {
15833c8a63e2SSteve Glendinning 	struct usb_device *usbdev;
15843c8a63e2SSteve Glendinning 	struct ufx_data *dev;
1585daa0524bSMarkus Elfring 	struct fb_info *info;
1586b76449eeSDongliang Mu 	int retval = -ENOMEM;
15873c8a63e2SSteve Glendinning 	u32 id_rev, fpga_rev;
15883c8a63e2SSteve Glendinning 
15893c8a63e2SSteve Glendinning 	/* usb initialization */
15903c8a63e2SSteve Glendinning 	usbdev = interface_to_usbdev(interface);
15913c8a63e2SSteve Glendinning 	BUG_ON(!usbdev);
15923c8a63e2SSteve Glendinning 
15933c8a63e2SSteve Glendinning 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
15943c8a63e2SSteve Glendinning 	if (dev == NULL) {
15953c8a63e2SSteve Glendinning 		dev_err(&usbdev->dev, "ufx_usb_probe: failed alloc of dev struct\n");
1596daa0524bSMarkus Elfring 		return -ENOMEM;
15973c8a63e2SSteve Glendinning 	}
15983c8a63e2SSteve Glendinning 
15993c8a63e2SSteve Glendinning 	/* we need to wait for both usb and fbdev to spin down on disconnect */
16003c8a63e2SSteve Glendinning 	kref_init(&dev->kref); /* matching kref_put in usb .disconnect fn */
16013c8a63e2SSteve Glendinning 	kref_get(&dev->kref); /* matching kref_put in free_framebuffer_work */
16023c8a63e2SSteve Glendinning 
16033c8a63e2SSteve Glendinning 	dev->udev = usbdev;
16043c8a63e2SSteve Glendinning 	dev->gdev = &usbdev->dev; /* our generic struct device * */
16053c8a63e2SSteve Glendinning 	usb_set_intfdata(interface, dev);
16063c8a63e2SSteve Glendinning 
16073c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "%s %s - serial #%s\n",
16083c8a63e2SSteve Glendinning 		usbdev->manufacturer, usbdev->product, usbdev->serial);
16093c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "vid_%04x&pid_%04x&rev_%04x driver's ufx_data struct at %p\n",
161012351855SJohan Hovold 		le16_to_cpu(usbdev->descriptor.idVendor),
161112351855SJohan Hovold 		le16_to_cpu(usbdev->descriptor.idProduct),
161212351855SJohan Hovold 		le16_to_cpu(usbdev->descriptor.bcdDevice), dev);
16133c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "console enable=%d\n", console);
16143c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "fb_defio enable=%d\n", fb_defio);
16153c8a63e2SSteve Glendinning 
16163c8a63e2SSteve Glendinning 	if (!ufx_alloc_urb_list(dev, WRITES_IN_FLIGHT, MAX_TRANSFER)) {
16173c8a63e2SSteve Glendinning 		dev_err(dev->gdev, "ufx_alloc_urb_list failed\n");
1618b76449eeSDongliang Mu 		goto put_ref;
16193c8a63e2SSteve Glendinning 	}
16203c8a63e2SSteve Glendinning 
16213c8a63e2SSteve Glendinning 	/* We don't register a new USB class. Our client interface is fbdev */
16223c8a63e2SSteve Glendinning 
16233c8a63e2SSteve Glendinning 	/* allocates framebuffer driver structure, not framebuffer memory */
16243c8a63e2SSteve Glendinning 	info = framebuffer_alloc(0, &usbdev->dev);
1625b76449eeSDongliang Mu 	if (!info) {
1626b76449eeSDongliang Mu 		dev_err(dev->gdev, "framebuffer_alloc failed\n");
1627b76449eeSDongliang Mu 		goto free_urb_list;
1628b76449eeSDongliang Mu 	}
16293c8a63e2SSteve Glendinning 
16303c8a63e2SSteve Glendinning 	dev->info = info;
16313c8a63e2SSteve Glendinning 	info->par = dev;
16323c8a63e2SSteve Glendinning 	info->pseudo_palette = dev->pseudo_palette;
16333c8a63e2SSteve Glendinning 	info->fbops = &ufx_ops;
16341791f487SWang Hai 	INIT_LIST_HEAD(&info->modelist);
16353c8a63e2SSteve Glendinning 
16363c8a63e2SSteve Glendinning 	retval = fb_alloc_cmap(&info->cmap, 256, 0);
16373c8a63e2SSteve Glendinning 	if (retval < 0) {
16383c8a63e2SSteve Glendinning 		dev_err(dev->gdev, "fb_alloc_cmap failed %x\n", retval);
1639daa0524bSMarkus Elfring 		goto destroy_modedb;
16403c8a63e2SSteve Glendinning 	}
16413c8a63e2SSteve Glendinning 
16423c8a63e2SSteve Glendinning 	retval = ufx_reg_read(dev, 0x3000, &id_rev);
16433c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d reading 0x3000 register from device", retval);
16443c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "ID_REV register value 0x%08x", id_rev);
16453c8a63e2SSteve Glendinning 
16463c8a63e2SSteve Glendinning 	retval = ufx_reg_read(dev, 0x3004, &fpga_rev);
16473c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d reading 0x3004 register from device", retval);
16483c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "FPGA_REV register value 0x%08x", fpga_rev);
16493c8a63e2SSteve Glendinning 
16503c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "resetting device");
16513c8a63e2SSteve Glendinning 	retval = ufx_lite_reset(dev);
16523c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d resetting device", retval);
16533c8a63e2SSteve Glendinning 
16543c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "configuring system clock");
16553c8a63e2SSteve Glendinning 	retval = ufx_config_sys_clk(dev);
16563c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d configuring system clock", retval);
16573c8a63e2SSteve Glendinning 
16583c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "configuring DDR2 controller");
16593c8a63e2SSteve Glendinning 	retval = ufx_config_ddr2(dev);
16603c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d initialising DDR2 controller", retval);
16613c8a63e2SSteve Glendinning 
16623c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "configuring I2C controller");
16633c8a63e2SSteve Glendinning 	retval = ufx_i2c_init(dev);
16643c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "error %d initialising I2C controller", retval);
16653c8a63e2SSteve Glendinning 
16663c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "selecting display mode");
16673c8a63e2SSteve Glendinning 	retval = ufx_setup_modes(dev, info, NULL, 0);
16683c8a63e2SSteve Glendinning 	check_warn_goto_error(retval, "unable to find common mode for display and adapter");
16693c8a63e2SSteve Glendinning 
16703c8a63e2SSteve Glendinning 	retval = ufx_reg_set_bits(dev, 0x4000, 0x00000001);
1671b76449eeSDongliang Mu 	if (retval < 0) {
1672b76449eeSDongliang Mu 		dev_err(dev->gdev, "error %d enabling graphics engine", retval);
1673b76449eeSDongliang Mu 		goto setup_modes;
1674b76449eeSDongliang Mu 	}
16753c8a63e2SSteve Glendinning 
16763c8a63e2SSteve Glendinning 	/* ready to begin using device */
16773c8a63e2SSteve Glendinning 	atomic_set(&dev->usb_active, 1);
16783c8a63e2SSteve Glendinning 
16793c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "checking var");
16803c8a63e2SSteve Glendinning 	retval = ufx_ops_check_var(&info->var, info);
1681b76449eeSDongliang Mu 	if (retval < 0) {
1682b76449eeSDongliang Mu 		dev_err(dev->gdev, "error %d ufx_ops_check_var", retval);
1683b76449eeSDongliang Mu 		goto reset_active;
1684b76449eeSDongliang Mu 	}
16853c8a63e2SSteve Glendinning 
16863c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "setting par");
16873c8a63e2SSteve Glendinning 	retval = ufx_ops_set_par(info);
1688b76449eeSDongliang Mu 	if (retval < 0) {
1689b76449eeSDongliang Mu 		dev_err(dev->gdev, "error %d ufx_ops_set_par", retval);
1690b76449eeSDongliang Mu 		goto reset_active;
1691b76449eeSDongliang Mu 	}
16923c8a63e2SSteve Glendinning 
16933c8a63e2SSteve Glendinning 	dev_dbg(dev->gdev, "registering framebuffer");
16943c8a63e2SSteve Glendinning 	retval = register_framebuffer(info);
1695b76449eeSDongliang Mu 	if (retval < 0) {
1696b76449eeSDongliang Mu 		dev_err(dev->gdev, "error %d register_framebuffer", retval);
1697b76449eeSDongliang Mu 		goto reset_active;
1698b76449eeSDongliang Mu 	}
16993c8a63e2SSteve Glendinning 
17003c8a63e2SSteve Glendinning 	dev_info(dev->gdev, "SMSC UDX USB device /dev/fb%d attached. %dx%d resolution."
17013c8a63e2SSteve Glendinning 		" Using %dK framebuffer memory\n", info->node,
17023c8a63e2SSteve Glendinning 		info->var.xres, info->var.yres, info->fix.smem_len >> 10);
17033c8a63e2SSteve Glendinning 
17043c8a63e2SSteve Glendinning 	return 0;
17053c8a63e2SSteve Glendinning 
1706b76449eeSDongliang Mu reset_active:
1707b76449eeSDongliang Mu 	atomic_set(&dev->usb_active, 0);
1708b76449eeSDongliang Mu setup_modes:
17093c8a63e2SSteve Glendinning 	fb_destroy_modedb(info->monspecs.modedb);
17106ca49268SThomas Zimmermann 	vfree(info->screen_buffer);
17113c8a63e2SSteve Glendinning 	fb_destroy_modelist(&info->modelist);
1712b76449eeSDongliang Mu error:
1713b76449eeSDongliang Mu 	fb_dealloc_cmap(&info->cmap);
1714b76449eeSDongliang Mu destroy_modedb:
17153c8a63e2SSteve Glendinning 	framebuffer_release(info);
1716b76449eeSDongliang Mu free_urb_list:
1717b76449eeSDongliang Mu 	if (dev->urbs.count > 0)
1718b76449eeSDongliang Mu 		ufx_free_urb_list(dev);
1719daa0524bSMarkus Elfring put_ref:
17203c8a63e2SSteve Glendinning 	kref_put(&dev->kref, ufx_free); /* ref for framebuffer */
17213c8a63e2SSteve Glendinning 	kref_put(&dev->kref, ufx_free); /* last ref from kref_init */
17223c8a63e2SSteve Glendinning 	return retval;
17233c8a63e2SSteve Glendinning }
17243c8a63e2SSteve Glendinning 
ufx_usb_disconnect(struct usb_interface * interface)17253c8a63e2SSteve Glendinning static void ufx_usb_disconnect(struct usb_interface *interface)
17263c8a63e2SSteve Glendinning {
17273c8a63e2SSteve Glendinning 	struct ufx_data *dev;
1728cc67482cSHyunwoo Kim 	struct fb_info *info;
17293c8a63e2SSteve Glendinning 
17305610bcfeSHyunwoo Kim 	mutex_lock(&disconnect_mutex);
17315610bcfeSHyunwoo Kim 
17323c8a63e2SSteve Glendinning 	dev = usb_get_intfdata(interface);
1733cc67482cSHyunwoo Kim 	info = dev->info;
17343c8a63e2SSteve Glendinning 
17353c8a63e2SSteve Glendinning 	pr_debug("USB disconnect starting\n");
17363c8a63e2SSteve Glendinning 
17373c8a63e2SSteve Glendinning 	/* we virtualize until all fb clients release. Then we free */
17383c8a63e2SSteve Glendinning 	dev->virtualized = true;
17393c8a63e2SSteve Glendinning 
17403c8a63e2SSteve Glendinning 	/* When non-active we'll update virtual framebuffer, but no new urbs */
17413c8a63e2SSteve Glendinning 	atomic_set(&dev->usb_active, 0);
17423c8a63e2SSteve Glendinning 
17433c8a63e2SSteve Glendinning 	usb_set_intfdata(interface, NULL);
17443c8a63e2SSteve Glendinning 
17453c8a63e2SSteve Glendinning 	/* if clients still have us open, will be freed on last close */
17463c8a63e2SSteve Glendinning 	if (dev->fb_count == 0)
1747cc67482cSHyunwoo Kim 		ufx_free_framebuffer(dev);
17483c8a63e2SSteve Glendinning 
1749cc67482cSHyunwoo Kim 	/* this function will wait for all in-flight urbs to complete */
1750cc67482cSHyunwoo Kim 	if (dev->urbs.count > 0)
1751cc67482cSHyunwoo Kim 		ufx_free_urb_list(dev);
17523c8a63e2SSteve Glendinning 
1753cc67482cSHyunwoo Kim 	pr_debug("freeing ufx_data %p", dev);
1754cc67482cSHyunwoo Kim 
1755cc67482cSHyunwoo Kim 	unregister_framebuffer(info);
17565610bcfeSHyunwoo Kim 
17575610bcfeSHyunwoo Kim 	mutex_unlock(&disconnect_mutex);
17583c8a63e2SSteve Glendinning }
17593c8a63e2SSteve Glendinning 
17603c8a63e2SSteve Glendinning static struct usb_driver ufx_driver = {
17613c8a63e2SSteve Glendinning 	.name = "smscufx",
17623c8a63e2SSteve Glendinning 	.probe = ufx_usb_probe,
17633c8a63e2SSteve Glendinning 	.disconnect = ufx_usb_disconnect,
17643c8a63e2SSteve Glendinning 	.id_table = id_table,
17653c8a63e2SSteve Glendinning };
17663c8a63e2SSteve Glendinning 
1767fe748483SGreg Kroah-Hartman module_usb_driver(ufx_driver);
17683c8a63e2SSteve Glendinning 
ufx_urb_completion(struct urb * urb)17693c8a63e2SSteve Glendinning static void ufx_urb_completion(struct urb *urb)
17703c8a63e2SSteve Glendinning {
17713c8a63e2SSteve Glendinning 	struct urb_node *unode = urb->context;
17723c8a63e2SSteve Glendinning 	struct ufx_data *dev = unode->dev;
17733c8a63e2SSteve Glendinning 	unsigned long flags;
17743c8a63e2SSteve Glendinning 
17753c8a63e2SSteve Glendinning 	/* sync/async unlink faults aren't errors */
17763c8a63e2SSteve Glendinning 	if (urb->status) {
17773c8a63e2SSteve Glendinning 		if (!(urb->status == -ENOENT ||
17783c8a63e2SSteve Glendinning 		    urb->status == -ECONNRESET ||
17793c8a63e2SSteve Glendinning 		    urb->status == -ESHUTDOWN)) {
17803c8a63e2SSteve Glendinning 			pr_err("%s - nonzero write bulk status received: %d\n",
17813c8a63e2SSteve Glendinning 				__func__, urb->status);
17823c8a63e2SSteve Glendinning 			atomic_set(&dev->lost_pixels, 1);
17833c8a63e2SSteve Glendinning 		}
17843c8a63e2SSteve Glendinning 	}
17853c8a63e2SSteve Glendinning 
17863c8a63e2SSteve Glendinning 	urb->transfer_buffer_length = dev->urbs.size; /* reset to actual */
17873c8a63e2SSteve Glendinning 
17883c8a63e2SSteve Glendinning 	spin_lock_irqsave(&dev->urbs.lock, flags);
17893c8a63e2SSteve Glendinning 	list_add_tail(&unode->entry, &dev->urbs.list);
17903c8a63e2SSteve Glendinning 	dev->urbs.available++;
17913c8a63e2SSteve Glendinning 	spin_unlock_irqrestore(&dev->urbs.lock, flags);
17923c8a63e2SSteve Glendinning 
17933c8a63e2SSteve Glendinning 	/* When using fb_defio, we deadlock if up() is called
17943c8a63e2SSteve Glendinning 	 * while another is waiting. So queue to another process */
17953c8a63e2SSteve Glendinning 	if (fb_defio)
17963c8a63e2SSteve Glendinning 		schedule_delayed_work(&unode->release_urb_work, 0);
17973c8a63e2SSteve Glendinning 	else
17983c8a63e2SSteve Glendinning 		up(&dev->urbs.limit_sem);
17993c8a63e2SSteve Glendinning }
18003c8a63e2SSteve Glendinning 
ufx_free_urb_list(struct ufx_data * dev)18013c8a63e2SSteve Glendinning static void ufx_free_urb_list(struct ufx_data *dev)
18023c8a63e2SSteve Glendinning {
18033c8a63e2SSteve Glendinning 	int count = dev->urbs.count;
18043c8a63e2SSteve Glendinning 	struct list_head *node;
18053c8a63e2SSteve Glendinning 	struct urb_node *unode;
18063c8a63e2SSteve Glendinning 	struct urb *urb;
18073c8a63e2SSteve Glendinning 	int ret;
18083c8a63e2SSteve Glendinning 	unsigned long flags;
18093c8a63e2SSteve Glendinning 
18103c8a63e2SSteve Glendinning 	pr_debug("Waiting for completes and freeing all render urbs\n");
18113c8a63e2SSteve Glendinning 
18123c8a63e2SSteve Glendinning 	/* keep waiting and freeing, until we've got 'em all */
18133c8a63e2SSteve Glendinning 	while (count--) {
18143c8a63e2SSteve Glendinning 		/* Getting interrupted means a leak, but ok at shutdown*/
18153c8a63e2SSteve Glendinning 		ret = down_interruptible(&dev->urbs.limit_sem);
18163c8a63e2SSteve Glendinning 		if (ret)
18173c8a63e2SSteve Glendinning 			break;
18183c8a63e2SSteve Glendinning 
18193c8a63e2SSteve Glendinning 		spin_lock_irqsave(&dev->urbs.lock, flags);
18203c8a63e2SSteve Glendinning 
18213c8a63e2SSteve Glendinning 		node = dev->urbs.list.next; /* have reserved one with sem */
18223c8a63e2SSteve Glendinning 		list_del_init(node);
18233c8a63e2SSteve Glendinning 
18243c8a63e2SSteve Glendinning 		spin_unlock_irqrestore(&dev->urbs.lock, flags);
18253c8a63e2SSteve Glendinning 
18263c8a63e2SSteve Glendinning 		unode = list_entry(node, struct urb_node, entry);
18273c8a63e2SSteve Glendinning 		urb = unode->urb;
18283c8a63e2SSteve Glendinning 
18293c8a63e2SSteve Glendinning 		/* Free each separately allocated piece */
18303c8a63e2SSteve Glendinning 		usb_free_coherent(urb->dev, dev->urbs.size,
18313c8a63e2SSteve Glendinning 				  urb->transfer_buffer, urb->transfer_dma);
18323c8a63e2SSteve Glendinning 		usb_free_urb(urb);
18333c8a63e2SSteve Glendinning 		kfree(node);
18343c8a63e2SSteve Glendinning 	}
18353c8a63e2SSteve Glendinning }
18363c8a63e2SSteve Glendinning 
ufx_alloc_urb_list(struct ufx_data * dev,int count,size_t size)18373c8a63e2SSteve Glendinning static int ufx_alloc_urb_list(struct ufx_data *dev, int count, size_t size)
18383c8a63e2SSteve Glendinning {
18393c8a63e2SSteve Glendinning 	int i = 0;
18403c8a63e2SSteve Glendinning 	struct urb *urb;
18413c8a63e2SSteve Glendinning 	struct urb_node *unode;
18423c8a63e2SSteve Glendinning 	char *buf;
18433c8a63e2SSteve Glendinning 
18443c8a63e2SSteve Glendinning 	spin_lock_init(&dev->urbs.lock);
18453c8a63e2SSteve Glendinning 
18463c8a63e2SSteve Glendinning 	dev->urbs.size = size;
18473c8a63e2SSteve Glendinning 	INIT_LIST_HEAD(&dev->urbs.list);
18483c8a63e2SSteve Glendinning 
18493c8a63e2SSteve Glendinning 	while (i < count) {
1850defddeffSMarkus Elfring 		unode = kzalloc(sizeof(*unode), GFP_KERNEL);
18513c8a63e2SSteve Glendinning 		if (!unode)
18523c8a63e2SSteve Glendinning 			break;
18533c8a63e2SSteve Glendinning 		unode->dev = dev;
18543c8a63e2SSteve Glendinning 
18553c8a63e2SSteve Glendinning 		INIT_DELAYED_WORK(&unode->release_urb_work,
18563c8a63e2SSteve Glendinning 			  ufx_release_urb_work);
18573c8a63e2SSteve Glendinning 
18583c8a63e2SSteve Glendinning 		urb = usb_alloc_urb(0, GFP_KERNEL);
18593c8a63e2SSteve Glendinning 		if (!urb) {
18603c8a63e2SSteve Glendinning 			kfree(unode);
18613c8a63e2SSteve Glendinning 			break;
18623c8a63e2SSteve Glendinning 		}
18633c8a63e2SSteve Glendinning 		unode->urb = urb;
18643c8a63e2SSteve Glendinning 
18653c8a63e2SSteve Glendinning 		buf = usb_alloc_coherent(dev->udev, size, GFP_KERNEL,
18663c8a63e2SSteve Glendinning 					 &urb->transfer_dma);
18673c8a63e2SSteve Glendinning 		if (!buf) {
18683c8a63e2SSteve Glendinning 			kfree(unode);
18693c8a63e2SSteve Glendinning 			usb_free_urb(urb);
18703c8a63e2SSteve Glendinning 			break;
18713c8a63e2SSteve Glendinning 		}
18723c8a63e2SSteve Glendinning 
18733c8a63e2SSteve Glendinning 		/* urb->transfer_buffer_length set to actual before submit */
18743c8a63e2SSteve Glendinning 		usb_fill_bulk_urb(urb, dev->udev, usb_sndbulkpipe(dev->udev, 1),
18753c8a63e2SSteve Glendinning 			buf, size, ufx_urb_completion, unode);
18763c8a63e2SSteve Glendinning 		urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
18773c8a63e2SSteve Glendinning 
18783c8a63e2SSteve Glendinning 		list_add_tail(&unode->entry, &dev->urbs.list);
18793c8a63e2SSteve Glendinning 
18803c8a63e2SSteve Glendinning 		i++;
18813c8a63e2SSteve Glendinning 	}
18823c8a63e2SSteve Glendinning 
18833c8a63e2SSteve Glendinning 	sema_init(&dev->urbs.limit_sem, i);
18843c8a63e2SSteve Glendinning 	dev->urbs.count = i;
18853c8a63e2SSteve Glendinning 	dev->urbs.available = i;
18863c8a63e2SSteve Glendinning 
18873c8a63e2SSteve Glendinning 	pr_debug("allocated %d %d byte urbs\n", i, (int) size);
18883c8a63e2SSteve Glendinning 
18893c8a63e2SSteve Glendinning 	return i;
18903c8a63e2SSteve Glendinning }
18913c8a63e2SSteve Glendinning 
ufx_get_urb(struct ufx_data * dev)18923c8a63e2SSteve Glendinning static struct urb *ufx_get_urb(struct ufx_data *dev)
18933c8a63e2SSteve Glendinning {
18943c8a63e2SSteve Glendinning 	int ret = 0;
18953c8a63e2SSteve Glendinning 	struct list_head *entry;
18963c8a63e2SSteve Glendinning 	struct urb_node *unode;
18973c8a63e2SSteve Glendinning 	struct urb *urb = NULL;
18983c8a63e2SSteve Glendinning 	unsigned long flags;
18993c8a63e2SSteve Glendinning 
19003c8a63e2SSteve Glendinning 	/* Wait for an in-flight buffer to complete and get re-queued */
19013c8a63e2SSteve Glendinning 	ret = down_timeout(&dev->urbs.limit_sem, GET_URB_TIMEOUT);
19023c8a63e2SSteve Glendinning 	if (ret) {
19033c8a63e2SSteve Glendinning 		atomic_set(&dev->lost_pixels, 1);
19043c8a63e2SSteve Glendinning 		pr_warn("wait for urb interrupted: %x available: %d\n",
19053c8a63e2SSteve Glendinning 		       ret, dev->urbs.available);
19063c8a63e2SSteve Glendinning 		goto error;
19073c8a63e2SSteve Glendinning 	}
19083c8a63e2SSteve Glendinning 
19093c8a63e2SSteve Glendinning 	spin_lock_irqsave(&dev->urbs.lock, flags);
19103c8a63e2SSteve Glendinning 
19113c8a63e2SSteve Glendinning 	BUG_ON(list_empty(&dev->urbs.list)); /* reserved one with limit_sem */
19123c8a63e2SSteve Glendinning 	entry = dev->urbs.list.next;
19133c8a63e2SSteve Glendinning 	list_del_init(entry);
19143c8a63e2SSteve Glendinning 	dev->urbs.available--;
19153c8a63e2SSteve Glendinning 
19163c8a63e2SSteve Glendinning 	spin_unlock_irqrestore(&dev->urbs.lock, flags);
19173c8a63e2SSteve Glendinning 
19183c8a63e2SSteve Glendinning 	unode = list_entry(entry, struct urb_node, entry);
19193c8a63e2SSteve Glendinning 	urb = unode->urb;
19203c8a63e2SSteve Glendinning 
19213c8a63e2SSteve Glendinning error:
19223c8a63e2SSteve Glendinning 	return urb;
19233c8a63e2SSteve Glendinning }
19243c8a63e2SSteve Glendinning 
ufx_submit_urb(struct ufx_data * dev,struct urb * urb,size_t len)19253c8a63e2SSteve Glendinning static int ufx_submit_urb(struct ufx_data *dev, struct urb *urb, size_t len)
19263c8a63e2SSteve Glendinning {
19273c8a63e2SSteve Glendinning 	int ret;
19283c8a63e2SSteve Glendinning 
19293c8a63e2SSteve Glendinning 	BUG_ON(len > dev->urbs.size);
19303c8a63e2SSteve Glendinning 
19313c8a63e2SSteve Glendinning 	urb->transfer_buffer_length = len; /* set to actual payload len */
19323c8a63e2SSteve Glendinning 	ret = usb_submit_urb(urb, GFP_KERNEL);
19333c8a63e2SSteve Glendinning 	if (ret) {
19343c8a63e2SSteve Glendinning 		ufx_urb_completion(urb); /* because no one else will */
19353c8a63e2SSteve Glendinning 		atomic_set(&dev->lost_pixels, 1);
19363c8a63e2SSteve Glendinning 		pr_err("usb_submit_urb error %x\n", ret);
19373c8a63e2SSteve Glendinning 	}
19383c8a63e2SSteve Glendinning 	return ret;
19393c8a63e2SSteve Glendinning }
19403c8a63e2SSteve Glendinning 
19413c8a63e2SSteve Glendinning module_param(console, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
19423c8a63e2SSteve Glendinning MODULE_PARM_DESC(console, "Allow fbcon to be used on this display");
19433c8a63e2SSteve Glendinning 
19443c8a63e2SSteve Glendinning module_param(fb_defio, bool, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
19453c8a63e2SSteve Glendinning MODULE_PARM_DESC(fb_defio, "Enable fb_defio mmap support");
19463c8a63e2SSteve Glendinning 
194790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
19483c8a63e2SSteve Glendinning MODULE_DESCRIPTION("SMSC UFX kernel framebuffer driver");
19493c8a63e2SSteve Glendinning MODULE_LICENSE("GPL");
1950