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