sh_mobile_lcdcfb.c 47 KB

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