sh_mobile_lcdcfb.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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. /* enable clocks before accessing the hardware */
  411. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  412. if (priv->ch[k].enabled) {
  413. sh_mobile_lcdc_clk_on(priv);
  414. if (!bpp)
  415. bpp = priv->ch[k].info->var.bits_per_pixel;
  416. }
  417. }
  418. /* reset */
  419. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LCDC_RESET);
  420. lcdc_wait_bit(priv, _LDCNT2R, LCDC_RESET, 0);
  421. /* enable LCDC channels */
  422. tmp = lcdc_read(priv, _LDCNT2R);
  423. tmp |= priv->ch[0].enabled;
  424. tmp |= priv->ch[1].enabled;
  425. lcdc_write(priv, _LDCNT2R, tmp);
  426. /* read data from external memory, avoid using the BEU for now */
  427. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) & ~DISPLAY_BEU);
  428. /* stop the lcdc first */
  429. sh_mobile_lcdc_start_stop(priv, 0);
  430. /* configure clocks */
  431. tmp = priv->lddckr;
  432. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  433. ch = &priv->ch[k];
  434. if (!priv->ch[k].enabled)
  435. continue;
  436. m = ch->cfg.clock_divider;
  437. if (!m)
  438. continue;
  439. if (m == 1)
  440. m = 1 << 6;
  441. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  442. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider denominator */
  443. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  444. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  445. }
  446. lcdc_write(priv, _LDDCKR, tmp);
  447. /* start dotclock again */
  448. lcdc_write(priv, _LDDCKSTPR, 0);
  449. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  450. /* interrupts are disabled to begin with */
  451. lcdc_write(priv, _LDINTR, 0);
  452. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  453. ch = &priv->ch[k];
  454. if (!ch->enabled)
  455. continue;
  456. sh_mobile_lcdc_geometry(ch);
  457. /* power supply */
  458. lcdc_write_chan(ch, LDPMR, 0);
  459. board_cfg = &ch->cfg.board_cfg;
  460. if (board_cfg->setup_sys) {
  461. int ret = board_cfg->setup_sys(board_cfg->board_data,
  462. ch, &sh_mobile_lcdc_sys_bus_ops);
  463. if (ret)
  464. return ret;
  465. }
  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. if (&ch->lcdc->notifier != nb)
  1169. return NOTIFY_DONE;
  1170. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1171. __func__, action, event->data);
  1172. switch(action) {
  1173. case FB_EVENT_SUSPEND:
  1174. if (board_cfg->display_off && try_module_get(board_cfg->owner)) {
  1175. board_cfg->display_off(board_cfg->board_data);
  1176. module_put(board_cfg->owner);
  1177. }
  1178. sh_mobile_lcdc_stop(ch->lcdc);
  1179. break;
  1180. case FB_EVENT_RESUME:
  1181. mutex_lock(&ch->open_lock);
  1182. sh_mobile_fb_reconfig(info);
  1183. mutex_unlock(&ch->open_lock);
  1184. /* HDMI must be enabled before LCDC configuration */
  1185. if (board_cfg->display_on && try_module_get(board_cfg->owner)) {
  1186. board_cfg->display_on(board_cfg->board_data, info);
  1187. module_put(board_cfg->owner);
  1188. }
  1189. sh_mobile_lcdc_start(ch->lcdc);
  1190. }
  1191. return NOTIFY_OK;
  1192. }
  1193. static int sh_mobile_lcdc_remove(struct platform_device *pdev);
  1194. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  1195. {
  1196. struct fb_info *info;
  1197. struct sh_mobile_lcdc_priv *priv;
  1198. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  1199. struct resource *res;
  1200. int error;
  1201. void *buf;
  1202. int i, j;
  1203. if (!pdata) {
  1204. dev_err(&pdev->dev, "no platform data defined\n");
  1205. return -EINVAL;
  1206. }
  1207. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1208. i = platform_get_irq(pdev, 0);
  1209. if (!res || i < 0) {
  1210. dev_err(&pdev->dev, "cannot get platform resources\n");
  1211. return -ENOENT;
  1212. }
  1213. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1214. if (!priv) {
  1215. dev_err(&pdev->dev, "cannot allocate device data\n");
  1216. return -ENOMEM;
  1217. }
  1218. platform_set_drvdata(pdev, priv);
  1219. error = request_irq(i, sh_mobile_lcdc_irq, IRQF_DISABLED,
  1220. dev_name(&pdev->dev), priv);
  1221. if (error) {
  1222. dev_err(&pdev->dev, "unable to request irq\n");
  1223. goto err1;
  1224. }
  1225. priv->irq = i;
  1226. atomic_set(&priv->hw_usecnt, -1);
  1227. j = 0;
  1228. for (i = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  1229. struct sh_mobile_lcdc_chan *ch = priv->ch + j;
  1230. ch->lcdc = priv;
  1231. memcpy(&ch->cfg, &pdata->ch[i], sizeof(pdata->ch[i]));
  1232. error = sh_mobile_lcdc_check_interface(ch);
  1233. if (error) {
  1234. dev_err(&pdev->dev, "unsupported interface type\n");
  1235. goto err1;
  1236. }
  1237. init_waitqueue_head(&ch->frame_end_wait);
  1238. init_completion(&ch->vsync_completion);
  1239. ch->pan_offset = 0;
  1240. /* probe the backlight is there is one defined */
  1241. if (ch->cfg.bl_info.max_brightness)
  1242. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  1243. switch (pdata->ch[i].chan) {
  1244. case LCDC_CHAN_MAINLCD:
  1245. ch->enabled = 1 << 1;
  1246. ch->reg_offs = lcdc_offs_mainlcd;
  1247. j++;
  1248. break;
  1249. case LCDC_CHAN_SUBLCD:
  1250. ch->enabled = 1 << 2;
  1251. ch->reg_offs = lcdc_offs_sublcd;
  1252. j++;
  1253. break;
  1254. }
  1255. }
  1256. if (!j) {
  1257. dev_err(&pdev->dev, "no channels defined\n");
  1258. error = -EINVAL;
  1259. goto err1;
  1260. }
  1261. /* for dual channel LCDC (MAIN + SUB) force shared bpp setting */
  1262. if (j == 2)
  1263. priv->forced_bpp = pdata->ch[0].bpp;
  1264. priv->base = ioremap_nocache(res->start, resource_size(res));
  1265. if (!priv->base)
  1266. goto err1;
  1267. error = sh_mobile_lcdc_setup_clocks(pdev, pdata->clock_source, priv);
  1268. if (error) {
  1269. dev_err(&pdev->dev, "unable to setup clocks\n");
  1270. goto err1;
  1271. }
  1272. priv->meram_dev = pdata->meram_dev;
  1273. for (i = 0; i < j; i++) {
  1274. struct fb_var_screeninfo *var;
  1275. const struct fb_videomode *lcd_cfg, *max_cfg = NULL;
  1276. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1277. struct sh_mobile_lcdc_chan_cfg *cfg = &ch->cfg;
  1278. const struct fb_videomode *mode = cfg->lcd_cfg;
  1279. unsigned long max_size = 0;
  1280. int k;
  1281. int num_cfg;
  1282. ch->info = framebuffer_alloc(0, &pdev->dev);
  1283. if (!ch->info) {
  1284. dev_err(&pdev->dev, "unable to allocate fb_info\n");
  1285. error = -ENOMEM;
  1286. break;
  1287. }
  1288. info = ch->info;
  1289. var = &info->var;
  1290. info->fbops = &sh_mobile_lcdc_ops;
  1291. info->par = ch;
  1292. mutex_init(&ch->open_lock);
  1293. for (k = 0, lcd_cfg = mode;
  1294. k < cfg->num_cfg && lcd_cfg;
  1295. k++, lcd_cfg++) {
  1296. unsigned long size = lcd_cfg->yres * lcd_cfg->xres;
  1297. /* NV12 buffers must have even number of lines */
  1298. if ((cfg->nonstd) && cfg->bpp == 12 &&
  1299. (lcd_cfg->yres & 0x1)) {
  1300. dev_err(&pdev->dev, "yres must be multiple of 2"
  1301. " for YCbCr420 mode.\n");
  1302. error = -EINVAL;
  1303. goto err1;
  1304. }
  1305. if (size > max_size) {
  1306. max_cfg = lcd_cfg;
  1307. max_size = size;
  1308. }
  1309. }
  1310. if (!mode)
  1311. max_size = MAX_XRES * MAX_YRES;
  1312. else if (max_cfg)
  1313. dev_dbg(&pdev->dev, "Found largest videomode %ux%u\n",
  1314. max_cfg->xres, max_cfg->yres);
  1315. info->fix = sh_mobile_lcdc_fix;
  1316. info->fix.smem_len = max_size * 2 * cfg->bpp / 8;
  1317. /* Only pan in 2 line steps for NV12 */
  1318. if (cfg->nonstd && cfg->bpp == 12)
  1319. info->fix.ypanstep = 2;
  1320. if (!mode) {
  1321. mode = &default_720p;
  1322. num_cfg = 1;
  1323. } else {
  1324. num_cfg = cfg->num_cfg;
  1325. }
  1326. fb_videomode_to_modelist(mode, num_cfg, &info->modelist);
  1327. fb_videomode_to_var(var, mode);
  1328. var->width = cfg->lcd_size_cfg.width;
  1329. var->height = cfg->lcd_size_cfg.height;
  1330. /* Default Y virtual resolution is 2x panel size */
  1331. var->yres_virtual = var->yres * 2;
  1332. var->activate = FB_ACTIVATE_NOW;
  1333. error = sh_mobile_lcdc_set_bpp(var, cfg->bpp, cfg->nonstd);
  1334. if (error)
  1335. break;
  1336. buf = dma_alloc_coherent(&pdev->dev, info->fix.smem_len,
  1337. &ch->dma_handle, GFP_KERNEL);
  1338. if (!buf) {
  1339. dev_err(&pdev->dev, "unable to allocate buffer\n");
  1340. error = -ENOMEM;
  1341. break;
  1342. }
  1343. info->pseudo_palette = &ch->pseudo_palette;
  1344. info->flags = FBINFO_FLAG_DEFAULT;
  1345. error = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1346. if (error < 0) {
  1347. dev_err(&pdev->dev, "unable to allocate cmap\n");
  1348. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1349. buf, ch->dma_handle);
  1350. break;
  1351. }
  1352. info->fix.smem_start = ch->dma_handle;
  1353. if (var->nonstd)
  1354. info->fix.line_length = var->xres;
  1355. else
  1356. info->fix.line_length = var->xres * (cfg->bpp / 8);
  1357. info->screen_base = buf;
  1358. info->device = &pdev->dev;
  1359. ch->display_var = *var;
  1360. }
  1361. if (error)
  1362. goto err1;
  1363. error = sh_mobile_lcdc_start(priv);
  1364. if (error) {
  1365. dev_err(&pdev->dev, "unable to start hardware\n");
  1366. goto err1;
  1367. }
  1368. for (i = 0; i < j; i++) {
  1369. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1370. info = ch->info;
  1371. if (info->fbdefio) {
  1372. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1373. info->fix.smem_len >> PAGE_SHIFT);
  1374. if (!ch->sglist) {
  1375. dev_err(&pdev->dev, "cannot allocate sglist\n");
  1376. goto err1;
  1377. }
  1378. }
  1379. info->bl_dev = ch->bl;
  1380. error = register_framebuffer(info);
  1381. if (error < 0)
  1382. goto err1;
  1383. dev_info(info->dev,
  1384. "registered %s/%s as %dx%d %dbpp.\n",
  1385. pdev->name,
  1386. (ch->cfg.chan == LCDC_CHAN_MAINLCD) ?
  1387. "mainlcd" : "sublcd",
  1388. info->var.xres, info->var.yres,
  1389. ch->cfg.bpp);
  1390. /* deferred io mode: disable clock to save power */
  1391. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1392. sh_mobile_lcdc_clk_off(priv);
  1393. }
  1394. /* Failure ignored */
  1395. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1396. fb_register_client(&priv->notifier);
  1397. return 0;
  1398. err1:
  1399. sh_mobile_lcdc_remove(pdev);
  1400. return error;
  1401. }
  1402. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1403. {
  1404. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1405. struct fb_info *info;
  1406. int i;
  1407. fb_unregister_client(&priv->notifier);
  1408. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1409. if (priv->ch[i].info && priv->ch[i].info->dev)
  1410. unregister_framebuffer(priv->ch[i].info);
  1411. sh_mobile_lcdc_stop(priv);
  1412. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1413. info = priv->ch[i].info;
  1414. if (!info || !info->device)
  1415. continue;
  1416. if (priv->ch[i].sglist)
  1417. vfree(priv->ch[i].sglist);
  1418. if (info->screen_base)
  1419. dma_free_coherent(&pdev->dev, info->fix.smem_len,
  1420. info->screen_base,
  1421. priv->ch[i].dma_handle);
  1422. fb_dealloc_cmap(&info->cmap);
  1423. framebuffer_release(info);
  1424. }
  1425. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1426. if (priv->ch[i].bl)
  1427. sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
  1428. }
  1429. if (priv->dot_clk)
  1430. clk_put(priv->dot_clk);
  1431. if (priv->dev)
  1432. pm_runtime_disable(priv->dev);
  1433. if (priv->base)
  1434. iounmap(priv->base);
  1435. if (priv->irq)
  1436. free_irq(priv->irq, priv);
  1437. kfree(priv);
  1438. return 0;
  1439. }
  1440. static struct platform_driver sh_mobile_lcdc_driver = {
  1441. .driver = {
  1442. .name = "sh_mobile_lcdc_fb",
  1443. .owner = THIS_MODULE,
  1444. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1445. },
  1446. .probe = sh_mobile_lcdc_probe,
  1447. .remove = sh_mobile_lcdc_remove,
  1448. };
  1449. static int __init sh_mobile_lcdc_init(void)
  1450. {
  1451. return platform_driver_register(&sh_mobile_lcdc_driver);
  1452. }
  1453. static void __exit sh_mobile_lcdc_exit(void)
  1454. {
  1455. platform_driver_unregister(&sh_mobile_lcdc_driver);
  1456. }
  1457. module_init(sh_mobile_lcdc_init);
  1458. module_exit(sh_mobile_lcdc_exit);
  1459. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1460. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1461. MODULE_LICENSE("GPL v2");