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