atmel_lcdfb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 <linux/backlight.h>
  19. #include <asm/arch/board.h>
  20. #include <asm/arch/cpu.h>
  21. #include <asm/arch/gpio.h>
  22. #include <video/atmel_lcdc.h>
  23. #define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg))
  24. #define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg))
  25. /* configurable parameters */
  26. #define ATMEL_LCDC_CVAL_DEFAULT 0xc8
  27. #define ATMEL_LCDC_DMA_BURST_LEN 8
  28. #if defined(CONFIG_ARCH_AT91SAM9263) || defined(CONFIG_ARCH_AT91CAP9) || \
  29. defined(CONFIG_ARCH_AT91SAM9RL)
  30. #define ATMEL_LCDC_FIFO_SIZE 2048
  31. #else
  32. #define ATMEL_LCDC_FIFO_SIZE 512
  33. #endif
  34. #if defined(CONFIG_ARCH_AT91)
  35. #define ATMEL_LCDFB_FBINFO_DEFAULT FBINFO_DEFAULT
  36. static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  37. struct fb_var_screeninfo *var)
  38. {
  39. }
  40. #elif defined(CONFIG_AVR32)
  41. #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \
  42. | FBINFO_PARTIAL_PAN_OK \
  43. | FBINFO_HWACCEL_XPAN \
  44. | FBINFO_HWACCEL_YPAN)
  45. static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo,
  46. struct fb_var_screeninfo *var)
  47. {
  48. u32 dma2dcfg;
  49. u32 pixeloff;
  50. pixeloff = (var->xoffset * var->bits_per_pixel) & 0x1f;
  51. dma2dcfg = ((var->xres_virtual - var->xres) * var->bits_per_pixel) / 8;
  52. dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET;
  53. lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg);
  54. /* Update configuration */
  55. lcdc_writel(sinfo, ATMEL_LCDC_DMACON,
  56. lcdc_readl(sinfo, ATMEL_LCDC_DMACON)
  57. | ATMEL_LCDC_DMAUPDT);
  58. }
  59. #endif
  60. static const u32 contrast_ctr = ATMEL_LCDC_PS_DIV8
  61. | ATMEL_LCDC_POL_POSITIVE
  62. | ATMEL_LCDC_ENA_PWMENABLE;
  63. #ifdef CONFIG_BACKLIGHT_ATMEL_LCDC
  64. /* some bl->props field just changed */
  65. static int atmel_bl_update_status(struct backlight_device *bl)
  66. {
  67. struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
  68. int power = sinfo->bl_power;
  69. int brightness = bl->props.brightness;
  70. /* REVISIT there may be a meaningful difference between
  71. * fb_blank and power ... there seem to be some cases
  72. * this doesn't handle correctly.
  73. */
  74. if (bl->props.fb_blank != sinfo->bl_power)
  75. power = bl->props.fb_blank;
  76. else if (bl->props.power != sinfo->bl_power)
  77. power = bl->props.power;
  78. if (brightness < 0 && power == FB_BLANK_UNBLANK)
  79. brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
  80. else if (power != FB_BLANK_UNBLANK)
  81. brightness = 0;
  82. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness);
  83. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR,
  84. brightness ? contrast_ctr : 0);
  85. bl->props.fb_blank = bl->props.power = sinfo->bl_power = power;
  86. return 0;
  87. }
  88. static int atmel_bl_get_brightness(struct backlight_device *bl)
  89. {
  90. struct atmel_lcdfb_info *sinfo = bl_get_data(bl);
  91. return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
  92. }
  93. static struct backlight_ops atmel_lcdc_bl_ops = {
  94. .update_status = atmel_bl_update_status,
  95. .get_brightness = atmel_bl_get_brightness,
  96. };
  97. static void init_backlight(struct atmel_lcdfb_info *sinfo)
  98. {
  99. struct backlight_device *bl;
  100. sinfo->bl_power = FB_BLANK_UNBLANK;
  101. if (sinfo->backlight)
  102. return;
  103. bl = backlight_device_register("backlight", &sinfo->pdev->dev,
  104. sinfo, &atmel_lcdc_bl_ops);
  105. if (IS_ERR(sinfo->backlight)) {
  106. dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n",
  107. PTR_ERR(bl));
  108. return;
  109. }
  110. sinfo->backlight = bl;
  111. bl->props.power = FB_BLANK_UNBLANK;
  112. bl->props.fb_blank = FB_BLANK_UNBLANK;
  113. bl->props.max_brightness = 0xff;
  114. bl->props.brightness = atmel_bl_get_brightness(bl);
  115. }
  116. static void exit_backlight(struct atmel_lcdfb_info *sinfo)
  117. {
  118. if (sinfo->backlight)
  119. backlight_device_unregister(sinfo->backlight);
  120. }
  121. #else
  122. static void init_backlight(struct atmel_lcdfb_info *sinfo)
  123. {
  124. dev_warn(&sinfo->pdev->dev, "backlight control is not available\n");
  125. }
  126. static void exit_backlight(struct atmel_lcdfb_info *sinfo)
  127. {
  128. }
  129. #endif
  130. static void init_contrast(struct atmel_lcdfb_info *sinfo)
  131. {
  132. /* have some default contrast/backlight settings */
  133. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr);
  134. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT);
  135. if (sinfo->lcdcon_is_backlight)
  136. init_backlight(sinfo);
  137. }
  138. static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = {
  139. .type = FB_TYPE_PACKED_PIXELS,
  140. .visual = FB_VISUAL_TRUECOLOR,
  141. .xpanstep = 0,
  142. .ypanstep = 0,
  143. .ywrapstep = 0,
  144. .accel = FB_ACCEL_NONE,
  145. };
  146. static unsigned long compute_hozval(unsigned long xres, unsigned long lcdcon2)
  147. {
  148. unsigned long value;
  149. if (!(cpu_is_at91sam9261() || cpu_is_at32ap7000()))
  150. return xres;
  151. value = xres;
  152. if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) {
  153. /* STN display */
  154. if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) {
  155. value *= 3;
  156. }
  157. if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4
  158. || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8
  159. && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL ))
  160. value = DIV_ROUND_UP(value, 4);
  161. else
  162. value = DIV_ROUND_UP(value, 8);
  163. }
  164. return value;
  165. }
  166. static void atmel_lcdfb_update_dma(struct fb_info *info,
  167. struct fb_var_screeninfo *var)
  168. {
  169. struct atmel_lcdfb_info *sinfo = info->par;
  170. struct fb_fix_screeninfo *fix = &info->fix;
  171. unsigned long dma_addr;
  172. dma_addr = (fix->smem_start + var->yoffset * fix->line_length
  173. + var->xoffset * var->bits_per_pixel / 8);
  174. dma_addr &= ~3UL;
  175. /* Set framebuffer DMA base address and pixel offset */
  176. lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr);
  177. atmel_lcdfb_update_dma2d(sinfo, var);
  178. }
  179. static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo)
  180. {
  181. struct fb_info *info = sinfo->info;
  182. dma_free_writecombine(info->device, info->fix.smem_len,
  183. info->screen_base, info->fix.smem_start);
  184. }
  185. /**
  186. * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory
  187. * @sinfo: the frame buffer to allocate memory for
  188. */
  189. static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo)
  190. {
  191. struct fb_info *info = sinfo->info;
  192. struct fb_var_screeninfo *var = &info->var;
  193. info->fix.smem_len = (var->xres_virtual * var->yres_virtual
  194. * ((var->bits_per_pixel + 7) / 8));
  195. info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len,
  196. (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL);
  197. if (!info->screen_base) {
  198. return -ENOMEM;
  199. }
  200. memset(info->screen_base, 0, info->fix.smem_len);
  201. return 0;
  202. }
  203. /**
  204. * atmel_lcdfb_check_var - Validates a var passed in.
  205. * @var: frame buffer variable screen structure
  206. * @info: frame buffer structure that represents a single frame buffer
  207. *
  208. * Checks to see if the hardware supports the state requested by
  209. * var passed in. This function does not alter the hardware
  210. * state!!! This means the data stored in struct fb_info and
  211. * struct atmel_lcdfb_info do not change. This includes the var
  212. * inside of struct fb_info. Do NOT change these. This function
  213. * can be called on its own if we intent to only test a mode and
  214. * not actually set it. The stuff in modedb.c is a example of
  215. * this. If the var passed in is slightly off by what the
  216. * hardware can support then we alter the var PASSED in to what
  217. * we can do. If the hardware doesn't support mode change a
  218. * -EINVAL will be returned by the upper layers. You don't need
  219. * to implement this function then. If you hardware doesn't
  220. * support changing the resolution then this function is not
  221. * needed. In this case the driver would just provide a var that
  222. * represents the static state the screen is in.
  223. *
  224. * Returns negative errno on error, or zero on success.
  225. */
  226. static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var,
  227. struct fb_info *info)
  228. {
  229. struct device *dev = info->device;
  230. struct atmel_lcdfb_info *sinfo = info->par;
  231. unsigned long clk_value_khz;
  232. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  233. dev_dbg(dev, "%s:\n", __func__);
  234. dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres);
  235. dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock));
  236. dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel);
  237. dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz);
  238. if ((PICOS2KHZ(var->pixclock) * var->bits_per_pixel / 8) > clk_value_khz) {
  239. dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock));
  240. return -EINVAL;
  241. }
  242. /* Force same alignment for each line */
  243. var->xres = (var->xres + 3) & ~3UL;
  244. var->xres_virtual = (var->xres_virtual + 3) & ~3UL;
  245. var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0;
  246. var->transp.msb_right = 0;
  247. var->transp.offset = var->transp.length = 0;
  248. var->xoffset = var->yoffset = 0;
  249. /* Saturate vertical and horizontal timings at maximum values */
  250. var->vsync_len = min_t(u32, var->vsync_len,
  251. (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1);
  252. var->upper_margin = min_t(u32, var->upper_margin,
  253. ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET);
  254. var->lower_margin = min_t(u32, var->lower_margin,
  255. ATMEL_LCDC_VFP);
  256. var->right_margin = min_t(u32, var->right_margin,
  257. (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1);
  258. var->hsync_len = min_t(u32, var->hsync_len,
  259. (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1);
  260. var->left_margin = min_t(u32, var->left_margin,
  261. ATMEL_LCDC_HBP + 1);
  262. /* Some parameters can't be zero */
  263. var->vsync_len = max_t(u32, var->vsync_len, 1);
  264. var->right_margin = max_t(u32, var->right_margin, 1);
  265. var->hsync_len = max_t(u32, var->hsync_len, 1);
  266. var->left_margin = max_t(u32, var->left_margin, 1);
  267. switch (var->bits_per_pixel) {
  268. case 1:
  269. case 2:
  270. case 4:
  271. case 8:
  272. var->red.offset = var->green.offset = var->blue.offset = 0;
  273. var->red.length = var->green.length = var->blue.length
  274. = var->bits_per_pixel;
  275. break;
  276. case 15:
  277. case 16:
  278. if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
  279. /* RGB:565 mode */
  280. var->red.offset = 11;
  281. var->blue.offset = 0;
  282. var->green.length = 6;
  283. } else {
  284. /* BGR:555 mode */
  285. var->red.offset = 0;
  286. var->blue.offset = 10;
  287. var->green.length = 5;
  288. }
  289. var->green.offset = 5;
  290. var->red.length = var->blue.length = 5;
  291. break;
  292. case 32:
  293. var->transp.offset = 24;
  294. var->transp.length = 8;
  295. /* fall through */
  296. case 24:
  297. if (sinfo->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) {
  298. /* RGB:888 mode */
  299. var->red.offset = 16;
  300. var->blue.offset = 0;
  301. } else {
  302. /* BGR:888 mode */
  303. var->red.offset = 0;
  304. var->blue.offset = 16;
  305. }
  306. var->green.offset = 8;
  307. var->red.length = var->green.length = var->blue.length = 8;
  308. break;
  309. default:
  310. dev_err(dev, "color depth %d not supported\n",
  311. var->bits_per_pixel);
  312. return -EINVAL;
  313. }
  314. return 0;
  315. }
  316. /**
  317. * atmel_lcdfb_set_par - Alters the hardware state.
  318. * @info: frame buffer structure that represents a single frame buffer
  319. *
  320. * Using the fb_var_screeninfo in fb_info we set the resolution
  321. * of the this particular framebuffer. This function alters the
  322. * par AND the fb_fix_screeninfo stored in fb_info. It doesn't
  323. * not alter var in fb_info since we are using that data. This
  324. * means we depend on the data in var inside fb_info to be
  325. * supported by the hardware. atmel_lcdfb_check_var is always called
  326. * before atmel_lcdfb_set_par to ensure this. Again if you can't
  327. * change the resolution you don't need this function.
  328. *
  329. */
  330. static int atmel_lcdfb_set_par(struct fb_info *info)
  331. {
  332. struct atmel_lcdfb_info *sinfo = info->par;
  333. unsigned long hozval_linesz;
  334. unsigned long value;
  335. unsigned long clk_value_khz;
  336. unsigned long bits_per_line;
  337. dev_dbg(info->device, "%s:\n", __func__);
  338. dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n",
  339. info->var.xres, info->var.yres,
  340. info->var.xres_virtual, info->var.yres_virtual);
  341. /* Turn off the LCD controller and the DMA controller */
  342. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET);
  343. /* Wait for the LCDC core to become idle */
  344. while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY)
  345. msleep(10);
  346. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0);
  347. if (info->var.bits_per_pixel == 1)
  348. info->fix.visual = FB_VISUAL_MONO01;
  349. else if (info->var.bits_per_pixel <= 8)
  350. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  351. else
  352. info->fix.visual = FB_VISUAL_TRUECOLOR;
  353. bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel;
  354. info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8);
  355. /* Re-initialize the DMA engine... */
  356. dev_dbg(info->device, " * update DMA engine\n");
  357. atmel_lcdfb_update_dma(info, &info->var);
  358. /* ...set frame size and burst length = 8 words (?) */
  359. value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32;
  360. value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET);
  361. lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value);
  362. /* Now, the LCDC core... */
  363. /* Set pixel clock */
  364. clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000;
  365. value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock));
  366. if (value < 2) {
  367. dev_notice(info->device, "Bypassing pixel clock divider\n");
  368. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS);
  369. } else {
  370. value = (value / 2) - 1;
  371. dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n",
  372. value);
  373. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1,
  374. value << ATMEL_LCDC_CLKVAL_OFFSET);
  375. info->var.pixclock = KHZ2PICOS(clk_value_khz / (2 * (value + 1)));
  376. dev_dbg(info->device, " updated pixclk: %lu KHz\n",
  377. PICOS2KHZ(info->var.pixclock));
  378. }
  379. /* Initialize control register 2 */
  380. value = sinfo->default_lcdcon2;
  381. if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT))
  382. value |= ATMEL_LCDC_INVLINE_INVERTED;
  383. if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT))
  384. value |= ATMEL_LCDC_INVFRAME_INVERTED;
  385. switch (info->var.bits_per_pixel) {
  386. case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break;
  387. case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break;
  388. case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break;
  389. case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break;
  390. case 15: /* fall through */
  391. case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break;
  392. case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break;
  393. case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break;
  394. default: BUG(); break;
  395. }
  396. dev_dbg(info->device, " * LCDCON2 = %08lx\n", value);
  397. lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value);
  398. /* Vertical timing */
  399. value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET;
  400. value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET;
  401. value |= info->var.lower_margin;
  402. dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value);
  403. lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value);
  404. /* Horizontal timing */
  405. value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET;
  406. value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET;
  407. value |= (info->var.left_margin - 1);
  408. dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value);
  409. lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value);
  410. /* Horizontal value (aka line size) */
  411. hozval_linesz = compute_hozval(info->var.xres,
  412. lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2));
  413. /* Display size */
  414. value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET;
  415. value |= info->var.yres - 1;
  416. dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value);
  417. lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value);
  418. /* FIFO Threshold: Use formula from data sheet */
  419. value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3);
  420. lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value);
  421. /* Toggle LCD_MODE every frame */
  422. lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0);
  423. /* Disable all interrupts */
  424. lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL);
  425. /* ...wait for DMA engine to become idle... */
  426. while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY)
  427. msleep(10);
  428. dev_dbg(info->device, " * re-enable DMA engine\n");
  429. /* ...and enable it with updated configuration */
  430. lcdc_writel(sinfo, ATMEL_LCDC_DMACON, sinfo->default_dmacon);
  431. dev_dbg(info->device, " * re-enable LCDC core\n");
  432. lcdc_writel(sinfo, ATMEL_LCDC_PWRCON,
  433. (sinfo->guard_time << ATMEL_LCDC_GUARDT_OFFSET) | ATMEL_LCDC_PWR);
  434. dev_dbg(info->device, " * DONE\n");
  435. return 0;
  436. }
  437. static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf)
  438. {
  439. chan &= 0xffff;
  440. chan >>= 16 - bf->length;
  441. return chan << bf->offset;
  442. }
  443. /**
  444. * atmel_lcdfb_setcolreg - Optional function. Sets a color register.
  445. * @regno: Which register in the CLUT we are programming
  446. * @red: The red value which can be up to 16 bits wide
  447. * @green: The green value which can be up to 16 bits wide
  448. * @blue: The blue value which can be up to 16 bits wide.
  449. * @transp: If supported the alpha value which can be up to 16 bits wide.
  450. * @info: frame buffer info structure
  451. *
  452. * Set a single color register. The values supplied have a 16 bit
  453. * magnitude which needs to be scaled in this function for the hardware.
  454. * Things to take into consideration are how many color registers, if
  455. * any, are supported with the current color visual. With truecolor mode
  456. * no color palettes are supported. Here a psuedo palette is created
  457. * which we store the value in pseudo_palette in struct fb_info. For
  458. * pseudocolor mode we have a limited color palette. To deal with this
  459. * we can program what color is displayed for a particular pixel value.
  460. * DirectColor is similar in that we can program each color field. If
  461. * we have a static colormap we don't need to implement this function.
  462. *
  463. * Returns negative errno on error, or zero on success. In an
  464. * ideal world, this would have been the case, but as it turns
  465. * out, the other drivers return 1 on failure, so that's what
  466. * we're going to do.
  467. */
  468. static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red,
  469. unsigned int green, unsigned int blue,
  470. unsigned int transp, struct fb_info *info)
  471. {
  472. struct atmel_lcdfb_info *sinfo = info->par;
  473. unsigned int val;
  474. u32 *pal;
  475. int ret = 1;
  476. if (info->var.grayscale)
  477. red = green = blue = (19595 * red + 38470 * green
  478. + 7471 * blue) >> 16;
  479. switch (info->fix.visual) {
  480. case FB_VISUAL_TRUECOLOR:
  481. if (regno < 16) {
  482. pal = info->pseudo_palette;
  483. val = chan_to_field(red, &info->var.red);
  484. val |= chan_to_field(green, &info->var.green);
  485. val |= chan_to_field(blue, &info->var.blue);
  486. pal[regno] = val;
  487. ret = 0;
  488. }
  489. break;
  490. case FB_VISUAL_PSEUDOCOLOR:
  491. if (regno < 256) {
  492. val = ((red >> 11) & 0x001f);
  493. val |= ((green >> 6) & 0x03e0);
  494. val |= ((blue >> 1) & 0x7c00);
  495. /*
  496. * TODO: intensity bit. Maybe something like
  497. * ~(red[10] ^ green[10] ^ blue[10]) & 1
  498. */
  499. lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
  500. ret = 0;
  501. }
  502. break;
  503. case FB_VISUAL_MONO01:
  504. if (regno < 2) {
  505. val = (regno == 0) ? 0x00 : 0x1F;
  506. lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val);
  507. ret = 0;
  508. }
  509. break;
  510. }
  511. return ret;
  512. }
  513. static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var,
  514. struct fb_info *info)
  515. {
  516. dev_dbg(info->device, "%s\n", __func__);
  517. atmel_lcdfb_update_dma(info, var);
  518. return 0;
  519. }
  520. static struct fb_ops atmel_lcdfb_ops = {
  521. .owner = THIS_MODULE,
  522. .fb_check_var = atmel_lcdfb_check_var,
  523. .fb_set_par = atmel_lcdfb_set_par,
  524. .fb_setcolreg = atmel_lcdfb_setcolreg,
  525. .fb_pan_display = atmel_lcdfb_pan_display,
  526. .fb_fillrect = cfb_fillrect,
  527. .fb_copyarea = cfb_copyarea,
  528. .fb_imageblit = cfb_imageblit,
  529. };
  530. static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id)
  531. {
  532. struct fb_info *info = dev_id;
  533. struct atmel_lcdfb_info *sinfo = info->par;
  534. u32 status;
  535. status = lcdc_readl(sinfo, ATMEL_LCDC_ISR);
  536. lcdc_writel(sinfo, ATMEL_LCDC_IDR, status);
  537. return IRQ_HANDLED;
  538. }
  539. static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo)
  540. {
  541. struct fb_info *info = sinfo->info;
  542. int ret = 0;
  543. info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
  544. dev_info(info->device,
  545. "%luKiB frame buffer at %08lx (mapped at %p)\n",
  546. (unsigned long)info->fix.smem_len / 1024,
  547. (unsigned long)info->fix.smem_start,
  548. info->screen_base);
  549. /* Allocate colormap */
  550. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  551. if (ret < 0)
  552. dev_err(info->device, "Alloc color map failed\n");
  553. return ret;
  554. }
  555. static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo)
  556. {
  557. if (sinfo->bus_clk)
  558. clk_enable(sinfo->bus_clk);
  559. clk_enable(sinfo->lcdc_clk);
  560. }
  561. static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo)
  562. {
  563. if (sinfo->bus_clk)
  564. clk_disable(sinfo->bus_clk);
  565. clk_disable(sinfo->lcdc_clk);
  566. }
  567. static int __init atmel_lcdfb_probe(struct platform_device *pdev)
  568. {
  569. struct device *dev = &pdev->dev;
  570. struct fb_info *info;
  571. struct atmel_lcdfb_info *sinfo;
  572. struct atmel_lcdfb_info *pdata_sinfo;
  573. struct resource *regs = NULL;
  574. struct resource *map = NULL;
  575. int ret;
  576. dev_dbg(dev, "%s BEGIN\n", __func__);
  577. ret = -ENOMEM;
  578. info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev);
  579. if (!info) {
  580. dev_err(dev, "cannot allocate memory\n");
  581. goto out;
  582. }
  583. sinfo = info->par;
  584. if (dev->platform_data) {
  585. pdata_sinfo = (struct atmel_lcdfb_info *)dev->platform_data;
  586. sinfo->default_bpp = pdata_sinfo->default_bpp;
  587. sinfo->default_dmacon = pdata_sinfo->default_dmacon;
  588. sinfo->default_lcdcon2 = pdata_sinfo->default_lcdcon2;
  589. sinfo->default_monspecs = pdata_sinfo->default_monspecs;
  590. sinfo->atmel_lcdfb_power_control = pdata_sinfo->atmel_lcdfb_power_control;
  591. sinfo->guard_time = pdata_sinfo->guard_time;
  592. sinfo->lcdcon_is_backlight = pdata_sinfo->lcdcon_is_backlight;
  593. sinfo->lcd_wiring_mode = pdata_sinfo->lcd_wiring_mode;
  594. } else {
  595. dev_err(dev, "cannot get default configuration\n");
  596. goto free_info;
  597. }
  598. sinfo->info = info;
  599. sinfo->pdev = pdev;
  600. strcpy(info->fix.id, sinfo->pdev->name);
  601. info->flags = ATMEL_LCDFB_FBINFO_DEFAULT;
  602. info->pseudo_palette = sinfo->pseudo_palette;
  603. info->fbops = &atmel_lcdfb_ops;
  604. memcpy(&info->monspecs, sinfo->default_monspecs, sizeof(info->monspecs));
  605. info->fix = atmel_lcdfb_fix;
  606. /* Enable LCDC Clocks */
  607. if (cpu_is_at91sam9261() || cpu_is_at32ap7000()) {
  608. sinfo->bus_clk = clk_get(dev, "hck1");
  609. if (IS_ERR(sinfo->bus_clk)) {
  610. ret = PTR_ERR(sinfo->bus_clk);
  611. goto free_info;
  612. }
  613. }
  614. sinfo->lcdc_clk = clk_get(dev, "lcdc_clk");
  615. if (IS_ERR(sinfo->lcdc_clk)) {
  616. ret = PTR_ERR(sinfo->lcdc_clk);
  617. goto put_bus_clk;
  618. }
  619. atmel_lcdfb_start_clock(sinfo);
  620. ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb,
  621. info->monspecs.modedb_len, info->monspecs.modedb,
  622. sinfo->default_bpp);
  623. if (!ret) {
  624. dev_err(dev, "no suitable video mode found\n");
  625. goto stop_clk;
  626. }
  627. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. if (!regs) {
  629. dev_err(dev, "resources unusable\n");
  630. ret = -ENXIO;
  631. goto stop_clk;
  632. }
  633. sinfo->irq_base = platform_get_irq(pdev, 0);
  634. if (sinfo->irq_base < 0) {
  635. dev_err(dev, "unable to get irq\n");
  636. ret = sinfo->irq_base;
  637. goto stop_clk;
  638. }
  639. /* Initialize video memory */
  640. map = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  641. if (map) {
  642. /* use a pre-allocated memory buffer */
  643. info->fix.smem_start = map->start;
  644. info->fix.smem_len = map->end - map->start + 1;
  645. if (!request_mem_region(info->fix.smem_start,
  646. info->fix.smem_len, pdev->name)) {
  647. ret = -EBUSY;
  648. goto stop_clk;
  649. }
  650. info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
  651. if (!info->screen_base)
  652. goto release_intmem;
  653. /*
  654. * Don't clear the framebuffer -- someone may have set
  655. * up a splash image.
  656. */
  657. } else {
  658. /* alocate memory buffer */
  659. ret = atmel_lcdfb_alloc_video_memory(sinfo);
  660. if (ret < 0) {
  661. dev_err(dev, "cannot allocate framebuffer: %d\n", ret);
  662. goto stop_clk;
  663. }
  664. }
  665. /* LCDC registers */
  666. info->fix.mmio_start = regs->start;
  667. info->fix.mmio_len = regs->end - regs->start + 1;
  668. if (!request_mem_region(info->fix.mmio_start,
  669. info->fix.mmio_len, pdev->name)) {
  670. ret = -EBUSY;
  671. goto free_fb;
  672. }
  673. sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len);
  674. if (!sinfo->mmio) {
  675. dev_err(dev, "cannot map LCDC registers\n");
  676. goto release_mem;
  677. }
  678. /* Initialize PWM for contrast or backlight ("off") */
  679. init_contrast(sinfo);
  680. /* interrupt */
  681. ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info);
  682. if (ret) {
  683. dev_err(dev, "request_irq failed: %d\n", ret);
  684. goto unmap_mmio;
  685. }
  686. ret = atmel_lcdfb_init_fbinfo(sinfo);
  687. if (ret < 0) {
  688. dev_err(dev, "init fbinfo failed: %d\n", ret);
  689. goto unregister_irqs;
  690. }
  691. /*
  692. * This makes sure that our colour bitfield
  693. * descriptors are correctly initialised.
  694. */
  695. atmel_lcdfb_check_var(&info->var, info);
  696. ret = fb_set_var(info, &info->var);
  697. if (ret) {
  698. dev_warn(dev, "unable to set display parameters\n");
  699. goto free_cmap;
  700. }
  701. dev_set_drvdata(dev, info);
  702. /*
  703. * Tell the world that we're ready to go
  704. */
  705. ret = register_framebuffer(info);
  706. if (ret < 0) {
  707. dev_err(dev, "failed to register framebuffer device: %d\n", ret);
  708. goto free_cmap;
  709. }
  710. /* Power up the LCDC screen */
  711. if (sinfo->atmel_lcdfb_power_control)
  712. sinfo->atmel_lcdfb_power_control(1);
  713. dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %lu\n",
  714. info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base);
  715. return 0;
  716. free_cmap:
  717. fb_dealloc_cmap(&info->cmap);
  718. unregister_irqs:
  719. free_irq(sinfo->irq_base, info);
  720. unmap_mmio:
  721. exit_backlight(sinfo);
  722. iounmap(sinfo->mmio);
  723. release_mem:
  724. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  725. free_fb:
  726. if (map)
  727. iounmap(info->screen_base);
  728. else
  729. atmel_lcdfb_free_video_memory(sinfo);
  730. release_intmem:
  731. if (map)
  732. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  733. stop_clk:
  734. atmel_lcdfb_stop_clock(sinfo);
  735. clk_put(sinfo->lcdc_clk);
  736. put_bus_clk:
  737. if (sinfo->bus_clk)
  738. clk_put(sinfo->bus_clk);
  739. free_info:
  740. framebuffer_release(info);
  741. out:
  742. dev_dbg(dev, "%s FAILED\n", __func__);
  743. return ret;
  744. }
  745. static int __exit atmel_lcdfb_remove(struct platform_device *pdev)
  746. {
  747. struct device *dev = &pdev->dev;
  748. struct fb_info *info = dev_get_drvdata(dev);
  749. struct atmel_lcdfb_info *sinfo = info->par;
  750. if (!sinfo)
  751. return 0;
  752. exit_backlight(sinfo);
  753. if (sinfo->atmel_lcdfb_power_control)
  754. sinfo->atmel_lcdfb_power_control(0);
  755. unregister_framebuffer(info);
  756. atmel_lcdfb_stop_clock(sinfo);
  757. clk_put(sinfo->lcdc_clk);
  758. if (sinfo->bus_clk)
  759. clk_put(sinfo->bus_clk);
  760. fb_dealloc_cmap(&info->cmap);
  761. free_irq(sinfo->irq_base, info);
  762. iounmap(sinfo->mmio);
  763. release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
  764. if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) {
  765. iounmap(info->screen_base);
  766. release_mem_region(info->fix.smem_start, info->fix.smem_len);
  767. } else {
  768. atmel_lcdfb_free_video_memory(sinfo);
  769. }
  770. dev_set_drvdata(dev, NULL);
  771. framebuffer_release(info);
  772. return 0;
  773. }
  774. #ifdef CONFIG_PM
  775. static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg)
  776. {
  777. struct fb_info *info = platform_get_drvdata(pdev);
  778. struct atmel_lcdfb_info *sinfo = info->par;
  779. sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL);
  780. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0);
  781. if (sinfo->atmel_lcdfb_power_control)
  782. sinfo->atmel_lcdfb_power_control(0);
  783. atmel_lcdfb_stop_clock(sinfo);
  784. return 0;
  785. }
  786. static int atmel_lcdfb_resume(struct platform_device *pdev)
  787. {
  788. struct fb_info *info = platform_get_drvdata(pdev);
  789. struct atmel_lcdfb_info *sinfo = info->par;
  790. atmel_lcdfb_start_clock(sinfo);
  791. if (sinfo->atmel_lcdfb_power_control)
  792. sinfo->atmel_lcdfb_power_control(1);
  793. lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon);
  794. return 0;
  795. }
  796. #else
  797. #define atmel_lcdfb_suspend NULL
  798. #define atmel_lcdfb_resume NULL
  799. #endif
  800. static struct platform_driver atmel_lcdfb_driver = {
  801. .remove = __exit_p(atmel_lcdfb_remove),
  802. .suspend = atmel_lcdfb_suspend,
  803. .resume = atmel_lcdfb_resume,
  804. .driver = {
  805. .name = "atmel_lcdfb",
  806. .owner = THIS_MODULE,
  807. },
  808. };
  809. static int __init atmel_lcdfb_init(void)
  810. {
  811. return platform_driver_probe(&atmel_lcdfb_driver, atmel_lcdfb_probe);
  812. }
  813. static void __exit atmel_lcdfb_exit(void)
  814. {
  815. platform_driver_unregister(&atmel_lcdfb_driver);
  816. }
  817. module_init(atmel_lcdfb_init);
  818. module_exit(atmel_lcdfb_exit);
  819. MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver");
  820. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  821. MODULE_LICENSE("GPL");