18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* linux/drivers/video/s3c-fb.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2008 Openmoko Inc.
58c2ecf20Sopenharmony_ci * Copyright 2008-2010 Simtec Electronics
68c2ecf20Sopenharmony_ci *      Ben Dooks <ben@simtec.co.uk>
78c2ecf20Sopenharmony_ci *      http://armlinux.simtec.co.uk/
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Samsung SoC Framebuffer driver
108c2ecf20Sopenharmony_ci*/
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/clk.h>
198c2ecf20Sopenharmony_ci#include <linux/fb.h>
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
228c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
238c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
248c2ecf20Sopenharmony_ci#include <linux/platform_data/video_s3c.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <video/samsung_fimd.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* This driver will export a number of framebuffer interfaces depending
298c2ecf20Sopenharmony_ci * on the configuration passed in via the platform data. Each fb instance
308c2ecf20Sopenharmony_ci * maps to a hardware window. Currently there is no support for runtime
318c2ecf20Sopenharmony_ci * setting of the alpha-blending functions that each window has, so only
328c2ecf20Sopenharmony_ci * window 0 is actually useful.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * Window 0 is treated specially, it is used for the basis of the LCD
358c2ecf20Sopenharmony_ci * output timings and as the control for the output power-down state.
368c2ecf20Sopenharmony_ci*/
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* note, the previous use of <mach/regs-fb.h> to get platform specific data
398c2ecf20Sopenharmony_ci * has been replaced by using the platform device name to pick the correct
408c2ecf20Sopenharmony_ci * configuration data for the system.
418c2ecf20Sopenharmony_ci*/
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_S3C_DEBUG_REGWRITE
448c2ecf20Sopenharmony_ci#undef writel
458c2ecf20Sopenharmony_ci#define writel(v, r) do { \
468c2ecf20Sopenharmony_ci	pr_debug("%s: %08x => %p\n", __func__, (unsigned int)v, r); \
478c2ecf20Sopenharmony_ci	__raw_writel(v, r); \
488c2ecf20Sopenharmony_ci} while (0)
498c2ecf20Sopenharmony_ci#endif /* FB_S3C_DEBUG_REGWRITE */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* irq_flags bits */
528c2ecf20Sopenharmony_ci#define S3C_FB_VSYNC_IRQ_EN	0
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define VSYNC_TIMEOUT_MSEC 50
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistruct s3c_fb;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define VALID_BPP(x) (1 << ((x) - 1))
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define OSD_BASE(win, variant) ((variant).osd + ((win) * (variant).osd_stride))
618c2ecf20Sopenharmony_ci#define VIDOSD_A(win, variant) (OSD_BASE(win, variant) + 0x00)
628c2ecf20Sopenharmony_ci#define VIDOSD_B(win, variant) (OSD_BASE(win, variant) + 0x04)
638c2ecf20Sopenharmony_ci#define VIDOSD_C(win, variant) (OSD_BASE(win, variant) + 0x08)
648c2ecf20Sopenharmony_ci#define VIDOSD_D(win, variant) (OSD_BASE(win, variant) + 0x0C)
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/**
678c2ecf20Sopenharmony_ci * struct s3c_fb_variant - fb variant information
688c2ecf20Sopenharmony_ci * @is_2443: Set if S3C2443/S3C2416 style hardware.
698c2ecf20Sopenharmony_ci * @nr_windows: The number of windows.
708c2ecf20Sopenharmony_ci * @vidtcon: The base for the VIDTCONx registers
718c2ecf20Sopenharmony_ci * @wincon: The base for the WINxCON registers.
728c2ecf20Sopenharmony_ci * @winmap: The base for the WINxMAP registers.
738c2ecf20Sopenharmony_ci * @keycon: The abse for the WxKEYCON registers.
748c2ecf20Sopenharmony_ci * @buf_start: Offset of buffer start registers.
758c2ecf20Sopenharmony_ci * @buf_size: Offset of buffer size registers.
768c2ecf20Sopenharmony_ci * @buf_end: Offset of buffer end registers.
778c2ecf20Sopenharmony_ci * @osd: The base for the OSD registers.
788c2ecf20Sopenharmony_ci * @palette: Address of palette memory, or 0 if none.
798c2ecf20Sopenharmony_ci * @has_prtcon: Set if has PRTCON register.
808c2ecf20Sopenharmony_ci * @has_shadowcon: Set if has SHADOWCON register.
818c2ecf20Sopenharmony_ci * @has_blendcon: Set if has BLENDCON register.
828c2ecf20Sopenharmony_ci * @has_clksel: Set if VIDCON0 register has CLKSEL bit.
838c2ecf20Sopenharmony_ci * @has_fixvclk: Set if VIDCON1 register has FIXVCLK bits.
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_cistruct s3c_fb_variant {
868c2ecf20Sopenharmony_ci	unsigned int	is_2443:1;
878c2ecf20Sopenharmony_ci	unsigned short	nr_windows;
888c2ecf20Sopenharmony_ci	unsigned int	vidtcon;
898c2ecf20Sopenharmony_ci	unsigned short	wincon;
908c2ecf20Sopenharmony_ci	unsigned short	winmap;
918c2ecf20Sopenharmony_ci	unsigned short	keycon;
928c2ecf20Sopenharmony_ci	unsigned short	buf_start;
938c2ecf20Sopenharmony_ci	unsigned short	buf_end;
948c2ecf20Sopenharmony_ci	unsigned short	buf_size;
958c2ecf20Sopenharmony_ci	unsigned short	osd;
968c2ecf20Sopenharmony_ci	unsigned short	osd_stride;
978c2ecf20Sopenharmony_ci	unsigned short	palette[S3C_FB_MAX_WIN];
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	unsigned int	has_prtcon:1;
1008c2ecf20Sopenharmony_ci	unsigned int	has_shadowcon:1;
1018c2ecf20Sopenharmony_ci	unsigned int	has_blendcon:1;
1028c2ecf20Sopenharmony_ci	unsigned int	has_clksel:1;
1038c2ecf20Sopenharmony_ci	unsigned int	has_fixvclk:1;
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/**
1078c2ecf20Sopenharmony_ci * struct s3c_fb_win_variant
1088c2ecf20Sopenharmony_ci * @has_osd_c: Set if has OSD C register.
1098c2ecf20Sopenharmony_ci * @has_osd_d: Set if has OSD D register.
1108c2ecf20Sopenharmony_ci * @has_osd_alpha: Set if can change alpha transparency for a window.
1118c2ecf20Sopenharmony_ci * @palette_sz: Size of palette in entries.
1128c2ecf20Sopenharmony_ci * @palette_16bpp: Set if palette is 16bits wide.
1138c2ecf20Sopenharmony_ci * @osd_size_off: If != 0, supports setting up OSD for a window; the appropriate
1148c2ecf20Sopenharmony_ci *                register is located at the given offset from OSD_BASE.
1158c2ecf20Sopenharmony_ci * @valid_bpp: 1 bit per BPP setting to show valid bits-per-pixel.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * valid_bpp bit x is set if (x+1)BPP is supported.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistruct s3c_fb_win_variant {
1208c2ecf20Sopenharmony_ci	unsigned int	has_osd_c:1;
1218c2ecf20Sopenharmony_ci	unsigned int	has_osd_d:1;
1228c2ecf20Sopenharmony_ci	unsigned int	has_osd_alpha:1;
1238c2ecf20Sopenharmony_ci	unsigned int	palette_16bpp:1;
1248c2ecf20Sopenharmony_ci	unsigned short	osd_size_off;
1258c2ecf20Sopenharmony_ci	unsigned short	palette_sz;
1268c2ecf20Sopenharmony_ci	u32		valid_bpp;
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/**
1308c2ecf20Sopenharmony_ci * struct s3c_fb_driverdata - per-device type driver data for init time.
1318c2ecf20Sopenharmony_ci * @variant: The variant information for this driver.
1328c2ecf20Sopenharmony_ci * @win: The window information for each window.
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_cistruct s3c_fb_driverdata {
1358c2ecf20Sopenharmony_ci	struct s3c_fb_variant	variant;
1368c2ecf20Sopenharmony_ci	struct s3c_fb_win_variant *win[S3C_FB_MAX_WIN];
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/**
1408c2ecf20Sopenharmony_ci * struct s3c_fb_palette - palette information
1418c2ecf20Sopenharmony_ci * @r: Red bitfield.
1428c2ecf20Sopenharmony_ci * @g: Green bitfield.
1438c2ecf20Sopenharmony_ci * @b: Blue bitfield.
1448c2ecf20Sopenharmony_ci * @a: Alpha bitfield.
1458c2ecf20Sopenharmony_ci */
1468c2ecf20Sopenharmony_cistruct s3c_fb_palette {
1478c2ecf20Sopenharmony_ci	struct fb_bitfield	r;
1488c2ecf20Sopenharmony_ci	struct fb_bitfield	g;
1498c2ecf20Sopenharmony_ci	struct fb_bitfield	b;
1508c2ecf20Sopenharmony_ci	struct fb_bitfield	a;
1518c2ecf20Sopenharmony_ci};
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/**
1548c2ecf20Sopenharmony_ci * struct s3c_fb_win - per window private data for each framebuffer.
1558c2ecf20Sopenharmony_ci * @windata: The platform data supplied for the window configuration.
1568c2ecf20Sopenharmony_ci * @parent: The hardware that this window is part of.
1578c2ecf20Sopenharmony_ci * @fbinfo: Pointer pack to the framebuffer info for this window.
1588c2ecf20Sopenharmony_ci * @varint: The variant information for this window.
1598c2ecf20Sopenharmony_ci * @palette_buffer: Buffer/cache to hold palette entries.
1608c2ecf20Sopenharmony_ci * @pseudo_palette: For use in TRUECOLOUR modes for entries 0..15/
1618c2ecf20Sopenharmony_ci * @index: The window number of this window.
1628c2ecf20Sopenharmony_ci * @palette: The bitfields for changing r/g/b into a hardware palette entry.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistruct s3c_fb_win {
1658c2ecf20Sopenharmony_ci	struct s3c_fb_pd_win	*windata;
1668c2ecf20Sopenharmony_ci	struct s3c_fb		*parent;
1678c2ecf20Sopenharmony_ci	struct fb_info		*fbinfo;
1688c2ecf20Sopenharmony_ci	struct s3c_fb_palette	 palette;
1698c2ecf20Sopenharmony_ci	struct s3c_fb_win_variant variant;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	u32			*palette_buffer;
1728c2ecf20Sopenharmony_ci	u32			 pseudo_palette[16];
1738c2ecf20Sopenharmony_ci	unsigned int		 index;
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/**
1778c2ecf20Sopenharmony_ci * struct s3c_fb_vsync - vsync information
1788c2ecf20Sopenharmony_ci * @wait:	a queue for processes waiting for vsync
1798c2ecf20Sopenharmony_ci * @count:	vsync interrupt count
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_cistruct s3c_fb_vsync {
1828c2ecf20Sopenharmony_ci	wait_queue_head_t	wait;
1838c2ecf20Sopenharmony_ci	unsigned int		count;
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci/**
1878c2ecf20Sopenharmony_ci * struct s3c_fb - overall hardware state of the hardware
1888c2ecf20Sopenharmony_ci * @slock: The spinlock protection for this data structure.
1898c2ecf20Sopenharmony_ci * @dev: The device that we bound to, for printing, etc.
1908c2ecf20Sopenharmony_ci * @bus_clk: The clk (hclk) feeding our interface and possibly pixclk.
1918c2ecf20Sopenharmony_ci * @lcd_clk: The clk (sclk) feeding pixclk.
1928c2ecf20Sopenharmony_ci * @regs: The mapped hardware registers.
1938c2ecf20Sopenharmony_ci * @variant: Variant information for this hardware.
1948c2ecf20Sopenharmony_ci * @enabled: A bitmask of enabled hardware windows.
1958c2ecf20Sopenharmony_ci * @output_on: Flag if the physical output is enabled.
1968c2ecf20Sopenharmony_ci * @pdata: The platform configuration data passed with the device.
1978c2ecf20Sopenharmony_ci * @windows: The hardware windows that have been claimed.
1988c2ecf20Sopenharmony_ci * @irq_no: IRQ line number
1998c2ecf20Sopenharmony_ci * @irq_flags: irq flags
2008c2ecf20Sopenharmony_ci * @vsync_info: VSYNC-related information (count, queues...)
2018c2ecf20Sopenharmony_ci */
2028c2ecf20Sopenharmony_cistruct s3c_fb {
2038c2ecf20Sopenharmony_ci	spinlock_t		slock;
2048c2ecf20Sopenharmony_ci	struct device		*dev;
2058c2ecf20Sopenharmony_ci	struct clk		*bus_clk;
2068c2ecf20Sopenharmony_ci	struct clk		*lcd_clk;
2078c2ecf20Sopenharmony_ci	void __iomem		*regs;
2088c2ecf20Sopenharmony_ci	struct s3c_fb_variant	 variant;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	unsigned char		 enabled;
2118c2ecf20Sopenharmony_ci	bool			 output_on;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	struct s3c_fb_platdata	*pdata;
2148c2ecf20Sopenharmony_ci	struct s3c_fb_win	*windows[S3C_FB_MAX_WIN];
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	int			 irq_no;
2178c2ecf20Sopenharmony_ci	unsigned long		 irq_flags;
2188c2ecf20Sopenharmony_ci	struct s3c_fb_vsync	 vsync_info;
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/**
2228c2ecf20Sopenharmony_ci * s3c_fb_validate_win_bpp - validate the bits-per-pixel for this mode.
2238c2ecf20Sopenharmony_ci * @win: The device window.
2248c2ecf20Sopenharmony_ci * @bpp: The bit depth.
2258c2ecf20Sopenharmony_ci */
2268c2ecf20Sopenharmony_cistatic bool s3c_fb_validate_win_bpp(struct s3c_fb_win *win, unsigned int bpp)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	return win->variant.valid_bpp & VALID_BPP(bpp);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/**
2328c2ecf20Sopenharmony_ci * s3c_fb_check_var() - framebuffer layer request to verify a given mode.
2338c2ecf20Sopenharmony_ci * @var: The screen information to verify.
2348c2ecf20Sopenharmony_ci * @info: The framebuffer device.
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * Framebuffer layer call to verify the given information and allow us to
2378c2ecf20Sopenharmony_ci * update various information depending on the hardware capabilities.
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_cistatic int s3c_fb_check_var(struct fb_var_screeninfo *var,
2408c2ecf20Sopenharmony_ci			    struct fb_info *info)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	struct s3c_fb_win *win = info->par;
2438c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "checking parameters\n");
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	var->xres_virtual = max(var->xres_virtual, var->xres);
2488c2ecf20Sopenharmony_ci	var->yres_virtual = max(var->yres_virtual, var->yres);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!s3c_fb_validate_win_bpp(win, var->bits_per_pixel)) {
2518c2ecf20Sopenharmony_ci		dev_dbg(sfb->dev, "win %d: unsupported bpp %d\n",
2528c2ecf20Sopenharmony_ci			win->index, var->bits_per_pixel);
2538c2ecf20Sopenharmony_ci		return -EINVAL;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/* always ensure these are zero, for drop through cases below */
2578c2ecf20Sopenharmony_ci	var->transp.offset = 0;
2588c2ecf20Sopenharmony_ci	var->transp.length = 0;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	switch (var->bits_per_pixel) {
2618c2ecf20Sopenharmony_ci	case 1:
2628c2ecf20Sopenharmony_ci	case 2:
2638c2ecf20Sopenharmony_ci	case 4:
2648c2ecf20Sopenharmony_ci	case 8:
2658c2ecf20Sopenharmony_ci		if (sfb->variant.palette[win->index] != 0) {
2668c2ecf20Sopenharmony_ci			/* non palletised, A:1,R:2,G:3,B:2 mode */
2678c2ecf20Sopenharmony_ci			var->red.offset		= 5;
2688c2ecf20Sopenharmony_ci			var->green.offset	= 2;
2698c2ecf20Sopenharmony_ci			var->blue.offset	= 0;
2708c2ecf20Sopenharmony_ci			var->red.length		= 2;
2718c2ecf20Sopenharmony_ci			var->green.length	= 3;
2728c2ecf20Sopenharmony_ci			var->blue.length	= 2;
2738c2ecf20Sopenharmony_ci			var->transp.offset	= 7;
2748c2ecf20Sopenharmony_ci			var->transp.length	= 1;
2758c2ecf20Sopenharmony_ci		} else {
2768c2ecf20Sopenharmony_ci			var->red.offset	= 0;
2778c2ecf20Sopenharmony_ci			var->red.length	= var->bits_per_pixel;
2788c2ecf20Sopenharmony_ci			var->green	= var->red;
2798c2ecf20Sopenharmony_ci			var->blue	= var->red;
2808c2ecf20Sopenharmony_ci		}
2818c2ecf20Sopenharmony_ci		break;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	case 19:
2848c2ecf20Sopenharmony_ci		/* 666 with one bit alpha/transparency */
2858c2ecf20Sopenharmony_ci		var->transp.offset	= 18;
2868c2ecf20Sopenharmony_ci		var->transp.length	= 1;
2878c2ecf20Sopenharmony_ci		fallthrough;
2888c2ecf20Sopenharmony_ci	case 18:
2898c2ecf20Sopenharmony_ci		var->bits_per_pixel	= 32;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		/* 666 format */
2928c2ecf20Sopenharmony_ci		var->red.offset		= 12;
2938c2ecf20Sopenharmony_ci		var->green.offset	= 6;
2948c2ecf20Sopenharmony_ci		var->blue.offset	= 0;
2958c2ecf20Sopenharmony_ci		var->red.length		= 6;
2968c2ecf20Sopenharmony_ci		var->green.length	= 6;
2978c2ecf20Sopenharmony_ci		var->blue.length	= 6;
2988c2ecf20Sopenharmony_ci		break;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	case 16:
3018c2ecf20Sopenharmony_ci		/* 16 bpp, 565 format */
3028c2ecf20Sopenharmony_ci		var->red.offset		= 11;
3038c2ecf20Sopenharmony_ci		var->green.offset	= 5;
3048c2ecf20Sopenharmony_ci		var->blue.offset	= 0;
3058c2ecf20Sopenharmony_ci		var->red.length		= 5;
3068c2ecf20Sopenharmony_ci		var->green.length	= 6;
3078c2ecf20Sopenharmony_ci		var->blue.length	= 5;
3088c2ecf20Sopenharmony_ci		break;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	case 32:
3118c2ecf20Sopenharmony_ci	case 28:
3128c2ecf20Sopenharmony_ci	case 25:
3138c2ecf20Sopenharmony_ci		var->transp.length	= var->bits_per_pixel - 24;
3148c2ecf20Sopenharmony_ci		var->transp.offset	= 24;
3158c2ecf20Sopenharmony_ci		fallthrough;
3168c2ecf20Sopenharmony_ci	case 24:
3178c2ecf20Sopenharmony_ci		/* our 24bpp is unpacked, so 32bpp */
3188c2ecf20Sopenharmony_ci		var->bits_per_pixel	= 32;
3198c2ecf20Sopenharmony_ci		var->red.offset		= 16;
3208c2ecf20Sopenharmony_ci		var->red.length		= 8;
3218c2ecf20Sopenharmony_ci		var->green.offset	= 8;
3228c2ecf20Sopenharmony_ci		var->green.length	= 8;
3238c2ecf20Sopenharmony_ci		var->blue.offset	= 0;
3248c2ecf20Sopenharmony_ci		var->blue.length	= 8;
3258c2ecf20Sopenharmony_ci		break;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	default:
3288c2ecf20Sopenharmony_ci		dev_err(sfb->dev, "invalid bpp\n");
3298c2ecf20Sopenharmony_ci		return -EINVAL;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "%s: verified parameters\n", __func__);
3338c2ecf20Sopenharmony_ci	return 0;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/**
3378c2ecf20Sopenharmony_ci * s3c_fb_calc_pixclk() - calculate the divider to create the pixel clock.
3388c2ecf20Sopenharmony_ci * @sfb: The hardware state.
3398c2ecf20Sopenharmony_ci * @pixclock: The pixel clock wanted, in picoseconds.
3408c2ecf20Sopenharmony_ci *
3418c2ecf20Sopenharmony_ci * Given the specified pixel clock, work out the necessary divider to get
3428c2ecf20Sopenharmony_ci * close to the output frequency.
3438c2ecf20Sopenharmony_ci */
3448c2ecf20Sopenharmony_cistatic int s3c_fb_calc_pixclk(struct s3c_fb *sfb, unsigned int pixclk)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	unsigned long clk;
3478c2ecf20Sopenharmony_ci	unsigned long long tmp;
3488c2ecf20Sopenharmony_ci	unsigned int result;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (sfb->variant.has_clksel)
3518c2ecf20Sopenharmony_ci		clk = clk_get_rate(sfb->bus_clk);
3528c2ecf20Sopenharmony_ci	else
3538c2ecf20Sopenharmony_ci		clk = clk_get_rate(sfb->lcd_clk);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	tmp = (unsigned long long)clk;
3568c2ecf20Sopenharmony_ci	tmp *= pixclk;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	do_div(tmp, 1000000000UL);
3598c2ecf20Sopenharmony_ci	result = (unsigned int)tmp / 1000;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "pixclk=%u, clk=%lu, div=%d (%lu)\n",
3628c2ecf20Sopenharmony_ci		pixclk, clk, result, result ? clk / result : clk);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return result;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci/**
3688c2ecf20Sopenharmony_ci * s3c_fb_align_word() - align pixel count to word boundary
3698c2ecf20Sopenharmony_ci * @bpp: The number of bits per pixel
3708c2ecf20Sopenharmony_ci * @pix: The value to be aligned.
3718c2ecf20Sopenharmony_ci *
3728c2ecf20Sopenharmony_ci * Align the given pixel count so that it will start on an 32bit word
3738c2ecf20Sopenharmony_ci * boundary.
3748c2ecf20Sopenharmony_ci */
3758c2ecf20Sopenharmony_cistatic int s3c_fb_align_word(unsigned int bpp, unsigned int pix)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	int pix_per_word;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	if (bpp > 16)
3808c2ecf20Sopenharmony_ci		return pix;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	pix_per_word = (8 * 32) / bpp;
3838c2ecf20Sopenharmony_ci	return ALIGN(pix, pix_per_word);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/**
3878c2ecf20Sopenharmony_ci * vidosd_set_size() - set OSD size for a window
3888c2ecf20Sopenharmony_ci *
3898c2ecf20Sopenharmony_ci * @win: the window to set OSD size for
3908c2ecf20Sopenharmony_ci * @size: OSD size register value
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_cistatic void vidosd_set_size(struct s3c_fb_win *win, u32 size)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* OSD can be set up if osd_size_off != 0 for this window */
3978c2ecf20Sopenharmony_ci	if (win->variant.osd_size_off)
3988c2ecf20Sopenharmony_ci		writel(size, sfb->regs + OSD_BASE(win->index, sfb->variant)
3998c2ecf20Sopenharmony_ci				+ win->variant.osd_size_off);
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci/**
4038c2ecf20Sopenharmony_ci * vidosd_set_alpha() - set alpha transparency for a window
4048c2ecf20Sopenharmony_ci *
4058c2ecf20Sopenharmony_ci * @win: the window to set OSD size for
4068c2ecf20Sopenharmony_ci * @alpha: alpha register value
4078c2ecf20Sopenharmony_ci */
4088c2ecf20Sopenharmony_cistatic void vidosd_set_alpha(struct s3c_fb_win *win, u32 alpha)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (win->variant.has_osd_alpha)
4138c2ecf20Sopenharmony_ci		writel(alpha, sfb->regs + VIDOSD_C(win->index, sfb->variant));
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci/**
4178c2ecf20Sopenharmony_ci * shadow_protect_win() - disable updating values from shadow registers at vsync
4188c2ecf20Sopenharmony_ci *
4198c2ecf20Sopenharmony_ci * @win: window to protect registers for
4208c2ecf20Sopenharmony_ci * @protect: 1 to protect (disable updates)
4218c2ecf20Sopenharmony_ci */
4228c2ecf20Sopenharmony_cistatic void shadow_protect_win(struct s3c_fb_win *win, bool protect)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
4258c2ecf20Sopenharmony_ci	u32 reg;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	if (protect) {
4288c2ecf20Sopenharmony_ci		if (sfb->variant.has_prtcon) {
4298c2ecf20Sopenharmony_ci			writel(PRTCON_PROTECT, sfb->regs + PRTCON);
4308c2ecf20Sopenharmony_ci		} else if (sfb->variant.has_shadowcon) {
4318c2ecf20Sopenharmony_ci			reg = readl(sfb->regs + SHADOWCON);
4328c2ecf20Sopenharmony_ci			writel(reg | SHADOWCON_WINx_PROTECT(win->index),
4338c2ecf20Sopenharmony_ci				sfb->regs + SHADOWCON);
4348c2ecf20Sopenharmony_ci		}
4358c2ecf20Sopenharmony_ci	} else {
4368c2ecf20Sopenharmony_ci		if (sfb->variant.has_prtcon) {
4378c2ecf20Sopenharmony_ci			writel(0, sfb->regs + PRTCON);
4388c2ecf20Sopenharmony_ci		} else if (sfb->variant.has_shadowcon) {
4398c2ecf20Sopenharmony_ci			reg = readl(sfb->regs + SHADOWCON);
4408c2ecf20Sopenharmony_ci			writel(reg & ~SHADOWCON_WINx_PROTECT(win->index),
4418c2ecf20Sopenharmony_ci				sfb->regs + SHADOWCON);
4428c2ecf20Sopenharmony_ci		}
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci/**
4478c2ecf20Sopenharmony_ci * s3c_fb_enable() - Set the state of the main LCD output
4488c2ecf20Sopenharmony_ci * @sfb: The main framebuffer state.
4498c2ecf20Sopenharmony_ci * @enable: The state to set.
4508c2ecf20Sopenharmony_ci */
4518c2ecf20Sopenharmony_cistatic void s3c_fb_enable(struct s3c_fb *sfb, int enable)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	u32 vidcon0 = readl(sfb->regs + VIDCON0);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (enable && !sfb->output_on)
4568c2ecf20Sopenharmony_ci		pm_runtime_get_sync(sfb->dev);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (enable) {
4598c2ecf20Sopenharmony_ci		vidcon0 |= VIDCON0_ENVID | VIDCON0_ENVID_F;
4608c2ecf20Sopenharmony_ci	} else {
4618c2ecf20Sopenharmony_ci		/* see the note in the framebuffer datasheet about
4628c2ecf20Sopenharmony_ci		 * why you cannot take both of these bits down at the
4638c2ecf20Sopenharmony_ci		 * same time. */
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci		if (vidcon0 & VIDCON0_ENVID) {
4668c2ecf20Sopenharmony_ci			vidcon0 |= VIDCON0_ENVID;
4678c2ecf20Sopenharmony_ci			vidcon0 &= ~VIDCON0_ENVID_F;
4688c2ecf20Sopenharmony_ci		}
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	writel(vidcon0, sfb->regs + VIDCON0);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if (!enable && sfb->output_on)
4748c2ecf20Sopenharmony_ci		pm_runtime_put_sync(sfb->dev);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	sfb->output_on = enable;
4778c2ecf20Sopenharmony_ci}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci/**
4808c2ecf20Sopenharmony_ci * s3c_fb_set_par() - framebuffer request to set new framebuffer state.
4818c2ecf20Sopenharmony_ci * @info: The framebuffer to change.
4828c2ecf20Sopenharmony_ci *
4838c2ecf20Sopenharmony_ci * Framebuffer layer request to set a new mode for the specified framebuffer
4848c2ecf20Sopenharmony_ci */
4858c2ecf20Sopenharmony_cistatic int s3c_fb_set_par(struct fb_info *info)
4868c2ecf20Sopenharmony_ci{
4878c2ecf20Sopenharmony_ci	struct fb_var_screeninfo *var = &info->var;
4888c2ecf20Sopenharmony_ci	struct s3c_fb_win *win = info->par;
4898c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
4908c2ecf20Sopenharmony_ci	void __iomem *regs = sfb->regs;
4918c2ecf20Sopenharmony_ci	void __iomem *buf = regs;
4928c2ecf20Sopenharmony_ci	int win_no = win->index;
4938c2ecf20Sopenharmony_ci	u32 alpha = 0;
4948c2ecf20Sopenharmony_ci	u32 data;
4958c2ecf20Sopenharmony_ci	u32 pagewidth;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "setting framebuffer parameters\n");
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	shadow_protect_win(win, 1);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	switch (var->bits_per_pixel) {
5048c2ecf20Sopenharmony_ci	case 32:
5058c2ecf20Sopenharmony_ci	case 24:
5068c2ecf20Sopenharmony_ci	case 16:
5078c2ecf20Sopenharmony_ci	case 12:
5088c2ecf20Sopenharmony_ci		info->fix.visual = FB_VISUAL_TRUECOLOR;
5098c2ecf20Sopenharmony_ci		break;
5108c2ecf20Sopenharmony_ci	case 8:
5118c2ecf20Sopenharmony_ci		if (win->variant.palette_sz >= 256)
5128c2ecf20Sopenharmony_ci			info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
5138c2ecf20Sopenharmony_ci		else
5148c2ecf20Sopenharmony_ci			info->fix.visual = FB_VISUAL_TRUECOLOR;
5158c2ecf20Sopenharmony_ci		break;
5168c2ecf20Sopenharmony_ci	case 1:
5178c2ecf20Sopenharmony_ci		info->fix.visual = FB_VISUAL_MONO01;
5188c2ecf20Sopenharmony_ci		break;
5198c2ecf20Sopenharmony_ci	default:
5208c2ecf20Sopenharmony_ci		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
5218c2ecf20Sopenharmony_ci		break;
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	info->fix.xpanstep = info->var.xres_virtual > info->var.xres ? 1 : 0;
5278c2ecf20Sopenharmony_ci	info->fix.ypanstep = info->var.yres_virtual > info->var.yres ? 1 : 0;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* disable the window whilst we update it */
5308c2ecf20Sopenharmony_ci	writel(0, regs + WINCON(win_no));
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	if (!sfb->output_on)
5338c2ecf20Sopenharmony_ci		s3c_fb_enable(sfb, 1);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	/* write the buffer address */
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	/* start and end registers stride is 8 */
5388c2ecf20Sopenharmony_ci	buf = regs + win_no * 8;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	writel(info->fix.smem_start, buf + sfb->variant.buf_start);
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	data = info->fix.smem_start + info->fix.line_length * var->yres;
5438c2ecf20Sopenharmony_ci	writel(data, buf + sfb->variant.buf_end);
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	pagewidth = (var->xres * var->bits_per_pixel) >> 3;
5468c2ecf20Sopenharmony_ci	data = VIDW_BUF_SIZE_OFFSET(info->fix.line_length - pagewidth) |
5478c2ecf20Sopenharmony_ci	       VIDW_BUF_SIZE_PAGEWIDTH(pagewidth) |
5488c2ecf20Sopenharmony_ci	       VIDW_BUF_SIZE_OFFSET_E(info->fix.line_length - pagewidth) |
5498c2ecf20Sopenharmony_ci	       VIDW_BUF_SIZE_PAGEWIDTH_E(pagewidth);
5508c2ecf20Sopenharmony_ci	writel(data, regs + sfb->variant.buf_size + (win_no * 4));
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* write 'OSD' registers to control position of framebuffer */
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	data = VIDOSDxA_TOPLEFT_X(0) | VIDOSDxA_TOPLEFT_Y(0) |
5558c2ecf20Sopenharmony_ci	       VIDOSDxA_TOPLEFT_X_E(0) | VIDOSDxA_TOPLEFT_Y_E(0);
5568c2ecf20Sopenharmony_ci	writel(data, regs + VIDOSD_A(win_no, sfb->variant));
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	data = VIDOSDxB_BOTRIGHT_X(s3c_fb_align_word(var->bits_per_pixel,
5598c2ecf20Sopenharmony_ci						     var->xres - 1)) |
5608c2ecf20Sopenharmony_ci	       VIDOSDxB_BOTRIGHT_Y(var->yres - 1) |
5618c2ecf20Sopenharmony_ci	       VIDOSDxB_BOTRIGHT_X_E(s3c_fb_align_word(var->bits_per_pixel,
5628c2ecf20Sopenharmony_ci						     var->xres - 1)) |
5638c2ecf20Sopenharmony_ci	       VIDOSDxB_BOTRIGHT_Y_E(var->yres - 1);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	writel(data, regs + VIDOSD_B(win_no, sfb->variant));
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	data = var->xres * var->yres;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	alpha = VIDISD14C_ALPHA1_R(0xf) |
5708c2ecf20Sopenharmony_ci		VIDISD14C_ALPHA1_G(0xf) |
5718c2ecf20Sopenharmony_ci		VIDISD14C_ALPHA1_B(0xf);
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	vidosd_set_alpha(win, alpha);
5748c2ecf20Sopenharmony_ci	vidosd_set_size(win, data);
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	/* Enable DMA channel for this window */
5778c2ecf20Sopenharmony_ci	if (sfb->variant.has_shadowcon) {
5788c2ecf20Sopenharmony_ci		data = readl(sfb->regs + SHADOWCON);
5798c2ecf20Sopenharmony_ci		data |= SHADOWCON_CHx_ENABLE(win_no);
5808c2ecf20Sopenharmony_ci		writel(data, sfb->regs + SHADOWCON);
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	data = WINCONx_ENWIN;
5848c2ecf20Sopenharmony_ci	sfb->enabled |= (1 << win->index);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	/* note, since we have to round up the bits-per-pixel, we end up
5878c2ecf20Sopenharmony_ci	 * relying on the bitfield information for r/g/b/a to work out
5888c2ecf20Sopenharmony_ci	 * exactly which mode of operation is intended. */
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	switch (var->bits_per_pixel) {
5918c2ecf20Sopenharmony_ci	case 1:
5928c2ecf20Sopenharmony_ci		data |= WINCON0_BPPMODE_1BPP;
5938c2ecf20Sopenharmony_ci		data |= WINCONx_BITSWP;
5948c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_4WORD;
5958c2ecf20Sopenharmony_ci		break;
5968c2ecf20Sopenharmony_ci	case 2:
5978c2ecf20Sopenharmony_ci		data |= WINCON0_BPPMODE_2BPP;
5988c2ecf20Sopenharmony_ci		data |= WINCONx_BITSWP;
5998c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_8WORD;
6008c2ecf20Sopenharmony_ci		break;
6018c2ecf20Sopenharmony_ci	case 4:
6028c2ecf20Sopenharmony_ci		data |= WINCON0_BPPMODE_4BPP;
6038c2ecf20Sopenharmony_ci		data |= WINCONx_BITSWP;
6048c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_8WORD;
6058c2ecf20Sopenharmony_ci		break;
6068c2ecf20Sopenharmony_ci	case 8:
6078c2ecf20Sopenharmony_ci		if (var->transp.length != 0)
6088c2ecf20Sopenharmony_ci			data |= WINCON1_BPPMODE_8BPP_1232;
6098c2ecf20Sopenharmony_ci		else
6108c2ecf20Sopenharmony_ci			data |= WINCON0_BPPMODE_8BPP_PALETTE;
6118c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_8WORD;
6128c2ecf20Sopenharmony_ci		data |= WINCONx_BYTSWP;
6138c2ecf20Sopenharmony_ci		break;
6148c2ecf20Sopenharmony_ci	case 16:
6158c2ecf20Sopenharmony_ci		if (var->transp.length != 0)
6168c2ecf20Sopenharmony_ci			data |= WINCON1_BPPMODE_16BPP_A1555;
6178c2ecf20Sopenharmony_ci		else
6188c2ecf20Sopenharmony_ci			data |= WINCON0_BPPMODE_16BPP_565;
6198c2ecf20Sopenharmony_ci		data |= WINCONx_HAWSWP;
6208c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_16WORD;
6218c2ecf20Sopenharmony_ci		break;
6228c2ecf20Sopenharmony_ci	case 24:
6238c2ecf20Sopenharmony_ci	case 32:
6248c2ecf20Sopenharmony_ci		if (var->red.length == 6) {
6258c2ecf20Sopenharmony_ci			if (var->transp.length != 0)
6268c2ecf20Sopenharmony_ci				data |= WINCON1_BPPMODE_19BPP_A1666;
6278c2ecf20Sopenharmony_ci			else
6288c2ecf20Sopenharmony_ci				data |= WINCON1_BPPMODE_18BPP_666;
6298c2ecf20Sopenharmony_ci		} else if (var->transp.length == 1)
6308c2ecf20Sopenharmony_ci			data |= WINCON1_BPPMODE_25BPP_A1888
6318c2ecf20Sopenharmony_ci				| WINCON1_BLD_PIX;
6328c2ecf20Sopenharmony_ci		else if ((var->transp.length == 4) ||
6338c2ecf20Sopenharmony_ci			(var->transp.length == 8))
6348c2ecf20Sopenharmony_ci			data |= WINCON1_BPPMODE_28BPP_A4888
6358c2ecf20Sopenharmony_ci				| WINCON1_BLD_PIX | WINCON1_ALPHA_SEL;
6368c2ecf20Sopenharmony_ci		else
6378c2ecf20Sopenharmony_ci			data |= WINCON0_BPPMODE_24BPP_888;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci		data |= WINCONx_WSWP;
6408c2ecf20Sopenharmony_ci		data |= WINCONx_BURSTLEN_16WORD;
6418c2ecf20Sopenharmony_ci		break;
6428c2ecf20Sopenharmony_ci	}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	/* Enable the colour keying for the window below this one */
6458c2ecf20Sopenharmony_ci	if (win_no > 0) {
6468c2ecf20Sopenharmony_ci		u32 keycon0_data = 0, keycon1_data = 0;
6478c2ecf20Sopenharmony_ci		void __iomem *keycon = regs + sfb->variant.keycon;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci		keycon0_data = ~(WxKEYCON0_KEYBL_EN |
6508c2ecf20Sopenharmony_ci				WxKEYCON0_KEYEN_F |
6518c2ecf20Sopenharmony_ci				WxKEYCON0_DIRCON) | WxKEYCON0_COMPKEY(0);
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		keycon1_data = WxKEYCON1_COLVAL(0xffffff);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci		keycon += (win_no - 1) * 8;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		writel(keycon0_data, keycon + WKEYCON0);
6588c2ecf20Sopenharmony_ci		writel(keycon1_data, keycon + WKEYCON1);
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	writel(data, regs + sfb->variant.wincon + (win_no * 4));
6628c2ecf20Sopenharmony_ci	writel(0x0, regs + sfb->variant.winmap + (win_no * 4));
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	/* Set alpha value width */
6658c2ecf20Sopenharmony_ci	if (sfb->variant.has_blendcon) {
6668c2ecf20Sopenharmony_ci		data = readl(sfb->regs + BLENDCON);
6678c2ecf20Sopenharmony_ci		data &= ~BLENDCON_NEW_MASK;
6688c2ecf20Sopenharmony_ci		if (var->transp.length > 4)
6698c2ecf20Sopenharmony_ci			data |= BLENDCON_NEW_8BIT_ALPHA_VALUE;
6708c2ecf20Sopenharmony_ci		else
6718c2ecf20Sopenharmony_ci			data |= BLENDCON_NEW_4BIT_ALPHA_VALUE;
6728c2ecf20Sopenharmony_ci		writel(data, sfb->regs + BLENDCON);
6738c2ecf20Sopenharmony_ci	}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	shadow_protect_win(win, 0);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	return 0;
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci/**
6838c2ecf20Sopenharmony_ci * s3c_fb_update_palette() - set or schedule a palette update.
6848c2ecf20Sopenharmony_ci * @sfb: The hardware information.
6858c2ecf20Sopenharmony_ci * @win: The window being updated.
6868c2ecf20Sopenharmony_ci * @reg: The palette index being changed.
6878c2ecf20Sopenharmony_ci * @value: The computed palette value.
6888c2ecf20Sopenharmony_ci *
6898c2ecf20Sopenharmony_ci * Change the value of a palette register, either by directly writing to
6908c2ecf20Sopenharmony_ci * the palette (this requires the palette RAM to be disconnected from the
6918c2ecf20Sopenharmony_ci * hardware whilst this is in progress) or schedule the update for later.
6928c2ecf20Sopenharmony_ci *
6938c2ecf20Sopenharmony_ci * At the moment, since we have no VSYNC interrupt support, we simply set
6948c2ecf20Sopenharmony_ci * the palette entry directly.
6958c2ecf20Sopenharmony_ci */
6968c2ecf20Sopenharmony_cistatic void s3c_fb_update_palette(struct s3c_fb *sfb,
6978c2ecf20Sopenharmony_ci				  struct s3c_fb_win *win,
6988c2ecf20Sopenharmony_ci				  unsigned int reg,
6998c2ecf20Sopenharmony_ci				  u32 value)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	void __iomem *palreg;
7028c2ecf20Sopenharmony_ci	u32 palcon;
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	palreg = sfb->regs + sfb->variant.palette[win->index];
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "%s: win %d, reg %d (%p): %08x\n",
7078c2ecf20Sopenharmony_ci		__func__, win->index, reg, palreg, value);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	win->palette_buffer[reg] = value;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	palcon = readl(sfb->regs + WPALCON);
7128c2ecf20Sopenharmony_ci	writel(palcon | WPALCON_PAL_UPDATE, sfb->regs + WPALCON);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	if (win->variant.palette_16bpp)
7158c2ecf20Sopenharmony_ci		writew(value, palreg + (reg * 2));
7168c2ecf20Sopenharmony_ci	else
7178c2ecf20Sopenharmony_ci		writel(value, palreg + (reg * 4));
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	writel(palcon, sfb->regs + WPALCON);
7208c2ecf20Sopenharmony_ci}
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic inline unsigned int chan_to_field(unsigned int chan,
7238c2ecf20Sopenharmony_ci					 struct fb_bitfield *bf)
7248c2ecf20Sopenharmony_ci{
7258c2ecf20Sopenharmony_ci	chan &= 0xffff;
7268c2ecf20Sopenharmony_ci	chan >>= 16 - bf->length;
7278c2ecf20Sopenharmony_ci	return chan << bf->offset;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci/**
7318c2ecf20Sopenharmony_ci * s3c_fb_setcolreg() - framebuffer layer request to change palette.
7328c2ecf20Sopenharmony_ci * @regno: The palette index to change.
7338c2ecf20Sopenharmony_ci * @red: The red field for the palette data.
7348c2ecf20Sopenharmony_ci * @green: The green field for the palette data.
7358c2ecf20Sopenharmony_ci * @blue: The blue field for the palette data.
7368c2ecf20Sopenharmony_ci * @trans: The transparency (alpha) field for the palette data.
7378c2ecf20Sopenharmony_ci * @info: The framebuffer being changed.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_cistatic int s3c_fb_setcolreg(unsigned regno,
7408c2ecf20Sopenharmony_ci			    unsigned red, unsigned green, unsigned blue,
7418c2ecf20Sopenharmony_ci			    unsigned transp, struct fb_info *info)
7428c2ecf20Sopenharmony_ci{
7438c2ecf20Sopenharmony_ci	struct s3c_fb_win *win = info->par;
7448c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
7458c2ecf20Sopenharmony_ci	unsigned int val;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "%s: win %d: %d => rgb=%d/%d/%d\n",
7488c2ecf20Sopenharmony_ci		__func__, win->index, regno, red, green, blue);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	switch (info->fix.visual) {
7538c2ecf20Sopenharmony_ci	case FB_VISUAL_TRUECOLOR:
7548c2ecf20Sopenharmony_ci		/* true-colour, use pseudo-palette */
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci		if (regno < 16) {
7578c2ecf20Sopenharmony_ci			u32 *pal = info->pseudo_palette;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci			val  = chan_to_field(red,   &info->var.red);
7608c2ecf20Sopenharmony_ci			val |= chan_to_field(green, &info->var.green);
7618c2ecf20Sopenharmony_ci			val |= chan_to_field(blue,  &info->var.blue);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci			pal[regno] = val;
7648c2ecf20Sopenharmony_ci		}
7658c2ecf20Sopenharmony_ci		break;
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	case FB_VISUAL_PSEUDOCOLOR:
7688c2ecf20Sopenharmony_ci		if (regno < win->variant.palette_sz) {
7698c2ecf20Sopenharmony_ci			val  = chan_to_field(red, &win->palette.r);
7708c2ecf20Sopenharmony_ci			val |= chan_to_field(green, &win->palette.g);
7718c2ecf20Sopenharmony_ci			val |= chan_to_field(blue, &win->palette.b);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci			s3c_fb_update_palette(sfb, win, regno, val);
7748c2ecf20Sopenharmony_ci		}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci		break;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	default:
7798c2ecf20Sopenharmony_ci		pm_runtime_put_sync(sfb->dev);
7808c2ecf20Sopenharmony_ci		return 1;	/* unknown type */
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
7848c2ecf20Sopenharmony_ci	return 0;
7858c2ecf20Sopenharmony_ci}
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci/**
7888c2ecf20Sopenharmony_ci * s3c_fb_blank() - blank or unblank the given window
7898c2ecf20Sopenharmony_ci * @blank_mode: The blank state from FB_BLANK_*
7908c2ecf20Sopenharmony_ci * @info: The framebuffer to blank.
7918c2ecf20Sopenharmony_ci *
7928c2ecf20Sopenharmony_ci * Framebuffer layer request to change the power state.
7938c2ecf20Sopenharmony_ci */
7948c2ecf20Sopenharmony_cistatic int s3c_fb_blank(int blank_mode, struct fb_info *info)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	struct s3c_fb_win *win = info->par;
7978c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
7988c2ecf20Sopenharmony_ci	unsigned int index = win->index;
7998c2ecf20Sopenharmony_ci	u32 wincon;
8008c2ecf20Sopenharmony_ci	u32 output_on = sfb->output_on;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "blank mode %d\n", blank_mode);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	wincon = readl(sfb->regs + sfb->variant.wincon + (index * 4));
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	switch (blank_mode) {
8098c2ecf20Sopenharmony_ci	case FB_BLANK_POWERDOWN:
8108c2ecf20Sopenharmony_ci		wincon &= ~WINCONx_ENWIN;
8118c2ecf20Sopenharmony_ci		sfb->enabled &= ~(1 << index);
8128c2ecf20Sopenharmony_ci		fallthrough;	/* to FB_BLANK_NORMAL */
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	case FB_BLANK_NORMAL:
8158c2ecf20Sopenharmony_ci		/* disable the DMA and display 0x0 (black) */
8168c2ecf20Sopenharmony_ci		shadow_protect_win(win, 1);
8178c2ecf20Sopenharmony_ci		writel(WINxMAP_MAP | WINxMAP_MAP_COLOUR(0x0),
8188c2ecf20Sopenharmony_ci		       sfb->regs + sfb->variant.winmap + (index * 4));
8198c2ecf20Sopenharmony_ci		shadow_protect_win(win, 0);
8208c2ecf20Sopenharmony_ci		break;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	case FB_BLANK_UNBLANK:
8238c2ecf20Sopenharmony_ci		shadow_protect_win(win, 1);
8248c2ecf20Sopenharmony_ci		writel(0x0, sfb->regs + sfb->variant.winmap + (index * 4));
8258c2ecf20Sopenharmony_ci		shadow_protect_win(win, 0);
8268c2ecf20Sopenharmony_ci		wincon |= WINCONx_ENWIN;
8278c2ecf20Sopenharmony_ci		sfb->enabled |= (1 << index);
8288c2ecf20Sopenharmony_ci		break;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	case FB_BLANK_VSYNC_SUSPEND:
8318c2ecf20Sopenharmony_ci	case FB_BLANK_HSYNC_SUSPEND:
8328c2ecf20Sopenharmony_ci	default:
8338c2ecf20Sopenharmony_ci		pm_runtime_put_sync(sfb->dev);
8348c2ecf20Sopenharmony_ci		return 1;
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	shadow_protect_win(win, 1);
8388c2ecf20Sopenharmony_ci	writel(wincon, sfb->regs + sfb->variant.wincon + (index * 4));
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	/* Check the enabled state to see if we need to be running the
8418c2ecf20Sopenharmony_ci	 * main LCD interface, as if there are no active windows then
8428c2ecf20Sopenharmony_ci	 * it is highly likely that we also do not need to output
8438c2ecf20Sopenharmony_ci	 * anything.
8448c2ecf20Sopenharmony_ci	 */
8458c2ecf20Sopenharmony_ci	s3c_fb_enable(sfb, sfb->enabled ? 1 : 0);
8468c2ecf20Sopenharmony_ci	shadow_protect_win(win, 0);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	return output_on == sfb->output_on;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci/**
8548c2ecf20Sopenharmony_ci * s3c_fb_pan_display() - Pan the display.
8558c2ecf20Sopenharmony_ci *
8568c2ecf20Sopenharmony_ci * Note that the offsets can be written to the device at any time, as their
8578c2ecf20Sopenharmony_ci * values are latched at each vsync automatically. This also means that only
8588c2ecf20Sopenharmony_ci * the last call to this function will have any effect on next vsync, but
8598c2ecf20Sopenharmony_ci * there is no need to sleep waiting for it to prevent tearing.
8608c2ecf20Sopenharmony_ci *
8618c2ecf20Sopenharmony_ci * @var: The screen information to verify.
8628c2ecf20Sopenharmony_ci * @info: The framebuffer device.
8638c2ecf20Sopenharmony_ci */
8648c2ecf20Sopenharmony_cistatic int s3c_fb_pan_display(struct fb_var_screeninfo *var,
8658c2ecf20Sopenharmony_ci			      struct fb_info *info)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	struct s3c_fb_win *win	= info->par;
8688c2ecf20Sopenharmony_ci	struct s3c_fb *sfb	= win->parent;
8698c2ecf20Sopenharmony_ci	void __iomem *buf	= sfb->regs + win->index * 8;
8708c2ecf20Sopenharmony_ci	unsigned int start_boff, end_boff;
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	/* Offset in bytes to the start of the displayed area */
8758c2ecf20Sopenharmony_ci	start_boff = var->yoffset * info->fix.line_length;
8768c2ecf20Sopenharmony_ci	/* X offset depends on the current bpp */
8778c2ecf20Sopenharmony_ci	if (info->var.bits_per_pixel >= 8) {
8788c2ecf20Sopenharmony_ci		start_boff += var->xoffset * (info->var.bits_per_pixel >> 3);
8798c2ecf20Sopenharmony_ci	} else {
8808c2ecf20Sopenharmony_ci		switch (info->var.bits_per_pixel) {
8818c2ecf20Sopenharmony_ci		case 4:
8828c2ecf20Sopenharmony_ci			start_boff += var->xoffset >> 1;
8838c2ecf20Sopenharmony_ci			break;
8848c2ecf20Sopenharmony_ci		case 2:
8858c2ecf20Sopenharmony_ci			start_boff += var->xoffset >> 2;
8868c2ecf20Sopenharmony_ci			break;
8878c2ecf20Sopenharmony_ci		case 1:
8888c2ecf20Sopenharmony_ci			start_boff += var->xoffset >> 3;
8898c2ecf20Sopenharmony_ci			break;
8908c2ecf20Sopenharmony_ci		default:
8918c2ecf20Sopenharmony_ci			dev_err(sfb->dev, "invalid bpp\n");
8928c2ecf20Sopenharmony_ci			pm_runtime_put_sync(sfb->dev);
8938c2ecf20Sopenharmony_ci			return -EINVAL;
8948c2ecf20Sopenharmony_ci		}
8958c2ecf20Sopenharmony_ci	}
8968c2ecf20Sopenharmony_ci	/* Offset in bytes to the end of the displayed area */
8978c2ecf20Sopenharmony_ci	end_boff = start_boff + info->var.yres * info->fix.line_length;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/* Temporarily turn off per-vsync update from shadow registers until
9008c2ecf20Sopenharmony_ci	 * both start and end addresses are updated to prevent corruption */
9018c2ecf20Sopenharmony_ci	shadow_protect_win(win, 1);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	writel(info->fix.smem_start + start_boff, buf + sfb->variant.buf_start);
9048c2ecf20Sopenharmony_ci	writel(info->fix.smem_start + end_boff, buf + sfb->variant.buf_end);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	shadow_protect_win(win, 0);
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
9098c2ecf20Sopenharmony_ci	return 0;
9108c2ecf20Sopenharmony_ci}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci/**
9138c2ecf20Sopenharmony_ci * s3c_fb_enable_irq() - enable framebuffer interrupts
9148c2ecf20Sopenharmony_ci * @sfb: main hardware state
9158c2ecf20Sopenharmony_ci */
9168c2ecf20Sopenharmony_cistatic void s3c_fb_enable_irq(struct s3c_fb *sfb)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	void __iomem *regs = sfb->regs;
9198c2ecf20Sopenharmony_ci	u32 irq_ctrl_reg;
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (!test_and_set_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
9228c2ecf20Sopenharmony_ci		/* IRQ disabled, enable it */
9238c2ecf20Sopenharmony_ci		irq_ctrl_reg = readl(regs + VIDINTCON0);
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci		irq_ctrl_reg |= VIDINTCON0_INT_ENABLE;
9268c2ecf20Sopenharmony_ci		irq_ctrl_reg |= VIDINTCON0_INT_FRAME;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci		irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL0_MASK;
9298c2ecf20Sopenharmony_ci		irq_ctrl_reg |= VIDINTCON0_FRAMESEL0_VSYNC;
9308c2ecf20Sopenharmony_ci		irq_ctrl_reg &= ~VIDINTCON0_FRAMESEL1_MASK;
9318c2ecf20Sopenharmony_ci		irq_ctrl_reg |= VIDINTCON0_FRAMESEL1_NONE;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci		writel(irq_ctrl_reg, regs + VIDINTCON0);
9348c2ecf20Sopenharmony_ci	}
9358c2ecf20Sopenharmony_ci}
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci/**
9388c2ecf20Sopenharmony_ci * s3c_fb_disable_irq() - disable framebuffer interrupts
9398c2ecf20Sopenharmony_ci * @sfb: main hardware state
9408c2ecf20Sopenharmony_ci */
9418c2ecf20Sopenharmony_cistatic void s3c_fb_disable_irq(struct s3c_fb *sfb)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	void __iomem *regs = sfb->regs;
9448c2ecf20Sopenharmony_ci	u32 irq_ctrl_reg;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	if (test_and_clear_bit(S3C_FB_VSYNC_IRQ_EN, &sfb->irq_flags)) {
9478c2ecf20Sopenharmony_ci		/* IRQ enabled, disable it */
9488c2ecf20Sopenharmony_ci		irq_ctrl_reg = readl(regs + VIDINTCON0);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci		irq_ctrl_reg &= ~VIDINTCON0_INT_FRAME;
9518c2ecf20Sopenharmony_ci		irq_ctrl_reg &= ~VIDINTCON0_INT_ENABLE;
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci		writel(irq_ctrl_reg, regs + VIDINTCON0);
9548c2ecf20Sopenharmony_ci	}
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_cistatic irqreturn_t s3c_fb_irq(int irq, void *dev_id)
9588c2ecf20Sopenharmony_ci{
9598c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = dev_id;
9608c2ecf20Sopenharmony_ci	void __iomem  *regs = sfb->regs;
9618c2ecf20Sopenharmony_ci	u32 irq_sts_reg;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	spin_lock(&sfb->slock);
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	irq_sts_reg = readl(regs + VIDINTCON1);
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	if (irq_sts_reg & VIDINTCON1_INT_FRAME) {
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci		/* VSYNC interrupt, accept it */
9708c2ecf20Sopenharmony_ci		writel(VIDINTCON1_INT_FRAME, regs + VIDINTCON1);
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci		sfb->vsync_info.count++;
9738c2ecf20Sopenharmony_ci		wake_up_interruptible(&sfb->vsync_info.wait);
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	/* We only support waiting for VSYNC for now, so it's safe
9778c2ecf20Sopenharmony_ci	 * to always disable irqs here.
9788c2ecf20Sopenharmony_ci	 */
9798c2ecf20Sopenharmony_ci	s3c_fb_disable_irq(sfb);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	spin_unlock(&sfb->slock);
9828c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci/**
9868c2ecf20Sopenharmony_ci * s3c_fb_wait_for_vsync() - sleep until next VSYNC interrupt or timeout
9878c2ecf20Sopenharmony_ci * @sfb: main hardware state
9888c2ecf20Sopenharmony_ci * @crtc: head index.
9898c2ecf20Sopenharmony_ci */
9908c2ecf20Sopenharmony_cistatic int s3c_fb_wait_for_vsync(struct s3c_fb *sfb, u32 crtc)
9918c2ecf20Sopenharmony_ci{
9928c2ecf20Sopenharmony_ci	unsigned long count;
9938c2ecf20Sopenharmony_ci	int ret;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	if (crtc != 0)
9968c2ecf20Sopenharmony_ci		return -ENODEV;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	count = sfb->vsync_info.count;
10018c2ecf20Sopenharmony_ci	s3c_fb_enable_irq(sfb);
10028c2ecf20Sopenharmony_ci	ret = wait_event_interruptible_timeout(sfb->vsync_info.wait,
10038c2ecf20Sopenharmony_ci				       count != sfb->vsync_info.count,
10048c2ecf20Sopenharmony_ci				       msecs_to_jiffies(VSYNC_TIMEOUT_MSEC));
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	if (ret == 0)
10098c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	return 0;
10128c2ecf20Sopenharmony_ci}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_cistatic int s3c_fb_ioctl(struct fb_info *info, unsigned int cmd,
10158c2ecf20Sopenharmony_ci			unsigned long arg)
10168c2ecf20Sopenharmony_ci{
10178c2ecf20Sopenharmony_ci	struct s3c_fb_win *win = info->par;
10188c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = win->parent;
10198c2ecf20Sopenharmony_ci	int ret;
10208c2ecf20Sopenharmony_ci	u32 crtc;
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	switch (cmd) {
10238c2ecf20Sopenharmony_ci	case FBIO_WAITFORVSYNC:
10248c2ecf20Sopenharmony_ci		if (get_user(crtc, (u32 __user *)arg)) {
10258c2ecf20Sopenharmony_ci			ret = -EFAULT;
10268c2ecf20Sopenharmony_ci			break;
10278c2ecf20Sopenharmony_ci		}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci		ret = s3c_fb_wait_for_vsync(sfb, crtc);
10308c2ecf20Sopenharmony_ci		break;
10318c2ecf20Sopenharmony_ci	default:
10328c2ecf20Sopenharmony_ci		ret = -ENOTTY;
10338c2ecf20Sopenharmony_ci	}
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	return ret;
10368c2ecf20Sopenharmony_ci}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_cistatic const struct fb_ops s3c_fb_ops = {
10398c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
10408c2ecf20Sopenharmony_ci	.fb_check_var	= s3c_fb_check_var,
10418c2ecf20Sopenharmony_ci	.fb_set_par	= s3c_fb_set_par,
10428c2ecf20Sopenharmony_ci	.fb_blank	= s3c_fb_blank,
10438c2ecf20Sopenharmony_ci	.fb_setcolreg	= s3c_fb_setcolreg,
10448c2ecf20Sopenharmony_ci	.fb_fillrect	= cfb_fillrect,
10458c2ecf20Sopenharmony_ci	.fb_copyarea	= cfb_copyarea,
10468c2ecf20Sopenharmony_ci	.fb_imageblit	= cfb_imageblit,
10478c2ecf20Sopenharmony_ci	.fb_pan_display	= s3c_fb_pan_display,
10488c2ecf20Sopenharmony_ci	.fb_ioctl	= s3c_fb_ioctl,
10498c2ecf20Sopenharmony_ci};
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci/**
10528c2ecf20Sopenharmony_ci * s3c_fb_missing_pixclock() - calculates pixel clock
10538c2ecf20Sopenharmony_ci * @mode: The video mode to change.
10548c2ecf20Sopenharmony_ci *
10558c2ecf20Sopenharmony_ci * Calculate the pixel clock when none has been given through platform data.
10568c2ecf20Sopenharmony_ci */
10578c2ecf20Sopenharmony_cistatic void s3c_fb_missing_pixclock(struct fb_videomode *mode)
10588c2ecf20Sopenharmony_ci{
10598c2ecf20Sopenharmony_ci	u64 pixclk = 1000000000000ULL;
10608c2ecf20Sopenharmony_ci	u32 div;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	div  = mode->left_margin + mode->hsync_len + mode->right_margin +
10638c2ecf20Sopenharmony_ci	       mode->xres;
10648c2ecf20Sopenharmony_ci	div *= mode->upper_margin + mode->vsync_len + mode->lower_margin +
10658c2ecf20Sopenharmony_ci	       mode->yres;
10668c2ecf20Sopenharmony_ci	div *= mode->refresh ? : 60;
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	do_div(pixclk, div);
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	mode->pixclock = pixclk;
10718c2ecf20Sopenharmony_ci}
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci/**
10748c2ecf20Sopenharmony_ci * s3c_fb_alloc_memory() - allocate display memory for framebuffer window
10758c2ecf20Sopenharmony_ci * @sfb: The base resources for the hardware.
10768c2ecf20Sopenharmony_ci * @win: The window to initialise memory for.
10778c2ecf20Sopenharmony_ci *
10788c2ecf20Sopenharmony_ci * Allocate memory for the given framebuffer.
10798c2ecf20Sopenharmony_ci */
10808c2ecf20Sopenharmony_cistatic int s3c_fb_alloc_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	struct s3c_fb_pd_win *windata = win->windata;
10838c2ecf20Sopenharmony_ci	unsigned int real_size, virt_size, size;
10848c2ecf20Sopenharmony_ci	struct fb_info *fbi = win->fbinfo;
10858c2ecf20Sopenharmony_ci	dma_addr_t map_dma;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "allocating memory for display\n");
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	real_size = windata->xres * windata->yres;
10908c2ecf20Sopenharmony_ci	virt_size = windata->virtual_x * windata->virtual_y;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "real_size=%u (%u.%u), virt_size=%u (%u.%u)\n",
10938c2ecf20Sopenharmony_ci		real_size, windata->xres, windata->yres,
10948c2ecf20Sopenharmony_ci		virt_size, windata->virtual_x, windata->virtual_y);
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	size = (real_size > virt_size) ? real_size : virt_size;
10978c2ecf20Sopenharmony_ci	size *= (windata->max_bpp > 16) ? 32 : windata->max_bpp;
10988c2ecf20Sopenharmony_ci	size /= 8;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	fbi->fix.smem_len = size;
11018c2ecf20Sopenharmony_ci	size = PAGE_ALIGN(size);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "want %u bytes for window\n", size);
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	fbi->screen_buffer = dma_alloc_wc(sfb->dev, size, &map_dma, GFP_KERNEL);
11068c2ecf20Sopenharmony_ci	if (!fbi->screen_buffer)
11078c2ecf20Sopenharmony_ci		return -ENOMEM;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "mapped %x to %p\n",
11108c2ecf20Sopenharmony_ci		(unsigned int)map_dma, fbi->screen_buffer);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	memset(fbi->screen_buffer, 0x0, size);
11138c2ecf20Sopenharmony_ci	fbi->fix.smem_start = map_dma;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	return 0;
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci/**
11198c2ecf20Sopenharmony_ci * s3c_fb_free_memory() - free the display memory for the given window
11208c2ecf20Sopenharmony_ci * @sfb: The base resources for the hardware.
11218c2ecf20Sopenharmony_ci * @win: The window to free the display memory for.
11228c2ecf20Sopenharmony_ci *
11238c2ecf20Sopenharmony_ci * Free the display memory allocated by s3c_fb_alloc_memory().
11248c2ecf20Sopenharmony_ci */
11258c2ecf20Sopenharmony_cistatic void s3c_fb_free_memory(struct s3c_fb *sfb, struct s3c_fb_win *win)
11268c2ecf20Sopenharmony_ci{
11278c2ecf20Sopenharmony_ci	struct fb_info *fbi = win->fbinfo;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	if (fbi->screen_buffer)
11308c2ecf20Sopenharmony_ci		dma_free_wc(sfb->dev, PAGE_ALIGN(fbi->fix.smem_len),
11318c2ecf20Sopenharmony_ci			    fbi->screen_buffer, fbi->fix.smem_start);
11328c2ecf20Sopenharmony_ci}
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci/**
11358c2ecf20Sopenharmony_ci * s3c_fb_release_win() - release resources for a framebuffer window.
11368c2ecf20Sopenharmony_ci * @win: The window to cleanup the resources for.
11378c2ecf20Sopenharmony_ci *
11388c2ecf20Sopenharmony_ci * Release the resources that where claimed for the hardware window,
11398c2ecf20Sopenharmony_ci * such as the framebuffer instance and any memory claimed for it.
11408c2ecf20Sopenharmony_ci */
11418c2ecf20Sopenharmony_cistatic void s3c_fb_release_win(struct s3c_fb *sfb, struct s3c_fb_win *win)
11428c2ecf20Sopenharmony_ci{
11438c2ecf20Sopenharmony_ci	u32 data;
11448c2ecf20Sopenharmony_ci
11458c2ecf20Sopenharmony_ci	if (win->fbinfo) {
11468c2ecf20Sopenharmony_ci		if (sfb->variant.has_shadowcon) {
11478c2ecf20Sopenharmony_ci			data = readl(sfb->regs + SHADOWCON);
11488c2ecf20Sopenharmony_ci			data &= ~SHADOWCON_CHx_ENABLE(win->index);
11498c2ecf20Sopenharmony_ci			data &= ~SHADOWCON_CHx_LOCAL_ENABLE(win->index);
11508c2ecf20Sopenharmony_ci			writel(data, sfb->regs + SHADOWCON);
11518c2ecf20Sopenharmony_ci		}
11528c2ecf20Sopenharmony_ci		unregister_framebuffer(win->fbinfo);
11538c2ecf20Sopenharmony_ci		if (win->fbinfo->cmap.len)
11548c2ecf20Sopenharmony_ci			fb_dealloc_cmap(&win->fbinfo->cmap);
11558c2ecf20Sopenharmony_ci		s3c_fb_free_memory(sfb, win);
11568c2ecf20Sopenharmony_ci		framebuffer_release(win->fbinfo);
11578c2ecf20Sopenharmony_ci	}
11588c2ecf20Sopenharmony_ci}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci/**
11618c2ecf20Sopenharmony_ci * s3c_fb_probe_win() - register an hardware window
11628c2ecf20Sopenharmony_ci * @sfb: The base resources for the hardware
11638c2ecf20Sopenharmony_ci * @variant: The variant information for this window.
11648c2ecf20Sopenharmony_ci * @res: Pointer to where to place the resultant window.
11658c2ecf20Sopenharmony_ci *
11668c2ecf20Sopenharmony_ci * Allocate and do the basic initialisation for one of the hardware's graphics
11678c2ecf20Sopenharmony_ci * windows.
11688c2ecf20Sopenharmony_ci */
11698c2ecf20Sopenharmony_cistatic int s3c_fb_probe_win(struct s3c_fb *sfb, unsigned int win_no,
11708c2ecf20Sopenharmony_ci			    struct s3c_fb_win_variant *variant,
11718c2ecf20Sopenharmony_ci			    struct s3c_fb_win **res)
11728c2ecf20Sopenharmony_ci{
11738c2ecf20Sopenharmony_ci	struct fb_var_screeninfo *var;
11748c2ecf20Sopenharmony_ci	struct fb_videomode initmode;
11758c2ecf20Sopenharmony_ci	struct s3c_fb_pd_win *windata;
11768c2ecf20Sopenharmony_ci	struct s3c_fb_win *win;
11778c2ecf20Sopenharmony_ci	struct fb_info *fbinfo;
11788c2ecf20Sopenharmony_ci	int palette_size;
11798c2ecf20Sopenharmony_ci	int ret;
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "probing window %d, variant %p\n", win_no, variant);
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	init_waitqueue_head(&sfb->vsync_info.wait);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	palette_size = variant->palette_sz * 4;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	fbinfo = framebuffer_alloc(sizeof(struct s3c_fb_win) +
11888c2ecf20Sopenharmony_ci				   palette_size * sizeof(u32), sfb->dev);
11898c2ecf20Sopenharmony_ci	if (!fbinfo)
11908c2ecf20Sopenharmony_ci		return -ENOMEM;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	windata = sfb->pdata->win[win_no];
11938c2ecf20Sopenharmony_ci	initmode = *sfb->pdata->vtiming;
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	WARN_ON(windata->max_bpp == 0);
11968c2ecf20Sopenharmony_ci	WARN_ON(windata->xres == 0);
11978c2ecf20Sopenharmony_ci	WARN_ON(windata->yres == 0);
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci	win = fbinfo->par;
12008c2ecf20Sopenharmony_ci	*res = win;
12018c2ecf20Sopenharmony_ci	var = &fbinfo->var;
12028c2ecf20Sopenharmony_ci	win->variant = *variant;
12038c2ecf20Sopenharmony_ci	win->fbinfo = fbinfo;
12048c2ecf20Sopenharmony_ci	win->parent = sfb;
12058c2ecf20Sopenharmony_ci	win->windata = windata;
12068c2ecf20Sopenharmony_ci	win->index = win_no;
12078c2ecf20Sopenharmony_ci	win->palette_buffer = (u32 *)(win + 1);
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	ret = s3c_fb_alloc_memory(sfb, win);
12108c2ecf20Sopenharmony_ci	if (ret) {
12118c2ecf20Sopenharmony_ci		dev_err(sfb->dev, "failed to allocate display memory\n");
12128c2ecf20Sopenharmony_ci		return ret;
12138c2ecf20Sopenharmony_ci	}
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	/* setup the r/b/g positions for the window's palette */
12168c2ecf20Sopenharmony_ci	if (win->variant.palette_16bpp) {
12178c2ecf20Sopenharmony_ci		/* Set RGB 5:6:5 as default */
12188c2ecf20Sopenharmony_ci		win->palette.r.offset = 11;
12198c2ecf20Sopenharmony_ci		win->palette.r.length = 5;
12208c2ecf20Sopenharmony_ci		win->palette.g.offset = 5;
12218c2ecf20Sopenharmony_ci		win->palette.g.length = 6;
12228c2ecf20Sopenharmony_ci		win->palette.b.offset = 0;
12238c2ecf20Sopenharmony_ci		win->palette.b.length = 5;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	} else {
12268c2ecf20Sopenharmony_ci		/* Set 8bpp or 8bpp and 1bit alpha */
12278c2ecf20Sopenharmony_ci		win->palette.r.offset = 16;
12288c2ecf20Sopenharmony_ci		win->palette.r.length = 8;
12298c2ecf20Sopenharmony_ci		win->palette.g.offset = 8;
12308c2ecf20Sopenharmony_ci		win->palette.g.length = 8;
12318c2ecf20Sopenharmony_ci		win->palette.b.offset = 0;
12328c2ecf20Sopenharmony_ci		win->palette.b.length = 8;
12338c2ecf20Sopenharmony_ci	}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci	/* setup the initial video mode from the window */
12368c2ecf20Sopenharmony_ci	initmode.xres = windata->xres;
12378c2ecf20Sopenharmony_ci	initmode.yres = windata->yres;
12388c2ecf20Sopenharmony_ci	fb_videomode_to_var(&fbinfo->var, &initmode);
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	fbinfo->fix.type	= FB_TYPE_PACKED_PIXELS;
12418c2ecf20Sopenharmony_ci	fbinfo->fix.accel	= FB_ACCEL_NONE;
12428c2ecf20Sopenharmony_ci	fbinfo->var.activate	= FB_ACTIVATE_NOW;
12438c2ecf20Sopenharmony_ci	fbinfo->var.vmode	= FB_VMODE_NONINTERLACED;
12448c2ecf20Sopenharmony_ci	fbinfo->var.bits_per_pixel = windata->default_bpp;
12458c2ecf20Sopenharmony_ci	fbinfo->fbops		= &s3c_fb_ops;
12468c2ecf20Sopenharmony_ci	fbinfo->flags		= FBINFO_FLAG_DEFAULT;
12478c2ecf20Sopenharmony_ci	fbinfo->pseudo_palette  = &win->pseudo_palette;
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	/* prepare to actually start the framebuffer */
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	ret = s3c_fb_check_var(&fbinfo->var, fbinfo);
12528c2ecf20Sopenharmony_ci	if (ret < 0) {
12538c2ecf20Sopenharmony_ci		dev_err(sfb->dev, "check_var failed on initial video params\n");
12548c2ecf20Sopenharmony_ci		return ret;
12558c2ecf20Sopenharmony_ci	}
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	/* create initial colour map */
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	ret = fb_alloc_cmap(&fbinfo->cmap, win->variant.palette_sz, 1);
12608c2ecf20Sopenharmony_ci	if (ret == 0)
12618c2ecf20Sopenharmony_ci		fb_set_cmap(&fbinfo->cmap, fbinfo);
12628c2ecf20Sopenharmony_ci	else
12638c2ecf20Sopenharmony_ci		dev_err(sfb->dev, "failed to allocate fb cmap\n");
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	s3c_fb_set_par(fbinfo);
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	dev_dbg(sfb->dev, "about to register framebuffer\n");
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	/* run the check_var and set_par on our configuration. */
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	ret = register_framebuffer(fbinfo);
12728c2ecf20Sopenharmony_ci	if (ret < 0) {
12738c2ecf20Sopenharmony_ci		dev_err(sfb->dev, "failed to register framebuffer\n");
12748c2ecf20Sopenharmony_ci		return ret;
12758c2ecf20Sopenharmony_ci	}
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	dev_info(sfb->dev, "window %d: fb %s\n", win_no, fbinfo->fix.id);
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	return 0;
12808c2ecf20Sopenharmony_ci}
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci/**
12838c2ecf20Sopenharmony_ci * s3c_fb_set_rgb_timing() - set video timing for rgb interface.
12848c2ecf20Sopenharmony_ci * @sfb: The base resources for the hardware.
12858c2ecf20Sopenharmony_ci *
12868c2ecf20Sopenharmony_ci * Set horizontal and vertical lcd rgb interface timing.
12878c2ecf20Sopenharmony_ci */
12888c2ecf20Sopenharmony_cistatic void s3c_fb_set_rgb_timing(struct s3c_fb *sfb)
12898c2ecf20Sopenharmony_ci{
12908c2ecf20Sopenharmony_ci	struct fb_videomode *vmode = sfb->pdata->vtiming;
12918c2ecf20Sopenharmony_ci	void __iomem *regs = sfb->regs;
12928c2ecf20Sopenharmony_ci	int clkdiv;
12938c2ecf20Sopenharmony_ci	u32 data;
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci	if (!vmode->pixclock)
12968c2ecf20Sopenharmony_ci		s3c_fb_missing_pixclock(vmode);
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	clkdiv = s3c_fb_calc_pixclk(sfb, vmode->pixclock);
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	data = sfb->pdata->vidcon0;
13018c2ecf20Sopenharmony_ci	data &= ~(VIDCON0_CLKVAL_F_MASK | VIDCON0_CLKDIR);
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	if (clkdiv > 1)
13048c2ecf20Sopenharmony_ci		data |= VIDCON0_CLKVAL_F(clkdiv-1) | VIDCON0_CLKDIR;
13058c2ecf20Sopenharmony_ci	else
13068c2ecf20Sopenharmony_ci		data &= ~VIDCON0_CLKDIR;	/* 1:1 clock */
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	if (sfb->variant.is_2443)
13098c2ecf20Sopenharmony_ci		data |= (1 << 5);
13108c2ecf20Sopenharmony_ci	writel(data, regs + VIDCON0);
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	data = VIDTCON0_VBPD(vmode->upper_margin - 1) |
13138c2ecf20Sopenharmony_ci	       VIDTCON0_VFPD(vmode->lower_margin - 1) |
13148c2ecf20Sopenharmony_ci	       VIDTCON0_VSPW(vmode->vsync_len - 1);
13158c2ecf20Sopenharmony_ci	writel(data, regs + sfb->variant.vidtcon);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	data = VIDTCON1_HBPD(vmode->left_margin - 1) |
13188c2ecf20Sopenharmony_ci	       VIDTCON1_HFPD(vmode->right_margin - 1) |
13198c2ecf20Sopenharmony_ci	       VIDTCON1_HSPW(vmode->hsync_len - 1);
13208c2ecf20Sopenharmony_ci	writel(data, regs + sfb->variant.vidtcon + 4);
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	data = VIDTCON2_LINEVAL(vmode->yres - 1) |
13238c2ecf20Sopenharmony_ci	       VIDTCON2_HOZVAL(vmode->xres - 1) |
13248c2ecf20Sopenharmony_ci	       VIDTCON2_LINEVAL_E(vmode->yres - 1) |
13258c2ecf20Sopenharmony_ci	       VIDTCON2_HOZVAL_E(vmode->xres - 1);
13268c2ecf20Sopenharmony_ci	writel(data, regs + sfb->variant.vidtcon + 8);
13278c2ecf20Sopenharmony_ci}
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci/**
13308c2ecf20Sopenharmony_ci * s3c_fb_clear_win() - clear hardware window registers.
13318c2ecf20Sopenharmony_ci * @sfb: The base resources for the hardware.
13328c2ecf20Sopenharmony_ci * @win: The window to process.
13338c2ecf20Sopenharmony_ci *
13348c2ecf20Sopenharmony_ci * Reset the specific window registers to a known state.
13358c2ecf20Sopenharmony_ci */
13368c2ecf20Sopenharmony_cistatic void s3c_fb_clear_win(struct s3c_fb *sfb, int win)
13378c2ecf20Sopenharmony_ci{
13388c2ecf20Sopenharmony_ci	void __iomem *regs = sfb->regs;
13398c2ecf20Sopenharmony_ci	u32 reg;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	writel(0, regs + sfb->variant.wincon + (win * 4));
13428c2ecf20Sopenharmony_ci	writel(0, regs + VIDOSD_A(win, sfb->variant));
13438c2ecf20Sopenharmony_ci	writel(0, regs + VIDOSD_B(win, sfb->variant));
13448c2ecf20Sopenharmony_ci	writel(0, regs + VIDOSD_C(win, sfb->variant));
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	if (sfb->variant.has_shadowcon) {
13478c2ecf20Sopenharmony_ci		reg = readl(sfb->regs + SHADOWCON);
13488c2ecf20Sopenharmony_ci		reg &= ~(SHADOWCON_WINx_PROTECT(win) |
13498c2ecf20Sopenharmony_ci			SHADOWCON_CHx_ENABLE(win) |
13508c2ecf20Sopenharmony_ci			SHADOWCON_CHx_LOCAL_ENABLE(win));
13518c2ecf20Sopenharmony_ci		writel(reg, sfb->regs + SHADOWCON);
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci}
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_cistatic int s3c_fb_probe(struct platform_device *pdev)
13568c2ecf20Sopenharmony_ci{
13578c2ecf20Sopenharmony_ci	const struct platform_device_id *platid;
13588c2ecf20Sopenharmony_ci	struct s3c_fb_driverdata *fbdrv;
13598c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
13608c2ecf20Sopenharmony_ci	struct s3c_fb_platdata *pd;
13618c2ecf20Sopenharmony_ci	struct s3c_fb *sfb;
13628c2ecf20Sopenharmony_ci	struct resource *res;
13638c2ecf20Sopenharmony_ci	int win;
13648c2ecf20Sopenharmony_ci	int ret = 0;
13658c2ecf20Sopenharmony_ci	u32 reg;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	platid = platform_get_device_id(pdev);
13688c2ecf20Sopenharmony_ci	fbdrv = (struct s3c_fb_driverdata *)platid->driver_data;
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	if (fbdrv->variant.nr_windows > S3C_FB_MAX_WIN) {
13718c2ecf20Sopenharmony_ci		dev_err(dev, "too many windows, cannot attach\n");
13728c2ecf20Sopenharmony_ci		return -EINVAL;
13738c2ecf20Sopenharmony_ci	}
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	pd = dev_get_platdata(&pdev->dev);
13768c2ecf20Sopenharmony_ci	if (!pd) {
13778c2ecf20Sopenharmony_ci		dev_err(dev, "no platform data specified\n");
13788c2ecf20Sopenharmony_ci		return -EINVAL;
13798c2ecf20Sopenharmony_ci	}
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	sfb = devm_kzalloc(dev, sizeof(*sfb), GFP_KERNEL);
13828c2ecf20Sopenharmony_ci	if (!sfb)
13838c2ecf20Sopenharmony_ci		return -ENOMEM;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	dev_dbg(dev, "allocate new framebuffer %p\n", sfb);
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	sfb->dev = dev;
13888c2ecf20Sopenharmony_ci	sfb->pdata = pd;
13898c2ecf20Sopenharmony_ci	sfb->variant = fbdrv->variant;
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci	spin_lock_init(&sfb->slock);
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	sfb->bus_clk = devm_clk_get(dev, "lcd");
13948c2ecf20Sopenharmony_ci	if (IS_ERR(sfb->bus_clk)) {
13958c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get bus clock\n");
13968c2ecf20Sopenharmony_ci		return PTR_ERR(sfb->bus_clk);
13978c2ecf20Sopenharmony_ci	}
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci	clk_prepare_enable(sfb->bus_clk);
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel) {
14028c2ecf20Sopenharmony_ci		sfb->lcd_clk = devm_clk_get(dev, "sclk_fimd");
14038c2ecf20Sopenharmony_ci		if (IS_ERR(sfb->lcd_clk)) {
14048c2ecf20Sopenharmony_ci			dev_err(dev, "failed to get lcd clock\n");
14058c2ecf20Sopenharmony_ci			ret = PTR_ERR(sfb->lcd_clk);
14068c2ecf20Sopenharmony_ci			goto err_bus_clk;
14078c2ecf20Sopenharmony_ci		}
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci		clk_prepare_enable(sfb->lcd_clk);
14108c2ecf20Sopenharmony_ci	}
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	pm_runtime_enable(sfb->dev);
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	sfb->regs = devm_platform_ioremap_resource(pdev, 0);
14158c2ecf20Sopenharmony_ci	if (IS_ERR(sfb->regs)) {
14168c2ecf20Sopenharmony_ci		ret = PTR_ERR(sfb->regs);
14178c2ecf20Sopenharmony_ci		goto err_lcd_clk;
14188c2ecf20Sopenharmony_ci	}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
14218c2ecf20Sopenharmony_ci	if (!res) {
14228c2ecf20Sopenharmony_ci		dev_err(dev, "failed to acquire irq resource\n");
14238c2ecf20Sopenharmony_ci		ret = -ENOENT;
14248c2ecf20Sopenharmony_ci		goto err_lcd_clk;
14258c2ecf20Sopenharmony_ci	}
14268c2ecf20Sopenharmony_ci	sfb->irq_no = res->start;
14278c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, sfb->irq_no, s3c_fb_irq,
14288c2ecf20Sopenharmony_ci			  0, "s3c_fb", sfb);
14298c2ecf20Sopenharmony_ci	if (ret) {
14308c2ecf20Sopenharmony_ci		dev_err(dev, "irq request failed\n");
14318c2ecf20Sopenharmony_ci		goto err_lcd_clk;
14328c2ecf20Sopenharmony_ci	}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	dev_dbg(dev, "got resources (regs %p), probing windows\n", sfb->regs);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, sfb);
14378c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	/* setup gpio and output polarity controls */
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	pd->setup_gpio();
14428c2ecf20Sopenharmony_ci
14438c2ecf20Sopenharmony_ci	writel(pd->vidcon1, sfb->regs + VIDCON1);
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci	/* set video clock running at under-run */
14468c2ecf20Sopenharmony_ci	if (sfb->variant.has_fixvclk) {
14478c2ecf20Sopenharmony_ci		reg = readl(sfb->regs + VIDCON1);
14488c2ecf20Sopenharmony_ci		reg &= ~VIDCON1_VCLK_MASK;
14498c2ecf20Sopenharmony_ci		reg |= VIDCON1_VCLK_RUN;
14508c2ecf20Sopenharmony_ci		writel(reg, sfb->regs + VIDCON1);
14518c2ecf20Sopenharmony_ci	}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	/* zero all windows before we do anything */
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	for (win = 0; win < fbdrv->variant.nr_windows; win++)
14568c2ecf20Sopenharmony_ci		s3c_fb_clear_win(sfb, win);
14578c2ecf20Sopenharmony_ci
14588c2ecf20Sopenharmony_ci	/* initialise colour key controls */
14598c2ecf20Sopenharmony_ci	for (win = 0; win < (fbdrv->variant.nr_windows - 1); win++) {
14608c2ecf20Sopenharmony_ci		void __iomem *regs = sfb->regs + sfb->variant.keycon;
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci		regs += (win * 8);
14638c2ecf20Sopenharmony_ci		writel(0xffffff, regs + WKEYCON0);
14648c2ecf20Sopenharmony_ci		writel(0xffffff, regs + WKEYCON1);
14658c2ecf20Sopenharmony_ci	}
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	s3c_fb_set_rgb_timing(sfb);
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci	/* we have the register setup, start allocating framebuffers */
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	for (win = 0; win < fbdrv->variant.nr_windows; win++) {
14728c2ecf20Sopenharmony_ci		if (!pd->win[win])
14738c2ecf20Sopenharmony_ci			continue;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci		ret = s3c_fb_probe_win(sfb, win, fbdrv->win[win],
14768c2ecf20Sopenharmony_ci				       &sfb->windows[win]);
14778c2ecf20Sopenharmony_ci		if (ret < 0) {
14788c2ecf20Sopenharmony_ci			dev_err(dev, "failed to create window %d\n", win);
14798c2ecf20Sopenharmony_ci			for (; win >= 0; win--)
14808c2ecf20Sopenharmony_ci				s3c_fb_release_win(sfb, sfb->windows[win]);
14818c2ecf20Sopenharmony_ci			goto err_pm_runtime;
14828c2ecf20Sopenharmony_ci		}
14838c2ecf20Sopenharmony_ci	}
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, sfb);
14868c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	return 0;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_cierr_pm_runtime:
14918c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_cierr_lcd_clk:
14948c2ecf20Sopenharmony_ci	pm_runtime_disable(sfb->dev);
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
14978c2ecf20Sopenharmony_ci		clk_disable_unprepare(sfb->lcd_clk);
14988c2ecf20Sopenharmony_ci
14998c2ecf20Sopenharmony_cierr_bus_clk:
15008c2ecf20Sopenharmony_ci	clk_disable_unprepare(sfb->bus_clk);
15018c2ecf20Sopenharmony_ci
15028c2ecf20Sopenharmony_ci	return ret;
15038c2ecf20Sopenharmony_ci}
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_ci/**
15068c2ecf20Sopenharmony_ci * s3c_fb_remove() - Cleanup on module finalisation
15078c2ecf20Sopenharmony_ci * @pdev: The platform device we are bound to.
15088c2ecf20Sopenharmony_ci *
15098c2ecf20Sopenharmony_ci * Shutdown and then release all the resources that the driver allocated
15108c2ecf20Sopenharmony_ci * on initialisation.
15118c2ecf20Sopenharmony_ci */
15128c2ecf20Sopenharmony_cistatic int s3c_fb_remove(struct platform_device *pdev)
15138c2ecf20Sopenharmony_ci{
15148c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = platform_get_drvdata(pdev);
15158c2ecf20Sopenharmony_ci	int win;
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci	for (win = 0; win < S3C_FB_MAX_WIN; win++)
15208c2ecf20Sopenharmony_ci		if (sfb->windows[win])
15218c2ecf20Sopenharmony_ci			s3c_fb_release_win(sfb, sfb->windows[win]);
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
15248c2ecf20Sopenharmony_ci		clk_disable_unprepare(sfb->lcd_clk);
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ci	clk_disable_unprepare(sfb->bus_clk);
15278c2ecf20Sopenharmony_ci
15288c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
15298c2ecf20Sopenharmony_ci	pm_runtime_disable(sfb->dev);
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_ci	return 0;
15328c2ecf20Sopenharmony_ci}
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
15358c2ecf20Sopenharmony_cistatic int s3c_fb_suspend(struct device *dev)
15368c2ecf20Sopenharmony_ci{
15378c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = dev_get_drvdata(dev);
15388c2ecf20Sopenharmony_ci	struct s3c_fb_win *win;
15398c2ecf20Sopenharmony_ci	int win_no;
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci	for (win_no = S3C_FB_MAX_WIN - 1; win_no >= 0; win_no--) {
15448c2ecf20Sopenharmony_ci		win = sfb->windows[win_no];
15458c2ecf20Sopenharmony_ci		if (!win)
15468c2ecf20Sopenharmony_ci			continue;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci		/* use the blank function to push into power-down */
15498c2ecf20Sopenharmony_ci		s3c_fb_blank(FB_BLANK_POWERDOWN, win->fbinfo);
15508c2ecf20Sopenharmony_ci	}
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
15538c2ecf20Sopenharmony_ci		clk_disable_unprepare(sfb->lcd_clk);
15548c2ecf20Sopenharmony_ci
15558c2ecf20Sopenharmony_ci	clk_disable_unprepare(sfb->bus_clk);
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	return 0;
15608c2ecf20Sopenharmony_ci}
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_cistatic int s3c_fb_resume(struct device *dev)
15638c2ecf20Sopenharmony_ci{
15648c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = dev_get_drvdata(dev);
15658c2ecf20Sopenharmony_ci	struct s3c_fb_platdata *pd = sfb->pdata;
15668c2ecf20Sopenharmony_ci	struct s3c_fb_win *win;
15678c2ecf20Sopenharmony_ci	int win_no;
15688c2ecf20Sopenharmony_ci	u32 reg;
15698c2ecf20Sopenharmony_ci
15708c2ecf20Sopenharmony_ci	pm_runtime_get_sync(sfb->dev);
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	clk_prepare_enable(sfb->bus_clk);
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
15758c2ecf20Sopenharmony_ci		clk_prepare_enable(sfb->lcd_clk);
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	/* setup gpio and output polarity controls */
15788c2ecf20Sopenharmony_ci	pd->setup_gpio();
15798c2ecf20Sopenharmony_ci	writel(pd->vidcon1, sfb->regs + VIDCON1);
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci	/* set video clock running at under-run */
15828c2ecf20Sopenharmony_ci	if (sfb->variant.has_fixvclk) {
15838c2ecf20Sopenharmony_ci		reg = readl(sfb->regs + VIDCON1);
15848c2ecf20Sopenharmony_ci		reg &= ~VIDCON1_VCLK_MASK;
15858c2ecf20Sopenharmony_ci		reg |= VIDCON1_VCLK_RUN;
15868c2ecf20Sopenharmony_ci		writel(reg, sfb->regs + VIDCON1);
15878c2ecf20Sopenharmony_ci	}
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	/* zero all windows before we do anything */
15908c2ecf20Sopenharmony_ci	for (win_no = 0; win_no < sfb->variant.nr_windows; win_no++)
15918c2ecf20Sopenharmony_ci		s3c_fb_clear_win(sfb, win_no);
15928c2ecf20Sopenharmony_ci
15938c2ecf20Sopenharmony_ci	for (win_no = 0; win_no < sfb->variant.nr_windows - 1; win_no++) {
15948c2ecf20Sopenharmony_ci		void __iomem *regs = sfb->regs + sfb->variant.keycon;
15958c2ecf20Sopenharmony_ci		win = sfb->windows[win_no];
15968c2ecf20Sopenharmony_ci		if (!win)
15978c2ecf20Sopenharmony_ci			continue;
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci		shadow_protect_win(win, 1);
16008c2ecf20Sopenharmony_ci		regs += (win_no * 8);
16018c2ecf20Sopenharmony_ci		writel(0xffffff, regs + WKEYCON0);
16028c2ecf20Sopenharmony_ci		writel(0xffffff, regs + WKEYCON1);
16038c2ecf20Sopenharmony_ci		shadow_protect_win(win, 0);
16048c2ecf20Sopenharmony_ci	}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci	s3c_fb_set_rgb_timing(sfb);
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	/* restore framebuffers */
16098c2ecf20Sopenharmony_ci	for (win_no = 0; win_no < S3C_FB_MAX_WIN; win_no++) {
16108c2ecf20Sopenharmony_ci		win = sfb->windows[win_no];
16118c2ecf20Sopenharmony_ci		if (!win)
16128c2ecf20Sopenharmony_ci			continue;
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci		dev_dbg(dev, "resuming window %d\n", win_no);
16158c2ecf20Sopenharmony_ci		s3c_fb_set_par(win->fbinfo);
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	pm_runtime_put_sync(sfb->dev);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	return 0;
16218c2ecf20Sopenharmony_ci}
16228c2ecf20Sopenharmony_ci#endif
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
16258c2ecf20Sopenharmony_cistatic int s3c_fb_runtime_suspend(struct device *dev)
16268c2ecf20Sopenharmony_ci{
16278c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = dev_get_drvdata(dev);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
16308c2ecf20Sopenharmony_ci		clk_disable_unprepare(sfb->lcd_clk);
16318c2ecf20Sopenharmony_ci
16328c2ecf20Sopenharmony_ci	clk_disable_unprepare(sfb->bus_clk);
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	return 0;
16358c2ecf20Sopenharmony_ci}
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_cistatic int s3c_fb_runtime_resume(struct device *dev)
16388c2ecf20Sopenharmony_ci{
16398c2ecf20Sopenharmony_ci	struct s3c_fb *sfb = dev_get_drvdata(dev);
16408c2ecf20Sopenharmony_ci	struct s3c_fb_platdata *pd = sfb->pdata;
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	clk_prepare_enable(sfb->bus_clk);
16438c2ecf20Sopenharmony_ci
16448c2ecf20Sopenharmony_ci	if (!sfb->variant.has_clksel)
16458c2ecf20Sopenharmony_ci		clk_prepare_enable(sfb->lcd_clk);
16468c2ecf20Sopenharmony_ci
16478c2ecf20Sopenharmony_ci	/* setup gpio and output polarity controls */
16488c2ecf20Sopenharmony_ci	pd->setup_gpio();
16498c2ecf20Sopenharmony_ci	writel(pd->vidcon1, sfb->regs + VIDCON1);
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	return 0;
16528c2ecf20Sopenharmony_ci}
16538c2ecf20Sopenharmony_ci#endif
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci#define VALID_BPP124 (VALID_BPP(1) | VALID_BPP(2) | VALID_BPP(4))
16568c2ecf20Sopenharmony_ci#define VALID_BPP1248 (VALID_BPP124 | VALID_BPP(8))
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cistatic struct s3c_fb_win_variant s3c_fb_data_64xx_wins[] = {
16598c2ecf20Sopenharmony_ci	[0] = {
16608c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
16618c2ecf20Sopenharmony_ci		.osd_size_off	= 0x8,
16628c2ecf20Sopenharmony_ci		.palette_sz	= 256,
16638c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP1248 | VALID_BPP(16) |
16648c2ecf20Sopenharmony_ci				   VALID_BPP(18) | VALID_BPP(24)),
16658c2ecf20Sopenharmony_ci	},
16668c2ecf20Sopenharmony_ci	[1] = {
16678c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
16688c2ecf20Sopenharmony_ci		.has_osd_d	= 1,
16698c2ecf20Sopenharmony_ci		.osd_size_off	= 0xc,
16708c2ecf20Sopenharmony_ci		.has_osd_alpha	= 1,
16718c2ecf20Sopenharmony_ci		.palette_sz	= 256,
16728c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP1248 | VALID_BPP(16) |
16738c2ecf20Sopenharmony_ci				   VALID_BPP(18) | VALID_BPP(19) |
16748c2ecf20Sopenharmony_ci				   VALID_BPP(24) | VALID_BPP(25) |
16758c2ecf20Sopenharmony_ci				   VALID_BPP(28)),
16768c2ecf20Sopenharmony_ci	},
16778c2ecf20Sopenharmony_ci	[2] = {
16788c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
16798c2ecf20Sopenharmony_ci		.has_osd_d	= 1,
16808c2ecf20Sopenharmony_ci		.osd_size_off	= 0xc,
16818c2ecf20Sopenharmony_ci		.has_osd_alpha	= 1,
16828c2ecf20Sopenharmony_ci		.palette_sz	= 16,
16838c2ecf20Sopenharmony_ci		.palette_16bpp	= 1,
16848c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP1248 | VALID_BPP(16) |
16858c2ecf20Sopenharmony_ci				   VALID_BPP(18) | VALID_BPP(19) |
16868c2ecf20Sopenharmony_ci				   VALID_BPP(24) | VALID_BPP(25) |
16878c2ecf20Sopenharmony_ci				   VALID_BPP(28)),
16888c2ecf20Sopenharmony_ci	},
16898c2ecf20Sopenharmony_ci	[3] = {
16908c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
16918c2ecf20Sopenharmony_ci		.has_osd_alpha	= 1,
16928c2ecf20Sopenharmony_ci		.palette_sz	= 16,
16938c2ecf20Sopenharmony_ci		.palette_16bpp	= 1,
16948c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP124  | VALID_BPP(16) |
16958c2ecf20Sopenharmony_ci				   VALID_BPP(18) | VALID_BPP(19) |
16968c2ecf20Sopenharmony_ci				   VALID_BPP(24) | VALID_BPP(25) |
16978c2ecf20Sopenharmony_ci				   VALID_BPP(28)),
16988c2ecf20Sopenharmony_ci	},
16998c2ecf20Sopenharmony_ci	[4] = {
17008c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
17018c2ecf20Sopenharmony_ci		.has_osd_alpha	= 1,
17028c2ecf20Sopenharmony_ci		.palette_sz	= 4,
17038c2ecf20Sopenharmony_ci		.palette_16bpp	= 1,
17048c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP(1) | VALID_BPP(2) |
17058c2ecf20Sopenharmony_ci				   VALID_BPP(16) | VALID_BPP(18) |
17068c2ecf20Sopenharmony_ci				   VALID_BPP(19) | VALID_BPP(24) |
17078c2ecf20Sopenharmony_ci				   VALID_BPP(25) | VALID_BPP(28)),
17088c2ecf20Sopenharmony_ci	},
17098c2ecf20Sopenharmony_ci};
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_cistatic struct s3c_fb_driverdata s3c_fb_data_64xx = {
17128c2ecf20Sopenharmony_ci	.variant = {
17138c2ecf20Sopenharmony_ci		.nr_windows	= 5,
17148c2ecf20Sopenharmony_ci		.vidtcon	= VIDTCON0,
17158c2ecf20Sopenharmony_ci		.wincon		= WINCON(0),
17168c2ecf20Sopenharmony_ci		.winmap		= WINxMAP(0),
17178c2ecf20Sopenharmony_ci		.keycon		= WKEYCON,
17188c2ecf20Sopenharmony_ci		.osd		= VIDOSD_BASE,
17198c2ecf20Sopenharmony_ci		.osd_stride	= 16,
17208c2ecf20Sopenharmony_ci		.buf_start	= VIDW_BUF_START(0),
17218c2ecf20Sopenharmony_ci		.buf_size	= VIDW_BUF_SIZE(0),
17228c2ecf20Sopenharmony_ci		.buf_end	= VIDW_BUF_END(0),
17238c2ecf20Sopenharmony_ci
17248c2ecf20Sopenharmony_ci		.palette = {
17258c2ecf20Sopenharmony_ci			[0] = 0x400,
17268c2ecf20Sopenharmony_ci			[1] = 0x800,
17278c2ecf20Sopenharmony_ci			[2] = 0x300,
17288c2ecf20Sopenharmony_ci			[3] = 0x320,
17298c2ecf20Sopenharmony_ci			[4] = 0x340,
17308c2ecf20Sopenharmony_ci		},
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci		.has_prtcon	= 1,
17338c2ecf20Sopenharmony_ci		.has_clksel	= 1,
17348c2ecf20Sopenharmony_ci	},
17358c2ecf20Sopenharmony_ci	.win[0]	= &s3c_fb_data_64xx_wins[0],
17368c2ecf20Sopenharmony_ci	.win[1]	= &s3c_fb_data_64xx_wins[1],
17378c2ecf20Sopenharmony_ci	.win[2]	= &s3c_fb_data_64xx_wins[2],
17388c2ecf20Sopenharmony_ci	.win[3]	= &s3c_fb_data_64xx_wins[3],
17398c2ecf20Sopenharmony_ci	.win[4]	= &s3c_fb_data_64xx_wins[4],
17408c2ecf20Sopenharmony_ci};
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci/* S3C2443/S3C2416 style hardware */
17438c2ecf20Sopenharmony_cistatic struct s3c_fb_driverdata s3c_fb_data_s3c2443 = {
17448c2ecf20Sopenharmony_ci	.variant = {
17458c2ecf20Sopenharmony_ci		.nr_windows	= 2,
17468c2ecf20Sopenharmony_ci		.is_2443	= 1,
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci		.vidtcon	= 0x08,
17498c2ecf20Sopenharmony_ci		.wincon		= 0x14,
17508c2ecf20Sopenharmony_ci		.winmap		= 0xd0,
17518c2ecf20Sopenharmony_ci		.keycon		= 0xb0,
17528c2ecf20Sopenharmony_ci		.osd		= 0x28,
17538c2ecf20Sopenharmony_ci		.osd_stride	= 12,
17548c2ecf20Sopenharmony_ci		.buf_start	= 0x64,
17558c2ecf20Sopenharmony_ci		.buf_size	= 0x94,
17568c2ecf20Sopenharmony_ci		.buf_end	= 0x7c,
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci		.palette = {
17598c2ecf20Sopenharmony_ci			[0] = 0x400,
17608c2ecf20Sopenharmony_ci			[1] = 0x800,
17618c2ecf20Sopenharmony_ci		},
17628c2ecf20Sopenharmony_ci		.has_clksel	= 1,
17638c2ecf20Sopenharmony_ci	},
17648c2ecf20Sopenharmony_ci	.win[0] = &(struct s3c_fb_win_variant) {
17658c2ecf20Sopenharmony_ci		.palette_sz	= 256,
17668c2ecf20Sopenharmony_ci		.valid_bpp	= VALID_BPP1248 | VALID_BPP(16) | VALID_BPP(24),
17678c2ecf20Sopenharmony_ci	},
17688c2ecf20Sopenharmony_ci	.win[1] = &(struct s3c_fb_win_variant) {
17698c2ecf20Sopenharmony_ci		.has_osd_c	= 1,
17708c2ecf20Sopenharmony_ci		.has_osd_alpha	= 1,
17718c2ecf20Sopenharmony_ci		.palette_sz	= 256,
17728c2ecf20Sopenharmony_ci		.valid_bpp	= (VALID_BPP1248 | VALID_BPP(16) |
17738c2ecf20Sopenharmony_ci				   VALID_BPP(18) | VALID_BPP(19) |
17748c2ecf20Sopenharmony_ci				   VALID_BPP(24) | VALID_BPP(25) |
17758c2ecf20Sopenharmony_ci				   VALID_BPP(28)),
17768c2ecf20Sopenharmony_ci	},
17778c2ecf20Sopenharmony_ci};
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_cistatic const struct platform_device_id s3c_fb_driver_ids[] = {
17808c2ecf20Sopenharmony_ci	{
17818c2ecf20Sopenharmony_ci		.name		= "s3c-fb",
17828c2ecf20Sopenharmony_ci		.driver_data	= (unsigned long)&s3c_fb_data_64xx,
17838c2ecf20Sopenharmony_ci	}, {
17848c2ecf20Sopenharmony_ci		.name		= "s3c2443-fb",
17858c2ecf20Sopenharmony_ci		.driver_data	= (unsigned long)&s3c_fb_data_s3c2443,
17868c2ecf20Sopenharmony_ci	},
17878c2ecf20Sopenharmony_ci	{},
17888c2ecf20Sopenharmony_ci};
17898c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, s3c_fb_driver_ids);
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_cistatic const struct dev_pm_ops s3cfb_pm_ops = {
17928c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(s3c_fb_suspend, s3c_fb_resume)
17938c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(s3c_fb_runtime_suspend, s3c_fb_runtime_resume,
17948c2ecf20Sopenharmony_ci			   NULL)
17958c2ecf20Sopenharmony_ci};
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_cistatic struct platform_driver s3c_fb_driver = {
17988c2ecf20Sopenharmony_ci	.probe		= s3c_fb_probe,
17998c2ecf20Sopenharmony_ci	.remove		= s3c_fb_remove,
18008c2ecf20Sopenharmony_ci	.id_table	= s3c_fb_driver_ids,
18018c2ecf20Sopenharmony_ci	.driver		= {
18028c2ecf20Sopenharmony_ci		.name	= "s3c-fb",
18038c2ecf20Sopenharmony_ci		.pm	= &s3cfb_pm_ops,
18048c2ecf20Sopenharmony_ci	},
18058c2ecf20Sopenharmony_ci};
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_cimodule_platform_driver(s3c_fb_driver);
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
18108c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Samsung S3C SoC Framebuffer driver");
18118c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
18128c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:s3c-fb");
1813