sh_mobile_lcdcfb.c 43 KB

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