sh_mobile_lcdcfb.c 41 KB

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