sh_mobile_lcdcfb.c 47 KB

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