sh_mobile_lcdcfb.c 49 KB

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