atmel_lcdfb.c 34 KB

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