nuc900fb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. *
  3. * Copyright (c) 2009 Nuvoton technology corporation
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Description:
  12. * Nuvoton LCD Controller Driver
  13. * Author:
  14. * Wang Qiang (rurality.linux@gmail.com) 2009/12/11
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/string.h>
  21. #include <linux/mm.h>
  22. #include <linux/tty.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/fb.h>
  26. #include <linux/init.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/wait.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/clk.h>
  33. #include <linux/cpufreq.h>
  34. #include <linux/io.h>
  35. #include <linux/pm.h>
  36. #include <linux/device.h>
  37. #include <mach/map.h>
  38. #include <mach/regs-clock.h>
  39. #include <mach/regs-ldm.h>
  40. #include <linux/platform_data/video-nuc900fb.h>
  41. #include "nuc900fb.h"
  42. /*
  43. * Initialize the nuc900 video (dual) buffer address
  44. */
  45. static void nuc900fb_set_lcdaddr(struct fb_info *info)
  46. {
  47. struct nuc900fb_info *fbi = info->par;
  48. void __iomem *regs = fbi->io;
  49. unsigned long vbaddr1, vbaddr2;
  50. vbaddr1 = info->fix.smem_start;
  51. vbaddr2 = info->fix.smem_start;
  52. vbaddr2 += info->fix.line_length * info->var.yres;
  53. /* set frambuffer start phy addr*/
  54. writel(vbaddr1, regs + REG_LCM_VA_BADDR0);
  55. writel(vbaddr2, regs + REG_LCM_VA_BADDR1);
  56. writel(fbi->regs.lcd_va_fbctrl, regs + REG_LCM_VA_FBCTRL);
  57. writel(fbi->regs.lcd_va_scale, regs + REG_LCM_VA_SCALE);
  58. }
  59. /*
  60. * calculate divider for lcd div
  61. */
  62. static unsigned int nuc900fb_calc_pixclk(struct nuc900fb_info *fbi,
  63. unsigned long pixclk)
  64. {
  65. unsigned long clk = fbi->clk_rate;
  66. unsigned long long div;
  67. /* pixclk is in picseconds. our clock is in Hz*/
  68. /* div = (clk * pixclk)/10^12 */
  69. div = (unsigned long long)clk * pixclk;
  70. div >>= 12;
  71. do_div(div, 625 * 625UL * 625);
  72. dev_dbg(fbi->dev, "pixclk %ld, divisor is %lld\n", pixclk, div);
  73. return div;
  74. }
  75. /*
  76. * Check the video params of 'var'.
  77. */
  78. static int nuc900fb_check_var(struct fb_var_screeninfo *var,
  79. struct fb_info *info)
  80. {
  81. struct nuc900fb_info *fbi = info->par;
  82. struct nuc900fb_mach_info *mach_info = dev_get_platdata(fbi->dev);
  83. struct nuc900fb_display *display = NULL;
  84. struct nuc900fb_display *default_display = mach_info->displays +
  85. mach_info->default_display;
  86. int i;
  87. dev_dbg(fbi->dev, "check_var(var=%p, info=%p)\n", var, info);
  88. /* validate x/y resolution */
  89. /* choose default mode if possible */
  90. if (var->xres == default_display->xres &&
  91. var->yres == default_display->yres &&
  92. var->bits_per_pixel == default_display->bpp)
  93. display = default_display;
  94. else
  95. for (i = 0; i < mach_info->num_displays; i++)
  96. if (var->xres == mach_info->displays[i].xres &&
  97. var->yres == mach_info->displays[i].yres &&
  98. var->bits_per_pixel == mach_info->displays[i].bpp) {
  99. display = mach_info->displays + i;
  100. break;
  101. }
  102. if (display == NULL) {
  103. printk(KERN_ERR "wrong resolution or depth %dx%d at %d bit per pixel\n",
  104. var->xres, var->yres, var->bits_per_pixel);
  105. return -EINVAL;
  106. }
  107. /* it should be the same size as the display */
  108. var->xres_virtual = display->xres;
  109. var->yres_virtual = display->yres;
  110. var->height = display->height;
  111. var->width = display->width;
  112. /* copy lcd settings */
  113. var->pixclock = display->pixclock;
  114. var->left_margin = display->left_margin;
  115. var->right_margin = display->right_margin;
  116. var->upper_margin = display->upper_margin;
  117. var->lower_margin = display->lower_margin;
  118. var->vsync_len = display->vsync_len;
  119. var->hsync_len = display->hsync_len;
  120. var->transp.offset = 0;
  121. var->transp.length = 0;
  122. fbi->regs.lcd_dccs = display->dccs;
  123. fbi->regs.lcd_device_ctrl = display->devctl;
  124. fbi->regs.lcd_va_fbctrl = display->fbctrl;
  125. fbi->regs.lcd_va_scale = display->scale;
  126. /* set R/G/B possions */
  127. switch (var->bits_per_pixel) {
  128. case 1:
  129. case 2:
  130. case 4:
  131. case 8:
  132. default:
  133. var->red.offset = 0;
  134. var->red.length = var->bits_per_pixel;
  135. var->green = var->red;
  136. var->blue = var->red;
  137. break;
  138. case 12:
  139. var->red.length = 4;
  140. var->green.length = 4;
  141. var->blue.length = 4;
  142. var->red.offset = 8;
  143. var->green.offset = 4;
  144. var->blue.offset = 0;
  145. break;
  146. case 16:
  147. var->red.length = 5;
  148. var->green.length = 6;
  149. var->blue.length = 5;
  150. var->red.offset = 11;
  151. var->green.offset = 5;
  152. var->blue.offset = 0;
  153. break;
  154. case 18:
  155. var->red.length = 6;
  156. var->green.length = 6;
  157. var->blue.length = 6;
  158. var->red.offset = 12;
  159. var->green.offset = 6;
  160. var->blue.offset = 0;
  161. break;
  162. case 32:
  163. var->red.length = 8;
  164. var->green.length = 8;
  165. var->blue.length = 8;
  166. var->red.offset = 16;
  167. var->green.offset = 8;
  168. var->blue.offset = 0;
  169. break;
  170. }
  171. return 0;
  172. }
  173. /*
  174. * Calculate lcd register values from var setting & save into hw
  175. */
  176. static void nuc900fb_calculate_lcd_regs(const struct fb_info *info,
  177. struct nuc900fb_hw *regs)
  178. {
  179. const struct fb_var_screeninfo *var = &info->var;
  180. int vtt = var->height + var->upper_margin + var->lower_margin;
  181. int htt = var->width + var->left_margin + var->right_margin;
  182. int hsync = var->width + var->right_margin;
  183. int vsync = var->height + var->lower_margin;
  184. regs->lcd_crtc_size = LCM_CRTC_SIZE_VTTVAL(vtt) |
  185. LCM_CRTC_SIZE_HTTVAL(htt);
  186. regs->lcd_crtc_dend = LCM_CRTC_DEND_VDENDVAL(var->height) |
  187. LCM_CRTC_DEND_HDENDVAL(var->width);
  188. regs->lcd_crtc_hr = LCM_CRTC_HR_EVAL(var->width + 5) |
  189. LCM_CRTC_HR_SVAL(var->width + 1);
  190. regs->lcd_crtc_hsync = LCM_CRTC_HSYNC_EVAL(hsync + var->hsync_len) |
  191. LCM_CRTC_HSYNC_SVAL(hsync);
  192. regs->lcd_crtc_vr = LCM_CRTC_VR_EVAL(vsync + var->vsync_len) |
  193. LCM_CRTC_VR_SVAL(vsync);
  194. }
  195. /*
  196. * Activate (set) the controller from the given framebuffer
  197. * information
  198. */
  199. static void nuc900fb_activate_var(struct fb_info *info)
  200. {
  201. struct nuc900fb_info *fbi = info->par;
  202. void __iomem *regs = fbi->io;
  203. struct fb_var_screeninfo *var = &info->var;
  204. int clkdiv;
  205. clkdiv = nuc900fb_calc_pixclk(fbi, var->pixclock) - 1;
  206. if (clkdiv < 0)
  207. clkdiv = 0;
  208. nuc900fb_calculate_lcd_regs(info, &fbi->regs);
  209. /* set the new lcd registers*/
  210. dev_dbg(fbi->dev, "new lcd register set:\n");
  211. dev_dbg(fbi->dev, "dccs = 0x%08x\n", fbi->regs.lcd_dccs);
  212. dev_dbg(fbi->dev, "dev_ctl = 0x%08x\n", fbi->regs.lcd_device_ctrl);
  213. dev_dbg(fbi->dev, "crtc_size = 0x%08x\n", fbi->regs.lcd_crtc_size);
  214. dev_dbg(fbi->dev, "crtc_dend = 0x%08x\n", fbi->regs.lcd_crtc_dend);
  215. dev_dbg(fbi->dev, "crtc_hr = 0x%08x\n", fbi->regs.lcd_crtc_hr);
  216. dev_dbg(fbi->dev, "crtc_hsync = 0x%08x\n", fbi->regs.lcd_crtc_hsync);
  217. dev_dbg(fbi->dev, "crtc_vr = 0x%08x\n", fbi->regs.lcd_crtc_vr);
  218. writel(fbi->regs.lcd_device_ctrl, regs + REG_LCM_DEV_CTRL);
  219. writel(fbi->regs.lcd_crtc_size, regs + REG_LCM_CRTC_SIZE);
  220. writel(fbi->regs.lcd_crtc_dend, regs + REG_LCM_CRTC_DEND);
  221. writel(fbi->regs.lcd_crtc_hr, regs + REG_LCM_CRTC_HR);
  222. writel(fbi->regs.lcd_crtc_hsync, regs + REG_LCM_CRTC_HSYNC);
  223. writel(fbi->regs.lcd_crtc_vr, regs + REG_LCM_CRTC_VR);
  224. /* set lcd address pointers */
  225. nuc900fb_set_lcdaddr(info);
  226. writel(fbi->regs.lcd_dccs, regs + REG_LCM_DCCS);
  227. }
  228. /*
  229. * Alters the hardware state.
  230. *
  231. */
  232. static int nuc900fb_set_par(struct fb_info *info)
  233. {
  234. struct fb_var_screeninfo *var = &info->var;
  235. switch (var->bits_per_pixel) {
  236. case 32:
  237. case 24:
  238. case 18:
  239. case 16:
  240. case 12:
  241. info->fix.visual = FB_VISUAL_TRUECOLOR;
  242. break;
  243. case 1:
  244. info->fix.visual = FB_VISUAL_MONO01;
  245. break;
  246. default:
  247. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  248. break;
  249. }
  250. info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;
  251. /* activate this new configuration */
  252. nuc900fb_activate_var(info);
  253. return 0;
  254. }
  255. static inline unsigned int chan_to_field(unsigned int chan,
  256. struct fb_bitfield *bf)
  257. {
  258. chan &= 0xffff;
  259. chan >>= 16 - bf->length;
  260. return chan << bf->offset;
  261. }
  262. static int nuc900fb_setcolreg(unsigned regno,
  263. unsigned red, unsigned green, unsigned blue,
  264. unsigned transp, struct fb_info *info)
  265. {
  266. unsigned int val;
  267. switch (info->fix.visual) {
  268. case FB_VISUAL_TRUECOLOR:
  269. /* true-colour, use pseuo-palette */
  270. if (regno < 16) {
  271. u32 *pal = info->pseudo_palette;
  272. val = chan_to_field(red, &info->var.red);
  273. val |= chan_to_field(green, &info->var.green);
  274. val |= chan_to_field(blue, &info->var.blue);
  275. pal[regno] = val;
  276. }
  277. break;
  278. default:
  279. return 1; /* unknown type */
  280. }
  281. return 0;
  282. }
  283. /**
  284. * nuc900fb_blank
  285. *
  286. */
  287. static int nuc900fb_blank(int blank_mode, struct fb_info *info)
  288. {
  289. return 0;
  290. }
  291. static struct fb_ops nuc900fb_ops = {
  292. .owner = THIS_MODULE,
  293. .fb_check_var = nuc900fb_check_var,
  294. .fb_set_par = nuc900fb_set_par,
  295. .fb_blank = nuc900fb_blank,
  296. .fb_setcolreg = nuc900fb_setcolreg,
  297. .fb_fillrect = cfb_fillrect,
  298. .fb_copyarea = cfb_copyarea,
  299. .fb_imageblit = cfb_imageblit,
  300. };
  301. static inline void modify_gpio(void __iomem *reg,
  302. unsigned long set, unsigned long mask)
  303. {
  304. unsigned long tmp;
  305. tmp = readl(reg) & ~mask;
  306. writel(tmp | set, reg);
  307. }
  308. /*
  309. * Initialise LCD-related registers
  310. */
  311. static int nuc900fb_init_registers(struct fb_info *info)
  312. {
  313. struct nuc900fb_info *fbi = info->par;
  314. struct nuc900fb_mach_info *mach_info = dev_get_platdata(fbi->dev);
  315. void __iomem *regs = fbi->io;
  316. /*reset the display engine*/
  317. writel(0, regs + REG_LCM_DCCS);
  318. writel(readl(regs + REG_LCM_DCCS) | LCM_DCCS_ENG_RST,
  319. regs + REG_LCM_DCCS);
  320. ndelay(100);
  321. writel(readl(regs + REG_LCM_DCCS) & (~LCM_DCCS_ENG_RST),
  322. regs + REG_LCM_DCCS);
  323. ndelay(100);
  324. writel(0, regs + REG_LCM_DEV_CTRL);
  325. /* config gpio output */
  326. modify_gpio(W90X900_VA_GPIO + 0x54, mach_info->gpio_dir,
  327. mach_info->gpio_dir_mask);
  328. modify_gpio(W90X900_VA_GPIO + 0x58, mach_info->gpio_data,
  329. mach_info->gpio_data_mask);
  330. return 0;
  331. }
  332. /*
  333. * Alloc the SDRAM region of NUC900 for the frame buffer.
  334. * The buffer should be a non-cached, non-buffered, memory region
  335. * to allow palette and pixel writes without flushing the cache.
  336. */
  337. static int nuc900fb_map_video_memory(struct fb_info *info)
  338. {
  339. struct nuc900fb_info *fbi = info->par;
  340. dma_addr_t map_dma;
  341. unsigned long map_size = PAGE_ALIGN(info->fix.smem_len);
  342. dev_dbg(fbi->dev, "nuc900fb_map_video_memory(fbi=%p) map_size %lu\n",
  343. fbi, map_size);
  344. info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,
  345. &map_dma, GFP_KERNEL);
  346. if (!info->screen_base)
  347. return -ENOMEM;
  348. memset(info->screen_base, 0x00, map_size);
  349. info->fix.smem_start = map_dma;
  350. return 0;
  351. }
  352. static inline void nuc900fb_unmap_video_memory(struct fb_info *info)
  353. {
  354. struct nuc900fb_info *fbi = info->par;
  355. dma_free_writecombine(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
  356. info->screen_base, info->fix.smem_start);
  357. }
  358. static irqreturn_t nuc900fb_irqhandler(int irq, void *dev_id)
  359. {
  360. struct nuc900fb_info *fbi = dev_id;
  361. void __iomem *regs = fbi->io;
  362. void __iomem *irq_base = fbi->irq_base;
  363. unsigned long lcdirq = readl(regs + REG_LCM_INT_CS);
  364. if (lcdirq & LCM_INT_CS_DISP_F_STATUS) {
  365. writel(readl(irq_base) | 1<<30, irq_base);
  366. /* wait VA_EN low */
  367. if ((readl(regs + REG_LCM_DCCS) &
  368. LCM_DCCS_SINGLE) == LCM_DCCS_SINGLE)
  369. while ((readl(regs + REG_LCM_DCCS) &
  370. LCM_DCCS_VA_EN) == LCM_DCCS_VA_EN)
  371. ;
  372. /* display_out-enable */
  373. writel(readl(regs + REG_LCM_DCCS) | LCM_DCCS_DISP_OUT_EN,
  374. regs + REG_LCM_DCCS);
  375. /* va-enable*/
  376. writel(readl(regs + REG_LCM_DCCS) | LCM_DCCS_VA_EN,
  377. regs + REG_LCM_DCCS);
  378. } else if (lcdirq & LCM_INT_CS_UNDERRUN_INT) {
  379. writel(readl(irq_base) | LCM_INT_CS_UNDERRUN_INT, irq_base);
  380. } else if (lcdirq & LCM_INT_CS_BUS_ERROR_INT) {
  381. writel(readl(irq_base) | LCM_INT_CS_BUS_ERROR_INT, irq_base);
  382. }
  383. return IRQ_HANDLED;
  384. }
  385. #ifdef CONFIG_CPU_FREQ
  386. static int nuc900fb_cpufreq_transition(struct notifier_block *nb,
  387. unsigned long val, void *data)
  388. {
  389. struct nuc900fb_info *info;
  390. struct fb_info *fbinfo;
  391. long delta_f;
  392. info = container_of(nb, struct nuc900fb_info, freq_transition);
  393. fbinfo = platform_get_drvdata(to_platform_device(info->dev));
  394. delta_f = info->clk_rate - clk_get_rate(info->clk);
  395. if ((val == CPUFREQ_POSTCHANGE && delta_f > 0) ||
  396. (val == CPUFREQ_PRECHANGE && delta_f < 0)) {
  397. info->clk_rate = clk_get_rate(info->clk);
  398. nuc900fb_activate_var(fbinfo);
  399. }
  400. return 0;
  401. }
  402. static inline int nuc900fb_cpufreq_register(struct nuc900fb_info *fbi)
  403. {
  404. fbi->freq_transition.notifier_call = nuc900fb_cpufreq_transition;
  405. return cpufreq_register_notifier(&fbi->freq_transition,
  406. CPUFREQ_TRANSITION_NOTIFIER);
  407. }
  408. static inline void nuc900fb_cpufreq_deregister(struct nuc900fb_info *fbi)
  409. {
  410. cpufreq_unregister_notifier(&fbi->freq_transition,
  411. CPUFREQ_TRANSITION_NOTIFIER);
  412. }
  413. #else
  414. static inline int nuc900fb_cpufreq_transition(struct notifier_block *nb,
  415. unsigned long val, void *data)
  416. {
  417. return 0;
  418. }
  419. static inline int nuc900fb_cpufreq_register(struct nuc900fb_info *fbi)
  420. {
  421. return 0;
  422. }
  423. static inline void nuc900fb_cpufreq_deregister(struct nuc900fb_info *info)
  424. {
  425. }
  426. #endif
  427. static char driver_name[] = "nuc900fb";
  428. static int nuc900fb_probe(struct platform_device *pdev)
  429. {
  430. struct nuc900fb_info *fbi;
  431. struct nuc900fb_display *display;
  432. struct fb_info *fbinfo;
  433. struct nuc900fb_mach_info *mach_info;
  434. struct resource *res;
  435. int ret;
  436. int irq;
  437. int i;
  438. int size;
  439. dev_dbg(&pdev->dev, "devinit\n");
  440. mach_info = dev_get_platdata(&pdev->dev);
  441. if (mach_info == NULL) {
  442. dev_err(&pdev->dev,
  443. "no platform data for lcd, cannot attach\n");
  444. return -EINVAL;
  445. }
  446. if (mach_info->default_display > mach_info->num_displays) {
  447. dev_err(&pdev->dev,
  448. "default display No. is %d but only %d displays \n",
  449. mach_info->default_display, mach_info->num_displays);
  450. return -EINVAL;
  451. }
  452. display = mach_info->displays + mach_info->default_display;
  453. irq = platform_get_irq(pdev, 0);
  454. if (irq < 0) {
  455. dev_err(&pdev->dev, "no irq for device\n");
  456. return -ENOENT;
  457. }
  458. fbinfo = framebuffer_alloc(sizeof(struct nuc900fb_info), &pdev->dev);
  459. if (!fbinfo)
  460. return -ENOMEM;
  461. platform_set_drvdata(pdev, fbinfo);
  462. fbi = fbinfo->par;
  463. fbi->dev = &pdev->dev;
  464. #ifdef CONFIG_CPU_NUC950
  465. fbi->drv_type = LCDDRV_NUC950;
  466. #endif
  467. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  468. size = resource_size(res);
  469. fbi->mem = request_mem_region(res->start, size, pdev->name);
  470. if (fbi->mem == NULL) {
  471. dev_err(&pdev->dev, "failed to alloc memory region\n");
  472. ret = -ENOENT;
  473. goto free_fb;
  474. }
  475. fbi->io = ioremap(res->start, size);
  476. if (fbi->io == NULL) {
  477. dev_err(&pdev->dev, "ioremap() of lcd registers failed\n");
  478. ret = -ENXIO;
  479. goto release_mem_region;
  480. }
  481. fbi->irq_base = fbi->io + REG_LCM_INT_CS;
  482. /* Stop the LCD */
  483. writel(0, fbi->io + REG_LCM_DCCS);
  484. /* fill the fbinfo*/
  485. strcpy(fbinfo->fix.id, driver_name);
  486. fbinfo->fix.type = FB_TYPE_PACKED_PIXELS;
  487. fbinfo->fix.type_aux = 0;
  488. fbinfo->fix.xpanstep = 0;
  489. fbinfo->fix.ypanstep = 0;
  490. fbinfo->fix.ywrapstep = 0;
  491. fbinfo->fix.accel = FB_ACCEL_NONE;
  492. fbinfo->var.nonstd = 0;
  493. fbinfo->var.activate = FB_ACTIVATE_NOW;
  494. fbinfo->var.accel_flags = 0;
  495. fbinfo->var.vmode = FB_VMODE_NONINTERLACED;
  496. fbinfo->fbops = &nuc900fb_ops;
  497. fbinfo->flags = FBINFO_FLAG_DEFAULT;
  498. fbinfo->pseudo_palette = &fbi->pseudo_pal;
  499. ret = request_irq(irq, nuc900fb_irqhandler, 0, pdev->name, fbi);
  500. if (ret) {
  501. dev_err(&pdev->dev, "cannot register irq handler %d -err %d\n",
  502. irq, ret);
  503. ret = -EBUSY;
  504. goto release_regs;
  505. }
  506. fbi->clk = clk_get(&pdev->dev, NULL);
  507. if (IS_ERR(fbi->clk)) {
  508. printk(KERN_ERR "nuc900-lcd:failed to get lcd clock source\n");
  509. ret = PTR_ERR(fbi->clk);
  510. goto release_irq;
  511. }
  512. clk_enable(fbi->clk);
  513. dev_dbg(&pdev->dev, "got and enabled clock\n");
  514. fbi->clk_rate = clk_get_rate(fbi->clk);
  515. /* calutate the video buffer size */
  516. for (i = 0; i < mach_info->num_displays; i++) {
  517. unsigned long smem_len = mach_info->displays[i].xres;
  518. smem_len *= mach_info->displays[i].yres;
  519. smem_len *= mach_info->displays[i].bpp;
  520. smem_len >>= 3;
  521. if (fbinfo->fix.smem_len < smem_len)
  522. fbinfo->fix.smem_len = smem_len;
  523. }
  524. /* Initialize Video Memory */
  525. ret = nuc900fb_map_video_memory(fbinfo);
  526. if (ret) {
  527. printk(KERN_ERR "Failed to allocate video RAM: %x\n", ret);
  528. goto release_clock;
  529. }
  530. dev_dbg(&pdev->dev, "got video memory\n");
  531. fbinfo->var.xres = display->xres;
  532. fbinfo->var.yres = display->yres;
  533. fbinfo->var.bits_per_pixel = display->bpp;
  534. nuc900fb_init_registers(fbinfo);
  535. nuc900fb_check_var(&fbinfo->var, fbinfo);
  536. ret = nuc900fb_cpufreq_register(fbi);
  537. if (ret < 0) {
  538. dev_err(&pdev->dev, "Failed to register cpufreq\n");
  539. goto free_video_memory;
  540. }
  541. ret = register_framebuffer(fbinfo);
  542. if (ret) {
  543. printk(KERN_ERR "failed to register framebuffer device: %d\n",
  544. ret);
  545. goto free_cpufreq;
  546. }
  547. fb_info(fbinfo, "%s frame buffer device\n", fbinfo->fix.id);
  548. return 0;
  549. free_cpufreq:
  550. nuc900fb_cpufreq_deregister(fbi);
  551. free_video_memory:
  552. nuc900fb_unmap_video_memory(fbinfo);
  553. release_clock:
  554. clk_disable(fbi->clk);
  555. clk_put(fbi->clk);
  556. release_irq:
  557. free_irq(irq, fbi);
  558. release_regs:
  559. iounmap(fbi->io);
  560. release_mem_region:
  561. release_mem_region(res->start, size);
  562. free_fb:
  563. framebuffer_release(fbinfo);
  564. return ret;
  565. }
  566. /*
  567. * shutdown the lcd controller
  568. */
  569. static void nuc900fb_stop_lcd(struct fb_info *info)
  570. {
  571. struct nuc900fb_info *fbi = info->par;
  572. void __iomem *regs = fbi->io;
  573. writel((~LCM_DCCS_DISP_INT_EN) | (~LCM_DCCS_VA_EN) | (~LCM_DCCS_OSD_EN),
  574. regs + REG_LCM_DCCS);
  575. }
  576. /*
  577. * Cleanup
  578. */
  579. static int nuc900fb_remove(struct platform_device *pdev)
  580. {
  581. struct fb_info *fbinfo = platform_get_drvdata(pdev);
  582. struct nuc900fb_info *fbi = fbinfo->par;
  583. int irq;
  584. nuc900fb_stop_lcd(fbinfo);
  585. msleep(1);
  586. unregister_framebuffer(fbinfo);
  587. nuc900fb_cpufreq_deregister(fbi);
  588. nuc900fb_unmap_video_memory(fbinfo);
  589. iounmap(fbi->io);
  590. irq = platform_get_irq(pdev, 0);
  591. free_irq(irq, fbi);
  592. release_resource(fbi->mem);
  593. kfree(fbi->mem);
  594. framebuffer_release(fbinfo);
  595. return 0;
  596. }
  597. #ifdef CONFIG_PM
  598. /*
  599. * suspend and resume support for the lcd controller
  600. */
  601. static int nuc900fb_suspend(struct platform_device *dev, pm_message_t state)
  602. {
  603. struct fb_info *fbinfo = platform_get_drvdata(dev);
  604. struct nuc900fb_info *info = fbinfo->par;
  605. nuc900fb_stop_lcd(fbinfo);
  606. msleep(1);
  607. clk_disable(info->clk);
  608. return 0;
  609. }
  610. static int nuc900fb_resume(struct platform_device *dev)
  611. {
  612. struct fb_info *fbinfo = platform_get_drvdata(dev);
  613. struct nuc900fb_info *fbi = fbinfo->par;
  614. printk(KERN_INFO "nuc900fb resume\n");
  615. clk_enable(fbi->clk);
  616. msleep(1);
  617. nuc900fb_init_registers(fbinfo);
  618. nuc900fb_activate_var(fbinfo);
  619. return 0;
  620. }
  621. #else
  622. #define nuc900fb_suspend NULL
  623. #define nuc900fb_resume NULL
  624. #endif
  625. static struct platform_driver nuc900fb_driver = {
  626. .probe = nuc900fb_probe,
  627. .remove = nuc900fb_remove,
  628. .suspend = nuc900fb_suspend,
  629. .resume = nuc900fb_resume,
  630. .driver = {
  631. .name = "nuc900-lcd",
  632. },
  633. };
  634. module_platform_driver(nuc900fb_driver);
  635. MODULE_DESCRIPTION("Framebuffer driver for the NUC900");
  636. MODULE_LICENSE("GPL");