da8xx-fb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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 <video/da8xx-fb.h>
  32. #define DRIVER_NAME "da8xx_lcdc"
  33. /* LCD Status Register */
  34. #define LCD_END_OF_FRAME0 BIT(8)
  35. #define LCD_FIFO_UNDERFLOW BIT(5)
  36. #define LCD_SYNC_LOST BIT(2)
  37. /* LCD DMA Control Register */
  38. #define LCD_DMA_BURST_SIZE(x) ((x) << 4)
  39. #define LCD_DMA_BURST_1 0x0
  40. #define LCD_DMA_BURST_2 0x1
  41. #define LCD_DMA_BURST_4 0x2
  42. #define LCD_DMA_BURST_8 0x3
  43. #define LCD_DMA_BURST_16 0x4
  44. #define LCD_END_OF_FRAME_INT_ENA BIT(2)
  45. #define LCD_DUAL_FRAME_BUFFER_ENABLE BIT(0)
  46. /* LCD Control Register */
  47. #define LCD_CLK_DIVISOR(x) ((x) << 8)
  48. #define LCD_RASTER_MODE 0x01
  49. /* LCD Raster Control Register */
  50. #define LCD_PALETTE_LOAD_MODE(x) ((x) << 20)
  51. #define PALETTE_AND_DATA 0x00
  52. #define PALETTE_ONLY 0x01
  53. #define LCD_MONO_8BIT_MODE BIT(9)
  54. #define LCD_RASTER_ORDER BIT(8)
  55. #define LCD_TFT_MODE BIT(7)
  56. #define LCD_UNDERFLOW_INT_ENA BIT(6)
  57. #define LCD_MONOCHROME_MODE BIT(1)
  58. #define LCD_RASTER_ENABLE BIT(0)
  59. #define LCD_TFT_ALT_ENABLE BIT(23)
  60. #define LCD_STN_565_ENABLE BIT(24)
  61. /* LCD Raster Timing 2 Register */
  62. #define LCD_AC_BIAS_TRANSITIONS_PER_INT(x) ((x) << 16)
  63. #define LCD_AC_BIAS_FREQUENCY(x) ((x) << 8)
  64. #define LCD_SYNC_CTRL BIT(25)
  65. #define LCD_SYNC_EDGE BIT(24)
  66. #define LCD_INVERT_PIXEL_CLOCK BIT(22)
  67. #define LCD_INVERT_LINE_CLOCK BIT(21)
  68. #define LCD_INVERT_FRAME_CLOCK BIT(20)
  69. /* LCD Block */
  70. #define LCD_CTRL_REG 0x4
  71. #define LCD_STAT_REG 0x8
  72. #define LCD_RASTER_CTRL_REG 0x28
  73. #define LCD_RASTER_TIMING_0_REG 0x2C
  74. #define LCD_RASTER_TIMING_1_REG 0x30
  75. #define LCD_RASTER_TIMING_2_REG 0x34
  76. #define LCD_DMA_CTRL_REG 0x40
  77. #define LCD_DMA_FRM_BUF_BASE_ADDR_0_REG 0x44
  78. #define LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG 0x48
  79. #define WSI_TIMEOUT 50
  80. #define PALETTE_SIZE 256
  81. #define LEFT_MARGIN 64
  82. #define RIGHT_MARGIN 64
  83. #define UPPER_MARGIN 32
  84. #define LOWER_MARGIN 32
  85. static resource_size_t da8xx_fb_reg_base;
  86. static struct resource *lcdc_regs;
  87. static inline unsigned int lcdc_read(unsigned int addr)
  88. {
  89. return (unsigned int)__raw_readl(da8xx_fb_reg_base + (addr));
  90. }
  91. static inline void lcdc_write(unsigned int val, unsigned int addr)
  92. {
  93. __raw_writel(val, da8xx_fb_reg_base + (addr));
  94. }
  95. struct da8xx_fb_par {
  96. resource_size_t p_palette_base;
  97. unsigned char *v_palette_base;
  98. struct clk *lcdc_clk;
  99. int irq;
  100. unsigned short pseudo_palette[16];
  101. unsigned int databuf_sz;
  102. unsigned int palette_sz;
  103. };
  104. /* Variable Screen Information */
  105. static struct fb_var_screeninfo da8xx_fb_var __devinitdata = {
  106. .xoffset = 0,
  107. .yoffset = 0,
  108. .transp = {0, 0, 0},
  109. .nonstd = 0,
  110. .activate = 0,
  111. .height = -1,
  112. .width = -1,
  113. .pixclock = 46666, /* 46us - AUO display */
  114. .accel_flags = 0,
  115. .left_margin = LEFT_MARGIN,
  116. .right_margin = RIGHT_MARGIN,
  117. .upper_margin = UPPER_MARGIN,
  118. .lower_margin = LOWER_MARGIN,
  119. .sync = 0,
  120. .vmode = FB_VMODE_NONINTERLACED
  121. };
  122. static struct fb_fix_screeninfo da8xx_fb_fix __devinitdata = {
  123. .id = "DA8xx FB Drv",
  124. .type = FB_TYPE_PACKED_PIXELS,
  125. .type_aux = 0,
  126. .visual = FB_VISUAL_PSEUDOCOLOR,
  127. .xpanstep = 1,
  128. .ypanstep = 1,
  129. .ywrapstep = 1,
  130. .accel = FB_ACCEL_NONE
  131. };
  132. struct da8xx_panel {
  133. const char name[25]; /* Full name <vendor>_<model> */
  134. unsigned short width;
  135. unsigned short height;
  136. int hfp; /* Horizontal front porch */
  137. int hbp; /* Horizontal back porch */
  138. int hsw; /* Horizontal Sync Pulse Width */
  139. int vfp; /* Vertical front porch */
  140. int vbp; /* Vertical back porch */
  141. int vsw; /* Vertical Sync Pulse Width */
  142. int pxl_clk; /* Pixel clock */
  143. unsigned char invert_pxl_clk; /* Invert Pixel clock */
  144. };
  145. static struct da8xx_panel known_lcd_panels[] = {
  146. /* Sharp LCD035Q3DG01 */
  147. [0] = {
  148. .name = "Sharp_LCD035Q3DG01",
  149. .width = 320,
  150. .height = 240,
  151. .hfp = 8,
  152. .hbp = 6,
  153. .hsw = 0,
  154. .vfp = 2,
  155. .vbp = 2,
  156. .vsw = 0,
  157. .pxl_clk = 0x10,
  158. .invert_pxl_clk = 1,
  159. },
  160. /* Sharp LK043T1DG01 */
  161. [1] = {
  162. .name = "Sharp_LK043T1DG01",
  163. .width = 480,
  164. .height = 272,
  165. .hfp = 2,
  166. .hbp = 2,
  167. .hsw = 41,
  168. .vfp = 2,
  169. .vbp = 2,
  170. .vsw = 10,
  171. .pxl_clk = 0x12,
  172. .invert_pxl_clk = 0,
  173. },
  174. };
  175. /* Disable the Raster Engine of the LCD Controller */
  176. static void lcd_disable_raster(struct da8xx_fb_par *par)
  177. {
  178. u32 reg;
  179. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  180. if (reg & LCD_RASTER_ENABLE)
  181. lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  182. }
  183. static void lcd_blit(int load_mode, struct da8xx_fb_par *par)
  184. {
  185. u32 tmp = par->p_palette_base + par->databuf_sz - 4;
  186. u32 reg;
  187. /* Update the databuf in the hw. */
  188. lcdc_write(par->p_palette_base, LCD_DMA_FRM_BUF_BASE_ADDR_0_REG);
  189. lcdc_write(tmp, LCD_DMA_FRM_BUF_CEILING_ADDR_0_REG);
  190. /* Start the DMA. */
  191. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  192. reg &= ~(3 << 20);
  193. if (load_mode == LOAD_DATA)
  194. reg |= LCD_PALETTE_LOAD_MODE(PALETTE_AND_DATA);
  195. else if (load_mode == LOAD_PALETTE)
  196. reg |= LCD_PALETTE_LOAD_MODE(PALETTE_ONLY);
  197. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  198. }
  199. /* Configure the Burst Size of DMA */
  200. static int lcd_cfg_dma(int burst_size)
  201. {
  202. u32 reg;
  203. reg = lcdc_read(LCD_DMA_CTRL_REG) & 0x00000001;
  204. switch (burst_size) {
  205. case 1:
  206. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_1);
  207. break;
  208. case 2:
  209. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_2);
  210. break;
  211. case 4:
  212. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_4);
  213. break;
  214. case 8:
  215. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_8);
  216. break;
  217. case 16:
  218. reg |= LCD_DMA_BURST_SIZE(LCD_DMA_BURST_16);
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. lcdc_write(reg, LCD_DMA_CTRL_REG);
  224. return 0;
  225. }
  226. static void lcd_cfg_ac_bias(int period, int transitions_per_int)
  227. {
  228. u32 reg;
  229. /* Set the AC Bias Period and Number of Transisitons per Interrupt */
  230. reg = lcdc_read(LCD_RASTER_TIMING_2_REG) & 0xFFF00000;
  231. reg |= LCD_AC_BIAS_FREQUENCY(period) |
  232. LCD_AC_BIAS_TRANSITIONS_PER_INT(transitions_per_int);
  233. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  234. }
  235. static void lcd_cfg_horizontal_sync(int back_porch, int pulse_width,
  236. int front_porch)
  237. {
  238. u32 reg;
  239. reg = lcdc_read(LCD_RASTER_TIMING_0_REG) & 0xf;
  240. reg |= ((back_porch & 0xff) << 24)
  241. | ((front_porch & 0xff) << 16)
  242. | ((pulse_width & 0x3f) << 10);
  243. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  244. }
  245. static void lcd_cfg_vertical_sync(int back_porch, int pulse_width,
  246. int front_porch)
  247. {
  248. u32 reg;
  249. reg = lcdc_read(LCD_RASTER_TIMING_1_REG) & 0x3ff;
  250. reg |= ((back_porch & 0xff) << 24)
  251. | ((front_porch & 0xff) << 16)
  252. | ((pulse_width & 0x3f) << 10);
  253. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  254. }
  255. static int lcd_cfg_display(const struct lcd_ctrl_config *cfg)
  256. {
  257. u32 reg;
  258. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(LCD_TFT_MODE |
  259. LCD_MONO_8BIT_MODE |
  260. LCD_MONOCHROME_MODE);
  261. switch (cfg->p_disp_panel->panel_shade) {
  262. case MONOCHROME:
  263. reg |= LCD_MONOCHROME_MODE;
  264. if (cfg->mono_8bit_mode)
  265. reg |= LCD_MONO_8BIT_MODE;
  266. break;
  267. case COLOR_ACTIVE:
  268. reg |= LCD_TFT_MODE;
  269. if (cfg->tft_alt_mode)
  270. reg |= LCD_TFT_ALT_ENABLE;
  271. break;
  272. case COLOR_PASSIVE:
  273. if (cfg->stn_565_mode)
  274. reg |= LCD_STN_565_ENABLE;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. /* enable additional interrupts here */
  280. reg |= LCD_UNDERFLOW_INT_ENA;
  281. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  282. reg = lcdc_read(LCD_RASTER_TIMING_2_REG);
  283. if (cfg->sync_ctrl)
  284. reg |= LCD_SYNC_CTRL;
  285. else
  286. reg &= ~LCD_SYNC_CTRL;
  287. if (cfg->sync_edge)
  288. reg |= LCD_SYNC_EDGE;
  289. else
  290. reg &= ~LCD_SYNC_EDGE;
  291. if (cfg->invert_line_clock)
  292. reg |= LCD_INVERT_LINE_CLOCK;
  293. else
  294. reg &= ~LCD_INVERT_LINE_CLOCK;
  295. if (cfg->invert_frm_clock)
  296. reg |= LCD_INVERT_FRAME_CLOCK;
  297. else
  298. reg &= ~LCD_INVERT_FRAME_CLOCK;
  299. lcdc_write(reg, LCD_RASTER_TIMING_2_REG);
  300. return 0;
  301. }
  302. static int lcd_cfg_frame_buffer(struct da8xx_fb_par *par, u32 width, u32 height,
  303. u32 bpp, u32 raster_order)
  304. {
  305. u32 bpl, reg;
  306. /* Disable Dual Frame Buffer. */
  307. reg = lcdc_read(LCD_DMA_CTRL_REG);
  308. lcdc_write(reg & ~LCD_DUAL_FRAME_BUFFER_ENABLE,
  309. LCD_DMA_CTRL_REG);
  310. /* Set the Panel Width */
  311. /* Pixels per line = (PPL + 1)*16 */
  312. /*0x3F in bits 4..9 gives max horisontal resolution = 1024 pixels*/
  313. width &= 0x3f0;
  314. reg = lcdc_read(LCD_RASTER_TIMING_0_REG);
  315. reg &= 0xfffffc00;
  316. reg |= ((width >> 4) - 1) << 4;
  317. lcdc_write(reg, LCD_RASTER_TIMING_0_REG);
  318. /* Set the Panel Height */
  319. reg = lcdc_read(LCD_RASTER_TIMING_1_REG);
  320. reg = ((height - 1) & 0x3ff) | (reg & 0xfffffc00);
  321. lcdc_write(reg, LCD_RASTER_TIMING_1_REG);
  322. /* Set the Raster Order of the Frame Buffer */
  323. reg = lcdc_read(LCD_RASTER_CTRL_REG) & ~(1 << 8);
  324. if (raster_order)
  325. reg |= LCD_RASTER_ORDER;
  326. lcdc_write(reg, LCD_RASTER_CTRL_REG);
  327. switch (bpp) {
  328. case 1:
  329. case 2:
  330. case 4:
  331. case 16:
  332. par->palette_sz = 16 * 2;
  333. break;
  334. case 8:
  335. par->palette_sz = 256 * 2;
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. bpl = width * bpp / 8;
  341. par->databuf_sz = height * bpl + par->palette_sz;
  342. return 0;
  343. }
  344. static int fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  345. unsigned blue, unsigned transp,
  346. struct fb_info *info)
  347. {
  348. struct da8xx_fb_par *par = info->par;
  349. unsigned short *palette = (unsigned short *)par->v_palette_base;
  350. u_short pal;
  351. if (regno > 255)
  352. return 1;
  353. if (info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  354. return 1;
  355. if (info->var.bits_per_pixel == 8) {
  356. red >>= 4;
  357. green >>= 8;
  358. blue >>= 12;
  359. pal = (red & 0x0f00);
  360. pal |= (green & 0x00f0);
  361. pal |= (blue & 0x000f);
  362. palette[regno] = pal;
  363. } else if ((info->var.bits_per_pixel == 16) && regno < 16) {
  364. red >>= (16 - info->var.red.length);
  365. red <<= info->var.red.offset;
  366. green >>= (16 - info->var.green.length);
  367. green <<= info->var.green.offset;
  368. blue >>= (16 - info->var.blue.length);
  369. blue <<= info->var.blue.offset;
  370. par->pseudo_palette[regno] = red | green | blue;
  371. palette[0] = 0x4000;
  372. }
  373. return 0;
  374. }
  375. static void lcd_reset(struct da8xx_fb_par *par)
  376. {
  377. /* Disable the Raster if previously Enabled */
  378. if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
  379. lcd_disable_raster(par);
  380. /* DMA has to be disabled */
  381. lcdc_write(0, LCD_DMA_CTRL_REG);
  382. lcdc_write(0, LCD_RASTER_CTRL_REG);
  383. }
  384. static int lcd_init(struct da8xx_fb_par *par, const struct lcd_ctrl_config *cfg,
  385. struct da8xx_panel *panel)
  386. {
  387. u32 bpp;
  388. int ret = 0;
  389. lcd_reset(par);
  390. /* Configure the LCD clock divisor. */
  391. lcdc_write(LCD_CLK_DIVISOR(panel->pxl_clk) |
  392. (LCD_RASTER_MODE & 0x1), LCD_CTRL_REG);
  393. if (panel->invert_pxl_clk)
  394. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) |
  395. LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  396. else
  397. lcdc_write((lcdc_read(LCD_RASTER_TIMING_2_REG) &
  398. ~LCD_INVERT_PIXEL_CLOCK), LCD_RASTER_TIMING_2_REG);
  399. /* Configure the DMA burst size. */
  400. ret = lcd_cfg_dma(cfg->dma_burst_sz);
  401. if (ret < 0)
  402. return ret;
  403. /* Configure the AC bias properties. */
  404. lcd_cfg_ac_bias(cfg->ac_bias, cfg->ac_bias_intrpt);
  405. /* Configure the vertical and horizontal sync properties. */
  406. lcd_cfg_vertical_sync(panel->vbp, panel->vsw, panel->vfp);
  407. lcd_cfg_horizontal_sync(panel->hbp, panel->hsw, panel->hfp);
  408. /* Configure for disply */
  409. ret = lcd_cfg_display(cfg);
  410. if (ret < 0)
  411. return ret;
  412. if (QVGA != cfg->p_disp_panel->panel_type)
  413. return -EINVAL;
  414. if (cfg->bpp <= cfg->p_disp_panel->max_bpp &&
  415. cfg->bpp >= cfg->p_disp_panel->min_bpp)
  416. bpp = cfg->bpp;
  417. else
  418. bpp = cfg->p_disp_panel->max_bpp;
  419. if (bpp == 12)
  420. bpp = 16;
  421. ret = lcd_cfg_frame_buffer(par, (unsigned int)panel->width,
  422. (unsigned int)panel->height, bpp,
  423. cfg->raster_order);
  424. if (ret < 0)
  425. return ret;
  426. /* Configure FDD */
  427. lcdc_write((lcdc_read(LCD_RASTER_CTRL_REG) & 0xfff00fff) |
  428. (cfg->fdd << 12), LCD_RASTER_CTRL_REG);
  429. return 0;
  430. }
  431. static irqreturn_t lcdc_irq_handler(int irq, void *arg)
  432. {
  433. u32 stat = lcdc_read(LCD_STAT_REG);
  434. u32 reg;
  435. if ((stat & LCD_SYNC_LOST) && (stat & LCD_FIFO_UNDERFLOW)) {
  436. reg = lcdc_read(LCD_RASTER_CTRL_REG);
  437. lcdc_write(reg & ~LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  438. lcdc_write(stat, LCD_STAT_REG);
  439. lcdc_write(reg | LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  440. } else
  441. lcdc_write(stat, LCD_STAT_REG);
  442. return IRQ_HANDLED;
  443. }
  444. static int fb_check_var(struct fb_var_screeninfo *var,
  445. struct fb_info *info)
  446. {
  447. int err = 0;
  448. switch (var->bits_per_pixel) {
  449. case 1:
  450. case 8:
  451. var->red.offset = 0;
  452. var->red.length = 8;
  453. var->green.offset = 0;
  454. var->green.length = 8;
  455. var->blue.offset = 0;
  456. var->blue.length = 8;
  457. var->transp.offset = 0;
  458. var->transp.length = 0;
  459. break;
  460. case 4:
  461. var->red.offset = 0;
  462. var->red.length = 4;
  463. var->green.offset = 0;
  464. var->green.length = 4;
  465. var->blue.offset = 0;
  466. var->blue.length = 4;
  467. var->transp.offset = 0;
  468. var->transp.length = 0;
  469. break;
  470. case 16: /* RGB 565 */
  471. var->red.offset = 11;
  472. var->red.length = 5;
  473. var->green.offset = 5;
  474. var->green.length = 6;
  475. var->blue.offset = 0;
  476. var->blue.length = 5;
  477. var->transp.offset = 0;
  478. var->transp.length = 0;
  479. break;
  480. default:
  481. err = -EINVAL;
  482. }
  483. var->red.msb_right = 0;
  484. var->green.msb_right = 0;
  485. var->blue.msb_right = 0;
  486. var->transp.msb_right = 0;
  487. return err;
  488. }
  489. static int __devexit fb_remove(struct platform_device *dev)
  490. {
  491. struct fb_info *info = dev_get_drvdata(&dev->dev);
  492. if (info) {
  493. struct da8xx_fb_par *par = info->par;
  494. if (lcdc_read(LCD_RASTER_CTRL_REG) & LCD_RASTER_ENABLE)
  495. lcd_disable_raster(par);
  496. lcdc_write(0, LCD_RASTER_CTRL_REG);
  497. /* disable DMA */
  498. lcdc_write(0, LCD_DMA_CTRL_REG);
  499. unregister_framebuffer(info);
  500. fb_dealloc_cmap(&info->cmap);
  501. dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
  502. info->screen_base - PAGE_SIZE,
  503. info->fix.smem_start);
  504. free_irq(par->irq, par);
  505. clk_disable(par->lcdc_clk);
  506. clk_put(par->lcdc_clk);
  507. framebuffer_release(info);
  508. iounmap((void __iomem *)da8xx_fb_reg_base);
  509. release_mem_region(lcdc_regs->start, resource_size(lcdc_regs));
  510. }
  511. return 0;
  512. }
  513. static int fb_ioctl(struct fb_info *info, unsigned int cmd,
  514. unsigned long arg)
  515. {
  516. struct lcd_sync_arg sync_arg;
  517. switch (cmd) {
  518. case FBIOGET_CONTRAST:
  519. case FBIOPUT_CONTRAST:
  520. case FBIGET_BRIGHTNESS:
  521. case FBIPUT_BRIGHTNESS:
  522. case FBIGET_COLOR:
  523. case FBIPUT_COLOR:
  524. return -ENOTTY;
  525. case FBIPUT_HSYNC:
  526. if (copy_from_user(&sync_arg, (char *)arg,
  527. sizeof(struct lcd_sync_arg)))
  528. return -EFAULT;
  529. lcd_cfg_horizontal_sync(sync_arg.back_porch,
  530. sync_arg.pulse_width,
  531. sync_arg.front_porch);
  532. break;
  533. case FBIPUT_VSYNC:
  534. if (copy_from_user(&sync_arg, (char *)arg,
  535. sizeof(struct lcd_sync_arg)))
  536. return -EFAULT;
  537. lcd_cfg_vertical_sync(sync_arg.back_porch,
  538. sync_arg.pulse_width,
  539. sync_arg.front_porch);
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. return 0;
  545. }
  546. static struct fb_ops da8xx_fb_ops = {
  547. .owner = THIS_MODULE,
  548. .fb_check_var = fb_check_var,
  549. .fb_setcolreg = fb_setcolreg,
  550. .fb_ioctl = fb_ioctl,
  551. .fb_fillrect = cfb_fillrect,
  552. .fb_copyarea = cfb_copyarea,
  553. .fb_imageblit = cfb_imageblit,
  554. };
  555. static int __init fb_probe(struct platform_device *device)
  556. {
  557. struct da8xx_lcdc_platform_data *fb_pdata =
  558. device->dev.platform_data;
  559. struct lcd_ctrl_config *lcd_cfg;
  560. struct da8xx_panel *lcdc_info;
  561. struct fb_info *da8xx_fb_info;
  562. struct clk *fb_clk = NULL;
  563. struct da8xx_fb_par *par;
  564. resource_size_t len;
  565. int ret, i;
  566. if (fb_pdata == NULL) {
  567. dev_err(&device->dev, "Can not get platform data\n");
  568. return -ENOENT;
  569. }
  570. lcdc_regs = platform_get_resource(device, IORESOURCE_MEM, 0);
  571. if (!lcdc_regs) {
  572. dev_err(&device->dev,
  573. "Can not get memory resource for LCD controller\n");
  574. return -ENOENT;
  575. }
  576. len = resource_size(lcdc_regs);
  577. lcdc_regs = request_mem_region(lcdc_regs->start, len, lcdc_regs->name);
  578. if (!lcdc_regs)
  579. return -EBUSY;
  580. da8xx_fb_reg_base = (resource_size_t)ioremap(lcdc_regs->start, len);
  581. if (!da8xx_fb_reg_base) {
  582. ret = -EBUSY;
  583. goto err_request_mem;
  584. }
  585. fb_clk = clk_get(&device->dev, NULL);
  586. if (IS_ERR(fb_clk)) {
  587. dev_err(&device->dev, "Can not get device clock\n");
  588. ret = -ENODEV;
  589. goto err_ioremap;
  590. }
  591. ret = clk_enable(fb_clk);
  592. if (ret)
  593. goto err_clk_put;
  594. for (i = 0, lcdc_info = known_lcd_panels;
  595. i < ARRAY_SIZE(known_lcd_panels);
  596. i++, lcdc_info++) {
  597. if (strcmp(fb_pdata->type, lcdc_info->name) == 0)
  598. break;
  599. }
  600. if (i == ARRAY_SIZE(known_lcd_panels)) {
  601. dev_err(&device->dev, "GLCD: No valid panel found\n");
  602. ret = -ENODEV;
  603. goto err_clk_disable;
  604. } else
  605. dev_info(&device->dev, "GLCD: Found %s panel\n",
  606. fb_pdata->type);
  607. lcd_cfg = (struct lcd_ctrl_config *)fb_pdata->controller_data;
  608. da8xx_fb_info = framebuffer_alloc(sizeof(struct da8xx_fb_par),
  609. &device->dev);
  610. if (!da8xx_fb_info) {
  611. dev_dbg(&device->dev, "Memory allocation failed for fb_info\n");
  612. ret = -ENOMEM;
  613. goto err_clk_disable;
  614. }
  615. par = da8xx_fb_info->par;
  616. if (lcd_init(par, lcd_cfg, lcdc_info) < 0) {
  617. dev_err(&device->dev, "lcd_init failed\n");
  618. ret = -EFAULT;
  619. goto err_release_fb;
  620. }
  621. /* allocate frame buffer */
  622. da8xx_fb_info->screen_base = dma_alloc_coherent(NULL,
  623. par->databuf_sz + PAGE_SIZE,
  624. (resource_size_t *)
  625. &da8xx_fb_info->fix.smem_start,
  626. GFP_KERNEL | GFP_DMA);
  627. if (!da8xx_fb_info->screen_base) {
  628. dev_err(&device->dev,
  629. "GLCD: kmalloc for frame buffer failed\n");
  630. ret = -EINVAL;
  631. goto err_release_fb;
  632. }
  633. /* move palette base pointer by (PAGE_SIZE - palette_sz) bytes */
  634. par->v_palette_base = da8xx_fb_info->screen_base +
  635. (PAGE_SIZE - par->palette_sz);
  636. par->p_palette_base = da8xx_fb_info->fix.smem_start +
  637. (PAGE_SIZE - par->palette_sz);
  638. /* the rest of the frame buffer is pixel data */
  639. da8xx_fb_info->screen_base = par->v_palette_base + par->palette_sz;
  640. da8xx_fb_fix.smem_start = par->p_palette_base + par->palette_sz;
  641. da8xx_fb_fix.smem_len = par->databuf_sz - par->palette_sz;
  642. da8xx_fb_fix.line_length = (lcdc_info->width * lcd_cfg->bpp) / 8;
  643. par->lcdc_clk = fb_clk;
  644. par->irq = platform_get_irq(device, 0);
  645. if (par->irq < 0) {
  646. ret = -ENOENT;
  647. goto err_release_fb_mem;
  648. }
  649. ret = request_irq(par->irq, lcdc_irq_handler, 0, DRIVER_NAME, par);
  650. if (ret)
  651. goto err_release_fb_mem;
  652. /* Initialize par */
  653. da8xx_fb_info->var.bits_per_pixel = lcd_cfg->bpp;
  654. da8xx_fb_var.xres = lcdc_info->width;
  655. da8xx_fb_var.xres_virtual = lcdc_info->width;
  656. da8xx_fb_var.yres = lcdc_info->height;
  657. da8xx_fb_var.yres_virtual = lcdc_info->height;
  658. da8xx_fb_var.grayscale =
  659. lcd_cfg->p_disp_panel->panel_shade == MONOCHROME ? 1 : 0;
  660. da8xx_fb_var.bits_per_pixel = lcd_cfg->bpp;
  661. da8xx_fb_var.hsync_len = lcdc_info->hsw;
  662. da8xx_fb_var.vsync_len = lcdc_info->vsw;
  663. /* Initialize fbinfo */
  664. da8xx_fb_info->flags = FBINFO_FLAG_DEFAULT;
  665. da8xx_fb_info->fix = da8xx_fb_fix;
  666. da8xx_fb_info->var = da8xx_fb_var;
  667. da8xx_fb_info->fbops = &da8xx_fb_ops;
  668. da8xx_fb_info->pseudo_palette = par->pseudo_palette;
  669. da8xx_fb_info->fix.visual = (da8xx_fb_info->var.bits_per_pixel <= 8) ?
  670. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  671. ret = fb_alloc_cmap(&da8xx_fb_info->cmap, PALETTE_SIZE, 0);
  672. if (ret)
  673. goto err_free_irq;
  674. /* First palette_sz byte of the frame buffer is the palette */
  675. da8xx_fb_info->cmap.len = par->palette_sz;
  676. /* Flush the buffer to the screen. */
  677. lcd_blit(LOAD_DATA, par);
  678. /* initialize var_screeninfo */
  679. da8xx_fb_var.activate = FB_ACTIVATE_FORCE;
  680. fb_set_var(da8xx_fb_info, &da8xx_fb_var);
  681. dev_set_drvdata(&device->dev, da8xx_fb_info);
  682. /* Register the Frame Buffer */
  683. if (register_framebuffer(da8xx_fb_info) < 0) {
  684. dev_err(&device->dev,
  685. "GLCD: Frame Buffer Registration Failed!\n");
  686. ret = -EINVAL;
  687. goto err_dealloc_cmap;
  688. }
  689. /* enable raster engine */
  690. lcdc_write(lcdc_read(LCD_RASTER_CTRL_REG) |
  691. LCD_RASTER_ENABLE, LCD_RASTER_CTRL_REG);
  692. return 0;
  693. err_dealloc_cmap:
  694. fb_dealloc_cmap(&da8xx_fb_info->cmap);
  695. err_free_irq:
  696. free_irq(par->irq, par);
  697. err_release_fb_mem:
  698. dma_free_coherent(NULL, par->databuf_sz + PAGE_SIZE,
  699. da8xx_fb_info->screen_base - PAGE_SIZE,
  700. da8xx_fb_info->fix.smem_start);
  701. err_release_fb:
  702. framebuffer_release(da8xx_fb_info);
  703. err_clk_disable:
  704. clk_disable(fb_clk);
  705. err_clk_put:
  706. clk_put(fb_clk);
  707. err_ioremap:
  708. iounmap((void __iomem *)da8xx_fb_reg_base);
  709. err_request_mem:
  710. release_mem_region(lcdc_regs->start, len);
  711. return ret;
  712. }
  713. #ifdef CONFIG_PM
  714. static int fb_suspend(struct platform_device *dev, pm_message_t state)
  715. {
  716. return -EBUSY;
  717. }
  718. static int fb_resume(struct platform_device *dev)
  719. {
  720. return -EBUSY;
  721. }
  722. #else
  723. #define fb_suspend NULL
  724. #define fb_resume NULL
  725. #endif
  726. static struct platform_driver da8xx_fb_driver = {
  727. .probe = fb_probe,
  728. .remove = fb_remove,
  729. .suspend = fb_suspend,
  730. .resume = fb_resume,
  731. .driver = {
  732. .name = DRIVER_NAME,
  733. .owner = THIS_MODULE,
  734. },
  735. };
  736. static int __init da8xx_fb_init(void)
  737. {
  738. return platform_driver_register(&da8xx_fb_driver);
  739. }
  740. static void __exit da8xx_fb_cleanup(void)
  741. {
  742. platform_driver_unregister(&da8xx_fb_driver);
  743. }
  744. module_init(da8xx_fb_init);
  745. module_exit(da8xx_fb_cleanup);
  746. MODULE_DESCRIPTION("Framebuffer driver for TI da8xx/omap-l1xx");
  747. MODULE_AUTHOR("Texas Instruments");
  748. MODULE_LICENSE("GPL");