atmel_lcdfb.c 23 KB

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