atmel_lcdfb.c 34 KB

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