atmel_lcdfb.c 30 KB

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