da8xx-fb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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/wait.h>
  31. #include <linux/clk.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/console.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/slab.h>
  36. #include <linux/delay.h>
  37. #include <linux/lcm.h>
  38. #include <video/da8xx-fb.h>
  39. #include <asm/div64.h>
  40. #define DRIVER_NAME "da8xx_lcdc"
  41. #define LCD_VERSION_1 1
  42. #define LCD_VERSION_2 2
  43. /* LCD Status Register */
  44. #define LCD_END_OF_FRAME1 BIT(9)
  45. #define LCD_END_OF_FRAME0 BIT(8)
  46. #define LCD_PL_LOAD_DONE BIT(6)
  47. #define LCD_FIFO_UNDERFLOW BIT(5)
  48. #define LCD_SYNC_LOST BIT(2)
  49. #define LCD_FRAME_DONE BIT(0)
  50. /* LCD DMA Control Register */
  51. #define LCD_DMA_BURST_SIZE(x) ((x) << 4)
  52. #define LCD_DMA_BURST_1 0x0
  53. #define LCD_DMA_BURST_2 0x1
  54. #define LCD_DMA_BURST_4 0x2
  55. #define LCD_DMA_BURST_8 0x3
  56. #define LCD_DMA_BURST_16 0x4
  57. #define LCD_V1_END_OF_FRAME_INT_ENA BIT(2)
  58. #define LCD_V2_END_OF_FRAME0_INT_ENA BIT(8)
  59. #define LCD_V2_END_OF_FRAME1_INT_ENA BIT(9)
  60. #define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
  61. /* LCD Control Register */
  62. #define LCD_CLK_DIVISOR(x) ((x) << 8)
  63. #define LCD_RASTER_MODE 0x01
  64. /* LCD Raster Control Register */
  65. #define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
  66. #define PALETTE_AND_DATA 0x00
  67. #define PALETTE_ONLY 0x01
  68. #define DATA_ONLY 0x02
  69. #define LCD_MONO_8BIT_MODE BIT(9)
  70. #define LCD_RASTER_ORDER BIT(8)
  71. #define LCD_TFT_MODE BIT(7)
  72. #define LCD_V1_UNDERFLOW_INT_ENA BIT(6)
  73. #define LCD_V2_UNDERFLOW_INT_ENA BIT(5)
  74. #define LCD_V1_PL_INT_ENA BIT(4)
  75. #define LCD_V2_PL_INT_ENA BIT(6)
  76. #define LCD_MONOCHROME_MODE BIT(1)
  77. #define LCD_RASTER_ENABLE BIT(0)
  78. #define LCD_TFT_ALT_ENABLE BIT(23)
  79. #define LCD_STN_565_ENABLE BIT(24)
  80. #define LCD_V2_DMA_CLK_EN BIT(2)
  81. #define LCD_V2_LIDD_CLK_EN BIT(1)
  82. #define LCD_V2_CORE_CLK_EN BIT(0)
  83. #define LCD_V2_LPP_B10 26
  84. #define LCD_V2_TFT_24BPP_MODE BIT(25)
  85. #define LCD_V2_TFT_24BPP_UNPACK BIT(26)
  86. /* LCD Raster Timing 2 Register */
  87. #define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
  88. #define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
  89. #define LCD_SYNC_CTRL BIT(25)
  90. #define LCD_SYNC_EDGE BIT(24)
  91. #define LCD_INVERT_PIXEL_CLOCK BIT(22)
  92. #define LCD_INVERT_LINE_CLOCK BIT(21)
  93. #define LCD_INVERT_FRAME_CLOCK BIT(20)
  94. /* LCD Block */
  95. #define LCD_PID_REG 0x0
  96. #define LCD_CTRL_REG 0x4
  97. #define LCD_STAT_REG 0x8
  98. #define LCD_RASTER_CTRL_REG 0x28
  99. #define LCD_RASTER_TIMING_0_REG 0x2C
  100. #define LCD_RASTER_TIMING_1_REG 0x30
  101. #define LCD_RASTER_TIMING_2_REG 0x34
  102. #define LCD_DMA_CTRL_REG 0x40
  103. #define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
  104. #define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
  105. #define LCD_DMA_FRM_BUF_BASE_ADDR_1_REG 0x4C
  106. #define LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG 0x50
  107. /* Interrupt Registers available only in Version 2 */
  108. #define LCD_RAW_STAT_REG 0x58
  109. #define LCD_MASKED_STAT_REG 0x5c
  110. #define LCD_INT_ENABLE_SET_REG 0x60
  111. #define LCD_INT_ENABLE_CLR_REG 0x64
  112. #define LCD_END_OF_INT_IND_REG 0x68
  113. /* Clock registers available only on Version 2 */
  114. #define LCD_CLK_ENABLE_REG 0x6c
  115. #define LCD_CLK_RESET_REG 0x70
  116. #define LCD_CLK_MAIN_RESET BIT(3)
  117. #define LCD_NUM_BUFFERS 2
  118. #define WSI_TIMEOUT 50
  119. #define PALETTE_SIZE 256
  120. #define LEFT_MARGIN 64
  121. #define RIGHT_MARGIN 64
  122. #define UPPER_MARGIN 32
  123. #define LOWER_MARGIN 32
  124. static resource_size_t da8xx_fb_reg_base;
  125. static struct resource *lcdc_regs;
  126. static unsigned int lcd_revision;
  127. static irq_handler_t lcdc_irq_handler;
  128. static wait_queue_head_t frame_done_wq;
  129. static int frame_done_flag;
  130. static inline unsigned int lcdc_read(unsigned int addr)
  131. {
  132. return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
  133. }
  134. static inline void lcdc_write(unsigned int val, unsigned int addr)
  135. {
  136. __raw_writel(val, da8xx_fb_reg_base + (addr));
  137. }
  138. struct da8xx_fb_par {
  139. resource_size_t p_palette_base;
  140. unsigned char *v_palette_base;
  141. dma_addr_t vram_phys;
  142. unsigned long vram_size;
  143. void *vram_virt;
  144. unsigned int dma_start;
  145. unsigned int dma_end;
  146. struct clk *lcdc_clk;
  147. int irq;
  148. unsigned int palette_sz;
  149. unsigned int pxl_clk;
  150. int blank;
  151. wait_queue_head_t vsync_wait;
  152. int vsync_flag;
  153. int vsync_timeout;
  154. spinlock_t lock_for_chan_update;
  155. /*
  156. * LCDC has 2 ping pong DMA channels, channel 0
  157. * and channel 1.
  158. */
  159. unsigned int which_dma_channel_done;
  160. #ifdef CONFIG_CPU_FREQ
  161. struct notifier_block freq_transition;
  162. unsigned int lcd_fck_rate;
  163. #endif
  164. void (*panel_power_ctrl)(int);
  165. u32 pseudo_palette[16];
  166. };
  167. /* Variable Screen Information */
  168. static struct fb_var_screeninfo da8xx_fb_var __devinitdata = {
  169. .xoffset = 0,
  170. .yoffset = 0,
  171. .transp = {0, 0, 0},
  172. .nonstd = 0,
  173. .activate = 0,
  174. .height = -1,
  175. .width = -1,
  176. .accel_flags = 0,
  177. .left_margin = LEFT_MARGIN,
  178. .right_margin = RIGHT_MARGIN,
  179. .upper_margin = UPPER_MARGIN,
  180. .lower_margin = LOWER_MARGIN,
  181. .sync = 0,
  182. .vmode = FB_VMODE_NONINTERLACED
  183. };
  184. static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
  185. .id = "DA8xx FB Drv",
  186. .type = FB_TYPE_PACKED_PIXELS,
  187. .type_aux = 0,
  188. .visual = FB_VISUAL_PSEUDOCOLOR,
  189. .xpanstep = 0,
  190. .ypanstep = 1,
  191. .ywrapstep = 0,
  192. .accel = FB_ACCEL_NONE
  193. };
  194. struct da8xx_panel {
  195. const char name[25]; /* Full name <vendor>_<model> */
  196. unsigned short width;
  197. unsigned short height;
  198. int hfp; /* Horizontal front porch */
  199. int hbp; /* Horizontal back porch */
  200. int hsw; /* Horizontal Sync Pulse Width */
  201. int vfp; /* Vertical front porch */
  202. int vbp; /* Vertical back porch */
  203. int vsw; /* Vertical Sync Pulse Width */
  204. unsigned int pxl_clk; /* Pixel clock */
  205. unsigned char invert_pxl_clk; /* Invert Pixel clock */
  206. };
  207. static struct da8xx_panel known_lcd_panels[] = {
  208. /* Sharp LCD035Q3DG01 */
  209. [0] = {
  210. .name = "Sharp_LCD035Q3DG01",
  211. .width = 320,
  212. .height = 240,
  213. .hfp = 8,
  214. .hbp = 6,
  215. .hsw = 0,
  216. .vfp = 2,
  217. .vbp = 2,
  218. .vsw = 0,
  219. .pxl_clk = 4608000,
  220. .invert_pxl_clk = 1,
  221. },
  222. /* Sharp LK043T1DG01 */
  223. [1] = {
  224. .name = "Sharp_LK043T1DG01",
  225. .width = 480,
  226. .height = 272,
  227. .hfp = 2,
  228. .hbp = 2,
  229. .hsw = 41,
  230. .vfp = 2,
  231. .vbp = 2,
  232. .vsw = 10,
  233. .pxl_clk = 7833600,
  234. .invert_pxl_clk = 0,
  235. },
  236. [2] = {
  237. /* Hitachi SP10Q010 */
  238. .name = "SP10Q010",
  239. .width = 320,
  240. .height = 240,
  241. .hfp = 10,
  242. .hbp = 10,
  243. .hsw = 10,
  244. .vfp = 10,
  245. .vbp = 10,
  246. .vsw = 10,
  247. .pxl_clk = 7833600,
  248. .invert_pxl_clk = 0,
  249. },
  250. };
  251. /* Enable the Raster Engine of the LCD Controller */
  252. static inline void lcd_enable_raster(void)
  253. {
  254. u32 reg;
  255. /* Put LCDC in reset for several cycles */
  256. if (lcd_revision == LCD_VERSION_2)
  257. /* Write 1 to reset LCDC */
  258. lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
  259. mdelay(1);
  260. /* Bring LCDC out of reset */
  261. if (lcd_revision == LCD_VERSION_2)
  262. lcdc_write(0, LCD_CLK_RESET_REG);
  263. mdelay(1);
  264. /* Above reset sequence doesnot reset register context */
  265. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  266. if (!(reg & LCD_RASTER_ENABLE))
  267. lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  268. }
  269. /* Disable the Raster Engine of the LCD Controller */
  270. static inline void lcd_disable_raster(bool wait_for_frame_done)
  271. {
  272. u32 reg;
  273. int ret;
  274. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  275. if (reg & LCD_RASTER_ENABLE)
  276. lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  277. else
  278. /* return if already disabled */
  279. return;
  280. if ((wait_for_frame_done == true) && (lcd_revision == LCD_VERSION_2)) {
  281. frame_done_flag = 0;
  282. ret = wait_event_interruptible_timeout(frame_done_wq,
  283. frame_done_flag != 0,
  284. msecs_to_jiffies(50));
  285. if (ret == 0)
  286. pr_err("LCD Controller timed out\n");
  287. }
  288. }
  289. static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
  290. {
  291. u32 start;
  292. u32 end;
  293. u32 reg_ras;
  294. u32 reg_dma;
  295. u32 reg_int;
  296. /* init reg to clear PLM (loading mode) fields */
  297. reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
  298. reg_ras &= ~(3 << 20);
  299. reg_dma = lcdc_read(LCD_DMA_CTRL_REG);
  300. if (load_mode == LOAD_DATA) {
  301. start = par->dma_start;
  302. end = par->dma_end;
  303. reg_ras |= LCD_PALETTE_LOAD_MODE(DATA_ONLY);
  304. if (lcd_revision == LCD_VERSION_1) {
  305. reg_dma |= LCD_V1_END_OF_FRAME_INT_ENA;
  306. } else {
  307. reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
  308. LCD_V2_END_OF_FRAME0_INT_ENA |
  309. LCD_V2_END_OF_FRAME1_INT_ENA |
  310. LCD_FRAME_DONE;
  311. lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
  312. }
  313. reg_dma |= LCD_DUAL_FRAME_BUFFER_ENABLE;
  314. lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  315. lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  316. lcdc_write(start, LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  317. lcdc_write(end, LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  318. } else if (load_mode == LOAD_PALETTE) {
  319. start = par->p_palette_base;
  320. end = start + par->palette_sz - 1;
  321. reg_ras |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
  322. if (lcd_revision == LCD_VERSION_1) {
  323. reg_ras |= LCD_V1_PL_INT_ENA;
  324. } else {
  325. reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
  326. LCD_V2_PL_INT_ENA;
  327. lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
  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. }
  332. lcdc_write(reg_dma, LCD_DMA_CTRL_REG);
  333. lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
  334. /*
  335. * The Raster enable bit must be set after all other control fields are
  336. * set.
  337. */
  338. lcd_enable_raster();
  339. }
  340. /* Configure the Burst Size and fifo threhold of DMA */
  341. static int lcd_cfg_dma(int burst_size, int fifo_th)
  342. {
  343. u32 reg;
  344. reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
  345. switch (burst_size) {
  346. case 1:
  347. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
  348. break;
  349. case 2:
  350. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
  351. break;
  352. case 4:
  353. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
  354. break;
  355. case 8:
  356. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
  357. break;
  358. case 16:
  359. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
  360. break;
  361. default:
  362. return -EINVAL;
  363. }
  364. reg |= (fifo_th << 8);
  365. lcdc_write(reg, LCD_DMA_CTRL_REG);
  366. return 0;
  367. }
  368. static void lcd_cfg_ac_bias(int period, int transitions_per_int)
  369. {
  370. u32 reg;
  371. /* Set the AC Bias Period and Number of Transisitons per Interrupt */
  372. reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
  373. reg |= LCD_AC_BIAS_FREQUENCY(period) |
  374. LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
  375. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  376. }
  377. static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
  378. int front_porch)
  379. {
  380. u32 reg;
  381. reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
  382. reg |= ((back_porch & 0xff) << 24)
  383. | ((front_porch & 0xff) << 16)
  384. | ((pulse_width & 0x3f) << 10);
  385. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  386. }
  387. static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
  388. int front_porch)
  389. {
  390. u32 reg;
  391. reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
  392. reg |= ((back_porch & 0xff) << 24)
  393. | ((front_porch & 0xff) << 16)
  394. | ((pulse_width & 0x3f) << 10);
  395. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  396. }
  397. static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
  398. {
  399. u32 reg;
  400. u32 reg_int;
  401. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
  402. LCD_MONO_8BIT_MODE |
  403. LCD_MONOCHROME_MODE);
  404. switch (cfg->p_disp_panel->panel_shade) {
  405. case MONOCHROME:
  406. reg |= LCD_MONOCHROME_MODE;
  407. if (cfg->mono_8bit_mode)
  408. reg |= LCD_MONO_8BIT_MODE;
  409. break;
  410. case COLOR_ACTIVE:
  411. reg |= LCD_TFT_MODE;
  412. if (cfg->tft_alt_mode)
  413. reg |= LCD_TFT_ALT_ENABLE;
  414. break;
  415. case COLOR_PASSIVE:
  416. if (cfg->stn_565_mode)
  417. reg |= LCD_STN_565_ENABLE;
  418. break;
  419. default:
  420. return -EINVAL;
  421. }
  422. /* enable additional interrupts here */
  423. if (lcd_revision == LCD_VERSION_1) {
  424. reg |= LCD_V1_UNDERFLOW_INT_ENA;
  425. } else {
  426. reg_int = lcdc_read(LCD_INT_ENABLE_SET_REG) |
  427. LCD_V2_UNDERFLOW_INT_ENA;
  428. lcdc_write(reg_int, LCD_INT_ENABLE_SET_REG);
  429. }
  430. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  431. reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
  432. if (cfg->sync_ctrl)
  433. reg |= LCD_SYNC_CTRL;
  434. else
  435. reg &= ~LCD_SYNC_CTRL;
  436. if (cfg->sync_edge)
  437. reg |= LCD_SYNC_EDGE;
  438. else
  439. reg &= ~LCD_SYNC_EDGE;
  440. if (cfg->invert_line_clock)
  441. reg |= LCD_INVERT_LINE_CLOCK;
  442. else
  443. reg &= ~LCD_INVERT_LINE_CLOCK;
  444. if (cfg->invert_frm_clock)
  445. reg |= LCD_INVERT_FRAME_CLOCK;
  446. else
  447. reg &= ~LCD_INVERT_FRAME_CLOCK;
  448. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  449. return 0;
  450. }
  451. static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
  452. u32 bpp, u32 raster_order)
  453. {
  454. u32 reg;
  455. if (bpp > 16 && lcd_revision == LCD_VERSION_1)
  456. return -EINVAL;
  457. /* Set the Panel Width */
  458. /* Pixels per line = (PPL + 1)*16 */
  459. if (lcd_revision == LCD_VERSION_1) {
  460. /*
  461. * 0x3F in bits 4..9 gives max horizontal resolution = 1024
  462. * pixels.
  463. */
  464. width &= 0x3f0;
  465. } else {
  466. /*
  467. * 0x7F in bits 4..10 gives max horizontal resolution = 2048
  468. * pixels.
  469. */
  470. width &= 0x7f0;
  471. }
  472. reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
  473. reg &= 0xfffffc00;
  474. if (lcd_revision == LCD_VERSION_1) {
  475. reg |= ((width >> 4) - 1) << 4;
  476. } else {
  477. width = (width >> 4) - 1;
  478. reg |= ((width & 0x3f) << 4) | ((width & 0x40) >> 3);
  479. }
  480. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  481. /* Set the Panel Height */
  482. /* Set bits 9:0 of Lines Per Pixel */
  483. reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
  484. reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
  485. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  486. /* Set bit 10 of Lines Per Pixel */
  487. if (lcd_revision == LCD_VERSION_2) {
  488. reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
  489. reg |= ((height - 1) & 0x400) << 16;
  490. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  491. }
  492. /* Set the Raster Order of the Frame Buffer */
  493. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
  494. if (raster_order)
  495. reg |= LCD_RASTER_ORDER;
  496. par->palette_sz = 16 * 2;
  497. switch (bpp) {
  498. case 1:
  499. case 2:
  500. case 4:
  501. case 16:
  502. break;
  503. case 24:
  504. reg |= LCD_V2_TFT_24BPP_MODE;
  505. case 32:
  506. reg |= LCD_V2_TFT_24BPP_UNPACK;
  507. break;
  508. case 8:
  509. par->palette_sz = 256 * 2;
  510. break;
  511. default:
  512. return -EINVAL;
  513. }
  514. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  515. return 0;
  516. }
  517. #define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
  518. static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  519. unsigned blue, unsigned transp,
  520. struct fb_info *info)
  521. {
  522. struct da8xx_fb_par *par = info->par;
  523. unsigned short *palette = (unsigned short *) par->v_palette_base;
  524. u_short pal;
  525. int update_hw = 0;
  526. if (regno > 255)
  527. return 1;
  528. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  529. return 1;
  530. if (info->var.bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
  531. return -EINVAL;
  532. switch (info->fix.visual) {
  533. case FB_VISUAL_TRUECOLOR:
  534. red = CNVT_TOHW(red, info->var.red.length);
  535. green = CNVT_TOHW(green, info->var.green.length);
  536. blue = CNVT_TOHW(blue, info->var.blue.length);
  537. break;
  538. case FB_VISUAL_PSEUDOCOLOR:
  539. switch (info->var.bits_per_pixel) {
  540. case 4:
  541. if (regno > 15)
  542. return -EINVAL;
  543. if (info->var.grayscale) {
  544. pal = regno;
  545. } else {
  546. red >>= 4;
  547. green >>= 8;
  548. blue >>= 12;
  549. pal = red & 0x0f00;
  550. pal |= green & 0x00f0;
  551. pal |= blue & 0x000f;
  552. }
  553. if (regno == 0)
  554. pal |= 0x2000;
  555. palette[regno] = pal;
  556. break;
  557. case 8:
  558. red >>= 4;
  559. green >>= 8;
  560. blue >>= 12;
  561. pal = (red & 0x0f00);
  562. pal |= (green & 0x00f0);
  563. pal |= (blue & 0x000f);
  564. if (palette[regno] != pal) {
  565. update_hw = 1;
  566. palette[regno] = pal;
  567. }
  568. break;
  569. }
  570. break;
  571. }
  572. /* Truecolor has hardware independent palette */
  573. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  574. u32 v;
  575. if (regno > 15)
  576. return -EINVAL;
  577. v = (red << info->var.red.offset) |
  578. (green << info->var.green.offset) |
  579. (blue << info->var.blue.offset);
  580. switch (info->var.bits_per_pixel) {
  581. case 16:
  582. ((u16 *) (info->pseudo_palette))[regno] = v;
  583. break;
  584. case 24:
  585. case 32:
  586. ((u32 *) (info->pseudo_palette))[regno] = v;
  587. break;
  588. }
  589. if (palette[0] != 0x4000) {
  590. update_hw = 1;
  591. palette[0] = 0x4000;
  592. }
  593. }
  594. /* Update the palette in the h/w as needed. */
  595. if (update_hw)
  596. lcd_blit(LOAD_PALETTE, par);
  597. return 0;
  598. }
  599. #undef CNVT_TOHW
  600. static void lcd_reset(struct da8xx_fb_par *par)
  601. {
  602. /* Disable the Raster if previously Enabled */
  603. lcd_disable_raster(false);
  604. /* DMA has to be disabled */
  605. lcdc_write(0, LCD_DMA_CTRL_REG);
  606. lcdc_write(0, LCD_RASTER_CTRL_REG);
  607. if (lcd_revision == LCD_VERSION_2) {
  608. lcdc_write(0, LCD_INT_ENABLE_SET_REG);
  609. /* Write 1 to reset */
  610. lcdc_write(LCD_CLK_MAIN_RESET, LCD_CLK_RESET_REG);
  611. lcdc_write(0, LCD_CLK_RESET_REG);
  612. }
  613. }
  614. static void lcd_calc_clk_divider(struct da8xx_fb_par *par)
  615. {
  616. unsigned int lcd_clk, div;
  617. lcd_clk = clk_get_rate(par->lcdc_clk);
  618. div = lcd_clk / par->pxl_clk;
  619. /* Configure the LCD clock divisor. */
  620. lcdc_write(LCD_CLK_DIVISOR(div) |
  621. (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
  622. if (lcd_revision == LCD_VERSION_2)
  623. lcdc_write(LCD_V2_DMA_CLK_EN | LCD_V2_LIDD_CLK_EN |
  624. LCD_V2_CORE_CLK_EN, LCD_CLK_ENABLE_REG);
  625. }
  626. static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
  627. struct da8xx_panel *panel)
  628. {
  629. u32 bpp;
  630. int ret = 0;
  631. lcd_reset(par);
  632. /* Calculate the divider */
  633. lcd_calc_clk_divider(par);
  634. if (panel->invert_pxl_clk)
  635. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
  636. LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  637. else
  638. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
  639. ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  640. /* Configure the DMA burst size and fifo threshold. */
  641. ret = lcd_cfg_dma(cfg->dma_burst_sz, cfg->fifo_th);
  642. if (ret < 0)
  643. return ret;
  644. /* Configure the AC bias properties. */
  645. lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
  646. /* Configure the vertical and horizontal sync properties. */
  647. lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
  648. lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
  649. /* Configure for disply */
  650. ret = lcd_cfg_display(cfg);
  651. if (ret < 0)
  652. return ret;
  653. if (QVGA != cfg->p_disp_panel->panel_type)
  654. return -EINVAL;
  655. if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
  656. cfg->bpp >= cfg->p_disp_panel->min_bpp)
  657. bpp = cfg->bpp;
  658. else
  659. bpp = cfg->p_disp_panel->max_bpp;
  660. if (bpp == 12)
  661. bpp = 16;
  662. ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
  663. (unsigned int)panel->height, bpp,
  664. cfg->raster_order);
  665. if (ret < 0)
  666. return ret;
  667. /* Configure FDD */
  668. lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
  669. (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
  670. return 0;
  671. }
  672. /* IRQ handler for version 2 of LCDC */
  673. static irqreturn_t lcdc_irq_handler_rev02(int irq, void *arg)
  674. {
  675. struct da8xx_fb_par *par = arg;
  676. u32 stat = lcdc_read(LCD_MASKED_STAT_REG);
  677. if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
  678. lcd_disable_raster(false);
  679. lcdc_write(stat, LCD_MASKED_STAT_REG);
  680. lcd_enable_raster();
  681. } else if (stat & LCD_PL_LOAD_DONE) {
  682. /*
  683. * Must disable raster before changing state of any control bit.
  684. * And also must be disabled before clearing the PL loading
  685. * interrupt via the following write to the status register. If
  686. * this is done after then one gets multiple PL done interrupts.
  687. */
  688. lcd_disable_raster(false);
  689. lcdc_write(stat, LCD_MASKED_STAT_REG);
  690. /* Disable PL completion interrupt */
  691. lcdc_write(LCD_V2_PL_INT_ENA, LCD_INT_ENABLE_CLR_REG);
  692. /* Setup and start data loading mode */
  693. lcd_blit(LOAD_DATA, par);
  694. } else {
  695. lcdc_write(stat, LCD_MASKED_STAT_REG);
  696. if (stat & LCD_END_OF_FRAME0) {
  697. par->which_dma_channel_done = 0;
  698. lcdc_write(par->dma_start,
  699. LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  700. lcdc_write(par->dma_end,
  701. LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  702. par->vsync_flag = 1;
  703. wake_up_interruptible(&par->vsync_wait);
  704. }
  705. if (stat & LCD_END_OF_FRAME1) {
  706. par->which_dma_channel_done = 1;
  707. lcdc_write(par->dma_start,
  708. LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  709. lcdc_write(par->dma_end,
  710. LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  711. par->vsync_flag = 1;
  712. wake_up_interruptible(&par->vsync_wait);
  713. }
  714. /* Set only when controller is disabled and at the end of
  715. * active frame
  716. */
  717. if (stat & BIT(0)) {
  718. frame_done_flag = 1;
  719. wake_up_interruptible(&frame_done_wq);
  720. }
  721. }
  722. lcdc_write(0, LCD_END_OF_INT_IND_REG);
  723. return IRQ_HANDLED;
  724. }
  725. /* IRQ handler for version 1 LCDC */
  726. static irqreturn_t lcdc_irq_handler_rev01(int irq, void *arg)
  727. {
  728. struct da8xx_fb_par *par = arg;
  729. u32 stat = lcdc_read(LCD_STAT_REG);
  730. u32 reg_ras;
  731. if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
  732. lcd_disable_raster(false);
  733. lcdc_write(stat, LCD_STAT_REG);
  734. lcd_enable_raster();
  735. } else if (stat & LCD_PL_LOAD_DONE) {
  736. /*
  737. * Must disable raster before changing state of any control bit.
  738. * And also must be disabled before clearing the PL loading
  739. * interrupt via the following write to the status register. If
  740. * this is done after then one gets multiple PL done interrupts.
  741. */
  742. lcd_disable_raster(false);
  743. lcdc_write(stat, LCD_STAT_REG);
  744. /* Disable PL completion inerrupt */
  745. reg_ras = lcdc_read(LCD_RASTER_CTRL_REG);
  746. reg_ras &= ~LCD_V1_PL_INT_ENA;
  747. lcdc_write(reg_ras, LCD_RASTER_CTRL_REG);
  748. /* Setup and start data loading mode */
  749. lcd_blit(LOAD_DATA, par);
  750. } else {
  751. lcdc_write(stat, LCD_STAT_REG);
  752. if (stat & LCD_END_OF_FRAME0) {
  753. par->which_dma_channel_done = 0;
  754. lcdc_write(par->dma_start,
  755. LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  756. lcdc_write(par->dma_end,
  757. LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  758. par->vsync_flag = 1;
  759. wake_up_interruptible(&par->vsync_wait);
  760. }
  761. if (stat & LCD_END_OF_FRAME1) {
  762. par->which_dma_channel_done = 1;
  763. lcdc_write(par->dma_start,
  764. LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  765. lcdc_write(par->dma_end,
  766. LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  767. par->vsync_flag = 1;
  768. wake_up_interruptible(&par->vsync_wait);
  769. }
  770. }
  771. return IRQ_HANDLED;
  772. }
  773. static int fb_check_var(struct fb_var_screeninfo *var,
  774. struct fb_info *info)
  775. {
  776. int err = 0;
  777. if (var->bits_per_pixel > 16 && lcd_revision == LCD_VERSION_1)
  778. return -EINVAL;
  779. switch (var->bits_per_pixel) {
  780. case 1:
  781. case 8:
  782. var->red.offset = 0;
  783. var->red.length = 8;
  784. var->green.offset = 0;
  785. var->green.length = 8;
  786. var->blue.offset = 0;
  787. var->blue.length = 8;
  788. var->transp.offset = 0;
  789. var->transp.length = 0;
  790. var->nonstd = 0;
  791. break;
  792. case 4:
  793. var->red.offset = 0;
  794. var->red.length = 4;
  795. var->green.offset = 0;
  796. var->green.length = 4;
  797. var->blue.offset = 0;
  798. var->blue.length = 4;
  799. var->transp.offset = 0;
  800. var->transp.length = 0;
  801. var->nonstd = FB_NONSTD_REV_PIX_IN_B;
  802. break;
  803. case 16: /* RGB 565 */
  804. var->red.offset = 11;
  805. var->red.length = 5;
  806. var->green.offset = 5;
  807. var->green.length = 6;
  808. var->blue.offset = 0;
  809. var->blue.length = 5;
  810. var->transp.offset = 0;
  811. var->transp.length = 0;
  812. var->nonstd = 0;
  813. break;
  814. case 24:
  815. var->red.offset = 16;
  816. var->red.length = 8;
  817. var->green.offset = 8;
  818. var->green.length = 8;
  819. var->blue.offset = 0;
  820. var->blue.length = 8;
  821. var->nonstd = 0;
  822. break;
  823. case 32:
  824. var->transp.offset = 24;
  825. var->transp.length = 8;
  826. var->red.offset = 16;
  827. var->red.length = 8;
  828. var->green.offset = 8;
  829. var->green.length = 8;
  830. var->blue.offset = 0;
  831. var->blue.length = 8;
  832. var->nonstd = 0;
  833. break;
  834. default:
  835. err = -EINVAL;
  836. }
  837. var->red.msb_right = 0;
  838. var->green.msb_right = 0;
  839. var->blue.msb_right = 0;
  840. var->transp.msb_right = 0;
  841. return err;
  842. }
  843. #ifdef CONFIG_CPU_FREQ
  844. static int lcd_da8xx_cpufreq_transition(struct notifier_block *nb,
  845. unsigned long val, void *data)
  846. {
  847. struct da8xx_fb_par *par;
  848. par = container_of(nb, struct da8xx_fb_par, freq_transition);
  849. if (val == CPUFREQ_POSTCHANGE) {
  850. if (par->lcd_fck_rate != clk_get_rate(par->lcdc_clk)) {
  851. par->lcd_fck_rate = clk_get_rate(par->lcdc_clk);
  852. lcd_disable_raster(true);
  853. lcd_calc_clk_divider(par);
  854. lcd_enable_raster();
  855. }
  856. }
  857. return 0;
  858. }
  859. static inline int lcd_da8xx_cpufreq_register(struct da8xx_fb_par *par)
  860. {
  861. par->freq_transition.notifier_call = lcd_da8xx_cpufreq_transition;
  862. return cpufreq_register_notifier(&par->freq_transition,
  863. CPUFREQ_TRANSITION_NOTIFIER);
  864. }
  865. static inline void lcd_da8xx_cpufreq_deregister(struct da8xx_fb_par *par)
  866. {
  867. cpufreq_unregister_notifier(&par->freq_transition,
  868. CPUFREQ_TRANSITION_NOTIFIER);
  869. }
  870. #endif
  871. static int __devexit fb_remove(struct platform_device *dev)
  872. {
  873. struct fb_info *info = dev_get_drvdata(&dev->dev);
  874. if (info) {
  875. struct da8xx_fb_par *par = info->par;
  876. #ifdef CONFIG_CPU_FREQ
  877. lcd_da8xx_cpufreq_deregister(par);
  878. #endif
  879. if (par->panel_power_ctrl)
  880. par->panel_power_ctrl(0);
  881. lcd_disable_raster(true);
  882. lcdc_write(0, LCD_RASTER_CTRL_REG);
  883. /* disable DMA */
  884. lcdc_write(0, LCD_DMA_CTRL_REG);
  885. unregister_framebuffer(info);
  886. fb_dealloc_cmap(&info->cmap);
  887. dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
  888. par->p_palette_base);
  889. dma_free_coherent(NULL, par->vram_size, par->vram_virt,
  890. par->vram_phys);
  891. free_irq(par->irq, par);
  892. clk_disable(par->lcdc_clk);
  893. clk_put(par->lcdc_clk);
  894. framebuffer_release(info);
  895. iounmap((void __iomem *)da8xx_fb_reg_base);
  896. release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
  897. }
  898. return 0;
  899. }
  900. /*
  901. * Function to wait for vertical sync which for this LCD peripheral
  902. * translates into waiting for the current raster frame to complete.
  903. */
  904. static int fb_wait_for_vsync(struct fb_info *info)
  905. {
  906. struct da8xx_fb_par *par = info->par;
  907. int ret;
  908. /*
  909. * Set flag to 0 and wait for isr to set to 1. It would seem there is a
  910. * race condition here where the ISR could have occurred just before or
  911. * just after this set. But since we are just coarsely waiting for
  912. * a frame to complete then that's OK. i.e. if the frame completed
  913. * just before this code executed then we have to wait another full
  914. * frame time but there is no way to avoid such a situation. On the
  915. * other hand if the frame completed just after then we don't need
  916. * to wait long at all. Either way we are guaranteed to return to the
  917. * user immediately after a frame completion which is all that is
  918. * required.
  919. */
  920. par->vsync_flag = 0;
  921. ret = wait_event_interruptible_timeout(par->vsync_wait,
  922. par->vsync_flag != 0,
  923. par->vsync_timeout);
  924. if (ret < 0)
  925. return ret;
  926. if (ret == 0)
  927. return -ETIMEDOUT;
  928. return 0;
  929. }
  930. static int fb_ioctl(struct fb_info *info, unsigned int cmd,
  931. unsigned long arg)
  932. {
  933. struct lcd_sync_arg sync_arg;
  934. switch (cmd) {
  935. case FBIOGET_CONTRAST:
  936. case FBIOPUT_CONTRAST:
  937. case FBIGET_BRIGHTNESS:
  938. case FBIPUT_BRIGHTNESS:
  939. case FBIGET_COLOR:
  940. case FBIPUT_COLOR:
  941. return -ENOTTY;
  942. case FBIPUT_HSYNC:
  943. if (copy_from_user(&sync_arg, (char *)arg,
  944. sizeof(struct lcd_sync_arg)))
  945. return -EFAULT;
  946. lcd_cfg_horizontal_sync(sync_arg.back_porch,
  947. sync_arg.pulse_width,
  948. sync_arg.front_porch);
  949. break;
  950. case FBIPUT_VSYNC:
  951. if (copy_from_user(&sync_arg, (char *)arg,
  952. sizeof(struct lcd_sync_arg)))
  953. return -EFAULT;
  954. lcd_cfg_vertical_sync(sync_arg.back_porch,
  955. sync_arg.pulse_width,
  956. sync_arg.front_porch);
  957. break;
  958. case FBIO_WAITFORVSYNC:
  959. return fb_wait_for_vsync(info);
  960. default:
  961. return -EINVAL;
  962. }
  963. return 0;
  964. }
  965. static int cfb_blank(int blank, struct fb_info *info)
  966. {
  967. struct da8xx_fb_par *par = info->par;
  968. int ret = 0;
  969. if (par->blank == blank)
  970. return 0;
  971. par->blank = blank;
  972. switch (blank) {
  973. case FB_BLANK_UNBLANK:
  974. lcd_enable_raster();
  975. if (par->panel_power_ctrl)
  976. par->panel_power_ctrl(1);
  977. break;
  978. case FB_BLANK_NORMAL:
  979. case FB_BLANK_VSYNC_SUSPEND:
  980. case FB_BLANK_HSYNC_SUSPEND:
  981. case FB_BLANK_POWERDOWN:
  982. if (par->panel_power_ctrl)
  983. par->panel_power_ctrl(0);
  984. lcd_disable_raster(true);
  985. break;
  986. default:
  987. ret = -EINVAL;
  988. }
  989. return ret;
  990. }
  991. /*
  992. * Set new x,y offsets in the virtual display for the visible area and switch
  993. * to the new mode.
  994. */
  995. static int da8xx_pan_display(struct fb_var_screeninfo *var,
  996. struct fb_info *fbi)
  997. {
  998. int ret = 0;
  999. struct fb_var_screeninfo new_var;
  1000. struct da8xx_fb_par *par = fbi->par;
  1001. struct fb_fix_screeninfo *fix = &fbi->fix;
  1002. unsigned int end;
  1003. unsigned int start;
  1004. unsigned long irq_flags;
  1005. if (var->xoffset != fbi->var.xoffset ||
  1006. var->yoffset != fbi->var.yoffset) {
  1007. memcpy(&new_var, &fbi->var, sizeof(new_var));
  1008. new_var.xoffset = var->xoffset;
  1009. new_var.yoffset = var->yoffset;
  1010. if (fb_check_var(&new_var, fbi))
  1011. ret = -EINVAL;
  1012. else {
  1013. memcpy(&fbi->var, &new_var, sizeof(new_var));
  1014. start = fix->smem_start +
  1015. new_var.yoffset * fix->line_length +
  1016. new_var.xoffset * fbi->var.bits_per_pixel / 8;
  1017. end = start + fbi->var.yres * fix->line_length - 1;
  1018. par->dma_start = start;
  1019. par->dma_end = end;
  1020. spin_lock_irqsave(&par->lock_for_chan_update,
  1021. irq_flags);
  1022. if (par->which_dma_channel_done == 0) {
  1023. lcdc_write(par->dma_start,
  1024. LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  1025. lcdc_write(par->dma_end,
  1026. LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  1027. } else if (par->which_dma_channel_done == 1) {
  1028. lcdc_write(par->dma_start,
  1029. LCD_DMA_FRM_BUF_BASE_ADDR_1_REG);
  1030. lcdc_write(par->dma_end,
  1031. LCD_DMA_FRM_BUF_CEILING_ADDR_1_REG);
  1032. }
  1033. spin_unlock_irqrestore(&par->lock_for_chan_update,
  1034. irq_flags);
  1035. }
  1036. }
  1037. return ret;
  1038. }
  1039. static struct fb_ops da8xx_fb_ops = {
  1040. .owner = THIS_MODULE,
  1041. .fb_check_var = fb_check_var,
  1042. .fb_setcolreg = fb_setcolreg,
  1043. .fb_pan_display = da8xx_pan_display,
  1044. .fb_ioctl = fb_ioctl,
  1045. .fb_fillrect = cfb_fillrect,
  1046. .fb_copyarea = cfb_copyarea,
  1047. .fb_imageblit = cfb_imageblit,
  1048. .fb_blank = cfb_blank,
  1049. };
  1050. /* Calculate and return pixel clock period in pico seconds */
  1051. static unsigned int da8xxfb_pixel_clk_period(struct da8xx_fb_par *par)
  1052. {
  1053. unsigned int lcd_clk, div;
  1054. unsigned int configured_pix_clk;
  1055. unsigned long long pix_clk_period_picosec = 1000000000000ULL;
  1056. lcd_clk = clk_get_rate(par->lcdc_clk);
  1057. div = lcd_clk / par->pxl_clk;
  1058. configured_pix_clk = (lcd_clk / div);
  1059. do_div(pix_clk_period_picosec, configured_pix_clk);
  1060. return pix_clk_period_picosec;
  1061. }
  1062. static int __devinit fb_probe(struct platform_device *device)
  1063. {
  1064. struct da8xx_lcdc_platform_data *fb_pdata =
  1065. device->dev.platform_data;
  1066. struct lcd_ctrl_config *lcd_cfg;
  1067. struct da8xx_panel *lcdc_info;
  1068. struct fb_info *da8xx_fb_info;
  1069. struct clk *fb_clk = NULL;
  1070. struct da8xx_fb_par *par;
  1071. resource_size_t len;
  1072. int ret, i;
  1073. unsigned long ulcm;
  1074. if (fb_pdata == NULL) {
  1075. dev_err(&device->dev, "Can not get platform data\n");
  1076. return -ENOENT;
  1077. }
  1078. lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
  1079. if (!lcdc_regs) {
  1080. dev_err(&device->dev,
  1081. "Can not get memory resource for LCD controller\n");
  1082. return -ENOENT;
  1083. }
  1084. len = resource_size(lcdc_regs);
  1085. lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
  1086. if (!lcdc_regs)
  1087. return -EBUSY;
  1088. da8xx_fb_reg_base = (resource_size_t)ioremap(lcdc_regs->start, len);
  1089. if (!da8xx_fb_reg_base) {
  1090. ret = -EBUSY;
  1091. goto err_request_mem;
  1092. }
  1093. fb_clk = clk_get(&device->dev, NULL);
  1094. if (IS_ERR(fb_clk)) {
  1095. dev_err(&device->dev, "Can not get device clock\n");
  1096. ret = -ENODEV;
  1097. goto err_ioremap;
  1098. }
  1099. ret = clk_enable(fb_clk);
  1100. if (ret)
  1101. goto err_clk_put;
  1102. /* Determine LCD IP Version */
  1103. switch (lcdc_read(LCD_PID_REG)) {
  1104. case 0x4C100102:
  1105. lcd_revision = LCD_VERSION_1;
  1106. break;
  1107. case 0x4F200800:
  1108. lcd_revision = LCD_VERSION_2;
  1109. break;
  1110. default:
  1111. dev_warn(&device->dev, "Unknown PID Reg value 0x%x, "
  1112. "defaulting to LCD revision 1\n",
  1113. lcdc_read(LCD_PID_REG));
  1114. lcd_revision = LCD_VERSION_1;
  1115. break;
  1116. }
  1117. for (i = 0, lcdc_info = known_lcd_panels;
  1118. i < ARRAY_SIZE(known_lcd_panels);
  1119. i++, lcdc_info++) {
  1120. if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
  1121. break;
  1122. }
  1123. if (i == ARRAY_SIZE(known_lcd_panels)) {
  1124. dev_err(&device->dev, "GLCD: No valid panel found\n");
  1125. ret = -ENODEV;
  1126. goto err_clk_disable;
  1127. } else
  1128. dev_info(&device->dev, "GLCD: Found %s panel\n",
  1129. fb_pdata->type);
  1130. lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
  1131. da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
  1132. &device->dev);
  1133. if (!da8xx_fb_info) {
  1134. dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
  1135. ret = -ENOMEM;
  1136. goto err_clk_disable;
  1137. }
  1138. par = da8xx_fb_info->par;
  1139. par->lcdc_clk = fb_clk;
  1140. #ifdef CONFIG_CPU_FREQ
  1141. par->lcd_fck_rate = clk_get_rate(fb_clk);
  1142. #endif
  1143. par->pxl_clk = lcdc_info->pxl_clk;
  1144. if (fb_pdata->panel_power_ctrl) {
  1145. par->panel_power_ctrl = fb_pdata->panel_power_ctrl;
  1146. par->panel_power_ctrl(1);
  1147. }
  1148. if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
  1149. dev_err(&device->dev, "lcd_init failed\n");
  1150. ret = -EFAULT;
  1151. goto err_release_fb;
  1152. }
  1153. /* allocate frame buffer */
  1154. par->vram_size = lcdc_info->width * lcdc_info->height * lcd_cfg->bpp;
  1155. ulcm = lcm((lcdc_info->width * lcd_cfg->bpp)/8, PAGE_SIZE);
  1156. par->vram_size = roundup(par->vram_size/8, ulcm);
  1157. par->vram_size = par->vram_size * LCD_NUM_BUFFERS;
  1158. par->vram_virt = dma_alloc_coherent(NULL,
  1159. par->vram_size,
  1160. (resource_size_t *) &par->vram_phys,
  1161. GFP_KERNEL | GFP_DMA);
  1162. if (!par->vram_virt) {
  1163. dev_err(&device->dev,
  1164. "GLCD: kmalloc for frame buffer failed\n");
  1165. ret = -EINVAL;
  1166. goto err_release_fb;
  1167. }
  1168. da8xx_fb_info->screen_base = (char __iomem *) par->vram_virt;
  1169. da8xx_fb_fix.smem_start = par->vram_phys;
  1170. da8xx_fb_fix.smem_len = par->vram_size;
  1171. da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
  1172. par->dma_start = par->vram_phys;
  1173. par->dma_end = par->dma_start + lcdc_info->height *
  1174. da8xx_fb_fix.line_length - 1;
  1175. /* allocate palette buffer */
  1176. par->v_palette_base = dma_alloc_coherent(NULL,
  1177. PALETTE_SIZE,
  1178. (resource_size_t *)
  1179. &par->p_palette_base,
  1180. GFP_KERNEL | GFP_DMA);
  1181. if (!par->v_palette_base) {
  1182. dev_err(&device->dev,
  1183. "GLCD: kmalloc for palette buffer failed\n");
  1184. ret = -EINVAL;
  1185. goto err_release_fb_mem;
  1186. }
  1187. memset(par->v_palette_base, 0, PALETTE_SIZE);
  1188. par->irq = platform_get_irq(device, 0);
  1189. if (par->irq < 0) {
  1190. ret = -ENOENT;
  1191. goto err_release_pl_mem;
  1192. }
  1193. /* Initialize par */
  1194. da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
  1195. da8xx_fb_var.xres = lcdc_info->width;
  1196. da8xx_fb_var.xres_virtual = lcdc_info->width;
  1197. da8xx_fb_var.yres = lcdc_info->height;
  1198. da8xx_fb_var.yres_virtual = lcdc_info->height * LCD_NUM_BUFFERS;
  1199. da8xx_fb_var.grayscale =
  1200. lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
  1201. da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
  1202. da8xx_fb_var.hsync_len = lcdc_info->hsw;
  1203. da8xx_fb_var.vsync_len = lcdc_info->vsw;
  1204. da8xx_fb_var.right_margin = lcdc_info->hfp;
  1205. da8xx_fb_var.left_margin = lcdc_info->hbp;
  1206. da8xx_fb_var.lower_margin = lcdc_info->vfp;
  1207. da8xx_fb_var.upper_margin = lcdc_info->vbp;
  1208. da8xx_fb_var.pixclock = da8xxfb_pixel_clk_period(par);
  1209. /* Initialize fbinfo */
  1210. da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
  1211. da8xx_fb_info->fix = da8xx_fb_fix;
  1212. da8xx_fb_info->var = da8xx_fb_var;
  1213. da8xx_fb_info->fbops = &da8xx_fb_ops;
  1214. da8xx_fb_info->pseudo_palette = par->pseudo_palette;
  1215. da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
  1216. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  1217. ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
  1218. if (ret)
  1219. goto err_release_pl_mem;
  1220. da8xx_fb_info->cmap.len = par->palette_sz;
  1221. /* initialize var_screeninfo */
  1222. da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
  1223. fb_set_var(da8xx_fb_info, &da8xx_fb_var);
  1224. dev_set_drvdata(&device->dev, da8xx_fb_info);
  1225. /* initialize the vsync wait queue */
  1226. init_waitqueue_head(&par->vsync_wait);
  1227. par->vsync_timeout = HZ / 5;
  1228. par->which_dma_channel_done = -1;
  1229. spin_lock_init(&par->lock_for_chan_update);
  1230. /* Register the Frame Buffer */
  1231. if (register_framebuffer(da8xx_fb_info) < 0) {
  1232. dev_err(&device->dev,
  1233. "GLCD: Frame Buffer Registration Failed!\n");
  1234. ret = -EINVAL;
  1235. goto err_dealloc_cmap;
  1236. }
  1237. #ifdef CONFIG_CPU_FREQ
  1238. ret = lcd_da8xx_cpufreq_register(par);
  1239. if (ret) {
  1240. dev_err(&device->dev, "failed to register cpufreq\n");
  1241. goto err_cpu_freq;
  1242. }
  1243. #endif
  1244. if (lcd_revision == LCD_VERSION_1)
  1245. lcdc_irq_handler = lcdc_irq_handler_rev01;
  1246. else {
  1247. init_waitqueue_head(&frame_done_wq);
  1248. lcdc_irq_handler = lcdc_irq_handler_rev02;
  1249. }
  1250. ret = request_irq(par->irq, lcdc_irq_handler, 0,
  1251. DRIVER_NAME, par);
  1252. if (ret)
  1253. goto irq_freq;
  1254. return 0;
  1255. irq_freq:
  1256. #ifdef CONFIG_CPU_FREQ
  1257. lcd_da8xx_cpufreq_deregister(par);
  1258. err_cpu_freq:
  1259. #endif
  1260. unregister_framebuffer(da8xx_fb_info);
  1261. err_dealloc_cmap:
  1262. fb_dealloc_cmap(&da8xx_fb_info->cmap);
  1263. err_release_pl_mem:
  1264. dma_free_coherent(NULL, PALETTE_SIZE, par->v_palette_base,
  1265. par->p_palette_base);
  1266. err_release_fb_mem:
  1267. dma_free_coherent(NULL, par->vram_size, par->vram_virt, par->vram_phys);
  1268. err_release_fb:
  1269. framebuffer_release(da8xx_fb_info);
  1270. err_clk_disable:
  1271. clk_disable(fb_clk);
  1272. err_clk_put:
  1273. clk_put(fb_clk);
  1274. err_ioremap:
  1275. iounmap((void __iomem *)da8xx_fb_reg_base);
  1276. err_request_mem:
  1277. release_mem_region(lcdc_regs->start, len);
  1278. return ret;
  1279. }
  1280. #ifdef CONFIG_PM
  1281. static int fb_suspend(struct platform_device *dev, pm_message_t state)
  1282. {
  1283. struct fb_info *info = platform_get_drvdata(dev);
  1284. struct da8xx_fb_par *par = info->par;
  1285. console_lock();
  1286. if (par->panel_power_ctrl)
  1287. par->panel_power_ctrl(0);
  1288. fb_set_suspend(info, 1);
  1289. lcd_disable_raster(true);
  1290. clk_disable(par->lcdc_clk);
  1291. console_unlock();
  1292. return 0;
  1293. }
  1294. static int fb_resume(struct platform_device *dev)
  1295. {
  1296. struct fb_info *info = platform_get_drvdata(dev);
  1297. struct da8xx_fb_par *par = info->par;
  1298. console_lock();
  1299. clk_enable(par->lcdc_clk);
  1300. lcd_enable_raster();
  1301. if (par->panel_power_ctrl)
  1302. par->panel_power_ctrl(1);
  1303. fb_set_suspend(info, 0);
  1304. console_unlock();
  1305. return 0;
  1306. }
  1307. #else
  1308. #define fb_suspend NULL
  1309. #define fb_resume NULL
  1310. #endif
  1311. static struct platform_driver da8xx_fb_driver = {
  1312. .probe = fb_probe,
  1313. .remove = __devexit_p(fb_remove),
  1314. .suspend = fb_suspend,
  1315. .resume = fb_resume,
  1316. .driver = {
  1317. .name = DRIVER_NAME,
  1318. .owner = THIS_MODULE,
  1319. },
  1320. };
  1321. static int __init da8xx_fb_init(void)
  1322. {
  1323. return platform_driver_register(&da8xx_fb_driver);
  1324. }
  1325. static void __exit da8xx_fb_cleanup(void)
  1326. {
  1327. platform_driver_unregister(&da8xx_fb_driver);
  1328. }
  1329. module_init(da8xx_fb_init);
  1330. module_exit(da8xx_fb_cleanup);
  1331. MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
  1332. MODULE_AUTHOR("Texas Instruments");
  1333. MODULE_LICENSE("GPL");