1/*
2 * File:         drivers/video/bfin-t350mcqb-fb.c
3 * Based on:
4 * Author:       Michael Hennerich <hennerich@blackfin.uclinux.org>
5 *
6 * Created:
7 * Description:  Blackfin LCD Framebuffer driver
8 *
9 *
10 * Modified:
11 *               Copyright 2004-2007 Analog Devices Inc.
12 *
13 * Bugs:         Enter bugs at http://blackfin.uclinux.org/
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see the file COPYING, or write
27 * to the Free Software Foundation, Inc.,
28 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 */
30
31#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/errno.h>
34#include <linux/string.h>
35#include <linux/gfp.h>
36#include <linux/fb.h>
37#include <linux/init.h>
38#include <linux/types.h>
39#include <linux/interrupt.h>
40#include <linux/device.h>
41#include <linux/backlight.h>
42#include <linux/lcd.h>
43#include <linux/dma-mapping.h>
44#include <linux/platform_device.h>
45
46#include <asm/blackfin.h>
47#include <asm/irq.h>
48#include <asm/dma-mapping.h>
49#include <asm/dma.h>
50#include <asm/portmux.h>
51#include <asm/gptimers.h>
52
53#define NO_BL_SUPPORT
54
55#define LCD_X_RES		320	/* Horizontal Resolution */
56#define LCD_Y_RES		240	/* Vertical Resolution */
57#define LCD_BPP			24	/* Bit Per Pixel */
58
59#define	DMA_BUS_SIZE		16
60#define	LCD_CLK         	(12*1000*1000)	/* 12MHz */
61
62#define CLOCKS_PER_PIX		3
63
64	/*
65	 * HS and VS timing parameters (all in number of PPI clk ticks)
66	 */
67
68#define U_LINE		1				/* Blanking Lines */
69
70#define H_ACTPIX	(LCD_X_RES * CLOCKS_PER_PIX)	/* active horizontal pixel */
71#define H_PERIOD	(408 * CLOCKS_PER_PIX)		/* HS period */
72#define H_PULSE		90				/* HS pulse width */
73#define H_START		204				/* first valid pixel */
74
75#define	V_LINES		(LCD_Y_RES + U_LINE)		/* total vertical lines */
76#define V_PULSE		(3 * H_PERIOD)			/* VS pulse width (1-5 H_PERIODs) */
77#define V_PERIOD	(H_PERIOD * V_LINES)		/* VS period */
78
79#define ACTIVE_VIDEO_MEM_OFFSET	(U_LINE * H_ACTPIX)
80
81#define BFIN_LCD_NBR_PALETTE_ENTRIES	256
82
83#define DRIVER_NAME "bfin-t350mcqb"
84static char driver_name[] = DRIVER_NAME;
85
86struct bfin_t350mcqbfb_info {
87	struct fb_info *fb;
88	struct device *dev;
89	unsigned char *fb_buffer;	/* RGB Buffer */
90	dma_addr_t dma_handle;
91	int lq043_open_cnt;
92	int irq;
93	spinlock_t lock;	/* lock */
94	u32 pseudo_pal[16];
95};
96
97static int nocursor;
98module_param(nocursor, int, 0644);
99MODULE_PARM_DESC(nocursor, "cursor enable/disable");
100
101#define PPI_TX_MODE		0x2
102#define PPI_XFER_TYPE_11	0xC
103#define PPI_PORT_CFG_01		0x10
104#define PPI_PACK_EN		0x80
105#define PPI_POLS_1		0x8000
106
107static void bfin_t350mcqb_config_ppi(struct bfin_t350mcqbfb_info *fbi)
108{
109	bfin_write_PPI_DELAY(H_START);
110	bfin_write_PPI_COUNT(H_ACTPIX-1);
111	bfin_write_PPI_FRAME(V_LINES);
112
113	bfin_write_PPI_CONTROL(PPI_TX_MODE |	   /* output mode , PORT_DIR */
114				PPI_XFER_TYPE_11 | /* sync mode XFR_TYPE */
115				PPI_PORT_CFG_01 |  /* two frame sync PORT_CFG */
116				PPI_PACK_EN |	   /* packing enabled PACK_EN */
117				PPI_POLS_1);	   /* faling edge syncs POLS */
118}
119
120static inline void bfin_t350mcqb_disable_ppi(void)
121{
122	bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() & ~PORT_EN);
123}
124
125static inline void bfin_t350mcqb_enable_ppi(void)
126{
127	bfin_write_PPI_CONTROL(bfin_read_PPI_CONTROL() | PORT_EN);
128}
129
130static void bfin_t350mcqb_start_timers(void)
131{
132	unsigned long flags;
133
134	local_irq_save(flags);
135		enable_gptimers(TIMER1bit);
136		enable_gptimers(TIMER0bit);
137	local_irq_restore(flags);
138}
139
140static void bfin_t350mcqb_stop_timers(void)
141{
142	disable_gptimers(TIMER0bit | TIMER1bit);
143
144	set_gptimer_status(0, TIMER_STATUS_TRUN0 | TIMER_STATUS_TRUN1 |
145				TIMER_STATUS_TIMIL0 | TIMER_STATUS_TIMIL1 |
146				 TIMER_STATUS_TOVF0 | TIMER_STATUS_TOVF1);
147
148}
149
150static void bfin_t350mcqb_init_timers(void)
151{
152
153	bfin_t350mcqb_stop_timers();
154
155	set_gptimer_period(TIMER0_id, H_PERIOD);
156	set_gptimer_pwidth(TIMER0_id, H_PULSE);
157	set_gptimer_config(TIMER0_id, TIMER_MODE_PWM | TIMER_PERIOD_CNT |
158				      TIMER_TIN_SEL | TIMER_CLK_SEL|
159				      TIMER_EMU_RUN);
160
161	set_gptimer_period(TIMER1_id, V_PERIOD);
162	set_gptimer_pwidth(TIMER1_id, V_PULSE);
163	set_gptimer_config(TIMER1_id, TIMER_MODE_PWM | TIMER_PERIOD_CNT |
164				      TIMER_TIN_SEL | TIMER_CLK_SEL |
165				      TIMER_EMU_RUN);
166
167}
168
169static void bfin_t350mcqb_config_dma(struct bfin_t350mcqbfb_info *fbi)
170{
171
172	set_dma_config(CH_PPI,
173		       set_bfin_dma_config(DIR_READ, DMA_FLOW_AUTO,
174					   INTR_DISABLE, DIMENSION_2D,
175					   DATA_SIZE_16,
176					   DMA_NOSYNC_KEEP_DMA_BUF));
177	set_dma_x_count(CH_PPI, (LCD_X_RES * LCD_BPP) / DMA_BUS_SIZE);
178	set_dma_x_modify(CH_PPI, DMA_BUS_SIZE / 8);
179	set_dma_y_count(CH_PPI, V_LINES);
180
181	set_dma_y_modify(CH_PPI, DMA_BUS_SIZE / 8);
182	set_dma_start_addr(CH_PPI, (unsigned long)fbi->fb_buffer);
183
184}
185
186static	u16 ppi0_req_8[] = {P_PPI0_CLK, P_PPI0_FS1, P_PPI0_FS2,
187			    P_PPI0_D0, P_PPI0_D1, P_PPI0_D2,
188			    P_PPI0_D3, P_PPI0_D4, P_PPI0_D5,
189			    P_PPI0_D6, P_PPI0_D7, 0};
190
191static int bfin_t350mcqb_request_ports(int action)
192{
193	if (action) {
194		if (peripheral_request_list(ppi0_req_8, DRIVER_NAME)) {
195			printk(KERN_ERR "Requesting Peripherals failed\n");
196			return -EFAULT;
197		}
198	} else
199		peripheral_free_list(ppi0_req_8);
200
201	return 0;
202}
203
204static int bfin_t350mcqb_fb_open(struct fb_info *info, int user)
205{
206	struct bfin_t350mcqbfb_info *fbi = info->par;
207
208	spin_lock(&fbi->lock);
209	fbi->lq043_open_cnt++;
210
211	if (fbi->lq043_open_cnt <= 1) {
212
213		bfin_t350mcqb_disable_ppi();
214		SSYNC();
215
216		bfin_t350mcqb_config_dma(fbi);
217		bfin_t350mcqb_config_ppi(fbi);
218		bfin_t350mcqb_init_timers();
219
220		/* start dma */
221		enable_dma(CH_PPI);
222		bfin_t350mcqb_enable_ppi();
223		bfin_t350mcqb_start_timers();
224	}
225
226	spin_unlock(&fbi->lock);
227
228	return 0;
229}
230
231static int bfin_t350mcqb_fb_release(struct fb_info *info, int user)
232{
233	struct bfin_t350mcqbfb_info *fbi = info->par;
234
235	spin_lock(&fbi->lock);
236
237	fbi->lq043_open_cnt--;
238
239	if (fbi->lq043_open_cnt <= 0) {
240		bfin_t350mcqb_disable_ppi();
241		SSYNC();
242		disable_dma(CH_PPI);
243		bfin_t350mcqb_stop_timers();
244	}
245
246	spin_unlock(&fbi->lock);
247
248	return 0;
249}
250
251static int bfin_t350mcqb_fb_check_var(struct fb_var_screeninfo *var,
252				   struct fb_info *info)
253{
254
255	switch (var->bits_per_pixel) {
256	case 24:/* TRUECOLOUR, 16m */
257		var->red.offset = 0;
258		var->green.offset = 8;
259		var->blue.offset = 16;
260		var->red.length = var->green.length = var->blue.length = 8;
261		var->transp.offset = 0;
262		var->transp.length = 0;
263		var->transp.msb_right = 0;
264		var->red.msb_right = 0;
265		var->green.msb_right = 0;
266		var->blue.msb_right = 0;
267		break;
268	default:
269		pr_debug("%s: depth not supported: %u BPP\n", __func__,
270			 var->bits_per_pixel);
271		return -EINVAL;
272	}
273
274	if (info->var.xres != var->xres || info->var.yres != var->yres ||
275	    info->var.xres_virtual != var->xres_virtual ||
276	    info->var.yres_virtual != var->yres_virtual) {
277		pr_debug("%s: Resolution not supported: X%u x Y%u \n",
278			 __func__, var->xres, var->yres);
279		return -EINVAL;
280	}
281
282	/*
283	 *  Memory limit
284	 */
285
286	if ((info->fix.line_length * var->yres_virtual) > info->fix.smem_len) {
287		pr_debug("%s: Memory Limit requested yres_virtual = %u\n",
288			 __func__, var->yres_virtual);
289		return -ENOMEM;
290	}
291
292	return 0;
293}
294
295int bfin_t350mcqb_fb_cursor(struct fb_info *info, struct fb_cursor *cursor)
296{
297	if (nocursor)
298		return 0;
299	else
300		return -EINVAL;	/* just to force soft_cursor() call */
301}
302
303static int bfin_t350mcqb_fb_setcolreg(u_int regno, u_int red, u_int green,
304				   u_int blue, u_int transp,
305				   struct fb_info *info)
306{
307	if (regno >= BFIN_LCD_NBR_PALETTE_ENTRIES)
308		return -EINVAL;
309
310	if (info->var.grayscale) {
311		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
312		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
313	}
314
315	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
316
317		u32 value;
318		/* Place color in the pseudopalette */
319		if (regno > 16)
320			return -EINVAL;
321
322		red >>= (16 - info->var.red.length);
323		green >>= (16 - info->var.green.length);
324		blue >>= (16 - info->var.blue.length);
325
326		value = (red << info->var.red.offset) |
327		    (green << info->var.green.offset) |
328		    (blue << info->var.blue.offset);
329		value &= 0xFFFFFF;
330
331		((u32 *) (info->pseudo_palette))[regno] = value;
332
333	}
334
335	return 0;
336}
337
338static struct fb_ops bfin_t350mcqb_fb_ops = {
339	.owner = THIS_MODULE,
340	.fb_open = bfin_t350mcqb_fb_open,
341	.fb_release = bfin_t350mcqb_fb_release,
342	.fb_check_var = bfin_t350mcqb_fb_check_var,
343	.fb_fillrect = cfb_fillrect,
344	.fb_copyarea = cfb_copyarea,
345	.fb_imageblit = cfb_imageblit,
346	.fb_cursor = bfin_t350mcqb_fb_cursor,
347	.fb_setcolreg = bfin_t350mcqb_fb_setcolreg,
348};
349
350#ifndef NO_BL_SUPPORT
351static int bl_get_brightness(struct backlight_device *bd)
352{
353	return 0;
354}
355
356static const struct backlight_ops bfin_lq043fb_bl_ops = {
357	.get_brightness = bl_get_brightness,
358};
359
360static struct backlight_device *bl_dev;
361
362static int bfin_lcd_get_power(struct lcd_device *dev)
363{
364	return 0;
365}
366
367static int bfin_lcd_set_power(struct lcd_device *dev, int power)
368{
369	return 0;
370}
371
372static int bfin_lcd_get_contrast(struct lcd_device *dev)
373{
374	return 0;
375}
376
377static int bfin_lcd_set_contrast(struct lcd_device *dev, int contrast)
378{
379
380	return 0;
381}
382
383static int bfin_lcd_check_fb(struct lcd_device *dev, struct fb_info *fi)
384{
385	if (!fi || (fi == &bfin_t350mcqb_fb))
386		return 1;
387	return 0;
388}
389
390static struct lcd_ops bfin_lcd_ops = {
391	.get_power = bfin_lcd_get_power,
392	.set_power = bfin_lcd_set_power,
393	.get_contrast = bfin_lcd_get_contrast,
394	.set_contrast = bfin_lcd_set_contrast,
395	.check_fb = bfin_lcd_check_fb,
396};
397
398static struct lcd_device *lcd_dev;
399#endif
400
401static irqreturn_t bfin_t350mcqb_irq_error(int irq, void *dev_id)
402{
403	/*struct bfin_t350mcqbfb_info *info = (struct bfin_t350mcqbfb_info *)dev_id;*/
404
405	u16 status = bfin_read_PPI_STATUS();
406	bfin_write_PPI_STATUS(0xFFFF);
407
408	if (status) {
409		bfin_t350mcqb_disable_ppi();
410		disable_dma(CH_PPI);
411
412		/* start dma */
413		enable_dma(CH_PPI);
414		bfin_t350mcqb_enable_ppi();
415		bfin_write_PPI_STATUS(0xFFFF);
416	}
417
418	return IRQ_HANDLED;
419}
420
421static int bfin_t350mcqb_probe(struct platform_device *pdev)
422{
423#ifndef NO_BL_SUPPORT
424	struct backlight_properties props;
425#endif
426	struct bfin_t350mcqbfb_info *info;
427	struct fb_info *fbinfo;
428	int ret;
429
430	printk(KERN_INFO DRIVER_NAME ": %dx%d %d-bit RGB FrameBuffer initializing...\n",
431					 LCD_X_RES, LCD_Y_RES, LCD_BPP);
432
433	if (request_dma(CH_PPI, "CH_PPI") < 0) {
434		printk(KERN_ERR DRIVER_NAME
435		       ": couldn't request CH_PPI DMA\n");
436		ret = -EFAULT;
437		goto out1;
438	}
439
440	fbinfo =
441	    framebuffer_alloc(sizeof(struct bfin_t350mcqbfb_info), &pdev->dev);
442	if (!fbinfo) {
443		ret = -ENOMEM;
444		goto out2;
445	}
446
447	info = fbinfo->par;
448	info->fb = fbinfo;
449	info->dev = &pdev->dev;
450	spin_lock_init(&info->lock);
451
452	platform_set_drvdata(pdev, fbinfo);
453
454	strcpy(fbinfo->fix.id, driver_name);
455
456	fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
457	fbinfo->fix.type_aux = 0;
458	fbinfo->fix.xpanstep = 0;
459	fbinfo->fix.ypanstep = 0;
460	fbinfo->fix.ywrapstep = 0;
461	fbinfo->fix.accel = FB_ACCEL_NONE;
462	fbinfo->fix.visual = FB_VISUAL_TRUECOLOR;
463
464	fbinfo->var.nonstd = 0;
465	fbinfo->var.activate = FB_ACTIVATE_NOW;
466	fbinfo->var.height = 53;
467	fbinfo->var.width = 70;
468	fbinfo->var.accel_flags = 0;
469	fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
470
471	fbinfo->var.xres = LCD_X_RES;
472	fbinfo->var.xres_virtual = LCD_X_RES;
473	fbinfo->var.yres = LCD_Y_RES;
474	fbinfo->var.yres_virtual = LCD_Y_RES;
475	fbinfo->var.bits_per_pixel = LCD_BPP;
476
477	fbinfo->var.red.offset = 0;
478	fbinfo->var.green.offset = 8;
479	fbinfo->var.blue.offset = 16;
480	fbinfo->var.transp.offset = 0;
481	fbinfo->var.red.length = 8;
482	fbinfo->var.green.length = 8;
483	fbinfo->var.blue.length = 8;
484	fbinfo->var.transp.length = 0;
485	fbinfo->fix.smem_len = LCD_X_RES * LCD_Y_RES * LCD_BPP / 8;
486
487	fbinfo->fix.line_length = fbinfo->var.xres_virtual *
488	    fbinfo->var.bits_per_pixel / 8;
489
490
491	fbinfo->fbops = &bfin_t350mcqb_fb_ops;
492	fbinfo->flags = FBINFO_FLAG_DEFAULT;
493
494	info->fb_buffer = dma_alloc_coherent(NULL, fbinfo->fix.smem_len +
495				ACTIVE_VIDEO_MEM_OFFSET,
496				&info->dma_handle, GFP_KERNEL);
497
498	if (NULL == info->fb_buffer) {
499		printk(KERN_ERR DRIVER_NAME
500		       ": couldn't allocate dma buffer.\n");
501		ret = -ENOMEM;
502		goto out3;
503	}
504
505	fbinfo->screen_base = (void *)info->fb_buffer + ACTIVE_VIDEO_MEM_OFFSET;
506	fbinfo->fix.smem_start = (int)info->fb_buffer + ACTIVE_VIDEO_MEM_OFFSET;
507
508	fbinfo->fbops = &bfin_t350mcqb_fb_ops;
509
510	fbinfo->pseudo_palette = &info->pseudo_pal;
511
512	if (fb_alloc_cmap(&fbinfo->cmap, BFIN_LCD_NBR_PALETTE_ENTRIES, 0)
513	    < 0) {
514		printk(KERN_ERR DRIVER_NAME
515		       "Fail to allocate colormap (%d entries)\n",
516		       BFIN_LCD_NBR_PALETTE_ENTRIES);
517		ret = -EFAULT;
518		goto out4;
519	}
520
521	if (bfin_t350mcqb_request_ports(1)) {
522		printk(KERN_ERR DRIVER_NAME ": couldn't request gpio port.\n");
523		ret = -EFAULT;
524		goto out6;
525	}
526
527	info->irq = platform_get_irq(pdev, 0);
528	if (info->irq < 0) {
529		ret = -EINVAL;
530		goto out7;
531	}
532
533	ret = request_irq(info->irq, bfin_t350mcqb_irq_error, 0,
534			"PPI ERROR", info);
535	if (ret < 0) {
536		printk(KERN_ERR DRIVER_NAME
537		       ": unable to request PPI ERROR IRQ\n");
538		goto out7;
539	}
540
541	if (register_framebuffer(fbinfo) < 0) {
542		printk(KERN_ERR DRIVER_NAME
543		       ": unable to register framebuffer.\n");
544		ret = -EINVAL;
545		goto out8;
546	}
547#ifndef NO_BL_SUPPORT
548	memset(&props, 0, sizeof(struct backlight_properties));
549	props.type = BACKLIGHT_RAW;
550	props.max_brightness = 255;
551	bl_dev = backlight_device_register("bf52x-bl", NULL, NULL,
552					   &bfin_lq043fb_bl_ops, &props);
553	if (IS_ERR(bl_dev)) {
554		printk(KERN_ERR DRIVER_NAME
555			": unable to register backlight.\n");
556		ret = -EINVAL;
557		unregister_framebuffer(fbinfo);
558		goto out8;
559	}
560
561	lcd_dev = lcd_device_register(DRIVER_NAME, NULL, &bfin_lcd_ops);
562	lcd_dev->props.max_contrast = 255, printk(KERN_INFO "Done.\n");
563#endif
564
565	return 0;
566
567out8:
568	free_irq(info->irq, info);
569out7:
570	bfin_t350mcqb_request_ports(0);
571out6:
572	fb_dealloc_cmap(&fbinfo->cmap);
573out4:
574	dma_free_coherent(NULL, fbinfo->fix.smem_len + ACTIVE_VIDEO_MEM_OFFSET,
575			 info->fb_buffer, info->dma_handle);
576out3:
577	framebuffer_release(fbinfo);
578out2:
579	free_dma(CH_PPI);
580out1:
581
582	return ret;
583}
584
585static int bfin_t350mcqb_remove(struct platform_device *pdev)
586{
587
588	struct fb_info *fbinfo = platform_get_drvdata(pdev);
589	struct bfin_t350mcqbfb_info *info = fbinfo->par;
590
591	unregister_framebuffer(fbinfo);
592
593	free_dma(CH_PPI);
594	free_irq(info->irq, info);
595
596	if (info->fb_buffer != NULL)
597		dma_free_coherent(NULL, fbinfo->fix.smem_len +
598			ACTIVE_VIDEO_MEM_OFFSET, info->fb_buffer,
599			info->dma_handle);
600
601	fb_dealloc_cmap(&fbinfo->cmap);
602
603#ifndef NO_BL_SUPPORT
604	lcd_device_unregister(lcd_dev);
605	backlight_device_unregister(bl_dev);
606#endif
607
608	bfin_t350mcqb_request_ports(0);
609
610	framebuffer_release(fbinfo);
611
612	printk(KERN_INFO DRIVER_NAME ": Unregister LCD driver.\n");
613
614	return 0;
615}
616
617#ifdef CONFIG_PM
618static int bfin_t350mcqb_suspend(struct platform_device *pdev, pm_message_t state)
619{
620	struct fb_info *fbinfo = platform_get_drvdata(pdev);
621	struct bfin_t350mcqbfb_info *fbi = fbinfo->par;
622
623	if (fbi->lq043_open_cnt) {
624		bfin_t350mcqb_disable_ppi();
625		disable_dma(CH_PPI);
626		bfin_t350mcqb_stop_timers();
627		bfin_write_PPI_STATUS(-1);
628	}
629
630
631	return 0;
632}
633
634static int bfin_t350mcqb_resume(struct platform_device *pdev)
635{
636	struct fb_info *fbinfo = platform_get_drvdata(pdev);
637	struct bfin_t350mcqbfb_info *fbi = fbinfo->par;
638
639	if (fbi->lq043_open_cnt) {
640		bfin_t350mcqb_config_dma(fbi);
641		bfin_t350mcqb_config_ppi(fbi);
642		bfin_t350mcqb_init_timers();
643
644		/* start dma */
645		enable_dma(CH_PPI);
646		bfin_t350mcqb_enable_ppi();
647		bfin_t350mcqb_start_timers();
648	}
649
650	return 0;
651}
652#else
653#define bfin_t350mcqb_suspend	NULL
654#define bfin_t350mcqb_resume	NULL
655#endif
656
657static struct platform_driver bfin_t350mcqb_driver = {
658	.probe = bfin_t350mcqb_probe,
659	.remove = bfin_t350mcqb_remove,
660	.suspend = bfin_t350mcqb_suspend,
661	.resume = bfin_t350mcqb_resume,
662	.driver = {
663		   .name = DRIVER_NAME,
664		   },
665};
666module_platform_driver(bfin_t350mcqb_driver);
667
668MODULE_DESCRIPTION("Blackfin TFT LCD Driver");
669MODULE_LICENSE("GPL");
670