atmel_lcdfb.c 24 KB

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