sh_mobile_lcdcfb.c 46 KB

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