da8xx-fb.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  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>
  29. #include <linux/interrupt.h>
  30. #include <linux/clk.h>
  31. #include <linux/cpufreq.h>
  32. #include <linux/console.h>
  33. #include <linux/slab.h>
  34. #include <video/da8xx-fb.h>
  35. #define DRIVER_NAME "da8xx_lcdc"
  36. /* LCD Status Register */
  37. #define LCD_END_OF_FRAME1 BIT(9)
  38. #define LCD_END_OF_FRAME0 BIT(8)
  39. #define LCD_PL_LOAD_DONE BIT(6)
  40. #define LCD_FIFO_UNDERFLOW BIT(5)
  41. #define LCD_SYNC_LOST BIT(2)
  42. /* LCD DMA Control Register */
  43. #define LCD_DMA_BURST_SIZE(x) ((x) << 4)
  44. #define LCD_DMA_BURST_1 0x0
  45. #define LCD_DMA_BURST_2 0x1
  46. #define LCD_DMA_BURST_4 0x2
  47. #define LCD_DMA_BURST_8 0x3
  48. #define LCD_DMA_BURST_16 0x4
  49. #define LCD_END_OF_FRAME_INT_ENA BIT(2)
  50. #define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
  51. /* LCD Control Register */
  52. #define LCD_CLK_DIVISOR(x) ((x) << 8)
  53. #define LCD_RASTER_MODE 0x01
  54. /* LCD Raster Control Register */
  55. #define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
  56. #define PALETTE_AND_DATA 0x00
  57. #define PALETTE_ONLY 0x01
  58. #define DATA_ONLY 0x02
  59. #define LCD_MONO_8BIT_MODE BIT(9)
  60. #define LCD_RASTER_ORDER BIT(8)
  61. #define LCD_TFT_MODE BIT(7)
  62. #define LCD_UNDERFLOW_INT_ENA BIT(6)
  63. #define LCD_PL_ENABLE BIT(4)
  64. #define LCD_MONOCHROME_MODE BIT(1)
  65. #define LCD_RASTER_ENABLE BIT(0)
  66. #define LCD_TFT_ALT_ENABLE BIT(23)
  67. #define LCD_STN_565_ENABLE BIT(24)
  68. /* LCD Raster Timing 2 Register */
  69. #define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
  70. #define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
  71. #define LCD_SYNC_CTRL BIT(25)
  72. #define LCD_SYNC_EDGE BIT(24)
  73. #define LCD_INVERT_PIXEL_CLOCK BIT(22)
  74. #define LCD_INVERT_LINE_CLOCK BIT(21)
  75. #define LCD_INVERT_FRAME_CLOCK BIT(20)
  76. /* LCD Block */
  77. #define LCD_CTRL_REG 0x4
  78. #define LCD_STAT_REG 0x8
  79. #define LCD_RASTER_CTRL_REG 0x28
  80. #define LCD_RASTER_TIMING_0_REG 0x2C
  81. #define LCD_RASTER_TIMING_1_REG 0x30
  82. #define LCD_RASTER_TIMING_2_REG 0x34
  83. #define LCD_DMA_CTRL_REG 0x40
  84. #define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
  85. #define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
  86. #define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
  87. #define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
  88. #define LCD_NUM_BUFFERS 2
  89. #define WSI_TIMEOUT 50
  90. #define PALETTE_SIZE 256
  91. #define LEFT_MARGIN 64
  92. #define RIGHT_MARGIN 64
  93. #define UPPER_MARGIN 32
  94. #define LOWER_MARGIN 32
  95. static resource_size_t da8xx_fb_reg_base;
  96. static struct resource *lcdc_regs;
  97. static inline unsigned int lcdc_read(unsigned int addr)
  98. {
  99. return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
  100. }
  101. static inline void lcdc_write(unsigned int val, unsigned int addr)
  102. {
  103. __raw_writel(val, da8xx_fb_reg_base + (addr));
  104. }
  105. struct da8xx_fb_par {
  106. resource_size_t p_palette_base;
  107. unsigned char *v_palette_base;
  108. dma_addr_t vram_phys;
  109. unsigned long vram_size;
  110. void *vram_virt;
  111. unsigned int dma_start;
  112. unsigned int dma_end;
  113. struct clk *lcdc_clk;
  114. int irq;
  115. unsigned short pseudo_palette[16];
  116. unsigned int palette_sz;
  117. unsigned int pxl_clk;
  118. int blank;
  119. wait_queue_head_t vsync_wait;
  120. int vsync_flag;
  121. int vsync_timeout;
  122. #ifdef CONFIG_CPU_FREQ
  123. struct notifier_block freq_transition;
  124. #endif
  125. void (*panel_power_ctrl)(int);
  126. };
  127. /* Variable Screen Information */
  128. static struct fb_var_screeninfo da8xx_fb_var __devinitdata = {
  129. .xoffset = 0,
  130. .yoffset = 0,
  131. .transp = {0, 0, 0},
  132. .nonstd = 0,
  133. .activate = 0,
  134. .height = -1,
  135. .width = -1,
  136. .pixclock = 46666, /* 46us - AUO display */
  137. .accel_flags = 0,
  138. .left_margin = LEFT_MARGIN,
  139. .right_margin = RIGHT_MARGIN,
  140. .upper_margin = UPPER_MARGIN,
  141. .lower_margin = LOWER_MARGIN,
  142. .sync = 0,
  143. .vmode = FB_VMODE_NONINTERLACED
  144. };
  145. static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
  146. .id = "DA8xx FB Drv",
  147. .type = FB_TYPE_PACKED_PIXELS,
  148. .type_aux = 0,
  149. .visual = FB_VISUAL_PSEUDOCOLOR,
  150. .xpanstep = 0,
  151. .ypanstep = 1,
  152. .ywrapstep = 0,
  153. .accel = FB_ACCEL_NONE
  154. };
  155. struct da8xx_panel {
  156. const char name[25]; /* Full name <vendor>_<model> */
  157. unsigned short width;
  158. unsigned short height;
  159. int hfp; /* Horizontal front porch */
  160. int hbp; /* Horizontal back porch */
  161. int hsw; /* Horizontal Sync Pulse Width */
  162. int vfp; /* Vertical front porch */
  163. int vbp; /* Vertical back porch */
  164. int vsw; /* Vertical Sync Pulse Width */
  165. unsigned int pxl_clk; /* Pixel clock */
  166. unsigned char invert_pxl_clk; /* Invert Pixel clock */
  167. };
  168. static struct da8xx_panel known_lcd_panels[] = {
  169. /* Sharp LCD035Q3DG01 */
  170. [0] = {
  171. .name = "Sharp_LCD035Q3DG01",
  172. .width = 320,
  173. .height = 240,
  174. .hfp = 8,
  175. .hbp = 6,
  176. .hsw = 0,
  177. .vfp = 2,
  178. .vbp = 2,
  179. .vsw = 0,
  180. .pxl_clk = 4608000,
  181. .invert_pxl_clk = 1,
  182. },
  183. /* Sharp LK043T1DG01 */
  184. [1] = {
  185. .name = "Sharp_LK043T1DG01",
  186. .width = 480,
  187. .height = 272,
  188. .hfp = 2,
  189. .hbp = 2,
  190. .hsw = 41,
  191. .vfp = 2,
  192. .vbp = 2,
  193. .vsw = 10,
  194. .pxl_clk = 7833600,
  195. .invert_pxl_clk = 0,
  196. },
  197. };
  198. /* Enable the Raster Engine of the LCD Controller */
  199. static inline void lcd_enable_raster(void)
  200. {
  201. u32 reg;
  202. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  203. if (!(reg & LCD_RASTER_ENABLE))
  204. lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  205. }
  206. /* Disable the Raster Engine of the LCD Controller */
  207. static inline void lcd_disable_raster(void)
  208. {
  209. u32 reg;
  210. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  211. if (reg & LCD_RASTER_ENABLE)
  212. lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  213. }
  214. static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
  215. {
  216. u32 start;
  217. u32 end;
  218. u32 reg_ras;
  219. u32 reg_dma;
  220. /* init reg to clear PLM (loading mode) fields */
  221. reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
  222. reg_ras &= ~(3 << 20);
  223. reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
  224. if (load_mode == LOAD_DATA) {
  225. start = par->dma_start;
  226. end = par->dma_end;
  227. reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
  228. reg_dma |= LCD_END_OF_FRAME_INT_ENA;
  229. reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
  230. lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  231. lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  232. lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  233. lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  234. } else if (load_mode == LOAD_PALETTE) {
  235. start = par->p_palette_base;
  236. end = start + par->palette_sz - 1;
  237. reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
  238. reg_ras |= LCD_PL_ENABLE;
  239. lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  240. lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  241. }
  242. lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
  243. lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
  244. /*
  245. * The Raster enable bit must be set after all other control fields are
  246. * set.
  247. */
  248. lcd_enable_raster();
  249. }
  250. /* Configure the Burst Size of DMA */
  251. static int lcd_cfg_dma(int burst_size)
  252. {
  253. u32 reg;
  254. reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
  255. switch (burst_size) {
  256. case 1:
  257. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
  258. break;
  259. case 2:
  260. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
  261. break;
  262. case 4:
  263. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
  264. break;
  265. case 8:
  266. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
  267. break;
  268. case 16:
  269. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
  270. break;
  271. default:
  272. return -EINVAL;
  273. }
  274. lcdc_write(reg, LCD_DMA_CTRL_REG);
  275. return 0;
  276. }
  277. static void lcd_cfg_ac_bias(int period, int transitions_per_int)
  278. {
  279. u32 reg;
  280. /* Set the AC Bias Period and Number of Transisitons per Interrupt */
  281. reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
  282. reg |= LCD_AC_BIAS_FREQUENCY(period) |
  283. LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
  284. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  285. }
  286. static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
  287. int front_porch)
  288. {
  289. u32 reg;
  290. reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
  291. reg |= ((back_porch & 0xff) << 24)
  292. | ((front_porch & 0xff) << 16)
  293. | ((pulse_width & 0x3f) << 10);
  294. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  295. }
  296. static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
  297. int front_porch)
  298. {
  299. u32 reg;
  300. reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
  301. reg |= ((back_porch & 0xff) << 24)
  302. | ((front_porch & 0xff) << 16)
  303. | ((pulse_width & 0x3f) << 10);
  304. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  305. }
  306. static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
  307. {
  308. u32 reg;
  309. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
  310. LCD_MONO_8BIT_MODE |
  311. LCD_MONOCHROME_MODE);
  312. switch (cfg->p_disp_panel->panel_shade) {
  313. case MONOCHROME:
  314. reg |= LCD_MONOCHROME_MODE;
  315. if (cfg->mono_8bit_mode)
  316. reg |= LCD_MONO_8BIT_MODE;
  317. break;
  318. case COLOR_ACTIVE:
  319. reg |= LCD_TFT_MODE;
  320. if (cfg->tft_alt_mode)
  321. reg |= LCD_TFT_ALT_ENABLE;
  322. break;
  323. case COLOR_PASSIVE:
  324. if (cfg->stn_565_mode)
  325. reg |= LCD_STN_565_ENABLE;
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. /* enable additional interrupts here */
  331. reg |= LCD_UNDERFLOW_INT_ENA;
  332. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  333. reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
  334. if (cfg->sync_ctrl)
  335. reg |= LCD_SYNC_CTRL;
  336. else
  337. reg &= ~LCD_SYNC_CTRL;
  338. if (cfg->sync_edge)
  339. reg |= LCD_SYNC_EDGE;
  340. else
  341. reg &= ~LCD_SYNC_EDGE;
  342. if (cfg->invert_line_clock)
  343. reg |= LCD_INVERT_LINE_CLOCK;
  344. else
  345. reg &= ~LCD_INVERT_LINE_CLOCK;
  346. if (cfg->invert_frm_clock)
  347. reg |= LCD_INVERT_FRAME_CLOCK;
  348. else
  349. reg &= ~LCD_INVERT_FRAME_CLOCK;
  350. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  351. return 0;
  352. }
  353. static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
  354. u32 bpp, u32 raster_order)
  355. {
  356. u32 reg;
  357. /* Set the Panel Width */
  358. /* Pixels per line = (PPL + 1)*16 */
  359. /*0x3F in bits 4..9 gives max horisontal resolution = 1024 pixels*/
  360. width &= 0x3f0;
  361. reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
  362. reg &= 0xfffffc00;
  363. reg |= ((width >> 4) - 1) << 4;
  364. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  365. /* Set the Panel Height */
  366. reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
  367. reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
  368. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  369. /* Set the Raster Order of the Frame Buffer */
  370. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
  371. if (raster_order)
  372. reg |= LCD_RASTER_ORDER;
  373. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  374. switch (bpp) {
  375. case 1:
  376. case 2:
  377. case 4:
  378. case 16:
  379. par->palette_sz = 16 * 2;
  380. break;
  381. case 8:
  382. par->palette_sz = 256 * 2;
  383. break;
  384. default:
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  390. unsigned blue, unsigned transp,
  391. struct fb_info *info)
  392. {
  393. struct da8xx_fb_par *par = info->par;
  394. unsigned short *palette = (unsigned short *) par->v_palette_base;
  395. u_short pal;
  396. int update_hw = 0;
  397. if (regno > 255)
  398. return 1;
  399. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  400. return 1;
  401. if (info->var.bits_per_pixel == 8) {
  402. red >>= 4;
  403. green >>= 8;
  404. blue >>= 12;
  405. pal = (red & 0x0f00);
  406. pal |= (green & 0x00f0);
  407. pal |= (blue & 0x000f);
  408. if (palette[regno] != pal) {
  409. update_hw = 1;
  410. palette[regno] = pal;
  411. }
  412. } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
  413. red >>= (16 - info->var.red.length);
  414. red <<= info->var.red.offset;
  415. green >>= (16 - info->var.green.length);
  416. green <<= info->var.green.offset;
  417. blue >>= (16 - info->var.blue.length);
  418. blue <<= info->var.blue.offset;
  419. par->pseudo_palette[regno] = red | green | blue;
  420. if (palette[0] != 0x4000) {
  421. update_hw = 1;
  422. palette[0] = 0x4000;
  423. }
  424. }
  425. /* Update the palette in the h/w as needed. */
  426. if (update_hw)
  427. lcd_blit(LOAD_PALETTE, par);
  428. return 0;
  429. }
  430. static void lcd_reset(struct da8xx_fb_par *par)
  431. {
  432. /* Disable the Raster if previously Enabled */
  433. lcd_disable_raster();
  434. /* DMA has to be disabled */
  435. lcdc_write(0, LCD_DMA_CTRL_REG);
  436. lcdc_write(0, LCD_RASTER_CTRL_REG);
  437. }
  438. static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
  439. {
  440. unsigned int lcd_clk, div;
  441. lcd_clk = clk_get_rate(par->lcdc_clk);
  442. div = lcd_clk / par->pxl_clk;
  443. /* Configure the LCD clock divisor. */
  444. lcdc_write(LCD_CLK_DIVISOR(div) |
  445. (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
  446. }
  447. static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
  448. struct da8xx_panel *panel)
  449. {
  450. u32 bpp;
  451. int ret = 0;
  452. lcd_reset(par);
  453. /* Calculate the divider */
  454. lcd_calc_clk_divider(par);
  455. if (panel->invert_pxl_clk)
  456. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
  457. LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  458. else
  459. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
  460. ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  461. /* Configure the DMA burst size. */
  462. ret = lcd_cfg_dma(cfg->dma_burst_sz);
  463. if (ret < 0)
  464. return ret;
  465. /* Configure the AC bias properties. */
  466. lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
  467. /* Configure the vertical and horizontal sync properties. */
  468. lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
  469. lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
  470. /* Configure for disply */
  471. ret = lcd_cfg_display(cfg);
  472. if (ret < 0)
  473. return ret;
  474. if (QVGA != cfg->p_disp_panel->panel_type)
  475. return -EINVAL;
  476. if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
  477. cfg->bpp >= cfg->p_disp_panel->min_bpp)
  478. bpp = cfg->bpp;
  479. else
  480. bpp = cfg->p_disp_panel->max_bpp;
  481. if (bpp == 12)
  482. bpp = 16;
  483. ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
  484. (unsigned int)panel->height, bpp,
  485. cfg->raster_order);
  486. if (ret < 0)
  487. return ret;
  488. /* Configure FDD */
  489. lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
  490. (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
  491. return 0;
  492. }
  493. static irqreturn_t lcdc_irq_handler(int irq, void *arg)
  494. {
  495. struct da8xx_fb_par *par = arg;
  496. u32 stat = lcdc_read(LCD_STAT_REG);
  497. u32 reg_ras;
  498. if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
  499. lcd_disable_raster();
  500. lcdc_write(stat, LCD_STAT_REG);
  501. lcd_enable_raster();
  502. } else if (stat & LCD_PL_LOAD_DONE) {
  503. /*
  504. * Must disable raster before changing state of any control bit.
  505. * And also must be disabled before clearing the PL loading
  506. * interrupt via the following write to the status register. If
  507. * this is done after then one gets multiple PL done interrupts.
  508. */
  509. lcd_disable_raster();
  510. lcdc_write(stat, LCD_STAT_REG);
  511. /* Disable PL completion inerrupt */
  512. reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
  513. reg_ras &= ~LCD_PL_ENABLE;
  514. lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
  515. /* Setup and start data loading mode */
  516. lcd_blit(LOAD_DATA, par);
  517. } else {
  518. lcdc_write(stat, LCD_STAT_REG);
  519. if (stat & LCD_END_OF_FRAME0) {
  520. lcdc_write(par->dma_start,
  521. LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  522. lcdc_write(par->dma_end,
  523. LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  524. par->vsync_flag = 1;
  525. wake_up_interruptible(&par->vsync_wait);
  526. }
  527. if (stat & LCD_END_OF_FRAME1) {
  528. lcdc_write(par->dma_start,
  529. LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  530. lcdc_write(par->dma_end,
  531. LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  532. par->vsync_flag = 1;
  533. wake_up_interruptible(&par->vsync_wait);
  534. }
  535. }
  536. return IRQ_HANDLED;
  537. }
  538. static int fb_check_var(struct fb_var_screeninfo *var,
  539. struct fb_info *info)
  540. {
  541. int err = 0;
  542. switch (var->bits_per_pixel) {
  543. case 1:
  544. case 8:
  545. var->red.offset = 0;
  546. var->red.length = 8;
  547. var->green.offset = 0;
  548. var->green.length = 8;
  549. var->blue.offset = 0;
  550. var->blue.length = 8;
  551. var->transp.offset = 0;
  552. var->transp.length = 0;
  553. break;
  554. case 4:
  555. var->red.offset = 0;
  556. var->red.length = 4;
  557. var->green.offset = 0;
  558. var->green.length = 4;
  559. var->blue.offset = 0;
  560. var->blue.length = 4;
  561. var->transp.offset = 0;
  562. var->transp.length = 0;
  563. break;
  564. case 16: /* RGB 565 */
  565. var->red.offset = 11;
  566. var->red.length = 5;
  567. var->green.offset = 5;
  568. var->green.length = 6;
  569. var->blue.offset = 0;
  570. var->blue.length = 5;
  571. var->transp.offset = 0;
  572. var->transp.length = 0;
  573. break;
  574. default:
  575. err = -EINVAL;
  576. }
  577. var->red.msb_right = 0;
  578. var->green.msb_right = 0;
  579. var->blue.msb_right = 0;
  580. var->transp.msb_right = 0;
  581. return err;
  582. }
  583. #ifdef CONFIG_CPU_FREQ
  584. static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
  585. unsigned long val, void *data)
  586. {
  587. struct da8xx_fb_par *par;
  588. par = container_of(nb, struct da8xx_fb_par, freq_transition);
  589. if (val == CPUFREQ_PRECHANGE) {
  590. lcd_disable_raster();
  591. } else if (val == CPUFREQ_POSTCHANGE) {
  592. lcd_calc_clk_divider(par);
  593. lcd_enable_raster();
  594. }
  595. return 0;
  596. }
  597. static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
  598. {
  599. par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
  600. return cpufreq_register_notifier(&par->freq_transition,
  601. CPUFREQ_TRANSITION_NOTIFIER);
  602. }
  603. static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
  604. {
  605. cpufreq_unregister_notifier(&par->freq_transition,
  606. CPUFREQ_TRANSITION_NOTIFIER);
  607. }
  608. #endif
  609. static int __devexit fb_remove(struct platform_device *dev)
  610. {
  611. struct fb_info *info = dev_get_drvdata(&dev->dev);
  612. if (info) {
  613. struct da8xx_fb_par *par = info->par;
  614. #ifdef CONFIG_CPU_FREQ
  615. lcd_da8xx_cpufreq_deregister(par);
  616. #endif
  617. if (par->panel_power_ctrl)
  618. par->panel_power_ctrl(0);
  619. lcd_disable_raster();
  620. lcdc_write(0, LCD_RASTER_CTRL_REG);
  621. /* disable DMA */
  622. lcdc_write(0, LCD_DMA_CTRL_REG);
  623. unregister_framebuffer(info);
  624. fb_dealloc_cmap(&info->cmap);
  625. dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
  626. par->p_palette_base);
  627. dma_free_coherent(NULL, par->vram_size, par->vram_virt,
  628. par->vram_phys);
  629. free_irq(par->irq, par);
  630. clk_disable(par->lcdc_clk);
  631. clk_put(par->lcdc_clk);
  632. framebuffer_release(info);
  633. iounmap((void __iomem *)da8xx_fb_reg_base);
  634. release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
  635. }
  636. return 0;
  637. }
  638. /*
  639. * Function to wait for vertical sync which for this LCD peripheral
  640. * translates into waiting for the current raster frame to complete.
  641. */
  642. static int fb_wait_for_vsync(struct fb_info *info)
  643. {
  644. struct da8xx_fb_par *par = info->par;
  645. int ret;
  646. /*
  647. * Set flag to 0 and wait for isr to set to 1. It would seem there is a
  648. * race condition here where the ISR could have occurred just before or
  649. * just after this set. But since we are just coarsely waiting for
  650. * a frame to complete then that's OK. i.e. if the frame completed
  651. * just before this code executed then we have to wait another full
  652. * frame time but there is no way to avoid such a situation. On the
  653. * other hand if the frame completed just after then we don't need
  654. * to wait long at all. Either way we are guaranteed to return to the
  655. * user immediately after a frame completion which is all that is
  656. * required.
  657. */
  658. par->vsync_flag = 0;
  659. ret = wait_event_interruptible_timeout(par->vsync_wait,
  660. par->vsync_flag != 0,
  661. par->vsync_timeout);
  662. if (ret < 0)
  663. return ret;
  664. if (ret == 0)
  665. return -ETIMEDOUT;
  666. return 0;
  667. }
  668. static int fb_ioctl(struct fb_info *info, unsigned int cmd,
  669. unsigned long arg)
  670. {
  671. struct lcd_sync_arg sync_arg;
  672. switch (cmd) {
  673. case FBIOGET_CONTRAST:
  674. case FBIOPUT_CONTRAST:
  675. case FBIGET_BRIGHTNESS:
  676. case FBIPUT_BRIGHTNESS:
  677. case FBIGET_COLOR:
  678. case FBIPUT_COLOR:
  679. return -ENOTTY;
  680. case FBIPUT_HSYNC:
  681. if (copy_from_user(&sync_arg, (char *)arg,
  682. sizeof(struct lcd_sync_arg)))
  683. return -EFAULT;
  684. lcd_cfg_horizontal_sync(sync_arg.back_porch,
  685. sync_arg.pulse_width,
  686. sync_arg.front_porch);
  687. break;
  688. case FBIPUT_VSYNC:
  689. if (copy_from_user(&sync_arg, (char *)arg,
  690. sizeof(struct lcd_sync_arg)))
  691. return -EFAULT;
  692. lcd_cfg_vertical_sync(sync_arg.back_porch,
  693. sync_arg.pulse_width,
  694. sync_arg.front_porch);
  695. break;
  696. case FBIO_WAITFORVSYNC:
  697. return fb_wait_for_vsync(info);
  698. default:
  699. return -EINVAL;
  700. }
  701. return 0;
  702. }
  703. static int cfb_blank(int blank, struct fb_info *info)
  704. {
  705. struct da8xx_fb_par *par = info->par;
  706. int ret = 0;
  707. if (par->blank == blank)
  708. return 0;
  709. par->blank = blank;
  710. switch (blank) {
  711. case FB_BLANK_UNBLANK:
  712. if (par->panel_power_ctrl)
  713. par->panel_power_ctrl(1);
  714. lcd_enable_raster();
  715. break;
  716. case FB_BLANK_POWERDOWN:
  717. if (par->panel_power_ctrl)
  718. par->panel_power_ctrl(0);
  719. lcd_disable_raster();
  720. break;
  721. default:
  722. ret = -EINVAL;
  723. }
  724. return ret;
  725. }
  726. /*
  727. * Set new x,y offsets in the virtual display for the visible area and switch
  728. * to the new mode.
  729. */
  730. static int da8xx_pan_display(struct fb_var_screeninfo *var,
  731. struct fb_info *fbi)
  732. {
  733. int ret = 0;
  734. struct fb_var_screeninfo new_var;
  735. struct da8xx_fb_par *par = fbi->par;
  736. struct fb_fix_screeninfo *fix = &fbi->fix;
  737. unsigned int end;
  738. unsigned int start;
  739. if (var->xoffset != fbi->var.xoffset ||
  740. var->yoffset != fbi->var.yoffset) {
  741. memcpy(&new_var, &fbi->var, sizeof(new_var));
  742. new_var.xoffset = var->xoffset;
  743. new_var.yoffset = var->yoffset;
  744. if (fb_check_var(&new_var, fbi))
  745. ret = -EINVAL;
  746. else {
  747. memcpy(&fbi->var, &new_var, sizeof(new_var));
  748. start = fix->smem_start +
  749. new_var.yoffset * fix->line_length +
  750. new_var.xoffset * var->bits_per_pixel / 8;
  751. end = start + var->yres * fix->line_length - 1;
  752. par->dma_start = start;
  753. par->dma_end = end;
  754. }
  755. }
  756. return ret;
  757. }
  758. static struct fb_ops da8xx_fb_ops = {
  759. .owner = THIS_MODULE,
  760. .fb_check_var = fb_check_var,
  761. .fb_setcolreg = fb_setcolreg,
  762. .fb_pan_display = da8xx_pan_display,
  763. .fb_ioctl = fb_ioctl,
  764. .fb_fillrect = cfb_fillrect,
  765. .fb_copyarea = cfb_copyarea,
  766. .fb_imageblit = cfb_imageblit,
  767. .fb_blank = cfb_blank,
  768. };
  769. static int __devinit fb_probe(struct platform_device *device)
  770. {
  771. struct da8xx_lcdc_platform_data *fb_pdata =
  772. device->dev.platform_data;
  773. struct lcd_ctrl_config *lcd_cfg;
  774. struct da8xx_panel *lcdc_info;
  775. struct fb_info *da8xx_fb_info;
  776. struct clk *fb_clk = NULL;
  777. struct da8xx_fb_par *par;
  778. resource_size_t len;
  779. int ret, i;
  780. if (fb_pdata == NULL) {
  781. dev_err(&device->dev, "Can not get platform data\n");
  782. return -ENOENT;
  783. }
  784. lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
  785. if (!lcdc_regs) {
  786. dev_err(&device->dev,
  787. "Can not get memory resource for LCD controller\n");
  788. return -ENOENT;
  789. }
  790. len = resource_size(lcdc_regs);
  791. lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
  792. if (!lcdc_regs)
  793. return -EBUSY;
  794. da8xx_fb_reg_base = (resource_size_t)ioremap(lcdc_regs->start, len);
  795. if (!da8xx_fb_reg_base) {
  796. ret = -EBUSY;
  797. goto err_request_mem;
  798. }
  799. fb_clk = clk_get(&device->dev, NULL);
  800. if (IS_ERR(fb_clk)) {
  801. dev_err(&device->dev, "Can not get device clock\n");
  802. ret = -ENODEV;
  803. goto err_ioremap;
  804. }
  805. ret = clk_enable(fb_clk);
  806. if (ret)
  807. goto err_clk_put;
  808. for (i = 0, lcdc_info = known_lcd_panels;
  809. i < ARRAY_SIZE(known_lcd_panels);
  810. i++, lcdc_info++) {
  811. if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
  812. break;
  813. }
  814. if (i == ARRAY_SIZE(known_lcd_panels)) {
  815. dev_err(&device->dev, "GLCD: No valid panel found\n");
  816. ret = -ENODEV;
  817. goto err_clk_disable;
  818. } else
  819. dev_info(&device->dev, "GLCD: Found %s panel\n",
  820. fb_pdata->type);
  821. lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
  822. da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
  823. &device->dev);
  824. if (!da8xx_fb_info) {
  825. dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
  826. ret = -ENOMEM;
  827. goto err_clk_disable;
  828. }
  829. par = da8xx_fb_info->par;
  830. par->lcdc_clk = fb_clk;
  831. par->pxl_clk = lcdc_info->pxl_clk;
  832. if (fb_pdata->panel_power_ctrl) {
  833. par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
  834. par->panel_power_ctrl(1);
  835. }
  836. if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
  837. dev_err(&device->dev, "lcd_init failed\n");
  838. ret = -EFAULT;
  839. goto err_release_fb;
  840. }
  841. /* allocate frame buffer */
  842. par->vram_size = lcdc_info->width * lcdc_info->height * lcd_cfg->bpp;
  843. par->vram_size = PAGE_ALIGN(par->vram_size/8);
  844. par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
  845. par->vram_virt = dma_alloc_coherent(NULL,
  846. par->vram_size,
  847. (resource_size_t *) &par->vram_phys,
  848. GFP_KERNEL | GFP_DMA);
  849. if (!par->vram_virt) {
  850. dev_err(&device->dev,
  851. "GLCD: kmalloc for frame buffer failed\n");
  852. ret = -EINVAL;
  853. goto err_release_fb;
  854. }
  855. da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
  856. da8xx_fb_fix.smem_start = par->vram_phys;
  857. da8xx_fb_fix.smem_len = par->vram_size;
  858. da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
  859. par->dma_start = par->vram_phys;
  860. par->dma_end = par->dma_start + lcdc_info->height *
  861. da8xx_fb_fix.line_length - 1;
  862. /* allocate palette buffer */
  863. par->v_palette_base = dma_alloc_coherent(NULL,
  864. PALETTE_SIZE,
  865. (resource_size_t *)
  866. &par->p_palette_base,
  867. GFP_KERNEL | GFP_DMA);
  868. if (!par->v_palette_base) {
  869. dev_err(&device->dev,
  870. "GLCD: kmalloc for palette buffer failed\n");
  871. ret = -EINVAL;
  872. goto err_release_fb_mem;
  873. }
  874. memset(par->v_palette_base, 0, PALETTE_SIZE);
  875. par->irq = platform_get_irq(device, 0);
  876. if (par->irq < 0) {
  877. ret = -ENOENT;
  878. goto err_release_pl_mem;
  879. }
  880. /* Initialize par */
  881. da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
  882. da8xx_fb_var.xres = lcdc_info->width;
  883. da8xx_fb_var.xres_virtual = lcdc_info->width;
  884. da8xx_fb_var.yres = lcdc_info->height;
  885. da8xx_fb_var.yres_virtual = lcdc_info->height * LCD_NUM_BUFFERS;
  886. da8xx_fb_var.grayscale =
  887. lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
  888. da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
  889. da8xx_fb_var.hsync_len = lcdc_info->hsw;
  890. da8xx_fb_var.vsync_len = lcdc_info->vsw;
  891. /* Initialize fbinfo */
  892. da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
  893. da8xx_fb_info->fix = da8xx_fb_fix;
  894. da8xx_fb_info->var = da8xx_fb_var;
  895. da8xx_fb_info->fbops = &da8xx_fb_ops;
  896. da8xx_fb_info->pseudo_palette = par->pseudo_palette;
  897. da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
  898. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  899. ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
  900. if (ret)
  901. goto err_release_pl_mem;
  902. da8xx_fb_info->cmap.len = par->palette_sz;
  903. /* initialize var_screeninfo */
  904. da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
  905. fb_set_var(da8xx_fb_info, &da8xx_fb_var);
  906. dev_set_drvdata(&device->dev, da8xx_fb_info);
  907. /* initialize the vsync wait queue */
  908. init_waitqueue_head(&par->vsync_wait);
  909. par->vsync_timeout = HZ / 5;
  910. /* Register the Frame Buffer */
  911. if (register_framebuffer(da8xx_fb_info) < 0) {
  912. dev_err(&device->dev,
  913. "GLCD: Frame Buffer Registration Failed!\n");
  914. ret = -EINVAL;
  915. goto err_dealloc_cmap;
  916. }
  917. #ifdef CONFIG_CPU_FREQ
  918. ret = lcd_da8xx_cpufreq_register(par);
  919. if (ret) {
  920. dev_err(&device->dev, "failed to register cpufreq\n");
  921. goto err_cpu_freq;
  922. }
  923. #endif
  924. ret = request_irq(par->irq, lcdc_irq_handler, 0, DRIVER_NAME, par);
  925. if (ret)
  926. goto irq_freq;
  927. return 0;
  928. irq_freq:
  929. #ifdef CONFIG_CPU_FREQ
  930. lcd_da8xx_cpufreq_deregister(par);
  931. #endif
  932. err_cpu_freq:
  933. unregister_framebuffer(da8xx_fb_info);
  934. err_dealloc_cmap:
  935. fb_dealloc_cmap(&da8xx_fb_info->cmap);
  936. err_release_pl_mem:
  937. dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
  938. par->p_palette_base);
  939. err_release_fb_mem:
  940. dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
  941. err_release_fb:
  942. framebuffer_release(da8xx_fb_info);
  943. err_clk_disable:
  944. clk_disable(fb_clk);
  945. err_clk_put:
  946. clk_put(fb_clk);
  947. err_ioremap:
  948. iounmap((void __iomem *)da8xx_fb_reg_base);
  949. err_request_mem:
  950. release_mem_region(lcdc_regs->start, len);
  951. return ret;
  952. }
  953. #ifdef CONFIG_PM
  954. static int fb_suspend(struct platform_device *dev, pm_message_t state)
  955. {
  956. struct fb_info *info = platform_get_drvdata(dev);
  957. struct da8xx_fb_par *par = info->par;
  958. console_lock();
  959. if (par->panel_power_ctrl)
  960. par->panel_power_ctrl(0);
  961. fb_set_suspend(info, 1);
  962. lcd_disable_raster();
  963. clk_disable(par->lcdc_clk);
  964. console_unlock();
  965. return 0;
  966. }
  967. static int fb_resume(struct platform_device *dev)
  968. {
  969. struct fb_info *info = platform_get_drvdata(dev);
  970. struct da8xx_fb_par *par = info->par;
  971. console_lock();
  972. if (par->panel_power_ctrl)
  973. par->panel_power_ctrl(1);
  974. clk_enable(par->lcdc_clk);
  975. lcd_enable_raster();
  976. fb_set_suspend(info, 0);
  977. console_unlock();
  978. return 0;
  979. }
  980. #else
  981. #define fb_suspend NULL
  982. #define fb_resume NULL
  983. #endif
  984. static struct platform_driver da8xx_fb_driver = {
  985. .probe = fb_probe,
  986. .remove = __devexit_p(fb_remove),
  987. .suspend = fb_suspend,
  988. .resume = fb_resume,
  989. .driver = {
  990. .name = DRIVER_NAME,
  991. .owner = THIS_MODULE,
  992. },
  993. };
  994. static int __init da8xx_fb_init(void)
  995. {
  996. return platform_driver_register(&da8xx_fb_driver);
  997. }
  998. static void __exit da8xx_fb_cleanup(void)
  999. {
  1000. platform_driver_unregister(&da8xx_fb_driver);
  1001. }
  1002. module_init(da8xx_fb_init);
  1003. module_exit(da8xx_fb_cleanup);
  1004. MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
  1005. MODULE_AUTHOR("Texas Instruments");
  1006. MODULE_LICENSE("GPL");