amba-clcd.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. * linux/drivers/video/amba-clcd.c
  3. *
  4. * Copyright (C) 2001 ARM Limited, by David A Rusling
  5. * Updated to 2.5, Deep Blue Solutions Ltd.
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive
  9. * for more details.
  10. *
  11. * ARM PrimeCell PL110 Color LCD Controller
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/delay.h>
  20. #include <linux/mm.h>
  21. #include <linux/fb.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/list.h>
  25. #include <linux/amba/bus.h>
  26. #include <linux/amba/clcd.h>
  27. #include <linux/bitops.h>
  28. #include <linux/clk.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_graph.h>
  33. #include <linux/backlight.h>
  34. #include <video/display_timing.h>
  35. #include <video/of_display_timing.h>
  36. #include <video/videomode.h>
  37. #include "amba-clcd-nomadik.h"
  38. #include "amba-clcd-versatile.h"
  39. #define to_clcd(info) container_of(info, struct clcd_fb, fb)
  40. /* This is limited to 16 characters when displayed by X startup */
  41. static const char *clcd_name = "CLCD FB";
  42. /*
  43. * Unfortunately, the enable/disable functions may be called either from
  44. * process or IRQ context, and we _need_ to delay. This is _not_ good.
  45. */
  46. static inline void clcdfb_sleep(unsigned int ms)
  47. {
  48. if (in_atomic()) {
  49. mdelay(ms);
  50. } else {
  51. msleep(ms);
  52. }
  53. }
  54. static inline void clcdfb_set_start(struct clcd_fb *fb)
  55. {
  56. unsigned long ustart = fb->fb.fix.smem_start;
  57. unsigned long lstart;
  58. ustart += fb->fb.var.yoffset * fb->fb.fix.line_length;
  59. lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2;
  60. writel(ustart, fb->regs + CLCD_UBAS);
  61. writel(lstart, fb->regs + CLCD_LBAS);
  62. }
  63. static void clcdfb_disable(struct clcd_fb *fb)
  64. {
  65. u32 val;
  66. if (fb->board->disable)
  67. fb->board->disable(fb);
  68. if (fb->panel->backlight) {
  69. fb->panel->backlight->props.power = FB_BLANK_POWERDOWN;
  70. backlight_update_status(fb->panel->backlight);
  71. }
  72. val = readl(fb->regs + fb->off_cntl);
  73. if (val & CNTL_LCDPWR) {
  74. val &= ~CNTL_LCDPWR;
  75. writel(val, fb->regs + fb->off_cntl);
  76. clcdfb_sleep(20);
  77. }
  78. if (val & CNTL_LCDEN) {
  79. val &= ~CNTL_LCDEN;
  80. writel(val, fb->regs + fb->off_cntl);
  81. }
  82. /*
  83. * Disable CLCD clock source.
  84. */
  85. if (fb->clk_enabled) {
  86. fb->clk_enabled = false;
  87. clk_disable(fb->clk);
  88. }
  89. }
  90. static void clcdfb_enable(struct clcd_fb *fb, u32 cntl)
  91. {
  92. /*
  93. * Enable the CLCD clock source.
  94. */
  95. if (!fb->clk_enabled) {
  96. fb->clk_enabled = true;
  97. clk_enable(fb->clk);
  98. }
  99. /*
  100. * Bring up by first enabling..
  101. */
  102. cntl |= CNTL_LCDEN;
  103. writel(cntl, fb->regs + fb->off_cntl);
  104. clcdfb_sleep(20);
  105. /*
  106. * and now apply power.
  107. */
  108. cntl |= CNTL_LCDPWR;
  109. writel(cntl, fb->regs + fb->off_cntl);
  110. /*
  111. * Turn on backlight
  112. */
  113. if (fb->panel->backlight) {
  114. fb->panel->backlight->props.power = FB_BLANK_UNBLANK;
  115. backlight_update_status(fb->panel->backlight);
  116. }
  117. /*
  118. * finally, enable the interface.
  119. */
  120. if (fb->board->enable)
  121. fb->board->enable(fb);
  122. }
  123. static int
  124. clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var)
  125. {
  126. u32 caps;
  127. int ret = 0;
  128. if (fb->panel->caps && fb->board->caps)
  129. caps = fb->panel->caps & fb->board->caps;
  130. else {
  131. /* Old way of specifying what can be used */
  132. caps = fb->panel->cntl & CNTL_BGR ?
  133. CLCD_CAP_BGR : CLCD_CAP_RGB;
  134. /* But mask out 444 modes as they weren't supported */
  135. caps &= ~CLCD_CAP_444;
  136. }
  137. /* Only TFT panels can do RGB888/BGR888 */
  138. if (!(fb->panel->cntl & CNTL_LCDTFT))
  139. caps &= ~CLCD_CAP_888;
  140. memset(&var->transp, 0, sizeof(var->transp));
  141. var->red.msb_right = 0;
  142. var->green.msb_right = 0;
  143. var->blue.msb_right = 0;
  144. switch (var->bits_per_pixel) {
  145. case 1:
  146. case 2:
  147. case 4:
  148. case 8:
  149. /* If we can't do 5551, reject */
  150. caps &= CLCD_CAP_5551;
  151. if (!caps) {
  152. ret = -EINVAL;
  153. break;
  154. }
  155. var->red.length = var->bits_per_pixel;
  156. var->red.offset = 0;
  157. var->green.length = var->bits_per_pixel;
  158. var->green.offset = 0;
  159. var->blue.length = var->bits_per_pixel;
  160. var->blue.offset = 0;
  161. break;
  162. case 16:
  163. /* If we can't do 444, 5551 or 565, reject */
  164. if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) {
  165. ret = -EINVAL;
  166. break;
  167. }
  168. /*
  169. * Green length can be 4, 5 or 6 depending whether
  170. * we're operating in 444, 5551 or 565 mode.
  171. */
  172. if (var->green.length == 4 && caps & CLCD_CAP_444)
  173. caps &= CLCD_CAP_444;
  174. if (var->green.length == 5 && caps & CLCD_CAP_5551)
  175. caps &= CLCD_CAP_5551;
  176. else if (var->green.length == 6 && caps & CLCD_CAP_565)
  177. caps &= CLCD_CAP_565;
  178. else {
  179. /*
  180. * PL110 officially only supports RGB555,
  181. * but may be wired up to allow RGB565.
  182. */
  183. if (caps & CLCD_CAP_565) {
  184. var->green.length = 6;
  185. caps &= CLCD_CAP_565;
  186. } else if (caps & CLCD_CAP_5551) {
  187. var->green.length = 5;
  188. caps &= CLCD_CAP_5551;
  189. } else {
  190. var->green.length = 4;
  191. caps &= CLCD_CAP_444;
  192. }
  193. }
  194. if (var->green.length >= 5) {
  195. var->red.length = 5;
  196. var->blue.length = 5;
  197. } else {
  198. var->red.length = 4;
  199. var->blue.length = 4;
  200. }
  201. break;
  202. case 24:
  203. if (fb->vendor->packed_24_bit_pixels) {
  204. var->red.length = 8;
  205. var->green.length = 8;
  206. var->blue.length = 8;
  207. } else {
  208. ret = -EINVAL;
  209. }
  210. break;
  211. case 32:
  212. /* If we can't do 888, reject */
  213. caps &= CLCD_CAP_888;
  214. if (!caps) {
  215. ret = -EINVAL;
  216. break;
  217. }
  218. var->red.length = 8;
  219. var->green.length = 8;
  220. var->blue.length = 8;
  221. break;
  222. default:
  223. ret = -EINVAL;
  224. break;
  225. }
  226. /*
  227. * >= 16bpp displays have separate colour component bitfields
  228. * encoded in the pixel data. Calculate their position from
  229. * the bitfield length defined above.
  230. */
  231. if (ret == 0 && var->bits_per_pixel >= 16) {
  232. bool bgr, rgb;
  233. bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0;
  234. rgb = caps & CLCD_CAP_RGB && var->red.offset == 0;
  235. if (!bgr && !rgb)
  236. /*
  237. * The requested format was not possible, try just
  238. * our capabilities. One of BGR or RGB must be
  239. * supported.
  240. */
  241. bgr = caps & CLCD_CAP_BGR;
  242. if (bgr) {
  243. var->blue.offset = 0;
  244. var->green.offset = var->blue.offset + var->blue.length;
  245. var->red.offset = var->green.offset + var->green.length;
  246. } else {
  247. var->red.offset = 0;
  248. var->green.offset = var->red.offset + var->red.length;
  249. var->blue.offset = var->green.offset + var->green.length;
  250. }
  251. }
  252. return ret;
  253. }
  254. static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  255. {
  256. struct clcd_fb *fb = to_clcd(info);
  257. int ret = -EINVAL;
  258. if (fb->board->check)
  259. ret = fb->board->check(fb, var);
  260. if (ret == 0 &&
  261. var->xres_virtual * var->bits_per_pixel / 8 *
  262. var->yres_virtual > fb->fb.fix.smem_len)
  263. ret = -EINVAL;
  264. if (ret == 0)
  265. ret = clcdfb_set_bitfields(fb, var);
  266. return ret;
  267. }
  268. static int clcdfb_set_par(struct fb_info *info)
  269. {
  270. struct clcd_fb *fb = to_clcd(info);
  271. struct clcd_regs regs;
  272. fb->fb.fix.line_length = fb->fb.var.xres_virtual *
  273. fb->fb.var.bits_per_pixel / 8;
  274. if (fb->fb.var.bits_per_pixel <= 8)
  275. fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  276. else
  277. fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  278. fb->board->decode(fb, &regs);
  279. clcdfb_disable(fb);
  280. /* Some variants must be clocked here */
  281. if (fb->vendor->clock_timregs && !fb->clk_enabled) {
  282. fb->clk_enabled = true;
  283. clk_enable(fb->clk);
  284. }
  285. writel(regs.tim0, fb->regs + CLCD_TIM0);
  286. writel(regs.tim1, fb->regs + CLCD_TIM1);
  287. writel(regs.tim2, fb->regs + CLCD_TIM2);
  288. writel(regs.tim3, fb->regs + CLCD_TIM3);
  289. clcdfb_set_start(fb);
  290. clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000);
  291. fb->clcd_cntl = regs.cntl;
  292. clcdfb_enable(fb, regs.cntl);
  293. #ifdef DEBUG
  294. printk(KERN_INFO
  295. "CLCD: Registers set to\n"
  296. " %08x %08x %08x %08x\n"
  297. " %08x %08x %08x %08x\n",
  298. readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1),
  299. readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3),
  300. readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS),
  301. readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl));
  302. #endif
  303. return 0;
  304. }
  305. static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  306. {
  307. unsigned int mask = (1 << bf->length) - 1;
  308. return (val >> (16 - bf->length) & mask) << bf->offset;
  309. }
  310. /*
  311. * Set a single color register. The values supplied have a 16 bit
  312. * magnitude. Return != 0 for invalid regno.
  313. */
  314. static int
  315. clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green,
  316. unsigned int blue, unsigned int transp, struct fb_info *info)
  317. {
  318. struct clcd_fb *fb = to_clcd(info);
  319. if (regno < 16)
  320. fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  321. convert_bitfield(blue, &fb->fb.var.blue) |
  322. convert_bitfield(green, &fb->fb.var.green) |
  323. convert_bitfield(red, &fb->fb.var.red);
  324. if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) {
  325. int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3);
  326. u32 val, mask, newval;
  327. newval = (red >> 11) & 0x001f;
  328. newval |= (green >> 6) & 0x03e0;
  329. newval |= (blue >> 1) & 0x7c00;
  330. /*
  331. * 3.2.11: if we're configured for big endian
  332. * byte order, the palette entries are swapped.
  333. */
  334. if (fb->clcd_cntl & CNTL_BEBO)
  335. regno ^= 1;
  336. if (regno & 1) {
  337. newval <<= 16;
  338. mask = 0x0000ffff;
  339. } else {
  340. mask = 0xffff0000;
  341. }
  342. val = readl(fb->regs + hw_reg) & mask;
  343. writel(val | newval, fb->regs + hw_reg);
  344. }
  345. return regno > 255;
  346. }
  347. /*
  348. * Blank the screen if blank_mode != 0, else unblank. If blank == NULL
  349. * then the caller blanks by setting the CLUT (Color Look Up Table) to all
  350. * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due
  351. * to e.g. a video mode which doesn't support it. Implements VESA suspend
  352. * and powerdown modes on hardware that supports disabling hsync/vsync:
  353. * blank_mode == 2: suspend vsync
  354. * blank_mode == 3: suspend hsync
  355. * blank_mode == 4: powerdown
  356. */
  357. static int clcdfb_blank(int blank_mode, struct fb_info *info)
  358. {
  359. struct clcd_fb *fb = to_clcd(info);
  360. if (blank_mode != 0) {
  361. clcdfb_disable(fb);
  362. } else {
  363. clcdfb_enable(fb, fb->clcd_cntl);
  364. }
  365. return 0;
  366. }
  367. static int clcdfb_mmap(struct fb_info *info,
  368. struct vm_area_struct *vma)
  369. {
  370. struct clcd_fb *fb = to_clcd(info);
  371. unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT;
  372. int ret = -EINVAL;
  373. len = info->fix.smem_len;
  374. if (off <= len && vma->vm_end - vma->vm_start <= len - off &&
  375. fb->board->mmap)
  376. ret = fb->board->mmap(fb, vma);
  377. return ret;
  378. }
  379. static struct fb_ops clcdfb_ops = {
  380. .owner = THIS_MODULE,
  381. .fb_check_var = clcdfb_check_var,
  382. .fb_set_par = clcdfb_set_par,
  383. .fb_setcolreg = clcdfb_setcolreg,
  384. .fb_blank = clcdfb_blank,
  385. .fb_fillrect = cfb_fillrect,
  386. .fb_copyarea = cfb_copyarea,
  387. .fb_imageblit = cfb_imageblit,
  388. .fb_mmap = clcdfb_mmap,
  389. };
  390. static int clcdfb_register(struct clcd_fb *fb)
  391. {
  392. int ret;
  393. /*
  394. * ARM PL111 always has IENB at 0x1c; it's only PL110
  395. * which is reversed on some platforms.
  396. */
  397. if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) {
  398. fb->off_ienb = CLCD_PL111_IENB;
  399. fb->off_cntl = CLCD_PL111_CNTL;
  400. } else {
  401. if (of_machine_is_compatible("arm,versatile-ab") ||
  402. of_machine_is_compatible("arm,versatile-pb")) {
  403. fb->off_ienb = CLCD_PL111_IENB;
  404. fb->off_cntl = CLCD_PL111_CNTL;
  405. } else {
  406. fb->off_ienb = CLCD_PL110_IENB;
  407. fb->off_cntl = CLCD_PL110_CNTL;
  408. }
  409. }
  410. fb->clk = clk_get(&fb->dev->dev, NULL);
  411. if (IS_ERR(fb->clk)) {
  412. ret = PTR_ERR(fb->clk);
  413. goto out;
  414. }
  415. ret = clk_prepare(fb->clk);
  416. if (ret)
  417. goto free_clk;
  418. fb->fb.device = &fb->dev->dev;
  419. fb->fb.fix.mmio_start = fb->dev->res.start;
  420. fb->fb.fix.mmio_len = resource_size(&fb->dev->res);
  421. fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len);
  422. if (!fb->regs) {
  423. printk(KERN_ERR "CLCD: unable to remap registers\n");
  424. ret = -ENOMEM;
  425. goto clk_unprep;
  426. }
  427. fb->fb.fbops = &clcdfb_ops;
  428. fb->fb.flags = FBINFO_FLAG_DEFAULT;
  429. fb->fb.pseudo_palette = fb->cmap;
  430. strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id));
  431. fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  432. fb->fb.fix.type_aux = 0;
  433. fb->fb.fix.xpanstep = 0;
  434. fb->fb.fix.ypanstep = 0;
  435. fb->fb.fix.ywrapstep = 0;
  436. fb->fb.fix.accel = FB_ACCEL_NONE;
  437. fb->fb.var.xres = fb->panel->mode.xres;
  438. fb->fb.var.yres = fb->panel->mode.yres;
  439. fb->fb.var.xres_virtual = fb->panel->mode.xres;
  440. fb->fb.var.yres_virtual = fb->panel->mode.yres;
  441. fb->fb.var.bits_per_pixel = fb->panel->bpp;
  442. fb->fb.var.grayscale = fb->panel->grayscale;
  443. fb->fb.var.pixclock = fb->panel->mode.pixclock;
  444. fb->fb.var.left_margin = fb->panel->mode.left_margin;
  445. fb->fb.var.right_margin = fb->panel->mode.right_margin;
  446. fb->fb.var.upper_margin = fb->panel->mode.upper_margin;
  447. fb->fb.var.lower_margin = fb->panel->mode.lower_margin;
  448. fb->fb.var.hsync_len = fb->panel->mode.hsync_len;
  449. fb->fb.var.vsync_len = fb->panel->mode.vsync_len;
  450. fb->fb.var.sync = fb->panel->mode.sync;
  451. fb->fb.var.vmode = fb->panel->mode.vmode;
  452. fb->fb.var.activate = FB_ACTIVATE_NOW;
  453. fb->fb.var.nonstd = 0;
  454. fb->fb.var.height = fb->panel->height;
  455. fb->fb.var.width = fb->panel->width;
  456. fb->fb.var.accel_flags = 0;
  457. fb->fb.monspecs.hfmin = 0;
  458. fb->fb.monspecs.hfmax = 100000;
  459. fb->fb.monspecs.vfmin = 0;
  460. fb->fb.monspecs.vfmax = 400;
  461. fb->fb.monspecs.dclkmin = 1000000;
  462. fb->fb.monspecs.dclkmax = 100000000;
  463. /*
  464. * Make sure that the bitfields are set appropriately.
  465. */
  466. clcdfb_set_bitfields(fb, &fb->fb.var);
  467. /*
  468. * Allocate colourmap.
  469. */
  470. ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0);
  471. if (ret)
  472. goto unmap;
  473. /*
  474. * Ensure interrupts are disabled.
  475. */
  476. writel(0, fb->regs + fb->off_ienb);
  477. fb_set_var(&fb->fb, &fb->fb.var);
  478. dev_info(&fb->dev->dev, "%s hardware, %s display\n",
  479. fb->board->name, fb->panel->mode.name);
  480. ret = register_framebuffer(&fb->fb);
  481. if (ret == 0)
  482. goto out;
  483. printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret);
  484. fb_dealloc_cmap(&fb->fb.cmap);
  485. unmap:
  486. iounmap(fb->regs);
  487. clk_unprep:
  488. clk_unprepare(fb->clk);
  489. free_clk:
  490. clk_put(fb->clk);
  491. out:
  492. return ret;
  493. }
  494. #ifdef CONFIG_OF
  495. static int clcdfb_of_get_dpi_panel_mode(struct device_node *node,
  496. struct clcd_panel *clcd_panel)
  497. {
  498. int err;
  499. struct display_timing timing;
  500. struct videomode video;
  501. err = of_get_display_timing(node, "panel-timing", &timing);
  502. if (err)
  503. return err;
  504. videomode_from_timing(&timing, &video);
  505. err = fb_videomode_from_videomode(&video, &clcd_panel->mode);
  506. if (err)
  507. return err;
  508. /* Set up some inversion flags */
  509. if (timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  510. clcd_panel->tim2 |= TIM2_IPC;
  511. else if (!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE))
  512. /*
  513. * To preserve backwards compatibility, the IPC (inverted
  514. * pixel clock) flag needs to be set on any display that
  515. * doesn't explicitly specify that the pixel clock is
  516. * active on the negative or positive edge.
  517. */
  518. clcd_panel->tim2 |= TIM2_IPC;
  519. if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW)
  520. clcd_panel->tim2 |= TIM2_IHS;
  521. if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW)
  522. clcd_panel->tim2 |= TIM2_IVS;
  523. if (timing.flags & DISPLAY_FLAGS_DE_LOW)
  524. clcd_panel->tim2 |= TIM2_IOE;
  525. return 0;
  526. }
  527. static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode)
  528. {
  529. return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres,
  530. mode->refresh);
  531. }
  532. static int clcdfb_of_get_backlight(struct device_node *endpoint,
  533. struct clcd_panel *clcd_panel)
  534. {
  535. struct device_node *panel;
  536. struct device_node *backlight;
  537. panel = of_graph_get_remote_port_parent(endpoint);
  538. if (!panel)
  539. return -ENODEV;
  540. /* Look up the optional backlight phandle */
  541. backlight = of_parse_phandle(panel, "backlight", 0);
  542. if (backlight) {
  543. clcd_panel->backlight = of_find_backlight_by_node(backlight);
  544. of_node_put(backlight);
  545. if (!clcd_panel->backlight)
  546. return -EPROBE_DEFER;
  547. }
  548. return 0;
  549. }
  550. static int clcdfb_of_get_mode(struct device *dev, struct device_node *endpoint,
  551. struct clcd_panel *clcd_panel)
  552. {
  553. int err;
  554. struct device_node *panel;
  555. struct fb_videomode *mode;
  556. char *name;
  557. int len;
  558. panel = of_graph_get_remote_port_parent(endpoint);
  559. if (!panel)
  560. return -ENODEV;
  561. /* Only directly connected DPI panels supported for now */
  562. if (of_device_is_compatible(panel, "panel-dpi"))
  563. err = clcdfb_of_get_dpi_panel_mode(panel, clcd_panel);
  564. else
  565. err = -ENOENT;
  566. if (err)
  567. return err;
  568. mode = &clcd_panel->mode;
  569. len = clcdfb_snprintf_mode(NULL, 0, mode);
  570. name = devm_kzalloc(dev, len + 1, GFP_KERNEL);
  571. if (!name)
  572. return -ENOMEM;
  573. clcdfb_snprintf_mode(name, len + 1, mode);
  574. mode->name = name;
  575. return 0;
  576. }
  577. static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0)
  578. {
  579. static struct {
  580. unsigned int part;
  581. u32 r0, g0, b0;
  582. u32 caps;
  583. } panels[] = {
  584. { 0x110, 1, 7, 13, CLCD_CAP_5551 },
  585. { 0x110, 0, 8, 16, CLCD_CAP_888 },
  586. { 0x110, 16, 8, 0, CLCD_CAP_888 },
  587. { 0x111, 4, 14, 20, CLCD_CAP_444 },
  588. { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 },
  589. { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 |
  590. CLCD_CAP_565 },
  591. { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 |
  592. CLCD_CAP_565 | CLCD_CAP_888 },
  593. };
  594. int i;
  595. /* Bypass pixel clock divider */
  596. fb->panel->tim2 |= TIM2_BCD;
  597. /* TFT display, vert. comp. interrupt at the start of the back porch */
  598. fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1);
  599. fb->panel->caps = 0;
  600. /* Match the setup with known variants */
  601. for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) {
  602. if (amba_part(fb->dev) != panels[i].part)
  603. continue;
  604. if (g0 != panels[i].g0)
  605. continue;
  606. if (r0 == panels[i].r0 && b0 == panels[i].b0)
  607. fb->panel->caps = panels[i].caps;
  608. }
  609. /*
  610. * If we actually physically connected the R lines to B and
  611. * vice versa
  612. */
  613. if (r0 != 0 && b0 == 0)
  614. fb->panel->bgr_connection = true;
  615. if (fb->panel->caps && fb->vendor->st_bitmux_control) {
  616. /*
  617. * Set up the special bits for the Nomadik control register
  618. * (other platforms tend to do this through an external
  619. * register).
  620. */
  621. /* Offset of the highest used color */
  622. int maxoff = max3(r0, g0, b0);
  623. /* Most significant bit out, highest used bit */
  624. int msb = 0;
  625. if (fb->panel->caps & CLCD_CAP_888) {
  626. msb = maxoff + 8 - 1;
  627. } else if (fb->panel->caps & CLCD_CAP_565) {
  628. msb = maxoff + 5 - 1;
  629. fb->panel->cntl |= CNTL_ST_1XBPP_565;
  630. } else if (fb->panel->caps & CLCD_CAP_5551) {
  631. msb = maxoff + 5 - 1;
  632. fb->panel->cntl |= CNTL_ST_1XBPP_5551;
  633. } else if (fb->panel->caps & CLCD_CAP_444) {
  634. msb = maxoff + 4 - 1;
  635. fb->panel->cntl |= CNTL_ST_1XBPP_444;
  636. }
  637. /* Send out as many bits as we need */
  638. if (msb > 17)
  639. fb->panel->cntl |= CNTL_ST_CDWID_24;
  640. else if (msb > 15)
  641. fb->panel->cntl |= CNTL_ST_CDWID_18;
  642. else if (msb > 11)
  643. fb->panel->cntl |= CNTL_ST_CDWID_16;
  644. else
  645. fb->panel->cntl |= CNTL_ST_CDWID_12;
  646. }
  647. return fb->panel->caps ? 0 : -EINVAL;
  648. }
  649. static int clcdfb_of_init_display(struct clcd_fb *fb)
  650. {
  651. struct device_node *endpoint;
  652. int err;
  653. unsigned int bpp;
  654. u32 max_bandwidth;
  655. u32 tft_r0b0g0[3];
  656. fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL);
  657. if (!fb->panel)
  658. return -ENOMEM;
  659. /*
  660. * Fetch the panel endpoint.
  661. */
  662. endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node, NULL);
  663. if (!endpoint)
  664. return -ENODEV;
  665. if (fb->vendor->init_panel) {
  666. err = fb->vendor->init_panel(fb, endpoint);
  667. if (err)
  668. return err;
  669. }
  670. err = clcdfb_of_get_backlight(endpoint, fb->panel);
  671. if (err)
  672. return err;
  673. err = clcdfb_of_get_mode(&fb->dev->dev, endpoint, fb->panel);
  674. if (err)
  675. return err;
  676. err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth",
  677. &max_bandwidth);
  678. if (!err) {
  679. /*
  680. * max_bandwidth is in bytes per second and pixclock in
  681. * pico-seconds, so the maximum allowed bits per pixel is
  682. * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000)
  683. * Rearrange this calculation to avoid overflow and then ensure
  684. * result is a valid format.
  685. */
  686. bpp = max_bandwidth / (1000 / 8)
  687. / PICOS2KHZ(fb->panel->mode.pixclock);
  688. bpp = rounddown_pow_of_two(bpp);
  689. if (bpp > 32)
  690. bpp = 32;
  691. } else
  692. bpp = 32;
  693. fb->panel->bpp = bpp;
  694. #ifdef CONFIG_CPU_BIG_ENDIAN
  695. fb->panel->cntl |= CNTL_BEBO;
  696. #endif
  697. fb->panel->width = -1;
  698. fb->panel->height = -1;
  699. if (of_property_read_u32_array(endpoint,
  700. "arm,pl11x,tft-r0g0b0-pads",
  701. tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) != 0)
  702. return -ENOENT;
  703. return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0],
  704. tft_r0b0g0[1], tft_r0b0g0[2]);
  705. }
  706. static int clcdfb_of_vram_setup(struct clcd_fb *fb)
  707. {
  708. int err;
  709. struct device_node *memory;
  710. u64 size;
  711. err = clcdfb_of_init_display(fb);
  712. if (err)
  713. return err;
  714. memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0);
  715. if (!memory)
  716. return -ENODEV;
  717. fb->fb.screen_base = of_iomap(memory, 0);
  718. if (!fb->fb.screen_base)
  719. return -ENOMEM;
  720. fb->fb.fix.smem_start = of_translate_address(memory,
  721. of_get_address(memory, 0, &size, NULL));
  722. fb->fb.fix.smem_len = size;
  723. return 0;
  724. }
  725. static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  726. {
  727. unsigned long off, user_size, kernel_size;
  728. off = vma->vm_pgoff << PAGE_SHIFT;
  729. user_size = vma->vm_end - vma->vm_start;
  730. kernel_size = fb->fb.fix.smem_len;
  731. if (off >= kernel_size || user_size > (kernel_size - off))
  732. return -ENXIO;
  733. return remap_pfn_range(vma, vma->vm_start,
  734. __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff,
  735. user_size,
  736. pgprot_writecombine(vma->vm_page_prot));
  737. }
  738. static void clcdfb_of_vram_remove(struct clcd_fb *fb)
  739. {
  740. iounmap(fb->fb.screen_base);
  741. }
  742. static int clcdfb_of_dma_setup(struct clcd_fb *fb)
  743. {
  744. unsigned long framesize;
  745. dma_addr_t dma;
  746. int err;
  747. err = clcdfb_of_init_display(fb);
  748. if (err)
  749. return err;
  750. framesize = PAGE_ALIGN(fb->panel->mode.xres * fb->panel->mode.yres *
  751. fb->panel->bpp / 8);
  752. fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize,
  753. &dma, GFP_KERNEL);
  754. if (!fb->fb.screen_base)
  755. return -ENOMEM;
  756. fb->fb.fix.smem_start = dma;
  757. fb->fb.fix.smem_len = framesize;
  758. return 0;
  759. }
  760. static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma)
  761. {
  762. return dma_mmap_wc(&fb->dev->dev, vma, fb->fb.screen_base,
  763. fb->fb.fix.smem_start, fb->fb.fix.smem_len);
  764. }
  765. static void clcdfb_of_dma_remove(struct clcd_fb *fb)
  766. {
  767. dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len,
  768. fb->fb.screen_base, fb->fb.fix.smem_start);
  769. }
  770. static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
  771. {
  772. struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board),
  773. GFP_KERNEL);
  774. struct device_node *node = dev->dev.of_node;
  775. if (!board)
  776. return NULL;
  777. board->name = of_node_full_name(node);
  778. board->caps = CLCD_CAP_ALL;
  779. board->check = clcdfb_check;
  780. board->decode = clcdfb_decode;
  781. if (of_find_property(node, "memory-region", NULL)) {
  782. board->setup = clcdfb_of_vram_setup;
  783. board->mmap = clcdfb_of_vram_mmap;
  784. board->remove = clcdfb_of_vram_remove;
  785. } else {
  786. board->setup = clcdfb_of_dma_setup;
  787. board->mmap = clcdfb_of_dma_mmap;
  788. board->remove = clcdfb_of_dma_remove;
  789. }
  790. return board;
  791. }
  792. #else
  793. static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev)
  794. {
  795. return NULL;
  796. }
  797. #endif
  798. static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id)
  799. {
  800. struct clcd_board *board = dev_get_platdata(&dev->dev);
  801. struct clcd_vendor_data *vendor = id->data;
  802. struct clcd_fb *fb;
  803. int ret;
  804. if (!board)
  805. board = clcdfb_of_get_board(dev);
  806. if (!board)
  807. return -EINVAL;
  808. if (vendor->init_board) {
  809. ret = vendor->init_board(dev, board);
  810. if (ret)
  811. return ret;
  812. }
  813. ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
  814. if (ret)
  815. goto out;
  816. ret = amba_request_regions(dev, NULL);
  817. if (ret) {
  818. printk(KERN_ERR "CLCD: unable to reserve regs region\n");
  819. goto out;
  820. }
  821. fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL);
  822. if (!fb) {
  823. printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n");
  824. ret = -ENOMEM;
  825. goto free_region;
  826. }
  827. fb->dev = dev;
  828. fb->vendor = vendor;
  829. fb->board = board;
  830. dev_info(&fb->dev->dev, "PL%03x designer %02x rev%u at 0x%08llx\n",
  831. amba_part(dev), amba_manf(dev), amba_rev(dev),
  832. (unsigned long long)dev->res.start);
  833. ret = fb->board->setup(fb);
  834. if (ret)
  835. goto free_fb;
  836. ret = clcdfb_register(fb);
  837. if (ret == 0) {
  838. amba_set_drvdata(dev, fb);
  839. goto out;
  840. }
  841. fb->board->remove(fb);
  842. free_fb:
  843. kfree(fb);
  844. free_region:
  845. amba_release_regions(dev);
  846. out:
  847. return ret;
  848. }
  849. static int clcdfb_remove(struct amba_device *dev)
  850. {
  851. struct clcd_fb *fb = amba_get_drvdata(dev);
  852. clcdfb_disable(fb);
  853. unregister_framebuffer(&fb->fb);
  854. if (fb->fb.cmap.len)
  855. fb_dealloc_cmap(&fb->fb.cmap);
  856. iounmap(fb->regs);
  857. clk_unprepare(fb->clk);
  858. clk_put(fb->clk);
  859. fb->board->remove(fb);
  860. kfree(fb);
  861. amba_release_regions(dev);
  862. return 0;
  863. }
  864. static struct clcd_vendor_data vendor_arm = {
  865. /* Sets up the versatile board displays */
  866. .init_panel = versatile_clcd_init_panel,
  867. };
  868. static struct clcd_vendor_data vendor_nomadik = {
  869. .clock_timregs = true,
  870. .packed_24_bit_pixels = true,
  871. .st_bitmux_control = true,
  872. .init_board = nomadik_clcd_init_board,
  873. .init_panel = nomadik_clcd_init_panel,
  874. };
  875. static struct amba_id clcdfb_id_table[] = {
  876. {
  877. .id = 0x00041110,
  878. .mask = 0x000ffffe,
  879. .data = &vendor_arm,
  880. },
  881. /* ST Electronics Nomadik variant */
  882. {
  883. .id = 0x00180110,
  884. .mask = 0x00fffffe,
  885. .data = &vendor_nomadik,
  886. },
  887. { 0, 0 },
  888. };
  889. MODULE_DEVICE_TABLE(amba, clcdfb_id_table);
  890. static struct amba_driver clcd_driver = {
  891. .drv = {
  892. .name = "clcd-pl11x",
  893. },
  894. .probe = clcdfb_probe,
  895. .remove = clcdfb_remove,
  896. .id_table = clcdfb_id_table,
  897. };
  898. static int __init amba_clcdfb_init(void)
  899. {
  900. if (fb_get_options("ambafb", NULL))
  901. return -ENODEV;
  902. return amba_driver_register(&clcd_driver);
  903. }
  904. module_init(amba_clcdfb_init);
  905. static void __exit amba_clcdfb_exit(void)
  906. {
  907. amba_driver_unregister(&clcd_driver);
  908. }
  909. module_exit(amba_clcdfb_exit);
  910. MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver");
  911. MODULE_LICENSE("GPL");