atmel_lcdfb.c 31 KB

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