atmel_lcdfb.c 27 KB

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