sh_mobile_lcdcfb.c 39 KB

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