sh_mobile_lcdcfb.c 50 KB

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