sh_mobile_lcdcfb.c 46 KB

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