sh_mobile_lcdcfb.c 43 KB

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