sh_mobile_lcdcfb.c 49 KB

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