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