sh_mobile_lcdcfb.c 42 KB

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