atmel_lcdfb.c 30 KB

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