1 /*
2  * SuperH Mobile LCDC Framebuffer
3  *
4  * Copyright (c) 2008 Magnus Damm
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  */
10 
11 #include <linux/atomic.h>
12 #include <linux/backlight.h>
13 #include <linux/clk.h>
14 #include <linux/console.h>
15 #include <linux/ctype.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/delay.h>
18 #include <linux/fbcon.h>
19 #include <linux/gpio.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/ioctl.h>
23 #include <linux/kernel.h>
24 #include <linux/mm.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/videodev2.h>
30 #include <linux/vmalloc.h>
31 
32 #include <video/sh_mobile_lcdc.h>
33 
34 #include "sh_mobile_lcdcfb.h"
35 
36 /* ----------------------------------------------------------------------------
37  * Overlay register definitions
38  */
39 
40 #define LDBCR			0xb00
41 #define LDBCR_UPC(n)		(1 << ((n) + 16))
42 #define LDBCR_UPF(n)		(1 << ((n) + 8))
43 #define LDBCR_UPD(n)		(1 << ((n) + 0))
44 #define LDBnBSIFR(n)		(0xb20 + (n) * 0x20 + 0x00)
45 #define LDBBSIFR_EN		(1 << 31)
46 #define LDBBSIFR_VS		(1 << 29)
47 #define LDBBSIFR_BRSEL		(1 << 28)
48 #define LDBBSIFR_MX		(1 << 27)
49 #define LDBBSIFR_MY		(1 << 26)
50 #define LDBBSIFR_CV3		(3 << 24)
51 #define LDBBSIFR_CV2		(2 << 24)
52 #define LDBBSIFR_CV1		(1 << 24)
53 #define LDBBSIFR_CV0		(0 << 24)
54 #define LDBBSIFR_CV_MASK	(3 << 24)
55 #define LDBBSIFR_LAY_MASK	(0xff << 16)
56 #define LDBBSIFR_LAY_SHIFT	16
57 #define LDBBSIFR_ROP3_MASK	(0xff << 16)
58 #define LDBBSIFR_ROP3_SHIFT	16
59 #define LDBBSIFR_AL_PL8		(3 << 14)
60 #define LDBBSIFR_AL_PL1		(2 << 14)
61 #define LDBBSIFR_AL_PK		(1 << 14)
62 #define LDBBSIFR_AL_1		(0 << 14)
63 #define LDBBSIFR_AL_MASK	(3 << 14)
64 #define LDBBSIFR_SWPL		(1 << 10)
65 #define LDBBSIFR_SWPW		(1 << 9)
66 #define LDBBSIFR_SWPB		(1 << 8)
67 #define LDBBSIFR_RY		(1 << 7)
68 #define LDBBSIFR_CHRR_420	(2 << 0)
69 #define LDBBSIFR_CHRR_422	(1 << 0)
70 #define LDBBSIFR_CHRR_444	(0 << 0)
71 #define LDBBSIFR_RPKF_ARGB32	(0x00 << 0)
72 #define LDBBSIFR_RPKF_RGB16	(0x03 << 0)
73 #define LDBBSIFR_RPKF_RGB24	(0x0b << 0)
74 #define LDBBSIFR_RPKF_MASK	(0x1f << 0)
75 #define LDBnBSSZR(n)		(0xb20 + (n) * 0x20 + 0x04)
76 #define LDBBSSZR_BVSS_MASK	(0xfff << 16)
77 #define LDBBSSZR_BVSS_SHIFT	16
78 #define LDBBSSZR_BHSS_MASK	(0xfff << 0)
79 #define LDBBSSZR_BHSS_SHIFT	0
80 #define LDBnBLOCR(n)		(0xb20 + (n) * 0x20 + 0x08)
81 #define LDBBLOCR_CVLC_MASK	(0xfff << 16)
82 #define LDBBLOCR_CVLC_SHIFT	16
83 #define LDBBLOCR_CHLC_MASK	(0xfff << 0)
84 #define LDBBLOCR_CHLC_SHIFT	0
85 #define LDBnBSMWR(n)		(0xb20 + (n) * 0x20 + 0x0c)
86 #define LDBBSMWR_BSMWA_MASK	(0xffff << 16)
87 #define LDBBSMWR_BSMWA_SHIFT	16
88 #define LDBBSMWR_BSMW_MASK	(0xffff << 0)
89 #define LDBBSMWR_BSMW_SHIFT	0
90 #define LDBnBSAYR(n)		(0xb20 + (n) * 0x20 + 0x10)
91 #define LDBBSAYR_FG1A_MASK	(0xff << 24)
92 #define LDBBSAYR_FG1A_SHIFT	24
93 #define LDBBSAYR_FG1R_MASK	(0xff << 16)
94 #define LDBBSAYR_FG1R_SHIFT	16
95 #define LDBBSAYR_FG1G_MASK	(0xff << 8)
96 #define LDBBSAYR_FG1G_SHIFT	8
97 #define LDBBSAYR_FG1B_MASK	(0xff << 0)
98 #define LDBBSAYR_FG1B_SHIFT	0
99 #define LDBnBSACR(n)		(0xb20 + (n) * 0x20 + 0x14)
100 #define LDBBSACR_FG2A_MASK	(0xff << 24)
101 #define LDBBSACR_FG2A_SHIFT	24
102 #define LDBBSACR_FG2R_MASK	(0xff << 16)
103 #define LDBBSACR_FG2R_SHIFT	16
104 #define LDBBSACR_FG2G_MASK	(0xff << 8)
105 #define LDBBSACR_FG2G_SHIFT	8
106 #define LDBBSACR_FG2B_MASK	(0xff << 0)
107 #define LDBBSACR_FG2B_SHIFT	0
108 #define LDBnBSAAR(n)		(0xb20 + (n) * 0x20 + 0x18)
109 #define LDBBSAAR_AP_MASK	(0xff << 24)
110 #define LDBBSAAR_AP_SHIFT	24
111 #define LDBBSAAR_R_MASK		(0xff << 16)
112 #define LDBBSAAR_R_SHIFT	16
113 #define LDBBSAAR_GY_MASK	(0xff << 8)
114 #define LDBBSAAR_GY_SHIFT	8
115 #define LDBBSAAR_B_MASK		(0xff << 0)
116 #define LDBBSAAR_B_SHIFT	0
117 #define LDBnBPPCR(n)		(0xb20 + (n) * 0x20 + 0x1c)
118 #define LDBBPPCR_AP_MASK	(0xff << 24)
119 #define LDBBPPCR_AP_SHIFT	24
120 #define LDBBPPCR_R_MASK		(0xff << 16)
121 #define LDBBPPCR_R_SHIFT	16
122 #define LDBBPPCR_GY_MASK	(0xff << 8)
123 #define LDBBPPCR_GY_SHIFT	8
124 #define LDBBPPCR_B_MASK		(0xff << 0)
125 #define LDBBPPCR_B_SHIFT	0
126 #define LDBnBBGCL(n)		(0xb10 + (n) * 0x04)
127 #define LDBBBGCL_BGA_MASK	(0xff << 24)
128 #define LDBBBGCL_BGA_SHIFT	24
129 #define LDBBBGCL_BGR_MASK	(0xff << 16)
130 #define LDBBBGCL_BGR_SHIFT	16
131 #define LDBBBGCL_BGG_MASK	(0xff << 8)
132 #define LDBBBGCL_BGG_SHIFT	8
133 #define LDBBBGCL_BGB_MASK	(0xff << 0)
134 #define LDBBBGCL_BGB_SHIFT	0
135 
136 #define SIDE_B_OFFSET 0x1000
137 #define MIRROR_OFFSET 0x2000
138 
139 #define MAX_XRES 1920
140 #define MAX_YRES 1080
141 
142 enum sh_mobile_lcdc_overlay_mode {
143 	LCDC_OVERLAY_BLEND,
144 	LCDC_OVERLAY_ROP3,
145 };
146 
147 /*
148  * struct sh_mobile_lcdc_overlay - LCDC display overlay
149  *
150  * @channel: LCDC channel this overlay belongs to
151  * @cfg: Overlay configuration
152  * @info: Frame buffer device
153  * @index: Overlay index (0-3)
154  * @base: Overlay registers base address
155  * @enabled: True if the overlay is enabled
156  * @mode: Overlay blending mode (alpha blend or ROP3)
157  * @alpha: Global alpha blending value (0-255, for alpha blending mode)
158  * @rop3: Raster operation (for ROP3 mode)
159  * @fb_mem: Frame buffer virtual memory address
160  * @fb_size: Frame buffer size in bytes
161  * @dma_handle: Frame buffer DMA address
162  * @base_addr_y: Overlay base address (RGB or luma component)
163  * @base_addr_c: Overlay base address (chroma component)
164  * @pan_y_offset: Panning linear offset in bytes (luma component)
165  * @format: Current pixelf format
166  * @xres: Horizontal visible resolution
167  * @xres_virtual: Horizontal total resolution
168  * @yres: Vertical visible resolution
169  * @yres_virtual: Vertical total resolution
170  * @pitch: Overlay line pitch
171  * @pos_x: Horizontal overlay position
172  * @pos_y: Vertical overlay position
173  */
174 struct sh_mobile_lcdc_overlay {
175 	struct sh_mobile_lcdc_chan *channel;
176 
177 	const struct sh_mobile_lcdc_overlay_cfg *cfg;
178 	struct fb_info *info;
179 
180 	unsigned int index;
181 	unsigned long base;
182 
183 	bool enabled;
184 	enum sh_mobile_lcdc_overlay_mode mode;
185 	unsigned int alpha;
186 	unsigned int rop3;
187 
188 	void *fb_mem;
189 	unsigned long fb_size;
190 
191 	dma_addr_t dma_handle;
192 	unsigned long base_addr_y;
193 	unsigned long base_addr_c;
194 	unsigned long pan_y_offset;
195 
196 	const struct sh_mobile_lcdc_format_info *format;
197 	unsigned int xres;
198 	unsigned int xres_virtual;
199 	unsigned int yres;
200 	unsigned int yres_virtual;
201 	unsigned int pitch;
202 	int pos_x;
203 	int pos_y;
204 };
205 
206 struct sh_mobile_lcdc_priv {
207 	void __iomem *base;
208 	int irq;
209 	atomic_t hw_usecnt;
210 	struct device *dev;
211 	struct clk *dot_clk;
212 	unsigned long lddckr;
213 
214 	struct sh_mobile_lcdc_chan ch[2];
215 	struct sh_mobile_lcdc_overlay overlays[4];
216 
217 	int started;
218 	int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
219 };
220 
221 /* -----------------------------------------------------------------------------
222  * Registers access
223  */
224 
225 static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
226 	[LDDCKPAT1R] = 0x400,
227 	[LDDCKPAT2R] = 0x404,
228 	[LDMT1R] = 0x418,
229 	[LDMT2R] = 0x41c,
230 	[LDMT3R] = 0x420,
231 	[LDDFR] = 0x424,
232 	[LDSM1R] = 0x428,
233 	[LDSM2R] = 0x42c,
234 	[LDSA1R] = 0x430,
235 	[LDSA2R] = 0x434,
236 	[LDMLSR] = 0x438,
237 	[LDHCNR] = 0x448,
238 	[LDHSYNR] = 0x44c,
239 	[LDVLNR] = 0x450,
240 	[LDVSYNR] = 0x454,
241 	[LDPMR] = 0x460,
242 	[LDHAJR] = 0x4a0,
243 };
244 
245 static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
246 	[LDDCKPAT1R] = 0x408,
247 	[LDDCKPAT2R] = 0x40c,
248 	[LDMT1R] = 0x600,
249 	[LDMT2R] = 0x604,
250 	[LDMT3R] = 0x608,
251 	[LDDFR] = 0x60c,
252 	[LDSM1R] = 0x610,
253 	[LDSM2R] = 0x614,
254 	[LDSA1R] = 0x618,
255 	[LDMLSR] = 0x620,
256 	[LDHCNR] = 0x624,
257 	[LDHSYNR] = 0x628,
258 	[LDVLNR] = 0x62c,
259 	[LDVSYNR] = 0x630,
260 	[LDPMR] = 0x63c,
261 };
262 
263 static bool banked(int reg_nr)
264 {
265 	switch (reg_nr) {
266 	case LDMT1R:
267 	case LDMT2R:
268 	case LDMT3R:
269 	case LDDFR:
270 	case LDSM1R:
271 	case LDSA1R:
272 	case LDSA2R:
273 	case LDMLSR:
274 	case LDHCNR:
275 	case LDHSYNR:
276 	case LDVLNR:
277 	case LDVSYNR:
278 		return true;
279 	}
280 	return false;
281 }
282 
283 static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
284 {
285 	return chan->cfg->chan == LCDC_CHAN_SUBLCD;
286 }
287 
288 static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
289 			    int reg_nr, unsigned long data)
290 {
291 	iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
292 	if (banked(reg_nr))
293 		iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
294 			  SIDE_B_OFFSET);
295 }
296 
297 static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
298 			    int reg_nr, unsigned long data)
299 {
300 	iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
301 		  MIRROR_OFFSET);
302 }
303 
304 static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
305 				    int reg_nr)
306 {
307 	return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
308 }
309 
310 static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
311 			       int reg, unsigned long data)
312 {
313 	iowrite32(data, ovl->channel->lcdc->base + reg);
314 	iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
315 }
316 
317 static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
318 		       unsigned long reg_offs, unsigned long data)
319 {
320 	iowrite32(data, priv->base + reg_offs);
321 }
322 
323 static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
324 			       unsigned long reg_offs)
325 {
326 	return ioread32(priv->base + reg_offs);
327 }
328 
329 static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
330 			  unsigned long reg_offs,
331 			  unsigned long mask, unsigned long until)
332 {
333 	while ((lcdc_read(priv, reg_offs) & mask) != until)
334 		cpu_relax();
335 }
336 
337 /* -----------------------------------------------------------------------------
338  * Clock management
339  */
340 
341 static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
342 {
343 	if (atomic_inc_and_test(&priv->hw_usecnt)) {
344 		clk_prepare_enable(priv->dot_clk);
345 		pm_runtime_get_sync(priv->dev);
346 	}
347 }
348 
349 static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
350 {
351 	if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
352 		pm_runtime_put(priv->dev);
353 		clk_disable_unprepare(priv->dot_clk);
354 	}
355 }
356 
357 static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
358 				       int clock_source)
359 {
360 	struct clk *clk;
361 	char *str;
362 
363 	switch (clock_source) {
364 	case LCDC_CLK_BUS:
365 		str = "bus_clk";
366 		priv->lddckr = LDDCKR_ICKSEL_BUS;
367 		break;
368 	case LCDC_CLK_PERIPHERAL:
369 		str = "peripheral_clk";
370 		priv->lddckr = LDDCKR_ICKSEL_MIPI;
371 		break;
372 	case LCDC_CLK_EXTERNAL:
373 		str = NULL;
374 		priv->lddckr = LDDCKR_ICKSEL_HDMI;
375 		break;
376 	default:
377 		return -EINVAL;
378 	}
379 
380 	if (str == NULL)
381 		return 0;
382 
383 	clk = clk_get(priv->dev, str);
384 	if (IS_ERR(clk)) {
385 		dev_err(priv->dev, "cannot get dot clock %s\n", str);
386 		return PTR_ERR(clk);
387 	}
388 
389 	priv->dot_clk = clk;
390 	return 0;
391 }
392 
393 /* -----------------------------------------------------------------------------
394  * Display, panel and deferred I/O
395  */
396 
397 static void lcdc_sys_write_index(void *handle, unsigned long data)
398 {
399 	struct sh_mobile_lcdc_chan *ch = handle;
400 
401 	lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
402 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
403 	lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
404 		   (lcdc_chan_is_sublcd(ch) ? 2 : 0));
405 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
406 }
407 
408 static void lcdc_sys_write_data(void *handle, unsigned long data)
409 {
410 	struct sh_mobile_lcdc_chan *ch = handle;
411 
412 	lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
413 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
414 	lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
415 		   (lcdc_chan_is_sublcd(ch) ? 2 : 0));
416 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
417 }
418 
419 static unsigned long lcdc_sys_read_data(void *handle)
420 {
421 	struct sh_mobile_lcdc_chan *ch = handle;
422 
423 	lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
424 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
425 	lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
426 		   (lcdc_chan_is_sublcd(ch) ? 2 : 0));
427 	udelay(1);
428 	lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
429 
430 	return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
431 }
432 
433 static struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
434 	.write_index	= lcdc_sys_write_index,
435 	.write_data	= lcdc_sys_write_data,
436 	.read_data	= lcdc_sys_read_data,
437 };
438 
439 static int sh_mobile_lcdc_sginit(struct fb_info *info,
440 				  struct list_head *pagelist)
441 {
442 	struct sh_mobile_lcdc_chan *ch = info->par;
443 	unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
444 	struct page *page;
445 	int nr_pages = 0;
446 
447 	sg_init_table(ch->sglist, nr_pages_max);
448 
449 	list_for_each_entry(page, pagelist, lru)
450 		sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
451 
452 	return nr_pages;
453 }
454 
455 static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
456 				       struct list_head *pagelist)
457 {
458 	struct sh_mobile_lcdc_chan *ch = info->par;
459 	const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
460 
461 	/* enable clocks before accessing hardware */
462 	sh_mobile_lcdc_clk_on(ch->lcdc);
463 
464 	/*
465 	 * It's possible to get here without anything on the pagelist via
466 	 * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
467 	 * invocation. In the former case, the acceleration routines are
468 	 * stepped in to when using the framebuffer console causing the
469 	 * workqueue to be scheduled without any dirty pages on the list.
470 	 *
471 	 * Despite this, a panel update is still needed given that the
472 	 * acceleration routines have their own methods for writing in
473 	 * that still need to be updated.
474 	 *
475 	 * The fsync() and empty pagelist case could be optimized for,
476 	 * but we don't bother, as any application exhibiting such
477 	 * behaviour is fundamentally broken anyways.
478 	 */
479 	if (!list_empty(pagelist)) {
480 		unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
481 
482 		/* trigger panel update */
483 		dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
484 		if (panel->start_transfer)
485 			panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
486 		lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
487 		dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
488 			     DMA_TO_DEVICE);
489 	} else {
490 		if (panel->start_transfer)
491 			panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
492 		lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
493 	}
494 }
495 
496 static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
497 {
498 	struct fb_deferred_io *fbdefio = info->fbdefio;
499 
500 	if (fbdefio)
501 		schedule_delayed_work(&info->deferred_work, fbdefio->delay);
502 }
503 
504 static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
505 {
506 	const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
507 
508 	if (ch->tx_dev) {
509 		int ret;
510 
511 		ret = ch->tx_dev->ops->display_on(ch->tx_dev);
512 		if (ret < 0)
513 			return;
514 
515 		if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
516 			ch->info->state = FBINFO_STATE_SUSPENDED;
517 	}
518 
519 	/* HDMI must be enabled before LCDC configuration */
520 	if (panel->display_on)
521 		panel->display_on();
522 }
523 
524 static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
525 {
526 	const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
527 
528 	if (panel->display_off)
529 		panel->display_off();
530 
531 	if (ch->tx_dev)
532 		ch->tx_dev->ops->display_off(ch->tx_dev);
533 }
534 
535 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
536 				    struct fb_info *info);
537 
538 /* -----------------------------------------------------------------------------
539  * Format helpers
540  */
541 
542 struct sh_mobile_lcdc_format_info {
543 	u32 fourcc;
544 	unsigned int bpp;
545 	bool yuv;
546 	u32 lddfr;
547 };
548 
549 static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
550 	{
551 		.fourcc = V4L2_PIX_FMT_RGB565,
552 		.bpp = 16,
553 		.yuv = false,
554 		.lddfr = LDDFR_PKF_RGB16,
555 	}, {
556 		.fourcc = V4L2_PIX_FMT_BGR24,
557 		.bpp = 24,
558 		.yuv = false,
559 		.lddfr = LDDFR_PKF_RGB24,
560 	}, {
561 		.fourcc = V4L2_PIX_FMT_BGR32,
562 		.bpp = 32,
563 		.yuv = false,
564 		.lddfr = LDDFR_PKF_ARGB32,
565 	}, {
566 		.fourcc = V4L2_PIX_FMT_NV12,
567 		.bpp = 12,
568 		.yuv = true,
569 		.lddfr = LDDFR_CC | LDDFR_YF_420,
570 	}, {
571 		.fourcc = V4L2_PIX_FMT_NV21,
572 		.bpp = 12,
573 		.yuv = true,
574 		.lddfr = LDDFR_CC | LDDFR_YF_420,
575 	}, {
576 		.fourcc = V4L2_PIX_FMT_NV16,
577 		.bpp = 16,
578 		.yuv = true,
579 		.lddfr = LDDFR_CC | LDDFR_YF_422,
580 	}, {
581 		.fourcc = V4L2_PIX_FMT_NV61,
582 		.bpp = 16,
583 		.yuv = true,
584 		.lddfr = LDDFR_CC | LDDFR_YF_422,
585 	}, {
586 		.fourcc = V4L2_PIX_FMT_NV24,
587 		.bpp = 24,
588 		.yuv = true,
589 		.lddfr = LDDFR_CC | LDDFR_YF_444,
590 	}, {
591 		.fourcc = V4L2_PIX_FMT_NV42,
592 		.bpp = 24,
593 		.yuv = true,
594 		.lddfr = LDDFR_CC | LDDFR_YF_444,
595 	},
596 };
597 
598 static const struct sh_mobile_lcdc_format_info *
599 sh_mobile_format_info(u32 fourcc)
600 {
601 	unsigned int i;
602 
603 	for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
604 		if (sh_mobile_format_infos[i].fourcc == fourcc)
605 			return &sh_mobile_format_infos[i];
606 	}
607 
608 	return NULL;
609 }
610 
611 static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
612 {
613 	if (var->grayscale > 1)
614 		return var->grayscale;
615 
616 	switch (var->bits_per_pixel) {
617 	case 16:
618 		return V4L2_PIX_FMT_RGB565;
619 	case 24:
620 		return V4L2_PIX_FMT_BGR24;
621 	case 32:
622 		return V4L2_PIX_FMT_BGR32;
623 	default:
624 		return 0;
625 	}
626 }
627 
628 static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
629 {
630 	return var->grayscale > 1;
631 }
632 
633 /* -----------------------------------------------------------------------------
634  * Start, stop and IRQ
635  */
636 
637 static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
638 {
639 	struct sh_mobile_lcdc_priv *priv = data;
640 	struct sh_mobile_lcdc_chan *ch;
641 	unsigned long ldintr;
642 	int is_sub;
643 	int k;
644 
645 	/* Acknowledge interrupts and disable further VSYNC End IRQs. */
646 	ldintr = lcdc_read(priv, _LDINTR);
647 	lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
648 
649 	/* figure out if this interrupt is for main or sub lcd */
650 	is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
651 
652 	/* wake up channel and disable clocks */
653 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
654 		ch = &priv->ch[k];
655 
656 		if (!ch->enabled)
657 			continue;
658 
659 		/* Frame End */
660 		if (ldintr & LDINTR_FS) {
661 			if (is_sub == lcdc_chan_is_sublcd(ch)) {
662 				ch->frame_end = 1;
663 				wake_up(&ch->frame_end_wait);
664 
665 				sh_mobile_lcdc_clk_off(priv);
666 			}
667 		}
668 
669 		/* VSYNC End */
670 		if (ldintr & LDINTR_VES)
671 			complete(&ch->vsync_completion);
672 	}
673 
674 	return IRQ_HANDLED;
675 }
676 
677 static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
678 {
679 	unsigned long ldintr;
680 	int ret;
681 
682 	/* Enable VSync End interrupt and be careful not to acknowledge any
683 	 * pending interrupt.
684 	 */
685 	ldintr = lcdc_read(ch->lcdc, _LDINTR);
686 	ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
687 	lcdc_write(ch->lcdc, _LDINTR, ldintr);
688 
689 	ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
690 							msecs_to_jiffies(100));
691 	if (!ret)
692 		return -ETIMEDOUT;
693 
694 	return 0;
695 }
696 
697 static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
698 				      int start)
699 {
700 	unsigned long tmp = lcdc_read(priv, _LDCNT2R);
701 	int k;
702 
703 	/* start or stop the lcdc */
704 	if (start)
705 		lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
706 	else
707 		lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
708 
709 	/* wait until power is applied/stopped on all channels */
710 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
711 		if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
712 			while (1) {
713 				tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
714 				    & LDPMR_LPS;
715 				if (start && tmp == LDPMR_LPS)
716 					break;
717 				if (!start && tmp == 0)
718 					break;
719 				cpu_relax();
720 			}
721 
722 	if (!start)
723 		lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
724 }
725 
726 static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
727 {
728 	const struct fb_var_screeninfo *var = &ch->info->var;
729 	const struct fb_videomode *mode = &ch->display.mode;
730 	unsigned long h_total, hsync_pos, display_h_total;
731 	u32 tmp;
732 
733 	tmp = ch->ldmt1r_value;
734 	tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
735 	tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
736 	tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
737 	tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
738 	tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
739 	tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
740 	tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
741 	lcdc_write_chan(ch, LDMT1R, tmp);
742 
743 	/* setup SYS bus */
744 	lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
745 	lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
746 
747 	/* horizontal configuration */
748 	h_total = mode->xres + mode->hsync_len + mode->left_margin
749 		+ mode->right_margin;
750 	tmp = h_total / 8; /* HTCN */
751 	tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
752 	lcdc_write_chan(ch, LDHCNR, tmp);
753 
754 	hsync_pos = mode->xres + mode->right_margin;
755 	tmp = hsync_pos / 8; /* HSYNP */
756 	tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
757 	lcdc_write_chan(ch, LDHSYNR, tmp);
758 
759 	/* vertical configuration */
760 	tmp = mode->yres + mode->vsync_len + mode->upper_margin
761 	    + mode->lower_margin; /* VTLN */
762 	tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
763 	lcdc_write_chan(ch, LDVLNR, tmp);
764 
765 	tmp = mode->yres + mode->lower_margin; /* VSYNP */
766 	tmp |= mode->vsync_len << 16; /* VSYNW */
767 	lcdc_write_chan(ch, LDVSYNR, tmp);
768 
769 	/* Adjust horizontal synchronisation for HDMI */
770 	display_h_total = mode->xres + mode->hsync_len + mode->left_margin
771 			+ mode->right_margin;
772 	tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
773 	    | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
774 	lcdc_write_chan(ch, LDHAJR, tmp);
775 	lcdc_write_chan_mirror(ch, LDHAJR, tmp);
776 }
777 
778 static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
779 {
780 	u32 format = 0;
781 
782 	if (!ovl->enabled) {
783 		lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
784 		lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
785 		lcdc_write(ovl->channel->lcdc, LDBCR,
786 			   LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
787 		return;
788 	}
789 
790 	ovl->base_addr_y = ovl->dma_handle;
791 	ovl->base_addr_c = ovl->dma_handle
792 			 + ovl->xres_virtual * ovl->yres_virtual;
793 
794 	switch (ovl->mode) {
795 	case LCDC_OVERLAY_BLEND:
796 		format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
797 		break;
798 
799 	case LCDC_OVERLAY_ROP3:
800 		format = LDBBSIFR_EN | LDBBSIFR_BRSEL
801 		       | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
802 		break;
803 	}
804 
805 	switch (ovl->format->fourcc) {
806 	case V4L2_PIX_FMT_RGB565:
807 	case V4L2_PIX_FMT_NV21:
808 	case V4L2_PIX_FMT_NV61:
809 	case V4L2_PIX_FMT_NV42:
810 		format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
811 		break;
812 	case V4L2_PIX_FMT_BGR24:
813 	case V4L2_PIX_FMT_NV12:
814 	case V4L2_PIX_FMT_NV16:
815 	case V4L2_PIX_FMT_NV24:
816 		format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
817 		break;
818 	case V4L2_PIX_FMT_BGR32:
819 	default:
820 		format |= LDBBSIFR_SWPL;
821 		break;
822 	}
823 
824 	switch (ovl->format->fourcc) {
825 	case V4L2_PIX_FMT_RGB565:
826 		format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
827 		break;
828 	case V4L2_PIX_FMT_BGR24:
829 		format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
830 		break;
831 	case V4L2_PIX_FMT_BGR32:
832 		format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
833 		break;
834 	case V4L2_PIX_FMT_NV12:
835 	case V4L2_PIX_FMT_NV21:
836 		format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
837 		break;
838 	case V4L2_PIX_FMT_NV16:
839 	case V4L2_PIX_FMT_NV61:
840 		format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
841 		break;
842 	case V4L2_PIX_FMT_NV24:
843 	case V4L2_PIX_FMT_NV42:
844 		format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
845 		break;
846 	}
847 
848 	lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
849 
850 	lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
851 
852 	lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
853 		(ovl->yres << LDBBSSZR_BVSS_SHIFT) |
854 		(ovl->xres << LDBBSSZR_BHSS_SHIFT));
855 	lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
856 		(ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
857 		(ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
858 	lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
859 		ovl->pitch << LDBBSMWR_BSMW_SHIFT);
860 
861 	lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
862 	lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
863 
864 	lcdc_write(ovl->channel->lcdc, LDBCR,
865 		   LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
866 }
867 
868 /*
869  * __sh_mobile_lcdc_start - Configure and start the LCDC
870  * @priv: LCDC device
871  *
872  * Configure all enabled channels and start the LCDC device. All external
873  * devices (clocks, MERAM, panels, ...) are not touched by this function.
874  */
875 static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
876 {
877 	struct sh_mobile_lcdc_chan *ch;
878 	unsigned long tmp;
879 	int k, m;
880 
881 	/* Enable LCDC channels. Read data from external memory, avoid using the
882 	 * BEU for now.
883 	 */
884 	lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
885 
886 	/* Stop the LCDC first and disable all interrupts. */
887 	sh_mobile_lcdc_start_stop(priv, 0);
888 	lcdc_write(priv, _LDINTR, 0);
889 
890 	/* Configure power supply, dot clocks and start them. */
891 	tmp = priv->lddckr;
892 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
893 		ch = &priv->ch[k];
894 		if (!ch->enabled)
895 			continue;
896 
897 		/* Power supply */
898 		lcdc_write_chan(ch, LDPMR, 0);
899 
900 		m = ch->cfg->clock_divider;
901 		if (!m)
902 			continue;
903 
904 		/* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
905 		 * denominator.
906 		 */
907 		lcdc_write_chan(ch, LDDCKPAT1R, 0);
908 		lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
909 
910 		if (m == 1)
911 			m = LDDCKR_MOSEL;
912 		tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
913 	}
914 
915 	lcdc_write(priv, _LDDCKR, tmp);
916 	lcdc_write(priv, _LDDCKSTPR, 0);
917 	lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
918 
919 	/* Setup geometry, format, frame buffer memory and operation mode. */
920 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
921 		ch = &priv->ch[k];
922 		if (!ch->enabled)
923 			continue;
924 
925 		sh_mobile_lcdc_geometry(ch);
926 
927 		tmp = ch->format->lddfr;
928 
929 		if (ch->format->yuv) {
930 			switch (ch->colorspace) {
931 			case V4L2_COLORSPACE_REC709:
932 				tmp |= LDDFR_CF1;
933 				break;
934 			case V4L2_COLORSPACE_JPEG:
935 				tmp |= LDDFR_CF0;
936 				break;
937 			}
938 		}
939 
940 		lcdc_write_chan(ch, LDDFR, tmp);
941 		lcdc_write_chan(ch, LDMLSR, ch->line_size);
942 		lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
943 		if (ch->format->yuv)
944 			lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
945 
946 		/* When using deferred I/O mode, configure the LCDC for one-shot
947 		 * operation and enable the frame end interrupt. Otherwise use
948 		 * continuous read mode.
949 		 */
950 		if (ch->ldmt1r_value & LDMT1R_IFM &&
951 		    ch->cfg->sys_bus_cfg.deferred_io_msec) {
952 			lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
953 			lcdc_write(priv, _LDINTR, LDINTR_FE);
954 		} else {
955 			lcdc_write_chan(ch, LDSM1R, 0);
956 		}
957 	}
958 
959 	/* Word and long word swap. */
960 	switch (priv->ch[0].format->fourcc) {
961 	case V4L2_PIX_FMT_RGB565:
962 	case V4L2_PIX_FMT_NV21:
963 	case V4L2_PIX_FMT_NV61:
964 	case V4L2_PIX_FMT_NV42:
965 		tmp = LDDDSR_LS | LDDDSR_WS;
966 		break;
967 	case V4L2_PIX_FMT_BGR24:
968 	case V4L2_PIX_FMT_NV12:
969 	case V4L2_PIX_FMT_NV16:
970 	case V4L2_PIX_FMT_NV24:
971 		tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
972 		break;
973 	case V4L2_PIX_FMT_BGR32:
974 	default:
975 		tmp = LDDDSR_LS;
976 		break;
977 	}
978 	lcdc_write(priv, _LDDDSR, tmp);
979 
980 	/* Enable the display output. */
981 	lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
982 	sh_mobile_lcdc_start_stop(priv, 1);
983 	priv->started = 1;
984 }
985 
986 static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
987 {
988 	struct sh_mobile_lcdc_chan *ch;
989 	unsigned long tmp;
990 	int ret;
991 	int k;
992 
993 	/* enable clocks before accessing the hardware */
994 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
995 		if (priv->ch[k].enabled)
996 			sh_mobile_lcdc_clk_on(priv);
997 	}
998 
999 	/* reset */
1000 	lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
1001 	lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
1002 
1003 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1004 		const struct sh_mobile_lcdc_panel_cfg *panel;
1005 
1006 		ch = &priv->ch[k];
1007 		if (!ch->enabled)
1008 			continue;
1009 
1010 		panel = &ch->cfg->panel_cfg;
1011 		if (panel->setup_sys) {
1012 			ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
1013 			if (ret)
1014 				return ret;
1015 		}
1016 	}
1017 
1018 	/* Compute frame buffer base address and pitch for each channel. */
1019 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1020 		ch = &priv->ch[k];
1021 		if (!ch->enabled)
1022 			continue;
1023 
1024 		ch->base_addr_y = ch->dma_handle;
1025 		ch->base_addr_c = ch->dma_handle
1026 				+ ch->xres_virtual * ch->yres_virtual;
1027 		ch->line_size = ch->pitch;
1028 	}
1029 
1030 	for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
1031 		struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
1032 		sh_mobile_lcdc_overlay_setup(ovl);
1033 	}
1034 
1035 	/* Start the LCDC. */
1036 	__sh_mobile_lcdc_start(priv);
1037 
1038 	/* Setup deferred I/O, tell the board code to enable the panels, and
1039 	 * turn backlight on.
1040 	 */
1041 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1042 		ch = &priv->ch[k];
1043 		if (!ch->enabled)
1044 			continue;
1045 
1046 		tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
1047 		if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
1048 			ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
1049 			ch->defio.delay = msecs_to_jiffies(tmp);
1050 			ch->info->fbdefio = &ch->defio;
1051 			fb_deferred_io_init(ch->info);
1052 		}
1053 
1054 		sh_mobile_lcdc_display_on(ch);
1055 
1056 		if (ch->bl) {
1057 			ch->bl->props.power = FB_BLANK_UNBLANK;
1058 			backlight_update_status(ch->bl);
1059 		}
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
1066 {
1067 	struct sh_mobile_lcdc_chan *ch;
1068 	int k;
1069 
1070 	/* clean up deferred io and ask board code to disable panel */
1071 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
1072 		ch = &priv->ch[k];
1073 		if (!ch->enabled)
1074 			continue;
1075 
1076 		/* deferred io mode:
1077 		 * flush frame, and wait for frame end interrupt
1078 		 * clean up deferred io and enable clock
1079 		 */
1080 		if (ch->info && ch->info->fbdefio) {
1081 			ch->frame_end = 0;
1082 			schedule_delayed_work(&ch->info->deferred_work, 0);
1083 			wait_event(ch->frame_end_wait, ch->frame_end);
1084 			fb_deferred_io_cleanup(ch->info);
1085 			ch->info->fbdefio = NULL;
1086 			sh_mobile_lcdc_clk_on(priv);
1087 		}
1088 
1089 		if (ch->bl) {
1090 			ch->bl->props.power = FB_BLANK_POWERDOWN;
1091 			backlight_update_status(ch->bl);
1092 		}
1093 
1094 		sh_mobile_lcdc_display_off(ch);
1095 	}
1096 
1097 	/* stop the lcdc */
1098 	if (priv->started) {
1099 		sh_mobile_lcdc_start_stop(priv, 0);
1100 		priv->started = 0;
1101 	}
1102 
1103 	/* stop clocks */
1104 	for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
1105 		if (priv->ch[k].enabled)
1106 			sh_mobile_lcdc_clk_off(priv);
1107 }
1108 
1109 static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1110 				      struct fb_info *info)
1111 {
1112 	if (var->xres > MAX_XRES || var->yres > MAX_YRES)
1113 		return -EINVAL;
1114 
1115 	/* Make sure the virtual resolution is at least as big as the visible
1116 	 * resolution.
1117 	 */
1118 	if (var->xres_virtual < var->xres)
1119 		var->xres_virtual = var->xres;
1120 	if (var->yres_virtual < var->yres)
1121 		var->yres_virtual = var->yres;
1122 
1123 	if (sh_mobile_format_is_fourcc(var)) {
1124 		const struct sh_mobile_lcdc_format_info *format;
1125 
1126 		format = sh_mobile_format_info(var->grayscale);
1127 		if (format == NULL)
1128 			return -EINVAL;
1129 		var->bits_per_pixel = format->bpp;
1130 
1131 		/* Default to RGB and JPEG color-spaces for RGB and YUV formats
1132 		 * respectively.
1133 		 */
1134 		if (!format->yuv)
1135 			var->colorspace = V4L2_COLORSPACE_SRGB;
1136 		else if (var->colorspace != V4L2_COLORSPACE_REC709)
1137 			var->colorspace = V4L2_COLORSPACE_JPEG;
1138 	} else {
1139 		if (var->bits_per_pixel <= 16) {		/* RGB 565 */
1140 			var->bits_per_pixel = 16;
1141 			var->red.offset = 11;
1142 			var->red.length = 5;
1143 			var->green.offset = 5;
1144 			var->green.length = 6;
1145 			var->blue.offset = 0;
1146 			var->blue.length = 5;
1147 			var->transp.offset = 0;
1148 			var->transp.length = 0;
1149 		} else if (var->bits_per_pixel <= 24) {		/* RGB 888 */
1150 			var->bits_per_pixel = 24;
1151 			var->red.offset = 16;
1152 			var->red.length = 8;
1153 			var->green.offset = 8;
1154 			var->green.length = 8;
1155 			var->blue.offset = 0;
1156 			var->blue.length = 8;
1157 			var->transp.offset = 0;
1158 			var->transp.length = 0;
1159 		} else if (var->bits_per_pixel <= 32) {		/* RGBA 888 */
1160 			var->bits_per_pixel = 32;
1161 			var->red.offset = 16;
1162 			var->red.length = 8;
1163 			var->green.offset = 8;
1164 			var->green.length = 8;
1165 			var->blue.offset = 0;
1166 			var->blue.length = 8;
1167 			var->transp.offset = 24;
1168 			var->transp.length = 8;
1169 		} else
1170 			return -EINVAL;
1171 
1172 		var->red.msb_right = 0;
1173 		var->green.msb_right = 0;
1174 		var->blue.msb_right = 0;
1175 		var->transp.msb_right = 0;
1176 	}
1177 
1178 	/* Make sure we don't exceed our allocated memory. */
1179 	if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
1180 	    info->fix.smem_len)
1181 		return -EINVAL;
1182 
1183 	return 0;
1184 }
1185 
1186 /* -----------------------------------------------------------------------------
1187  * Frame buffer operations - Overlays
1188  */
1189 
1190 static ssize_t
1191 overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
1192 {
1193 	struct fb_info *info = dev_get_drvdata(dev);
1194 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1195 
1196 	return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
1197 }
1198 
1199 static ssize_t
1200 overlay_alpha_store(struct device *dev, struct device_attribute *attr,
1201 		    const char *buf, size_t count)
1202 {
1203 	struct fb_info *info = dev_get_drvdata(dev);
1204 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1205 	unsigned int alpha;
1206 	char *endp;
1207 
1208 	alpha = simple_strtoul(buf, &endp, 10);
1209 	if (isspace(*endp))
1210 		endp++;
1211 
1212 	if (endp - buf != count)
1213 		return -EINVAL;
1214 
1215 	if (alpha > 255)
1216 		return -EINVAL;
1217 
1218 	if (ovl->alpha != alpha) {
1219 		ovl->alpha = alpha;
1220 
1221 		if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
1222 			sh_mobile_lcdc_overlay_setup(ovl);
1223 	}
1224 
1225 	return count;
1226 }
1227 
1228 static ssize_t
1229 overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1230 {
1231 	struct fb_info *info = dev_get_drvdata(dev);
1232 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1233 
1234 	return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
1235 }
1236 
1237 static ssize_t
1238 overlay_mode_store(struct device *dev, struct device_attribute *attr,
1239 		   const char *buf, size_t count)
1240 {
1241 	struct fb_info *info = dev_get_drvdata(dev);
1242 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1243 	unsigned int mode;
1244 	char *endp;
1245 
1246 	mode = simple_strtoul(buf, &endp, 10);
1247 	if (isspace(*endp))
1248 		endp++;
1249 
1250 	if (endp - buf != count)
1251 		return -EINVAL;
1252 
1253 	if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
1254 		return -EINVAL;
1255 
1256 	if (ovl->mode != mode) {
1257 		ovl->mode = mode;
1258 
1259 		if (ovl->enabled)
1260 			sh_mobile_lcdc_overlay_setup(ovl);
1261 	}
1262 
1263 	return count;
1264 }
1265 
1266 static ssize_t
1267 overlay_position_show(struct device *dev, struct device_attribute *attr,
1268 		      char *buf)
1269 {
1270 	struct fb_info *info = dev_get_drvdata(dev);
1271 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1272 
1273 	return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
1274 }
1275 
1276 static ssize_t
1277 overlay_position_store(struct device *dev, struct device_attribute *attr,
1278 		       const char *buf, size_t count)
1279 {
1280 	struct fb_info *info = dev_get_drvdata(dev);
1281 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1282 	char *endp;
1283 	int pos_x;
1284 	int pos_y;
1285 
1286 	pos_x = simple_strtol(buf, &endp, 10);
1287 	if (*endp != ',')
1288 		return -EINVAL;
1289 
1290 	pos_y = simple_strtol(endp + 1, &endp, 10);
1291 	if (isspace(*endp))
1292 		endp++;
1293 
1294 	if (endp - buf != count)
1295 		return -EINVAL;
1296 
1297 	if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
1298 		ovl->pos_x = pos_x;
1299 		ovl->pos_y = pos_y;
1300 
1301 		if (ovl->enabled)
1302 			sh_mobile_lcdc_overlay_setup(ovl);
1303 	}
1304 
1305 	return count;
1306 }
1307 
1308 static ssize_t
1309 overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
1310 {
1311 	struct fb_info *info = dev_get_drvdata(dev);
1312 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1313 
1314 	return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
1315 }
1316 
1317 static ssize_t
1318 overlay_rop3_store(struct device *dev, struct device_attribute *attr,
1319 		    const char *buf, size_t count)
1320 {
1321 	struct fb_info *info = dev_get_drvdata(dev);
1322 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1323 	unsigned int rop3;
1324 	char *endp;
1325 
1326 	rop3 = simple_strtoul(buf, &endp, 10);
1327 	if (isspace(*endp))
1328 		endp++;
1329 
1330 	if (endp - buf != count)
1331 		return -EINVAL;
1332 
1333 	if (rop3 > 255)
1334 		return -EINVAL;
1335 
1336 	if (ovl->rop3 != rop3) {
1337 		ovl->rop3 = rop3;
1338 
1339 		if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
1340 			sh_mobile_lcdc_overlay_setup(ovl);
1341 	}
1342 
1343 	return count;
1344 }
1345 
1346 static const struct device_attribute overlay_sysfs_attrs[] = {
1347 	__ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
1348 	       overlay_alpha_show, overlay_alpha_store),
1349 	__ATTR(ovl_mode, S_IRUGO|S_IWUSR,
1350 	       overlay_mode_show, overlay_mode_store),
1351 	__ATTR(ovl_position, S_IRUGO|S_IWUSR,
1352 	       overlay_position_show, overlay_position_store),
1353 	__ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
1354 	       overlay_rop3_show, overlay_rop3_store),
1355 };
1356 
1357 static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix  = {
1358 	.id =		"SH Mobile LCDC",
1359 	.type =		FB_TYPE_PACKED_PIXELS,
1360 	.visual =	FB_VISUAL_TRUECOLOR,
1361 	.accel =	FB_ACCEL_NONE,
1362 	.xpanstep =	1,
1363 	.ypanstep =	1,
1364 	.ywrapstep =	0,
1365 	.capabilities =	FB_CAP_FOURCC,
1366 };
1367 
1368 static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
1369 				    struct fb_info *info)
1370 {
1371 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1372 	unsigned long base_addr_y;
1373 	unsigned long base_addr_c;
1374 	unsigned long y_offset;
1375 	unsigned long c_offset;
1376 
1377 	if (!ovl->format->yuv) {
1378 		y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
1379 			 * ovl->format->bpp / 8;
1380 		c_offset = 0;
1381 	} else {
1382 		unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
1383 		unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
1384 
1385 		y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
1386 		c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
1387 			 + var->xoffset * 2 / xsub;
1388 	}
1389 
1390 	/* If the Y offset hasn't changed, the C offset hasn't either. There's
1391 	 * nothing to do in that case.
1392 	 */
1393 	if (y_offset == ovl->pan_y_offset)
1394 		return 0;
1395 
1396 	/* Set the source address for the next refresh */
1397 	base_addr_y = ovl->dma_handle + y_offset;
1398 	base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
1399 		    + c_offset;
1400 
1401 	ovl->base_addr_y = base_addr_y;
1402 	ovl->base_addr_c = base_addr_c;
1403 	ovl->pan_y_offset = y_offset;
1404 
1405 	lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
1406 
1407 	lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
1408 	lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
1409 
1410 	lcdc_write(ovl->channel->lcdc, LDBCR,
1411 		   LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
1412 
1413 	return 0;
1414 }
1415 
1416 static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
1417 				      unsigned long arg)
1418 {
1419 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1420 
1421 	switch (cmd) {
1422 	case FBIO_WAITFORVSYNC:
1423 		return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
1424 
1425 	default:
1426 		return -ENOIOCTLCMD;
1427 	}
1428 }
1429 
1430 static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
1431 					  struct fb_info *info)
1432 {
1433 	return __sh_mobile_lcdc_check_var(var, info);
1434 }
1435 
1436 static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
1437 {
1438 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1439 
1440 	ovl->format =
1441 		sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1442 
1443 	ovl->xres = info->var.xres;
1444 	ovl->xres_virtual = info->var.xres_virtual;
1445 	ovl->yres = info->var.yres;
1446 	ovl->yres_virtual = info->var.yres_virtual;
1447 
1448 	if (ovl->format->yuv)
1449 		ovl->pitch = info->var.xres_virtual;
1450 	else
1451 		ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
1452 
1453 	sh_mobile_lcdc_overlay_setup(ovl);
1454 
1455 	info->fix.line_length = ovl->pitch;
1456 
1457 	if (sh_mobile_format_is_fourcc(&info->var)) {
1458 		info->fix.type = FB_TYPE_FOURCC;
1459 		info->fix.visual = FB_VISUAL_FOURCC;
1460 	} else {
1461 		info->fix.type = FB_TYPE_PACKED_PIXELS;
1462 		info->fix.visual = FB_VISUAL_TRUECOLOR;
1463 	}
1464 
1465 	return 0;
1466 }
1467 
1468 /* Overlay blanking. Disable the overlay when blanked. */
1469 static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
1470 {
1471 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1472 
1473 	ovl->enabled = !blank;
1474 	sh_mobile_lcdc_overlay_setup(ovl);
1475 
1476 	/* Prevent the backlight from receiving a blanking event by returning
1477 	 * a non-zero value.
1478 	 */
1479 	return 1;
1480 }
1481 
1482 static int
1483 sh_mobile_lcdc_overlay_mmap(struct fb_info *info, struct vm_area_struct *vma)
1484 {
1485 	struct sh_mobile_lcdc_overlay *ovl = info->par;
1486 
1487 	return dma_mmap_coherent(ovl->channel->lcdc->dev, vma, ovl->fb_mem,
1488 				 ovl->dma_handle, ovl->fb_size);
1489 }
1490 
1491 static const struct fb_ops sh_mobile_lcdc_overlay_ops = {
1492 	.owner          = THIS_MODULE,
1493 	.fb_read        = fb_sys_read,
1494 	.fb_write       = fb_sys_write,
1495 	.fb_fillrect	= sys_fillrect,
1496 	.fb_copyarea	= sys_copyarea,
1497 	.fb_imageblit	= sys_imageblit,
1498 	.fb_blank	= sh_mobile_lcdc_overlay_blank,
1499 	.fb_pan_display = sh_mobile_lcdc_overlay_pan,
1500 	.fb_ioctl       = sh_mobile_lcdc_overlay_ioctl,
1501 	.fb_check_var	= sh_mobile_lcdc_overlay_check_var,
1502 	.fb_set_par	= sh_mobile_lcdc_overlay_set_par,
1503 	.fb_mmap	= sh_mobile_lcdc_overlay_mmap,
1504 };
1505 
1506 static void
1507 sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
1508 {
1509 	struct fb_info *info = ovl->info;
1510 
1511 	if (info == NULL || info->dev == NULL)
1512 		return;
1513 
1514 	unregister_framebuffer(ovl->info);
1515 }
1516 
1517 static int
1518 sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
1519 {
1520 	struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
1521 	struct fb_info *info = ovl->info;
1522 	unsigned int i;
1523 	int ret;
1524 
1525 	if (info == NULL)
1526 		return 0;
1527 
1528 	ret = register_framebuffer(info);
1529 	if (ret < 0)
1530 		return ret;
1531 
1532 	dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
1533 		 dev_name(lcdc->dev), ovl->index, info->var.xres,
1534 		 info->var.yres, info->var.bits_per_pixel);
1535 
1536 	for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
1537 		ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
1538 		if (ret < 0)
1539 			return ret;
1540 	}
1541 
1542 	return 0;
1543 }
1544 
1545 static void
1546 sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
1547 {
1548 	struct fb_info *info = ovl->info;
1549 
1550 	if (info == NULL || info->device == NULL)
1551 		return;
1552 
1553 	framebuffer_release(info);
1554 }
1555 
1556 static int
1557 sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
1558 {
1559 	struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
1560 	struct fb_var_screeninfo *var;
1561 	struct fb_info *info;
1562 
1563 	/* Allocate and initialize the frame buffer device. */
1564 	info = framebuffer_alloc(0, priv->dev);
1565 	if (!info)
1566 		return -ENOMEM;
1567 
1568 	ovl->info = info;
1569 
1570 	info->flags = FBINFO_FLAG_DEFAULT;
1571 	info->fbops = &sh_mobile_lcdc_overlay_ops;
1572 	info->device = priv->dev;
1573 	info->screen_buffer = ovl->fb_mem;
1574 	info->par = ovl;
1575 
1576 	/* Initialize fixed screen information. Restrict pan to 2 lines steps
1577 	 * for NV12 and NV21.
1578 	 */
1579 	info->fix = sh_mobile_lcdc_overlay_fix;
1580 	snprintf(info->fix.id, sizeof(info->fix.id),
1581 		 "SH Mobile LCDC Overlay %u", ovl->index);
1582 	info->fix.smem_start = ovl->dma_handle;
1583 	info->fix.smem_len = ovl->fb_size;
1584 	info->fix.line_length = ovl->pitch;
1585 
1586 	if (ovl->format->yuv)
1587 		info->fix.visual = FB_VISUAL_FOURCC;
1588 	else
1589 		info->fix.visual = FB_VISUAL_TRUECOLOR;
1590 
1591 	switch (ovl->format->fourcc) {
1592 	case V4L2_PIX_FMT_NV12:
1593 	case V4L2_PIX_FMT_NV21:
1594 		info->fix.ypanstep = 2;
1595 		fallthrough;
1596 	case V4L2_PIX_FMT_NV16:
1597 	case V4L2_PIX_FMT_NV61:
1598 		info->fix.xpanstep = 2;
1599 	}
1600 
1601 	/* Initialize variable screen information. */
1602 	var = &info->var;
1603 	memset(var, 0, sizeof(*var));
1604 	var->xres = ovl->xres;
1605 	var->yres = ovl->yres;
1606 	var->xres_virtual = ovl->xres_virtual;
1607 	var->yres_virtual = ovl->yres_virtual;
1608 	var->activate = FB_ACTIVATE_NOW;
1609 
1610 	/* Use the legacy API by default for RGB formats, and the FOURCC API
1611 	 * for YUV formats.
1612 	 */
1613 	if (!ovl->format->yuv)
1614 		var->bits_per_pixel = ovl->format->bpp;
1615 	else
1616 		var->grayscale = ovl->format->fourcc;
1617 
1618 	return sh_mobile_lcdc_overlay_check_var(var, info);
1619 }
1620 
1621 /* -----------------------------------------------------------------------------
1622  * Frame buffer operations - main frame buffer
1623  */
1624 
1625 static int sh_mobile_lcdc_setcolreg(u_int regno,
1626 				    u_int red, u_int green, u_int blue,
1627 				    u_int transp, struct fb_info *info)
1628 {
1629 	u32 *palette = info->pseudo_palette;
1630 
1631 	if (regno >= PALETTE_NR)
1632 		return -EINVAL;
1633 
1634 	/* only FB_VISUAL_TRUECOLOR supported */
1635 
1636 	red >>= 16 - info->var.red.length;
1637 	green >>= 16 - info->var.green.length;
1638 	blue >>= 16 - info->var.blue.length;
1639 	transp >>= 16 - info->var.transp.length;
1640 
1641 	palette[regno] = (red << info->var.red.offset) |
1642 	  (green << info->var.green.offset) |
1643 	  (blue << info->var.blue.offset) |
1644 	  (transp << info->var.transp.offset);
1645 
1646 	return 0;
1647 }
1648 
1649 static const struct fb_fix_screeninfo sh_mobile_lcdc_fix  = {
1650 	.id =		"SH Mobile LCDC",
1651 	.type =		FB_TYPE_PACKED_PIXELS,
1652 	.visual =	FB_VISUAL_TRUECOLOR,
1653 	.accel =	FB_ACCEL_NONE,
1654 	.xpanstep =	1,
1655 	.ypanstep =	1,
1656 	.ywrapstep =	0,
1657 	.capabilities =	FB_CAP_FOURCC,
1658 };
1659 
1660 static void sh_mobile_lcdc_fillrect(struct fb_info *info,
1661 				    const struct fb_fillrect *rect)
1662 {
1663 	sys_fillrect(info, rect);
1664 	sh_mobile_lcdc_deferred_io_touch(info);
1665 }
1666 
1667 static void sh_mobile_lcdc_copyarea(struct fb_info *info,
1668 				    const struct fb_copyarea *area)
1669 {
1670 	sys_copyarea(info, area);
1671 	sh_mobile_lcdc_deferred_io_touch(info);
1672 }
1673 
1674 static void sh_mobile_lcdc_imageblit(struct fb_info *info,
1675 				     const struct fb_image *image)
1676 {
1677 	sys_imageblit(info, image);
1678 	sh_mobile_lcdc_deferred_io_touch(info);
1679 }
1680 
1681 static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
1682 			      struct fb_info *info)
1683 {
1684 	struct sh_mobile_lcdc_chan *ch = info->par;
1685 	struct sh_mobile_lcdc_priv *priv = ch->lcdc;
1686 	unsigned long ldrcntr;
1687 	unsigned long base_addr_y, base_addr_c;
1688 	unsigned long y_offset;
1689 	unsigned long c_offset;
1690 
1691 	if (!ch->format->yuv) {
1692 		y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
1693 			 * ch->format->bpp / 8;
1694 		c_offset = 0;
1695 	} else {
1696 		unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
1697 		unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
1698 
1699 		y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
1700 		c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
1701 			 + var->xoffset * 2 / xsub;
1702 	}
1703 
1704 	/* If the Y offset hasn't changed, the C offset hasn't either. There's
1705 	 * nothing to do in that case.
1706 	 */
1707 	if (y_offset == ch->pan_y_offset)
1708 		return 0;
1709 
1710 	/* Set the source address for the next refresh */
1711 	base_addr_y = ch->dma_handle + y_offset;
1712 	base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
1713 		    + c_offset;
1714 
1715 	ch->base_addr_y = base_addr_y;
1716 	ch->base_addr_c = base_addr_c;
1717 	ch->pan_y_offset = y_offset;
1718 
1719 	lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
1720 	if (ch->format->yuv)
1721 		lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
1722 
1723 	ldrcntr = lcdc_read(priv, _LDRCNTR);
1724 	if (lcdc_chan_is_sublcd(ch))
1725 		lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
1726 	else
1727 		lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
1728 
1729 
1730 	sh_mobile_lcdc_deferred_io_touch(info);
1731 
1732 	return 0;
1733 }
1734 
1735 static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
1736 				unsigned long arg)
1737 {
1738 	struct sh_mobile_lcdc_chan *ch = info->par;
1739 	int retval;
1740 
1741 	switch (cmd) {
1742 	case FBIO_WAITFORVSYNC:
1743 		retval = sh_mobile_lcdc_wait_for_vsync(ch);
1744 		break;
1745 
1746 	default:
1747 		retval = -ENOIOCTLCMD;
1748 		break;
1749 	}
1750 	return retval;
1751 }
1752 
1753 static void sh_mobile_fb_reconfig(struct fb_info *info)
1754 {
1755 	struct sh_mobile_lcdc_chan *ch = info->par;
1756 	struct fb_var_screeninfo var;
1757 	struct fb_videomode mode;
1758 
1759 	if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
1760 		/* More framebuffer users are active */
1761 		return;
1762 
1763 	fb_var_to_videomode(&mode, &info->var);
1764 
1765 	if (fb_mode_is_equal(&ch->display.mode, &mode))
1766 		return;
1767 
1768 	/* Display has been re-plugged, framebuffer is free now, reconfigure */
1769 	var = info->var;
1770 	fb_videomode_to_var(&var, &ch->display.mode);
1771 	var.width = ch->display.width;
1772 	var.height = ch->display.height;
1773 	var.activate = FB_ACTIVATE_NOW;
1774 
1775 	if (fb_set_var(info, &var) < 0)
1776 		/* Couldn't reconfigure, hopefully, can continue as before */
1777 		return;
1778 
1779 	fbcon_update_vcs(info, true);
1780 }
1781 
1782 /*
1783  * Locking: both .fb_release() and .fb_open() are called with info->lock held if
1784  * user == 1, or with console sem held, if user == 0.
1785  */
1786 static int sh_mobile_lcdc_release(struct fb_info *info, int user)
1787 {
1788 	struct sh_mobile_lcdc_chan *ch = info->par;
1789 
1790 	mutex_lock(&ch->open_lock);
1791 	dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1792 
1793 	ch->use_count--;
1794 
1795 	/* Nothing to reconfigure, when called from fbcon */
1796 	if (user) {
1797 		console_lock();
1798 		sh_mobile_fb_reconfig(info);
1799 		console_unlock();
1800 	}
1801 
1802 	mutex_unlock(&ch->open_lock);
1803 
1804 	return 0;
1805 }
1806 
1807 static int sh_mobile_lcdc_open(struct fb_info *info, int user)
1808 {
1809 	struct sh_mobile_lcdc_chan *ch = info->par;
1810 
1811 	mutex_lock(&ch->open_lock);
1812 	ch->use_count++;
1813 
1814 	dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
1815 	mutex_unlock(&ch->open_lock);
1816 
1817 	return 0;
1818 }
1819 
1820 static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
1821 				    struct fb_info *info)
1822 {
1823 	struct sh_mobile_lcdc_chan *ch = info->par;
1824 	struct sh_mobile_lcdc_priv *p = ch->lcdc;
1825 	unsigned int best_dist = (unsigned int)-1;
1826 	unsigned int best_xres = 0;
1827 	unsigned int best_yres = 0;
1828 	unsigned int i;
1829 	int ret;
1830 
1831 	/* If board code provides us with a list of available modes, make sure
1832 	 * we use one of them. Find the mode closest to the requested one. The
1833 	 * distance between two modes is defined as the size of the
1834 	 * non-overlapping parts of the two rectangles.
1835 	 */
1836 	for (i = 0; i < ch->cfg->num_modes; ++i) {
1837 		const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
1838 		unsigned int dist;
1839 
1840 		/* We can only round up. */
1841 		if (var->xres > mode->xres || var->yres > mode->yres)
1842 			continue;
1843 
1844 		dist = var->xres * var->yres + mode->xres * mode->yres
1845 		     - 2 * min(var->xres, mode->xres)
1846 			 * min(var->yres, mode->yres);
1847 
1848 		if (dist < best_dist) {
1849 			best_xres = mode->xres;
1850 			best_yres = mode->yres;
1851 			best_dist = dist;
1852 		}
1853 	}
1854 
1855 	/* If no available mode can be used, return an error. */
1856 	if (ch->cfg->num_modes != 0) {
1857 		if (best_dist == (unsigned int)-1)
1858 			return -EINVAL;
1859 
1860 		var->xres = best_xres;
1861 		var->yres = best_yres;
1862 	}
1863 
1864 	ret = __sh_mobile_lcdc_check_var(var, info);
1865 	if (ret < 0)
1866 		return ret;
1867 
1868 	/* only accept the forced_fourcc for dual channel configurations */
1869 	if (p->forced_fourcc &&
1870 	    p->forced_fourcc != sh_mobile_format_fourcc(var))
1871 		return -EINVAL;
1872 
1873 	return 0;
1874 }
1875 
1876 static int sh_mobile_lcdc_set_par(struct fb_info *info)
1877 {
1878 	struct sh_mobile_lcdc_chan *ch = info->par;
1879 	int ret;
1880 
1881 	sh_mobile_lcdc_stop(ch->lcdc);
1882 
1883 	ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
1884 	ch->colorspace = info->var.colorspace;
1885 
1886 	ch->xres = info->var.xres;
1887 	ch->xres_virtual = info->var.xres_virtual;
1888 	ch->yres = info->var.yres;
1889 	ch->yres_virtual = info->var.yres_virtual;
1890 
1891 	if (ch->format->yuv)
1892 		ch->pitch = info->var.xres_virtual;
1893 	else
1894 		ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
1895 
1896 	ret = sh_mobile_lcdc_start(ch->lcdc);
1897 	if (ret < 0)
1898 		dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
1899 
1900 	info->fix.line_length = ch->pitch;
1901 
1902 	if (sh_mobile_format_is_fourcc(&info->var)) {
1903 		info->fix.type = FB_TYPE_FOURCC;
1904 		info->fix.visual = FB_VISUAL_FOURCC;
1905 	} else {
1906 		info->fix.type = FB_TYPE_PACKED_PIXELS;
1907 		info->fix.visual = FB_VISUAL_TRUECOLOR;
1908 	}
1909 
1910 	return ret;
1911 }
1912 
1913 /*
1914  * Screen blanking. Behavior is as follows:
1915  * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
1916  * FB_BLANK_NORMAL: screen blanked, clocks enabled
1917  * FB_BLANK_VSYNC,
1918  * FB_BLANK_HSYNC,
1919  * FB_BLANK_POWEROFF: screen blanked, clocks disabled
1920  */
1921 static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
1922 {
1923 	struct sh_mobile_lcdc_chan *ch = info->par;
1924 	struct sh_mobile_lcdc_priv *p = ch->lcdc;
1925 
1926 	/* blank the screen? */
1927 	if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
1928 		struct fb_fillrect rect = {
1929 			.width = ch->xres,
1930 			.height = ch->yres,
1931 		};
1932 		sh_mobile_lcdc_fillrect(info, &rect);
1933 	}
1934 	/* turn clocks on? */
1935 	if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
1936 		sh_mobile_lcdc_clk_on(p);
1937 	}
1938 	/* turn clocks off? */
1939 	if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
1940 		/* make sure the screen is updated with the black fill before
1941 		 * switching the clocks off. one vsync is not enough since
1942 		 * blanking may occur in the middle of a refresh. deferred io
1943 		 * mode will reenable the clocks and update the screen in time,
1944 		 * so it does not need this. */
1945 		if (!info->fbdefio) {
1946 			sh_mobile_lcdc_wait_for_vsync(ch);
1947 			sh_mobile_lcdc_wait_for_vsync(ch);
1948 		}
1949 		sh_mobile_lcdc_clk_off(p);
1950 	}
1951 
1952 	ch->blank_status = blank;
1953 	return 0;
1954 }
1955 
1956 static int
1957 sh_mobile_lcdc_mmap(struct fb_info *info, struct vm_area_struct *vma)
1958 {
1959 	struct sh_mobile_lcdc_chan *ch = info->par;
1960 
1961 	return dma_mmap_coherent(ch->lcdc->dev, vma, ch->fb_mem,
1962 				 ch->dma_handle, ch->fb_size);
1963 }
1964 
1965 static const struct fb_ops sh_mobile_lcdc_ops = {
1966 	.owner          = THIS_MODULE,
1967 	.fb_setcolreg	= sh_mobile_lcdc_setcolreg,
1968 	.fb_read        = fb_sys_read,
1969 	.fb_write       = fb_sys_write,
1970 	.fb_fillrect	= sh_mobile_lcdc_fillrect,
1971 	.fb_copyarea	= sh_mobile_lcdc_copyarea,
1972 	.fb_imageblit	= sh_mobile_lcdc_imageblit,
1973 	.fb_blank	= sh_mobile_lcdc_blank,
1974 	.fb_pan_display = sh_mobile_lcdc_pan,
1975 	.fb_ioctl       = sh_mobile_lcdc_ioctl,
1976 	.fb_open	= sh_mobile_lcdc_open,
1977 	.fb_release	= sh_mobile_lcdc_release,
1978 	.fb_check_var	= sh_mobile_lcdc_check_var,
1979 	.fb_set_par	= sh_mobile_lcdc_set_par,
1980 	.fb_mmap	= sh_mobile_lcdc_mmap,
1981 };
1982 
1983 static void
1984 sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
1985 {
1986 	if (ch->info && ch->info->dev)
1987 		unregister_framebuffer(ch->info);
1988 }
1989 
1990 static int
1991 sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
1992 {
1993 	struct fb_info *info = ch->info;
1994 	int ret;
1995 
1996 	if (info->fbdefio) {
1997 		ch->sglist = vmalloc(sizeof(struct scatterlist) *
1998 				     ch->fb_size >> PAGE_SHIFT);
1999 		if (!ch->sglist)
2000 			return -ENOMEM;
2001 	}
2002 
2003 	info->bl_dev = ch->bl;
2004 
2005 	ret = register_framebuffer(info);
2006 	if (ret < 0)
2007 		return ret;
2008 
2009 	dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
2010 		 dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
2011 		 "mainlcd" : "sublcd", info->var.xres, info->var.yres,
2012 		 info->var.bits_per_pixel);
2013 
2014 	/* deferred io mode: disable clock to save power */
2015 	if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
2016 		sh_mobile_lcdc_clk_off(ch->lcdc);
2017 
2018 	return ret;
2019 }
2020 
2021 static void
2022 sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
2023 {
2024 	struct fb_info *info = ch->info;
2025 
2026 	if (!info || !info->device)
2027 		return;
2028 
2029 	vfree(ch->sglist);
2030 
2031 	fb_dealloc_cmap(&info->cmap);
2032 	framebuffer_release(info);
2033 }
2034 
2035 static int
2036 sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
2037 			       const struct fb_videomode *modes,
2038 			       unsigned int num_modes)
2039 {
2040 	struct sh_mobile_lcdc_priv *priv = ch->lcdc;
2041 	struct fb_var_screeninfo *var;
2042 	struct fb_info *info;
2043 	int ret;
2044 
2045 	/* Allocate and initialize the frame buffer device. Create the modes
2046 	 * list and allocate the color map.
2047 	 */
2048 	info = framebuffer_alloc(0, priv->dev);
2049 	if (!info)
2050 		return -ENOMEM;
2051 
2052 	ch->info = info;
2053 
2054 	info->flags = FBINFO_FLAG_DEFAULT;
2055 	info->fbops = &sh_mobile_lcdc_ops;
2056 	info->device = priv->dev;
2057 	info->screen_buffer = ch->fb_mem;
2058 	info->pseudo_palette = &ch->pseudo_palette;
2059 	info->par = ch;
2060 
2061 	fb_videomode_to_modelist(modes, num_modes, &info->modelist);
2062 
2063 	ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
2064 	if (ret < 0) {
2065 		dev_err(priv->dev, "unable to allocate cmap\n");
2066 		return ret;
2067 	}
2068 
2069 	/* Initialize fixed screen information. Restrict pan to 2 lines steps
2070 	 * for NV12 and NV21.
2071 	 */
2072 	info->fix = sh_mobile_lcdc_fix;
2073 	info->fix.smem_start = ch->dma_handle;
2074 	info->fix.smem_len = ch->fb_size;
2075 	info->fix.line_length = ch->pitch;
2076 
2077 	if (ch->format->yuv)
2078 		info->fix.visual = FB_VISUAL_FOURCC;
2079 	else
2080 		info->fix.visual = FB_VISUAL_TRUECOLOR;
2081 
2082 	switch (ch->format->fourcc) {
2083 	case V4L2_PIX_FMT_NV12:
2084 	case V4L2_PIX_FMT_NV21:
2085 		info->fix.ypanstep = 2;
2086 		fallthrough;
2087 	case V4L2_PIX_FMT_NV16:
2088 	case V4L2_PIX_FMT_NV61:
2089 		info->fix.xpanstep = 2;
2090 	}
2091 
2092 	/* Initialize variable screen information using the first mode as
2093 	 * default.
2094 	 */
2095 	var = &info->var;
2096 	fb_videomode_to_var(var, modes);
2097 	var->width = ch->display.width;
2098 	var->height = ch->display.height;
2099 	var->xres_virtual = ch->xres_virtual;
2100 	var->yres_virtual = ch->yres_virtual;
2101 	var->activate = FB_ACTIVATE_NOW;
2102 
2103 	/* Use the legacy API by default for RGB formats, and the FOURCC API
2104 	 * for YUV formats.
2105 	 */
2106 	if (!ch->format->yuv)
2107 		var->bits_per_pixel = ch->format->bpp;
2108 	else
2109 		var->grayscale = ch->format->fourcc;
2110 
2111 	ret = sh_mobile_lcdc_check_var(var, info);
2112 	if (ret)
2113 		return ret;
2114 
2115 	return 0;
2116 }
2117 
2118 /* -----------------------------------------------------------------------------
2119  * Backlight
2120  */
2121 
2122 static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
2123 {
2124 	struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2125 	int brightness = bdev->props.brightness;
2126 
2127 	if (bdev->props.power != FB_BLANK_UNBLANK ||
2128 	    bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
2129 		brightness = 0;
2130 
2131 	ch->bl_brightness = brightness;
2132 	return ch->cfg->bl_info.set_brightness(brightness);
2133 }
2134 
2135 static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
2136 {
2137 	struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
2138 
2139 	return ch->bl_brightness;
2140 }
2141 
2142 static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
2143 				   struct fb_info *info)
2144 {
2145 	return (info->bl_dev == bdev);
2146 }
2147 
2148 static const struct backlight_ops sh_mobile_lcdc_bl_ops = {
2149 	.options	= BL_CORE_SUSPENDRESUME,
2150 	.update_status	= sh_mobile_lcdc_update_bl,
2151 	.get_brightness	= sh_mobile_lcdc_get_brightness,
2152 	.check_fb	= sh_mobile_lcdc_check_fb,
2153 };
2154 
2155 static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
2156 					       struct sh_mobile_lcdc_chan *ch)
2157 {
2158 	struct backlight_device *bl;
2159 
2160 	bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
2161 				       &sh_mobile_lcdc_bl_ops, NULL);
2162 	if (IS_ERR(bl)) {
2163 		dev_err(parent, "unable to register backlight device: %ld\n",
2164 			PTR_ERR(bl));
2165 		return NULL;
2166 	}
2167 
2168 	bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
2169 	bl->props.brightness = bl->props.max_brightness;
2170 	backlight_update_status(bl);
2171 
2172 	return bl;
2173 }
2174 
2175 static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
2176 {
2177 	backlight_device_unregister(bdev);
2178 }
2179 
2180 /* -----------------------------------------------------------------------------
2181  * Power management
2182  */
2183 
2184 static int sh_mobile_lcdc_suspend(struct device *dev)
2185 {
2186 	struct platform_device *pdev = to_platform_device(dev);
2187 
2188 	sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
2189 	return 0;
2190 }
2191 
2192 static int sh_mobile_lcdc_resume(struct device *dev)
2193 {
2194 	struct platform_device *pdev = to_platform_device(dev);
2195 
2196 	return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
2197 }
2198 
2199 static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
2200 {
2201 	struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
2202 
2203 	/* turn off LCDC hardware */
2204 	lcdc_write(priv, _LDCNT1R, 0);
2205 
2206 	return 0;
2207 }
2208 
2209 static int sh_mobile_lcdc_runtime_resume(struct device *dev)
2210 {
2211 	struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
2212 
2213 	__sh_mobile_lcdc_start(priv);
2214 
2215 	return 0;
2216 }
2217 
2218 static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
2219 	.suspend = sh_mobile_lcdc_suspend,
2220 	.resume = sh_mobile_lcdc_resume,
2221 	.runtime_suspend = sh_mobile_lcdc_runtime_suspend,
2222 	.runtime_resume = sh_mobile_lcdc_runtime_resume,
2223 };
2224 
2225 /* -----------------------------------------------------------------------------
2226  * Framebuffer notifier
2227  */
2228 
2229 /* -----------------------------------------------------------------------------
2230  * Probe/remove and driver init/exit
2231  */
2232 
2233 static const struct fb_videomode default_720p = {
2234 	.name = "HDMI 720p",
2235 	.xres = 1280,
2236 	.yres = 720,
2237 
2238 	.left_margin = 220,
2239 	.right_margin = 110,
2240 	.hsync_len = 40,
2241 
2242 	.upper_margin = 20,
2243 	.lower_margin = 5,
2244 	.vsync_len = 5,
2245 
2246 	.pixclock = 13468,
2247 	.refresh = 60,
2248 	.sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
2249 };
2250 
2251 static int sh_mobile_lcdc_remove(struct platform_device *pdev)
2252 {
2253 	struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
2254 	unsigned int i;
2255 
2256 	for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
2257 		sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
2258 	for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
2259 		sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
2260 
2261 	sh_mobile_lcdc_stop(priv);
2262 
2263 	for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
2264 		struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2265 
2266 		sh_mobile_lcdc_overlay_fb_cleanup(ovl);
2267 
2268 		if (ovl->fb_mem)
2269 			dma_free_coherent(&pdev->dev, ovl->fb_size,
2270 					  ovl->fb_mem, ovl->dma_handle);
2271 	}
2272 
2273 	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2274 		struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2275 
2276 		if (ch->tx_dev) {
2277 			ch->tx_dev->lcdc = NULL;
2278 			module_put(ch->cfg->tx_dev->dev.driver->owner);
2279 		}
2280 
2281 		sh_mobile_lcdc_channel_fb_cleanup(ch);
2282 
2283 		if (ch->fb_mem)
2284 			dma_free_coherent(&pdev->dev, ch->fb_size,
2285 					  ch->fb_mem, ch->dma_handle);
2286 	}
2287 
2288 	for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
2289 		struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2290 
2291 		if (ch->bl)
2292 			sh_mobile_lcdc_bl_remove(ch->bl);
2293 		mutex_destroy(&ch->open_lock);
2294 	}
2295 
2296 	if (priv->dot_clk) {
2297 		pm_runtime_disable(&pdev->dev);
2298 		clk_put(priv->dot_clk);
2299 	}
2300 
2301 	if (priv->base)
2302 		iounmap(priv->base);
2303 
2304 	if (priv->irq)
2305 		free_irq(priv->irq, priv);
2306 	kfree(priv);
2307 	return 0;
2308 }
2309 
2310 static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
2311 {
2312 	int interface_type = ch->cfg->interface_type;
2313 
2314 	switch (interface_type) {
2315 	case RGB8:
2316 	case RGB9:
2317 	case RGB12A:
2318 	case RGB12B:
2319 	case RGB16:
2320 	case RGB18:
2321 	case RGB24:
2322 	case SYS8A:
2323 	case SYS8B:
2324 	case SYS8C:
2325 	case SYS8D:
2326 	case SYS9:
2327 	case SYS12:
2328 	case SYS16A:
2329 	case SYS16B:
2330 	case SYS16C:
2331 	case SYS18:
2332 	case SYS24:
2333 		break;
2334 	default:
2335 		return -EINVAL;
2336 	}
2337 
2338 	/* SUBLCD only supports SYS interface */
2339 	if (lcdc_chan_is_sublcd(ch)) {
2340 		if (!(interface_type & LDMT1R_IFM))
2341 			return -EINVAL;
2342 
2343 		interface_type &= ~LDMT1R_IFM;
2344 	}
2345 
2346 	ch->ldmt1r_value = interface_type;
2347 	return 0;
2348 }
2349 
2350 static int
2351 sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay *ovl)
2352 {
2353 	const struct sh_mobile_lcdc_format_info *format;
2354 	struct device *dev = ovl->channel->lcdc->dev;
2355 	int ret;
2356 
2357 	if (ovl->cfg->fourcc == 0)
2358 		return 0;
2359 
2360 	/* Validate the format. */
2361 	format = sh_mobile_format_info(ovl->cfg->fourcc);
2362 	if (format == NULL) {
2363 		dev_err(dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
2364 		return -EINVAL;
2365 	}
2366 
2367 	ovl->enabled = false;
2368 	ovl->mode = LCDC_OVERLAY_BLEND;
2369 	ovl->alpha = 255;
2370 	ovl->rop3 = 0;
2371 	ovl->pos_x = 0;
2372 	ovl->pos_y = 0;
2373 
2374 	/* The default Y virtual resolution is twice the panel size to allow for
2375 	 * double-buffering.
2376 	 */
2377 	ovl->format = format;
2378 	ovl->xres = ovl->cfg->max_xres;
2379 	ovl->xres_virtual = ovl->xres;
2380 	ovl->yres = ovl->cfg->max_yres;
2381 	ovl->yres_virtual = ovl->yres * 2;
2382 
2383 	if (!format->yuv)
2384 		ovl->pitch = ovl->xres_virtual * format->bpp / 8;
2385 	else
2386 		ovl->pitch = ovl->xres_virtual;
2387 
2388 	/* Allocate frame buffer memory. */
2389 	ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
2390 		       * format->bpp / 8 * 2;
2391 	ovl->fb_mem = dma_alloc_coherent(dev, ovl->fb_size, &ovl->dma_handle,
2392 					 GFP_KERNEL);
2393 	if (!ovl->fb_mem) {
2394 		dev_err(dev, "unable to allocate buffer\n");
2395 		return -ENOMEM;
2396 	}
2397 
2398 	ret = sh_mobile_lcdc_overlay_fb_init(ovl);
2399 	if (ret < 0)
2400 		return ret;
2401 
2402 	return 0;
2403 }
2404 
2405 static int
2406 sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch)
2407 {
2408 	const struct sh_mobile_lcdc_format_info *format;
2409 	const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
2410 	struct device *dev = ch->lcdc->dev;
2411 	const struct fb_videomode *max_mode;
2412 	const struct fb_videomode *mode;
2413 	unsigned int num_modes;
2414 	unsigned int max_size;
2415 	unsigned int i;
2416 
2417 	/* Validate the format. */
2418 	format = sh_mobile_format_info(cfg->fourcc);
2419 	if (format == NULL) {
2420 		dev_err(dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
2421 		return -EINVAL;
2422 	}
2423 
2424 	/* Iterate through the modes to validate them and find the highest
2425 	 * resolution.
2426 	 */
2427 	max_mode = NULL;
2428 	max_size = 0;
2429 
2430 	for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
2431 		unsigned int size = mode->yres * mode->xres;
2432 
2433 		/* NV12/NV21 buffers must have even number of lines */
2434 		if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
2435 		     cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
2436 			dev_err(dev, "yres must be multiple of 2 for "
2437 				"YCbCr420 mode.\n");
2438 			return -EINVAL;
2439 		}
2440 
2441 		if (size > max_size) {
2442 			max_mode = mode;
2443 			max_size = size;
2444 		}
2445 	}
2446 
2447 	if (!max_size)
2448 		max_size = MAX_XRES * MAX_YRES;
2449 	else
2450 		dev_dbg(dev, "Found largest videomode %ux%u\n",
2451 			max_mode->xres, max_mode->yres);
2452 
2453 	if (cfg->lcd_modes == NULL) {
2454 		mode = &default_720p;
2455 		num_modes = 1;
2456 	} else {
2457 		mode = cfg->lcd_modes;
2458 		num_modes = cfg->num_modes;
2459 	}
2460 
2461 	/* Use the first mode as default. The default Y virtual resolution is
2462 	 * twice the panel size to allow for double-buffering.
2463 	 */
2464 	ch->format = format;
2465 	ch->xres = mode->xres;
2466 	ch->xres_virtual = mode->xres;
2467 	ch->yres = mode->yres;
2468 	ch->yres_virtual = mode->yres * 2;
2469 
2470 	if (!format->yuv) {
2471 		ch->colorspace = V4L2_COLORSPACE_SRGB;
2472 		ch->pitch = ch->xres_virtual * format->bpp / 8;
2473 	} else {
2474 		ch->colorspace = V4L2_COLORSPACE_REC709;
2475 		ch->pitch = ch->xres_virtual;
2476 	}
2477 
2478 	ch->display.width = cfg->panel_cfg.width;
2479 	ch->display.height = cfg->panel_cfg.height;
2480 	ch->display.mode = *mode;
2481 
2482 	/* Allocate frame buffer memory. */
2483 	ch->fb_size = max_size * format->bpp / 8 * 2;
2484 	ch->fb_mem = dma_alloc_coherent(dev, ch->fb_size, &ch->dma_handle,
2485 					GFP_KERNEL);
2486 	if (ch->fb_mem == NULL) {
2487 		dev_err(dev, "unable to allocate buffer\n");
2488 		return -ENOMEM;
2489 	}
2490 
2491 	/* Initialize the transmitter device if present. */
2492 	if (cfg->tx_dev) {
2493 		if (!cfg->tx_dev->dev.driver ||
2494 		    !try_module_get(cfg->tx_dev->dev.driver->owner)) {
2495 			dev_warn(dev, "unable to get transmitter device\n");
2496 			return -EINVAL;
2497 		}
2498 		ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
2499 		ch->tx_dev->lcdc = ch;
2500 		ch->tx_dev->def_mode = *mode;
2501 	}
2502 
2503 	return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
2504 }
2505 
2506 static int sh_mobile_lcdc_probe(struct platform_device *pdev)
2507 {
2508 	struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
2509 	struct sh_mobile_lcdc_priv *priv;
2510 	struct resource *res;
2511 	int num_channels;
2512 	int error;
2513 	int irq, i;
2514 
2515 	if (!pdata) {
2516 		dev_err(&pdev->dev, "no platform data defined\n");
2517 		return -EINVAL;
2518 	}
2519 
2520 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2521 	irq = platform_get_irq(pdev, 0);
2522 	if (!res || irq < 0) {
2523 		dev_err(&pdev->dev, "cannot get platform resources\n");
2524 		return -ENOENT;
2525 	}
2526 
2527 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2528 	if (!priv)
2529 		return -ENOMEM;
2530 
2531 	priv->dev = &pdev->dev;
2532 
2533 	for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
2534 		mutex_init(&priv->ch[i].open_lock);
2535 	platform_set_drvdata(pdev, priv);
2536 
2537 	error = request_irq(irq, sh_mobile_lcdc_irq, 0,
2538 			    dev_name(&pdev->dev), priv);
2539 	if (error) {
2540 		dev_err(&pdev->dev, "unable to request irq\n");
2541 		goto err1;
2542 	}
2543 
2544 	priv->irq = irq;
2545 	atomic_set(&priv->hw_usecnt, -1);
2546 
2547 	for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
2548 		struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
2549 
2550 		ch->lcdc = priv;
2551 		ch->cfg = &pdata->ch[i];
2552 
2553 		error = sh_mobile_lcdc_check_interface(ch);
2554 		if (error) {
2555 			dev_err(&pdev->dev, "unsupported interface type\n");
2556 			goto err1;
2557 		}
2558 		init_waitqueue_head(&ch->frame_end_wait);
2559 		init_completion(&ch->vsync_completion);
2560 
2561 		/* probe the backlight is there is one defined */
2562 		if (ch->cfg->bl_info.max_brightness)
2563 			ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
2564 
2565 		switch (pdata->ch[i].chan) {
2566 		case LCDC_CHAN_MAINLCD:
2567 			ch->enabled = LDCNT2R_ME;
2568 			ch->reg_offs = lcdc_offs_mainlcd;
2569 			num_channels++;
2570 			break;
2571 		case LCDC_CHAN_SUBLCD:
2572 			ch->enabled = LDCNT2R_SE;
2573 			ch->reg_offs = lcdc_offs_sublcd;
2574 			num_channels++;
2575 			break;
2576 		}
2577 	}
2578 
2579 	if (!num_channels) {
2580 		dev_err(&pdev->dev, "no channels defined\n");
2581 		error = -EINVAL;
2582 		goto err1;
2583 	}
2584 
2585 	/* for dual channel LCDC (MAIN + SUB) force shared format setting */
2586 	if (num_channels == 2)
2587 		priv->forced_fourcc = pdata->ch[0].fourcc;
2588 
2589 	priv->base = ioremap(res->start, resource_size(res));
2590 	if (!priv->base) {
2591 		error = -ENOMEM;
2592 		goto err1;
2593 	}
2594 
2595 	error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
2596 	if (error) {
2597 		dev_err(&pdev->dev, "unable to setup clocks\n");
2598 		goto err1;
2599 	}
2600 
2601 	/* Enable runtime PM. */
2602 	pm_runtime_enable(&pdev->dev);
2603 
2604 	for (i = 0; i < num_channels; i++) {
2605 		struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
2606 
2607 		error = sh_mobile_lcdc_channel_init(ch);
2608 		if (error)
2609 			goto err1;
2610 	}
2611 
2612 	for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2613 		struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2614 
2615 		ovl->cfg = &pdata->overlays[i];
2616 		ovl->channel = &priv->ch[0];
2617 
2618 		error = sh_mobile_lcdc_overlay_init(ovl);
2619 		if (error)
2620 			goto err1;
2621 	}
2622 
2623 	error = sh_mobile_lcdc_start(priv);
2624 	if (error) {
2625 		dev_err(&pdev->dev, "unable to start hardware\n");
2626 		goto err1;
2627 	}
2628 
2629 	for (i = 0; i < num_channels; i++) {
2630 		struct sh_mobile_lcdc_chan *ch = priv->ch + i;
2631 
2632 		error = sh_mobile_lcdc_channel_fb_register(ch);
2633 		if (error)
2634 			goto err1;
2635 	}
2636 
2637 	for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
2638 		struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
2639 
2640 		error = sh_mobile_lcdc_overlay_fb_register(ovl);
2641 		if (error)
2642 			goto err1;
2643 	}
2644 
2645 	return 0;
2646 err1:
2647 	sh_mobile_lcdc_remove(pdev);
2648 
2649 	return error;
2650 }
2651 
2652 static struct platform_driver sh_mobile_lcdc_driver = {
2653 	.driver		= {
2654 		.name		= "sh_mobile_lcdc_fb",
2655 		.pm		= &sh_mobile_lcdc_dev_pm_ops,
2656 	},
2657 	.probe		= sh_mobile_lcdc_probe,
2658 	.remove		= sh_mobile_lcdc_remove,
2659 };
2660 
2661 module_platform_driver(sh_mobile_lcdc_driver);
2662 
2663 MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
2664 MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
2665 MODULE_LICENSE("GPL v2");
2666