sh_mobile_lcdcfb.c 43 KB

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