sh_mobile_lcdcfb.c 50 KB

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