blob: f2f66605e8fbe41185f70f16f1441c9b28d99241 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07002/*
3 * Copyright (C) 2008-2009 MontaVista Software Inc.
4 * Copyright (C) 2008-2009 Texas Instruments Inc
5 *
6 * Based on the LCD driver for TI Avalanche processors written by
7 * Ajay Singh and Shalom Hai.
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07008 */
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/fb.h>
12#include <linux/dma-mapping.h>
13#include <linux/device.h>
14#include <linux/platform_device.h>
15#include <linux/uaccess.h>
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +053016#include <linux/pm_runtime.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070017#include <linux/interrupt.h>
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +053018#include <linux/wait.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070019#include <linux/clk.h>
Chaithrika U Se04e5482009-12-15 16:46:29 -080020#include <linux/cpufreq.h>
Chaithrika U S1d3c6c72009-12-15 16:46:39 -080021#include <linux/console.h>
Bartosz Golaszewski611097d2019-07-22 15:44:18 +020022#include <linux/regulator/consumer.h>
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +053023#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Florian Tobias Schandinata0239072012-07-29 16:47:40 +000025#include <linux/delay.h>
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +053026#include <linux/lcm.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070027#include <video/da8xx-fb.h>
Manjunathappa, Prakash12fa8352012-02-09 11:54:06 +053028#include <asm/div64.h>
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070029
30#define DRIVER_NAME "da8xx_lcdc"
31
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053032#define LCD_VERSION_1 1
33#define LCD_VERSION_2 2
34
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070035/* LCD Status Register */
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070036#define LCD_END_OF_FRAME1 BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070037#define LCD_END_OF_FRAME0 BIT(8)
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070038#define LCD_PL_LOAD_DONE BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070039#define LCD_FIFO_UNDERFLOW BIT(5)
40#define LCD_SYNC_LOST BIT(2)
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +053041#define LCD_FRAME_DONE BIT(0)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070042
43/* LCD DMA Control Register */
44#define LCD_DMA_BURST_SIZE(x) ((x) << 4)
45#define LCD_DMA_BURST_1 0x0
46#define LCD_DMA_BURST_2 0x1
47#define LCD_DMA_BURST_4 0x2
48#define LCD_DMA_BURST_8 0x3
49#define LCD_DMA_BURST_16 0x4
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053050#define LCD_V1_END_OF_FRAME_INT_ENA BIT(2)
51#define LCD_V2_END_OF_FRAME0_INT_ENA BIT(8)
52#define LCD_V2_END_OF_FRAME1_INT_ENA BIT(9)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070053#define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
54
55/* LCD Control Register */
56#define LCD_CLK_DIVISOR(x) ((x) << 8)
57#define LCD_RASTER_MODE 0x01
58
59/* LCD Raster Control Register */
60#define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
61#define PALETTE_AND_DATA 0x00
62#define PALETTE_ONLY 0x01
Martin Ambrose1f9c3e12010-05-24 14:34:01 -070063#define DATA_ONLY 0x02
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070064
65#define LCD_MONO_8BIT_MODE BIT(9)
66#define LCD_RASTER_ORDER BIT(8)
67#define LCD_TFT_MODE BIT(7)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053068#define LCD_V1_UNDERFLOW_INT_ENA BIT(6)
69#define LCD_V2_UNDERFLOW_INT_ENA BIT(5)
70#define LCD_V1_PL_INT_ENA BIT(4)
71#define LCD_V2_PL_INT_ENA BIT(6)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070072#define LCD_MONOCHROME_MODE BIT(1)
73#define LCD_RASTER_ENABLE BIT(0)
74#define LCD_TFT_ALT_ENABLE BIT(23)
75#define LCD_STN_565_ENABLE BIT(24)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053076#define LCD_V2_DMA_CLK_EN BIT(2)
77#define LCD_V2_LIDD_CLK_EN BIT(1)
78#define LCD_V2_CORE_CLK_EN BIT(0)
79#define LCD_V2_LPP_B10 26
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +053080#define LCD_V2_TFT_24BPP_MODE BIT(25)
81#define LCD_V2_TFT_24BPP_UNPACK BIT(26)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070082
83/* LCD Raster Timing 2 Register */
84#define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
85#define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
86#define LCD_SYNC_CTRL BIT(25)
87#define LCD_SYNC_EDGE BIT(24)
88#define LCD_INVERT_PIXEL_CLOCK BIT(22)
89#define LCD_INVERT_LINE_CLOCK BIT(21)
90#define LCD_INVERT_FRAME_CLOCK BIT(20)
91
92/* LCD Block */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +053093#define LCD_PID_REG 0x0
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -070094#define LCD_CTRL_REG 0x4
95#define LCD_STAT_REG 0x8
96#define LCD_RASTER_CTRL_REG 0x28
97#define LCD_RASTER_TIMING_0_REG 0x2C
98#define LCD_RASTER_TIMING_1_REG 0x30
99#define LCD_RASTER_TIMING_2_REG 0x34
100#define LCD_DMA_CTRL_REG 0x40
101#define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
102#define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700103#define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
104#define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
105
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530106/* Interrupt Registers available only in Version 2 */
107#define LCD_RAW_STAT_REG 0x58
108#define LCD_MASKED_STAT_REG 0x5c
109#define LCD_INT_ENABLE_SET_REG 0x60
110#define LCD_INT_ENABLE_CLR_REG 0x64
111#define LCD_END_OF_INT_IND_REG 0x68
112
113/* Clock registers available only on Version 2 */
114#define LCD_CLK_ENABLE_REG 0x6c
115#define LCD_CLK_RESET_REG 0x70
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530116#define LCD_CLK_MAIN_RESET BIT(3)
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530117
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700118#define LCD_NUM_BUFFERS 2
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700119
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700120#define PALETTE_SIZE 256
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700121
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500122#define CLK_MIN_DIV 2
123#define CLK_MAX_DIV 255
124
Arnd Bergmann34aef6e2012-09-14 20:33:43 +0000125static void __iomem *da8xx_fb_reg_base;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530126static unsigned int lcd_revision;
127static irq_handler_t lcdc_irq_handler;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530128static wait_queue_head_t frame_done_wq;
129static int frame_done_flag;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700130
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500131static unsigned int lcdc_read(unsigned int addr)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700132{
133 return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
134}
135
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500136static void lcdc_write(unsigned int val, unsigned int addr)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700137{
138 __raw_writel(val, da8xx_fb_reg_base + (addr));
139}
140
141struct da8xx_fb_par {
Afzal Mohammeddbe8e482013-08-05 17:02:27 -0500142 struct device *dev;
Arnd Bergmannef88ee42016-01-25 16:05:18 +0100143 dma_addr_t p_palette_base;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700144 unsigned char *v_palette_base;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700145 dma_addr_t vram_phys;
146 unsigned long vram_size;
147 void *vram_virt;
148 unsigned int dma_start;
149 unsigned int dma_end;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700150 struct clk *lcdc_clk;
151 int irq;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700152 unsigned int palette_sz;
Chaithrika U S36113802009-12-15 16:46:38 -0800153 int blank;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700154 wait_queue_head_t vsync_wait;
155 int vsync_flag;
156 int vsync_timeout;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530157 spinlock_t lock_for_chan_update;
158
159 /*
160 * LCDC has 2 ping pong DMA channels, channel 0
161 * and channel 1.
162 */
163 unsigned int which_dma_channel_done;
Chaithrika U Se04e5482009-12-15 16:46:29 -0800164#ifdef CONFIG_CPU_FREQ
165 struct notifier_block freq_transition;
166#endif
Darren Etheridge0715c722013-08-05 17:02:37 -0500167 unsigned int lcdc_clk_rate;
Bartosz Golaszewski611097d2019-07-22 15:44:18 +0200168 struct regulator *lcd_supply;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530169 u32 pseudo_palette[16];
Afzal Mohammedb6dbe8e2013-08-05 17:02:24 -0500170 struct fb_videomode mode;
171 struct lcd_ctrl_config cfg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700172};
173
Afzal Mohammedbe0f6db2013-08-05 17:02:23 -0500174static struct fb_var_screeninfo da8xx_fb_var;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700175
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800176static struct fb_fix_screeninfo da8xx_fb_fix = {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700177 .id = "DA8xx FB Drv",
178 .type = FB_TYPE_PACKED_PIXELS,
179 .type_aux = 0,
180 .visual = FB_VISUAL_PSEUDOCOLOR,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700181 .xpanstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700182 .ypanstep = 1,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700183 .ywrapstep = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700184 .accel = FB_ACCEL_NONE
185};
186
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530187static struct fb_videomode known_lcd_panels[] = {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700188 /* Sharp LCD035Q3DG01 */
189 [0] = {
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530190 .name = "Sharp_LCD035Q3DG01",
191 .xres = 320,
192 .yres = 240,
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500193 .pixclock = KHZ2PICOS(4607),
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530194 .left_margin = 6,
195 .right_margin = 8,
196 .upper_margin = 2,
197 .lower_margin = 2,
198 .hsync_len = 0,
199 .vsync_len = 0,
Sushaanth Srirangapathi713fced2016-02-29 18:42:19 +0530200 .sync = FB_SYNC_CLK_INVERT,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700201 },
202 /* Sharp LK043T1DG01 */
203 [1] = {
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530204 .name = "Sharp_LK043T1DG01",
205 .xres = 480,
206 .yres = 272,
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500207 .pixclock = KHZ2PICOS(7833),
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530208 .left_margin = 2,
209 .right_margin = 2,
210 .upper_margin = 2,
211 .lower_margin = 2,
212 .hsync_len = 41,
213 .vsync_len = 10,
Sushaanth Srirangapathi713fced2016-02-29 18:42:19 +0530214 .sync = 0,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530215 .flag = 0,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700216 },
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100217 [2] = {
218 /* Hitachi SP10Q010 */
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530219 .name = "SP10Q010",
220 .xres = 320,
221 .yres = 240,
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500222 .pixclock = KHZ2PICOS(7833),
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530223 .left_margin = 10,
224 .right_margin = 10,
225 .upper_margin = 10,
226 .lower_margin = 10,
227 .hsync_len = 10,
228 .vsync_len = 10,
Sushaanth Srirangapathi713fced2016-02-29 18:42:19 +0530229 .sync = 0,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530230 .flag = 0,
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100231 },
Jon Ringlef474f0e2014-04-01 08:39:33 -0400232 [3] = {
233 /* Densitron 84-0023-001T */
234 .name = "Densitron_84-0023-001T",
235 .xres = 320,
236 .yres = 240,
237 .pixclock = KHZ2PICOS(6400),
238 .left_margin = 0,
239 .right_margin = 0,
240 .upper_margin = 0,
241 .lower_margin = 0,
242 .hsync_len = 30,
243 .vsync_len = 3,
244 .sync = 0,
245 },
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700246};
247
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500248static bool da8xx_fb_is_raster_enabled(void)
Darren Etheridgefe8c98f2013-08-05 17:02:29 -0500249{
250 return !!(lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE);
251}
252
Chaithrika U S36113802009-12-15 16:46:38 -0800253/* Enable the Raster Engine of the LCD Controller */
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500254static void lcd_enable_raster(void)
Chaithrika U S36113802009-12-15 16:46:38 -0800255{
256 u32 reg;
257
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530258 /* Put LCDC in reset for several cycles */
259 if (lcd_revision == LCD_VERSION_2)
260 /* Write 1 to reset LCDC */
261 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
262 mdelay(1);
263
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530264 /* Bring LCDC out of reset */
265 if (lcd_revision == LCD_VERSION_2)
266 lcdc_write(0, LCD_CLK_RESET_REG);
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530267 mdelay(1);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530268
Manjunathappa, Prakash92b4e452012-07-20 21:21:11 +0530269 /* Above reset sequence doesnot reset register context */
Chaithrika U S36113802009-12-15 16:46:38 -0800270 reg = lcdc_read(LCD_RASTER_CTRL_REG);
271 if (!(reg & LCD_RASTER_ENABLE))
272 lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
273}
274
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700275/* Disable the Raster Engine of the LCD Controller */
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500276static void lcd_disable_raster(enum da8xx_frame_complete wait_for_frame_done)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700277{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700278 u32 reg;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530279 int ret;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700280
281 reg = lcdc_read(LCD_RASTER_CTRL_REG);
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700282 if (reg & LCD_RASTER_ENABLE)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700283 lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530284 else
285 /* return if already disabled */
286 return;
287
Darren Etheridge26e71642013-08-05 17:02:30 -0500288 if ((wait_for_frame_done == DA8XX_FRAME_WAIT) &&
289 (lcd_revision == LCD_VERSION_2)) {
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530290 frame_done_flag = 0;
291 ret = wait_event_interruptible_timeout(frame_done_wq,
292 frame_done_flag != 0,
293 msecs_to_jiffies(50));
294 if (ret == 0)
295 pr_err("LCD Controller timed out\n");
296 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700297}
298
299static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
300{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700301 u32 start;
302 u32 end;
303 u32 reg_ras;
304 u32 reg_dma;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530305 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700306
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700307 /* init reg to clear PLM (loading mode) fields */
308 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
309 reg_ras &= ~(3 << 20);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700310
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700311 reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700312
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700313 if (load_mode == LOAD_DATA) {
314 start = par->dma_start;
315 end = par->dma_end;
316
317 reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530318 if (lcd_revision == LCD_VERSION_1) {
319 reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
320 } else {
321 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
322 LCD_V2_END_OF_FRAME0_INT_ENA |
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530323 LCD_V2_END_OF_FRAME1_INT_ENA |
Afzal Mohammede4008e22013-08-05 17:02:32 -0500324 LCD_FRAME_DONE | LCD_SYNC_LOST;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530325 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
326 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700327 reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
328
329 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
330 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
331 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
332 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
333 } else if (load_mode == LOAD_PALETTE) {
334 start = par->p_palette_base;
335 end = start + par->palette_sz - 1;
336
337 reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530338
339 if (lcd_revision == LCD_VERSION_1) {
340 reg_ras |= LCD_V1_PL_INT_ENA;
341 } else {
342 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
343 LCD_V2_PL_INT_ENA;
344 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
345 }
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700346
347 lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
348 lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
349 }
350
351 lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
352 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
353
354 /*
355 * The Raster enable bit must be set after all other control fields are
356 * set.
357 */
358 lcd_enable_raster();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700359}
360
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530361/* Configure the Burst Size and fifo threhold of DMA */
362static int lcd_cfg_dma(int burst_size, int fifo_th)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700363{
364 u32 reg;
365
366 reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
367 switch (burst_size) {
368 case 1:
369 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
370 break;
371 case 2:
372 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
373 break;
374 case 4:
375 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
376 break;
377 case 8:
378 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
379 break;
380 case 16:
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530381 default:
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700382 reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
383 break;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700384 }
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530385
386 reg |= (fifo_th << 8);
387
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700388 lcdc_write(reg, LCD_DMA_CTRL_REG);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700389
390 return 0;
391}
392
393static void lcd_cfg_ac_bias(int period, int transitions_per_int)
394{
395 u32 reg;
396
397 /* Set the AC Bias Period and Number of Transisitons per Interrupt */
398 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
399 reg |= LCD_AC_BIAS_FREQUENCY(period) |
400 LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
401 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
402}
403
404static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
405 int front_porch)
406{
407 u32 reg;
408
Ian Abbott4d4e2c02014-07-03 14:46:39 +0100409 reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0x3ff;
Darren Etheridge83edd732013-08-23 16:52:51 -0500410 reg |= (((back_porch-1) & 0xff) << 24)
411 | (((front_porch-1) & 0xff) << 16)
412 | (((pulse_width-1) & 0x3f) << 10);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700413 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
Darren Etheridge2645ad12013-08-23 16:52:52 -0500414
415 /*
416 * LCDC Version 2 adds some extra bits that increase the allowable
417 * size of the horizontal timing registers.
418 * remember that the registers use 0 to represent 1 so all values
419 * that get set into register need to be decremented by 1
420 */
421 if (lcd_revision == LCD_VERSION_2) {
422 /* Mask off the bits we want to change */
423 reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & ~0x780000ff;
424 reg |= ((front_porch-1) & 0x300) >> 8;
425 reg |= ((back_porch-1) & 0x300) >> 4;
426 reg |= ((pulse_width-1) & 0x3c0) << 21;
427 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
428 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700429}
430
431static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
432 int front_porch)
433{
434 u32 reg;
435
436 reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
437 reg |= ((back_porch & 0xff) << 24)
438 | ((front_porch & 0xff) << 16)
Darren Etheridge83edd732013-08-23 16:52:51 -0500439 | (((pulse_width-1) & 0x3f) << 10);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700440 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
441}
442
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530443static int lcd_cfg_display(const struct lcd_ctrl_config *cfg,
444 struct fb_videomode *panel)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700445{
446 u32 reg;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530447 u32 reg_int;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700448
449 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
450 LCD_MONO_8BIT_MODE |
451 LCD_MONOCHROME_MODE);
452
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530453 switch (cfg->panel_shade) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700454 case MONOCHROME:
455 reg |= LCD_MONOCHROME_MODE;
456 if (cfg->mono_8bit_mode)
457 reg |= LCD_MONO_8BIT_MODE;
458 break;
459 case COLOR_ACTIVE:
460 reg |= LCD_TFT_MODE;
461 if (cfg->tft_alt_mode)
462 reg |= LCD_TFT_ALT_ENABLE;
463 break;
464
465 case COLOR_PASSIVE:
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530466 /* AC bias applicable only for Pasive panels */
467 lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
468 if (cfg->bpp == 12 && cfg->stn_565_mode)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700469 reg |= LCD_STN_565_ENABLE;
470 break;
471
472 default:
473 return -EINVAL;
474 }
475
476 /* enable additional interrupts here */
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530477 if (lcd_revision == LCD_VERSION_1) {
478 reg |= LCD_V1_UNDERFLOW_INT_ENA;
479 } else {
480 reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
481 LCD_V2_UNDERFLOW_INT_ENA;
482 lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
483 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700484
485 lcdc_write(reg, LCD_RASTER_CTRL_REG);
486
487 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
488
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530489 reg |= LCD_SYNC_CTRL;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700490
491 if (cfg->sync_edge)
492 reg |= LCD_SYNC_EDGE;
493 else
494 reg &= ~LCD_SYNC_EDGE;
495
Darren Etheridge028cd862013-08-23 16:52:53 -0500496 if ((panel->sync & FB_SYNC_HOR_HIGH_ACT) == 0)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700497 reg |= LCD_INVERT_LINE_CLOCK;
498 else
499 reg &= ~LCD_INVERT_LINE_CLOCK;
500
Darren Etheridge028cd862013-08-23 16:52:53 -0500501 if ((panel->sync & FB_SYNC_VERT_HIGH_ACT) == 0)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700502 reg |= LCD_INVERT_FRAME_CLOCK;
503 else
504 reg &= ~LCD_INVERT_FRAME_CLOCK;
505
506 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
507
508 return 0;
509}
510
511static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
512 u32 bpp, u32 raster_order)
513{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700514 u32 reg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700515
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530516 if (bpp > 16 && lcd_revision == LCD_VERSION_1)
517 return -EINVAL;
518
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700519 /* Set the Panel Width */
520 /* Pixels per line = (PPL + 1)*16 */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530521 if (lcd_revision == LCD_VERSION_1) {
522 /*
523 * 0x3F in bits 4..9 gives max horizontal resolution = 1024
524 * pixels.
525 */
526 width &= 0x3f0;
527 } else {
528 /*
529 * 0x7F in bits 4..10 gives max horizontal resolution = 2048
530 * pixels.
531 */
532 width &= 0x7f0;
533 }
534
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700535 reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
536 reg &= 0xfffffc00;
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530537 if (lcd_revision == LCD_VERSION_1) {
538 reg |= ((width >> 4) - 1) << 4;
539 } else {
540 width = (width >> 4) - 1;
541 reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
542 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700543 lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
544
545 /* Set the Panel Height */
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530546 /* Set bits 9:0 of Lines Per Pixel */
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700547 reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
548 reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
549 lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
550
Manjunathappa, Prakash4d740802011-07-18 09:58:53 +0530551 /* Set bit 10 of Lines Per Pixel */
552 if (lcd_revision == LCD_VERSION_2) {
553 reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
554 reg |= ((height - 1) & 0x400) << 16;
555 lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
556 }
557
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700558 /* Set the Raster Order of the Frame Buffer */
559 reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
560 if (raster_order)
561 reg |= LCD_RASTER_ORDER;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530562
563 par->palette_sz = 16 * 2;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700564
565 switch (bpp) {
566 case 1:
567 case 2:
568 case 4:
569 case 16:
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530570 break;
571 case 24:
572 reg |= LCD_V2_TFT_24BPP_MODE;
Darren Etheridgefa8a00c2013-08-05 17:02:31 -0500573 break;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530574 case 32:
Darren Etheridgefa8a00c2013-08-05 17:02:31 -0500575 reg |= LCD_V2_TFT_24BPP_MODE;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530576 reg |= LCD_V2_TFT_24BPP_UNPACK;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700577 break;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700578 case 8:
579 par->palette_sz = 256 * 2;
580 break;
581
582 default:
583 return -EINVAL;
584 }
585
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530586 lcdc_write(reg, LCD_RASTER_CTRL_REG);
587
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700588 return 0;
589}
590
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530591#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700592static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
593 unsigned blue, unsigned transp,
594 struct fb_info *info)
595{
596 struct da8xx_fb_par *par = info->par;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700597 unsigned short *palette = (unsigned short *) par->v_palette_base;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700598 u_short pal;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700599 int update_hw = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700600
601 if (regno > 255)
602 return 1;
603
604 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
605 return 1;
606
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530607 if (info->var.bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
608 return -EINVAL;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100609
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530610 switch (info->fix.visual) {
611 case FB_VISUAL_TRUECOLOR:
612 red = CNVT_TOHW(red, info->var.red.length);
613 green = CNVT_TOHW(green, info->var.green.length);
614 blue = CNVT_TOHW(blue, info->var.blue.length);
615 break;
616 case FB_VISUAL_PSEUDOCOLOR:
617 switch (info->var.bits_per_pixel) {
618 case 4:
619 if (regno > 15)
620 return -EINVAL;
621
622 if (info->var.grayscale) {
623 pal = regno;
624 } else {
625 red >>= 4;
626 green >>= 8;
627 blue >>= 12;
628
629 pal = red & 0x0f00;
630 pal |= green & 0x00f0;
631 pal |= blue & 0x000f;
632 }
633 if (regno == 0)
634 pal |= 0x2000;
635 palette[regno] = pal;
636 break;
637
638 case 8:
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100639 red >>= 4;
640 green >>= 8;
641 blue >>= 12;
642
643 pal = (red & 0x0f00);
644 pal |= (green & 0x00f0);
645 pal |= (blue & 0x000f);
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530646
647 if (palette[regno] != pal) {
648 update_hw = 1;
649 palette[regno] = pal;
650 }
651 break;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100652 }
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530653 break;
654 }
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100655
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530656 /* Truecolor has hardware independent palette */
657 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
658 u32 v;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700659
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530660 if (regno > 15)
661 return -EINVAL;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700662
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530663 v = (red << info->var.red.offset) |
664 (green << info->var.green.offset) |
665 (blue << info->var.blue.offset);
666
Jon Ringle8d018642014-04-01 08:39:34 -0400667 ((u32 *) (info->pseudo_palette))[regno] = v;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700668 if (palette[0] != 0x4000) {
669 update_hw = 1;
670 palette[0] = 0x4000;
671 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700672 }
673
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700674 /* Update the palette in the h/w as needed. */
675 if (update_hw)
676 lcd_blit(LOAD_PALETTE, par);
677
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700678 return 0;
679}
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530680#undef CNVT_TOHW
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700681
Afzal Mohammed39c87d42013-08-05 17:02:21 -0500682static void da8xx_fb_lcd_reset(void)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700683{
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700684 /* DMA has to be disabled */
685 lcdc_write(0, LCD_DMA_CTRL_REG);
686 lcdc_write(0, LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530687
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530688 if (lcd_revision == LCD_VERSION_2) {
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530689 lcdc_write(0, LCD_INT_ENABLE_SET_REG);
Manjunathappa, Prakash74a0efd2011-11-15 17:32:23 +0530690 /* Write 1 to reset */
691 lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
692 lcdc_write(0, LCD_CLK_RESET_REG);
693 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700694}
695
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500696static int da8xx_fb_config_clk_divider(struct da8xx_fb_par *par,
697 unsigned lcdc_clk_div,
698 unsigned lcdc_clk_rate)
Chaithrika U S8097b172009-12-15 16:46:29 -0800699{
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500700 int ret;
Chaithrika U S8097b172009-12-15 16:46:29 -0800701
Darren Etheridge0715c722013-08-05 17:02:37 -0500702 if (par->lcdc_clk_rate != lcdc_clk_rate) {
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500703 ret = clk_set_rate(par->lcdc_clk, lcdc_clk_rate);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200704 if (ret) {
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500705 dev_err(par->dev,
706 "unable to set clock rate at %u\n",
707 lcdc_clk_rate);
708 return ret;
709 }
Darren Etheridge0715c722013-08-05 17:02:37 -0500710 par->lcdc_clk_rate = clk_get_rate(par->lcdc_clk);
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500711 }
Afzal Mohammed404fdfe2013-08-05 17:02:28 -0500712
Chaithrika U S8097b172009-12-15 16:46:29 -0800713 /* Configure the LCD clock divisor. */
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500714 lcdc_write(LCD_CLK_DIVISOR(lcdc_clk_div) |
Chaithrika U S8097b172009-12-15 16:46:29 -0800715 (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530716
717 if (lcd_revision == LCD_VERSION_2)
718 lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
719 LCD_V2_CORE_CLK_EN, LCD_CLK_ENABLE_REG);
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500720
721 return 0;
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500722}
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530723
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500724static unsigned int da8xx_fb_calc_clk_divider(struct da8xx_fb_par *par,
725 unsigned pixclock,
726 unsigned *lcdc_clk_rate)
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500727{
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500728 unsigned lcdc_clk_div;
Darren Etheridgea6a799f2013-08-05 17:02:26 -0500729
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500730 pixclock = PICOS2KHZ(pixclock) * 1000;
731
Darren Etheridge0715c722013-08-05 17:02:37 -0500732 *lcdc_clk_rate = par->lcdc_clk_rate;
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500733
734 if (pixclock < (*lcdc_clk_rate / CLK_MAX_DIV)) {
735 *lcdc_clk_rate = clk_round_rate(par->lcdc_clk,
736 pixclock * CLK_MAX_DIV);
737 lcdc_clk_div = CLK_MAX_DIV;
738 } else if (pixclock > (*lcdc_clk_rate / CLK_MIN_DIV)) {
739 *lcdc_clk_rate = clk_round_rate(par->lcdc_clk,
740 pixclock * CLK_MIN_DIV);
741 lcdc_clk_div = CLK_MIN_DIV;
742 } else {
743 lcdc_clk_div = *lcdc_clk_rate / pixclock;
744 }
745
746 return lcdc_clk_div;
747}
748
749static int da8xx_fb_calc_config_clk_divider(struct da8xx_fb_par *par,
750 struct fb_videomode *mode)
751{
752 unsigned lcdc_clk_rate;
753 unsigned lcdc_clk_div = da8xx_fb_calc_clk_divider(par, mode->pixclock,
754 &lcdc_clk_rate);
755
756 return da8xx_fb_config_clk_divider(par, lcdc_clk_div, lcdc_clk_rate);
757}
758
Darren Etheridgea9cd67c2013-08-05 17:02:38 -0500759static unsigned da8xx_fb_round_clk(struct da8xx_fb_par *par,
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500760 unsigned pixclock)
761{
762 unsigned lcdc_clk_div, lcdc_clk_rate;
763
764 lcdc_clk_div = da8xx_fb_calc_clk_divider(par, pixclock, &lcdc_clk_rate);
765 return KHZ2PICOS(lcdc_clk_rate / (1000 * lcdc_clk_div));
Chaithrika U S8097b172009-12-15 16:46:29 -0800766}
767
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700768static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530769 struct fb_videomode *panel)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700770{
771 u32 bpp;
772 int ret = 0;
773
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500774 ret = da8xx_fb_calc_config_clk_divider(par, panel);
Arnd Bergmann287980e2016-05-27 23:23:25 +0200775 if (ret) {
Darren Etheridge2dfa77a2013-08-05 17:02:36 -0500776 dev_err(par->dev, "unable to configure clock\n");
777 return ret;
778 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700779
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530780 if (panel->sync & FB_SYNC_CLK_INVERT)
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -0700781 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
782 LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
783 else
784 lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
785 ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
786
Manjunathappa, Prakashfb8fa942012-07-18 21:03:36 +0530787 /* Configure the DMA burst size and fifo threshold. */
788 ret = lcd_cfg_dma(cfg->dma_burst_sz, cfg->fifo_th);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700789 if (ret < 0)
790 return ret;
791
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700792 /* Configure the vertical and horizontal sync properties. */
Darren Etheridgea592d9f2013-08-23 16:52:50 -0500793 lcd_cfg_vertical_sync(panel->upper_margin, panel->vsync_len,
794 panel->lower_margin);
795 lcd_cfg_horizontal_sync(panel->left_margin, panel->hsync_len,
796 panel->right_margin);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700797
798 /* Configure for disply */
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530799 ret = lcd_cfg_display(cfg, panel);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700800 if (ret < 0)
801 return ret;
802
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +0530803 bpp = cfg->bpp;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700804
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700805 if (bpp == 12)
806 bpp = 16;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +0530807 ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->xres,
808 (unsigned int)panel->yres, bpp,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700809 cfg->raster_order);
810 if (ret < 0)
811 return ret;
812
813 /* Configure FDD */
814 lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
815 (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
816
817 return 0;
818}
819
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530820/* IRQ handler for version 2 of LCDC */
821static irqreturn_t lcdc_irq_handler_rev02(int irq, void *arg)
822{
823 struct da8xx_fb_par *par = arg;
824 u32 stat = lcdc_read(LCD_MASKED_STAT_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530825
826 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
Darren Etheridge26e71642013-08-05 17:02:30 -0500827 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530828 lcdc_write(stat, LCD_MASKED_STAT_REG);
829 lcd_enable_raster();
830 } else if (stat & LCD_PL_LOAD_DONE) {
831 /*
832 * Must disable raster before changing state of any control bit.
833 * And also must be disabled before clearing the PL loading
834 * interrupt via the following write to the status register. If
835 * this is done after then one gets multiple PL done interrupts.
836 */
Darren Etheridge26e71642013-08-05 17:02:30 -0500837 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530838
839 lcdc_write(stat, LCD_MASKED_STAT_REG);
840
Manjunathappa, Prakash8a81dcc2012-07-18 20:51:11 +0530841 /* Disable PL completion interrupt */
842 lcdc_write(LCD_V2_PL_INT_ENA, LCD_INT_ENABLE_CLR_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530843
844 /* Setup and start data loading mode */
845 lcd_blit(LOAD_DATA, par);
846 } else {
847 lcdc_write(stat, LCD_MASKED_STAT_REG);
848
849 if (stat & LCD_END_OF_FRAME0) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530850 par->which_dma_channel_done = 0;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530851 lcdc_write(par->dma_start,
852 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
853 lcdc_write(par->dma_end,
854 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
855 par->vsync_flag = 1;
856 wake_up_interruptible(&par->vsync_wait);
857 }
858
859 if (stat & LCD_END_OF_FRAME1) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530860 par->which_dma_channel_done = 1;
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530861 lcdc_write(par->dma_start,
862 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
863 lcdc_write(par->dma_end,
864 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
865 par->vsync_flag = 1;
866 wake_up_interruptible(&par->vsync_wait);
867 }
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +0530868
869 /* Set only when controller is disabled and at the end of
870 * active frame
871 */
872 if (stat & BIT(0)) {
873 frame_done_flag = 1;
874 wake_up_interruptible(&frame_done_wq);
875 }
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530876 }
877
878 lcdc_write(0, LCD_END_OF_INT_IND_REG);
879 return IRQ_HANDLED;
880}
881
882/* IRQ handler for version 1 LCDC */
883static irqreturn_t lcdc_irq_handler_rev01(int irq, void *arg)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700884{
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700885 struct da8xx_fb_par *par = arg;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700886 u32 stat = lcdc_read(LCD_STAT_REG);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700887 u32 reg_ras;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700888
889 if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
Darren Etheridge26e71642013-08-05 17:02:30 -0500890 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700891 lcdc_write(stat, LCD_STAT_REG);
Chaithrika U S36113802009-12-15 16:46:38 -0800892 lcd_enable_raster();
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700893 } else if (stat & LCD_PL_LOAD_DONE) {
894 /*
895 * Must disable raster before changing state of any control bit.
896 * And also must be disabled before clearing the PL loading
897 * interrupt via the following write to the status register. If
898 * this is done after then one gets multiple PL done interrupts.
899 */
Darren Etheridge26e71642013-08-05 17:02:30 -0500900 lcd_disable_raster(DA8XX_FRAME_NOWAIT);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700901
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700902 lcdc_write(stat, LCD_STAT_REG);
903
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700904 /* Disable PL completion inerrupt */
905 reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +0530906 reg_ras &= ~LCD_V1_PL_INT_ENA;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700907 lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
908
909 /* Setup and start data loading mode */
910 lcd_blit(LOAD_DATA, par);
911 } else {
912 lcdc_write(stat, LCD_STAT_REG);
913
914 if (stat & LCD_END_OF_FRAME0) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530915 par->which_dma_channel_done = 0;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700916 lcdc_write(par->dma_start,
917 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
918 lcdc_write(par->dma_end,
919 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
920 par->vsync_flag = 1;
921 wake_up_interruptible(&par->vsync_wait);
922 }
923
924 if (stat & LCD_END_OF_FRAME1) {
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +0530925 par->which_dma_channel_done = 1;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -0700926 lcdc_write(par->dma_start,
927 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
928 lcdc_write(par->dma_end,
929 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
930 par->vsync_flag = 1;
931 wake_up_interruptible(&par->vsync_wait);
932 }
933 }
934
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700935 return IRQ_HANDLED;
936}
937
938static int fb_check_var(struct fb_var_screeninfo *var,
939 struct fb_info *info)
940{
941 int err = 0;
Afzal Mohammed87dac712013-08-05 17:02:20 -0500942 struct da8xx_fb_par *par = info->par;
943 int bpp = var->bits_per_pixel >> 3;
944 unsigned long line_size = var->xres_virtual * bpp;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700945
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530946 if (var->bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
947 return -EINVAL;
948
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700949 switch (var->bits_per_pixel) {
950 case 1:
951 case 8:
952 var->red.offset = 0;
953 var->red.length = 8;
954 var->green.offset = 0;
955 var->green.length = 8;
956 var->blue.offset = 0;
957 var->blue.length = 8;
958 var->transp.offset = 0;
959 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100960 var->nonstd = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700961 break;
962 case 4:
963 var->red.offset = 0;
964 var->red.length = 4;
965 var->green.offset = 0;
966 var->green.length = 4;
967 var->blue.offset = 0;
968 var->blue.length = 4;
969 var->transp.offset = 0;
970 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100971 var->nonstd = FB_NONSTD_REV_PIX_IN_B;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700972 break;
973 case 16: /* RGB 565 */
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800974 var->red.offset = 11;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700975 var->red.length = 5;
976 var->green.offset = 5;
977 var->green.length = 6;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -0800978 var->blue.offset = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700979 var->blue.length = 5;
980 var->transp.offset = 0;
981 var->transp.length = 0;
Anatolij Gustschinf4130702012-03-13 14:13:57 +0100982 var->nonstd = 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -0700983 break;
Manjunathappa, Prakash1a2b7502012-08-14 18:51:42 +0530984 case 24:
985 var->red.offset = 16;
986 var->red.length = 8;
987 var->green.offset = 8;
988 var->green.length = 8;
989 var->blue.offset = 0;
990 var->blue.length = 8;
991 var->nonstd = 0;
992 break;
993 case 32:
994 var->transp.offset = 24;
995 var->transp.length = 8;
996 var->red.offset = 16;
997 var->red.length = 8;
998 var->green.offset = 8;
999 var->green.length = 8;
1000 var->blue.offset = 0;
1001 var->blue.length = 8;
1002 var->nonstd = 0;
1003 break;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001004 default:
1005 err = -EINVAL;
1006 }
1007
1008 var->red.msb_right = 0;
1009 var->green.msb_right = 0;
1010 var->blue.msb_right = 0;
1011 var->transp.msb_right = 0;
Afzal Mohammed87dac712013-08-05 17:02:20 -05001012
1013 if (line_size * var->yres_virtual > par->vram_size)
1014 var->yres_virtual = par->vram_size / line_size;
1015
1016 if (var->yres > var->yres_virtual)
1017 var->yres = var->yres_virtual;
1018
1019 if (var->xres > var->xres_virtual)
1020 var->xres = var->xres_virtual;
1021
1022 if (var->xres + var->xoffset > var->xres_virtual)
1023 var->xoffset = var->xres_virtual - var->xres;
1024 if (var->yres + var->yoffset > var->yres_virtual)
1025 var->yoffset = var->yres_virtual - var->yres;
1026
Afzal Mohammed404fdfe2013-08-05 17:02:28 -05001027 var->pixclock = da8xx_fb_round_clk(par, var->pixclock);
1028
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001029 return err;
1030}
1031
Chaithrika U Se04e5482009-12-15 16:46:29 -08001032#ifdef CONFIG_CPU_FREQ
1033static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
1034 unsigned long val, void *data)
1035{
1036 struct da8xx_fb_par *par;
Chaithrika U Se04e5482009-12-15 16:46:29 -08001037
1038 par = container_of(nb, struct da8xx_fb_par, freq_transition);
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +05301039 if (val == CPUFREQ_POSTCHANGE) {
Darren Etheridge0715c722013-08-05 17:02:37 -05001040 if (par->lcdc_clk_rate != clk_get_rate(par->lcdc_clk)) {
1041 par->lcdc_clk_rate = clk_get_rate(par->lcdc_clk);
Darren Etheridge26e71642013-08-05 17:02:30 -05001042 lcd_disable_raster(DA8XX_FRAME_WAIT);
Darren Etheridgea6a799f2013-08-05 17:02:26 -05001043 da8xx_fb_calc_config_clk_divider(par, &par->mode);
Manjunathappa, Prakash67900812012-08-31 19:48:59 +05301044 if (par->blank == FB_BLANK_UNBLANK)
1045 lcd_enable_raster();
Manjunathappa, Prakashf8209172012-01-03 18:10:51 +05301046 }
Chaithrika U Se04e5482009-12-15 16:46:29 -08001047 }
1048
1049 return 0;
1050}
1051
Darren Etheridgea9cd67c2013-08-05 17:02:38 -05001052static int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
Chaithrika U Se04e5482009-12-15 16:46:29 -08001053{
1054 par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
1055
1056 return cpufreq_register_notifier(&par->freq_transition,
1057 CPUFREQ_TRANSITION_NOTIFIER);
1058}
1059
Darren Etheridgea9cd67c2013-08-05 17:02:38 -05001060static void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
Chaithrika U Se04e5482009-12-15 16:46:29 -08001061{
1062 cpufreq_unregister_notifier(&par->freq_transition,
1063 CPUFREQ_TRANSITION_NOTIFIER);
1064}
1065#endif
1066
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001067static int fb_remove(struct platform_device *dev)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001068{
1069 struct fb_info *info = dev_get_drvdata(&dev->dev);
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001070 int ret;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001071
1072 if (info) {
1073 struct da8xx_fb_par *par = info->par;
1074
Chaithrika U Se04e5482009-12-15 16:46:29 -08001075#ifdef CONFIG_CPU_FREQ
1076 lcd_da8xx_cpufreq_deregister(par);
1077#endif
Bartosz Golaszewski3fca9e02019-07-22 15:44:20 +02001078 if (par->lcd_supply) {
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001079 ret = regulator_disable(par->lcd_supply);
1080 if (ret)
1081 return ret;
1082 }
Chaithrika U S36113802009-12-15 16:46:38 -08001083
Darren Etheridge26e71642013-08-05 17:02:30 -05001084 lcd_disable_raster(DA8XX_FRAME_WAIT);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001085 lcdc_write(0, LCD_RASTER_CTRL_REG);
1086
1087 /* disable DMA */
1088 lcdc_write(0, LCD_DMA_CTRL_REG);
1089
1090 unregister_framebuffer(info);
1091 fb_dealloc_cmap(&info->cmap);
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001092 dma_free_coherent(par->dev, PALETTE_SIZE, par->v_palette_base,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001093 par->p_palette_base);
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001094 dma_free_coherent(par->dev, par->vram_size, par->vram_virt,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001095 par->vram_phys);
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301096 pm_runtime_put_sync(&dev->dev);
1097 pm_runtime_disable(&dev->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001098 framebuffer_release(info);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001099
1100 }
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001101 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001102}
1103
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001104/*
1105 * Function to wait for vertical sync which for this LCD peripheral
1106 * translates into waiting for the current raster frame to complete.
1107 */
1108static int fb_wait_for_vsync(struct fb_info *info)
1109{
1110 struct da8xx_fb_par *par = info->par;
1111 int ret;
1112
1113 /*
1114 * 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 -03001115 * race condition here where the ISR could have occurred just before or
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001116 * just after this set. But since we are just coarsely waiting for
1117 * a frame to complete then that's OK. i.e. if the frame completed
1118 * just before this code executed then we have to wait another full
1119 * frame time but there is no way to avoid such a situation. On the
1120 * other hand if the frame completed just after then we don't need
1121 * to wait long at all. Either way we are guaranteed to return to the
1122 * user immediately after a frame completion which is all that is
1123 * required.
1124 */
1125 par->vsync_flag = 0;
1126 ret = wait_event_interruptible_timeout(par->vsync_wait,
1127 par->vsync_flag != 0,
1128 par->vsync_timeout);
1129 if (ret < 0)
1130 return ret;
1131 if (ret == 0)
1132 return -ETIMEDOUT;
1133
1134 return 0;
1135}
1136
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001137static int fb_ioctl(struct fb_info *info, unsigned int cmd,
1138 unsigned long arg)
1139{
1140 struct lcd_sync_arg sync_arg;
1141
1142 switch (cmd) {
1143 case FBIOGET_CONTRAST:
1144 case FBIOPUT_CONTRAST:
1145 case FBIGET_BRIGHTNESS:
1146 case FBIPUT_BRIGHTNESS:
1147 case FBIGET_COLOR:
1148 case FBIPUT_COLOR:
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001149 return -ENOTTY;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001150 case FBIPUT_HSYNC:
1151 if (copy_from_user(&sync_arg, (char *)arg,
1152 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001153 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001154 lcd_cfg_horizontal_sync(sync_arg.back_porch,
1155 sync_arg.pulse_width,
1156 sync_arg.front_porch);
1157 break;
1158 case FBIPUT_VSYNC:
1159 if (copy_from_user(&sync_arg, (char *)arg,
1160 sizeof(struct lcd_sync_arg)))
Sudhakar Rajashekhara2f93e8f2009-09-22 16:47:06 -07001161 return -EFAULT;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001162 lcd_cfg_vertical_sync(sync_arg.back_porch,
1163 sync_arg.pulse_width,
1164 sync_arg.front_porch);
1165 break;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001166 case FBIO_WAITFORVSYNC:
1167 return fb_wait_for_vsync(info);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001168 default:
1169 return -EINVAL;
1170 }
1171 return 0;
1172}
1173
Chaithrika U S312d9712009-12-15 16:46:39 -08001174static int cfb_blank(int blank, struct fb_info *info)
1175{
1176 struct da8xx_fb_par *par = info->par;
1177 int ret = 0;
1178
1179 if (par->blank == blank)
1180 return 0;
1181
1182 par->blank = blank;
1183 switch (blank) {
1184 case FB_BLANK_UNBLANK:
Manjunathappa, Prakashf7c848b2012-07-24 09:45:25 +05301185 lcd_enable_raster();
1186
Bartosz Golaszewski3fca9e02019-07-22 15:44:20 +02001187 if (par->lcd_supply) {
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001188 ret = regulator_enable(par->lcd_supply);
1189 if (ret)
1190 return ret;
1191 }
Chaithrika U S312d9712009-12-15 16:46:39 -08001192 break;
Yegor Yefremov99a647d2012-07-06 16:01:28 +02001193 case FB_BLANK_NORMAL:
1194 case FB_BLANK_VSYNC_SUSPEND:
1195 case FB_BLANK_HSYNC_SUSPEND:
Chaithrika U S312d9712009-12-15 16:46:39 -08001196 case FB_BLANK_POWERDOWN:
Bartosz Golaszewski3fca9e02019-07-22 15:44:20 +02001197 if (par->lcd_supply) {
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001198 ret = regulator_disable(par->lcd_supply);
1199 if (ret)
1200 return ret;
1201 }
Chaithrika U S312d9712009-12-15 16:46:39 -08001202
Darren Etheridge26e71642013-08-05 17:02:30 -05001203 lcd_disable_raster(DA8XX_FRAME_WAIT);
Chaithrika U S312d9712009-12-15 16:46:39 -08001204 break;
1205 default:
1206 ret = -EINVAL;
1207 }
1208
1209 return ret;
1210}
1211
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001212/*
1213 * Set new x,y offsets in the virtual display for the visible area and switch
1214 * to the new mode.
1215 */
1216static int da8xx_pan_display(struct fb_var_screeninfo *var,
1217 struct fb_info *fbi)
1218{
1219 int ret = 0;
1220 struct fb_var_screeninfo new_var;
1221 struct da8xx_fb_par *par = fbi->par;
1222 struct fb_fix_screeninfo *fix = &fbi->fix;
1223 unsigned int end;
1224 unsigned int start;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301225 unsigned long irq_flags;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001226
1227 if (var->xoffset != fbi->var.xoffset ||
1228 var->yoffset != fbi->var.yoffset) {
1229 memcpy(&new_var, &fbi->var, sizeof(new_var));
1230 new_var.xoffset = var->xoffset;
1231 new_var.yoffset = var->yoffset;
1232 if (fb_check_var(&new_var, fbi))
1233 ret = -EINVAL;
1234 else {
1235 memcpy(&fbi->var, &new_var, sizeof(new_var));
1236
1237 start = fix->smem_start +
1238 new_var.yoffset * fix->line_length +
Laurent Pincharte6c4d3d2011-06-14 09:24:45 +00001239 new_var.xoffset * fbi->var.bits_per_pixel / 8;
1240 end = start + fbi->var.yres * fix->line_length - 1;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001241 par->dma_start = start;
1242 par->dma_end = end;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301243 spin_lock_irqsave(&par->lock_for_chan_update,
1244 irq_flags);
1245 if (par->which_dma_channel_done == 0) {
1246 lcdc_write(par->dma_start,
1247 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1248 lcdc_write(par->dma_end,
1249 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1250 } else if (par->which_dma_channel_done == 1) {
1251 lcdc_write(par->dma_start,
1252 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1253 lcdc_write(par->dma_end,
1254 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1255 }
1256 spin_unlock_irqrestore(&par->lock_for_chan_update,
1257 irq_flags);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001258 }
1259 }
1260
1261 return ret;
1262}
1263
Darren Etheridgefe8c98f2013-08-05 17:02:29 -05001264static int da8xxfb_set_par(struct fb_info *info)
1265{
1266 struct da8xx_fb_par *par = info->par;
1267 int ret;
1268 bool raster = da8xx_fb_is_raster_enabled();
1269
1270 if (raster)
Darren Etheridge26e71642013-08-05 17:02:30 -05001271 lcd_disable_raster(DA8XX_FRAME_WAIT);
Darren Etheridgefe8c98f2013-08-05 17:02:29 -05001272
1273 fb_var_to_videomode(&par->mode, &info->var);
1274
1275 par->cfg.bpp = info->var.bits_per_pixel;
1276
1277 info->fix.visual = (par->cfg.bpp <= 8) ?
1278 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
1279 info->fix.line_length = (par->mode.xres * par->cfg.bpp) / 8;
1280
1281 ret = lcd_init(par, &par->cfg, &par->mode);
1282 if (ret < 0) {
1283 dev_err(par->dev, "lcd init failed\n");
1284 return ret;
1285 }
1286
1287 par->dma_start = info->fix.smem_start +
1288 info->var.yoffset * info->fix.line_length +
1289 info->var.xoffset * info->var.bits_per_pixel / 8;
1290 par->dma_end = par->dma_start +
1291 info->var.yres * info->fix.line_length - 1;
1292
1293 lcdc_write(par->dma_start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1294 lcdc_write(par->dma_end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1295 lcdc_write(par->dma_start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1296 lcdc_write(par->dma_end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1297
1298 if (raster)
1299 lcd_enable_raster();
1300
1301 return 0;
1302}
1303
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001304static struct fb_ops da8xx_fb_ops = {
1305 .owner = THIS_MODULE,
1306 .fb_check_var = fb_check_var,
Darren Etheridgefe8c98f2013-08-05 17:02:29 -05001307 .fb_set_par = da8xxfb_set_par,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001308 .fb_setcolreg = fb_setcolreg,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001309 .fb_pan_display = da8xx_pan_display,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001310 .fb_ioctl = fb_ioctl,
1311 .fb_fillrect = cfb_fillrect,
1312 .fb_copyarea = cfb_copyarea,
1313 .fb_imageblit = cfb_imageblit,
Chaithrika U S312d9712009-12-15 16:46:39 -08001314 .fb_blank = cfb_blank,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001315};
1316
Afzal Mohammed2bdff062013-08-05 17:02:35 -05001317static struct fb_videomode *da8xx_fb_get_videomode(struct platform_device *dev)
1318{
Jingoo Han700601d2013-09-17 14:04:50 +09001319 struct da8xx_lcdc_platform_data *fb_pdata = dev_get_platdata(&dev->dev);
Afzal Mohammed2bdff062013-08-05 17:02:35 -05001320 struct fb_videomode *lcdc_info;
1321 int i;
1322
1323 for (i = 0, lcdc_info = known_lcd_panels;
1324 i < ARRAY_SIZE(known_lcd_panels); i++, lcdc_info++) {
1325 if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
1326 break;
1327 }
1328
1329 if (i == ARRAY_SIZE(known_lcd_panels)) {
1330 dev_err(&dev->dev, "no panel found\n");
1331 return NULL;
1332 }
1333 dev_info(&dev->dev, "found %s panel\n", lcdc_info->name);
1334
1335 return lcdc_info;
1336}
1337
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001338static int fb_probe(struct platform_device *device)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001339{
1340 struct da8xx_lcdc_platform_data *fb_pdata =
Jingoo Han700601d2013-09-17 14:04:50 +09001341 dev_get_platdata(&device->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001342 struct lcd_ctrl_config *lcd_cfg;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301343 struct fb_videomode *lcdc_info;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001344 struct fb_info *da8xx_fb_info;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001345 struct da8xx_fb_par *par;
Darren Etheridge0715c722013-08-05 17:02:37 -05001346 struct clk *tmp_lcdc_clk;
Afzal Mohammed2bdff062013-08-05 17:02:35 -05001347 int ret;
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +05301348 unsigned long ulcm;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001349
1350 if (fb_pdata == NULL) {
1351 dev_err(&device->dev, "Can not get platform data\n");
1352 return -ENOENT;
1353 }
1354
Afzal Mohammed2bdff062013-08-05 17:02:35 -05001355 lcdc_info = da8xx_fb_get_videomode(device);
1356 if (lcdc_info == NULL)
1357 return -ENODEV;
1358
Bartosz Golaszewskic957c882019-07-22 15:44:21 +02001359 da8xx_fb_reg_base = devm_platform_ioremap_resource(device, 0);
Darren Etheridgec45757f2013-08-05 17:02:33 -05001360 if (IS_ERR(da8xx_fb_reg_base))
1361 return PTR_ERR(da8xx_fb_reg_base);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001362
Darren Etheridge0715c722013-08-05 17:02:37 -05001363 tmp_lcdc_clk = devm_clk_get(&device->dev, "fck");
1364 if (IS_ERR(tmp_lcdc_clk)) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001365 dev_err(&device->dev, "Can not get device clock\n");
Darren Etheridge0715c722013-08-05 17:02:37 -05001366 return PTR_ERR(tmp_lcdc_clk);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001367 }
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301368
1369 pm_runtime_enable(&device->dev);
1370 pm_runtime_get_sync(&device->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001371
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301372 /* Determine LCD IP Version */
1373 switch (lcdc_read(LCD_PID_REG)) {
1374 case 0x4C100102:
1375 lcd_revision = LCD_VERSION_1;
1376 break;
1377 case 0x4F200800:
Pantelis Antoniou8f22e8e2012-10-31 17:56:24 +02001378 case 0x4F201000:
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301379 lcd_revision = LCD_VERSION_2;
1380 break;
1381 default:
1382 dev_warn(&device->dev, "Unknown PID Reg value 0x%x, "
1383 "defaulting to LCD revision 1\n",
1384 lcdc_read(LCD_PID_REG));
1385 lcd_revision = LCD_VERSION_1;
1386 break;
1387 }
1388
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001389 lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
1390
Afzal Mohammed3a581012013-08-05 17:02:34 -05001391 if (!lcd_cfg) {
1392 ret = -EINVAL;
1393 goto err_pm_runtime_disable;
1394 }
1395
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001396 da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
1397 &device->dev);
1398 if (!da8xx_fb_info) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001399 ret = -ENOMEM;
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301400 goto err_pm_runtime_disable;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001401 }
1402
1403 par = da8xx_fb_info->par;
Afzal Mohammeddbe8e482013-08-05 17:02:27 -05001404 par->dev = &device->dev;
Darren Etheridge0715c722013-08-05 17:02:37 -05001405 par->lcdc_clk = tmp_lcdc_clk;
1406 par->lcdc_clk_rate = clk_get_rate(par->lcdc_clk);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001407
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001408 par->lcd_supply = devm_regulator_get_optional(&device->dev, "lcd");
1409 if (IS_ERR(par->lcd_supply)) {
1410 if (PTR_ERR(par->lcd_supply) == -EPROBE_DEFER) {
1411 ret = -EPROBE_DEFER;
1412 goto err_pm_runtime_disable;
1413 }
1414
1415 par->lcd_supply = NULL;
1416 } else {
1417 ret = regulator_enable(par->lcd_supply);
1418 if (ret)
1419 goto err_pm_runtime_disable;
1420 }
1421
Afzal Mohammedb8664582013-08-05 17:02:22 -05001422 fb_videomode_to_var(&da8xx_fb_var, lcdc_info);
Afzal Mohammedb6dbe8e2013-08-05 17:02:24 -05001423 par->cfg = *lcd_cfg;
Afzal Mohammedb8664582013-08-05 17:02:22 -05001424
Darren Etheridgefe8c98f2013-08-05 17:02:29 -05001425 da8xx_fb_lcd_reset();
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001426
1427 /* allocate frame buffer */
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301428 par->vram_size = lcdc_info->xres * lcdc_info->yres * lcd_cfg->bpp;
1429 ulcm = lcm((lcdc_info->xres * lcd_cfg->bpp)/8, PAGE_SIZE);
Aditya Nellutla3b9cc4e2012-05-23 11:36:31 +05301430 par->vram_size = roundup(par->vram_size/8, ulcm);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001431 par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001432
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001433 par->vram_virt = dma_alloc_coherent(par->dev,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001434 par->vram_size,
Arnd Bergmannef88ee42016-01-25 16:05:18 +01001435 &par->vram_phys,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001436 GFP_KERNEL | GFP_DMA);
1437 if (!par->vram_virt) {
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001438 dev_err(&device->dev,
1439 "GLCD: kmalloc for frame buffer failed\n");
1440 ret = -EINVAL;
1441 goto err_release_fb;
1442 }
1443
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001444 da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
1445 da8xx_fb_fix.smem_start = par->vram_phys;
1446 da8xx_fb_fix.smem_len = par->vram_size;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301447 da8xx_fb_fix.line_length = (lcdc_info->xres * lcd_cfg->bpp) / 8;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001448
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001449 par->dma_start = par->vram_phys;
Manjunathappa, Prakashf772fab2012-10-16 10:23:15 +05301450 par->dma_end = par->dma_start + lcdc_info->yres *
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001451 da8xx_fb_fix.line_length - 1;
1452
1453 /* allocate palette buffer */
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001454 par->v_palette_base = dma_alloc_coherent(par->dev, PALETTE_SIZE,
Luis Chamberlain750afb02019-01-04 09:23:09 +01001455 &par->p_palette_base,
1456 GFP_KERNEL | GFP_DMA);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001457 if (!par->v_palette_base) {
1458 dev_err(&device->dev,
1459 "GLCD: kmalloc for palette buffer failed\n");
1460 ret = -EINVAL;
1461 goto err_release_fb_mem;
1462 }
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001463
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001464 par->irq = platform_get_irq(device, 0);
1465 if (par->irq < 0) {
1466 ret = -ENOENT;
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001467 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001468 }
1469
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001470 da8xx_fb_var.grayscale =
Manjunathappa, Prakash3b43ad22012-10-16 10:23:16 +05301471 lcd_cfg->panel_shade == MONOCHROME ? 1 : 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001472 da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001473
1474 /* Initialize fbinfo */
1475 da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
1476 da8xx_fb_info->fix = da8xx_fb_fix;
1477 da8xx_fb_info->var = da8xx_fb_var;
1478 da8xx_fb_info->fbops = &da8xx_fb_ops;
1479 da8xx_fb_info->pseudo_palette = par->pseudo_palette;
Sudhakar Rajashekhara3510b8f2009-12-01 13:17:43 -08001480 da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
1481 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001482
1483 ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
1484 if (ret)
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001485 goto err_release_pl_mem;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001486 da8xx_fb_info->cmap.len = par->palette_sz;
1487
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001488 /* initialize var_screeninfo */
1489 da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
1490 fb_set_var(da8xx_fb_info, &da8xx_fb_var);
1491
1492 dev_set_drvdata(&device->dev, da8xx_fb_info);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001493
1494 /* initialize the vsync wait queue */
1495 init_waitqueue_head(&par->vsync_wait);
1496 par->vsync_timeout = HZ / 5;
Manjunathappa, Prakashdeb95c62012-07-18 21:01:56 +05301497 par->which_dma_channel_done = -1;
1498 spin_lock_init(&par->lock_for_chan_update);
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001499
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001500 /* Register the Frame Buffer */
1501 if (register_framebuffer(da8xx_fb_info) < 0) {
1502 dev_err(&device->dev,
1503 "GLCD: Frame Buffer Registration Failed!\n");
1504 ret = -EINVAL;
1505 goto err_dealloc_cmap;
1506 }
1507
Chaithrika U Se04e5482009-12-15 16:46:29 -08001508#ifdef CONFIG_CPU_FREQ
1509 ret = lcd_da8xx_cpufreq_register(par);
1510 if (ret) {
1511 dev_err(&device->dev, "failed to register cpufreq\n");
1512 goto err_cpu_freq;
1513 }
1514#endif
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001515
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301516 if (lcd_revision == LCD_VERSION_1)
1517 lcdc_irq_handler = lcdc_irq_handler_rev01;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301518 else {
1519 init_waitqueue_head(&frame_done_wq);
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301520 lcdc_irq_handler = lcdc_irq_handler_rev02;
Manjunathappa, Prakasha481b372012-08-24 18:43:00 +05301521 }
Manjunathappa, Prakashc6daf052011-07-05 15:51:20 +05301522
Darren Etheridgec45757f2013-08-05 17:02:33 -05001523 ret = devm_request_irq(&device->dev, par->irq, lcdc_irq_handler, 0,
1524 DRIVER_NAME, par);
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001525 if (ret)
1526 goto irq_freq;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001527 return 0;
1528
Caglar Akyuz93c176f2010-11-30 20:04:14 +00001529irq_freq:
Chaithrika U Se04e5482009-12-15 16:46:29 -08001530#ifdef CONFIG_CPU_FREQ
axel lin360c2022011-01-20 03:50:51 +00001531 lcd_da8xx_cpufreq_deregister(par);
Chaithrika U Se04e5482009-12-15 16:46:29 -08001532err_cpu_freq:
Manjunathappa, Prakash3a844092012-02-09 10:34:38 +05301533#endif
Chaithrika U Se04e5482009-12-15 16:46:29 -08001534 unregister_framebuffer(da8xx_fb_info);
Chaithrika U Se04e5482009-12-15 16:46:29 -08001535
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001536err_dealloc_cmap:
1537 fb_dealloc_cmap(&da8xx_fb_info->cmap);
1538
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001539err_release_pl_mem:
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001540 dma_free_coherent(par->dev, PALETTE_SIZE, par->v_palette_base,
Martin Ambrose1f9c3e12010-05-24 14:34:01 -07001541 par->p_palette_base);
1542
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001543err_release_fb_mem:
Christoph Hellwig12a7b032019-01-23 09:05:27 +01001544 dma_free_coherent(par->dev, par->vram_size, par->vram_virt,
1545 par->vram_phys);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001546
1547err_release_fb:
1548 framebuffer_release(da8xx_fb_info);
1549
Manjunathappa, Prakash9dd44d52012-09-21 21:20:57 +05301550err_pm_runtime_disable:
1551 pm_runtime_put_sync(&device->dev);
1552 pm_runtime_disable(&device->dev);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001553
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001554 return ret;
1555}
1556
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301557#ifdef CONFIG_PM_SLEEP
Sachin Kamat58f38532013-09-25 16:59:51 +05301558static struct lcdc_context {
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301559 u32 clk_enable;
1560 u32 ctrl;
1561 u32 dma_ctrl;
1562 u32 raster_timing_0;
1563 u32 raster_timing_1;
1564 u32 raster_timing_2;
1565 u32 int_enable_set;
1566 u32 dma_frm_buf_base_addr_0;
1567 u32 dma_frm_buf_ceiling_addr_0;
1568 u32 dma_frm_buf_base_addr_1;
1569 u32 dma_frm_buf_ceiling_addr_1;
1570 u32 raster_ctrl;
1571} reg_context;
1572
1573static void lcd_context_save(void)
1574{
1575 if (lcd_revision == LCD_VERSION_2) {
1576 reg_context.clk_enable = lcdc_read(LCD_CLK_ENABLE_REG);
1577 reg_context.int_enable_set = lcdc_read(LCD_INT_ENABLE_SET_REG);
1578 }
1579
1580 reg_context.ctrl = lcdc_read(LCD_CTRL_REG);
1581 reg_context.dma_ctrl = lcdc_read(LCD_DMA_CTRL_REG);
1582 reg_context.raster_timing_0 = lcdc_read(LCD_RASTER_TIMING_0_REG);
1583 reg_context.raster_timing_1 = lcdc_read(LCD_RASTER_TIMING_1_REG);
1584 reg_context.raster_timing_2 = lcdc_read(LCD_RASTER_TIMING_2_REG);
1585 reg_context.dma_frm_buf_base_addr_0 =
1586 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1587 reg_context.dma_frm_buf_ceiling_addr_0 =
1588 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1589 reg_context.dma_frm_buf_base_addr_1 =
1590 lcdc_read(LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1591 reg_context.dma_frm_buf_ceiling_addr_1 =
1592 lcdc_read(LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1593 reg_context.raster_ctrl = lcdc_read(LCD_RASTER_CTRL_REG);
1594 return;
1595}
1596
1597static void lcd_context_restore(void)
1598{
1599 if (lcd_revision == LCD_VERSION_2) {
1600 lcdc_write(reg_context.clk_enable, LCD_CLK_ENABLE_REG);
1601 lcdc_write(reg_context.int_enable_set, LCD_INT_ENABLE_SET_REG);
1602 }
1603
1604 lcdc_write(reg_context.ctrl, LCD_CTRL_REG);
1605 lcdc_write(reg_context.dma_ctrl, LCD_DMA_CTRL_REG);
1606 lcdc_write(reg_context.raster_timing_0, LCD_RASTER_TIMING_0_REG);
1607 lcdc_write(reg_context.raster_timing_1, LCD_RASTER_TIMING_1_REG);
1608 lcdc_write(reg_context.raster_timing_2, LCD_RASTER_TIMING_2_REG);
1609 lcdc_write(reg_context.dma_frm_buf_base_addr_0,
1610 LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
1611 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_0,
1612 LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
1613 lcdc_write(reg_context.dma_frm_buf_base_addr_1,
1614 LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
1615 lcdc_write(reg_context.dma_frm_buf_ceiling_addr_1,
1616 LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
1617 lcdc_write(reg_context.raster_ctrl, LCD_RASTER_CTRL_REG);
1618 return;
1619}
1620
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301621static int fb_suspend(struct device *dev)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001622{
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301623 struct fb_info *info = dev_get_drvdata(dev);
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001624 struct da8xx_fb_par *par = info->par;
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001625 int ret;
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001626
Torben Hohnac751ef2011-01-25 15:07:35 -08001627 console_lock();
Bartosz Golaszewski3fca9e02019-07-22 15:44:20 +02001628 if (par->lcd_supply) {
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001629 ret = regulator_disable(par->lcd_supply);
1630 if (ret)
1631 return ret;
1632 }
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001633
1634 fb_set_suspend(info, 1);
Darren Etheridge26e71642013-08-05 17:02:30 -05001635 lcd_disable_raster(DA8XX_FRAME_WAIT);
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301636 lcd_context_save();
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301637 pm_runtime_put_sync(dev);
Torben Hohnac751ef2011-01-25 15:07:35 -08001638 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001639
1640 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001641}
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301642static int fb_resume(struct device *dev)
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001643{
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301644 struct fb_info *info = dev_get_drvdata(dev);
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001645 struct da8xx_fb_par *par = info->par;
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001646 int ret;
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001647
Torben Hohnac751ef2011-01-25 15:07:35 -08001648 console_lock();
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301649 pm_runtime_get_sync(dev);
Manjunathappa, Prakash7a93cbb2012-09-25 19:41:41 +05301650 lcd_context_restore();
Manjunathappa, Prakash67900812012-08-31 19:48:59 +05301651 if (par->blank == FB_BLANK_UNBLANK) {
1652 lcd_enable_raster();
Manjunathappa, Prakashf7c848b2012-07-24 09:45:25 +05301653
Bartosz Golaszewski3fca9e02019-07-22 15:44:20 +02001654 if (par->lcd_supply) {
Bartosz Golaszewski611097d2019-07-22 15:44:18 +02001655 ret = regulator_enable(par->lcd_supply);
1656 if (ret)
1657 return ret;
1658 }
Manjunathappa, Prakash67900812012-08-31 19:48:59 +05301659 }
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001660
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001661 fb_set_suspend(info, 0);
Torben Hohnac751ef2011-01-25 15:07:35 -08001662 console_unlock();
Chaithrika U S1d3c6c72009-12-15 16:46:39 -08001663
1664 return 0;
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001665}
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001666#endif
1667
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301668static SIMPLE_DEV_PM_OPS(fb_pm_ops, fb_suspend, fb_resume);
1669
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001670static struct platform_driver da8xx_fb_driver = {
1671 .probe = fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001672 .remove = fb_remove,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001673 .driver = {
1674 .name = DRIVER_NAME,
Manish Badarkhe622f7c92014-02-18 08:55:36 +05301675 .pm = &fb_pm_ops,
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001676 },
1677};
Hanjun Guo44f544f2013-09-29 13:32:05 +08001678module_platform_driver(da8xx_fb_driver);
Sudhakar Rajashekhara4ed824d2009-09-22 16:47:06 -07001679
1680MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
1681MODULE_AUTHOR("Texas Instruments");
1682MODULE_LICENSE("GPL");