1 /*
2  *  Driver for AT91/AT32 LCD Controller
3  *
4  *  Copyright (C) 2007 Atmel Corporation
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 for
8  * more details.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/platform_device.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/interrupt.h>
15 #include <linux/clk.h>
16 #include <linux/fb.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/backlight.h>
20 #include <linux/gfp.h>
21 #include <linux/module.h>
22 #include <linux/platform_data/atmel.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/of_gpio.h>
26 #include <video/of_display_timing.h>
27 #include <linux/regulator/consumer.h>
28 #include <video/videomode.h>
29 
30 #include <mach/cpu.h>
31 #include <asm/gpio.h>
32 
33 #include <video/atmel_lcdc.h>
34 
35 struct atmel_lcdfb_config {
36 	bool have_alt_pixclock;
37 	bool have_hozval;
38 	bool have_intensity_bit;
39 };
40 
41  /* LCD Controller info data structure, stored in device platform_data */
42 struct atmel_lcdfb_info {
43 	spinlock_t		lock;
44 	struct fb_info		*info;
45 	void __iomem		*mmio;
46 	int			irq_base;
47 	struct work_struct	task;
48 
49 	unsigned int		smem_len;
50 	struct platform_device	*pdev;
51 	struct clk		*bus_clk;
52 	struct clk		*lcdc_clk;
53 
54 	struct backlight_device	*backlight;
55 	u8			bl_power;
56 	u8			saved_lcdcon;
57 
58 	u32			pseudo_palette[16];
59 	bool			have_intensity_bit;
60 
61 	struct atmel_lcdfb_pdata pdata;
62 
63 	struct atmel_lcdfb_config *config;
64 	struct regulator	*reg_lcd;
65 };
66 
67 struct atmel_lcdfb_power_ctrl_gpio {
68 	int gpio;
69 	int active_low;
70 
71 	struct list_head list;
72 };
73 
74 #define lcdc_readl(sinfo, reg)		__raw_readl((sinfo)->mmio+(reg))
75 #define lcdc_writel(sinfo, reg, val)	__raw_writel((val), (sinfo)->mmio+(reg))
76 
77 /* configurable parameters */
78 #define ATMEL_LCDC_CVAL_DEFAULT		0xc8
79 #define ATMEL_LCDC_DMA_BURST_LEN	8	/* words */
80 #define ATMEL_LCDC_FIFO_SIZE		512	/* words */
81 
82 static struct atmel_lcdfb_config at91sam9261_config = {
83 	.have_hozval		= true,
84 	.have_intensity_bit	= true,
85 };
86 
87 static struct atmel_lcdfb_config at91sam9263_config = {
88 	.have_intensity_bit	= true,
89 };
90 
91 static struct atmel_lcdfb_config at91sam9g10_config = {
92 	.have_hozval		= true,
93 };
94 
95 static struct atmel_lcdfb_config at91sam9g45_config = {
96 	.have_alt_pixclock	= true,
97 };
98 
99 static struct atmel_lcdfb_config at91sam9g45es_config = {
100 };
101 
102 static struct atmel_lcdfb_config at91sam9rl_config = {
103 	.have_intensity_bit	= true,
104 };
105 
106 static struct atmel_lcdfb_config at32ap_config = {
107 	.have_hozval		= true,
108 };
109 
110 static const struct platform_device_id atmel_lcdfb_devtypes[] = {
111 	{
112 		.name = "at91sam9261-lcdfb",
113 		.driver_data = (unsigned long)&at91sam9261_config,
114 	}, {
115 		.name = "at91sam9263-lcdfb",
116 		.driver_data = (unsigned long)&at91sam9263_config,
117 	}, {
118 		.name = "at91sam9g10-lcdfb",
119 		.driver_data = (unsigned long)&at91sam9g10_config,
120 	}, {
121 		.name = "at91sam9g45-lcdfb",
122 		.driver_data = (unsigned long)&at91sam9g45_config,
123 	}, {
124 		.name = "at91sam9g45es-lcdfb",
125 		.driver_data = (unsigned long)&at91sam9g45es_config,
126 	}, {
127 		.name = "at91sam9rl-lcdfb",
128 		.driver_data = (unsigned long)&at91sam9rl_config,
129 	}, {
130 		.name = "at32ap-lcdfb",
131 		.driver_data = (unsigned long)&at32ap_config,
132 	}, {
133 		/* terminator */
134 	}
135 };
136 MODULE_DEVICE_TABLE(platform, atmel_lcdfb_devtypes);
137 
138 static struct atmel_lcdfb_config *
139 atmel_lcdfb_get_config(struct platform_device *pdev)
140 {
141 	unsigned long data;
142 
143 	data = platform_get_device_id(pdev)->driver_data;
144 
145 	return (struct atmel_lcdfb_config *)data;
146 }
147 
148 #if defined(CONFIG_ARCH_AT91)
149 #define	ATMEL_LCDFB_FBINFO_DEFAULT	(FBINFO_DEFAULT \
150 					 | FBINFO_PARTIAL_PAN_OK \
151 					 | FBINFO_HWACCEL_YPAN)
152 
153 static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
154 					struct fb_var_screeninfo *var,
155 					struct fb_info *info)
156 {
157 
158 }
159 #elif defined(CONFIG_AVR32)
160 #define	ATMEL_LCDFB_FBINFO_DEFAULT	(FBINFO_DEFAULT \
161 					| FBINFO_PARTIAL_PAN_OK \
162 					| FBINFO_HWACCEL_XPAN \
163 					| FBINFO_HWACCEL_YPAN)
164 
165 static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
166 				     struct fb_var_screeninfo *var,
167 				     struct fb_info *info)
168 {
169 	u32 dma2dcfg;
170 	u32 pixeloff;
171 
172 	pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f;
173 
174 	dma2dcfg = (info->var.xres_virtual - info->var.xres)
175 		 * info->var.bits_per_pixel / 8;
176 	dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
177 	lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
178 
179 	/* Update configuration */
180 	lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
181 		    lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
182 		    | ATMEL_LCDC_DMAUPDT);
183 }
184 #endif
185 
186 static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
187 		| ATMEL_LCDC_POL_POSITIVE
188 		| ATMEL_LCDC_ENA_PWMENABLE;
189 
190 #ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
191 
192 /* some bl->props field just changed */
193 static int atmel_bl_update_status(struct backlight_device *bl)
194 {
195 	struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
196 	int			power = sinfo->bl_power;
197 	int			brightness = bl->props.brightness;
198 
199 	/* REVISIT there may be a meaningful difference between
200 	 * fb_blank and power ... there seem to be some cases
201 	 * this doesn't handle correctly.
202 	 */
203 	if (bl->props.fb_blank != sinfo->bl_power)
204 		power = bl->props.fb_blank;
205 	else if (bl->props.power != sinfo->bl_power)
206 		power = bl->props.power;
207 
208 	if (brightness < 0 && power == FB_BLANK_UNBLANK)
209 		brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
210 	else if (power != FB_BLANK_UNBLANK)
211 		brightness = 0;
212 
213 	lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
214 	if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE)
215 		lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
216 			brightness ? contrast_ctr : 0);
217 	else
218 		lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
219 
220 	bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
221 
222 	return 0;
223 }
224 
225 static int atmel_bl_get_brightness(struct backlight_device *bl)
226 {
227 	struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
228 
229 	return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
230 }
231 
232 static const struct backlight_ops atmel_lcdc_bl_ops = {
233 	.update_status = atmel_bl_update_status,
234 	.get_brightness = atmel_bl_get_brightness,
235 };
236 
237 static void init_backlight(struct atmel_lcdfb_info *sinfo)
238 {
239 	struct backlight_properties props;
240 	struct backlight_device	*bl;
241 
242 	sinfo->bl_power = FB_BLANK_UNBLANK;
243 
244 	if (sinfo->backlight)
245 		return;
246 
247 	memset(&props, 0, sizeof(struct backlight_properties));
248 	props.type = BACKLIGHT_RAW;
249 	props.max_brightness = 0xff;
250 	bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo,
251 				       &atmel_lcdc_bl_ops, &props);
252 	if (IS_ERR(bl)) {
253 		dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
254 				PTR_ERR(bl));
255 		return;
256 	}
257 	sinfo->backlight = bl;
258 
259 	bl->props.power = FB_BLANK_UNBLANK;
260 	bl->props.fb_blank = FB_BLANK_UNBLANK;
261 	bl->props.brightness = atmel_bl_get_brightness(bl);
262 }
263 
264 static void exit_backlight(struct atmel_lcdfb_info *sinfo)
265 {
266 	if (!sinfo->backlight)
267 		return;
268 
269 	if (sinfo->backlight->ops) {
270 		sinfo->backlight->props.power = FB_BLANK_POWERDOWN;
271 		sinfo->backlight->ops->update_status(sinfo->backlight);
272 	}
273 	backlight_device_unregister(sinfo->backlight);
274 }
275 
276 #else
277 
278 static void init_backlight(struct atmel_lcdfb_info *sinfo)
279 {
280 	dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
281 }
282 
283 static void exit_backlight(struct atmel_lcdfb_info *sinfo)
284 {
285 }
286 
287 #endif
288 
289 static void init_contrast(struct atmel_lcdfb_info *sinfo)
290 {
291 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
292 
293 	/* contrast pwm can be 'inverted' */
294 	if (pdata->lcdcon_pol_negative)
295 		contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE);
296 
297 	/* have some default contrast/backlight settings */
298 	lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
299 	lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
300 
301 	if (pdata->lcdcon_is_backlight)
302 		init_backlight(sinfo);
303 }
304 
305 static inline void atmel_lcdfb_power_control(struct atmel_lcdfb_info *sinfo, int on)
306 {
307 	int ret;
308 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
309 
310 	if (pdata->atmel_lcdfb_power_control)
311 		pdata->atmel_lcdfb_power_control(pdata, on);
312 	else if (sinfo->reg_lcd) {
313 		if (on) {
314 			ret = regulator_enable(sinfo->reg_lcd);
315 			if (ret)
316 				dev_err(&sinfo->pdev->dev,
317 					"lcd regulator enable failed:	%d\n", ret);
318 		} else {
319 			ret = regulator_disable(sinfo->reg_lcd);
320 			if (ret)
321 				dev_err(&sinfo->pdev->dev,
322 					"lcd regulator disable failed: %d\n", ret);
323 		}
324 	}
325 }
326 
327 static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
328 	.type		= FB_TYPE_PACKED_PIXELS,
329 	.visual		= FB_VISUAL_TRUECOLOR,
330 	.xpanstep	= 0,
331 	.ypanstep	= 1,
332 	.ywrapstep	= 0,
333 	.accel		= FB_ACCEL_NONE,
334 };
335 
336 static unsigned long compute_hozval(struct atmel_lcdfb_info *sinfo,
337 							unsigned long xres)
338 {
339 	unsigned long lcdcon2;
340 	unsigned long value;
341 
342 	if (!sinfo->config->have_hozval)
343 		return xres;
344 
345 	lcdcon2 = lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2);
346 	value = xres;
347 	if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
348 		/* STN display */
349 		if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
350 			value *= 3;
351 		}
352 		if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
353 		   || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
354 		      && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
355 			value = DIV_ROUND_UP(value, 4);
356 		else
357 			value = DIV_ROUND_UP(value, 8);
358 	}
359 
360 	return value;
361 }
362 
363 static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo)
364 {
365 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
366 
367 	/* Turn off the LCD controller and the DMA controller */
368 	lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
369 			pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
370 
371 	/* Wait for the LCDC core to become idle */
372 	while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
373 		msleep(10);
374 
375 	lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
376 }
377 
378 static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo)
379 {
380 	atmel_lcdfb_stop_nowait(sinfo);
381 
382 	/* Wait for DMA engine to become idle... */
383 	while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
384 		msleep(10);
385 }
386 
387 static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo)
388 {
389 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
390 
391 	lcdc_writel(sinfo, ATMEL_LCDC_DMACON, pdata->default_dmacon);
392 	lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
393 		(pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET)
394 		| ATMEL_LCDC_PWR);
395 }
396 
397 static void atmel_lcdfb_update_dma(struct fb_info *info,
398 			       struct fb_var_screeninfo *var)
399 {
400 	struct atmel_lcdfb_info *sinfo = info->par;
401 	struct fb_fix_screeninfo *fix = &info->fix;
402 	unsigned long dma_addr;
403 
404 	dma_addr = (fix->smem_start + var->yoffset * fix->line_length
405 		    + var->xoffset * info->var.bits_per_pixel / 8);
406 
407 	dma_addr &= ~3UL;
408 
409 	/* Set framebuffer DMA base address and pixel offset */
410 	lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
411 
412 	atmel_lcdfb_update_dma2d(sinfo, var, info);
413 }
414 
415 static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
416 {
417 	struct fb_info *info = sinfo->info;
418 
419 	dma_free_writecombine(info->device, info->fix.smem_len,
420 				info->screen_base, info->fix.smem_start);
421 }
422 
423 /**
424  *	atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
425  *	@sinfo: the frame buffer to allocate memory for
426  *
427  * 	This function is called only from the atmel_lcdfb_probe()
428  * 	so no locking by fb_info->mm_lock around smem_len setting is needed.
429  */
430 static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
431 {
432 	struct fb_info *info = sinfo->info;
433 	struct fb_var_screeninfo *var = &info->var;
434 	unsigned int smem_len;
435 
436 	smem_len = (var->xres_virtual * var->yres_virtual
437 		    * ((var->bits_per_pixel + 7) / 8));
438 	info->fix.smem_len = max(smem_len, sinfo->smem_len);
439 
440 	info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
441 					(dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
442 
443 	if (!info->screen_base) {
444 		return -ENOMEM;
445 	}
446 
447 	memset(info->screen_base, 0, info->fix.smem_len);
448 
449 	return 0;
450 }
451 
452 static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var,
453 						     struct fb_info *info)
454 {
455 	struct fb_videomode varfbmode;
456 	const struct fb_videomode *fbmode = NULL;
457 
458 	fb_var_to_videomode(&varfbmode, var);
459 	fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist);
460 	if (fbmode)
461 		fb_videomode_to_var(var, fbmode);
462 	return fbmode;
463 }
464 
465 
466 /**
467  *      atmel_lcdfb_check_var - Validates a var passed in.
468  *      @var: frame buffer variable screen structure
469  *      @info: frame buffer structure that represents a single frame buffer
470  *
471  *	Checks to see if the hardware supports the state requested by
472  *	var passed in. This function does not alter the hardware
473  *	state!!!  This means the data stored in struct fb_info and
474  *	struct atmel_lcdfb_info do not change. This includes the var
475  *	inside of struct fb_info.  Do NOT change these. This function
476  *	can be called on its own if we intent to only test a mode and
477  *	not actually set it. The stuff in modedb.c is a example of
478  *	this. If the var passed in is slightly off by what the
479  *	hardware can support then we alter the var PASSED in to what
480  *	we can do. If the hardware doesn't support mode change a
481  *	-EINVAL will be returned by the upper layers. You don't need
482  *	to implement this function then. If you hardware doesn't
483  *	support changing the resolution then this function is not
484  *	needed. In this case the driver would just provide a var that
485  *	represents the static state the screen is in.
486  *
487  *	Returns negative errno on error, or zero on success.
488  */
489 static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
490 			     struct fb_info *info)
491 {
492 	struct device *dev = info->device;
493 	struct atmel_lcdfb_info *sinfo = info->par;
494 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
495 	unsigned long clk_value_khz;
496 
497 	clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
498 
499 	dev_dbg(dev, "%s:\n", __func__);
500 
501 	if (!(var->pixclock && var->bits_per_pixel)) {
502 		/* choose a suitable mode if possible */
503 		if (!atmel_lcdfb_choose_mode(var, info)) {
504 			dev_err(dev, "needed value not specified\n");
505 			return -EINVAL;
506 		}
507 	}
508 
509 	dev_dbg(dev, "  resolution: %ux%u\n", var->xres, var->yres);
510 	dev_dbg(dev, "  pixclk:     %lu KHz\n", PICOS2KHZ(var->pixclock));
511 	dev_dbg(dev, "  bpp:        %u\n", var->bits_per_pixel);
512 	dev_dbg(dev, "  clk:        %lu KHz\n", clk_value_khz);
513 
514 	if (PICOS2KHZ(var->pixclock) > clk_value_khz) {
515 		dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
516 		return -EINVAL;
517 	}
518 
519 	/* Do not allow to have real resoulution larger than virtual */
520 	if (var->xres > var->xres_virtual)
521 		var->xres_virtual = var->xres;
522 
523 	if (var->yres > var->yres_virtual)
524 		var->yres_virtual = var->yres;
525 
526 	/* Force same alignment for each line */
527 	var->xres = (var->xres + 3) & ~3UL;
528 	var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
529 
530 	var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
531 	var->transp.msb_right = 0;
532 	var->transp.offset = var->transp.length = 0;
533 	var->xoffset = var->yoffset = 0;
534 
535 	if (info->fix.smem_len) {
536 		unsigned int smem_len = (var->xres_virtual * var->yres_virtual
537 					 * ((var->bits_per_pixel + 7) / 8));
538 		if (smem_len > info->fix.smem_len) {
539 			dev_err(dev, "Frame buffer is too small (%u) for screen size (need at least %u)\n",
540 				info->fix.smem_len, smem_len);
541 			return -EINVAL;
542 		}
543 	}
544 
545 	/* Saturate vertical and horizontal timings at maximum values */
546 	var->vsync_len = min_t(u32, var->vsync_len,
547 			(ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
548 	var->upper_margin = min_t(u32, var->upper_margin,
549 			ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
550 	var->lower_margin = min_t(u32, var->lower_margin,
551 			ATMEL_LCDC_VFP);
552 	var->right_margin = min_t(u32, var->right_margin,
553 			(ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
554 	var->hsync_len = min_t(u32, var->hsync_len,
555 			(ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
556 	var->left_margin = min_t(u32, var->left_margin,
557 			ATMEL_LCDC_HBP + 1);
558 
559 	/* Some parameters can't be zero */
560 	var->vsync_len = max_t(u32, var->vsync_len, 1);
561 	var->right_margin = max_t(u32, var->right_margin, 1);
562 	var->hsync_len = max_t(u32, var->hsync_len, 1);
563 	var->left_margin = max_t(u32, var->left_margin, 1);
564 
565 	switch (var->bits_per_pixel) {
566 	case 1:
567 	case 2:
568 	case 4:
569 	case 8:
570 		var->red.offset = var->green.offset = var->blue.offset = 0;
571 		var->red.length = var->green.length = var->blue.length
572 			= var->bits_per_pixel;
573 		break;
574 	case 16:
575 		/* Older SOCs use IBGR:555 rather than BGR:565. */
576 		if (sinfo->config->have_intensity_bit)
577 			var->green.length = 5;
578 		else
579 			var->green.length = 6;
580 
581 		if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
582 			/* RGB:5X5 mode */
583 			var->red.offset = var->green.length + 5;
584 			var->blue.offset = 0;
585 		} else {
586 			/* BGR:5X5 mode */
587 			var->red.offset = 0;
588 			var->blue.offset = var->green.length + 5;
589 		}
590 		var->green.offset = 5;
591 		var->red.length = var->blue.length = 5;
592 		break;
593 	case 32:
594 		var->transp.offset = 24;
595 		var->transp.length = 8;
596 		/* fall through */
597 	case 24:
598 		if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
599 			/* RGB:888 mode */
600 			var->red.offset = 16;
601 			var->blue.offset = 0;
602 		} else {
603 			/* BGR:888 mode */
604 			var->red.offset = 0;
605 			var->blue.offset = 16;
606 		}
607 		var->green.offset = 8;
608 		var->red.length = var->green.length = var->blue.length = 8;
609 		break;
610 	default:
611 		dev_err(dev, "color depth %d not supported\n",
612 					var->bits_per_pixel);
613 		return -EINVAL;
614 	}
615 
616 	return 0;
617 }
618 
619 /*
620  * LCD reset sequence
621  */
622 static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo)
623 {
624 	might_sleep();
625 
626 	atmel_lcdfb_stop(sinfo);
627 	atmel_lcdfb_start(sinfo);
628 }
629 
630 /**
631  *      atmel_lcdfb_set_par - Alters the hardware state.
632  *      @info: frame buffer structure that represents a single frame buffer
633  *
634  *	Using the fb_var_screeninfo in fb_info we set the resolution
635  *	of the this particular framebuffer. This function alters the
636  *	par AND the fb_fix_screeninfo stored in fb_info. It doesn't
637  *	not alter var in fb_info since we are using that data. This
638  *	means we depend on the data in var inside fb_info to be
639  *	supported by the hardware.  atmel_lcdfb_check_var is always called
640  *	before atmel_lcdfb_set_par to ensure this.  Again if you can't
641  *	change the resolution you don't need this function.
642  *
643  */
644 static int atmel_lcdfb_set_par(struct fb_info *info)
645 {
646 	struct atmel_lcdfb_info *sinfo = info->par;
647 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
648 	unsigned long hozval_linesz;
649 	unsigned long value;
650 	unsigned long clk_value_khz;
651 	unsigned long bits_per_line;
652 	unsigned long pix_factor = 2;
653 
654 	might_sleep();
655 
656 	dev_dbg(info->device, "%s:\n", __func__);
657 	dev_dbg(info->device, "  * resolution: %ux%u (%ux%u virtual)\n",
658 		 info->var.xres, info->var.yres,
659 		 info->var.xres_virtual, info->var.yres_virtual);
660 
661 	atmel_lcdfb_stop_nowait(sinfo);
662 
663 	if (info->var.bits_per_pixel == 1)
664 		info->fix.visual = FB_VISUAL_MONO01;
665 	else if (info->var.bits_per_pixel <= 8)
666 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
667 	else
668 		info->fix.visual = FB_VISUAL_TRUECOLOR;
669 
670 	bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
671 	info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
672 
673 	/* Re-initialize the DMA engine... */
674 	dev_dbg(info->device, "  * update DMA engine\n");
675 	atmel_lcdfb_update_dma(info, &info->var);
676 
677 	/* ...set frame size and burst length = 8 words (?) */
678 	value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
679 	value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
680 	lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
681 
682 	/* Now, the LCDC core... */
683 
684 	/* Set pixel clock */
685 	if (sinfo->config->have_alt_pixclock)
686 		pix_factor = 1;
687 
688 	clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
689 
690 	value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
691 
692 	if (value < pix_factor) {
693 		dev_notice(info->device, "Bypassing pixel clock divider\n");
694 		lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
695 	} else {
696 		value = (value / pix_factor) - 1;
697 		dev_dbg(info->device, "  * programming CLKVAL = 0x%08lx\n",
698 				value);
699 		lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
700 				value << ATMEL_LCDC_CLKVAL_OFFSET);
701 		info->var.pixclock =
702 			KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1)));
703 		dev_dbg(info->device, "  updated pixclk:     %lu KHz\n",
704 					PICOS2KHZ(info->var.pixclock));
705 	}
706 
707 
708 	/* Initialize control register 2 */
709 	value = pdata->default_lcdcon2;
710 
711 	if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
712 		value |= ATMEL_LCDC_INVLINE_INVERTED;
713 	if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
714 		value |= ATMEL_LCDC_INVFRAME_INVERTED;
715 
716 	switch (info->var.bits_per_pixel) {
717 		case 1:	value |= ATMEL_LCDC_PIXELSIZE_1; break;
718 		case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
719 		case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
720 		case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
721 		case 15: /* fall through */
722 		case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
723 		case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
724 		case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
725 		default: BUG(); break;
726 	}
727 	dev_dbg(info->device, "  * LCDCON2 = %08lx\n", value);
728 	lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
729 
730 	/* Vertical timing */
731 	value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
732 	value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
733 	value |= info->var.lower_margin;
734 	dev_dbg(info->device, "  * LCDTIM1 = %08lx\n", value);
735 	lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
736 
737 	/* Horizontal timing */
738 	value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
739 	value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
740 	value |= (info->var.left_margin - 1);
741 	dev_dbg(info->device, "  * LCDTIM2 = %08lx\n", value);
742 	lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
743 
744 	/* Horizontal value (aka line size) */
745 	hozval_linesz = compute_hozval(sinfo, info->var.xres);
746 
747 	/* Display size */
748 	value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
749 	value |= info->var.yres - 1;
750 	dev_dbg(info->device, "  * LCDFRMCFG = %08lx\n", value);
751 	lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
752 
753 	/* FIFO Threshold: Use formula from data sheet */
754 	value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
755 	lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
756 
757 	/* Toggle LCD_MODE every frame */
758 	lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
759 
760 	/* Disable all interrupts */
761 	lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
762 	/* Enable FIFO & DMA errors */
763 	lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
764 
765 	/* ...wait for DMA engine to become idle... */
766 	while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
767 		msleep(10);
768 
769 	atmel_lcdfb_start(sinfo);
770 
771 	dev_dbg(info->device, "  * DONE\n");
772 
773 	return 0;
774 }
775 
776 static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
777 {
778 	chan &= 0xffff;
779 	chan >>= 16 - bf->length;
780 	return chan << bf->offset;
781 }
782 
783 /**
784  *  	atmel_lcdfb_setcolreg - Optional function. Sets a color register.
785  *      @regno: Which register in the CLUT we are programming
786  *      @red: The red value which can be up to 16 bits wide
787  *	@green: The green value which can be up to 16 bits wide
788  *	@blue:  The blue value which can be up to 16 bits wide.
789  *	@transp: If supported the alpha value which can be up to 16 bits wide.
790  *      @info: frame buffer info structure
791  *
792  *  	Set a single color register. The values supplied have a 16 bit
793  *  	magnitude which needs to be scaled in this function for the hardware.
794  *	Things to take into consideration are how many color registers, if
795  *	any, are supported with the current color visual. With truecolor mode
796  *	no color palettes are supported. Here a pseudo palette is created
797  *	which we store the value in pseudo_palette in struct fb_info. For
798  *	pseudocolor mode we have a limited color palette. To deal with this
799  *	we can program what color is displayed for a particular pixel value.
800  *	DirectColor is similar in that we can program each color field. If
801  *	we have a static colormap we don't need to implement this function.
802  *
803  *	Returns negative errno on error, or zero on success. In an
804  *	ideal world, this would have been the case, but as it turns
805  *	out, the other drivers return 1 on failure, so that's what
806  *	we're going to do.
807  */
808 static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
809 			     unsigned int green, unsigned int blue,
810 			     unsigned int transp, struct fb_info *info)
811 {
812 	struct atmel_lcdfb_info *sinfo = info->par;
813 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
814 	unsigned int val;
815 	u32 *pal;
816 	int ret = 1;
817 
818 	if (info->var.grayscale)
819 		red = green = blue = (19595 * red + 38470 * green
820 				      + 7471 * blue) >> 16;
821 
822 	switch (info->fix.visual) {
823 	case FB_VISUAL_TRUECOLOR:
824 		if (regno < 16) {
825 			pal = info->pseudo_palette;
826 
827 			val  = chan_to_field(red, &info->var.red);
828 			val |= chan_to_field(green, &info->var.green);
829 			val |= chan_to_field(blue, &info->var.blue);
830 
831 			pal[regno] = val;
832 			ret = 0;
833 		}
834 		break;
835 
836 	case FB_VISUAL_PSEUDOCOLOR:
837 		if (regno < 256) {
838 			if (sinfo->config->have_intensity_bit) {
839 				/* old style I+BGR:555 */
840 				val  = ((red   >> 11) & 0x001f);
841 				val |= ((green >>  6) & 0x03e0);
842 				val |= ((blue  >>  1) & 0x7c00);
843 
844 				/*
845 				 * TODO: intensity bit. Maybe something like
846 				 *   ~(red[10] ^ green[10] ^ blue[10]) & 1
847 				 */
848 			} else {
849 				/* new style BGR:565 / RGB:565 */
850 				if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
851 					val  = ((blue >> 11) & 0x001f);
852 					val |= ((red  >>  0) & 0xf800);
853 				} else {
854 					val  = ((red  >> 11) & 0x001f);
855 					val |= ((blue >>  0) & 0xf800);
856 				}
857 
858 				val |= ((green >>  5) & 0x07e0);
859 			}
860 
861 			lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
862 			ret = 0;
863 		}
864 		break;
865 
866 	case FB_VISUAL_MONO01:
867 		if (regno < 2) {
868 			val = (regno == 0) ? 0x00 : 0x1F;
869 			lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
870 			ret = 0;
871 		}
872 		break;
873 
874 	}
875 
876 	return ret;
877 }
878 
879 static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
880 			       struct fb_info *info)
881 {
882 	dev_dbg(info->device, "%s\n", __func__);
883 
884 	atmel_lcdfb_update_dma(info, var);
885 
886 	return 0;
887 }
888 
889 static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info)
890 {
891 	struct atmel_lcdfb_info *sinfo = info->par;
892 
893 	switch (blank_mode) {
894 	case FB_BLANK_UNBLANK:
895 	case FB_BLANK_NORMAL:
896 		atmel_lcdfb_start(sinfo);
897 		break;
898 	case FB_BLANK_VSYNC_SUSPEND:
899 	case FB_BLANK_HSYNC_SUSPEND:
900 		break;
901 	case FB_BLANK_POWERDOWN:
902 		atmel_lcdfb_stop(sinfo);
903 		break;
904 	default:
905 		return -EINVAL;
906 	}
907 
908 	/* let fbcon do a soft blank for us */
909 	return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0);
910 }
911 
912 static struct fb_ops atmel_lcdfb_ops = {
913 	.owner		= THIS_MODULE,
914 	.fb_check_var	= atmel_lcdfb_check_var,
915 	.fb_set_par	= atmel_lcdfb_set_par,
916 	.fb_setcolreg	= atmel_lcdfb_setcolreg,
917 	.fb_blank	= atmel_lcdfb_blank,
918 	.fb_pan_display	= atmel_lcdfb_pan_display,
919 	.fb_fillrect	= cfb_fillrect,
920 	.fb_copyarea	= cfb_copyarea,
921 	.fb_imageblit	= cfb_imageblit,
922 };
923 
924 static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
925 {
926 	struct fb_info *info = dev_id;
927 	struct atmel_lcdfb_info *sinfo = info->par;
928 	u32 status;
929 
930 	status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
931 	if (status & ATMEL_LCDC_UFLWI) {
932 		dev_warn(info->device, "FIFO underflow %#x\n", status);
933 		/* reset DMA and FIFO to avoid screen shifting */
934 		schedule_work(&sinfo->task);
935 	}
936 	lcdc_writel(sinfo, ATMEL_LCDC_ICR, status);
937 	return IRQ_HANDLED;
938 }
939 
940 /*
941  * LCD controller task (to reset the LCD)
942  */
943 static void atmel_lcdfb_task(struct work_struct *work)
944 {
945 	struct atmel_lcdfb_info *sinfo =
946 		container_of(work, struct atmel_lcdfb_info, task);
947 
948 	atmel_lcdfb_reset(sinfo);
949 }
950 
951 static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
952 {
953 	struct fb_info *info = sinfo->info;
954 	int ret = 0;
955 
956 	info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
957 
958 	dev_info(info->device,
959 	       "%luKiB frame buffer at %08lx (mapped at %p)\n",
960 	       (unsigned long)info->fix.smem_len / 1024,
961 	       (unsigned long)info->fix.smem_start,
962 	       info->screen_base);
963 
964 	/* Allocate colormap */
965 	ret = fb_alloc_cmap(&info->cmap, 256, 0);
966 	if (ret < 0)
967 		dev_err(info->device, "Alloc color map failed\n");
968 
969 	return ret;
970 }
971 
972 static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
973 {
974 	clk_prepare_enable(sinfo->bus_clk);
975 	clk_prepare_enable(sinfo->lcdc_clk);
976 }
977 
978 static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
979 {
980 	clk_disable_unprepare(sinfo->bus_clk);
981 	clk_disable_unprepare(sinfo->lcdc_clk);
982 }
983 
984 #ifdef CONFIG_OF
985 static const struct of_device_id atmel_lcdfb_dt_ids[] = {
986 	{ .compatible = "atmel,at91sam9261-lcdc" , .data = &at91sam9261_config, },
987 	{ .compatible = "atmel,at91sam9263-lcdc" , .data = &at91sam9263_config, },
988 	{ .compatible = "atmel,at91sam9g10-lcdc" , .data = &at91sam9g10_config, },
989 	{ .compatible = "atmel,at91sam9g45-lcdc" , .data = &at91sam9g45_config, },
990 	{ .compatible = "atmel,at91sam9g45es-lcdc" , .data = &at91sam9g45es_config, },
991 	{ .compatible = "atmel,at91sam9rl-lcdc" , .data = &at91sam9rl_config, },
992 	{ .compatible = "atmel,at32ap-lcdc" , .data = &at32ap_config, },
993 	{ /* sentinel */ }
994 };
995 
996 MODULE_DEVICE_TABLE(of, atmel_lcdfb_dt_ids);
997 
998 static const char *atmel_lcdfb_wiring_modes[] = {
999 	[ATMEL_LCDC_WIRING_BGR]	= "BRG",
1000 	[ATMEL_LCDC_WIRING_RGB]	= "RGB",
1001 };
1002 
1003 const int atmel_lcdfb_get_of_wiring_modes(struct device_node *np)
1004 {
1005 	const char *mode;
1006 	int err, i;
1007 
1008 	err = of_property_read_string(np, "atmel,lcd-wiring-mode", &mode);
1009 	if (err < 0)
1010 		return ATMEL_LCDC_WIRING_BGR;
1011 
1012 	for (i = 0; i < ARRAY_SIZE(atmel_lcdfb_wiring_modes); i++)
1013 		if (!strcasecmp(mode, atmel_lcdfb_wiring_modes[i]))
1014 			return i;
1015 
1016 	return -ENODEV;
1017 }
1018 
1019 static void atmel_lcdfb_power_control_gpio(struct atmel_lcdfb_pdata *pdata, int on)
1020 {
1021 	struct atmel_lcdfb_power_ctrl_gpio *og;
1022 
1023 	list_for_each_entry(og, &pdata->pwr_gpios, list)
1024 		gpio_set_value(og->gpio, on);
1025 }
1026 
1027 static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
1028 {
1029 	struct fb_info *info = sinfo->info;
1030 	struct atmel_lcdfb_pdata *pdata = &sinfo->pdata;
1031 	struct fb_var_screeninfo *var = &info->var;
1032 	struct device *dev = &sinfo->pdev->dev;
1033 	struct device_node *np =dev->of_node;
1034 	struct device_node *display_np;
1035 	struct device_node *timings_np;
1036 	struct display_timings *timings;
1037 	enum of_gpio_flags flags;
1038 	struct atmel_lcdfb_power_ctrl_gpio *og;
1039 	bool is_gpio_power = false;
1040 	int ret = -ENOENT;
1041 	int i, gpio;
1042 
1043 	sinfo->config = (struct atmel_lcdfb_config*)
1044 		of_match_device(atmel_lcdfb_dt_ids, dev)->data;
1045 
1046 	display_np = of_parse_phandle(np, "display", 0);
1047 	if (!display_np) {
1048 		dev_err(dev, "failed to find display phandle\n");
1049 		return -ENOENT;
1050 	}
1051 
1052 	ret = of_property_read_u32(display_np, "bits-per-pixel", &var->bits_per_pixel);
1053 	if (ret < 0) {
1054 		dev_err(dev, "failed to get property bits-per-pixel\n");
1055 		goto put_display_node;
1056 	}
1057 
1058 	ret = of_property_read_u32(display_np, "atmel,guard-time", &pdata->guard_time);
1059 	if (ret < 0) {
1060 		dev_err(dev, "failed to get property atmel,guard-time\n");
1061 		goto put_display_node;
1062 	}
1063 
1064 	ret = of_property_read_u32(display_np, "atmel,lcdcon2", &pdata->default_lcdcon2);
1065 	if (ret < 0) {
1066 		dev_err(dev, "failed to get property atmel,lcdcon2\n");
1067 		goto put_display_node;
1068 	}
1069 
1070 	ret = of_property_read_u32(display_np, "atmel,dmacon", &pdata->default_dmacon);
1071 	if (ret < 0) {
1072 		dev_err(dev, "failed to get property bits-per-pixel\n");
1073 		goto put_display_node;
1074 	}
1075 
1076 	INIT_LIST_HEAD(&pdata->pwr_gpios);
1077 	ret = -ENOMEM;
1078 	for (i = 0; i < of_gpio_named_count(display_np, "atmel,power-control-gpio"); i++) {
1079 		gpio = of_get_named_gpio_flags(display_np, "atmel,power-control-gpio",
1080 					       i, &flags);
1081 		if (gpio < 0)
1082 			continue;
1083 
1084 		og = devm_kzalloc(dev, sizeof(*og), GFP_KERNEL);
1085 		if (!og)
1086 			goto put_display_node;
1087 
1088 		og->gpio = gpio;
1089 		og->active_low = flags & OF_GPIO_ACTIVE_LOW;
1090 		is_gpio_power = true;
1091 		ret = devm_gpio_request(dev, gpio, "lcd-power-control-gpio");
1092 		if (ret) {
1093 			dev_err(dev, "request gpio %d failed\n", gpio);
1094 			goto put_display_node;
1095 		}
1096 
1097 		ret = gpio_direction_output(gpio, og->active_low);
1098 		if (ret) {
1099 			dev_err(dev, "set direction output gpio %d failed\n", gpio);
1100 			goto put_display_node;
1101 		}
1102 		list_add(&og->list, &pdata->pwr_gpios);
1103 	}
1104 
1105 	if (is_gpio_power)
1106 		pdata->atmel_lcdfb_power_control = atmel_lcdfb_power_control_gpio;
1107 
1108 	ret = atmel_lcdfb_get_of_wiring_modes(display_np);
1109 	if (ret < 0) {
1110 		dev_err(dev, "invalid atmel,lcd-wiring-mode\n");
1111 		goto put_display_node;
1112 	}
1113 	pdata->lcd_wiring_mode = ret;
1114 
1115 	pdata->lcdcon_is_backlight = of_property_read_bool(display_np, "atmel,lcdcon-backlight");
1116 	pdata->lcdcon_pol_negative = of_property_read_bool(display_np, "atmel,lcdcon-backlight-inverted");
1117 
1118 	timings = of_get_display_timings(display_np);
1119 	if (!timings) {
1120 		dev_err(dev, "failed to get display timings\n");
1121 		ret = -EINVAL;
1122 		goto put_display_node;
1123 	}
1124 
1125 	timings_np = of_find_node_by_name(display_np, "display-timings");
1126 	if (!timings_np) {
1127 		dev_err(dev, "failed to find display-timings node\n");
1128 		ret = -ENODEV;
1129 		goto put_display_node;
1130 	}
1131 
1132 	for (i = 0; i < of_get_child_count(timings_np); i++) {
1133 		struct videomode vm;
1134 		struct fb_videomode fb_vm;
1135 
1136 		ret = videomode_from_timings(timings, &vm, i);
1137 		if (ret < 0)
1138 			goto put_timings_node;
1139 		ret = fb_videomode_from_videomode(&vm, &fb_vm);
1140 		if (ret < 0)
1141 			goto put_timings_node;
1142 
1143 		fb_add_videomode(&fb_vm, &info->modelist);
1144 	}
1145 
1146 	return 0;
1147 
1148 put_timings_node:
1149 	of_node_put(timings_np);
1150 put_display_node:
1151 	of_node_put(display_np);
1152 	return ret;
1153 }
1154 #else
1155 static int atmel_lcdfb_of_init(struct atmel_lcdfb_info *sinfo)
1156 {
1157 	return 0;
1158 }
1159 #endif
1160 
1161 static int __init atmel_lcdfb_probe(struct platform_device *pdev)
1162 {
1163 	struct device *dev = &pdev->dev;
1164 	struct fb_info *info;
1165 	struct atmel_lcdfb_info *sinfo;
1166 	struct atmel_lcdfb_pdata *pdata = NULL;
1167 	struct resource *regs = NULL;
1168 	struct resource *map = NULL;
1169 	struct fb_modelist *modelist;
1170 	int ret;
1171 
1172 	dev_dbg(dev, "%s BEGIN\n", __func__);
1173 
1174 	ret = -ENOMEM;
1175 	info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
1176 	if (!info) {
1177 		dev_err(dev, "cannot allocate memory\n");
1178 		goto out;
1179 	}
1180 
1181 	sinfo = info->par;
1182 	sinfo->pdev = pdev;
1183 	sinfo->info = info;
1184 
1185 	INIT_LIST_HEAD(&info->modelist);
1186 
1187 	if (pdev->dev.of_node) {
1188 		ret = atmel_lcdfb_of_init(sinfo);
1189 		if (ret)
1190 			goto free_info;
1191 	} else if (dev_get_platdata(dev)) {
1192 		struct fb_monspecs *monspecs;
1193 		int i;
1194 
1195 		pdata = dev_get_platdata(dev);
1196 		monspecs = pdata->default_monspecs;
1197 		sinfo->pdata = *pdata;
1198 
1199 		for (i = 0; i < monspecs->modedb_len; i++)
1200 			fb_add_videomode(&monspecs->modedb[i], &info->modelist);
1201 
1202 		sinfo->config = atmel_lcdfb_get_config(pdev);
1203 
1204 		info->var.bits_per_pixel = pdata->default_bpp ? pdata->default_bpp : 16;
1205 		memcpy(&info->monspecs, pdata->default_monspecs, sizeof(info->monspecs));
1206 	} else {
1207 		dev_err(dev, "cannot get default configuration\n");
1208 		goto free_info;
1209 	}
1210 
1211 	if (!sinfo->config)
1212 		goto free_info;
1213 
1214 	sinfo->reg_lcd = devm_regulator_get(&pdev->dev, "lcd");
1215 	if (IS_ERR(sinfo->reg_lcd))
1216 		sinfo->reg_lcd = NULL;
1217 
1218 	info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
1219 	info->pseudo_palette = sinfo->pseudo_palette;
1220 	info->fbops = &atmel_lcdfb_ops;
1221 
1222 	info->fix = atmel_lcdfb_fix;
1223 	strcpy(info->fix.id, sinfo->pdev->name);
1224 
1225 	/* Enable LCDC Clocks */
1226 	sinfo->bus_clk = clk_get(dev, "hclk");
1227 	if (IS_ERR(sinfo->bus_clk)) {
1228 		ret = PTR_ERR(sinfo->bus_clk);
1229 		goto free_info;
1230 	}
1231 	sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
1232 	if (IS_ERR(sinfo->lcdc_clk)) {
1233 		ret = PTR_ERR(sinfo->lcdc_clk);
1234 		goto put_bus_clk;
1235 	}
1236 	atmel_lcdfb_start_clock(sinfo);
1237 
1238 	modelist = list_first_entry(&info->modelist,
1239 			struct fb_modelist, list);
1240 	fb_videomode_to_var(&info->var, &modelist->mode);
1241 
1242 	atmel_lcdfb_check_var(&info->var, info);
1243 
1244 	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1245 	if (!regs) {
1246 		dev_err(dev, "resources unusable\n");
1247 		ret = -ENXIO;
1248 		goto stop_clk;
1249 	}
1250 
1251 	sinfo->irq_base = platform_get_irq(pdev, 0);
1252 	if (sinfo->irq_base < 0) {
1253 		dev_err(dev, "unable to get irq\n");
1254 		ret = sinfo->irq_base;
1255 		goto stop_clk;
1256 	}
1257 
1258 	/* Initialize video memory */
1259 	map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1260 	if (map) {
1261 		/* use a pre-allocated memory buffer */
1262 		info->fix.smem_start = map->start;
1263 		info->fix.smem_len = resource_size(map);
1264 		if (!request_mem_region(info->fix.smem_start,
1265 					info->fix.smem_len, pdev->name)) {
1266 			ret = -EBUSY;
1267 			goto stop_clk;
1268 		}
1269 
1270 		info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
1271 		if (!info->screen_base) {
1272 			ret = -ENOMEM;
1273 			goto release_intmem;
1274 		}
1275 
1276 		/*
1277 		 * Don't clear the framebuffer -- someone may have set
1278 		 * up a splash image.
1279 		 */
1280 	} else {
1281 		/* allocate memory buffer */
1282 		ret = atmel_lcdfb_alloc_video_memory(sinfo);
1283 		if (ret < 0) {
1284 			dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
1285 			goto stop_clk;
1286 		}
1287 	}
1288 
1289 	/* LCDC registers */
1290 	info->fix.mmio_start = regs->start;
1291 	info->fix.mmio_len = resource_size(regs);
1292 
1293 	if (!request_mem_region(info->fix.mmio_start,
1294 				info->fix.mmio_len, pdev->name)) {
1295 		ret = -EBUSY;
1296 		goto free_fb;
1297 	}
1298 
1299 	sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
1300 	if (!sinfo->mmio) {
1301 		dev_err(dev, "cannot map LCDC registers\n");
1302 		ret = -ENOMEM;
1303 		goto release_mem;
1304 	}
1305 
1306 	/* Initialize PWM for contrast or backlight ("off") */
1307 	init_contrast(sinfo);
1308 
1309 	/* interrupt */
1310 	ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
1311 	if (ret) {
1312 		dev_err(dev, "request_irq failed: %d\n", ret);
1313 		goto unmap_mmio;
1314 	}
1315 
1316 	/* Some operations on the LCDC might sleep and
1317 	 * require a preemptible task context */
1318 	INIT_WORK(&sinfo->task, atmel_lcdfb_task);
1319 
1320 	ret = atmel_lcdfb_init_fbinfo(sinfo);
1321 	if (ret < 0) {
1322 		dev_err(dev, "init fbinfo failed: %d\n", ret);
1323 		goto unregister_irqs;
1324 	}
1325 
1326 	ret = atmel_lcdfb_set_par(info);
1327 	if (ret < 0) {
1328 		dev_err(dev, "set par failed: %d\n", ret);
1329 		goto unregister_irqs;
1330 	}
1331 
1332 	dev_set_drvdata(dev, info);
1333 
1334 	/*
1335 	 * Tell the world that we're ready to go
1336 	 */
1337 	ret = register_framebuffer(info);
1338 	if (ret < 0) {
1339 		dev_err(dev, "failed to register framebuffer device: %d\n", ret);
1340 		goto reset_drvdata;
1341 	}
1342 
1343 	/* Power up the LCDC screen */
1344 	atmel_lcdfb_power_control(sinfo, 1);
1345 
1346 	dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n",
1347 		       info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
1348 
1349 	return 0;
1350 
1351 reset_drvdata:
1352 	dev_set_drvdata(dev, NULL);
1353 	fb_dealloc_cmap(&info->cmap);
1354 unregister_irqs:
1355 	cancel_work_sync(&sinfo->task);
1356 	free_irq(sinfo->irq_base, info);
1357 unmap_mmio:
1358 	exit_backlight(sinfo);
1359 	iounmap(sinfo->mmio);
1360 release_mem:
1361  	release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1362 free_fb:
1363 	if (map)
1364 		iounmap(info->screen_base);
1365 	else
1366 		atmel_lcdfb_free_video_memory(sinfo);
1367 
1368 release_intmem:
1369 	if (map)
1370 		release_mem_region(info->fix.smem_start, info->fix.smem_len);
1371 stop_clk:
1372 	atmel_lcdfb_stop_clock(sinfo);
1373 	clk_put(sinfo->lcdc_clk);
1374 put_bus_clk:
1375 	clk_put(sinfo->bus_clk);
1376 free_info:
1377 	framebuffer_release(info);
1378 out:
1379 	dev_dbg(dev, "%s FAILED\n", __func__);
1380 	return ret;
1381 }
1382 
1383 static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
1384 {
1385 	struct device *dev = &pdev->dev;
1386 	struct fb_info *info = dev_get_drvdata(dev);
1387 	struct atmel_lcdfb_info *sinfo;
1388 	struct atmel_lcdfb_pdata *pdata;
1389 
1390 	if (!info || !info->par)
1391 		return 0;
1392 	sinfo = info->par;
1393 	pdata = &sinfo->pdata;
1394 
1395 	cancel_work_sync(&sinfo->task);
1396 	exit_backlight(sinfo);
1397 	atmel_lcdfb_power_control(sinfo, 0);
1398 	unregister_framebuffer(info);
1399 	atmel_lcdfb_stop_clock(sinfo);
1400 	clk_put(sinfo->lcdc_clk);
1401 	clk_put(sinfo->bus_clk);
1402 	fb_dealloc_cmap(&info->cmap);
1403 	free_irq(sinfo->irq_base, info);
1404 	iounmap(sinfo->mmio);
1405  	release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
1406 	if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
1407 		iounmap(info->screen_base);
1408 		release_mem_region(info->fix.smem_start, info->fix.smem_len);
1409 	} else {
1410 		atmel_lcdfb_free_video_memory(sinfo);
1411 	}
1412 
1413 	framebuffer_release(info);
1414 
1415 	return 0;
1416 }
1417 
1418 #ifdef CONFIG_PM
1419 
1420 static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
1421 {
1422 	struct fb_info *info = platform_get_drvdata(pdev);
1423 	struct atmel_lcdfb_info *sinfo = info->par;
1424 
1425 	/*
1426 	 * We don't want to handle interrupts while the clock is
1427 	 * stopped. It may take forever.
1428 	 */
1429 	lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
1430 
1431 	sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_CTR);
1432 	lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
1433 	atmel_lcdfb_power_control(sinfo, 0);
1434 	atmel_lcdfb_stop(sinfo);
1435 	atmel_lcdfb_stop_clock(sinfo);
1436 
1437 	return 0;
1438 }
1439 
1440 static int atmel_lcdfb_resume(struct platform_device *pdev)
1441 {
1442 	struct fb_info *info = platform_get_drvdata(pdev);
1443 	struct atmel_lcdfb_info *sinfo = info->par;
1444 
1445 	atmel_lcdfb_start_clock(sinfo);
1446 	atmel_lcdfb_start(sinfo);
1447 	atmel_lcdfb_power_control(sinfo, 1);
1448 	lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
1449 
1450 	/* Enable FIFO & DMA errors */
1451 	lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI
1452 			| ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI);
1453 
1454 	return 0;
1455 }
1456 
1457 #else
1458 #define atmel_lcdfb_suspend	NULL
1459 #define atmel_lcdfb_resume	NULL
1460 #endif
1461 
1462 static struct platform_driver atmel_lcdfb_driver = {
1463 	.remove		= __exit_p(atmel_lcdfb_remove),
1464 	.suspend	= atmel_lcdfb_suspend,
1465 	.resume		= atmel_lcdfb_resume,
1466 	.id_table	= atmel_lcdfb_devtypes,
1467 	.driver		= {
1468 		.name	= "atmel_lcdfb",
1469 		.owner	= THIS_MODULE,
1470 		.of_match_table	= of_match_ptr(atmel_lcdfb_dt_ids),
1471 	},
1472 };
1473 
1474 module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe);
1475 
1476 MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
1477 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1478 MODULE_LICENSE("GPL");
1479