sh_mobile_lcdcfb.c 49 KB

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