sh_mobile_lcdcfb.c 46 KB

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