atmel_lcdfb.c 31 KB

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