atmel_lcdfb.c 31 KB

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