sh_mobile_lcdcfb.c 39 KB

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