atmel_lcdfb.c 31 KB

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