atmel_lcdfb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Driver for AT91/AT32 LCD Controller
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file COPYING in the main directory of this archive for
  8. * more details.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/clk.h>
  15. #include <linux/fb.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <asm/arch/board.h>
  19. #include <asm/arch/cpu.h>
  20. #include <asm/arch/gpio.h>
  21. #include <video/atmel_lcdc.h>
  22. #define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
  23. #define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
  24. /* configurable parameters */
  25. #define ATMEL_LCDC_CVAL_DEFAULT 0xc8
  26. #define ATMEL_LCDC_DMA_BURST_LEN 8
  27. #if defined(CONFIG_ARCH_AT91SAM9263)
  28. #define ATMEL_LCDC_FIFO_SIZE 2048
  29. #else
  30. #define ATMEL_LCDC_FIFO_SIZE 512
  31. #endif
  32. #if defined(CONFIG_ARCH_AT91)
  33. #define ATMEL_LCDFB_FBINFO_DEFAULT FBINFO_DEFAULT
  34. static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  35. struct fb_var_screeninfo *var)
  36. {
  37. }
  38. #elif defined(CONFIG_AVR32)
  39. #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
  40. | FBINFO_PARTIAL_PAN_OK \
  41. | FBINFO_HWACCEL_XPAN \
  42. | FBINFO_HWACCEL_YPAN)
  43. static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  44. struct fb_var_screeninfo *var)
  45. {
  46. u32 dma2dcfg;
  47. u32 pixeloff;
  48. pixeloff = (var->xoffset * var->bits_per_pixel) & 0x1f;
  49. dma2dcfg = ((var->xres_virtual - var->xres) * var->bits_per_pixel) / 8;
  50. dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
  51. lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
  52. /* Update configuration */
  53. lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
  54. lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
  55. | ATMEL_LCDC_DMAUPDT);
  56. }
  57. #endif
  58. static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
  59. .type = FB_TYPE_PACKED_PIXELS,
  60. .visual = FB_VISUAL_TRUECOLOR,
  61. .xpanstep = 0,
  62. .ypanstep = 0,
  63. .ywrapstep = 0,
  64. .accel = FB_ACCEL_NONE,
  65. };
  66. static void atmel_lcdfb_update_dma(struct fb_info *info,
  67. struct fb_var_screeninfo *var)
  68. {
  69. struct atmel_lcdfb_info *sinfo = info->par;
  70. struct fb_fix_screeninfo *fix = &info->fix;
  71. unsigned long dma_addr;
  72. dma_addr = (fix->smem_start + var->yoffset * fix->line_length
  73. + var->xoffset * var->bits_per_pixel / 8);
  74. dma_addr &= ~3UL;
  75. /* Set framebuffer DMA base address and pixel offset */
  76. lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
  77. atmel_lcdfb_update_dma2d(sinfo, var);
  78. }
  79. static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
  80. {
  81. struct fb_info *info = sinfo->info;
  82. dma_free_writecombine(info->device, info->fix.smem_len,
  83. info->screen_base, info->fix.smem_start);
  84. }
  85. /**
  86. * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
  87. * @sinfo: the frame buffer to allocate memory for
  88. */
  89. static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
  90. {
  91. struct fb_info *info = sinfo->info;
  92. struct fb_var_screeninfo *var = &info->var;
  93. info->fix.smem_len = (var->xres_virtual * var->yres_virtual
  94. * ((var->bits_per_pixel + 7) / 8));
  95. info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
  96. (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
  97. if (!info->screen_base) {
  98. return -ENOMEM;
  99. }
  100. return 0;
  101. }
  102. /**
  103. * atmel_lcdfb_check_var - Validates a var passed in.
  104. * @var: frame buffer variable screen structure
  105. * @info: frame buffer structure that represents a single frame buffer
  106. *
  107. * Checks to see if the hardware supports the state requested by
  108. * var passed in. This function does not alter the hardware
  109. * state!!! This means the data stored in struct fb_info and
  110. * struct atmel_lcdfb_info do not change. This includes the var
  111. * inside of struct fb_info. Do NOT change these. This function
  112. * can be called on its own if we intent to only test a mode and
  113. * not actually set it. The stuff in modedb.c is a example of
  114. * this. If the var passed in is slightly off by what the
  115. * hardware can support then we alter the var PASSED in to what
  116. * we can do. If the hardware doesn't support mode change a
  117. * -EINVAL will be returned by the upper layers. You don't need
  118. * to implement this function then. If you hardware doesn't
  119. * support changing the resolution then this function is not
  120. * needed. In this case the driver would just provide a var that
  121. * represents the static state the screen is in.
  122. *
  123. * Returns negative errno on error, or zero on success.
  124. */
  125. static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
  126. struct fb_info *info)
  127. {
  128. struct device *dev = info->device;
  129. struct atmel_lcdfb_info *sinfo = info->par;
  130. unsigned long clk_value_khz;
  131. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  132. dev_dbg(dev, "%s:\n", __func__);
  133. dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
  134. dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
  135. dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
  136. dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
  137. if ((PICOS2KHZ(var->pixclock) * var->bits_per_pixel / 8) > clk_value_khz) {
  138. dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
  139. return -EINVAL;
  140. }
  141. /* Force same alignment for each line */
  142. var->xres = (var->xres + 3) & ~3UL;
  143. var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
  144. var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
  145. var->transp.msb_right = 0;
  146. var->transp.offset = var->transp.length = 0;
  147. var->xoffset = var->yoffset = 0;
  148. switch (var->bits_per_pixel) {
  149. case 2:
  150. case 4:
  151. case 8:
  152. var->red.offset = var->green.offset = var->blue.offset = 0;
  153. var->red.length = var->green.length = var->blue.length
  154. = var->bits_per_pixel;
  155. break;
  156. case 15:
  157. case 16:
  158. var->red.offset = 0;
  159. var->green.offset = 5;
  160. var->blue.offset = 10;
  161. var->red.length = var->green.length = var->blue.length = 5;
  162. break;
  163. case 24:
  164. case 32:
  165. var->red.offset = 0;
  166. var->green.offset = 8;
  167. var->blue.offset = 16;
  168. var->red.length = var->green.length = var->blue.length = 8;
  169. break;
  170. default:
  171. dev_err(dev, "color depth %d not supported\n",
  172. var->bits_per_pixel);
  173. return -EINVAL;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * atmel_lcdfb_set_par - Alters the hardware state.
  179. * @info: frame buffer structure that represents a single frame buffer
  180. *
  181. * Using the fb_var_screeninfo in fb_info we set the resolution
  182. * of the this particular framebuffer. This function alters the
  183. * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
  184. * not alter var in fb_info since we are using that data. This
  185. * means we depend on the data in var inside fb_info to be
  186. * supported by the hardware. atmel_lcdfb_check_var is always called
  187. * before atmel_lcdfb_set_par to ensure this. Again if you can't
  188. * change the resolution you don't need this function.
  189. *
  190. */
  191. static int atmel_lcdfb_set_par(struct fb_info *info)
  192. {
  193. struct atmel_lcdfb_info *sinfo = info->par;
  194. unsigned long value;
  195. unsigned long clk_value_khz;
  196. dev_dbg(info->device, "%s:\n", __func__);
  197. dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
  198. info->var.xres, info->var.yres,
  199. info->var.xres_virtual, info->var.yres_virtual);
  200. /* Turn off the LCD controller and the DMA controller */
  201. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
  202. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
  203. if (info->var.bits_per_pixel <= 8)
  204. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  205. else
  206. info->fix.visual = FB_VISUAL_TRUECOLOR;
  207. info->fix.line_length = info->var.xres_virtual * (info->var.bits_per_pixel / 8);
  208. /* Re-initialize the DMA engine... */
  209. dev_dbg(info->device, " * update DMA engine\n");
  210. atmel_lcdfb_update_dma(info, &info->var);
  211. /* ...set frame size and burst length = 8 words (?) */
  212. value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
  213. value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
  214. lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
  215. /* Now, the LCDC core... */
  216. /* Set pixel clock */
  217. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  218. value = clk_value_khz / PICOS2KHZ(info->var.pixclock);
  219. if (clk_value_khz % PICOS2KHZ(info->var.pixclock))
  220. value++;
  221. value = (value / 2) - 1;
  222. if (value <= 0) {
  223. dev_notice(info->device, "Bypassing pixel clock divider\n");
  224. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
  225. } else
  226. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, value << ATMEL_LCDC_CLKVAL_OFFSET);
  227. /* Initialize control register 2 */
  228. value = sinfo->default_lcdcon2;
  229. if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
  230. value |= ATMEL_LCDC_INVLINE_INVERTED;
  231. if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
  232. value |= ATMEL_LCDC_INVFRAME_INVERTED;
  233. switch (info->var.bits_per_pixel) {
  234. case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
  235. case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
  236. case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
  237. case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
  238. case 15: /* fall through */
  239. case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
  240. case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
  241. case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
  242. default: BUG(); break;
  243. }
  244. dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
  245. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
  246. /* Vertical timing */
  247. value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
  248. value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
  249. value |= info->var.lower_margin;
  250. dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
  251. lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
  252. /* Horizontal timing */
  253. value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
  254. value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
  255. value |= (info->var.left_margin - 1);
  256. dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
  257. lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
  258. /* Display size */
  259. value = (info->var.xres - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
  260. value |= info->var.yres - 1;
  261. lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
  262. /* FIFO Threshold: Use formula from data sheet */
  263. value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
  264. lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
  265. /* Toggle LCD_MODE every frame */
  266. lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
  267. /* Disable all interrupts */
  268. lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
  269. /* Set contrast */
  270. value = ATMEL_LCDC_PS_DIV8 | ATMEL_LCDC_POL_POSITIVE | ATMEL_LCDC_ENA_PWMENABLE;
  271. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, value);
  272. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
  273. /* ...wait for DMA engine to become idle... */
  274. while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
  275. msleep(10);
  276. dev_dbg(info->device, " * re-enable DMA engine\n");
  277. /* ...and enable it with updated configuration */
  278. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
  279. dev_dbg(info->device, " * re-enable LCDC core\n");
  280. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
  281. (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
  282. dev_dbg(info->device, " * DONE\n");
  283. return 0;
  284. }
  285. static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
  286. {
  287. chan &= 0xffff;
  288. chan >>= 16 - bf->length;
  289. return chan << bf->offset;
  290. }
  291. /**
  292. * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
  293. * @regno: Which register in the CLUT we are programming
  294. * @red: The red value which can be up to 16 bits wide
  295. * @green: The green value which can be up to 16 bits wide
  296. * @blue: The blue value which can be up to 16 bits wide.
  297. * @transp: If supported the alpha value which can be up to 16 bits wide.
  298. * @info: frame buffer info structure
  299. *
  300. * Set a single color register. The values supplied have a 16 bit
  301. * magnitude which needs to be scaled in this function for the hardware.
  302. * Things to take into consideration are how many color registers, if
  303. * any, are supported with the current color visual. With truecolor mode
  304. * no color palettes are supported. Here a psuedo palette is created
  305. * which we store the value in pseudo_palette in struct fb_info. For
  306. * pseudocolor mode we have a limited color palette. To deal with this
  307. * we can program what color is displayed for a particular pixel value.
  308. * DirectColor is similar in that we can program each color field. If
  309. * we have a static colormap we don't need to implement this function.
  310. *
  311. * Returns negative errno on error, or zero on success. In an
  312. * ideal world, this would have been the case, but as it turns
  313. * out, the other drivers return 1 on failure, so that's what
  314. * we're going to do.
  315. */
  316. static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  317. unsigned int green, unsigned int blue,
  318. unsigned int transp, struct fb_info *info)
  319. {
  320. struct atmel_lcdfb_info *sinfo = info->par;
  321. unsigned int val;
  322. u32 *pal;
  323. int ret = 1;
  324. if (info->var.grayscale)
  325. red = green = blue = (19595 * red + 38470 * green
  326. + 7471 * blue) >> 16;
  327. switch (info->fix.visual) {
  328. case FB_VISUAL_TRUECOLOR:
  329. if (regno < 16) {
  330. pal = info->pseudo_palette;
  331. val = chan_to_field(red, &info->var.red);
  332. val |= chan_to_field(green, &info->var.green);
  333. val |= chan_to_field(blue, &info->var.blue);
  334. pal[regno] = val;
  335. ret = 0;
  336. }
  337. break;
  338. case FB_VISUAL_PSEUDOCOLOR:
  339. if (regno < 256) {
  340. val = ((red >> 11) & 0x001f);
  341. val |= ((green >> 6) & 0x03e0);
  342. val |= ((blue >> 1) & 0x7c00);
  343. /*
  344. * TODO: intensity bit. Maybe something like
  345. * ~(red[10] ^ green[10] ^ blue[10]) & 1
  346. */
  347. lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
  348. ret = 0;
  349. }
  350. break;
  351. }
  352. return ret;
  353. }
  354. static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
  355. struct fb_info *info)
  356. {
  357. dev_dbg(info->device, "%s\n", __func__);
  358. atmel_lcdfb_update_dma(info, var);
  359. return 0;
  360. }
  361. static struct fb_ops atmel_lcdfb_ops = {
  362. .owner = THIS_MODULE,
  363. .fb_check_var = atmel_lcdfb_check_var,
  364. .fb_set_par = atmel_lcdfb_set_par,
  365. .fb_setcolreg = atmel_lcdfb_setcolreg,
  366. .fb_pan_display = atmel_lcdfb_pan_display,
  367. .fb_fillrect = cfb_fillrect,
  368. .fb_copyarea = cfb_copyarea,
  369. .fb_imageblit = cfb_imageblit,
  370. };
  371. static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
  372. {
  373. struct fb_info *info = dev_id;
  374. struct atmel_lcdfb_info *sinfo = info->par;
  375. u32 status;
  376. status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
  377. lcdc_writel(sinfo, ATMEL_LCDC_IDR, status);
  378. return IRQ_HANDLED;
  379. }
  380. static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
  381. {
  382. struct fb_info *info = sinfo->info;
  383. int ret = 0;
  384. memset_io(info->screen_base, 0, info->fix.smem_len);
  385. info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
  386. dev_info(info->device,
  387. "%luKiB frame buffer at %08lx (mapped at %p)\n",
  388. (unsigned long)info->fix.smem_len / 1024,
  389. (unsigned long)info->fix.smem_start,
  390. info->screen_base);
  391. /* Allocate colormap */
  392. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  393. if (ret < 0)
  394. dev_err(info->device, "Alloc color map failed\n");
  395. return ret;
  396. }
  397. static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
  398. {
  399. if (sinfo->bus_clk)
  400. clk_enable(sinfo->bus_clk);
  401. clk_enable(sinfo->lcdc_clk);
  402. }
  403. static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
  404. {
  405. if (sinfo->bus_clk)
  406. clk_disable(sinfo->bus_clk);
  407. clk_disable(sinfo->lcdc_clk);
  408. }
  409. static int __init atmel_lcdfb_probe(struct platform_device *pdev)
  410. {
  411. struct device *dev = &pdev->dev;
  412. struct fb_info *info;
  413. struct atmel_lcdfb_info *sinfo;
  414. struct atmel_lcdfb_info *pdata_sinfo;
  415. struct resource *regs = NULL;
  416. struct resource *map = NULL;
  417. int ret;
  418. dev_dbg(dev, "%s BEGIN\n", __func__);
  419. ret = -ENOMEM;
  420. info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
  421. if (!info) {
  422. dev_err(dev, "cannot allocate memory\n");
  423. goto out;
  424. }
  425. sinfo = info->par;
  426. if (dev->platform_data) {
  427. pdata_sinfo = (struct atmel_lcdfb_info *)dev->platform_data;
  428. sinfo->default_bpp = pdata_sinfo->default_bpp;
  429. sinfo->default_dmacon = pdata_sinfo->default_dmacon;
  430. sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
  431. sinfo->default_monspecs = pdata_sinfo->default_monspecs;
  432. sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
  433. sinfo->guard_time = pdata_sinfo->guard_time;
  434. } else {
  435. dev_err(dev, "cannot get default configuration\n");
  436. goto free_info;
  437. }
  438. sinfo->info = info;
  439. sinfo->pdev = pdev;
  440. strcpy(info->fix.id, sinfo->pdev->name);
  441. info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
  442. info->pseudo_palette = sinfo->pseudo_palette;
  443. info->fbops = &atmel_lcdfb_ops;
  444. memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
  445. info->fix = atmel_lcdfb_fix;
  446. /* Enable LCDC Clocks */
  447. if (cpu_is_at91sam9261() || cpu_is_at32ap7000()) {
  448. sinfo->bus_clk = clk_get(dev, "hck1");
  449. if (IS_ERR(sinfo->bus_clk)) {
  450. ret = PTR_ERR(sinfo->bus_clk);
  451. goto free_info;
  452. }
  453. }
  454. sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
  455. if (IS_ERR(sinfo->lcdc_clk)) {
  456. ret = PTR_ERR(sinfo->lcdc_clk);
  457. goto put_bus_clk;
  458. }
  459. atmel_lcdfb_start_clock(sinfo);
  460. ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
  461. info->monspecs.modedb_len, info->monspecs.modedb,
  462. sinfo->default_bpp);
  463. if (!ret) {
  464. dev_err(dev, "no suitable video mode found\n");
  465. goto stop_clk;
  466. }
  467. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  468. if (!regs) {
  469. dev_err(dev, "resources unusable\n");
  470. ret = -ENXIO;
  471. goto stop_clk;
  472. }
  473. sinfo->irq_base = platform_get_irq(pdev, 0);
  474. if (sinfo->irq_base < 0) {
  475. dev_err(dev, "unable to get irq\n");
  476. ret = sinfo->irq_base;
  477. goto stop_clk;
  478. }
  479. /* Initialize video memory */
  480. map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  481. if (map) {
  482. /* use a pre-allocated memory buffer */
  483. info->fix.smem_start = map->start;
  484. info->fix.smem_len = map->end - map->start + 1;
  485. if (!request_mem_region(info->fix.smem_start,
  486. info->fix.smem_len, pdev->name)) {
  487. ret = -EBUSY;
  488. goto stop_clk;
  489. }
  490. info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
  491. if (!info->screen_base)
  492. goto release_intmem;
  493. } else {
  494. /* alocate memory buffer */
  495. ret = atmel_lcdfb_alloc_video_memory(sinfo);
  496. if (ret < 0) {
  497. dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
  498. goto stop_clk;
  499. }
  500. }
  501. /* LCDC registers */
  502. info->fix.mmio_start = regs->start;
  503. info->fix.mmio_len = regs->end - regs->start + 1;
  504. if (!request_mem_region(info->fix.mmio_start,
  505. info->fix.mmio_len, pdev->name)) {
  506. ret = -EBUSY;
  507. goto free_fb;
  508. }
  509. sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
  510. if (!sinfo->mmio) {
  511. dev_err(dev, "cannot map LCDC registers\n");
  512. goto release_mem;
  513. }
  514. /* interrupt */
  515. ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
  516. if (ret) {
  517. dev_err(dev, "request_irq failed: %d\n", ret);
  518. goto unmap_mmio;
  519. }
  520. ret = atmel_lcdfb_init_fbinfo(sinfo);
  521. if (ret < 0) {
  522. dev_err(dev, "init fbinfo failed: %d\n", ret);
  523. goto unregister_irqs;
  524. }
  525. /*
  526. * This makes sure that our colour bitfield
  527. * descriptors are correctly initialised.
  528. */
  529. atmel_lcdfb_check_var(&info->var, info);
  530. ret = fb_set_var(info, &info->var);
  531. if (ret) {
  532. dev_warn(dev, "unable to set display parameters\n");
  533. goto free_cmap;
  534. }
  535. dev_set_drvdata(dev, info);
  536. /*
  537. * Tell the world that we're ready to go
  538. */
  539. ret = register_framebuffer(info);
  540. if (ret < 0) {
  541. dev_err(dev, "failed to register framebuffer device: %d\n", ret);
  542. goto free_cmap;
  543. }
  544. /* Power up the LCDC screen */
  545. if (sinfo->atmel_lcdfb_power_control)
  546. sinfo->atmel_lcdfb_power_control(1);
  547. dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %lu\n",
  548. info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
  549. return 0;
  550. free_cmap:
  551. fb_dealloc_cmap(&info->cmap);
  552. unregister_irqs:
  553. free_irq(sinfo->irq_base, info);
  554. unmap_mmio:
  555. iounmap(sinfo->mmio);
  556. release_mem:
  557. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  558. free_fb:
  559. if (map)
  560. iounmap(info->screen_base);
  561. else
  562. atmel_lcdfb_free_video_memory(sinfo);
  563. release_intmem:
  564. if (map)
  565. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  566. stop_clk:
  567. atmel_lcdfb_stop_clock(sinfo);
  568. clk_put(sinfo->lcdc_clk);
  569. put_bus_clk:
  570. if (sinfo->bus_clk)
  571. clk_put(sinfo->bus_clk);
  572. free_info:
  573. framebuffer_release(info);
  574. out:
  575. dev_dbg(dev, "%s FAILED\n", __func__);
  576. return ret;
  577. }
  578. static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
  579. {
  580. struct device *dev = &pdev->dev;
  581. struct fb_info *info = dev_get_drvdata(dev);
  582. struct atmel_lcdfb_info *sinfo = info->par;
  583. if (!sinfo)
  584. return 0;
  585. if (sinfo->atmel_lcdfb_power_control)
  586. sinfo->atmel_lcdfb_power_control(0);
  587. unregister_framebuffer(info);
  588. atmel_lcdfb_stop_clock(sinfo);
  589. clk_put(sinfo->lcdc_clk);
  590. if (sinfo->bus_clk)
  591. clk_put(sinfo->bus_clk);
  592. fb_dealloc_cmap(&info->cmap);
  593. free_irq(sinfo->irq_base, info);
  594. iounmap(sinfo->mmio);
  595. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  596. if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
  597. iounmap(info->screen_base);
  598. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  599. } else {
  600. atmel_lcdfb_free_video_memory(sinfo);
  601. }
  602. dev_set_drvdata(dev, NULL);
  603. framebuffer_release(info);
  604. return 0;
  605. }
  606. static struct platform_driver atmel_lcdfb_driver = {
  607. .remove = __exit_p(atmel_lcdfb_remove),
  608. .driver = {
  609. .name = "atmel_lcdfb",
  610. .owner = THIS_MODULE,
  611. },
  612. };
  613. static int __init atmel_lcdfb_init(void)
  614. {
  615. return platform_driver_probe(&atmel_lcdfb_driver, atmel_lcdfb_probe);
  616. }
  617. static void __exit atmel_lcdfb_exit(void)
  618. {
  619. platform_driver_unregister(&atmel_lcdfb_driver);
  620. }
  621. module_init(atmel_lcdfb_init);
  622. module_exit(atmel_lcdfb_exit);
  623. MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
  624. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@rfo.atmel.com>");
  625. MODULE_LICENSE("GPL");