atmel_lcdfb.c 30 KB

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