blob: f1d88ac986da4be11379f63c437c7e5cebdc54f9 [file] [log] [blame]
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001/*
2 * Copyright (C) 2008-2009 MontaVista Software Inc.
3 * Copyright (C) 2008-2009 Texas Instruments Inc
4 *
5 * Based on the LCD driver for TI Avalanche processors written by
6 * Ajay Singh and Shalom Hai.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option)any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/fb.h>
25#include <linux/dma-mapping.h>
26#include <linux/device.h>
27#include <linux/platform_device.h>
28#include <linux/uaccess.h>
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +053029#include <linux/pm_runtime.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070030#include <linux/interrupt.h>
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +053031#include <linux/wait.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070032#include <linux/clk.h>
Chaithrika U Se04e5482009-12-15 16:46:29 -080033#include <linux/cpufreq.h>
Chaithrika U S1d3c6c72009-12-15 16:46:39 -080034#include <linux/console.h>
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +053035#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Florian Tobias Schandinata0239072012-07-29 16:47:40 +000037#include <linux/delay.h>
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +053038#include <linux/lcm.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070039#include <video/da8xx-fb.h>
Manjunathappa, Prakash12fa8352012-02-09 11:54:06 +053040#include <asm/div64.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070041
42#define DRIVER_NAME "da8xx_lcdc"
43
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053044#define LCD_VERSION_1 1
45#define LCD_VERSION_2 2
46
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070047/* LCD Status Register */
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070048#define LCD_END_OF_FRAME1 BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070049#define LCD_END_OF_FRAME0 BIT(8)
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070050#define LCD_PL_LOAD_DONE BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070051#define LCD_FIFO_UNDERFLOW BIT(5)
52#define LCD_SYNC_LOST BIT(2)
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +053053#define LCD_FRAME_DONE BIT(0)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070054
55/* LCD DMA Control Register */
56#define LCD_DMA_BURST_SIZE(x) ((x) << 4)
57#define LCD_DMA_BURST_1 0x0
58#define LCD_DMA_BURST_2 0x1
59#define LCD_DMA_BURST_4 0x2
60#define LCD_DMA_BURST_8 0x3
61#define LCD_DMA_BURST_16 0x4
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053062#define LCD_V1_END_OF_FRAME_INT_ENA BIT(2)
63#define LCD_V2_END_OF_FRAME0_INT_ENA BIT(8)
64#define LCD_V2_END_OF_FRAME1_INT_ENA BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070065#define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
66
67/* LCD Control Register */
68#define LCD_CLK_DIVISOR(x) ((x) << 8)
69#define LCD_RASTER_MODE 0x01
70
71/* LCD Raster Control Register */
72#define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
73#define PALETTE_AND_DATA 0x00
74#define PALETTE_ONLY 0x01
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070075#define DATA_ONLY 0x02
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070076
77#define LCD_MONO_8BIT_MODE BIT(9)
78#define LCD_RASTER_ORDER BIT(8)
79#define LCD_TFT_MODE BIT(7)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053080#define LCD_V1_UNDERFLOW_INT_ENA BIT(6)
81#define LCD_V2_UNDERFLOW_INT_ENA BIT(5)
82#define LCD_V1_PL_INT_ENA BIT(4)
83#define LCD_V2_PL_INT_ENA BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070084#define LCD_MONOCHROME_MODE BIT(1)
85#define LCD_RASTER_ENABLE BIT(0)
86#define LCD_TFT_ALT_ENABLE BIT(23)
87#define LCD_STN_565_ENABLE BIT(24)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053088#define LCD_V2_DMA_CLK_EN BIT(2)
89#define LCD_V2_LIDD_CLK_EN BIT(1)
90#define LCD_V2_CORE_CLK_EN BIT(0)
91#define LCD_V2_LPP_B10 26
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +053092#define LCD_V2_TFT_24BPP_MODE BIT(25)
93#define LCD_V2_TFT_24BPP_UNPACK BIT(26)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070094
95/* LCD Raster Timing 2 Register */
96#define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
97#define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
98#define LCD_SYNC_CTRL BIT(25)
99#define LCD_SYNC_EDGE BIT(24)
100#define LCD_INVERT_PIXEL_CLOCK BIT(22)
101#define LCD_INVERT_LINE_CLOCK BIT(21)
102#define LCD_INVERT_FRAME_CLOCK BIT(20)
103
104/* LCD Block */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530105#define LCD_PID_REG 0x0
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700106#define LCD_CTRL_REG 0x4
107#define LCD_STAT_REG 0x8
108#define LCD_RASTER_CTRL_REG 0x28
109#define LCD_RASTER_TIMING_0_REG 0x2C
110#define LCD_RASTER_TIMING_1_REG 0x30
111#define LCD_RASTER_TIMING_2_REG 0x34
112#define LCD_DMA_CTRL_REG 0x40
113#define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
114#define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700115#define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
116#define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
117
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530118/* Interrupt Registers available only in Version 2 */
119#define LCD_RAW_STAT_REG 0x58
120#define LCD_MASKED_STAT_REG 0x5c
121#define LCD_INT_ENABLE_SET_REG 0x60
122#define LCD_INT_ENABLE_CLR_REG 0x64
123#define LCD_END_OF_INT_IND_REG 0x68
124
125/* Clock registers available only on Version 2 */
126#define LCD_CLK_ENABLE_REG 0x6c
127#define LCD_CLK_RESET_REG 0x70
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530128#define LCD_CLK_MAIN_RESET BIT(3)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530129
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700130#define LCD_NUM_BUFFERS 2
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700131
132#define WSI_TIMEOUT 50
133#define PALETTE_SIZE 256
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700134
Arnd Bergmann34aef6e2012-09-14 20:33:43 +0000135static void __iomem *da8xx_fb_reg_base;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700136static struct resource *lcdc_regs;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530137static unsigned int lcd_revision;
138static irq_handler_t lcdc_irq_handler;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530139static wait_queue_head_t frame_done_wq;
140static int frame_done_flag;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700141
142static inline unsigned int lcdc_read(unsigned int addr)
143{
144 return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
145}
146
147static inline void lcdc_write(unsigned int val, unsigned int addr)
148{
149 __raw_writel(val, da8xx_fb_reg_base + (addr));
150}
151
152struct da8xx_fb_par {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700153 resource_size_t p_palette_base;
154 unsigned char *v_palette_base;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700155 dma_addr_t vram_phys;
156 unsigned long vram_size;
157 void *vram_virt;
158 unsigned int dma_start;
159 unsigned int dma_end;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700160 struct clk *lcdc_clk;
161 int irq;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700162 unsigned int palette_sz;
Chaithrika U S8097b172009-12-15 16:46:29 -0800163 unsigned int pxl_clk;
Chaithrika U S36113802009-12-15 16:46:38 -0800164 int blank;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700165 wait_queue_head_t vsync_wait;
166 int vsync_flag;
167 int vsync_timeout;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530168 spinlock_t lock_for_chan_update;
169
170 /*
171 * LCDC has 2 ping pong DMA channels, channel 0
172 * and channel 1.
173 */
174 unsigned int which_dma_channel_done;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800175#ifdef CONFIG_CPU_FREQ
176 struct notifier_block freq_transition;
177#endif
Afzal Mohammed44f627a2013-08-05 17:02:25 -0500178 unsigned int lcd_fck_rate;
Chaithrika U S36113802009-12-15 16:46:38 -0800179 void (*panel_power_ctrl)(int);
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530180 u32 pseudo_palette[16];
Afzal Mohammedb6dbe8e2013-08-05 17:02:24 -0500181 struct fb_videomode mode;
182 struct lcd_ctrl_config cfg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700183};
184
Afzal Mohammedbe0f6db2013-08-05 17:02:23 -0500185static struct fb_var_screeninfo da8xx_fb_var;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700186
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800187static struct fb_fix_screeninfo da8xx_fb_fix = {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700188 .id = "DA8xx FB Drv",
189 .type = FB_TYPE_PACKED_PIXELS,
190 .type_aux = 0,
191 .visual = FB_VISUAL_PSEUDOCOLOR,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700192 .xpanstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700193 .ypanstep = 1,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700194 .ywrapstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700195 .accel = FB_ACCEL_NONE
196};
197
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530198static struct fb_videomode known_lcd_panels[] = {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700199 /* Sharp LCD035Q3DG01 */
200 [0] = {
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530201 .name = "Sharp_LCD035Q3DG01",
202 .xres = 320,
203 .yres = 240,
204 .pixclock = 4608000,
205 .left_margin = 6,
206 .right_margin = 8,
207 .upper_margin = 2,
208 .lower_margin = 2,
209 .hsync_len = 0,
210 .vsync_len = 0,
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530211 .sync = FB_SYNC_CLK_INVERT |
212 FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700213 },
214 /* Sharp LK043T1DG01 */
215 [1] = {
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530216 .name = "Sharp_LK043T1DG01",
217 .xres = 480,
218 .yres = 272,
219 .pixclock = 7833600,
220 .left_margin = 2,
221 .right_margin = 2,
222 .upper_margin = 2,
223 .lower_margin = 2,
224 .hsync_len = 41,
225 .vsync_len = 10,
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530226 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530227 .flag = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700228 },
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100229 [2] = {
230 /* Hitachi SP10Q010 */
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530231 .name = "SP10Q010",
232 .xres = 320,
233 .yres = 240,
234 .pixclock = 7833600,
235 .left_margin = 10,
236 .right_margin = 10,
237 .upper_margin = 10,
238 .lower_margin = 10,
239 .hsync_len = 10,
240 .vsync_len = 10,
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530241 .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530242 .flag = 0,
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100243 },
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700244};
245
Chaithrika U S36113802009-12-15 16:46:38 -0800246/* Enable the Raster Engine of the LCD Controller */
247static inline void lcd_enable_raster(void)
248{
249 u32 reg;
250
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530251 /* Put LCDC in reset for several cycles */
252 if (lcd_revision == LCD_VERSION_2)
253 /* Write 1 to reset LCDC */
254 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
255 mdelay(1);
256
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530257 /* Bring LCDC out of reset */
258 if (lcd_revision == LCD_VERSION_2)
259 lcdc_write(0, LCD_CLK_RESET_REG);
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530260 mdelay(1);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530261
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530262 /* Above reset sequence doesnot reset register context */
Chaithrika U S36113802009-12-15 16:46:38 -0800263 reg = lcdc_read(LCD_RASTER_CTRL_REG);
264 if (!(reg & LCD_RASTER_ENABLE))
265 lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
266}
267
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700268/* Disable the Raster Engine of the LCD Controller */
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530269static inline void lcd_disable_raster(bool wait_for_frame_done)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700270{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700271 u32 reg;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530272 int ret;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700273
274 reg = lcdc_read(LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700275 if (reg & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700276 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530277 else
278 /* return if already disabled */
279 return;
280
281 if ((wait_for_frame_done == true) && (lcd_revision == LCD_VERSION_2)) {
282 frame_done_flag = 0;
283 ret = wait_event_interruptible_timeout(frame_done_wq,
284 frame_done_flag != 0,
285 msecs_to_jiffies(50));
286 if (ret == 0)
287 pr_err("LCD Controller timed out\n");
288 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700289}
290
291static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
292{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700293 u32 start;
294 u32 end;
295 u32 reg_ras;
296 u32 reg_dma;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530297 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700298
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700299 /* init reg to clear PLM (loading mode) fields */
300 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
301 reg_ras &= ~(3 << 20);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700302
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700303 reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700304
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700305 if (load_mode == LOAD_DATA) {
306 start = par->dma_start;
307 end = par->dma_end;
308
309 reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530310 if (lcd_revision == LCD_VERSION_1) {
311 reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
312 } else {
313 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
314 LCD_V2_END_OF_FRAME0_INT_ENA |
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530315 LCD_V2_END_OF_FRAME1_INT_ENA |
316 LCD_FRAME_DONE;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530317 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
318 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700319 reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
320
321 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
322 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
323 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
324 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
325 } else if (load_mode == LOAD_PALETTE) {
326 start = par->p_palette_base;
327 end = start + par->palette_sz - 1;
328
329 reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530330
331 if (lcd_revision == LCD_VERSION_1) {
332 reg_ras |= LCD_V1_PL_INT_ENA;
333 } else {
334 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
335 LCD_V2_PL_INT_ENA;
336 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
337 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700338
339 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
340 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
341 }
342
343 lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
344 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
345
346 /*
347 * The Raster enable bit must be set after all other control fields are
348 * set.
349 */
350 lcd_enable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700351}
352
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530353/* Configure the Burst Size and fifo threhold of DMA */
354static int lcd_cfg_dma(int burst_size, int fifo_th)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700355{
356 u32 reg;
357
358 reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
359 switch (burst_size) {
360 case 1:
361 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
362 break;
363 case 2:
364 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
365 break;
366 case 4:
367 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
368 break;
369 case 8:
370 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
371 break;
372 case 16:
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530373 default:
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700374 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
375 break;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700376 }
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530377
378 reg |= (fifo_th << 8);
379
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700380 lcdc_write(reg, LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700381
382 return 0;
383}
384
385static void lcd_cfg_ac_bias(int period, int transitions_per_int)
386{
387 u32 reg;
388
389 /* Set the AC Bias Period and Number of Transisitons per Interrupt */
390 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
391 reg |= LCD_AC_BIAS_FREQUENCY(period) |
392 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
393 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
394}
395
396static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
397 int front_porch)
398{
399 u32 reg;
400
401 reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
402 reg |= ((back_porch & 0xff) << 24)
403 | ((front_porch & 0xff) << 16)
404 | ((pulse_width & 0x3f) << 10);
405 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
406}
407
408static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
409 int front_porch)
410{
411 u32 reg;
412
413 reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
414 reg |= ((back_porch & 0xff) << 24)
415 | ((front_porch & 0xff) << 16)
416 | ((pulse_width & 0x3f) << 10);
417 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
418}
419
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530420static int lcd_cfg_display(const struct lcd_ctrl_config *cfg,
421 struct fb_videomode *panel)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700422{
423 u32 reg;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530424 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700425
426 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
427 LCD_MONO_8BIT_MODE |
428 LCD_MONOCHROME_MODE);
429
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530430 switch (cfg->panel_shade) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700431 case MONOCHROME:
432 reg |= LCD_MONOCHROME_MODE;
433 if (cfg->mono_8bit_mode)
434 reg |= LCD_MONO_8BIT_MODE;
435 break;
436 case COLOR_ACTIVE:
437 reg |= LCD_TFT_MODE;
438 if (cfg->tft_alt_mode)
439 reg |= LCD_TFT_ALT_ENABLE;
440 break;
441
442 case COLOR_PASSIVE:
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530443 /* AC bias applicable only for Pasive panels */
444 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
445 if (cfg->bpp == 12 && cfg->stn_565_mode)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700446 reg |= LCD_STN_565_ENABLE;
447 break;
448
449 default:
450 return -EINVAL;
451 }
452
453 /* enable additional interrupts here */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530454 if (lcd_revision == LCD_VERSION_1) {
455 reg |= LCD_V1_UNDERFLOW_INT_ENA;
456 } else {
457 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
458 LCD_V2_UNDERFLOW_INT_ENA;
459 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
460 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700461
462 lcdc_write(reg, LCD_RASTER_CTRL_REG);
463
464 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
465
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530466 reg |= LCD_SYNC_CTRL;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700467
468 if (cfg->sync_edge)
469 reg |= LCD_SYNC_EDGE;
470 else
471 reg &= ~LCD_SYNC_EDGE;
472
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530473 if (panel->sync & FB_SYNC_HOR_HIGH_ACT)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700474 reg |= LCD_INVERT_LINE_CLOCK;
475 else
476 reg &= ~LCD_INVERT_LINE_CLOCK;
477
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530478 if (panel->sync & FB_SYNC_VERT_HIGH_ACT)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700479 reg |= LCD_INVERT_FRAME_CLOCK;
480 else
481 reg &= ~LCD_INVERT_FRAME_CLOCK;
482
483 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
484
485 return 0;
486}
487
488static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
489 u32 bpp, u32 raster_order)
490{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700491 u32 reg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700492
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530493 if (bpp > 16 && lcd_revision == LCD_VERSION_1)
494 return -EINVAL;
495
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700496 /* Set the Panel Width */
497 /* Pixels per line = (PPL + 1)*16 */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530498 if (lcd_revision == LCD_VERSION_1) {
499 /*
500 * 0x3F in bits 4..9 gives max horizontal resolution = 1024
501 * pixels.
502 */
503 width &= 0x3f0;
504 } else {
505 /*
506 * 0x7F in bits 4..10 gives max horizontal resolution = 2048
507 * pixels.
508 */
509 width &= 0x7f0;
510 }
511
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700512 reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
513 reg &= 0xfffffc00;
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530514 if (lcd_revision == LCD_VERSION_1) {
515 reg |= ((width >> 4) - 1) << 4;
516 } else {
517 width = (width >> 4) - 1;
518 reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
519 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700520 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
521
522 /* Set the Panel Height */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530523 /* Set bits 9:0 of Lines Per Pixel */
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700524 reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
525 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
526 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
527
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530528 /* Set bit 10 of Lines Per Pixel */
529 if (lcd_revision == LCD_VERSION_2) {
530 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
531 reg |= ((height - 1) & 0x400) << 16;
532 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
533 }
534
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700535 /* Set the Raster Order of the Frame Buffer */
536 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
537 if (raster_order)
538 reg |= LCD_RASTER_ORDER;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530539
540 par->palette_sz = 16 * 2;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700541
542 switch (bpp) {
543 case 1:
544 case 2:
545 case 4:
546 case 16:
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530547 break;
548 case 24:
549 reg |= LCD_V2_TFT_24BPP_MODE;
550 case 32:
551 reg |= LCD_V2_TFT_24BPP_UNPACK;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700552 break;
553
554 case 8:
555 par->palette_sz = 256 * 2;
556 break;
557
558 default:
559 return -EINVAL;
560 }
561
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530562 lcdc_write(reg, LCD_RASTER_CTRL_REG);
563
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700564 return 0;
565}
566
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530567#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700568static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
569 unsigned blue, unsigned transp,
570 struct fb_info *info)
571{
572 struct da8xx_fb_par *par = info->par;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700573 unsigned short *palette = (unsigned short *) par->v_palette_base;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700574 u_short pal;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700575 int update_hw = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700576
577 if (regno > 255)
578 return 1;
579
580 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
581 return 1;
582
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530583 if (info->var.bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
584 return -EINVAL;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100585
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530586 switch (info->fix.visual) {
587 case FB_VISUAL_TRUECOLOR:
588 red = CNVT_TOHW(red, info->var.red.length);
589 green = CNVT_TOHW(green, info->var.green.length);
590 blue = CNVT_TOHW(blue, info->var.blue.length);
591 break;
592 case FB_VISUAL_PSEUDOCOLOR:
593 switch (info->var.bits_per_pixel) {
594 case 4:
595 if (regno > 15)
596 return -EINVAL;
597
598 if (info->var.grayscale) {
599 pal = regno;
600 } else {
601 red >>= 4;
602 green >>= 8;
603 blue >>= 12;
604
605 pal = red & 0x0f00;
606 pal |= green & 0x00f0;
607 pal |= blue & 0x000f;
608 }
609 if (regno == 0)
610 pal |= 0x2000;
611 palette[regno] = pal;
612 break;
613
614 case 8:
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100615 red >>= 4;
616 green >>= 8;
617 blue >>= 12;
618
619 pal = (red & 0x0f00);
620 pal |= (green & 0x00f0);
621 pal |= (blue & 0x000f);
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530622
623 if (palette[regno] != pal) {
624 update_hw = 1;
625 palette[regno] = pal;
626 }
627 break;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100628 }
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530629 break;
630 }
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100631
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530632 /* Truecolor has hardware independent palette */
633 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
634 u32 v;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700635
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530636 if (regno > 15)
637 return -EINVAL;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700638
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530639 v = (red << info->var.red.offset) |
640 (green << info->var.green.offset) |
641 (blue << info->var.blue.offset);
642
643 switch (info->var.bits_per_pixel) {
644 case 16:
645 ((u16 *) (info->pseudo_palette))[regno] = v;
646 break;
647 case 24:
648 case 32:
649 ((u32 *) (info->pseudo_palette))[regno] = v;
650 break;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700651 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700652 if (palette[0] != 0x4000) {
653 update_hw = 1;
654 palette[0] = 0x4000;
655 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700656 }
657
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700658 /* Update the palette in the h/w as needed. */
659 if (update_hw)
660 lcd_blit(LOAD_PALETTE, par);
661
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700662 return 0;
663}
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530664#undef CNVT_TOHW
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700665
Afzal Mohammed39c87d42013-08-05 17:02:21 -0500666static void da8xx_fb_lcd_reset(void)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700667{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700668 /* Disable the Raster if previously Enabled */
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530669 lcd_disable_raster(false);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700670
671 /* DMA has to be disabled */
672 lcdc_write(0, LCD_DMA_CTRL_REG);
673 lcdc_write(0, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530674
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530675 if (lcd_revision == LCD_VERSION_2) {
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530676 lcdc_write(0, LCD_INT_ENABLE_SET_REG);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530677 /* Write 1 to reset */
678 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
679 lcdc_write(0, LCD_CLK_RESET_REG);
680 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700681}
682
Chaithrika U S8097b172009-12-15 16:46:29 -0800683static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
684{
685 unsigned int lcd_clk, div;
686
687 lcd_clk = clk_get_rate(par->lcdc_clk);
688 div = lcd_clk / par->pxl_clk;
689
690 /* Configure the LCD clock divisor. */
691 lcdc_write(LCD_CLK_DIVISOR(div) |
692 (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530693
694 if (lcd_revision == LCD_VERSION_2)
695 lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
696 LCD_V2_CORE_CLK_EN, LCD_CLK_ENABLE_REG);
697
Chaithrika U S8097b172009-12-15 16:46:29 -0800698}
699
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700700static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530701 struct fb_videomode *panel)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700702{
703 u32 bpp;
704 int ret = 0;
705
Afzal Mohammed39c87d42013-08-05 17:02:21 -0500706 da8xx_fb_lcd_reset();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700707
Chaithrika U S8097b172009-12-15 16:46:29 -0800708 /* Calculate the divider */
709 lcd_calc_clk_divider(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700710
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530711 if (panel->sync & FB_SYNC_CLK_INVERT)
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700712 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
713 LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
714 else
715 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
716 ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
717
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530718 /* Configure the DMA burst size and fifo threshold. */
719 ret = lcd_cfg_dma(cfg->dma_burst_sz, cfg->fifo_th);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700720 if (ret < 0)
721 return ret;
722
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700723 /* Configure the vertical and horizontal sync properties. */
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530724 lcd_cfg_vertical_sync(panel->lower_margin, panel->vsync_len,
725 panel->upper_margin);
726 lcd_cfg_horizontal_sync(panel->right_margin, panel->hsync_len,
727 panel->left_margin);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700728
729 /* Configure for disply */
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530730 ret = lcd_cfg_display(cfg, panel);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700731 if (ret < 0)
732 return ret;
733
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530734 bpp = cfg->bpp;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700735
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700736 if (bpp == 12)
737 bpp = 16;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530738 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->xres,
739 (unsigned int)panel->yres, bpp,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700740 cfg->raster_order);
741 if (ret < 0)
742 return ret;
743
744 /* Configure FDD */
745 lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
746 (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
747
748 return 0;
749}
750
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530751/* IRQ handler for version 2 of LCDC */
752static irqreturn_t lcdc_irq_handler_rev02(int irq, void *arg)
753{
754 struct da8xx_fb_par *par = arg;
755 u32 stat = lcdc_read(LCD_MASKED_STAT_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530756
757 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530758 lcd_disable_raster(false);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530759 lcdc_write(stat, LCD_MASKED_STAT_REG);
760 lcd_enable_raster();
761 } else if (stat & LCD_PL_LOAD_DONE) {
762 /*
763 * Must disable raster before changing state of any control bit.
764 * And also must be disabled before clearing the PL loading
765 * interrupt via the following write to the status register. If
766 * this is done after then one gets multiple PL done interrupts.
767 */
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530768 lcd_disable_raster(false);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530769
770 lcdc_write(stat, LCD_MASKED_STAT_REG);
771
Manjunathappa, Prakash8a81dcc2012-07-18 20:51:11 +0530772 /* Disable PL completion interrupt */
773 lcdc_write(LCD_V2_PL_INT_ENA, LCD_INT_ENABLE_CLR_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530774
775 /* Setup and start data loading mode */
776 lcd_blit(LOAD_DATA, par);
777 } else {
778 lcdc_write(stat, LCD_MASKED_STAT_REG);
779
780 if (stat & LCD_END_OF_FRAME0) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530781 par->which_dma_channel_done = 0;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530782 lcdc_write(par->dma_start,
783 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
784 lcdc_write(par->dma_end,
785 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
786 par->vsync_flag = 1;
787 wake_up_interruptible(&par->vsync_wait);
788 }
789
790 if (stat & LCD_END_OF_FRAME1) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530791 par->which_dma_channel_done = 1;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530792 lcdc_write(par->dma_start,
793 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
794 lcdc_write(par->dma_end,
795 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
796 par->vsync_flag = 1;
797 wake_up_interruptible(&par->vsync_wait);
798 }
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530799
800 /* Set only when controller is disabled and at the end of
801 * active frame
802 */
803 if (stat & BIT(0)) {
804 frame_done_flag = 1;
805 wake_up_interruptible(&frame_done_wq);
806 }
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530807 }
808
809 lcdc_write(0, LCD_END_OF_INT_IND_REG);
810 return IRQ_HANDLED;
811}
812
813/* IRQ handler for version 1 LCDC */
814static irqreturn_t lcdc_irq_handler_rev01(int irq, void *arg)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700815{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700816 struct da8xx_fb_par *par = arg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700817 u32 stat = lcdc_read(LCD_STAT_REG);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700818 u32 reg_ras;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700819
820 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530821 lcd_disable_raster(false);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700822 lcdc_write(stat, LCD_STAT_REG);
Chaithrika U S36113802009-12-15 16:46:38 -0800823 lcd_enable_raster();
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700824 } else if (stat & LCD_PL_LOAD_DONE) {
825 /*
826 * Must disable raster before changing state of any control bit.
827 * And also must be disabled before clearing the PL loading
828 * interrupt via the following write to the status register. If
829 * this is done after then one gets multiple PL done interrupts.
830 */
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530831 lcd_disable_raster(false);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700832
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700833 lcdc_write(stat, LCD_STAT_REG);
834
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700835 /* Disable PL completion inerrupt */
836 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530837 reg_ras &= ~LCD_V1_PL_INT_ENA;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700838 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
839
840 /* Setup and start data loading mode */
841 lcd_blit(LOAD_DATA, par);
842 } else {
843 lcdc_write(stat, LCD_STAT_REG);
844
845 if (stat & LCD_END_OF_FRAME0) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530846 par->which_dma_channel_done = 0;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700847 lcdc_write(par->dma_start,
848 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
849 lcdc_write(par->dma_end,
850 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
851 par->vsync_flag = 1;
852 wake_up_interruptible(&par->vsync_wait);
853 }
854
855 if (stat & LCD_END_OF_FRAME1) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530856 par->which_dma_channel_done = 1;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700857 lcdc_write(par->dma_start,
858 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
859 lcdc_write(par->dma_end,
860 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
861 par->vsync_flag = 1;
862 wake_up_interruptible(&par->vsync_wait);
863 }
864 }
865
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700866 return IRQ_HANDLED;
867}
868
869static int fb_check_var(struct fb_var_screeninfo *var,
870 struct fb_info *info)
871{
872 int err = 0;
Afzal Mohammed87dac712013-08-05 17:02:20 -0500873 struct da8xx_fb_par *par = info->par;
874 int bpp = var->bits_per_pixel >> 3;
875 unsigned long line_size = var->xres_virtual * bpp;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700876
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530877 if (var->bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
878 return -EINVAL;
879
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700880 switch (var->bits_per_pixel) {
881 case 1:
882 case 8:
883 var->red.offset = 0;
884 var->red.length = 8;
885 var->green.offset = 0;
886 var->green.length = 8;
887 var->blue.offset = 0;
888 var->blue.length = 8;
889 var->transp.offset = 0;
890 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100891 var->nonstd = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700892 break;
893 case 4:
894 var->red.offset = 0;
895 var->red.length = 4;
896 var->green.offset = 0;
897 var->green.length = 4;
898 var->blue.offset = 0;
899 var->blue.length = 4;
900 var->transp.offset = 0;
901 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100902 var->nonstd = FB_NONSTD_REV_PIX_IN_B;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700903 break;
904 case 16: /* RGB 565 */
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800905 var->red.offset = 11;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700906 var->red.length = 5;
907 var->green.offset = 5;
908 var->green.length = 6;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800909 var->blue.offset = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700910 var->blue.length = 5;
911 var->transp.offset = 0;
912 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100913 var->nonstd = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700914 break;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530915 case 24:
916 var->red.offset = 16;
917 var->red.length = 8;
918 var->green.offset = 8;
919 var->green.length = 8;
920 var->blue.offset = 0;
921 var->blue.length = 8;
922 var->nonstd = 0;
923 break;
924 case 32:
925 var->transp.offset = 24;
926 var->transp.length = 8;
927 var->red.offset = 16;
928 var->red.length = 8;
929 var->green.offset = 8;
930 var->green.length = 8;
931 var->blue.offset = 0;
932 var->blue.length = 8;
933 var->nonstd = 0;
934 break;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700935 default:
936 err = -EINVAL;
937 }
938
939 var->red.msb_right = 0;
940 var->green.msb_right = 0;
941 var->blue.msb_right = 0;
942 var->transp.msb_right = 0;
Afzal Mohammed87dac712013-08-05 17:02:20 -0500943
944 if (line_size * var->yres_virtual > par->vram_size)
945 var->yres_virtual = par->vram_size / line_size;
946
947 if (var->yres > var->yres_virtual)
948 var->yres = var->yres_virtual;
949
950 if (var->xres > var->xres_virtual)
951 var->xres = var->xres_virtual;
952
953 if (var->xres + var->xoffset > var->xres_virtual)
954 var->xoffset = var->xres_virtual - var->xres;
955 if (var->yres + var->yoffset > var->yres_virtual)
956 var->yoffset = var->yres_virtual - var->yres;
957
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700958 return err;
959}
960
Chaithrika U Se04e5482009-12-15 16:46:29 -0800961#ifdef CONFIG_CPU_FREQ
962static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
963 unsigned long val, void *data)
964{
965 struct da8xx_fb_par *par;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800966
967 par = container_of(nb, struct da8xx_fb_par, freq_transition);
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +0530968 if (val == CPUFREQ_POSTCHANGE) {
969 if (par->lcd_fck_rate != clk_get_rate(par->lcdc_clk)) {
970 par->lcd_fck_rate = clk_get_rate(par->lcdc_clk);
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530971 lcd_disable_raster(true);
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +0530972 lcd_calc_clk_divider(par);
Manjunathappa, Prakash67900812012-08-31 19:48:59 +0530973 if (par->blank == FB_BLANK_UNBLANK)
974 lcd_enable_raster();
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +0530975 }
Chaithrika U Se04e5482009-12-15 16:46:29 -0800976 }
977
978 return 0;
979}
980
981static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
982{
983 par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
984
985 return cpufreq_register_notifier(&par->freq_transition,
986 CPUFREQ_TRANSITION_NOTIFIER);
987}
988
989static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
990{
991 cpufreq_unregister_notifier(&par->freq_transition,
992 CPUFREQ_TRANSITION_NOTIFIER);
993}
994#endif
995
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800996static int fb_remove(struct platform_device *dev)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700997{
998 struct fb_info *info = dev_get_drvdata(&dev->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700999
1000 if (info) {
1001 struct da8xx_fb_par *par = info->par;
1002
Chaithrika U Se04e5482009-12-15 16:46:29 -08001003#ifdef CONFIG_CPU_FREQ
1004 lcd_da8xx_cpufreq_deregister(par);
1005#endif
Chaithrika U S36113802009-12-15 16:46:38 -08001006 if (par->panel_power_ctrl)
1007 par->panel_power_ctrl(0);
1008
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301009 lcd_disable_raster(true);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001010 lcdc_write(0, LCD_RASTER_CTRL_REG);
1011
1012 /* disable DMA */
1013 lcdc_write(0, LCD_DMA_CTRL_REG);
1014
1015 unregister_framebuffer(info);
1016 fb_dealloc_cmap(&info->cmap);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001017 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
1018 par->p_palette_base);
1019 dma_free_coherent(NULL, par->vram_size, par->vram_virt,
1020 par->vram_phys);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001021 free_irq(par->irq, par);
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301022 pm_runtime_put_sync(&dev->dev);
1023 pm_runtime_disable(&dev->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001024 framebuffer_release(info);
Arnd Bergmann34aef6e2012-09-14 20:33:43 +00001025 iounmap(da8xx_fb_reg_base);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001026 release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
1027
1028 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001029 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001030}
1031
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001032/*
1033 * Function to wait for vertical sync which for this LCD peripheral
1034 * translates into waiting for the current raster frame to complete.
1035 */
1036static int fb_wait_for_vsync(struct fb_info *info)
1037{
1038 struct da8xx_fb_par *par = info->par;
1039 int ret;
1040
1041 /*
1042 * Set flag to 0 and wait for isr to set to 1. It would seem there is a
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001043 * race condition here where the ISR could have occurred just before or
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001044 * just after this set. But since we are just coarsely waiting for
1045 * a frame to complete then that's OK. i.e. if the frame completed
1046 * just before this code executed then we have to wait another full
1047 * frame time but there is no way to avoid such a situation. On the
1048 * other hand if the frame completed just after then we don't need
1049 * to wait long at all. Either way we are guaranteed to return to the
1050 * user immediately after a frame completion which is all that is
1051 * required.
1052 */
1053 par->vsync_flag = 0;
1054 ret = wait_event_interruptible_timeout(par->vsync_wait,
1055 par->vsync_flag != 0,
1056 par->vsync_timeout);
1057 if (ret < 0)
1058 return ret;
1059 if (ret == 0)
1060 return -ETIMEDOUT;
1061
1062 return 0;
1063}
1064
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001065static int fb_ioctl(struct fb_info *info, unsigned int cmd,
1066 unsigned long arg)
1067{
1068 struct lcd_sync_arg sync_arg;
1069
1070 switch (cmd) {
1071 case FBIOGET_CONTRAST:
1072 case FBIOPUT_CONTRAST:
1073 case FBIGET_BRIGHTNESS:
1074 case FBIPUT_BRIGHTNESS:
1075 case FBIGET_COLOR:
1076 case FBIPUT_COLOR:
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001077 return -ENOTTY;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001078 case FBIPUT_HSYNC:
1079 if (copy_from_user(&sync_arg, (char *)arg,
1080 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001081 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001082 lcd_cfg_horizontal_sync(sync_arg.back_porch,
1083 sync_arg.pulse_width,
1084 sync_arg.front_porch);
1085 break;
1086 case FBIPUT_VSYNC:
1087 if (copy_from_user(&sync_arg, (char *)arg,
1088 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001089 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001090 lcd_cfg_vertical_sync(sync_arg.back_porch,
1091 sync_arg.pulse_width,
1092 sync_arg.front_porch);
1093 break;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001094 case FBIO_WAITFORVSYNC:
1095 return fb_wait_for_vsync(info);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001096 default:
1097 return -EINVAL;
1098 }
1099 return 0;
1100}
1101
Chaithrika U S312d9712009-12-15 16:46:39 -08001102static int cfb_blank(int blank, struct fb_info *info)
1103{
1104 struct da8xx_fb_par *par = info->par;
1105 int ret = 0;
1106
1107 if (par->blank == blank)
1108 return 0;
1109
1110 par->blank = blank;
1111 switch (blank) {
1112 case FB_BLANK_UNBLANK:
Manjunathappa, Prakashf7c848b2012-07-24 09:45:25 +05301113 lcd_enable_raster();
1114
Chaithrika U S312d9712009-12-15 16:46:39 -08001115 if (par->panel_power_ctrl)
1116 par->panel_power_ctrl(1);
Chaithrika U S312d9712009-12-15 16:46:39 -08001117 break;
Yegor Yefremov99a647d2012-07-06 16:01:28 +02001118 case FB_BLANK_NORMAL:
1119 case FB_BLANK_VSYNC_SUSPEND:
1120 case FB_BLANK_HSYNC_SUSPEND:
Chaithrika U S312d9712009-12-15 16:46:39 -08001121 case FB_BLANK_POWERDOWN:
1122 if (par->panel_power_ctrl)
1123 par->panel_power_ctrl(0);
1124
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301125 lcd_disable_raster(true);
Chaithrika U S312d9712009-12-15 16:46:39 -08001126 break;
1127 default:
1128 ret = -EINVAL;
1129 }
1130
1131 return ret;
1132}
1133
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001134/*
1135 * Set new x,y offsets in the virtual display for the visible area and switch
1136 * to the new mode.
1137 */
1138static int da8xx_pan_display(struct fb_var_screeninfo *var,
1139 struct fb_info *fbi)
1140{
1141 int ret = 0;
1142 struct fb_var_screeninfo new_var;
1143 struct da8xx_fb_par *par = fbi->par;
1144 struct fb_fix_screeninfo *fix = &fbi->fix;
1145 unsigned int end;
1146 unsigned int start;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301147 unsigned long irq_flags;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001148
1149 if (var->xoffset != fbi->var.xoffset ||
1150 var->yoffset != fbi->var.yoffset) {
1151 memcpy(&new_var, &fbi->var, sizeof(new_var));
1152 new_var.xoffset = var->xoffset;
1153 new_var.yoffset = var->yoffset;
1154 if (fb_check_var(&new_var, fbi))
1155 ret = -EINVAL;
1156 else {
1157 memcpy(&fbi->var, &new_var, sizeof(new_var));
1158
1159 start = fix->smem_start +
1160 new_var.yoffset * fix->line_length +
Laurent Pincharte6c4d3d2011-06-14 09:24:45 +00001161 new_var.xoffset * fbi->var.bits_per_pixel / 8;
1162 end = start + fbi->var.yres * fix->line_length - 1;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001163 par->dma_start = start;
1164 par->dma_end = end;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301165 spin_lock_irqsave(&par->lock_for_chan_update,
1166 irq_flags);
1167 if (par->which_dma_channel_done == 0) {
1168 lcdc_write(par->dma_start,
1169 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1170 lcdc_write(par->dma_end,
1171 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1172 } else if (par->which_dma_channel_done == 1) {
1173 lcdc_write(par->dma_start,
1174 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1175 lcdc_write(par->dma_end,
1176 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1177 }
1178 spin_unlock_irqrestore(&par->lock_for_chan_update,
1179 irq_flags);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001180 }
1181 }
1182
1183 return ret;
1184}
1185
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001186static struct fb_ops da8xx_fb_ops = {
1187 .owner = THIS_MODULE,
1188 .fb_check_var = fb_check_var,
1189 .fb_setcolreg = fb_setcolreg,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001190 .fb_pan_display = da8xx_pan_display,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001191 .fb_ioctl = fb_ioctl,
1192 .fb_fillrect = cfb_fillrect,
1193 .fb_copyarea = cfb_copyarea,
1194 .fb_imageblit = cfb_imageblit,
Chaithrika U S312d9712009-12-15 16:46:39 -08001195 .fb_blank = cfb_blank,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001196};
1197
Manjunathappa, Prakash12fa8352012-02-09 11:54:06 +05301198/* Calculate and return pixel clock period in pico seconds */
1199static unsigned int da8xxfb_pixel_clk_period(struct da8xx_fb_par *par)
1200{
1201 unsigned int lcd_clk, div;
1202 unsigned int configured_pix_clk;
1203 unsigned long long pix_clk_period_picosec = 1000000000000ULL;
1204
1205 lcd_clk = clk_get_rate(par->lcdc_clk);
1206 div = lcd_clk / par->pxl_clk;
1207 configured_pix_clk = (lcd_clk / div);
1208
1209 do_div(pix_clk_period_picosec, configured_pix_clk);
1210
1211 return pix_clk_period_picosec;
1212}
1213
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001214static int fb_probe(struct platform_device *device)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001215{
1216 struct da8xx_lcdc_platform_data *fb_pdata =
1217 device->dev.platform_data;
1218 struct lcd_ctrl_config *lcd_cfg;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301219 struct fb_videomode *lcdc_info;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001220 struct fb_info *da8xx_fb_info;
1221 struct clk *fb_clk = NULL;
1222 struct da8xx_fb_par *par;
1223 resource_size_t len;
1224 int ret, i;
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +05301225 unsigned long ulcm;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001226
1227 if (fb_pdata == NULL) {
1228 dev_err(&device->dev, "Can not get platform data\n");
1229 return -ENOENT;
1230 }
1231
1232 lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
1233 if (!lcdc_regs) {
1234 dev_err(&device->dev,
1235 "Can not get memory resource for LCD controller\n");
1236 return -ENOENT;
1237 }
1238
1239 len = resource_size(lcdc_regs);
1240
1241 lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
1242 if (!lcdc_regs)
1243 return -EBUSY;
1244
Arnd Bergmann34aef6e2012-09-14 20:33:43 +00001245 da8xx_fb_reg_base = ioremap(lcdc_regs->start, len);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001246 if (!da8xx_fb_reg_base) {
1247 ret = -EBUSY;
1248 goto err_request_mem;
1249 }
1250
Manjunathappa81cec3c2012-11-20 18:11:01 +05301251 fb_clk = clk_get(&device->dev, "fck");
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001252 if (IS_ERR(fb_clk)) {
1253 dev_err(&device->dev, "Can not get device clock\n");
1254 ret = -ENODEV;
1255 goto err_ioremap;
1256 }
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301257
1258 pm_runtime_enable(&device->dev);
1259 pm_runtime_get_sync(&device->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001260
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301261 /* Determine LCD IP Version */
1262 switch (lcdc_read(LCD_PID_REG)) {
1263 case 0x4C100102:
1264 lcd_revision = LCD_VERSION_1;
1265 break;
1266 case 0x4F200800:
Pantelis Antoniou8f22e8e2012-10-31 17:56:24 +02001267 case 0x4F201000:
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301268 lcd_revision = LCD_VERSION_2;
1269 break;
1270 default:
1271 dev_warn(&device->dev, "Unknown PID Reg value 0x%x, "
1272 "defaulting to LCD revision 1\n",
1273 lcdc_read(LCD_PID_REG));
1274 lcd_revision = LCD_VERSION_1;
1275 break;
1276 }
1277
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001278 for (i = 0, lcdc_info = known_lcd_panels;
1279 i < ARRAY_SIZE(known_lcd_panels);
1280 i++, lcdc_info++) {
1281 if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
1282 break;
1283 }
1284
1285 if (i == ARRAY_SIZE(known_lcd_panels)) {
1286 dev_err(&device->dev, "GLCD: No valid panel found\n");
Roel Kluindd04a6b2009-11-17 14:06:15 -08001287 ret = -ENODEV;
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301288 goto err_pm_runtime_disable;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001289 } else
1290 dev_info(&device->dev, "GLCD: Found %s panel\n",
1291 fb_pdata->type);
1292
1293 lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
1294
1295 da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
1296 &device->dev);
1297 if (!da8xx_fb_info) {
1298 dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
1299 ret = -ENOMEM;
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301300 goto err_pm_runtime_disable;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001301 }
1302
1303 par = da8xx_fb_info->par;
Chaithrika U S8097b172009-12-15 16:46:29 -08001304 par->lcdc_clk = fb_clk;
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +05301305 par->lcd_fck_rate = clk_get_rate(fb_clk);
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301306 par->pxl_clk = lcdc_info->pixclock;
Chaithrika U S36113802009-12-15 16:46:38 -08001307 if (fb_pdata->panel_power_ctrl) {
1308 par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
1309 par->panel_power_ctrl(1);
1310 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001311
Afzal Mohammedb8664582013-08-05 17:02:22 -05001312 fb_videomode_to_var(&da8xx_fb_var, lcdc_info);
Afzal Mohammedb6dbe8e2013-08-05 17:02:24 -05001313 fb_var_to_videomode(&par->mode, &da8xx_fb_var);
1314 par->cfg = *lcd_cfg;
Afzal Mohammedb8664582013-08-05 17:02:22 -05001315
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001316 if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
1317 dev_err(&device->dev, "lcd_init failed\n");
1318 ret = -EFAULT;
1319 goto err_release_fb;
1320 }
1321
1322 /* allocate frame buffer */
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301323 par->vram_size = lcdc_info->xres * lcdc_info->yres * lcd_cfg->bpp;
1324 ulcm = lcm((lcdc_info->xres * lcd_cfg->bpp)/8, PAGE_SIZE);
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +05301325 par->vram_size = roundup(par->vram_size/8, ulcm);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001326 par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001327
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001328 par->vram_virt = dma_alloc_coherent(NULL,
1329 par->vram_size,
1330 (resource_size_t *) &par->vram_phys,
1331 GFP_KERNEL | GFP_DMA);
1332 if (!par->vram_virt) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001333 dev_err(&device->dev,
1334 "GLCD: kmalloc for frame buffer failed\n");
1335 ret = -EINVAL;
1336 goto err_release_fb;
1337 }
1338
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001339 da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
1340 da8xx_fb_fix.smem_start = par->vram_phys;
1341 da8xx_fb_fix.smem_len = par->vram_size;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301342 da8xx_fb_fix.line_length = (lcdc_info->xres * lcd_cfg->bpp) / 8;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001343
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001344 par->dma_start = par->vram_phys;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301345 par->dma_end = par->dma_start + lcdc_info->yres *
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001346 da8xx_fb_fix.line_length - 1;
1347
1348 /* allocate palette buffer */
1349 par->v_palette_base = dma_alloc_coherent(NULL,
1350 PALETTE_SIZE,
1351 (resource_size_t *)
1352 &par->p_palette_base,
1353 GFP_KERNEL | GFP_DMA);
1354 if (!par->v_palette_base) {
1355 dev_err(&device->dev,
1356 "GLCD: kmalloc for palette buffer failed\n");
1357 ret = -EINVAL;
1358 goto err_release_fb_mem;
1359 }
1360 memset(par->v_palette_base, 0, PALETTE_SIZE);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001361
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001362 par->irq = platform_get_irq(device, 0);
1363 if (par->irq < 0) {
1364 ret = -ENOENT;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001365 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001366 }
1367
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001368 da8xx_fb_var.grayscale =
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +05301369 lcd_cfg->panel_shade == MONOCHROME ? 1 : 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001370 da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
Manjunathappa, Prakash12fa8352012-02-09 11:54:06 +05301371 da8xx_fb_var.pixclock = da8xxfb_pixel_clk_period(par);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001372
1373 /* Initialize fbinfo */
1374 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
1375 da8xx_fb_info->fix = da8xx_fb_fix;
1376 da8xx_fb_info->var = da8xx_fb_var;
1377 da8xx_fb_info->fbops = &da8xx_fb_ops;
1378 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -08001379 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
1380 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001381
1382 ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
1383 if (ret)
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001384 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001385 da8xx_fb_info->cmap.len = par->palette_sz;
1386
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001387 /* initialize var_screeninfo */
1388 da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
1389 fb_set_var(da8xx_fb_info, &da8xx_fb_var);
1390
1391 dev_set_drvdata(&device->dev, da8xx_fb_info);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001392
1393 /* initialize the vsync wait queue */
1394 init_waitqueue_head(&par->vsync_wait);
1395 par->vsync_timeout = HZ / 5;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301396 par->which_dma_channel_done = -1;
1397 spin_lock_init(&par->lock_for_chan_update);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001398
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001399 /* Register the Frame Buffer */
1400 if (register_framebuffer(da8xx_fb_info) < 0) {
1401 dev_err(&device->dev,
1402 "GLCD: Frame Buffer Registration Failed!\n");
1403 ret = -EINVAL;
1404 goto err_dealloc_cmap;
1405 }
1406
Chaithrika U Se04e5482009-12-15 16:46:29 -08001407#ifdef CONFIG_CPU_FREQ
1408 ret = lcd_da8xx_cpufreq_register(par);
1409 if (ret) {
1410 dev_err(&device->dev, "failed to register cpufreq\n");
1411 goto err_cpu_freq;
1412 }
1413#endif
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001414
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301415 if (lcd_revision == LCD_VERSION_1)
1416 lcdc_irq_handler = lcdc_irq_handler_rev01;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301417 else {
1418 init_waitqueue_head(&frame_done_wq);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301419 lcdc_irq_handler = lcdc_irq_handler_rev02;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301420 }
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301421
1422 ret = request_irq(par->irq, lcdc_irq_handler, 0,
1423 DRIVER_NAME, par);
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001424 if (ret)
1425 goto irq_freq;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001426 return 0;
1427
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001428irq_freq:
Chaithrika U Se04e5482009-12-15 16:46:29 -08001429#ifdef CONFIG_CPU_FREQ
axel lin360c2022011-01-20 03:50:51 +00001430 lcd_da8xx_cpufreq_deregister(par);
Chaithrika U Se04e5482009-12-15 16:46:29 -08001431err_cpu_freq:
Manjunathappa, Prakash3a844092012-02-09 10:34:38 +05301432#endif
Chaithrika U Se04e5482009-12-15 16:46:29 -08001433 unregister_framebuffer(da8xx_fb_info);
Chaithrika U Se04e5482009-12-15 16:46:29 -08001434
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001435err_dealloc_cmap:
1436 fb_dealloc_cmap(&da8xx_fb_info->cmap);
1437
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001438err_release_pl_mem:
1439 dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
1440 par->p_palette_base);
1441
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001442err_release_fb_mem:
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001443 dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001444
1445err_release_fb:
1446 framebuffer_release(da8xx_fb_info);
1447
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301448err_pm_runtime_disable:
1449 pm_runtime_put_sync(&device->dev);
1450 pm_runtime_disable(&device->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001451
1452err_ioremap:
Arnd Bergmann34aef6e2012-09-14 20:33:43 +00001453 iounmap(da8xx_fb_reg_base);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001454
1455err_request_mem:
1456 release_mem_region(lcdc_regs->start, len);
1457
1458 return ret;
1459}
1460
1461#ifdef CONFIG_PM
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301462struct lcdc_context {
1463 u32 clk_enable;
1464 u32 ctrl;
1465 u32 dma_ctrl;
1466 u32 raster_timing_0;
1467 u32 raster_timing_1;
1468 u32 raster_timing_2;
1469 u32 int_enable_set;
1470 u32 dma_frm_buf_base_addr_0;
1471 u32 dma_frm_buf_ceiling_addr_0;
1472 u32 dma_frm_buf_base_addr_1;
1473 u32 dma_frm_buf_ceiling_addr_1;
1474 u32 raster_ctrl;
1475} reg_context;
1476
1477static void lcd_context_save(void)
1478{
1479 if (lcd_revision == LCD_VERSION_2) {
1480 reg_context.clk_enable = lcdc_read(LCD_CLK_ENABLE_REG);
1481 reg_context.int_enable_set = lcdc_read(LCD_INT_ENABLE_SET_REG);
1482 }
1483
1484 reg_context.ctrl = lcdc_read(LCD_CTRL_REG);
1485 reg_context.dma_ctrl = lcdc_read(LCD_DMA_CTRL_REG);
1486 reg_context.raster_timing_0 = lcdc_read(LCD_RASTER_TIMING_0_REG);
1487 reg_context.raster_timing_1 = lcdc_read(LCD_RASTER_TIMING_1_REG);
1488 reg_context.raster_timing_2 = lcdc_read(LCD_RASTER_TIMING_2_REG);
1489 reg_context.dma_frm_buf_base_addr_0 =
1490 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1491 reg_context.dma_frm_buf_ceiling_addr_0 =
1492 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1493 reg_context.dma_frm_buf_base_addr_1 =
1494 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1495 reg_context.dma_frm_buf_ceiling_addr_1 =
1496 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1497 reg_context.raster_ctrl = lcdc_read(LCD_RASTER_CTRL_REG);
1498 return;
1499}
1500
1501static void lcd_context_restore(void)
1502{
1503 if (lcd_revision == LCD_VERSION_2) {
1504 lcdc_write(reg_context.clk_enable, LCD_CLK_ENABLE_REG);
1505 lcdc_write(reg_context.int_enable_set, LCD_INT_ENABLE_SET_REG);
1506 }
1507
1508 lcdc_write(reg_context.ctrl, LCD_CTRL_REG);
1509 lcdc_write(reg_context.dma_ctrl, LCD_DMA_CTRL_REG);
1510 lcdc_write(reg_context.raster_timing_0, LCD_RASTER_TIMING_0_REG);
1511 lcdc_write(reg_context.raster_timing_1, LCD_RASTER_TIMING_1_REG);
1512 lcdc_write(reg_context.raster_timing_2, LCD_RASTER_TIMING_2_REG);
1513 lcdc_write(reg_context.dma_frm_buf_base_addr_0,
1514 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1515 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_0,
1516 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1517 lcdc_write(reg_context.dma_frm_buf_base_addr_1,
1518 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1519 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_1,
1520 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1521 lcdc_write(reg_context.raster_ctrl, LCD_RASTER_CTRL_REG);
1522 return;
1523}
1524
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001525static int fb_suspend(struct platform_device *dev, pm_message_t state)
1526{
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001527 struct fb_info *info = platform_get_drvdata(dev);
1528 struct da8xx_fb_par *par = info->par;
1529
Torben Hohnac751ef2011-01-25 15:07:35 -08001530 console_lock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001531 if (par->panel_power_ctrl)
1532 par->panel_power_ctrl(0);
1533
1534 fb_set_suspend(info, 1);
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301535 lcd_disable_raster(true);
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301536 lcd_context_save();
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301537 pm_runtime_put_sync(&dev->dev);
Torben Hohnac751ef2011-01-25 15:07:35 -08001538 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001539
1540 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001541}
1542static int fb_resume(struct platform_device *dev)
1543{
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001544 struct fb_info *info = platform_get_drvdata(dev);
1545 struct da8xx_fb_par *par = info->par;
1546
Torben Hohnac751ef2011-01-25 15:07:35 -08001547 console_lock();
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301548 pm_runtime_get_sync(&dev->dev);
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301549 lcd_context_restore();
Manjunathappa, Prakash67900812012-08-31 19:48:59 +05301550 if (par->blank == FB_BLANK_UNBLANK) {
1551 lcd_enable_raster();
Manjunathappa, Prakashf7c848b2012-07-24 09:45:25 +05301552
Manjunathappa, Prakash67900812012-08-31 19:48:59 +05301553 if (par->panel_power_ctrl)
1554 par->panel_power_ctrl(1);
1555 }
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001556
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001557 fb_set_suspend(info, 0);
Torben Hohnac751ef2011-01-25 15:07:35 -08001558 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001559
1560 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001561}
1562#else
1563#define fb_suspend NULL
1564#define fb_resume NULL
1565#endif
1566
1567static struct platform_driver da8xx_fb_driver = {
1568 .probe = fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001569 .remove = fb_remove,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001570 .suspend = fb_suspend,
1571 .resume = fb_resume,
1572 .driver = {
1573 .name = DRIVER_NAME,
1574 .owner = THIS_MODULE,
1575 },
1576};
1577
1578static int __init da8xx_fb_init(void)
1579{
1580 return platform_driver_register(&da8xx_fb_driver);
1581}
1582
1583static void __exit da8xx_fb_cleanup(void)
1584{
1585 platform_driver_unregister(&da8xx_fb_driver);
1586}
1587
1588module_init(da8xx_fb_init);
1589module_exit(da8xx_fb_cleanup);
1590
1591MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
1592MODULE_AUTHOR("Texas Instruments");
1593MODULE_LICENSE("GPL");