sh_mobile_lcdcfb.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. /*
  2. * SuperH Mobile LCDC Framebuffer
  3. *
  4. * Copyright (c) 2008 Magnus Damm
  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
  8. * for more details.
  9. */
  10. #include <linux/atomic.h>
  11. #include <linux/backlight.h>
  12. #include <linux/clk.h>
  13. #include <linux/console.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioctl.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/slab.h>
  26. #include <linux/videodev2.h>
  27. #include <linux/vmalloc.h>
  28. #include <video/sh_mobile_lcdc.h>
  29. #include <video/sh_mobile_meram.h>
  30. #include "sh_mobile_lcdcfb.h"
  31. #define SIDE_B_OFFSET 0x1000
  32. #define MIRROR_OFFSET 0x2000
  33. #define MAX_XRES 1920
  34. #define MAX_YRES 1080
  35. struct sh_mobile_lcdc_priv {
  36. void __iomem *base;
  37. int irq;
  38. atomic_t hw_usecnt;
  39. struct device *dev;
  40. struct clk *dot_clk;
  41. unsigned long lddckr;
  42. struct sh_mobile_lcdc_chan ch[2];
  43. struct notifier_block notifier;
  44. int started;
  45. int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
  46. struct sh_mobile_meram_info *meram_dev;
  47. };
  48. /* -----------------------------------------------------------------------------
  49. * Registers access
  50. */
  51. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  52. [LDDCKPAT1R] = 0x400,
  53. [LDDCKPAT2R] = 0x404,
  54. [LDMT1R] = 0x418,
  55. [LDMT2R] = 0x41c,
  56. [LDMT3R] = 0x420,
  57. [LDDFR] = 0x424,
  58. [LDSM1R] = 0x428,
  59. [LDSM2R] = 0x42c,
  60. [LDSA1R] = 0x430,
  61. [LDSA2R] = 0x434,
  62. [LDMLSR] = 0x438,
  63. [LDHCNR] = 0x448,
  64. [LDHSYNR] = 0x44c,
  65. [LDVLNR] = 0x450,
  66. [LDVSYNR] = 0x454,
  67. [LDPMR] = 0x460,
  68. [LDHAJR] = 0x4a0,
  69. };
  70. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  71. [LDDCKPAT1R] = 0x408,
  72. [LDDCKPAT2R] = 0x40c,
  73. [LDMT1R] = 0x600,
  74. [LDMT2R] = 0x604,
  75. [LDMT3R] = 0x608,
  76. [LDDFR] = 0x60c,
  77. [LDSM1R] = 0x610,
  78. [LDSM2R] = 0x614,
  79. [LDSA1R] = 0x618,
  80. [LDMLSR] = 0x620,
  81. [LDHCNR] = 0x624,
  82. [LDHSYNR] = 0x628,
  83. [LDVLNR] = 0x62c,
  84. [LDVSYNR] = 0x630,
  85. [LDPMR] = 0x63c,
  86. };
  87. static bool banked(int reg_nr)
  88. {
  89. switch (reg_nr) {
  90. case LDMT1R:
  91. case LDMT2R:
  92. case LDMT3R:
  93. case LDDFR:
  94. case LDSM1R:
  95. case LDSA1R:
  96. case LDSA2R:
  97. case LDMLSR:
  98. case LDHCNR:
  99. case LDHSYNR:
  100. case LDVLNR:
  101. case LDVSYNR:
  102. return true;
  103. }
  104. return false;
  105. }
  106. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  107. {
  108. return chan->cfg.chan == LCDC_CHAN_SUBLCD;
  109. }
  110. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  111. int reg_nr, unsigned long data)
  112. {
  113. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  114. if (banked(reg_nr))
  115. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  116. SIDE_B_OFFSET);
  117. }
  118. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  119. int reg_nr, unsigned long data)
  120. {
  121. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  122. MIRROR_OFFSET);
  123. }
  124. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  125. int reg_nr)
  126. {
  127. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  128. }
  129. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  130. unsigned long reg_offs, unsigned long data)
  131. {
  132. iowrite32(data, priv->base + reg_offs);
  133. }
  134. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  135. unsigned long reg_offs)
  136. {
  137. return ioread32(priv->base + reg_offs);
  138. }
  139. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  140. unsigned long reg_offs,
  141. unsigned long mask, unsigned long until)
  142. {
  143. while ((lcdc_read(priv, reg_offs) & mask) != until)
  144. cpu_relax();
  145. }
  146. /* -----------------------------------------------------------------------------
  147. * Clock management
  148. */
  149. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  150. {
  151. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  152. if (priv->dot_clk)
  153. clk_enable(priv->dot_clk);
  154. pm_runtime_get_sync(priv->dev);
  155. if (priv->meram_dev && priv->meram_dev->pdev)
  156. pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
  157. }
  158. }
  159. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  160. {
  161. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  162. if (priv->meram_dev && priv->meram_dev->pdev)
  163. pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
  164. pm_runtime_put(priv->dev);
  165. if (priv->dot_clk)
  166. clk_disable(priv->dot_clk);
  167. }
  168. }
  169. static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
  170. int clock_source)
  171. {
  172. struct clk *clk;
  173. char *str;
  174. switch (clock_source) {
  175. case LCDC_CLK_BUS:
  176. str = "bus_clk";
  177. priv->lddckr = LDDCKR_ICKSEL_BUS;
  178. break;
  179. case LCDC_CLK_PERIPHERAL:
  180. str = "peripheral_clk";
  181. priv->lddckr = LDDCKR_ICKSEL_MIPI;
  182. break;
  183. case LCDC_CLK_EXTERNAL:
  184. str = NULL;
  185. priv->lddckr = LDDCKR_ICKSEL_HDMI;
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. if (str == NULL)
  191. return 0;
  192. clk = clk_get(priv->dev, str);
  193. if (IS_ERR(clk)) {
  194. dev_err(priv->dev, "cannot get dot clock %s\n", str);
  195. return PTR_ERR(clk);
  196. }
  197. priv->dot_clk = clk;
  198. return 0;
  199. }
  200. /* -----------------------------------------------------------------------------
  201. * Display, panel and deferred I/O
  202. */
  203. static void lcdc_sys_write_index(void *handle, unsigned long data)
  204. {
  205. struct sh_mobile_lcdc_chan *ch = handle;
  206. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
  207. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  208. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  209. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  210. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  211. }
  212. static void lcdc_sys_write_data(void *handle, unsigned long data)
  213. {
  214. struct sh_mobile_lcdc_chan *ch = handle;
  215. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
  216. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  217. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  218. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  219. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  220. }
  221. static unsigned long lcdc_sys_read_data(void *handle)
  222. {
  223. struct sh_mobile_lcdc_chan *ch = handle;
  224. lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
  225. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  226. lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
  227. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  228. udelay(1);
  229. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  230. return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
  231. }
  232. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  233. lcdc_sys_write_index,
  234. lcdc_sys_write_data,
  235. lcdc_sys_read_data,
  236. };
  237. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  238. struct list_head *pagelist)
  239. {
  240. struct sh_mobile_lcdc_chan *ch = info->par;
  241. unsigned int nr_pages_max = info->fix.smem_len >> PAGE_SHIFT;
  242. struct page *page;
  243. int nr_pages = 0;
  244. sg_init_table(ch->sglist, nr_pages_max);
  245. list_for_each_entry(page, pagelist, lru)
  246. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  247. return nr_pages;
  248. }
  249. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  250. struct list_head *pagelist)
  251. {
  252. struct sh_mobile_lcdc_chan *ch = info->par;
  253. struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
  254. /* enable clocks before accessing hardware */
  255. sh_mobile_lcdc_clk_on(ch->lcdc);
  256. /*
  257. * It's possible to get here without anything on the pagelist via
  258. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  259. * invocation. In the former case, the acceleration routines are
  260. * stepped in to when using the framebuffer console causing the
  261. * workqueue to be scheduled without any dirty pages on the list.
  262. *
  263. * Despite this, a panel update is still needed given that the
  264. * acceleration routines have their own methods for writing in
  265. * that still need to be updated.
  266. *
  267. * The fsync() and empty pagelist case could be optimized for,
  268. * but we don't bother, as any application exhibiting such
  269. * behaviour is fundamentally broken anyways.
  270. */
  271. if (!list_empty(pagelist)) {
  272. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  273. /* trigger panel update */
  274. dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  275. if (panel->start_transfer)
  276. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  277. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  278. dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
  279. DMA_TO_DEVICE);
  280. } else {
  281. if (panel->start_transfer)
  282. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  283. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  284. }
  285. }
  286. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  287. {
  288. struct fb_deferred_io *fbdefio = info->fbdefio;
  289. if (fbdefio)
  290. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  291. }
  292. static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
  293. {
  294. struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
  295. if (ch->tx_dev) {
  296. int ret;
  297. ret = ch->tx_dev->ops->display_on(ch->tx_dev);
  298. if (ret < 0)
  299. return;
  300. if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
  301. ch->info->state = FBINFO_STATE_SUSPENDED;
  302. }
  303. /* HDMI must be enabled before LCDC configuration */
  304. if (panel->display_on)
  305. panel->display_on();
  306. }
  307. static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
  308. {
  309. struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg.panel_cfg;
  310. if (panel->display_off)
  311. panel->display_off();
  312. if (ch->tx_dev)
  313. ch->tx_dev->ops->display_off(ch->tx_dev);
  314. }
  315. static bool
  316. sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
  317. const struct fb_videomode *new_mode)
  318. {
  319. dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
  320. ch->display.mode.xres, ch->display.mode.yres,
  321. new_mode->xres, new_mode->yres);
  322. /* It can be a different monitor with an equal video-mode */
  323. if (fb_mode_is_equal(&ch->display.mode, new_mode))
  324. return false;
  325. dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
  326. ch->display.mode.yres, new_mode->yres);
  327. ch->display.mode = *new_mode;
  328. return true;
  329. }
  330. static int sh_mobile_check_var(struct fb_var_screeninfo *var,
  331. struct fb_info *info);
  332. static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
  333. enum sh_mobile_lcdc_entity_event event,
  334. const struct fb_videomode *mode,
  335. const struct fb_monspecs *monspec)
  336. {
  337. struct fb_info *info = ch->info;
  338. struct fb_var_screeninfo var;
  339. int ret = 0;
  340. switch (event) {
  341. case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
  342. /* HDMI plug in */
  343. if (lock_fb_info(info)) {
  344. console_lock();
  345. ch->display.width = monspec->max_x * 10;
  346. ch->display.height = monspec->max_y * 10;
  347. if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
  348. info->state == FBINFO_STATE_RUNNING) {
  349. /* First activation with the default monitor.
  350. * Just turn on, if we run a resume here, the
  351. * logo disappears.
  352. */
  353. info->var.width = monspec->max_x * 10;
  354. info->var.height = monspec->max_y * 10;
  355. sh_mobile_lcdc_display_on(ch);
  356. } else {
  357. /* New monitor or have to wake up */
  358. fb_set_suspend(info, 0);
  359. }
  360. console_unlock();
  361. unlock_fb_info(info);
  362. }
  363. break;
  364. case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
  365. /* HDMI disconnect */
  366. if (lock_fb_info(info)) {
  367. console_lock();
  368. fb_set_suspend(info, 1);
  369. console_unlock();
  370. unlock_fb_info(info);
  371. }
  372. break;
  373. case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
  374. /* Validate a proposed new mode */
  375. fb_videomode_to_var(&var, mode);
  376. var.bits_per_pixel = info->var.bits_per_pixel;
  377. var.grayscale = info->var.grayscale;
  378. ret = sh_mobile_check_var(&var, info);
  379. break;
  380. }
  381. return ret;
  382. }
  383. /* -----------------------------------------------------------------------------
  384. * Format helpers
  385. */
  386. struct sh_mobile_lcdc_format_info {
  387. u32 fourcc;
  388. unsigned int bpp;
  389. bool yuv;
  390. u32 lddfr;
  391. };
  392. static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
  393. {
  394. .fourcc = V4L2_PIX_FMT_RGB565,
  395. .bpp = 16,
  396. .yuv = false,
  397. .lddfr = LDDFR_PKF_RGB16,
  398. }, {
  399. .fourcc = V4L2_PIX_FMT_BGR24,
  400. .bpp = 24,
  401. .yuv = false,
  402. .lddfr = LDDFR_PKF_RGB24,
  403. }, {
  404. .fourcc = V4L2_PIX_FMT_BGR32,
  405. .bpp = 32,
  406. .yuv = false,
  407. .lddfr = LDDFR_PKF_ARGB32,
  408. }, {
  409. .fourcc = V4L2_PIX_FMT_NV12,
  410. .bpp = 12,
  411. .yuv = true,
  412. .lddfr = LDDFR_CC | LDDFR_YF_420,
  413. }, {
  414. .fourcc = V4L2_PIX_FMT_NV21,
  415. .bpp = 12,
  416. .yuv = true,
  417. .lddfr = LDDFR_CC | LDDFR_YF_420,
  418. }, {
  419. .fourcc = V4L2_PIX_FMT_NV16,
  420. .bpp = 16,
  421. .yuv = true,
  422. .lddfr = LDDFR_CC | LDDFR_YF_422,
  423. }, {
  424. .fourcc = V4L2_PIX_FMT_NV61,
  425. .bpp = 16,
  426. .yuv = true,
  427. .lddfr = LDDFR_CC | LDDFR_YF_422,
  428. }, {
  429. .fourcc = V4L2_PIX_FMT_NV24,
  430. .bpp = 24,
  431. .yuv = true,
  432. .lddfr = LDDFR_CC | LDDFR_YF_444,
  433. }, {
  434. .fourcc = V4L2_PIX_FMT_NV42,
  435. .bpp = 24,
  436. .yuv = true,
  437. .lddfr = LDDFR_CC | LDDFR_YF_444,
  438. },
  439. };
  440. static const struct sh_mobile_lcdc_format_info *
  441. sh_mobile_format_info(u32 fourcc)
  442. {
  443. unsigned int i;
  444. for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
  445. if (sh_mobile_format_infos[i].fourcc == fourcc)
  446. return &sh_mobile_format_infos[i];
  447. }
  448. return NULL;
  449. }
  450. static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
  451. {
  452. if (var->grayscale > 1)
  453. return var->grayscale;
  454. switch (var->bits_per_pixel) {
  455. case 16:
  456. return V4L2_PIX_FMT_RGB565;
  457. case 24:
  458. return V4L2_PIX_FMT_BGR24;
  459. case 32:
  460. return V4L2_PIX_FMT_BGR32;
  461. default:
  462. return 0;
  463. }
  464. }
  465. static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
  466. {
  467. return var->grayscale > 1;
  468. }
  469. static bool sh_mobile_format_is_yuv(const struct fb_var_screeninfo *var)
  470. {
  471. const struct sh_mobile_lcdc_format_info *format;
  472. if (var->grayscale <= 1)
  473. return false;
  474. format = sh_mobile_format_info(var->grayscale);
  475. return format ? format->yuv : false;
  476. }
  477. /* -----------------------------------------------------------------------------
  478. * Start, stop and IRQ
  479. */
  480. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  481. {
  482. struct sh_mobile_lcdc_priv *priv = data;
  483. struct sh_mobile_lcdc_chan *ch;
  484. unsigned long ldintr;
  485. int is_sub;
  486. int k;
  487. /* Acknowledge interrupts and disable further VSYNC End IRQs. */
  488. ldintr = lcdc_read(priv, _LDINTR);
  489. lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
  490. /* figure out if this interrupt is for main or sub lcd */
  491. is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
  492. /* wake up channel and disable clocks */
  493. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  494. ch = &priv->ch[k];
  495. if (!ch->enabled)
  496. continue;
  497. /* Frame End */
  498. if (ldintr & LDINTR_FS) {
  499. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  500. ch->frame_end = 1;
  501. wake_up(&ch->frame_end_wait);
  502. sh_mobile_lcdc_clk_off(priv);
  503. }
  504. }
  505. /* VSYNC End */
  506. if (ldintr & LDINTR_VES)
  507. complete(&ch->vsync_completion);
  508. }
  509. return IRQ_HANDLED;
  510. }
  511. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  512. int start)
  513. {
  514. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  515. int k;
  516. /* start or stop the lcdc */
  517. if (start)
  518. lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
  519. else
  520. lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
  521. /* wait until power is applied/stopped on all channels */
  522. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  523. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  524. while (1) {
  525. tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
  526. & LDPMR_LPS;
  527. if (start && tmp == LDPMR_LPS)
  528. break;
  529. if (!start && tmp == 0)
  530. break;
  531. cpu_relax();
  532. }
  533. if (!start)
  534. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  535. }
  536. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  537. {
  538. const struct fb_var_screeninfo *var = &ch->info->var;
  539. const struct fb_videomode *mode = &ch->display.mode;
  540. unsigned long h_total, hsync_pos, display_h_total;
  541. u32 tmp;
  542. tmp = ch->ldmt1r_value;
  543. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
  544. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
  545. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
  546. tmp |= (ch->cfg.flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
  547. tmp |= (ch->cfg.flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
  548. tmp |= (ch->cfg.flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
  549. tmp |= (ch->cfg.flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
  550. lcdc_write_chan(ch, LDMT1R, tmp);
  551. /* setup SYS bus */
  552. lcdc_write_chan(ch, LDMT2R, ch->cfg.sys_bus_cfg.ldmt2r);
  553. lcdc_write_chan(ch, LDMT3R, ch->cfg.sys_bus_cfg.ldmt3r);
  554. /* horizontal configuration */
  555. h_total = mode->xres + mode->hsync_len + mode->left_margin
  556. + mode->right_margin;
  557. tmp = h_total / 8; /* HTCN */
  558. tmp |= (min(mode->xres, var->xres) / 8) << 16; /* HDCN */
  559. lcdc_write_chan(ch, LDHCNR, tmp);
  560. hsync_pos = mode->xres + mode->right_margin;
  561. tmp = hsync_pos / 8; /* HSYNP */
  562. tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
  563. lcdc_write_chan(ch, LDHSYNR, tmp);
  564. /* vertical configuration */
  565. tmp = mode->yres + mode->vsync_len + mode->upper_margin
  566. + mode->lower_margin; /* VTLN */
  567. tmp |= min(mode->yres, var->yres) << 16; /* VDLN */
  568. lcdc_write_chan(ch, LDVLNR, tmp);
  569. tmp = mode->yres + mode->lower_margin; /* VSYNP */
  570. tmp |= mode->vsync_len << 16; /* VSYNW */
  571. lcdc_write_chan(ch, LDVSYNR, tmp);
  572. /* Adjust horizontal synchronisation for HDMI */
  573. display_h_total = mode->xres + mode->hsync_len + mode->left_margin
  574. + mode->right_margin;
  575. tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
  576. | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
  577. lcdc_write_chan(ch, LDHAJR, tmp);
  578. }
  579. /*
  580. * __sh_mobile_lcdc_start - Configure and tart the LCDC
  581. * @priv: LCDC device
  582. *
  583. * Configure all enabled channels and start the LCDC device. All external
  584. * devices (clocks, MERAM, panels, ...) are not touched by this function.
  585. */
  586. static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  587. {
  588. struct sh_mobile_lcdc_chan *ch;
  589. unsigned long tmp;
  590. int k, m;
  591. /* Enable LCDC channels. Read data from external memory, avoid using the
  592. * BEU for now.
  593. */
  594. lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
  595. /* Stop the LCDC first and disable all interrupts. */
  596. sh_mobile_lcdc_start_stop(priv, 0);
  597. lcdc_write(priv, _LDINTR, 0);
  598. /* Configure power supply, dot clocks and start them. */
  599. tmp = priv->lddckr;
  600. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  601. ch = &priv->ch[k];
  602. if (!ch->enabled)
  603. continue;
  604. /* Power supply */
  605. lcdc_write_chan(ch, LDPMR, 0);
  606. m = ch->cfg.clock_divider;
  607. if (!m)
  608. continue;
  609. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  610. * denominator.
  611. */
  612. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  613. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  614. if (m == 1)
  615. m = LDDCKR_MOSEL;
  616. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  617. }
  618. lcdc_write(priv, _LDDCKR, tmp);
  619. lcdc_write(priv, _LDDCKSTPR, 0);
  620. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  621. /* Setup geometry, format, frame buffer memory and operation mode. */
  622. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  623. ch = &priv->ch[k];
  624. if (!ch->enabled)
  625. continue;
  626. sh_mobile_lcdc_geometry(ch);
  627. tmp = ch->format->lddfr;
  628. if (ch->format->yuv) {
  629. switch (ch->info->var.colorspace) {
  630. case V4L2_COLORSPACE_REC709:
  631. tmp |= LDDFR_CF1;
  632. break;
  633. case V4L2_COLORSPACE_JPEG:
  634. tmp |= LDDFR_CF0;
  635. break;
  636. }
  637. }
  638. lcdc_write_chan(ch, LDDFR, tmp);
  639. lcdc_write_chan(ch, LDMLSR, ch->pitch);
  640. lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
  641. if (ch->format->yuv)
  642. lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
  643. /* When using deferred I/O mode, configure the LCDC for one-shot
  644. * operation and enable the frame end interrupt. Otherwise use
  645. * continuous read mode.
  646. */
  647. if (ch->ldmt1r_value & LDMT1R_IFM &&
  648. ch->cfg.sys_bus_cfg.deferred_io_msec) {
  649. lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
  650. lcdc_write(priv, _LDINTR, LDINTR_FE);
  651. } else {
  652. lcdc_write_chan(ch, LDSM1R, 0);
  653. }
  654. }
  655. /* Word and long word swap. */
  656. switch (priv->ch[0].format->fourcc) {
  657. case V4L2_PIX_FMT_RGB565:
  658. case V4L2_PIX_FMT_NV21:
  659. case V4L2_PIX_FMT_NV61:
  660. case V4L2_PIX_FMT_NV42:
  661. tmp = LDDDSR_LS | LDDDSR_WS;
  662. break;
  663. case V4L2_PIX_FMT_BGR24:
  664. case V4L2_PIX_FMT_NV12:
  665. case V4L2_PIX_FMT_NV16:
  666. case V4L2_PIX_FMT_NV24:
  667. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  668. break;
  669. case V4L2_PIX_FMT_BGR32:
  670. default:
  671. tmp = LDDDSR_LS;
  672. break;
  673. }
  674. lcdc_write(priv, _LDDDSR, tmp);
  675. /* Enable the display output. */
  676. lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
  677. sh_mobile_lcdc_start_stop(priv, 1);
  678. priv->started = 1;
  679. }
  680. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  681. {
  682. struct sh_mobile_meram_info *mdev = priv->meram_dev;
  683. struct sh_mobile_lcdc_chan *ch;
  684. unsigned long tmp;
  685. int ret;
  686. int k;
  687. /* enable clocks before accessing the hardware */
  688. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  689. if (priv->ch[k].enabled)
  690. sh_mobile_lcdc_clk_on(priv);
  691. }
  692. /* reset */
  693. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
  694. lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
  695. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  696. struct sh_mobile_lcdc_panel_cfg *panel;
  697. ch = &priv->ch[k];
  698. if (!ch->enabled)
  699. continue;
  700. panel = &ch->cfg.panel_cfg;
  701. if (panel->setup_sys) {
  702. ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
  703. if (ret)
  704. return ret;
  705. }
  706. }
  707. /* Compute frame buffer base address and pitch for each channel. */
  708. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  709. struct sh_mobile_meram_cfg *cfg;
  710. int pixelformat;
  711. ch = &priv->ch[k];
  712. if (!ch->enabled)
  713. continue;
  714. ch->base_addr_y = ch->info->fix.smem_start;
  715. ch->base_addr_c = ch->base_addr_y
  716. + ch->info->var.xres
  717. * ch->info->var.yres_virtual;
  718. ch->pitch = ch->info->fix.line_length;
  719. /* Enable MERAM if possible. */
  720. cfg = ch->cfg.meram_cfg;
  721. if (mdev == NULL || mdev->ops == NULL || cfg == NULL)
  722. continue;
  723. /* we need to de-init configured ICBs before we can
  724. * re-initialize them.
  725. */
  726. if (ch->meram_enabled) {
  727. mdev->ops->meram_unregister(mdev, cfg);
  728. ch->meram_enabled = 0;
  729. }
  730. switch (ch->format->fourcc) {
  731. case V4L2_PIX_FMT_NV12:
  732. case V4L2_PIX_FMT_NV21:
  733. case V4L2_PIX_FMT_NV16:
  734. case V4L2_PIX_FMT_NV61:
  735. pixelformat = SH_MOBILE_MERAM_PF_NV;
  736. break;
  737. case V4L2_PIX_FMT_NV24:
  738. case V4L2_PIX_FMT_NV42:
  739. pixelformat = SH_MOBILE_MERAM_PF_NV24;
  740. break;
  741. case V4L2_PIX_FMT_RGB565:
  742. case V4L2_PIX_FMT_BGR24:
  743. case V4L2_PIX_FMT_BGR32:
  744. default:
  745. pixelformat = SH_MOBILE_MERAM_PF_RGB;
  746. break;
  747. }
  748. ret = mdev->ops->meram_register(mdev, cfg, ch->pitch,
  749. ch->info->var.yres, pixelformat,
  750. ch->base_addr_y, ch->base_addr_c,
  751. &ch->base_addr_y, &ch->base_addr_c,
  752. &ch->pitch);
  753. if (!ret)
  754. ch->meram_enabled = 1;
  755. }
  756. /* Start the LCDC. */
  757. __sh_mobile_lcdc_start(priv);
  758. /* Setup deferred I/O, tell the board code to enable the panels, and
  759. * turn backlight on.
  760. */
  761. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  762. ch = &priv->ch[k];
  763. if (!ch->enabled)
  764. continue;
  765. tmp = ch->cfg.sys_bus_cfg.deferred_io_msec;
  766. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  767. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  768. ch->defio.delay = msecs_to_jiffies(tmp);
  769. ch->info->fbdefio = &ch->defio;
  770. fb_deferred_io_init(ch->info);
  771. }
  772. sh_mobile_lcdc_display_on(ch);
  773. if (ch->bl) {
  774. ch->bl->props.power = FB_BLANK_UNBLANK;
  775. backlight_update_status(ch->bl);
  776. }
  777. }
  778. return 0;
  779. }
  780. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  781. {
  782. struct sh_mobile_lcdc_chan *ch;
  783. int k;
  784. /* clean up deferred io and ask board code to disable panel */
  785. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  786. ch = &priv->ch[k];
  787. if (!ch->enabled)
  788. continue;
  789. /* deferred io mode:
  790. * flush frame, and wait for frame end interrupt
  791. * clean up deferred io and enable clock
  792. */
  793. if (ch->info && ch->info->fbdefio) {
  794. ch->frame_end = 0;
  795. schedule_delayed_work(&ch->info->deferred_work, 0);
  796. wait_event(ch->frame_end_wait, ch->frame_end);
  797. fb_deferred_io_cleanup(ch->info);
  798. ch->info->fbdefio = NULL;
  799. sh_mobile_lcdc_clk_on(priv);
  800. }
  801. if (ch->bl) {
  802. ch->bl->props.power = FB_BLANK_POWERDOWN;
  803. backlight_update_status(ch->bl);
  804. }
  805. sh_mobile_lcdc_display_off(ch);
  806. /* disable the meram */
  807. if (ch->meram_enabled) {
  808. struct sh_mobile_meram_cfg *cfg;
  809. struct sh_mobile_meram_info *mdev;
  810. cfg = ch->cfg.meram_cfg;
  811. mdev = priv->meram_dev;
  812. mdev->ops->meram_unregister(mdev, cfg);
  813. ch->meram_enabled = 0;
  814. }
  815. }
  816. /* stop the lcdc */
  817. if (priv->started) {
  818. sh_mobile_lcdc_start_stop(priv, 0);
  819. priv->started = 0;
  820. }
  821. /* stop clocks */
  822. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  823. if (priv->ch[k].enabled)
  824. sh_mobile_lcdc_clk_off(priv);
  825. }
  826. /* -----------------------------------------------------------------------------
  827. * Frame buffer operations
  828. */
  829. static int sh_mobile_lcdc_setcolreg(u_int regno,
  830. u_int red, u_int green, u_int blue,
  831. u_int transp, struct fb_info *info)
  832. {
  833. u32 *palette = info->pseudo_palette;
  834. if (regno >= PALETTE_NR)
  835. return -EINVAL;
  836. /* only FB_VISUAL_TRUECOLOR supported */
  837. red >>= 16 - info->var.red.length;
  838. green >>= 16 - info->var.green.length;
  839. blue >>= 16 - info->var.blue.length;
  840. transp >>= 16 - info->var.transp.length;
  841. palette[regno] = (red << info->var.red.offset) |
  842. (green << info->var.green.offset) |
  843. (blue << info->var.blue.offset) |
  844. (transp << info->var.transp.offset);
  845. return 0;
  846. }
  847. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  848. .id = "SH Mobile LCDC",
  849. .type = FB_TYPE_PACKED_PIXELS,
  850. .visual = FB_VISUAL_TRUECOLOR,
  851. .accel = FB_ACCEL_NONE,
  852. .xpanstep = 0,
  853. .ypanstep = 1,
  854. .ywrapstep = 0,
  855. .capabilities = FB_CAP_FOURCC,
  856. };
  857. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  858. const struct fb_fillrect *rect)
  859. {
  860. sys_fillrect(info, rect);
  861. sh_mobile_lcdc_deferred_io_touch(info);
  862. }
  863. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  864. const struct fb_copyarea *area)
  865. {
  866. sys_copyarea(info, area);
  867. sh_mobile_lcdc_deferred_io_touch(info);
  868. }
  869. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  870. const struct fb_image *image)
  871. {
  872. sys_imageblit(info, image);
  873. sh_mobile_lcdc_deferred_io_touch(info);
  874. }
  875. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  876. struct fb_info *info)
  877. {
  878. struct sh_mobile_lcdc_chan *ch = info->par;
  879. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  880. unsigned long ldrcntr;
  881. unsigned long new_pan_offset;
  882. unsigned long base_addr_y, base_addr_c;
  883. unsigned long c_offset;
  884. bool yuv = sh_mobile_format_is_yuv(&info->var);
  885. if (!yuv)
  886. new_pan_offset = var->yoffset * info->fix.line_length
  887. + var->xoffset * (info->var.bits_per_pixel / 8);
  888. else
  889. new_pan_offset = var->yoffset * info->fix.line_length
  890. + var->xoffset;
  891. if (new_pan_offset == ch->pan_offset)
  892. return 0; /* No change, do nothing */
  893. ldrcntr = lcdc_read(priv, _LDRCNTR);
  894. /* Set the source address for the next refresh */
  895. base_addr_y = ch->dma_handle + new_pan_offset;
  896. if (yuv) {
  897. /* Set y offset */
  898. c_offset = var->yoffset * info->fix.line_length
  899. * (info->var.bits_per_pixel - 8) / 8;
  900. base_addr_c = ch->dma_handle
  901. + info->var.xres * info->var.yres_virtual
  902. + c_offset;
  903. /* Set x offset */
  904. if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
  905. base_addr_c += 2 * var->xoffset;
  906. else
  907. base_addr_c += var->xoffset;
  908. }
  909. if (ch->meram_enabled) {
  910. struct sh_mobile_meram_cfg *cfg;
  911. struct sh_mobile_meram_info *mdev;
  912. int ret;
  913. cfg = ch->cfg.meram_cfg;
  914. mdev = priv->meram_dev;
  915. ret = mdev->ops->meram_update(mdev, cfg,
  916. base_addr_y, base_addr_c,
  917. &base_addr_y, &base_addr_c);
  918. if (ret)
  919. return ret;
  920. }
  921. ch->base_addr_y = base_addr_y;
  922. ch->base_addr_c = base_addr_c;
  923. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  924. if (yuv)
  925. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  926. if (lcdc_chan_is_sublcd(ch))
  927. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  928. else
  929. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  930. ch->pan_offset = new_pan_offset;
  931. sh_mobile_lcdc_deferred_io_touch(info);
  932. return 0;
  933. }
  934. static int sh_mobile_wait_for_vsync(struct fb_info *info)
  935. {
  936. struct sh_mobile_lcdc_chan *ch = info->par;
  937. unsigned long ldintr;
  938. int ret;
  939. /* Enable VSync End interrupt and be careful not to acknowledge any
  940. * pending interrupt.
  941. */
  942. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  943. ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
  944. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  945. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  946. msecs_to_jiffies(100));
  947. if (!ret)
  948. return -ETIMEDOUT;
  949. return 0;
  950. }
  951. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  952. unsigned long arg)
  953. {
  954. int retval;
  955. switch (cmd) {
  956. case FBIO_WAITFORVSYNC:
  957. retval = sh_mobile_wait_for_vsync(info);
  958. break;
  959. default:
  960. retval = -ENOIOCTLCMD;
  961. break;
  962. }
  963. return retval;
  964. }
  965. static void sh_mobile_fb_reconfig(struct fb_info *info)
  966. {
  967. struct sh_mobile_lcdc_chan *ch = info->par;
  968. struct fb_var_screeninfo var;
  969. struct fb_videomode mode;
  970. struct fb_event event;
  971. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  972. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  973. /* More framebuffer users are active */
  974. return;
  975. fb_var_to_videomode(&mode, &info->var);
  976. if (fb_mode_is_equal(&ch->display.mode, &mode))
  977. return;
  978. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  979. var = info->var;
  980. fb_videomode_to_var(&var, &ch->display.mode);
  981. var.width = ch->display.width;
  982. var.height = ch->display.height;
  983. var.activate = FB_ACTIVATE_NOW;
  984. if (fb_set_var(info, &var) < 0)
  985. /* Couldn't reconfigure, hopefully, can continue as before */
  986. return;
  987. /*
  988. * fb_set_var() calls the notifier change internally, only if
  989. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  990. * user event, we have to call the chain ourselves.
  991. */
  992. event.info = info;
  993. event.data = &ch->display.mode;
  994. fb_notifier_call_chain(evnt, &event);
  995. }
  996. /*
  997. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  998. * user == 1, or with console sem held, if user == 0.
  999. */
  1000. static int sh_mobile_release(struct fb_info *info, int user)
  1001. {
  1002. struct sh_mobile_lcdc_chan *ch = info->par;
  1003. mutex_lock(&ch->open_lock);
  1004. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1005. ch->use_count--;
  1006. /* Nothing to reconfigure, when called from fbcon */
  1007. if (user) {
  1008. console_lock();
  1009. sh_mobile_fb_reconfig(info);
  1010. console_unlock();
  1011. }
  1012. mutex_unlock(&ch->open_lock);
  1013. return 0;
  1014. }
  1015. static int sh_mobile_open(struct fb_info *info, int user)
  1016. {
  1017. struct sh_mobile_lcdc_chan *ch = info->par;
  1018. mutex_lock(&ch->open_lock);
  1019. ch->use_count++;
  1020. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1021. mutex_unlock(&ch->open_lock);
  1022. return 0;
  1023. }
  1024. static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  1025. {
  1026. struct sh_mobile_lcdc_chan *ch = info->par;
  1027. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1028. unsigned int best_dist = (unsigned int)-1;
  1029. unsigned int best_xres = 0;
  1030. unsigned int best_yres = 0;
  1031. unsigned int i;
  1032. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  1033. return -EINVAL;
  1034. /* If board code provides us with a list of available modes, make sure
  1035. * we use one of them. Find the mode closest to the requested one. The
  1036. * distance between two modes is defined as the size of the
  1037. * non-overlapping parts of the two rectangles.
  1038. */
  1039. for (i = 0; i < ch->cfg.num_modes; ++i) {
  1040. const struct fb_videomode *mode = &ch->cfg.lcd_modes[i];
  1041. unsigned int dist;
  1042. /* We can only round up. */
  1043. if (var->xres > mode->xres || var->yres > mode->yres)
  1044. continue;
  1045. dist = var->xres * var->yres + mode->xres * mode->yres
  1046. - 2 * min(var->xres, mode->xres)
  1047. * min(var->yres, mode->yres);
  1048. if (dist < best_dist) {
  1049. best_xres = mode->xres;
  1050. best_yres = mode->yres;
  1051. best_dist = dist;
  1052. }
  1053. }
  1054. /* If no available mode can be used, return an error. */
  1055. if (ch->cfg.num_modes != 0) {
  1056. if (best_dist == (unsigned int)-1)
  1057. return -EINVAL;
  1058. var->xres = best_xres;
  1059. var->yres = best_yres;
  1060. }
  1061. /* Make sure the virtual resolution is at least as big as the visible
  1062. * resolution.
  1063. */
  1064. if (var->xres_virtual < var->xres)
  1065. var->xres_virtual = var->xres;
  1066. if (var->yres_virtual < var->yres)
  1067. var->yres_virtual = var->yres;
  1068. if (sh_mobile_format_is_fourcc(var)) {
  1069. const struct sh_mobile_lcdc_format_info *format;
  1070. format = sh_mobile_format_info(var->grayscale);
  1071. if (format == NULL)
  1072. return -EINVAL;
  1073. var->bits_per_pixel = format->bpp;
  1074. /* Default to RGB and JPEG color-spaces for RGB and YUV formats
  1075. * respectively.
  1076. */
  1077. if (!format->yuv)
  1078. var->colorspace = V4L2_COLORSPACE_SRGB;
  1079. else if (var->colorspace != V4L2_COLORSPACE_REC709)
  1080. var->colorspace = V4L2_COLORSPACE_JPEG;
  1081. } else {
  1082. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  1083. var->bits_per_pixel = 16;
  1084. var->red.offset = 11;
  1085. var->red.length = 5;
  1086. var->green.offset = 5;
  1087. var->green.length = 6;
  1088. var->blue.offset = 0;
  1089. var->blue.length = 5;
  1090. var->transp.offset = 0;
  1091. var->transp.length = 0;
  1092. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  1093. var->bits_per_pixel = 24;
  1094. var->red.offset = 16;
  1095. var->red.length = 8;
  1096. var->green.offset = 8;
  1097. var->green.length = 8;
  1098. var->blue.offset = 0;
  1099. var->blue.length = 8;
  1100. var->transp.offset = 0;
  1101. var->transp.length = 0;
  1102. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  1103. var->bits_per_pixel = 32;
  1104. var->red.offset = 16;
  1105. var->red.length = 8;
  1106. var->green.offset = 8;
  1107. var->green.length = 8;
  1108. var->blue.offset = 0;
  1109. var->blue.length = 8;
  1110. var->transp.offset = 24;
  1111. var->transp.length = 8;
  1112. } else
  1113. return -EINVAL;
  1114. var->red.msb_right = 0;
  1115. var->green.msb_right = 0;
  1116. var->blue.msb_right = 0;
  1117. var->transp.msb_right = 0;
  1118. }
  1119. /* Make sure we don't exceed our allocated memory. */
  1120. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  1121. info->fix.smem_len)
  1122. return -EINVAL;
  1123. /* only accept the forced_fourcc for dual channel configurations */
  1124. if (p->forced_fourcc &&
  1125. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1126. return -EINVAL;
  1127. return 0;
  1128. }
  1129. static int sh_mobile_set_par(struct fb_info *info)
  1130. {
  1131. struct sh_mobile_lcdc_chan *ch = info->par;
  1132. u32 line_length = info->fix.line_length;
  1133. int ret;
  1134. sh_mobile_lcdc_stop(ch->lcdc);
  1135. if (sh_mobile_format_is_yuv(&info->var))
  1136. info->fix.line_length = info->var.xres;
  1137. else
  1138. info->fix.line_length = info->var.xres
  1139. * info->var.bits_per_pixel / 8;
  1140. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1141. ret = sh_mobile_lcdc_start(ch->lcdc);
  1142. if (ret < 0) {
  1143. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1144. info->fix.line_length = line_length;
  1145. }
  1146. if (sh_mobile_format_is_fourcc(&info->var)) {
  1147. info->fix.type = FB_TYPE_FOURCC;
  1148. info->fix.visual = FB_VISUAL_FOURCC;
  1149. } else {
  1150. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1151. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1152. }
  1153. return ret;
  1154. }
  1155. /*
  1156. * Screen blanking. Behavior is as follows:
  1157. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1158. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1159. * FB_BLANK_VSYNC,
  1160. * FB_BLANK_HSYNC,
  1161. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1162. */
  1163. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1164. {
  1165. struct sh_mobile_lcdc_chan *ch = info->par;
  1166. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1167. /* blank the screen? */
  1168. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1169. struct fb_fillrect rect = {
  1170. .width = info->var.xres,
  1171. .height = info->var.yres,
  1172. };
  1173. sh_mobile_lcdc_fillrect(info, &rect);
  1174. }
  1175. /* turn clocks on? */
  1176. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1177. sh_mobile_lcdc_clk_on(p);
  1178. }
  1179. /* turn clocks off? */
  1180. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1181. /* make sure the screen is updated with the black fill before
  1182. * switching the clocks off. one vsync is not enough since
  1183. * blanking may occur in the middle of a refresh. deferred io
  1184. * mode will reenable the clocks and update the screen in time,
  1185. * so it does not need this. */
  1186. if (!info->fbdefio) {
  1187. sh_mobile_wait_for_vsync(info);
  1188. sh_mobile_wait_for_vsync(info);
  1189. }
  1190. sh_mobile_lcdc_clk_off(p);
  1191. }
  1192. ch->blank_status = blank;
  1193. return 0;
  1194. }
  1195. static struct fb_ops sh_mobile_lcdc_ops = {
  1196. .owner = THIS_MODULE,
  1197. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1198. .fb_read = fb_sys_read,
  1199. .fb_write = fb_sys_write,
  1200. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1201. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1202. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1203. .fb_blank = sh_mobile_lcdc_blank,
  1204. .fb_pan_display = sh_mobile_fb_pan_display,
  1205. .fb_ioctl = sh_mobile_ioctl,
  1206. .fb_open = sh_mobile_open,
  1207. .fb_release = sh_mobile_release,
  1208. .fb_check_var = sh_mobile_check_var,
  1209. .fb_set_par = sh_mobile_set_par,
  1210. };
  1211. static void
  1212. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1213. {
  1214. if (ch->info && ch->info->dev)
  1215. unregister_framebuffer(ch->info);
  1216. }
  1217. static int __devinit
  1218. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1219. {
  1220. struct fb_info *info = ch->info;
  1221. int ret;
  1222. if (info->fbdefio) {
  1223. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1224. ch->fb_size >> PAGE_SHIFT);
  1225. if (!ch->sglist) {
  1226. dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
  1227. return -ENOMEM;
  1228. }
  1229. }
  1230. info->bl_dev = ch->bl;
  1231. ret = register_framebuffer(info);
  1232. if (ret < 0)
  1233. return ret;
  1234. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1235. dev_name(ch->lcdc->dev), (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  1236. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1237. info->var.bits_per_pixel);
  1238. /* deferred io mode: disable clock to save power */
  1239. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1240. sh_mobile_lcdc_clk_off(ch->lcdc);
  1241. return ret;
  1242. }
  1243. static void
  1244. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1245. {
  1246. struct fb_info *info = ch->info;
  1247. if (!info || !info->device)
  1248. return;
  1249. if (ch->sglist)
  1250. vfree(ch->sglist);
  1251. fb_dealloc_cmap(&info->cmap);
  1252. framebuffer_release(info);
  1253. }
  1254. static int __devinit
  1255. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1256. const struct fb_videomode *mode,
  1257. unsigned int num_modes)
  1258. {
  1259. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1260. struct fb_var_screeninfo *var;
  1261. struct fb_info *info;
  1262. int ret;
  1263. /* Allocate and initialize the frame buffer device. Create the modes
  1264. * list and allocate the color map.
  1265. */
  1266. info = framebuffer_alloc(0, priv->dev);
  1267. if (info == NULL) {
  1268. dev_err(priv->dev, "unable to allocate fb_info\n");
  1269. return -ENOMEM;
  1270. }
  1271. ch->info = info;
  1272. info->flags = FBINFO_FLAG_DEFAULT;
  1273. info->fbops = &sh_mobile_lcdc_ops;
  1274. info->device = priv->dev;
  1275. info->screen_base = ch->fb_mem;
  1276. info->pseudo_palette = &ch->pseudo_palette;
  1277. info->par = ch;
  1278. fb_videomode_to_modelist(mode, num_modes, &info->modelist);
  1279. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1280. if (ret < 0) {
  1281. dev_err(priv->dev, "unable to allocate cmap\n");
  1282. return ret;
  1283. }
  1284. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1285. * for NV12 and NV21.
  1286. */
  1287. info->fix = sh_mobile_lcdc_fix;
  1288. info->fix.smem_start = ch->dma_handle;
  1289. info->fix.smem_len = ch->fb_size;
  1290. if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
  1291. ch->format->fourcc == V4L2_PIX_FMT_NV21)
  1292. info->fix.ypanstep = 2;
  1293. /* Initialize variable screen information using the first mode as
  1294. * default. The default Y virtual resolution is twice the panel size to
  1295. * allow for double-buffering.
  1296. */
  1297. var = &info->var;
  1298. fb_videomode_to_var(var, mode);
  1299. var->width = ch->cfg.panel_cfg.width;
  1300. var->height = ch->cfg.panel_cfg.height;
  1301. var->yres_virtual = var->yres * 2;
  1302. var->activate = FB_ACTIVATE_NOW;
  1303. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1304. * for YUV formats.
  1305. */
  1306. if (!ch->format->yuv)
  1307. var->bits_per_pixel = ch->format->bpp;
  1308. else
  1309. var->grayscale = ch->format->fourcc;
  1310. ret = sh_mobile_check_var(var, info);
  1311. if (ret)
  1312. return ret;
  1313. if (ch->format->yuv) {
  1314. info->fix.line_length = var->xres;
  1315. info->fix.visual = FB_VISUAL_FOURCC;
  1316. } else {
  1317. info->fix.line_length = var->xres * ch->format->bpp / 8;
  1318. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1319. }
  1320. return 0;
  1321. }
  1322. /* -----------------------------------------------------------------------------
  1323. * Backlight
  1324. */
  1325. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1326. {
  1327. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1328. int brightness = bdev->props.brightness;
  1329. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1330. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1331. brightness = 0;
  1332. return ch->cfg.bl_info.set_brightness(brightness);
  1333. }
  1334. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1335. {
  1336. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1337. return ch->cfg.bl_info.get_brightness();
  1338. }
  1339. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1340. struct fb_info *info)
  1341. {
  1342. return (info->bl_dev == bdev);
  1343. }
  1344. static struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1345. .options = BL_CORE_SUSPENDRESUME,
  1346. .update_status = sh_mobile_lcdc_update_bl,
  1347. .get_brightness = sh_mobile_lcdc_get_brightness,
  1348. .check_fb = sh_mobile_lcdc_check_fb,
  1349. };
  1350. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1351. struct sh_mobile_lcdc_chan *ch)
  1352. {
  1353. struct backlight_device *bl;
  1354. bl = backlight_device_register(ch->cfg.bl_info.name, parent, ch,
  1355. &sh_mobile_lcdc_bl_ops, NULL);
  1356. if (IS_ERR(bl)) {
  1357. dev_err(parent, "unable to register backlight device: %ld\n",
  1358. PTR_ERR(bl));
  1359. return NULL;
  1360. }
  1361. bl->props.max_brightness = ch->cfg.bl_info.max_brightness;
  1362. bl->props.brightness = bl->props.max_brightness;
  1363. backlight_update_status(bl);
  1364. return bl;
  1365. }
  1366. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1367. {
  1368. backlight_device_unregister(bdev);
  1369. }
  1370. /* -----------------------------------------------------------------------------
  1371. * Power management
  1372. */
  1373. static int sh_mobile_lcdc_suspend(struct device *dev)
  1374. {
  1375. struct platform_device *pdev = to_platform_device(dev);
  1376. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1377. return 0;
  1378. }
  1379. static int sh_mobile_lcdc_resume(struct device *dev)
  1380. {
  1381. struct platform_device *pdev = to_platform_device(dev);
  1382. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1383. }
  1384. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1385. {
  1386. struct platform_device *pdev = to_platform_device(dev);
  1387. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1388. /* turn off LCDC hardware */
  1389. lcdc_write(priv, _LDCNT1R, 0);
  1390. return 0;
  1391. }
  1392. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1393. {
  1394. struct platform_device *pdev = to_platform_device(dev);
  1395. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1396. __sh_mobile_lcdc_start(priv);
  1397. return 0;
  1398. }
  1399. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1400. .suspend = sh_mobile_lcdc_suspend,
  1401. .resume = sh_mobile_lcdc_resume,
  1402. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1403. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1404. };
  1405. /* -----------------------------------------------------------------------------
  1406. * Framebuffer notifier
  1407. */
  1408. /* locking: called with info->lock held */
  1409. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1410. unsigned long action, void *data)
  1411. {
  1412. struct fb_event *event = data;
  1413. struct fb_info *info = event->info;
  1414. struct sh_mobile_lcdc_chan *ch = info->par;
  1415. if (&ch->lcdc->notifier != nb)
  1416. return NOTIFY_DONE;
  1417. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1418. __func__, action, event->data);
  1419. switch(action) {
  1420. case FB_EVENT_SUSPEND:
  1421. sh_mobile_lcdc_display_off(ch);
  1422. sh_mobile_lcdc_stop(ch->lcdc);
  1423. break;
  1424. case FB_EVENT_RESUME:
  1425. mutex_lock(&ch->open_lock);
  1426. sh_mobile_fb_reconfig(info);
  1427. mutex_unlock(&ch->open_lock);
  1428. sh_mobile_lcdc_display_on(ch);
  1429. sh_mobile_lcdc_start(ch->lcdc);
  1430. }
  1431. return NOTIFY_OK;
  1432. }
  1433. /* -----------------------------------------------------------------------------
  1434. * Probe/remove and driver init/exit
  1435. */
  1436. static const struct fb_videomode default_720p __devinitconst = {
  1437. .name = "HDMI 720p",
  1438. .xres = 1280,
  1439. .yres = 720,
  1440. .left_margin = 220,
  1441. .right_margin = 110,
  1442. .hsync_len = 40,
  1443. .upper_margin = 20,
  1444. .lower_margin = 5,
  1445. .vsync_len = 5,
  1446. .pixclock = 13468,
  1447. .refresh = 60,
  1448. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  1449. };
  1450. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1451. {
  1452. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1453. int i;
  1454. fb_unregister_client(&priv->notifier);
  1455. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1456. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  1457. sh_mobile_lcdc_stop(priv);
  1458. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1459. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  1460. if (ch->tx_dev) {
  1461. ch->tx_dev->lcdc = NULL;
  1462. module_put(ch->cfg.tx_dev->dev.driver->owner);
  1463. }
  1464. sh_mobile_lcdc_channel_fb_cleanup(ch);
  1465. if (ch->fb_mem)
  1466. dma_free_coherent(&pdev->dev, ch->fb_size,
  1467. ch->fb_mem, ch->dma_handle);
  1468. }
  1469. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1470. if (priv->ch[i].bl)
  1471. sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
  1472. }
  1473. if (priv->dot_clk) {
  1474. pm_runtime_disable(&pdev->dev);
  1475. clk_put(priv->dot_clk);
  1476. }
  1477. if (priv->base)
  1478. iounmap(priv->base);
  1479. if (priv->irq)
  1480. free_irq(priv->irq, priv);
  1481. kfree(priv);
  1482. return 0;
  1483. }
  1484. static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  1485. {
  1486. int interface_type = ch->cfg.interface_type;
  1487. switch (interface_type) {
  1488. case RGB8:
  1489. case RGB9:
  1490. case RGB12A:
  1491. case RGB12B:
  1492. case RGB16:
  1493. case RGB18:
  1494. case RGB24:
  1495. case SYS8A:
  1496. case SYS8B:
  1497. case SYS8C:
  1498. case SYS8D:
  1499. case SYS9:
  1500. case SYS12:
  1501. case SYS16A:
  1502. case SYS16B:
  1503. case SYS16C:
  1504. case SYS18:
  1505. case SYS24:
  1506. break;
  1507. default:
  1508. return -EINVAL;
  1509. }
  1510. /* SUBLCD only supports SYS interface */
  1511. if (lcdc_chan_is_sublcd(ch)) {
  1512. if (!(interface_type & LDMT1R_IFM))
  1513. return -EINVAL;
  1514. interface_type &= ~LDMT1R_IFM;
  1515. }
  1516. ch->ldmt1r_value = interface_type;
  1517. return 0;
  1518. }
  1519. static int __devinit
  1520. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
  1521. struct sh_mobile_lcdc_chan *ch)
  1522. {
  1523. const struct sh_mobile_lcdc_format_info *format;
  1524. struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
  1525. const struct fb_videomode *max_mode;
  1526. const struct fb_videomode *mode;
  1527. unsigned int num_modes;
  1528. unsigned int max_size;
  1529. unsigned int i;
  1530. mutex_init(&ch->open_lock);
  1531. ch->notify = sh_mobile_lcdc_display_notify;
  1532. /* Validate the format. */
  1533. format = sh_mobile_format_info(cfg->fourcc);
  1534. if (format == NULL) {
  1535. dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  1536. return -EINVAL;
  1537. }
  1538. ch->format = format;
  1539. /* Iterate through the modes to validate them and find the highest
  1540. * resolution.
  1541. */
  1542. max_mode = NULL;
  1543. max_size = 0;
  1544. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  1545. unsigned int size = mode->yres * mode->xres;
  1546. /* NV12/NV21 buffers must have even number of lines */
  1547. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  1548. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  1549. dev_err(priv->dev, "yres must be multiple of 2 for "
  1550. "YCbCr420 mode.\n");
  1551. return -EINVAL;
  1552. }
  1553. if (size > max_size) {
  1554. max_mode = mode;
  1555. max_size = size;
  1556. }
  1557. }
  1558. if (!max_size)
  1559. max_size = MAX_XRES * MAX_YRES;
  1560. else
  1561. dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
  1562. max_mode->xres, max_mode->yres);
  1563. if (cfg->lcd_modes == NULL) {
  1564. mode = &default_720p;
  1565. num_modes = 1;
  1566. } else {
  1567. mode = cfg->lcd_modes;
  1568. num_modes = cfg->num_modes;
  1569. }
  1570. ch->display.width = cfg->panel_cfg.width;
  1571. ch->display.height = cfg->panel_cfg.height;
  1572. ch->display.mode = *mode;
  1573. /* Allocate frame buffer memory. */
  1574. ch->fb_size = max_size * format->bpp / 8 * 2;
  1575. ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
  1576. GFP_KERNEL);
  1577. if (ch->fb_mem == NULL) {
  1578. dev_err(priv->dev, "unable to allocate buffer\n");
  1579. return -ENOMEM;
  1580. }
  1581. /* Initialize the transmitter device if present. */
  1582. if (cfg->tx_dev) {
  1583. if (!cfg->tx_dev->dev.driver ||
  1584. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  1585. dev_warn(priv->dev,
  1586. "unable to get transmitter device\n");
  1587. return -EINVAL;
  1588. }
  1589. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  1590. ch->tx_dev->lcdc = ch;
  1591. ch->tx_dev->def_mode = *mode;
  1592. }
  1593. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  1594. }
  1595. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  1596. {
  1597. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  1598. struct sh_mobile_lcdc_priv *priv;
  1599. struct resource *res;
  1600. int num_channels;
  1601. int error;
  1602. int i;
  1603. if (!pdata) {
  1604. dev_err(&pdev->dev, "no platform data defined\n");
  1605. return -EINVAL;
  1606. }
  1607. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1608. i = platform_get_irq(pdev, 0);
  1609. if (!res || i < 0) {
  1610. dev_err(&pdev->dev, "cannot get platform resources\n");
  1611. return -ENOENT;
  1612. }
  1613. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1614. if (!priv) {
  1615. dev_err(&pdev->dev, "cannot allocate device data\n");
  1616. return -ENOMEM;
  1617. }
  1618. priv->dev = &pdev->dev;
  1619. priv->meram_dev = pdata->meram_dev;
  1620. platform_set_drvdata(pdev, priv);
  1621. error = request_irq(i, sh_mobile_lcdc_irq, 0,
  1622. dev_name(&pdev->dev), priv);
  1623. if (error) {
  1624. dev_err(&pdev->dev, "unable to request irq\n");
  1625. goto err1;
  1626. }
  1627. priv->irq = i;
  1628. atomic_set(&priv->hw_usecnt, -1);
  1629. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  1630. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  1631. ch->lcdc = priv;
  1632. memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  1633. error = sh_mobile_lcdc_check_interface(ch);
  1634. if (error) {
  1635. dev_err(&pdev->dev, "unsupported interface type\n");
  1636. goto err1;
  1637. }
  1638. init_waitqueue_head(&ch->frame_end_wait);
  1639. init_completion(&ch->vsync_completion);
  1640. ch->pan_offset = 0;
  1641. /* probe the backlight is there is one defined */
  1642. if (ch->cfg.bl_info.max_brightness)
  1643. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  1644. switch (pdata->ch[i].chan) {
  1645. case LCDC_CHAN_MAINLCD:
  1646. ch->enabled = LDCNT2R_ME;
  1647. ch->reg_offs = lcdc_offs_mainlcd;
  1648. num_channels++;
  1649. break;
  1650. case LCDC_CHAN_SUBLCD:
  1651. ch->enabled = LDCNT2R_SE;
  1652. ch->reg_offs = lcdc_offs_sublcd;
  1653. num_channels++;
  1654. break;
  1655. }
  1656. }
  1657. if (!num_channels) {
  1658. dev_err(&pdev->dev, "no channels defined\n");
  1659. error = -EINVAL;
  1660. goto err1;
  1661. }
  1662. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  1663. if (num_channels == 2)
  1664. priv->forced_fourcc = pdata->ch[0].fourcc;
  1665. priv->base = ioremap_nocache(res->start, resource_size(res));
  1666. if (!priv->base)
  1667. goto err1;
  1668. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  1669. if (error) {
  1670. dev_err(&pdev->dev, "unable to setup clocks\n");
  1671. goto err1;
  1672. }
  1673. /* Enable runtime PM. */
  1674. pm_runtime_enable(&pdev->dev);
  1675. for (i = 0; i < num_channels; i++) {
  1676. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1677. error = sh_mobile_lcdc_channel_init(priv, ch);
  1678. if (error)
  1679. goto err1;
  1680. }
  1681. error = sh_mobile_lcdc_start(priv);
  1682. if (error) {
  1683. dev_err(&pdev->dev, "unable to start hardware\n");
  1684. goto err1;
  1685. }
  1686. for (i = 0; i < num_channels; i++) {
  1687. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1688. error = sh_mobile_lcdc_channel_fb_register(ch);
  1689. if (error)
  1690. goto err1;
  1691. }
  1692. /* Failure ignored */
  1693. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1694. fb_register_client(&priv->notifier);
  1695. return 0;
  1696. err1:
  1697. sh_mobile_lcdc_remove(pdev);
  1698. return error;
  1699. }
  1700. static struct platform_driver sh_mobile_lcdc_driver = {
  1701. .driver = {
  1702. .name = "sh_mobile_lcdc_fb",
  1703. .owner = THIS_MODULE,
  1704. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1705. },
  1706. .probe = sh_mobile_lcdc_probe,
  1707. .remove = sh_mobile_lcdc_remove,
  1708. };
  1709. module_platform_driver(sh_mobile_lcdc_driver);
  1710. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1711. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1712. MODULE_LICENSE("GPL v2");