atmel_lcdfb.c 23 KB

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