sh_mobile_lcdcfb.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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/atomic.h>
  11. #include <linux/backlight.h>
  12. #include <linux/clk.h>
  13. #include <linux/console.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/delay.h>
  16. #include <linux/gpio.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/ioctl.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/slab.h>
  26. #include <linux/videodev2.h>
  27. #include <linux/vmalloc.h>
  28. #include <video/sh_mobile_lcdc.h>
  29. #include <video/sh_mobile_meram.h>
  30. #include "sh_mobile_lcdcfb.h"
  31. #define SIDE_B_OFFSET 0x1000
  32. #define MIRROR_OFFSET 0x2000
  33. #define MAX_XRES 1920
  34. #define MAX_YRES 1080
  35. struct sh_mobile_lcdc_priv {
  36. void __iomem *base;
  37. int irq;
  38. atomic_t hw_usecnt;
  39. struct device *dev;
  40. struct clk *dot_clk;
  41. unsigned long lddckr;
  42. struct sh_mobile_lcdc_chan ch[2];
  43. struct notifier_block notifier;
  44. int started;
  45. int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
  46. struct sh_mobile_meram_info *meram_dev;
  47. };
  48. /* -----------------------------------------------------------------------------
  49. * Registers access
  50. */
  51. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  52. [LDDCKPAT1R] = 0x400,
  53. [LDDCKPAT2R] = 0x404,
  54. [LDMT1R] = 0x418,
  55. [LDMT2R] = 0x41c,
  56. [LDMT3R] = 0x420,
  57. [LDDFR] = 0x424,
  58. [LDSM1R] = 0x428,
  59. [LDSM2R] = 0x42c,
  60. [LDSA1R] = 0x430,
  61. [LDSA2R] = 0x434,
  62. [LDMLSR] = 0x438,
  63. [LDHCNR] = 0x448,
  64. [LDHSYNR] = 0x44c,
  65. [LDVLNR] = 0x450,
  66. [LDVSYNR] = 0x454,
  67. [LDPMR] = 0x460,
  68. [LDHAJR] = 0x4a0,
  69. };
  70. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  71. [LDDCKPAT1R] = 0x408,
  72. [LDDCKPAT2R] = 0x40c,
  73. [LDMT1R] = 0x600,
  74. [LDMT2R] = 0x604,
  75. [LDMT3R] = 0x608,
  76. [LDDFR] = 0x60c,
  77. [LDSM1R] = 0x610,
  78. [LDSM2R] = 0x614,
  79. [LDSA1R] = 0x618,
  80. [LDMLSR] = 0x620,
  81. [LDHCNR] = 0x624,
  82. [LDHSYNR] = 0x628,
  83. [LDVLNR] = 0x62c,
  84. [LDVSYNR] = 0x630,
  85. [LDPMR] = 0x63c,
  86. };
  87. static bool banked(int reg_nr)
  88. {
  89. switch (reg_nr) {
  90. case LDMT1R:
  91. case LDMT2R:
  92. case LDMT3R:
  93. case LDDFR:
  94. case LDSM1R:
  95. case LDSA1R:
  96. case LDSA2R:
  97. case LDMLSR:
  98. case LDHCNR:
  99. case LDHSYNR:
  100. case LDVLNR:
  101. case LDVSYNR:
  102. return true;
  103. }
  104. return false;
  105. }
  106. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  107. {
  108. return chan->cfg->chan == LCDC_CHAN_SUBLCD;
  109. }
  110. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  111. int reg_nr, unsigned long data)
  112. {
  113. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  114. if (banked(reg_nr))
  115. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  116. SIDE_B_OFFSET);
  117. }
  118. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  119. int reg_nr, unsigned long data)
  120. {
  121. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  122. MIRROR_OFFSET);
  123. }
  124. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  125. int reg_nr)
  126. {
  127. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  128. }
  129. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  130. unsigned long reg_offs, unsigned long data)
  131. {
  132. iowrite32(data, priv->base + reg_offs);
  133. }
  134. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  135. unsigned long reg_offs)
  136. {
  137. return ioread32(priv->base + reg_offs);
  138. }
  139. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  140. unsigned long reg_offs,
  141. unsigned long mask, unsigned long until)
  142. {
  143. while ((lcdc_read(priv, reg_offs) & mask) != until)
  144. cpu_relax();
  145. }
  146. /* -----------------------------------------------------------------------------
  147. * Clock management
  148. */
  149. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  150. {
  151. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  152. if (priv->dot_clk)
  153. clk_enable(priv->dot_clk);
  154. pm_runtime_get_sync(priv->dev);
  155. if (priv->meram_dev && priv->meram_dev->pdev)
  156. pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
  157. }
  158. }
  159. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  160. {
  161. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  162. if (priv->meram_dev && priv->meram_dev->pdev)
  163. pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
  164. pm_runtime_put(priv->dev);
  165. if (priv->dot_clk)
  166. clk_disable(priv->dot_clk);
  167. }
  168. }
  169. static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
  170. int clock_source)
  171. {
  172. struct clk *clk;
  173. char *str;
  174. switch (clock_source) {
  175. case LCDC_CLK_BUS:
  176. str = "bus_clk";
  177. priv->lddckr = LDDCKR_ICKSEL_BUS;
  178. break;
  179. case LCDC_CLK_PERIPHERAL:
  180. str = "peripheral_clk";
  181. priv->lddckr = LDDCKR_ICKSEL_MIPI;
  182. break;
  183. case LCDC_CLK_EXTERNAL:
  184. str = NULL;
  185. priv->lddckr = LDDCKR_ICKSEL_HDMI;
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. if (str == NULL)
  191. return 0;
  192. clk = clk_get(priv->dev, str);
  193. if (IS_ERR(clk)) {
  194. dev_err(priv->dev, "cannot get dot clock %s\n", str);
  195. return PTR_ERR(clk);
  196. }
  197. priv->dot_clk = clk;
  198. return 0;
  199. }
  200. /* -----------------------------------------------------------------------------
  201. * Display, panel and deferred I/O
  202. */
  203. static void lcdc_sys_write_index(void *handle, unsigned long data)
  204. {
  205. struct sh_mobile_lcdc_chan *ch = handle;
  206. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
  207. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  208. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  209. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  210. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  211. }
  212. static void lcdc_sys_write_data(void *handle, unsigned long data)
  213. {
  214. struct sh_mobile_lcdc_chan *ch = handle;
  215. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
  216. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  217. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  218. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  219. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  220. }
  221. static unsigned long lcdc_sys_read_data(void *handle)
  222. {
  223. struct sh_mobile_lcdc_chan *ch = handle;
  224. lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
  225. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  226. lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
  227. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  228. udelay(1);
  229. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  230. return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
  231. }
  232. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  233. lcdc_sys_write_index,
  234. lcdc_sys_write_data,
  235. lcdc_sys_read_data,
  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 = ch->fb_size >> 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. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_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(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  275. if (panel->start_transfer)
  276. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  277. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  278. dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
  279. DMA_TO_DEVICE);
  280. } else {
  281. if (panel->start_transfer)
  282. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  283. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  284. }
  285. }
  286. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  287. {
  288. struct fb_deferred_io *fbdefio = info->fbdefio;
  289. if (fbdefio)
  290. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  291. }
  292. static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
  293. {
  294. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  295. if (ch->tx_dev) {
  296. int ret;
  297. ret = ch->tx_dev->ops->display_on(ch->tx_dev);
  298. if (ret < 0)
  299. return;
  300. if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
  301. ch->info->state = FBINFO_STATE_SUSPENDED;
  302. }
  303. /* HDMI must be enabled before LCDC configuration */
  304. if (panel->display_on)
  305. panel->display_on();
  306. }
  307. static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
  308. {
  309. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  310. if (panel->display_off)
  311. panel->display_off();
  312. if (ch->tx_dev)
  313. ch->tx_dev->ops->display_off(ch->tx_dev);
  314. }
  315. static bool
  316. sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
  317. const struct fb_videomode *new_mode)
  318. {
  319. dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
  320. ch->display.mode.xres, ch->display.mode.yres,
  321. new_mode->xres, new_mode->yres);
  322. /* It can be a different monitor with an equal video-mode */
  323. if (fb_mode_is_equal(&ch->display.mode, new_mode))
  324. return false;
  325. dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
  326. ch->display.mode.yres, new_mode->yres);
  327. ch->display.mode = *new_mode;
  328. return true;
  329. }
  330. static int sh_mobile_check_var(struct fb_var_screeninfo *var,
  331. struct fb_info *info);
  332. static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
  333. enum sh_mobile_lcdc_entity_event event,
  334. const struct fb_videomode *mode,
  335. const struct fb_monspecs *monspec)
  336. {
  337. struct fb_info *info = ch->info;
  338. struct fb_var_screeninfo var;
  339. int ret = 0;
  340. switch (event) {
  341. case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
  342. /* HDMI plug in */
  343. if (lock_fb_info(info)) {
  344. console_lock();
  345. ch->display.width = monspec->max_x * 10;
  346. ch->display.height = monspec->max_y * 10;
  347. if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
  348. info->state == FBINFO_STATE_RUNNING) {
  349. /* First activation with the default monitor.
  350. * Just turn on, if we run a resume here, the
  351. * logo disappears.
  352. */
  353. info->var.width = monspec->max_x * 10;
  354. info->var.height = monspec->max_y * 10;
  355. sh_mobile_lcdc_display_on(ch);
  356. } else {
  357. /* New monitor or have to wake up */
  358. fb_set_suspend(info, 0);
  359. }
  360. console_unlock();
  361. unlock_fb_info(info);
  362. }
  363. break;
  364. case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
  365. /* HDMI disconnect */
  366. if (lock_fb_info(info)) {
  367. console_lock();
  368. fb_set_suspend(info, 1);
  369. console_unlock();
  370. unlock_fb_info(info);
  371. }
  372. break;
  373. case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
  374. /* Validate a proposed new mode */
  375. fb_videomode_to_var(&var, mode);
  376. var.bits_per_pixel = info->var.bits_per_pixel;
  377. var.grayscale = info->var.grayscale;
  378. ret = sh_mobile_check_var(&var, info);
  379. break;
  380. }
  381. return ret;
  382. }
  383. /* -----------------------------------------------------------------------------
  384. * Format helpers
  385. */
  386. struct sh_mobile_lcdc_format_info {
  387. u32 fourcc;
  388. unsigned int bpp;
  389. bool yuv;
  390. u32 lddfr;
  391. };
  392. static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
  393. {
  394. .fourcc = V4L2_PIX_FMT_RGB565,
  395. .bpp = 16,
  396. .yuv = false,
  397. .lddfr = LDDFR_PKF_RGB16,
  398. }, {
  399. .fourcc = V4L2_PIX_FMT_BGR24,
  400. .bpp = 24,
  401. .yuv = false,
  402. .lddfr = LDDFR_PKF_RGB24,
  403. }, {
  404. .fourcc = V4L2_PIX_FMT_BGR32,
  405. .bpp = 32,
  406. .yuv = false,
  407. .lddfr = LDDFR_PKF_ARGB32,
  408. }, {
  409. .fourcc = V4L2_PIX_FMT_NV12,
  410. .bpp = 12,
  411. .yuv = true,
  412. .lddfr = LDDFR_CC | LDDFR_YF_420,
  413. }, {
  414. .fourcc = V4L2_PIX_FMT_NV21,
  415. .bpp = 12,
  416. .yuv = true,
  417. .lddfr = LDDFR_CC | LDDFR_YF_420,
  418. }, {
  419. .fourcc = V4L2_PIX_FMT_NV16,
  420. .bpp = 16,
  421. .yuv = true,
  422. .lddfr = LDDFR_CC | LDDFR_YF_422,
  423. }, {
  424. .fourcc = V4L2_PIX_FMT_NV61,
  425. .bpp = 16,
  426. .yuv = true,
  427. .lddfr = LDDFR_CC | LDDFR_YF_422,
  428. }, {
  429. .fourcc = V4L2_PIX_FMT_NV24,
  430. .bpp = 24,
  431. .yuv = true,
  432. .lddfr = LDDFR_CC | LDDFR_YF_444,
  433. }, {
  434. .fourcc = V4L2_PIX_FMT_NV42,
  435. .bpp = 24,
  436. .yuv = true,
  437. .lddfr = LDDFR_CC | LDDFR_YF_444,
  438. },
  439. };
  440. static const struct sh_mobile_lcdc_format_info *
  441. sh_mobile_format_info(u32 fourcc)
  442. {
  443. unsigned int i;
  444. for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
  445. if (sh_mobile_format_infos[i].fourcc == fourcc)
  446. return &sh_mobile_format_infos[i];
  447. }
  448. return NULL;
  449. }
  450. static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
  451. {
  452. if (var->grayscale > 1)
  453. return var->grayscale;
  454. switch (var->bits_per_pixel) {
  455. case 16:
  456. return V4L2_PIX_FMT_RGB565;
  457. case 24:
  458. return V4L2_PIX_FMT_BGR24;
  459. case 32:
  460. return V4L2_PIX_FMT_BGR32;
  461. default:
  462. return 0;
  463. }
  464. }
  465. static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
  466. {
  467. return var->grayscale > 1;
  468. }
  469. /* -----------------------------------------------------------------------------
  470. * Start, stop and IRQ
  471. */
  472. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  473. {
  474. struct sh_mobile_lcdc_priv *priv = data;
  475. struct sh_mobile_lcdc_chan *ch;
  476. unsigned long ldintr;
  477. int is_sub;
  478. int k;
  479. /* Acknowledge interrupts and disable further VSYNC End IRQs. */
  480. ldintr = lcdc_read(priv, _LDINTR);
  481. lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
  482. /* figure out if this interrupt is for main or sub lcd */
  483. is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
  484. /* wake up channel and disable clocks */
  485. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  486. ch = &priv->ch[k];
  487. if (!ch->enabled)
  488. continue;
  489. /* Frame End */
  490. if (ldintr & LDINTR_FS) {
  491. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  492. ch->frame_end = 1;
  493. wake_up(&ch->frame_end_wait);
  494. sh_mobile_lcdc_clk_off(priv);
  495. }
  496. }
  497. /* VSYNC End */
  498. if (ldintr & LDINTR_VES)
  499. complete(&ch->vsync_completion);
  500. }
  501. return IRQ_HANDLED;
  502. }
  503. static int sh_mobile_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
  504. {
  505. unsigned long ldintr;
  506. int ret;
  507. /* Enable VSync End interrupt and be careful not to acknowledge any
  508. * pending interrupt.
  509. */
  510. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  511. ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
  512. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  513. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  514. msecs_to_jiffies(100));
  515. if (!ret)
  516. return -ETIMEDOUT;
  517. return 0;
  518. }
  519. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  520. int start)
  521. {
  522. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  523. int k;
  524. /* start or stop the lcdc */
  525. if (start)
  526. lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
  527. else
  528. lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
  529. /* wait until power is applied/stopped on all channels */
  530. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  531. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  532. while (1) {
  533. tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
  534. & LDPMR_LPS;
  535. if (start && tmp == LDPMR_LPS)
  536. break;
  537. if (!start && tmp == 0)
  538. break;
  539. cpu_relax();
  540. }
  541. if (!start)
  542. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  543. }
  544. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  545. {
  546. const struct fb_var_screeninfo *var = &ch->info->var;
  547. const struct fb_videomode *mode = &ch->display.mode;
  548. unsigned long h_total, hsync_pos, display_h_total;
  549. u32 tmp;
  550. tmp = ch->ldmt1r_value;
  551. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
  552. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
  553. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
  554. tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
  555. tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
  556. tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
  557. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
  558. lcdc_write_chan(ch, LDMT1R, tmp);
  559. /* setup SYS bus */
  560. lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
  561. lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
  562. /* horizontal configuration */
  563. h_total = mode->xres + mode->hsync_len + mode->left_margin
  564. + mode->right_margin;
  565. tmp = h_total / 8; /* HTCN */
  566. tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
  567. lcdc_write_chan(ch, LDHCNR, tmp);
  568. hsync_pos = mode->xres + mode->right_margin;
  569. tmp = hsync_pos / 8; /* HSYNP */
  570. tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
  571. lcdc_write_chan(ch, LDHSYNR, tmp);
  572. /* vertical configuration */
  573. tmp = mode->yres + mode->vsync_len + mode->upper_margin
  574. + mode->lower_margin; /* VTLN */
  575. tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
  576. lcdc_write_chan(ch, LDVLNR, tmp);
  577. tmp = mode->yres + mode->lower_margin; /* VSYNP */
  578. tmp |= mode->vsync_len << 16; /* VSYNW */
  579. lcdc_write_chan(ch, LDVSYNR, tmp);
  580. /* Adjust horizontal synchronisation for HDMI */
  581. display_h_total = mode->xres + mode->hsync_len + mode->left_margin
  582. + mode->right_margin;
  583. tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
  584. | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
  585. lcdc_write_chan(ch, LDHAJR, tmp);
  586. }
  587. /*
  588. * __sh_mobile_lcdc_start - Configure and tart the LCDC
  589. * @priv: LCDC device
  590. *
  591. * Configure all enabled channels and start the LCDC device. All external
  592. * devices (clocks, MERAM, panels, ...) are not touched by this function.
  593. */
  594. static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  595. {
  596. struct sh_mobile_lcdc_chan *ch;
  597. unsigned long tmp;
  598. int k, m;
  599. /* Enable LCDC channels. Read data from external memory, avoid using the
  600. * BEU for now.
  601. */
  602. lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
  603. /* Stop the LCDC first and disable all interrupts. */
  604. sh_mobile_lcdc_start_stop(priv, 0);
  605. lcdc_write(priv, _LDINTR, 0);
  606. /* Configure power supply, dot clocks and start them. */
  607. tmp = priv->lddckr;
  608. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  609. ch = &priv->ch[k];
  610. if (!ch->enabled)
  611. continue;
  612. /* Power supply */
  613. lcdc_write_chan(ch, LDPMR, 0);
  614. m = ch->cfg->clock_divider;
  615. if (!m)
  616. continue;
  617. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  618. * denominator.
  619. */
  620. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  621. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  622. if (m == 1)
  623. m = LDDCKR_MOSEL;
  624. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  625. }
  626. lcdc_write(priv, _LDDCKR, tmp);
  627. lcdc_write(priv, _LDDCKSTPR, 0);
  628. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  629. /* Setup geometry, format, frame buffer memory and operation mode. */
  630. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  631. ch = &priv->ch[k];
  632. if (!ch->enabled)
  633. continue;
  634. sh_mobile_lcdc_geometry(ch);
  635. tmp = ch->format->lddfr;
  636. if (ch->format->yuv) {
  637. switch (ch->colorspace) {
  638. case V4L2_COLORSPACE_REC709:
  639. tmp |= LDDFR_CF1;
  640. break;
  641. case V4L2_COLORSPACE_JPEG:
  642. tmp |= LDDFR_CF0;
  643. break;
  644. }
  645. }
  646. lcdc_write_chan(ch, LDDFR, tmp);
  647. lcdc_write_chan(ch, LDMLSR, ch->pitch);
  648. lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
  649. if (ch->format->yuv)
  650. lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
  651. /* When using deferred I/O mode, configure the LCDC for one-shot
  652. * operation and enable the frame end interrupt. Otherwise use
  653. * continuous read mode.
  654. */
  655. if (ch->ldmt1r_value & LDMT1R_IFM &&
  656. ch->cfg->sys_bus_cfg.deferred_io_msec) {
  657. lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
  658. lcdc_write(priv, _LDINTR, LDINTR_FE);
  659. } else {
  660. lcdc_write_chan(ch, LDSM1R, 0);
  661. }
  662. }
  663. /* Word and long word swap. */
  664. switch (priv->ch[0].format->fourcc) {
  665. case V4L2_PIX_FMT_RGB565:
  666. case V4L2_PIX_FMT_NV21:
  667. case V4L2_PIX_FMT_NV61:
  668. case V4L2_PIX_FMT_NV42:
  669. tmp = LDDDSR_LS | LDDDSR_WS;
  670. break;
  671. case V4L2_PIX_FMT_BGR24:
  672. case V4L2_PIX_FMT_NV12:
  673. case V4L2_PIX_FMT_NV16:
  674. case V4L2_PIX_FMT_NV24:
  675. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  676. break;
  677. case V4L2_PIX_FMT_BGR32:
  678. default:
  679. tmp = LDDDSR_LS;
  680. break;
  681. }
  682. lcdc_write(priv, _LDDDSR, tmp);
  683. /* Enable the display output. */
  684. lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
  685. sh_mobile_lcdc_start_stop(priv, 1);
  686. priv->started = 1;
  687. }
  688. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  689. {
  690. struct sh_mobile_meram_info *mdev = priv->meram_dev;
  691. struct sh_mobile_lcdc_chan *ch;
  692. unsigned long tmp;
  693. int ret;
  694. int k;
  695. /* enable clocks before accessing the hardware */
  696. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  697. if (priv->ch[k].enabled)
  698. sh_mobile_lcdc_clk_on(priv);
  699. }
  700. /* reset */
  701. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
  702. lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
  703. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  704. const struct sh_mobile_lcdc_panel_cfg *panel;
  705. ch = &priv->ch[k];
  706. if (!ch->enabled)
  707. continue;
  708. panel = &ch->cfg->panel_cfg;
  709. if (panel->setup_sys) {
  710. ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
  711. if (ret)
  712. return ret;
  713. }
  714. }
  715. /* Compute frame buffer base address and pitch for each channel. */
  716. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  717. int pixelformat;
  718. void *meram;
  719. ch = &priv->ch[k];
  720. if (!ch->enabled)
  721. continue;
  722. ch->base_addr_y = ch->dma_handle;
  723. ch->base_addr_c = ch->base_addr_y + ch->xres * ch->yres_virtual;
  724. /* Enable MERAM if possible. */
  725. if (mdev == NULL || mdev->ops == NULL ||
  726. ch->cfg->meram_cfg == NULL)
  727. continue;
  728. /* we need to de-init configured ICBs before we can
  729. * re-initialize them.
  730. */
  731. if (ch->meram) {
  732. mdev->ops->meram_unregister(mdev, ch->meram);
  733. ch->meram = NULL;
  734. }
  735. switch (ch->format->fourcc) {
  736. case V4L2_PIX_FMT_NV12:
  737. case V4L2_PIX_FMT_NV21:
  738. case V4L2_PIX_FMT_NV16:
  739. case V4L2_PIX_FMT_NV61:
  740. pixelformat = SH_MOBILE_MERAM_PF_NV;
  741. break;
  742. case V4L2_PIX_FMT_NV24:
  743. case V4L2_PIX_FMT_NV42:
  744. pixelformat = SH_MOBILE_MERAM_PF_NV24;
  745. break;
  746. case V4L2_PIX_FMT_RGB565:
  747. case V4L2_PIX_FMT_BGR24:
  748. case V4L2_PIX_FMT_BGR32:
  749. default:
  750. pixelformat = SH_MOBILE_MERAM_PF_RGB;
  751. break;
  752. }
  753. meram = mdev->ops->meram_register(mdev, ch->cfg->meram_cfg,
  754. ch->pitch, ch->yres, pixelformat,
  755. &ch->pitch);
  756. if (!IS_ERR(meram)) {
  757. mdev->ops->meram_update(mdev, meram,
  758. ch->base_addr_y, ch->base_addr_c,
  759. &ch->base_addr_y, &ch->base_addr_c);
  760. ch->meram = meram;
  761. }
  762. }
  763. /* Start the LCDC. */
  764. __sh_mobile_lcdc_start(priv);
  765. /* Setup deferred I/O, tell the board code to enable the panels, and
  766. * turn backlight on.
  767. */
  768. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  769. ch = &priv->ch[k];
  770. if (!ch->enabled)
  771. continue;
  772. tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
  773. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  774. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  775. ch->defio.delay = msecs_to_jiffies(tmp);
  776. ch->info->fbdefio = &ch->defio;
  777. fb_deferred_io_init(ch->info);
  778. }
  779. sh_mobile_lcdc_display_on(ch);
  780. if (ch->bl) {
  781. ch->bl->props.power = FB_BLANK_UNBLANK;
  782. backlight_update_status(ch->bl);
  783. }
  784. }
  785. return 0;
  786. }
  787. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  788. {
  789. struct sh_mobile_lcdc_chan *ch;
  790. int k;
  791. /* clean up deferred io and ask board code to disable panel */
  792. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  793. ch = &priv->ch[k];
  794. if (!ch->enabled)
  795. continue;
  796. /* deferred io mode:
  797. * flush frame, and wait for frame end interrupt
  798. * clean up deferred io and enable clock
  799. */
  800. if (ch->info && ch->info->fbdefio) {
  801. ch->frame_end = 0;
  802. schedule_delayed_work(&ch->info->deferred_work, 0);
  803. wait_event(ch->frame_end_wait, ch->frame_end);
  804. fb_deferred_io_cleanup(ch->info);
  805. ch->info->fbdefio = NULL;
  806. sh_mobile_lcdc_clk_on(priv);
  807. }
  808. if (ch->bl) {
  809. ch->bl->props.power = FB_BLANK_POWERDOWN;
  810. backlight_update_status(ch->bl);
  811. }
  812. sh_mobile_lcdc_display_off(ch);
  813. /* disable the meram */
  814. if (ch->meram) {
  815. struct sh_mobile_meram_info *mdev;
  816. mdev = priv->meram_dev;
  817. mdev->ops->meram_unregister(mdev, ch->meram);
  818. ch->meram = 0;
  819. }
  820. }
  821. /* stop the lcdc */
  822. if (priv->started) {
  823. sh_mobile_lcdc_start_stop(priv, 0);
  824. priv->started = 0;
  825. }
  826. /* stop clocks */
  827. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  828. if (priv->ch[k].enabled)
  829. sh_mobile_lcdc_clk_off(priv);
  830. }
  831. /* -----------------------------------------------------------------------------
  832. * Frame buffer operations
  833. */
  834. static int sh_mobile_lcdc_setcolreg(u_int regno,
  835. u_int red, u_int green, u_int blue,
  836. u_int transp, struct fb_info *info)
  837. {
  838. u32 *palette = info->pseudo_palette;
  839. if (regno >= PALETTE_NR)
  840. return -EINVAL;
  841. /* only FB_VISUAL_TRUECOLOR supported */
  842. red >>= 16 - info->var.red.length;
  843. green >>= 16 - info->var.green.length;
  844. blue >>= 16 - info->var.blue.length;
  845. transp >>= 16 - info->var.transp.length;
  846. palette[regno] = (red << info->var.red.offset) |
  847. (green << info->var.green.offset) |
  848. (blue << info->var.blue.offset) |
  849. (transp << info->var.transp.offset);
  850. return 0;
  851. }
  852. static struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  853. .id = "SH Mobile LCDC",
  854. .type = FB_TYPE_PACKED_PIXELS,
  855. .visual = FB_VISUAL_TRUECOLOR,
  856. .accel = FB_ACCEL_NONE,
  857. .xpanstep = 0,
  858. .ypanstep = 1,
  859. .ywrapstep = 0,
  860. .capabilities = FB_CAP_FOURCC,
  861. };
  862. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  863. const struct fb_fillrect *rect)
  864. {
  865. sys_fillrect(info, rect);
  866. sh_mobile_lcdc_deferred_io_touch(info);
  867. }
  868. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  869. const struct fb_copyarea *area)
  870. {
  871. sys_copyarea(info, area);
  872. sh_mobile_lcdc_deferred_io_touch(info);
  873. }
  874. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  875. const struct fb_image *image)
  876. {
  877. sys_imageblit(info, image);
  878. sh_mobile_lcdc_deferred_io_touch(info);
  879. }
  880. static int sh_mobile_fb_pan_display(struct fb_var_screeninfo *var,
  881. struct fb_info *info)
  882. {
  883. struct sh_mobile_lcdc_chan *ch = info->par;
  884. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  885. unsigned long ldrcntr;
  886. unsigned long new_pan_offset;
  887. unsigned long base_addr_y, base_addr_c;
  888. unsigned long c_offset;
  889. if (!ch->format->yuv)
  890. new_pan_offset = var->yoffset * ch->pitch
  891. + var->xoffset * (ch->format->bpp / 8);
  892. else
  893. new_pan_offset = var->yoffset * ch->pitch + var->xoffset;
  894. if (new_pan_offset == ch->pan_offset)
  895. return 0; /* No change, do nothing */
  896. ldrcntr = lcdc_read(priv, _LDRCNTR);
  897. /* Set the source address for the next refresh */
  898. base_addr_y = ch->dma_handle + new_pan_offset;
  899. if (ch->format->yuv) {
  900. /* Set y offset */
  901. c_offset = var->yoffset * ch->pitch
  902. * (ch->format->bpp - 8) / 8;
  903. base_addr_c = ch->dma_handle + ch->xres * ch->yres_virtual
  904. + c_offset;
  905. /* Set x offset */
  906. if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
  907. base_addr_c += 2 * var->xoffset;
  908. else
  909. base_addr_c += var->xoffset;
  910. }
  911. if (ch->meram) {
  912. struct sh_mobile_meram_info *mdev;
  913. mdev = priv->meram_dev;
  914. mdev->ops->meram_update(mdev, ch->meram,
  915. base_addr_y, base_addr_c,
  916. &base_addr_y, &base_addr_c);
  917. }
  918. ch->base_addr_y = base_addr_y;
  919. ch->base_addr_c = base_addr_c;
  920. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  921. if (ch->format->yuv)
  922. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  923. if (lcdc_chan_is_sublcd(ch))
  924. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  925. else
  926. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  927. ch->pan_offset = new_pan_offset;
  928. sh_mobile_lcdc_deferred_io_touch(info);
  929. return 0;
  930. }
  931. static int sh_mobile_ioctl(struct fb_info *info, unsigned int cmd,
  932. unsigned long arg)
  933. {
  934. int retval;
  935. switch (cmd) {
  936. case FBIO_WAITFORVSYNC:
  937. retval = sh_mobile_wait_for_vsync(info->par);
  938. break;
  939. default:
  940. retval = -ENOIOCTLCMD;
  941. break;
  942. }
  943. return retval;
  944. }
  945. static void sh_mobile_fb_reconfig(struct fb_info *info)
  946. {
  947. struct sh_mobile_lcdc_chan *ch = info->par;
  948. struct fb_var_screeninfo var;
  949. struct fb_videomode mode;
  950. struct fb_event event;
  951. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  952. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  953. /* More framebuffer users are active */
  954. return;
  955. fb_var_to_videomode(&mode, &info->var);
  956. if (fb_mode_is_equal(&ch->display.mode, &mode))
  957. return;
  958. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  959. var = info->var;
  960. fb_videomode_to_var(&var, &ch->display.mode);
  961. var.width = ch->display.width;
  962. var.height = ch->display.height;
  963. var.activate = FB_ACTIVATE_NOW;
  964. if (fb_set_var(info, &var) < 0)
  965. /* Couldn't reconfigure, hopefully, can continue as before */
  966. return;
  967. /*
  968. * fb_set_var() calls the notifier change internally, only if
  969. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  970. * user event, we have to call the chain ourselves.
  971. */
  972. event.info = info;
  973. event.data = &ch->display.mode;
  974. fb_notifier_call_chain(evnt, &event);
  975. }
  976. /*
  977. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  978. * user == 1, or with console sem held, if user == 0.
  979. */
  980. static int sh_mobile_release(struct fb_info *info, int user)
  981. {
  982. struct sh_mobile_lcdc_chan *ch = info->par;
  983. mutex_lock(&ch->open_lock);
  984. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  985. ch->use_count--;
  986. /* Nothing to reconfigure, when called from fbcon */
  987. if (user) {
  988. console_lock();
  989. sh_mobile_fb_reconfig(info);
  990. console_unlock();
  991. }
  992. mutex_unlock(&ch->open_lock);
  993. return 0;
  994. }
  995. static int sh_mobile_open(struct fb_info *info, int user)
  996. {
  997. struct sh_mobile_lcdc_chan *ch = info->par;
  998. mutex_lock(&ch->open_lock);
  999. ch->use_count++;
  1000. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1001. mutex_unlock(&ch->open_lock);
  1002. return 0;
  1003. }
  1004. static int sh_mobile_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  1005. {
  1006. struct sh_mobile_lcdc_chan *ch = info->par;
  1007. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1008. unsigned int best_dist = (unsigned int)-1;
  1009. unsigned int best_xres = 0;
  1010. unsigned int best_yres = 0;
  1011. unsigned int i;
  1012. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  1013. return -EINVAL;
  1014. /* If board code provides us with a list of available modes, make sure
  1015. * we use one of them. Find the mode closest to the requested one. The
  1016. * distance between two modes is defined as the size of the
  1017. * non-overlapping parts of the two rectangles.
  1018. */
  1019. for (i = 0; i < ch->cfg->num_modes; ++i) {
  1020. const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
  1021. unsigned int dist;
  1022. /* We can only round up. */
  1023. if (var->xres > mode->xres || var->yres > mode->yres)
  1024. continue;
  1025. dist = var->xres * var->yres + mode->xres * mode->yres
  1026. - 2 * min(var->xres, mode->xres)
  1027. * min(var->yres, mode->yres);
  1028. if (dist < best_dist) {
  1029. best_xres = mode->xres;
  1030. best_yres = mode->yres;
  1031. best_dist = dist;
  1032. }
  1033. }
  1034. /* If no available mode can be used, return an error. */
  1035. if (ch->cfg->num_modes != 0) {
  1036. if (best_dist == (unsigned int)-1)
  1037. return -EINVAL;
  1038. var->xres = best_xres;
  1039. var->yres = best_yres;
  1040. }
  1041. /* Make sure the virtual resolution is at least as big as the visible
  1042. * resolution.
  1043. */
  1044. if (var->xres_virtual < var->xres)
  1045. var->xres_virtual = var->xres;
  1046. if (var->yres_virtual < var->yres)
  1047. var->yres_virtual = var->yres;
  1048. if (sh_mobile_format_is_fourcc(var)) {
  1049. const struct sh_mobile_lcdc_format_info *format;
  1050. format = sh_mobile_format_info(var->grayscale);
  1051. if (format == NULL)
  1052. return -EINVAL;
  1053. var->bits_per_pixel = format->bpp;
  1054. /* Default to RGB and JPEG color-spaces for RGB and YUV formats
  1055. * respectively.
  1056. */
  1057. if (!format->yuv)
  1058. var->colorspace = V4L2_COLORSPACE_SRGB;
  1059. else if (var->colorspace != V4L2_COLORSPACE_REC709)
  1060. var->colorspace = V4L2_COLORSPACE_JPEG;
  1061. } else {
  1062. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  1063. var->bits_per_pixel = 16;
  1064. var->red.offset = 11;
  1065. var->red.length = 5;
  1066. var->green.offset = 5;
  1067. var->green.length = 6;
  1068. var->blue.offset = 0;
  1069. var->blue.length = 5;
  1070. var->transp.offset = 0;
  1071. var->transp.length = 0;
  1072. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  1073. var->bits_per_pixel = 24;
  1074. var->red.offset = 16;
  1075. var->red.length = 8;
  1076. var->green.offset = 8;
  1077. var->green.length = 8;
  1078. var->blue.offset = 0;
  1079. var->blue.length = 8;
  1080. var->transp.offset = 0;
  1081. var->transp.length = 0;
  1082. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  1083. var->bits_per_pixel = 32;
  1084. var->red.offset = 16;
  1085. var->red.length = 8;
  1086. var->green.offset = 8;
  1087. var->green.length = 8;
  1088. var->blue.offset = 0;
  1089. var->blue.length = 8;
  1090. var->transp.offset = 24;
  1091. var->transp.length = 8;
  1092. } else
  1093. return -EINVAL;
  1094. var->red.msb_right = 0;
  1095. var->green.msb_right = 0;
  1096. var->blue.msb_right = 0;
  1097. var->transp.msb_right = 0;
  1098. }
  1099. /* Make sure we don't exceed our allocated memory. */
  1100. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  1101. info->fix.smem_len)
  1102. return -EINVAL;
  1103. /* only accept the forced_fourcc for dual channel configurations */
  1104. if (p->forced_fourcc &&
  1105. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1106. return -EINVAL;
  1107. return 0;
  1108. }
  1109. static int sh_mobile_set_par(struct fb_info *info)
  1110. {
  1111. struct sh_mobile_lcdc_chan *ch = info->par;
  1112. int ret;
  1113. sh_mobile_lcdc_stop(ch->lcdc);
  1114. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1115. ch->colorspace = info->var.colorspace;
  1116. ch->xres = info->var.xres;
  1117. ch->xres_virtual = info->var.xres_virtual;
  1118. ch->yres = info->var.yres;
  1119. ch->yres_virtual = info->var.yres_virtual;
  1120. if (ch->format->yuv)
  1121. ch->pitch = info->var.xres;
  1122. else
  1123. ch->pitch = info->var.xres * ch->format->bpp / 8;
  1124. ret = sh_mobile_lcdc_start(ch->lcdc);
  1125. if (ret < 0)
  1126. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1127. info->fix.line_length = ch->pitch;
  1128. if (sh_mobile_format_is_fourcc(&info->var)) {
  1129. info->fix.type = FB_TYPE_FOURCC;
  1130. info->fix.visual = FB_VISUAL_FOURCC;
  1131. } else {
  1132. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1133. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1134. }
  1135. return ret;
  1136. }
  1137. /*
  1138. * Screen blanking. Behavior is as follows:
  1139. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1140. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1141. * FB_BLANK_VSYNC,
  1142. * FB_BLANK_HSYNC,
  1143. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1144. */
  1145. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1146. {
  1147. struct sh_mobile_lcdc_chan *ch = info->par;
  1148. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1149. /* blank the screen? */
  1150. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1151. struct fb_fillrect rect = {
  1152. .width = ch->xres,
  1153. .height = ch->yres,
  1154. };
  1155. sh_mobile_lcdc_fillrect(info, &rect);
  1156. }
  1157. /* turn clocks on? */
  1158. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1159. sh_mobile_lcdc_clk_on(p);
  1160. }
  1161. /* turn clocks off? */
  1162. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1163. /* make sure the screen is updated with the black fill before
  1164. * switching the clocks off. one vsync is not enough since
  1165. * blanking may occur in the middle of a refresh. deferred io
  1166. * mode will reenable the clocks and update the screen in time,
  1167. * so it does not need this. */
  1168. if (!info->fbdefio) {
  1169. sh_mobile_wait_for_vsync(ch);
  1170. sh_mobile_wait_for_vsync(ch);
  1171. }
  1172. sh_mobile_lcdc_clk_off(p);
  1173. }
  1174. ch->blank_status = blank;
  1175. return 0;
  1176. }
  1177. static struct fb_ops sh_mobile_lcdc_ops = {
  1178. .owner = THIS_MODULE,
  1179. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1180. .fb_read = fb_sys_read,
  1181. .fb_write = fb_sys_write,
  1182. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1183. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1184. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1185. .fb_blank = sh_mobile_lcdc_blank,
  1186. .fb_pan_display = sh_mobile_fb_pan_display,
  1187. .fb_ioctl = sh_mobile_ioctl,
  1188. .fb_open = sh_mobile_open,
  1189. .fb_release = sh_mobile_release,
  1190. .fb_check_var = sh_mobile_check_var,
  1191. .fb_set_par = sh_mobile_set_par,
  1192. };
  1193. static void
  1194. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1195. {
  1196. if (ch->info && ch->info->dev)
  1197. unregister_framebuffer(ch->info);
  1198. }
  1199. static int __devinit
  1200. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1201. {
  1202. struct fb_info *info = ch->info;
  1203. int ret;
  1204. if (info->fbdefio) {
  1205. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1206. ch->fb_size >> PAGE_SHIFT);
  1207. if (!ch->sglist) {
  1208. dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
  1209. return -ENOMEM;
  1210. }
  1211. }
  1212. info->bl_dev = ch->bl;
  1213. ret = register_framebuffer(info);
  1214. if (ret < 0)
  1215. return ret;
  1216. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1217. dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
  1218. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1219. info->var.bits_per_pixel);
  1220. /* deferred io mode: disable clock to save power */
  1221. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1222. sh_mobile_lcdc_clk_off(ch->lcdc);
  1223. return ret;
  1224. }
  1225. static void
  1226. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1227. {
  1228. struct fb_info *info = ch->info;
  1229. if (!info || !info->device)
  1230. return;
  1231. if (ch->sglist)
  1232. vfree(ch->sglist);
  1233. fb_dealloc_cmap(&info->cmap);
  1234. framebuffer_release(info);
  1235. }
  1236. static int __devinit
  1237. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1238. const struct fb_videomode *mode,
  1239. unsigned int num_modes)
  1240. {
  1241. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1242. struct fb_var_screeninfo *var;
  1243. struct fb_info *info;
  1244. int ret;
  1245. /* Allocate and initialize the frame buffer device. Create the modes
  1246. * list and allocate the color map.
  1247. */
  1248. info = framebuffer_alloc(0, priv->dev);
  1249. if (info == NULL) {
  1250. dev_err(priv->dev, "unable to allocate fb_info\n");
  1251. return -ENOMEM;
  1252. }
  1253. ch->info = info;
  1254. info->flags = FBINFO_FLAG_DEFAULT;
  1255. info->fbops = &sh_mobile_lcdc_ops;
  1256. info->device = priv->dev;
  1257. info->screen_base = ch->fb_mem;
  1258. info->pseudo_palette = &ch->pseudo_palette;
  1259. info->par = ch;
  1260. fb_videomode_to_modelist(mode, num_modes, &info->modelist);
  1261. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1262. if (ret < 0) {
  1263. dev_err(priv->dev, "unable to allocate cmap\n");
  1264. return ret;
  1265. }
  1266. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1267. * for NV12 and NV21.
  1268. */
  1269. info->fix = sh_mobile_lcdc_fix;
  1270. info->fix.smem_start = ch->dma_handle;
  1271. info->fix.smem_len = ch->fb_size;
  1272. info->fix.line_length = ch->pitch;
  1273. if (ch->format->yuv)
  1274. info->fix.visual = FB_VISUAL_FOURCC;
  1275. else
  1276. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1277. if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
  1278. ch->format->fourcc == V4L2_PIX_FMT_NV21)
  1279. info->fix.ypanstep = 2;
  1280. /* Initialize variable screen information using the first mode as
  1281. * default. The default Y virtual resolution is twice the panel size to
  1282. * allow for double-buffering.
  1283. */
  1284. var = &info->var;
  1285. fb_videomode_to_var(var, mode);
  1286. var->width = ch->cfg->panel_cfg.width;
  1287. var->height = ch->cfg->panel_cfg.height;
  1288. var->yres_virtual = var->yres * 2;
  1289. var->activate = FB_ACTIVATE_NOW;
  1290. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1291. * for YUV formats.
  1292. */
  1293. if (!ch->format->yuv)
  1294. var->bits_per_pixel = ch->format->bpp;
  1295. else
  1296. var->grayscale = ch->format->fourcc;
  1297. ret = sh_mobile_check_var(var, info);
  1298. if (ret)
  1299. return ret;
  1300. return 0;
  1301. }
  1302. /* -----------------------------------------------------------------------------
  1303. * Backlight
  1304. */
  1305. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1306. {
  1307. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1308. int brightness = bdev->props.brightness;
  1309. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1310. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1311. brightness = 0;
  1312. return ch->cfg->bl_info.set_brightness(brightness);
  1313. }
  1314. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1315. {
  1316. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1317. return ch->cfg->bl_info.get_brightness();
  1318. }
  1319. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1320. struct fb_info *info)
  1321. {
  1322. return (info->bl_dev == bdev);
  1323. }
  1324. static struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1325. .options = BL_CORE_SUSPENDRESUME,
  1326. .update_status = sh_mobile_lcdc_update_bl,
  1327. .get_brightness = sh_mobile_lcdc_get_brightness,
  1328. .check_fb = sh_mobile_lcdc_check_fb,
  1329. };
  1330. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1331. struct sh_mobile_lcdc_chan *ch)
  1332. {
  1333. struct backlight_device *bl;
  1334. bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
  1335. &sh_mobile_lcdc_bl_ops, NULL);
  1336. if (IS_ERR(bl)) {
  1337. dev_err(parent, "unable to register backlight device: %ld\n",
  1338. PTR_ERR(bl));
  1339. return NULL;
  1340. }
  1341. bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
  1342. bl->props.brightness = bl->props.max_brightness;
  1343. backlight_update_status(bl);
  1344. return bl;
  1345. }
  1346. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1347. {
  1348. backlight_device_unregister(bdev);
  1349. }
  1350. /* -----------------------------------------------------------------------------
  1351. * Power management
  1352. */
  1353. static int sh_mobile_lcdc_suspend(struct device *dev)
  1354. {
  1355. struct platform_device *pdev = to_platform_device(dev);
  1356. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1357. return 0;
  1358. }
  1359. static int sh_mobile_lcdc_resume(struct device *dev)
  1360. {
  1361. struct platform_device *pdev = to_platform_device(dev);
  1362. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1363. }
  1364. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1365. {
  1366. struct platform_device *pdev = to_platform_device(dev);
  1367. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1368. /* turn off LCDC hardware */
  1369. lcdc_write(priv, _LDCNT1R, 0);
  1370. return 0;
  1371. }
  1372. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1373. {
  1374. struct platform_device *pdev = to_platform_device(dev);
  1375. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1376. __sh_mobile_lcdc_start(priv);
  1377. return 0;
  1378. }
  1379. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1380. .suspend = sh_mobile_lcdc_suspend,
  1381. .resume = sh_mobile_lcdc_resume,
  1382. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1383. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1384. };
  1385. /* -----------------------------------------------------------------------------
  1386. * Framebuffer notifier
  1387. */
  1388. /* locking: called with info->lock held */
  1389. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1390. unsigned long action, void *data)
  1391. {
  1392. struct fb_event *event = data;
  1393. struct fb_info *info = event->info;
  1394. struct sh_mobile_lcdc_chan *ch = info->par;
  1395. if (&ch->lcdc->notifier != nb)
  1396. return NOTIFY_DONE;
  1397. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1398. __func__, action, event->data);
  1399. switch(action) {
  1400. case FB_EVENT_SUSPEND:
  1401. sh_mobile_lcdc_display_off(ch);
  1402. sh_mobile_lcdc_stop(ch->lcdc);
  1403. break;
  1404. case FB_EVENT_RESUME:
  1405. mutex_lock(&ch->open_lock);
  1406. sh_mobile_fb_reconfig(info);
  1407. mutex_unlock(&ch->open_lock);
  1408. sh_mobile_lcdc_display_on(ch);
  1409. sh_mobile_lcdc_start(ch->lcdc);
  1410. }
  1411. return NOTIFY_OK;
  1412. }
  1413. /* -----------------------------------------------------------------------------
  1414. * Probe/remove and driver init/exit
  1415. */
  1416. static const struct fb_videomode default_720p __devinitconst = {
  1417. .name = "HDMI 720p",
  1418. .xres = 1280,
  1419. .yres = 720,
  1420. .left_margin = 220,
  1421. .right_margin = 110,
  1422. .hsync_len = 40,
  1423. .upper_margin = 20,
  1424. .lower_margin = 5,
  1425. .vsync_len = 5,
  1426. .pixclock = 13468,
  1427. .refresh = 60,
  1428. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  1429. };
  1430. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  1431. {
  1432. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1433. int i;
  1434. fb_unregister_client(&priv->notifier);
  1435. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1436. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  1437. sh_mobile_lcdc_stop(priv);
  1438. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1439. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  1440. if (ch->tx_dev) {
  1441. ch->tx_dev->lcdc = NULL;
  1442. module_put(ch->cfg->tx_dev->dev.driver->owner);
  1443. }
  1444. sh_mobile_lcdc_channel_fb_cleanup(ch);
  1445. if (ch->fb_mem)
  1446. dma_free_coherent(&pdev->dev, ch->fb_size,
  1447. ch->fb_mem, ch->dma_handle);
  1448. }
  1449. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1450. if (priv->ch[i].bl)
  1451. sh_mobile_lcdc_bl_remove(priv->ch[i].bl);
  1452. }
  1453. if (priv->dot_clk) {
  1454. pm_runtime_disable(&pdev->dev);
  1455. clk_put(priv->dot_clk);
  1456. }
  1457. if (priv->base)
  1458. iounmap(priv->base);
  1459. if (priv->irq)
  1460. free_irq(priv->irq, priv);
  1461. kfree(priv);
  1462. return 0;
  1463. }
  1464. static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  1465. {
  1466. int interface_type = ch->cfg->interface_type;
  1467. switch (interface_type) {
  1468. case RGB8:
  1469. case RGB9:
  1470. case RGB12A:
  1471. case RGB12B:
  1472. case RGB16:
  1473. case RGB18:
  1474. case RGB24:
  1475. case SYS8A:
  1476. case SYS8B:
  1477. case SYS8C:
  1478. case SYS8D:
  1479. case SYS9:
  1480. case SYS12:
  1481. case SYS16A:
  1482. case SYS16B:
  1483. case SYS16C:
  1484. case SYS18:
  1485. case SYS24:
  1486. break;
  1487. default:
  1488. return -EINVAL;
  1489. }
  1490. /* SUBLCD only supports SYS interface */
  1491. if (lcdc_chan_is_sublcd(ch)) {
  1492. if (!(interface_type & LDMT1R_IFM))
  1493. return -EINVAL;
  1494. interface_type &= ~LDMT1R_IFM;
  1495. }
  1496. ch->ldmt1r_value = interface_type;
  1497. return 0;
  1498. }
  1499. static int __devinit
  1500. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
  1501. struct sh_mobile_lcdc_chan *ch)
  1502. {
  1503. const struct sh_mobile_lcdc_format_info *format;
  1504. const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
  1505. const struct fb_videomode *max_mode;
  1506. const struct fb_videomode *mode;
  1507. unsigned int num_modes;
  1508. unsigned int max_size;
  1509. unsigned int i;
  1510. mutex_init(&ch->open_lock);
  1511. ch->notify = sh_mobile_lcdc_display_notify;
  1512. /* Validate the format. */
  1513. format = sh_mobile_format_info(cfg->fourcc);
  1514. if (format == NULL) {
  1515. dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  1516. return -EINVAL;
  1517. }
  1518. /* Iterate through the modes to validate them and find the highest
  1519. * resolution.
  1520. */
  1521. max_mode = NULL;
  1522. max_size = 0;
  1523. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  1524. unsigned int size = mode->yres * mode->xres;
  1525. /* NV12/NV21 buffers must have even number of lines */
  1526. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  1527. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  1528. dev_err(priv->dev, "yres must be multiple of 2 for "
  1529. "YCbCr420 mode.\n");
  1530. return -EINVAL;
  1531. }
  1532. if (size > max_size) {
  1533. max_mode = mode;
  1534. max_size = size;
  1535. }
  1536. }
  1537. if (!max_size)
  1538. max_size = MAX_XRES * MAX_YRES;
  1539. else
  1540. dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
  1541. max_mode->xres, max_mode->yres);
  1542. if (cfg->lcd_modes == NULL) {
  1543. mode = &default_720p;
  1544. num_modes = 1;
  1545. } else {
  1546. mode = cfg->lcd_modes;
  1547. num_modes = cfg->num_modes;
  1548. }
  1549. /* Use the first mode as default. */
  1550. ch->format = format;
  1551. ch->xres = mode->xres;
  1552. ch->xres_virtual = mode->xres;
  1553. ch->yres = mode->yres;
  1554. ch->yres_virtual = mode->yres * 2;
  1555. if (!format->yuv) {
  1556. ch->colorspace = V4L2_COLORSPACE_SRGB;
  1557. ch->pitch = ch->xres * format->bpp / 8;
  1558. } else {
  1559. ch->colorspace = V4L2_COLORSPACE_REC709;
  1560. ch->pitch = ch->xres;
  1561. }
  1562. ch->display.width = cfg->panel_cfg.width;
  1563. ch->display.height = cfg->panel_cfg.height;
  1564. ch->display.mode = *mode;
  1565. /* Allocate frame buffer memory. */
  1566. ch->fb_size = max_size * format->bpp / 8 * 2;
  1567. ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
  1568. GFP_KERNEL);
  1569. if (ch->fb_mem == NULL) {
  1570. dev_err(priv->dev, "unable to allocate buffer\n");
  1571. return -ENOMEM;
  1572. }
  1573. /* Initialize the transmitter device if present. */
  1574. if (cfg->tx_dev) {
  1575. if (!cfg->tx_dev->dev.driver ||
  1576. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  1577. dev_warn(priv->dev,
  1578. "unable to get transmitter device\n");
  1579. return -EINVAL;
  1580. }
  1581. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  1582. ch->tx_dev->lcdc = ch;
  1583. ch->tx_dev->def_mode = *mode;
  1584. }
  1585. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  1586. }
  1587. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  1588. {
  1589. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  1590. struct sh_mobile_lcdc_priv *priv;
  1591. struct resource *res;
  1592. int num_channels;
  1593. int error;
  1594. int i;
  1595. if (!pdata) {
  1596. dev_err(&pdev->dev, "no platform data defined\n");
  1597. return -EINVAL;
  1598. }
  1599. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1600. i = platform_get_irq(pdev, 0);
  1601. if (!res || i < 0) {
  1602. dev_err(&pdev->dev, "cannot get platform resources\n");
  1603. return -ENOENT;
  1604. }
  1605. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1606. if (!priv) {
  1607. dev_err(&pdev->dev, "cannot allocate device data\n");
  1608. return -ENOMEM;
  1609. }
  1610. priv->dev = &pdev->dev;
  1611. priv->meram_dev = pdata->meram_dev;
  1612. platform_set_drvdata(pdev, priv);
  1613. error = request_irq(i, sh_mobile_lcdc_irq, 0,
  1614. dev_name(&pdev->dev), priv);
  1615. if (error) {
  1616. dev_err(&pdev->dev, "unable to request irq\n");
  1617. goto err1;
  1618. }
  1619. priv->irq = i;
  1620. atomic_set(&priv->hw_usecnt, -1);
  1621. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  1622. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  1623. ch->lcdc = priv;
  1624. ch->cfg = &pdata->ch[i];
  1625. error = sh_mobile_lcdc_check_interface(ch);
  1626. if (error) {
  1627. dev_err(&pdev->dev, "unsupported interface type\n");
  1628. goto err1;
  1629. }
  1630. init_waitqueue_head(&ch->frame_end_wait);
  1631. init_completion(&ch->vsync_completion);
  1632. ch->pan_offset = 0;
  1633. /* probe the backlight is there is one defined */
  1634. if (ch->cfg->bl_info.max_brightness)
  1635. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  1636. switch (pdata->ch[i].chan) {
  1637. case LCDC_CHAN_MAINLCD:
  1638. ch->enabled = LDCNT2R_ME;
  1639. ch->reg_offs = lcdc_offs_mainlcd;
  1640. num_channels++;
  1641. break;
  1642. case LCDC_CHAN_SUBLCD:
  1643. ch->enabled = LDCNT2R_SE;
  1644. ch->reg_offs = lcdc_offs_sublcd;
  1645. num_channels++;
  1646. break;
  1647. }
  1648. }
  1649. if (!num_channels) {
  1650. dev_err(&pdev->dev, "no channels defined\n");
  1651. error = -EINVAL;
  1652. goto err1;
  1653. }
  1654. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  1655. if (num_channels == 2)
  1656. priv->forced_fourcc = pdata->ch[0].fourcc;
  1657. priv->base = ioremap_nocache(res->start, resource_size(res));
  1658. if (!priv->base)
  1659. goto err1;
  1660. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  1661. if (error) {
  1662. dev_err(&pdev->dev, "unable to setup clocks\n");
  1663. goto err1;
  1664. }
  1665. /* Enable runtime PM. */
  1666. pm_runtime_enable(&pdev->dev);
  1667. for (i = 0; i < num_channels; i++) {
  1668. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1669. error = sh_mobile_lcdc_channel_init(priv, ch);
  1670. if (error)
  1671. goto err1;
  1672. }
  1673. error = sh_mobile_lcdc_start(priv);
  1674. if (error) {
  1675. dev_err(&pdev->dev, "unable to start hardware\n");
  1676. goto err1;
  1677. }
  1678. for (i = 0; i < num_channels; i++) {
  1679. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  1680. error = sh_mobile_lcdc_channel_fb_register(ch);
  1681. if (error)
  1682. goto err1;
  1683. }
  1684. /* Failure ignored */
  1685. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  1686. fb_register_client(&priv->notifier);
  1687. return 0;
  1688. err1:
  1689. sh_mobile_lcdc_remove(pdev);
  1690. return error;
  1691. }
  1692. static struct platform_driver sh_mobile_lcdc_driver = {
  1693. .driver = {
  1694. .name = "sh_mobile_lcdc_fb",
  1695. .owner = THIS_MODULE,
  1696. .pm = &sh_mobile_lcdc_dev_pm_ops,
  1697. },
  1698. .probe = sh_mobile_lcdc_probe,
  1699. .remove = sh_mobile_lcdc_remove,
  1700. };
  1701. module_platform_driver(sh_mobile_lcdc_driver);
  1702. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  1703. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  1704. MODULE_LICENSE("GPL v2");