sh_mobile_lcdcfb.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840
  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/ctype.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/slab.h>
  27. #include <linux/videodev2.h>
  28. #include <linux/vmalloc.h>
  29. #include <video/sh_mobile_lcdc.h>
  30. #include <video/sh_mobile_meram.h>
  31. #include "sh_mobile_lcdcfb.h"
  32. /* ----------------------------------------------------------------------------
  33. * Overlay register definitions
  34. */
  35. #define LDBCR 0xb00
  36. #define LDBCR_UPC(n) (1 << ((n) + 16))
  37. #define LDBCR_UPF(n) (1 << ((n) + 8))
  38. #define LDBCR_UPD(n) (1 << ((n) + 0))
  39. #define LDBnBSIFR(n) (0xb20 + (n) * 0x20 + 0x00)
  40. #define LDBBSIFR_EN (1 << 31)
  41. #define LDBBSIFR_VS (1 << 29)
  42. #define LDBBSIFR_BRSEL (1 << 28)
  43. #define LDBBSIFR_MX (1 << 27)
  44. #define LDBBSIFR_MY (1 << 26)
  45. #define LDBBSIFR_CV3 (3 << 24)
  46. #define LDBBSIFR_CV2 (2 << 24)
  47. #define LDBBSIFR_CV1 (1 << 24)
  48. #define LDBBSIFR_CV0 (0 << 24)
  49. #define LDBBSIFR_CV_MASK (3 << 24)
  50. #define LDBBSIFR_LAY_MASK (0xff << 16)
  51. #define LDBBSIFR_LAY_SHIFT 16
  52. #define LDBBSIFR_ROP3_MASK (0xff << 16)
  53. #define LDBBSIFR_ROP3_SHIFT 16
  54. #define LDBBSIFR_AL_PL8 (3 << 14)
  55. #define LDBBSIFR_AL_PL1 (2 << 14)
  56. #define LDBBSIFR_AL_PK (1 << 14)
  57. #define LDBBSIFR_AL_1 (0 << 14)
  58. #define LDBBSIFR_AL_MASK (3 << 14)
  59. #define LDBBSIFR_SWPL (1 << 10)
  60. #define LDBBSIFR_SWPW (1 << 9)
  61. #define LDBBSIFR_SWPB (1 << 8)
  62. #define LDBBSIFR_RY (1 << 7)
  63. #define LDBBSIFR_CHRR_420 (2 << 0)
  64. #define LDBBSIFR_CHRR_422 (1 << 0)
  65. #define LDBBSIFR_CHRR_444 (0 << 0)
  66. #define LDBBSIFR_RPKF_ARGB32 (0x00 << 0)
  67. #define LDBBSIFR_RPKF_RGB16 (0x03 << 0)
  68. #define LDBBSIFR_RPKF_RGB24 (0x0b << 0)
  69. #define LDBBSIFR_RPKF_MASK (0x1f << 0)
  70. #define LDBnBSSZR(n) (0xb20 + (n) * 0x20 + 0x04)
  71. #define LDBBSSZR_BVSS_MASK (0xfff << 16)
  72. #define LDBBSSZR_BVSS_SHIFT 16
  73. #define LDBBSSZR_BHSS_MASK (0xfff << 0)
  74. #define LDBBSSZR_BHSS_SHIFT 0
  75. #define LDBnBLOCR(n) (0xb20 + (n) * 0x20 + 0x08)
  76. #define LDBBLOCR_CVLC_MASK (0xfff << 16)
  77. #define LDBBLOCR_CVLC_SHIFT 16
  78. #define LDBBLOCR_CHLC_MASK (0xfff << 0)
  79. #define LDBBLOCR_CHLC_SHIFT 0
  80. #define LDBnBSMWR(n) (0xb20 + (n) * 0x20 + 0x0c)
  81. #define LDBBSMWR_BSMWA_MASK (0xffff << 16)
  82. #define LDBBSMWR_BSMWA_SHIFT 16
  83. #define LDBBSMWR_BSMW_MASK (0xffff << 0)
  84. #define LDBBSMWR_BSMW_SHIFT 0
  85. #define LDBnBSAYR(n) (0xb20 + (n) * 0x20 + 0x10)
  86. #define LDBBSAYR_FG1A_MASK (0xff << 24)
  87. #define LDBBSAYR_FG1A_SHIFT 24
  88. #define LDBBSAYR_FG1R_MASK (0xff << 16)
  89. #define LDBBSAYR_FG1R_SHIFT 16
  90. #define LDBBSAYR_FG1G_MASK (0xff << 8)
  91. #define LDBBSAYR_FG1G_SHIFT 8
  92. #define LDBBSAYR_FG1B_MASK (0xff << 0)
  93. #define LDBBSAYR_FG1B_SHIFT 0
  94. #define LDBnBSACR(n) (0xb20 + (n) * 0x20 + 0x14)
  95. #define LDBBSACR_FG2A_MASK (0xff << 24)
  96. #define LDBBSACR_FG2A_SHIFT 24
  97. #define LDBBSACR_FG2R_MASK (0xff << 16)
  98. #define LDBBSACR_FG2R_SHIFT 16
  99. #define LDBBSACR_FG2G_MASK (0xff << 8)
  100. #define LDBBSACR_FG2G_SHIFT 8
  101. #define LDBBSACR_FG2B_MASK (0xff << 0)
  102. #define LDBBSACR_FG2B_SHIFT 0
  103. #define LDBnBSAAR(n) (0xb20 + (n) * 0x20 + 0x18)
  104. #define LDBBSAAR_AP_MASK (0xff << 24)
  105. #define LDBBSAAR_AP_SHIFT 24
  106. #define LDBBSAAR_R_MASK (0xff << 16)
  107. #define LDBBSAAR_R_SHIFT 16
  108. #define LDBBSAAR_GY_MASK (0xff << 8)
  109. #define LDBBSAAR_GY_SHIFT 8
  110. #define LDBBSAAR_B_MASK (0xff << 0)
  111. #define LDBBSAAR_B_SHIFT 0
  112. #define LDBnBPPCR(n) (0xb20 + (n) * 0x20 + 0x1c)
  113. #define LDBBPPCR_AP_MASK (0xff << 24)
  114. #define LDBBPPCR_AP_SHIFT 24
  115. #define LDBBPPCR_R_MASK (0xff << 16)
  116. #define LDBBPPCR_R_SHIFT 16
  117. #define LDBBPPCR_GY_MASK (0xff << 8)
  118. #define LDBBPPCR_GY_SHIFT 8
  119. #define LDBBPPCR_B_MASK (0xff << 0)
  120. #define LDBBPPCR_B_SHIFT 0
  121. #define LDBnBBGCL(n) (0xb10 + (n) * 0x04)
  122. #define LDBBBGCL_BGA_MASK (0xff << 24)
  123. #define LDBBBGCL_BGA_SHIFT 24
  124. #define LDBBBGCL_BGR_MASK (0xff << 16)
  125. #define LDBBBGCL_BGR_SHIFT 16
  126. #define LDBBBGCL_BGG_MASK (0xff << 8)
  127. #define LDBBBGCL_BGG_SHIFT 8
  128. #define LDBBBGCL_BGB_MASK (0xff << 0)
  129. #define LDBBBGCL_BGB_SHIFT 0
  130. #define SIDE_B_OFFSET 0x1000
  131. #define MIRROR_OFFSET 0x2000
  132. #define MAX_XRES 1920
  133. #define MAX_YRES 1080
  134. enum sh_mobile_lcdc_overlay_mode {
  135. LCDC_OVERLAY_BLEND,
  136. LCDC_OVERLAY_ROP3,
  137. };
  138. /*
  139. * struct sh_mobile_lcdc_overlay - LCDC display overlay
  140. *
  141. * @channel: LCDC channel this overlay belongs to
  142. * @cfg: Overlay configuration
  143. * @info: Frame buffer device
  144. * @index: Overlay index (0-3)
  145. * @base: Overlay registers base address
  146. * @enabled: True if the overlay is enabled
  147. * @mode: Overlay blending mode (alpha blend or ROP3)
  148. * @alpha: Global alpha blending value (0-255, for alpha blending mode)
  149. * @rop3: Raster operation (for ROP3 mode)
  150. * @fb_mem: Frame buffer virtual memory address
  151. * @fb_size: Frame buffer size in bytes
  152. * @dma_handle: Frame buffer DMA address
  153. * @base_addr_y: Overlay base address (RGB or luma component)
  154. * @base_addr_c: Overlay base address (chroma component)
  155. * @pan_y_offset: Panning linear offset in bytes (luma component)
  156. * @format: Current pixelf format
  157. * @xres: Horizontal visible resolution
  158. * @xres_virtual: Horizontal total resolution
  159. * @yres: Vertical visible resolution
  160. * @yres_virtual: Vertical total resolution
  161. * @pitch: Overlay line pitch
  162. * @pos_x: Horizontal overlay position
  163. * @pos_y: Vertical overlay position
  164. */
  165. struct sh_mobile_lcdc_overlay {
  166. struct sh_mobile_lcdc_chan *channel;
  167. const struct sh_mobile_lcdc_overlay_cfg *cfg;
  168. struct fb_info *info;
  169. unsigned int index;
  170. unsigned long base;
  171. bool enabled;
  172. enum sh_mobile_lcdc_overlay_mode mode;
  173. unsigned int alpha;
  174. unsigned int rop3;
  175. void *fb_mem;
  176. unsigned long fb_size;
  177. dma_addr_t dma_handle;
  178. unsigned long base_addr_y;
  179. unsigned long base_addr_c;
  180. unsigned long pan_y_offset;
  181. const struct sh_mobile_lcdc_format_info *format;
  182. unsigned int xres;
  183. unsigned int xres_virtual;
  184. unsigned int yres;
  185. unsigned int yres_virtual;
  186. unsigned int pitch;
  187. int pos_x;
  188. int pos_y;
  189. };
  190. struct sh_mobile_lcdc_priv {
  191. void __iomem *base;
  192. int irq;
  193. atomic_t hw_usecnt;
  194. struct device *dev;
  195. struct clk *dot_clk;
  196. unsigned long lddckr;
  197. struct sh_mobile_lcdc_chan ch[2];
  198. struct sh_mobile_lcdc_overlay overlays[4];
  199. struct notifier_block notifier;
  200. int started;
  201. int forced_fourcc; /* 2 channel LCDC must share fourcc setting */
  202. struct sh_mobile_meram_info *meram_dev;
  203. };
  204. /* -----------------------------------------------------------------------------
  205. * Registers access
  206. */
  207. static unsigned long lcdc_offs_mainlcd[NR_CH_REGS] = {
  208. [LDDCKPAT1R] = 0x400,
  209. [LDDCKPAT2R] = 0x404,
  210. [LDMT1R] = 0x418,
  211. [LDMT2R] = 0x41c,
  212. [LDMT3R] = 0x420,
  213. [LDDFR] = 0x424,
  214. [LDSM1R] = 0x428,
  215. [LDSM2R] = 0x42c,
  216. [LDSA1R] = 0x430,
  217. [LDSA2R] = 0x434,
  218. [LDMLSR] = 0x438,
  219. [LDHCNR] = 0x448,
  220. [LDHSYNR] = 0x44c,
  221. [LDVLNR] = 0x450,
  222. [LDVSYNR] = 0x454,
  223. [LDPMR] = 0x460,
  224. [LDHAJR] = 0x4a0,
  225. };
  226. static unsigned long lcdc_offs_sublcd[NR_CH_REGS] = {
  227. [LDDCKPAT1R] = 0x408,
  228. [LDDCKPAT2R] = 0x40c,
  229. [LDMT1R] = 0x600,
  230. [LDMT2R] = 0x604,
  231. [LDMT3R] = 0x608,
  232. [LDDFR] = 0x60c,
  233. [LDSM1R] = 0x610,
  234. [LDSM2R] = 0x614,
  235. [LDSA1R] = 0x618,
  236. [LDMLSR] = 0x620,
  237. [LDHCNR] = 0x624,
  238. [LDHSYNR] = 0x628,
  239. [LDVLNR] = 0x62c,
  240. [LDVSYNR] = 0x630,
  241. [LDPMR] = 0x63c,
  242. };
  243. static bool banked(int reg_nr)
  244. {
  245. switch (reg_nr) {
  246. case LDMT1R:
  247. case LDMT2R:
  248. case LDMT3R:
  249. case LDDFR:
  250. case LDSM1R:
  251. case LDSA1R:
  252. case LDSA2R:
  253. case LDMLSR:
  254. case LDHCNR:
  255. case LDHSYNR:
  256. case LDVLNR:
  257. case LDVSYNR:
  258. return true;
  259. }
  260. return false;
  261. }
  262. static int lcdc_chan_is_sublcd(struct sh_mobile_lcdc_chan *chan)
  263. {
  264. return chan->cfg->chan == LCDC_CHAN_SUBLCD;
  265. }
  266. static void lcdc_write_chan(struct sh_mobile_lcdc_chan *chan,
  267. int reg_nr, unsigned long data)
  268. {
  269. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr]);
  270. if (banked(reg_nr))
  271. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  272. SIDE_B_OFFSET);
  273. }
  274. static void lcdc_write_chan_mirror(struct sh_mobile_lcdc_chan *chan,
  275. int reg_nr, unsigned long data)
  276. {
  277. iowrite32(data, chan->lcdc->base + chan->reg_offs[reg_nr] +
  278. MIRROR_OFFSET);
  279. }
  280. static unsigned long lcdc_read_chan(struct sh_mobile_lcdc_chan *chan,
  281. int reg_nr)
  282. {
  283. return ioread32(chan->lcdc->base + chan->reg_offs[reg_nr]);
  284. }
  285. static void lcdc_write_overlay(struct sh_mobile_lcdc_overlay *ovl,
  286. int reg, unsigned long data)
  287. {
  288. iowrite32(data, ovl->channel->lcdc->base + reg);
  289. iowrite32(data, ovl->channel->lcdc->base + reg + SIDE_B_OFFSET);
  290. }
  291. static void lcdc_write(struct sh_mobile_lcdc_priv *priv,
  292. unsigned long reg_offs, unsigned long data)
  293. {
  294. iowrite32(data, priv->base + reg_offs);
  295. }
  296. static unsigned long lcdc_read(struct sh_mobile_lcdc_priv *priv,
  297. unsigned long reg_offs)
  298. {
  299. return ioread32(priv->base + reg_offs);
  300. }
  301. static void lcdc_wait_bit(struct sh_mobile_lcdc_priv *priv,
  302. unsigned long reg_offs,
  303. unsigned long mask, unsigned long until)
  304. {
  305. while ((lcdc_read(priv, reg_offs) & mask) != until)
  306. cpu_relax();
  307. }
  308. /* -----------------------------------------------------------------------------
  309. * Clock management
  310. */
  311. static void sh_mobile_lcdc_clk_on(struct sh_mobile_lcdc_priv *priv)
  312. {
  313. if (atomic_inc_and_test(&priv->hw_usecnt)) {
  314. if (priv->dot_clk)
  315. clk_enable(priv->dot_clk);
  316. pm_runtime_get_sync(priv->dev);
  317. if (priv->meram_dev && priv->meram_dev->pdev)
  318. pm_runtime_get_sync(&priv->meram_dev->pdev->dev);
  319. }
  320. }
  321. static void sh_mobile_lcdc_clk_off(struct sh_mobile_lcdc_priv *priv)
  322. {
  323. if (atomic_sub_return(1, &priv->hw_usecnt) == -1) {
  324. if (priv->meram_dev && priv->meram_dev->pdev)
  325. pm_runtime_put_sync(&priv->meram_dev->pdev->dev);
  326. pm_runtime_put(priv->dev);
  327. if (priv->dot_clk)
  328. clk_disable(priv->dot_clk);
  329. }
  330. }
  331. static int sh_mobile_lcdc_setup_clocks(struct sh_mobile_lcdc_priv *priv,
  332. int clock_source)
  333. {
  334. struct clk *clk;
  335. char *str;
  336. switch (clock_source) {
  337. case LCDC_CLK_BUS:
  338. str = "bus_clk";
  339. priv->lddckr = LDDCKR_ICKSEL_BUS;
  340. break;
  341. case LCDC_CLK_PERIPHERAL:
  342. str = "peripheral_clk";
  343. priv->lddckr = LDDCKR_ICKSEL_MIPI;
  344. break;
  345. case LCDC_CLK_EXTERNAL:
  346. str = NULL;
  347. priv->lddckr = LDDCKR_ICKSEL_HDMI;
  348. break;
  349. default:
  350. return -EINVAL;
  351. }
  352. if (str == NULL)
  353. return 0;
  354. clk = clk_get(priv->dev, str);
  355. if (IS_ERR(clk)) {
  356. dev_err(priv->dev, "cannot get dot clock %s\n", str);
  357. return PTR_ERR(clk);
  358. }
  359. priv->dot_clk = clk;
  360. return 0;
  361. }
  362. /* -----------------------------------------------------------------------------
  363. * Display, panel and deferred I/O
  364. */
  365. static void lcdc_sys_write_index(void *handle, unsigned long data)
  366. {
  367. struct sh_mobile_lcdc_chan *ch = handle;
  368. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT);
  369. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  370. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  371. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  372. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  373. }
  374. static void lcdc_sys_write_data(void *handle, unsigned long data)
  375. {
  376. struct sh_mobile_lcdc_chan *ch = handle;
  377. lcdc_write(ch->lcdc, _LDDWD0R, data | LDDWDxR_WDACT | LDDWDxR_RSW);
  378. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  379. lcdc_write(ch->lcdc, _LDDWAR, LDDWAR_WA |
  380. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  381. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  382. }
  383. static unsigned long lcdc_sys_read_data(void *handle)
  384. {
  385. struct sh_mobile_lcdc_chan *ch = handle;
  386. lcdc_write(ch->lcdc, _LDDRDR, LDDRDR_RSR);
  387. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  388. lcdc_write(ch->lcdc, _LDDRAR, LDDRAR_RA |
  389. (lcdc_chan_is_sublcd(ch) ? 2 : 0));
  390. udelay(1);
  391. lcdc_wait_bit(ch->lcdc, _LDSR, LDSR_AS, 0);
  392. return lcdc_read(ch->lcdc, _LDDRDR) & LDDRDR_DRD_MASK;
  393. }
  394. struct sh_mobile_lcdc_sys_bus_ops sh_mobile_lcdc_sys_bus_ops = {
  395. lcdc_sys_write_index,
  396. lcdc_sys_write_data,
  397. lcdc_sys_read_data,
  398. };
  399. static int sh_mobile_lcdc_sginit(struct fb_info *info,
  400. struct list_head *pagelist)
  401. {
  402. struct sh_mobile_lcdc_chan *ch = info->par;
  403. unsigned int nr_pages_max = ch->fb_size >> PAGE_SHIFT;
  404. struct page *page;
  405. int nr_pages = 0;
  406. sg_init_table(ch->sglist, nr_pages_max);
  407. list_for_each_entry(page, pagelist, lru)
  408. sg_set_page(&ch->sglist[nr_pages++], page, PAGE_SIZE, 0);
  409. return nr_pages;
  410. }
  411. static void sh_mobile_lcdc_deferred_io(struct fb_info *info,
  412. struct list_head *pagelist)
  413. {
  414. struct sh_mobile_lcdc_chan *ch = info->par;
  415. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  416. /* enable clocks before accessing hardware */
  417. sh_mobile_lcdc_clk_on(ch->lcdc);
  418. /*
  419. * It's possible to get here without anything on the pagelist via
  420. * sh_mobile_lcdc_deferred_io_touch() or via a userspace fsync()
  421. * invocation. In the former case, the acceleration routines are
  422. * stepped in to when using the framebuffer console causing the
  423. * workqueue to be scheduled without any dirty pages on the list.
  424. *
  425. * Despite this, a panel update is still needed given that the
  426. * acceleration routines have their own methods for writing in
  427. * that still need to be updated.
  428. *
  429. * The fsync() and empty pagelist case could be optimized for,
  430. * but we don't bother, as any application exhibiting such
  431. * behaviour is fundamentally broken anyways.
  432. */
  433. if (!list_empty(pagelist)) {
  434. unsigned int nr_pages = sh_mobile_lcdc_sginit(info, pagelist);
  435. /* trigger panel update */
  436. dma_map_sg(ch->lcdc->dev, ch->sglist, nr_pages, DMA_TO_DEVICE);
  437. if (panel->start_transfer)
  438. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  439. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  440. dma_unmap_sg(ch->lcdc->dev, ch->sglist, nr_pages,
  441. DMA_TO_DEVICE);
  442. } else {
  443. if (panel->start_transfer)
  444. panel->start_transfer(ch, &sh_mobile_lcdc_sys_bus_ops);
  445. lcdc_write_chan(ch, LDSM2R, LDSM2R_OSTRG);
  446. }
  447. }
  448. static void sh_mobile_lcdc_deferred_io_touch(struct fb_info *info)
  449. {
  450. struct fb_deferred_io *fbdefio = info->fbdefio;
  451. if (fbdefio)
  452. schedule_delayed_work(&info->deferred_work, fbdefio->delay);
  453. }
  454. static void sh_mobile_lcdc_display_on(struct sh_mobile_lcdc_chan *ch)
  455. {
  456. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  457. if (ch->tx_dev) {
  458. int ret;
  459. ret = ch->tx_dev->ops->display_on(ch->tx_dev);
  460. if (ret < 0)
  461. return;
  462. if (ret == SH_MOBILE_LCDC_DISPLAY_DISCONNECTED)
  463. ch->info->state = FBINFO_STATE_SUSPENDED;
  464. }
  465. /* HDMI must be enabled before LCDC configuration */
  466. if (panel->display_on)
  467. panel->display_on();
  468. }
  469. static void sh_mobile_lcdc_display_off(struct sh_mobile_lcdc_chan *ch)
  470. {
  471. const struct sh_mobile_lcdc_panel_cfg *panel = &ch->cfg->panel_cfg;
  472. if (panel->display_off)
  473. panel->display_off();
  474. if (ch->tx_dev)
  475. ch->tx_dev->ops->display_off(ch->tx_dev);
  476. }
  477. static bool
  478. sh_mobile_lcdc_must_reconfigure(struct sh_mobile_lcdc_chan *ch,
  479. const struct fb_videomode *new_mode)
  480. {
  481. dev_dbg(ch->info->dev, "Old %ux%u, new %ux%u\n",
  482. ch->display.mode.xres, ch->display.mode.yres,
  483. new_mode->xres, new_mode->yres);
  484. /* It can be a different monitor with an equal video-mode */
  485. if (fb_mode_is_equal(&ch->display.mode, new_mode))
  486. return false;
  487. dev_dbg(ch->info->dev, "Switching %u -> %u lines\n",
  488. ch->display.mode.yres, new_mode->yres);
  489. ch->display.mode = *new_mode;
  490. return true;
  491. }
  492. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  493. struct fb_info *info);
  494. static int sh_mobile_lcdc_display_notify(struct sh_mobile_lcdc_chan *ch,
  495. enum sh_mobile_lcdc_entity_event event,
  496. const struct fb_videomode *mode,
  497. const struct fb_monspecs *monspec)
  498. {
  499. struct fb_info *info = ch->info;
  500. struct fb_var_screeninfo var;
  501. int ret = 0;
  502. switch (event) {
  503. case SH_MOBILE_LCDC_EVENT_DISPLAY_CONNECT:
  504. /* HDMI plug in */
  505. if (lock_fb_info(info)) {
  506. console_lock();
  507. ch->display.width = monspec->max_x * 10;
  508. ch->display.height = monspec->max_y * 10;
  509. if (!sh_mobile_lcdc_must_reconfigure(ch, mode) &&
  510. info->state == FBINFO_STATE_RUNNING) {
  511. /* First activation with the default monitor.
  512. * Just turn on, if we run a resume here, the
  513. * logo disappears.
  514. */
  515. info->var.width = monspec->max_x * 10;
  516. info->var.height = monspec->max_y * 10;
  517. sh_mobile_lcdc_display_on(ch);
  518. } else {
  519. /* New monitor or have to wake up */
  520. fb_set_suspend(info, 0);
  521. }
  522. console_unlock();
  523. unlock_fb_info(info);
  524. }
  525. break;
  526. case SH_MOBILE_LCDC_EVENT_DISPLAY_DISCONNECT:
  527. /* HDMI disconnect */
  528. if (lock_fb_info(info)) {
  529. console_lock();
  530. fb_set_suspend(info, 1);
  531. console_unlock();
  532. unlock_fb_info(info);
  533. }
  534. break;
  535. case SH_MOBILE_LCDC_EVENT_DISPLAY_MODE:
  536. /* Validate a proposed new mode */
  537. fb_videomode_to_var(&var, mode);
  538. var.bits_per_pixel = info->var.bits_per_pixel;
  539. var.grayscale = info->var.grayscale;
  540. ret = sh_mobile_lcdc_check_var(&var, info);
  541. break;
  542. }
  543. return ret;
  544. }
  545. /* -----------------------------------------------------------------------------
  546. * Format helpers
  547. */
  548. struct sh_mobile_lcdc_format_info {
  549. u32 fourcc;
  550. unsigned int bpp;
  551. bool yuv;
  552. u32 lddfr;
  553. };
  554. static const struct sh_mobile_lcdc_format_info sh_mobile_format_infos[] = {
  555. {
  556. .fourcc = V4L2_PIX_FMT_RGB565,
  557. .bpp = 16,
  558. .yuv = false,
  559. .lddfr = LDDFR_PKF_RGB16,
  560. }, {
  561. .fourcc = V4L2_PIX_FMT_BGR24,
  562. .bpp = 24,
  563. .yuv = false,
  564. .lddfr = LDDFR_PKF_RGB24,
  565. }, {
  566. .fourcc = V4L2_PIX_FMT_BGR32,
  567. .bpp = 32,
  568. .yuv = false,
  569. .lddfr = LDDFR_PKF_ARGB32,
  570. }, {
  571. .fourcc = V4L2_PIX_FMT_NV12,
  572. .bpp = 12,
  573. .yuv = true,
  574. .lddfr = LDDFR_CC | LDDFR_YF_420,
  575. }, {
  576. .fourcc = V4L2_PIX_FMT_NV21,
  577. .bpp = 12,
  578. .yuv = true,
  579. .lddfr = LDDFR_CC | LDDFR_YF_420,
  580. }, {
  581. .fourcc = V4L2_PIX_FMT_NV16,
  582. .bpp = 16,
  583. .yuv = true,
  584. .lddfr = LDDFR_CC | LDDFR_YF_422,
  585. }, {
  586. .fourcc = V4L2_PIX_FMT_NV61,
  587. .bpp = 16,
  588. .yuv = true,
  589. .lddfr = LDDFR_CC | LDDFR_YF_422,
  590. }, {
  591. .fourcc = V4L2_PIX_FMT_NV24,
  592. .bpp = 24,
  593. .yuv = true,
  594. .lddfr = LDDFR_CC | LDDFR_YF_444,
  595. }, {
  596. .fourcc = V4L2_PIX_FMT_NV42,
  597. .bpp = 24,
  598. .yuv = true,
  599. .lddfr = LDDFR_CC | LDDFR_YF_444,
  600. },
  601. };
  602. static const struct sh_mobile_lcdc_format_info *
  603. sh_mobile_format_info(u32 fourcc)
  604. {
  605. unsigned int i;
  606. for (i = 0; i < ARRAY_SIZE(sh_mobile_format_infos); ++i) {
  607. if (sh_mobile_format_infos[i].fourcc == fourcc)
  608. return &sh_mobile_format_infos[i];
  609. }
  610. return NULL;
  611. }
  612. static int sh_mobile_format_fourcc(const struct fb_var_screeninfo *var)
  613. {
  614. if (var->grayscale > 1)
  615. return var->grayscale;
  616. switch (var->bits_per_pixel) {
  617. case 16:
  618. return V4L2_PIX_FMT_RGB565;
  619. case 24:
  620. return V4L2_PIX_FMT_BGR24;
  621. case 32:
  622. return V4L2_PIX_FMT_BGR32;
  623. default:
  624. return 0;
  625. }
  626. }
  627. static int sh_mobile_format_is_fourcc(const struct fb_var_screeninfo *var)
  628. {
  629. return var->grayscale > 1;
  630. }
  631. /* -----------------------------------------------------------------------------
  632. * Start, stop and IRQ
  633. */
  634. static irqreturn_t sh_mobile_lcdc_irq(int irq, void *data)
  635. {
  636. struct sh_mobile_lcdc_priv *priv = data;
  637. struct sh_mobile_lcdc_chan *ch;
  638. unsigned long ldintr;
  639. int is_sub;
  640. int k;
  641. /* Acknowledge interrupts and disable further VSYNC End IRQs. */
  642. ldintr = lcdc_read(priv, _LDINTR);
  643. lcdc_write(priv, _LDINTR, (ldintr ^ LDINTR_STATUS_MASK) & ~LDINTR_VEE);
  644. /* figure out if this interrupt is for main or sub lcd */
  645. is_sub = (lcdc_read(priv, _LDSR) & LDSR_MSS) ? 1 : 0;
  646. /* wake up channel and disable clocks */
  647. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  648. ch = &priv->ch[k];
  649. if (!ch->enabled)
  650. continue;
  651. /* Frame End */
  652. if (ldintr & LDINTR_FS) {
  653. if (is_sub == lcdc_chan_is_sublcd(ch)) {
  654. ch->frame_end = 1;
  655. wake_up(&ch->frame_end_wait);
  656. sh_mobile_lcdc_clk_off(priv);
  657. }
  658. }
  659. /* VSYNC End */
  660. if (ldintr & LDINTR_VES)
  661. complete(&ch->vsync_completion);
  662. }
  663. return IRQ_HANDLED;
  664. }
  665. static int sh_mobile_lcdc_wait_for_vsync(struct sh_mobile_lcdc_chan *ch)
  666. {
  667. unsigned long ldintr;
  668. int ret;
  669. /* Enable VSync End interrupt and be careful not to acknowledge any
  670. * pending interrupt.
  671. */
  672. ldintr = lcdc_read(ch->lcdc, _LDINTR);
  673. ldintr |= LDINTR_VEE | LDINTR_STATUS_MASK;
  674. lcdc_write(ch->lcdc, _LDINTR, ldintr);
  675. ret = wait_for_completion_interruptible_timeout(&ch->vsync_completion,
  676. msecs_to_jiffies(100));
  677. if (!ret)
  678. return -ETIMEDOUT;
  679. return 0;
  680. }
  681. static void sh_mobile_lcdc_start_stop(struct sh_mobile_lcdc_priv *priv,
  682. int start)
  683. {
  684. unsigned long tmp = lcdc_read(priv, _LDCNT2R);
  685. int k;
  686. /* start or stop the lcdc */
  687. if (start)
  688. lcdc_write(priv, _LDCNT2R, tmp | LDCNT2R_DO);
  689. else
  690. lcdc_write(priv, _LDCNT2R, tmp & ~LDCNT2R_DO);
  691. /* wait until power is applied/stopped on all channels */
  692. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  693. if (lcdc_read(priv, _LDCNT2R) & priv->ch[k].enabled)
  694. while (1) {
  695. tmp = lcdc_read_chan(&priv->ch[k], LDPMR)
  696. & LDPMR_LPS;
  697. if (start && tmp == LDPMR_LPS)
  698. break;
  699. if (!start && tmp == 0)
  700. break;
  701. cpu_relax();
  702. }
  703. if (!start)
  704. lcdc_write(priv, _LDDCKSTPR, 1); /* stop dotclock */
  705. }
  706. static void sh_mobile_lcdc_geometry(struct sh_mobile_lcdc_chan *ch)
  707. {
  708. const struct fb_var_screeninfo *var = &ch->info->var;
  709. const struct fb_videomode *mode = &ch->display.mode;
  710. unsigned long h_total, hsync_pos, display_h_total;
  711. u32 tmp;
  712. tmp = ch->ldmt1r_value;
  713. tmp |= (var->sync & FB_SYNC_VERT_HIGH_ACT) ? 0 : LDMT1R_VPOL;
  714. tmp |= (var->sync & FB_SYNC_HOR_HIGH_ACT) ? 0 : LDMT1R_HPOL;
  715. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWPOL) ? LDMT1R_DWPOL : 0;
  716. tmp |= (ch->cfg->flags & LCDC_FLAGS_DIPOL) ? LDMT1R_DIPOL : 0;
  717. tmp |= (ch->cfg->flags & LCDC_FLAGS_DAPOL) ? LDMT1R_DAPOL : 0;
  718. tmp |= (ch->cfg->flags & LCDC_FLAGS_HSCNT) ? LDMT1R_HSCNT : 0;
  719. tmp |= (ch->cfg->flags & LCDC_FLAGS_DWCNT) ? LDMT1R_DWCNT : 0;
  720. lcdc_write_chan(ch, LDMT1R, tmp);
  721. /* setup SYS bus */
  722. lcdc_write_chan(ch, LDMT2R, ch->cfg->sys_bus_cfg.ldmt2r);
  723. lcdc_write_chan(ch, LDMT3R, ch->cfg->sys_bus_cfg.ldmt3r);
  724. /* horizontal configuration */
  725. h_total = mode->xres + mode->hsync_len + mode->left_margin
  726. + mode->right_margin;
  727. tmp = h_total / 8; /* HTCN */
  728. tmp |= (min(mode->xres, ch->xres) / 8) << 16; /* HDCN */
  729. lcdc_write_chan(ch, LDHCNR, tmp);
  730. hsync_pos = mode->xres + mode->right_margin;
  731. tmp = hsync_pos / 8; /* HSYNP */
  732. tmp |= (mode->hsync_len / 8) << 16; /* HSYNW */
  733. lcdc_write_chan(ch, LDHSYNR, tmp);
  734. /* vertical configuration */
  735. tmp = mode->yres + mode->vsync_len + mode->upper_margin
  736. + mode->lower_margin; /* VTLN */
  737. tmp |= min(mode->yres, ch->yres) << 16; /* VDLN */
  738. lcdc_write_chan(ch, LDVLNR, tmp);
  739. tmp = mode->yres + mode->lower_margin; /* VSYNP */
  740. tmp |= mode->vsync_len << 16; /* VSYNW */
  741. lcdc_write_chan(ch, LDVSYNR, tmp);
  742. /* Adjust horizontal synchronisation for HDMI */
  743. display_h_total = mode->xres + mode->hsync_len + mode->left_margin
  744. + mode->right_margin;
  745. tmp = ((mode->xres & 7) << 24) | ((display_h_total & 7) << 16)
  746. | ((mode->hsync_len & 7) << 8) | (hsync_pos & 7);
  747. lcdc_write_chan(ch, LDHAJR, tmp);
  748. }
  749. static void sh_mobile_lcdc_overlay_setup(struct sh_mobile_lcdc_overlay *ovl)
  750. {
  751. u32 format = 0;
  752. if (!ovl->enabled) {
  753. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  754. lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), 0);
  755. lcdc_write(ovl->channel->lcdc, LDBCR,
  756. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  757. return;
  758. }
  759. ovl->base_addr_y = ovl->dma_handle;
  760. ovl->base_addr_c = ovl->dma_handle
  761. + ovl->xres_virtual * ovl->yres_virtual;
  762. switch (ovl->mode) {
  763. case LCDC_OVERLAY_BLEND:
  764. format = LDBBSIFR_EN | (ovl->alpha << LDBBSIFR_LAY_SHIFT);
  765. break;
  766. case LCDC_OVERLAY_ROP3:
  767. format = LDBBSIFR_EN | LDBBSIFR_BRSEL
  768. | (ovl->rop3 << LDBBSIFR_ROP3_SHIFT);
  769. break;
  770. }
  771. switch (ovl->format->fourcc) {
  772. case V4L2_PIX_FMT_RGB565:
  773. case V4L2_PIX_FMT_NV21:
  774. case V4L2_PIX_FMT_NV61:
  775. case V4L2_PIX_FMT_NV42:
  776. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW;
  777. break;
  778. case V4L2_PIX_FMT_BGR24:
  779. case V4L2_PIX_FMT_NV12:
  780. case V4L2_PIX_FMT_NV16:
  781. case V4L2_PIX_FMT_NV24:
  782. format |= LDBBSIFR_SWPL | LDBBSIFR_SWPW | LDBBSIFR_SWPB;
  783. break;
  784. case V4L2_PIX_FMT_BGR32:
  785. default:
  786. format |= LDBBSIFR_SWPL;
  787. break;
  788. }
  789. switch (ovl->format->fourcc) {
  790. case V4L2_PIX_FMT_RGB565:
  791. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB16;
  792. break;
  793. case V4L2_PIX_FMT_BGR24:
  794. format |= LDBBSIFR_AL_1 | LDBBSIFR_RY | LDBBSIFR_RPKF_RGB24;
  795. break;
  796. case V4L2_PIX_FMT_BGR32:
  797. format |= LDBBSIFR_AL_PK | LDBBSIFR_RY | LDDFR_PKF_ARGB32;
  798. break;
  799. case V4L2_PIX_FMT_NV12:
  800. case V4L2_PIX_FMT_NV21:
  801. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_420;
  802. break;
  803. case V4L2_PIX_FMT_NV16:
  804. case V4L2_PIX_FMT_NV61:
  805. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_422;
  806. break;
  807. case V4L2_PIX_FMT_NV24:
  808. case V4L2_PIX_FMT_NV42:
  809. format |= LDBBSIFR_AL_1 | LDBBSIFR_CHRR_444;
  810. break;
  811. }
  812. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  813. lcdc_write_overlay(ovl, LDBnBSIFR(ovl->index), format);
  814. lcdc_write_overlay(ovl, LDBnBSSZR(ovl->index),
  815. (ovl->yres << LDBBSSZR_BVSS_SHIFT) |
  816. (ovl->xres << LDBBSSZR_BHSS_SHIFT));
  817. lcdc_write_overlay(ovl, LDBnBLOCR(ovl->index),
  818. (ovl->pos_y << LDBBLOCR_CVLC_SHIFT) |
  819. (ovl->pos_x << LDBBLOCR_CHLC_SHIFT));
  820. lcdc_write_overlay(ovl, LDBnBSMWR(ovl->index),
  821. ovl->pitch << LDBBSMWR_BSMW_SHIFT);
  822. lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
  823. lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
  824. lcdc_write(ovl->channel->lcdc, LDBCR,
  825. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  826. }
  827. /*
  828. * __sh_mobile_lcdc_start - Configure and start the LCDC
  829. * @priv: LCDC device
  830. *
  831. * Configure all enabled channels and start the LCDC device. All external
  832. * devices (clocks, MERAM, panels, ...) are not touched by this function.
  833. */
  834. static void __sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  835. {
  836. struct sh_mobile_lcdc_chan *ch;
  837. unsigned long tmp;
  838. int k, m;
  839. /* Enable LCDC channels. Read data from external memory, avoid using the
  840. * BEU for now.
  841. */
  842. lcdc_write(priv, _LDCNT2R, priv->ch[0].enabled | priv->ch[1].enabled);
  843. /* Stop the LCDC first and disable all interrupts. */
  844. sh_mobile_lcdc_start_stop(priv, 0);
  845. lcdc_write(priv, _LDINTR, 0);
  846. /* Configure power supply, dot clocks and start them. */
  847. tmp = priv->lddckr;
  848. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  849. ch = &priv->ch[k];
  850. if (!ch->enabled)
  851. continue;
  852. /* Power supply */
  853. lcdc_write_chan(ch, LDPMR, 0);
  854. m = ch->cfg->clock_divider;
  855. if (!m)
  856. continue;
  857. /* FIXME: sh7724 can only use 42, 48, 54 and 60 for the divider
  858. * denominator.
  859. */
  860. lcdc_write_chan(ch, LDDCKPAT1R, 0);
  861. lcdc_write_chan(ch, LDDCKPAT2R, (1 << (m/2)) - 1);
  862. if (m == 1)
  863. m = LDDCKR_MOSEL;
  864. tmp |= m << (lcdc_chan_is_sublcd(ch) ? 8 : 0);
  865. }
  866. lcdc_write(priv, _LDDCKR, tmp);
  867. lcdc_write(priv, _LDDCKSTPR, 0);
  868. lcdc_wait_bit(priv, _LDDCKSTPR, ~0, 0);
  869. /* Setup geometry, format, frame buffer memory and operation mode. */
  870. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  871. ch = &priv->ch[k];
  872. if (!ch->enabled)
  873. continue;
  874. sh_mobile_lcdc_geometry(ch);
  875. tmp = ch->format->lddfr;
  876. if (ch->format->yuv) {
  877. switch (ch->colorspace) {
  878. case V4L2_COLORSPACE_REC709:
  879. tmp |= LDDFR_CF1;
  880. break;
  881. case V4L2_COLORSPACE_JPEG:
  882. tmp |= LDDFR_CF0;
  883. break;
  884. }
  885. }
  886. lcdc_write_chan(ch, LDDFR, tmp);
  887. lcdc_write_chan(ch, LDMLSR, ch->line_size);
  888. lcdc_write_chan(ch, LDSA1R, ch->base_addr_y);
  889. if (ch->format->yuv)
  890. lcdc_write_chan(ch, LDSA2R, ch->base_addr_c);
  891. /* When using deferred I/O mode, configure the LCDC for one-shot
  892. * operation and enable the frame end interrupt. Otherwise use
  893. * continuous read mode.
  894. */
  895. if (ch->ldmt1r_value & LDMT1R_IFM &&
  896. ch->cfg->sys_bus_cfg.deferred_io_msec) {
  897. lcdc_write_chan(ch, LDSM1R, LDSM1R_OS);
  898. lcdc_write(priv, _LDINTR, LDINTR_FE);
  899. } else {
  900. lcdc_write_chan(ch, LDSM1R, 0);
  901. }
  902. }
  903. /* Word and long word swap. */
  904. switch (priv->ch[0].format->fourcc) {
  905. case V4L2_PIX_FMT_RGB565:
  906. case V4L2_PIX_FMT_NV21:
  907. case V4L2_PIX_FMT_NV61:
  908. case V4L2_PIX_FMT_NV42:
  909. tmp = LDDDSR_LS | LDDDSR_WS;
  910. break;
  911. case V4L2_PIX_FMT_BGR24:
  912. case V4L2_PIX_FMT_NV12:
  913. case V4L2_PIX_FMT_NV16:
  914. case V4L2_PIX_FMT_NV24:
  915. tmp = LDDDSR_LS | LDDDSR_WS | LDDDSR_BS;
  916. break;
  917. case V4L2_PIX_FMT_BGR32:
  918. default:
  919. tmp = LDDDSR_LS;
  920. break;
  921. }
  922. lcdc_write(priv, _LDDDSR, tmp);
  923. /* Enable the display output. */
  924. lcdc_write(priv, _LDCNT1R, LDCNT1R_DE);
  925. sh_mobile_lcdc_start_stop(priv, 1);
  926. priv->started = 1;
  927. }
  928. static int sh_mobile_lcdc_start(struct sh_mobile_lcdc_priv *priv)
  929. {
  930. struct sh_mobile_meram_info *mdev = priv->meram_dev;
  931. struct sh_mobile_lcdc_chan *ch;
  932. unsigned long tmp;
  933. int ret;
  934. int k;
  935. /* enable clocks before accessing the hardware */
  936. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  937. if (priv->ch[k].enabled)
  938. sh_mobile_lcdc_clk_on(priv);
  939. }
  940. /* reset */
  941. lcdc_write(priv, _LDCNT2R, lcdc_read(priv, _LDCNT2R) | LDCNT2R_BR);
  942. lcdc_wait_bit(priv, _LDCNT2R, LDCNT2R_BR, 0);
  943. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  944. const struct sh_mobile_lcdc_panel_cfg *panel;
  945. ch = &priv->ch[k];
  946. if (!ch->enabled)
  947. continue;
  948. panel = &ch->cfg->panel_cfg;
  949. if (panel->setup_sys) {
  950. ret = panel->setup_sys(ch, &sh_mobile_lcdc_sys_bus_ops);
  951. if (ret)
  952. return ret;
  953. }
  954. }
  955. /* Compute frame buffer base address and pitch for each channel. */
  956. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  957. int pixelformat;
  958. void *cache;
  959. ch = &priv->ch[k];
  960. if (!ch->enabled)
  961. continue;
  962. ch->base_addr_y = ch->dma_handle;
  963. ch->base_addr_c = ch->dma_handle
  964. + ch->xres_virtual * ch->yres_virtual;
  965. ch->line_size = ch->pitch;
  966. /* Enable MERAM if possible. */
  967. if (mdev == NULL || ch->cfg->meram_cfg == NULL)
  968. continue;
  969. /* Free the allocated MERAM cache. */
  970. if (ch->cache) {
  971. sh_mobile_meram_cache_free(mdev, ch->cache);
  972. ch->cache = NULL;
  973. }
  974. switch (ch->format->fourcc) {
  975. case V4L2_PIX_FMT_NV12:
  976. case V4L2_PIX_FMT_NV21:
  977. case V4L2_PIX_FMT_NV16:
  978. case V4L2_PIX_FMT_NV61:
  979. pixelformat = SH_MOBILE_MERAM_PF_NV;
  980. break;
  981. case V4L2_PIX_FMT_NV24:
  982. case V4L2_PIX_FMT_NV42:
  983. pixelformat = SH_MOBILE_MERAM_PF_NV24;
  984. break;
  985. case V4L2_PIX_FMT_RGB565:
  986. case V4L2_PIX_FMT_BGR24:
  987. case V4L2_PIX_FMT_BGR32:
  988. default:
  989. pixelformat = SH_MOBILE_MERAM_PF_RGB;
  990. break;
  991. }
  992. cache = sh_mobile_meram_cache_alloc(mdev, ch->cfg->meram_cfg,
  993. ch->pitch, ch->yres, pixelformat,
  994. &ch->line_size);
  995. if (!IS_ERR(cache)) {
  996. sh_mobile_meram_cache_update(mdev, cache,
  997. ch->base_addr_y, ch->base_addr_c,
  998. &ch->base_addr_y, &ch->base_addr_c);
  999. ch->cache = cache;
  1000. }
  1001. }
  1002. for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
  1003. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
  1004. sh_mobile_lcdc_overlay_setup(ovl);
  1005. }
  1006. /* Start the LCDC. */
  1007. __sh_mobile_lcdc_start(priv);
  1008. /* Setup deferred I/O, tell the board code to enable the panels, and
  1009. * turn backlight on.
  1010. */
  1011. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  1012. ch = &priv->ch[k];
  1013. if (!ch->enabled)
  1014. continue;
  1015. tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
  1016. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  1017. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  1018. ch->defio.delay = msecs_to_jiffies(tmp);
  1019. ch->info->fbdefio = &ch->defio;
  1020. fb_deferred_io_init(ch->info);
  1021. }
  1022. sh_mobile_lcdc_display_on(ch);
  1023. if (ch->bl) {
  1024. ch->bl->props.power = FB_BLANK_UNBLANK;
  1025. backlight_update_status(ch->bl);
  1026. }
  1027. }
  1028. return 0;
  1029. }
  1030. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  1031. {
  1032. struct sh_mobile_lcdc_chan *ch;
  1033. int k;
  1034. /* clean up deferred io and ask board code to disable panel */
  1035. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  1036. ch = &priv->ch[k];
  1037. if (!ch->enabled)
  1038. continue;
  1039. /* deferred io mode:
  1040. * flush frame, and wait for frame end interrupt
  1041. * clean up deferred io and enable clock
  1042. */
  1043. if (ch->info && ch->info->fbdefio) {
  1044. ch->frame_end = 0;
  1045. schedule_delayed_work(&ch->info->deferred_work, 0);
  1046. wait_event(ch->frame_end_wait, ch->frame_end);
  1047. fb_deferred_io_cleanup(ch->info);
  1048. ch->info->fbdefio = NULL;
  1049. sh_mobile_lcdc_clk_on(priv);
  1050. }
  1051. if (ch->bl) {
  1052. ch->bl->props.power = FB_BLANK_POWERDOWN;
  1053. backlight_update_status(ch->bl);
  1054. }
  1055. sh_mobile_lcdc_display_off(ch);
  1056. /* Free the MERAM cache. */
  1057. if (ch->cache) {
  1058. sh_mobile_meram_cache_free(priv->meram_dev, ch->cache);
  1059. ch->cache = 0;
  1060. }
  1061. }
  1062. /* stop the lcdc */
  1063. if (priv->started) {
  1064. sh_mobile_lcdc_start_stop(priv, 0);
  1065. priv->started = 0;
  1066. }
  1067. /* stop clocks */
  1068. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  1069. if (priv->ch[k].enabled)
  1070. sh_mobile_lcdc_clk_off(priv);
  1071. }
  1072. static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1073. struct fb_info *info)
  1074. {
  1075. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  1076. return -EINVAL;
  1077. /* Make sure the virtual resolution is at least as big as the visible
  1078. * resolution.
  1079. */
  1080. if (var->xres_virtual < var->xres)
  1081. var->xres_virtual = var->xres;
  1082. if (var->yres_virtual < var->yres)
  1083. var->yres_virtual = var->yres;
  1084. if (sh_mobile_format_is_fourcc(var)) {
  1085. const struct sh_mobile_lcdc_format_info *format;
  1086. format = sh_mobile_format_info(var->grayscale);
  1087. if (format == NULL)
  1088. return -EINVAL;
  1089. var->bits_per_pixel = format->bpp;
  1090. /* Default to RGB and JPEG color-spaces for RGB and YUV formats
  1091. * respectively.
  1092. */
  1093. if (!format->yuv)
  1094. var->colorspace = V4L2_COLORSPACE_SRGB;
  1095. else if (var->colorspace != V4L2_COLORSPACE_REC709)
  1096. var->colorspace = V4L2_COLORSPACE_JPEG;
  1097. } else {
  1098. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  1099. var->bits_per_pixel = 16;
  1100. var->red.offset = 11;
  1101. var->red.length = 5;
  1102. var->green.offset = 5;
  1103. var->green.length = 6;
  1104. var->blue.offset = 0;
  1105. var->blue.length = 5;
  1106. var->transp.offset = 0;
  1107. var->transp.length = 0;
  1108. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  1109. var->bits_per_pixel = 24;
  1110. var->red.offset = 16;
  1111. var->red.length = 8;
  1112. var->green.offset = 8;
  1113. var->green.length = 8;
  1114. var->blue.offset = 0;
  1115. var->blue.length = 8;
  1116. var->transp.offset = 0;
  1117. var->transp.length = 0;
  1118. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  1119. var->bits_per_pixel = 32;
  1120. var->red.offset = 16;
  1121. var->red.length = 8;
  1122. var->green.offset = 8;
  1123. var->green.length = 8;
  1124. var->blue.offset = 0;
  1125. var->blue.length = 8;
  1126. var->transp.offset = 24;
  1127. var->transp.length = 8;
  1128. } else
  1129. return -EINVAL;
  1130. var->red.msb_right = 0;
  1131. var->green.msb_right = 0;
  1132. var->blue.msb_right = 0;
  1133. var->transp.msb_right = 0;
  1134. }
  1135. /* Make sure we don't exceed our allocated memory. */
  1136. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  1137. info->fix.smem_len)
  1138. return -EINVAL;
  1139. return 0;
  1140. }
  1141. /* -----------------------------------------------------------------------------
  1142. * Frame buffer operations - Overlays
  1143. */
  1144. static ssize_t
  1145. overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
  1146. {
  1147. struct fb_info *info = dev_get_drvdata(dev);
  1148. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1149. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
  1150. }
  1151. static ssize_t
  1152. overlay_alpha_store(struct device *dev, struct device_attribute *attr,
  1153. const char *buf, size_t count)
  1154. {
  1155. struct fb_info *info = dev_get_drvdata(dev);
  1156. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1157. unsigned int alpha;
  1158. char *endp;
  1159. alpha = simple_strtoul(buf, &endp, 10);
  1160. if (isspace(*endp))
  1161. endp++;
  1162. if (endp - buf != count)
  1163. return -EINVAL;
  1164. if (alpha > 255)
  1165. return -EINVAL;
  1166. if (ovl->alpha != alpha) {
  1167. ovl->alpha = alpha;
  1168. if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
  1169. sh_mobile_lcdc_overlay_setup(ovl);
  1170. }
  1171. return count;
  1172. }
  1173. static ssize_t
  1174. overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1175. {
  1176. struct fb_info *info = dev_get_drvdata(dev);
  1177. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1178. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
  1179. }
  1180. static ssize_t
  1181. overlay_mode_store(struct device *dev, struct device_attribute *attr,
  1182. const char *buf, size_t count)
  1183. {
  1184. struct fb_info *info = dev_get_drvdata(dev);
  1185. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1186. unsigned int mode;
  1187. char *endp;
  1188. mode = simple_strtoul(buf, &endp, 10);
  1189. if (isspace(*endp))
  1190. endp++;
  1191. if (endp - buf != count)
  1192. return -EINVAL;
  1193. if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
  1194. return -EINVAL;
  1195. if (ovl->mode != mode) {
  1196. ovl->mode = mode;
  1197. if (ovl->enabled)
  1198. sh_mobile_lcdc_overlay_setup(ovl);
  1199. }
  1200. return count;
  1201. }
  1202. static ssize_t
  1203. overlay_position_show(struct device *dev, struct device_attribute *attr,
  1204. char *buf)
  1205. {
  1206. struct fb_info *info = dev_get_drvdata(dev);
  1207. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1208. return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
  1209. }
  1210. static ssize_t
  1211. overlay_position_store(struct device *dev, struct device_attribute *attr,
  1212. const char *buf, size_t count)
  1213. {
  1214. struct fb_info *info = dev_get_drvdata(dev);
  1215. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1216. char *endp;
  1217. int pos_x;
  1218. int pos_y;
  1219. pos_x = simple_strtol(buf, &endp, 10);
  1220. if (*endp != ',')
  1221. return -EINVAL;
  1222. pos_y = simple_strtol(endp + 1, &endp, 10);
  1223. if (isspace(*endp))
  1224. endp++;
  1225. if (endp - buf != count)
  1226. return -EINVAL;
  1227. if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
  1228. ovl->pos_x = pos_x;
  1229. ovl->pos_y = pos_y;
  1230. if (ovl->enabled)
  1231. sh_mobile_lcdc_overlay_setup(ovl);
  1232. }
  1233. return count;
  1234. }
  1235. static ssize_t
  1236. overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
  1237. {
  1238. struct fb_info *info = dev_get_drvdata(dev);
  1239. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1240. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
  1241. }
  1242. static ssize_t
  1243. overlay_rop3_store(struct device *dev, struct device_attribute *attr,
  1244. const char *buf, size_t count)
  1245. {
  1246. struct fb_info *info = dev_get_drvdata(dev);
  1247. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1248. unsigned int rop3;
  1249. char *endp;
  1250. rop3 = !!simple_strtoul(buf, &endp, 10);
  1251. if (isspace(*endp))
  1252. endp++;
  1253. if (endp - buf != count)
  1254. return -EINVAL;
  1255. if (rop3 > 255)
  1256. return -EINVAL;
  1257. if (ovl->rop3 != rop3) {
  1258. ovl->rop3 = rop3;
  1259. if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
  1260. sh_mobile_lcdc_overlay_setup(ovl);
  1261. }
  1262. return count;
  1263. }
  1264. static const struct device_attribute overlay_sysfs_attrs[] = {
  1265. __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
  1266. overlay_alpha_show, overlay_alpha_store),
  1267. __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
  1268. overlay_mode_show, overlay_mode_store),
  1269. __ATTR(ovl_position, S_IRUGO|S_IWUSR,
  1270. overlay_position_show, overlay_position_store),
  1271. __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
  1272. overlay_rop3_show, overlay_rop3_store),
  1273. };
  1274. static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
  1275. .id = "SH Mobile LCDC",
  1276. .type = FB_TYPE_PACKED_PIXELS,
  1277. .visual = FB_VISUAL_TRUECOLOR,
  1278. .accel = FB_ACCEL_NONE,
  1279. .xpanstep = 1,
  1280. .ypanstep = 1,
  1281. .ywrapstep = 0,
  1282. .capabilities = FB_CAP_FOURCC,
  1283. };
  1284. static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
  1285. struct fb_info *info)
  1286. {
  1287. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1288. unsigned long base_addr_y;
  1289. unsigned long base_addr_c;
  1290. unsigned long y_offset;
  1291. unsigned long c_offset;
  1292. if (!ovl->format->yuv) {
  1293. y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
  1294. * ovl->format->bpp / 8;
  1295. c_offset = 0;
  1296. } else {
  1297. unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
  1298. unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
  1299. y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
  1300. c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
  1301. + var->xoffset * 2 / xsub;
  1302. }
  1303. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1304. * nothing to do in that case.
  1305. */
  1306. if (y_offset == ovl->pan_y_offset)
  1307. return 0;
  1308. /* Set the source address for the next refresh */
  1309. base_addr_y = ovl->dma_handle + y_offset;
  1310. base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
  1311. + c_offset;
  1312. ovl->base_addr_y = base_addr_y;
  1313. ovl->base_addr_c = base_addr_c;
  1314. ovl->pan_y_offset = y_offset;
  1315. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  1316. lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
  1317. lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
  1318. lcdc_write(ovl->channel->lcdc, LDBCR,
  1319. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  1320. return 0;
  1321. }
  1322. static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
  1323. unsigned long arg)
  1324. {
  1325. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1326. switch (cmd) {
  1327. case FBIO_WAITFORVSYNC:
  1328. return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
  1329. default:
  1330. return -ENOIOCTLCMD;
  1331. }
  1332. }
  1333. static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
  1334. struct fb_info *info)
  1335. {
  1336. return __sh_mobile_lcdc_check_var(var, info);
  1337. }
  1338. static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
  1339. {
  1340. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1341. ovl->format =
  1342. sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1343. ovl->xres = info->var.xres;
  1344. ovl->xres_virtual = info->var.xres_virtual;
  1345. ovl->yres = info->var.yres;
  1346. ovl->yres_virtual = info->var.yres_virtual;
  1347. if (ovl->format->yuv)
  1348. ovl->pitch = info->var.xres_virtual;
  1349. else
  1350. ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
  1351. sh_mobile_lcdc_overlay_setup(ovl);
  1352. info->fix.line_length = ovl->pitch;
  1353. if (sh_mobile_format_is_fourcc(&info->var)) {
  1354. info->fix.type = FB_TYPE_FOURCC;
  1355. info->fix.visual = FB_VISUAL_FOURCC;
  1356. } else {
  1357. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1358. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1359. }
  1360. return 0;
  1361. }
  1362. /* Overlay blanking. Disable the overlay when blanked. */
  1363. static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
  1364. {
  1365. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1366. ovl->enabled = !blank;
  1367. sh_mobile_lcdc_overlay_setup(ovl);
  1368. /* Prevent the backlight from receiving a blanking event by returning
  1369. * a non-zero value.
  1370. */
  1371. return 1;
  1372. }
  1373. static struct fb_ops sh_mobile_lcdc_overlay_ops = {
  1374. .owner = THIS_MODULE,
  1375. .fb_read = fb_sys_read,
  1376. .fb_write = fb_sys_write,
  1377. .fb_fillrect = sys_fillrect,
  1378. .fb_copyarea = sys_copyarea,
  1379. .fb_imageblit = sys_imageblit,
  1380. .fb_blank = sh_mobile_lcdc_overlay_blank,
  1381. .fb_pan_display = sh_mobile_lcdc_overlay_pan,
  1382. .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
  1383. .fb_check_var = sh_mobile_lcdc_overlay_check_var,
  1384. .fb_set_par = sh_mobile_lcdc_overlay_set_par,
  1385. };
  1386. static void
  1387. sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
  1388. {
  1389. struct fb_info *info = ovl->info;
  1390. if (info == NULL || info->dev == NULL)
  1391. return;
  1392. unregister_framebuffer(ovl->info);
  1393. }
  1394. static int __devinit
  1395. sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
  1396. {
  1397. struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
  1398. struct fb_info *info = ovl->info;
  1399. unsigned int i;
  1400. int ret;
  1401. if (info == NULL)
  1402. return 0;
  1403. ret = register_framebuffer(info);
  1404. if (ret < 0)
  1405. return ret;
  1406. dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
  1407. dev_name(lcdc->dev), ovl->index, info->var.xres,
  1408. info->var.yres, info->var.bits_per_pixel);
  1409. for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
  1410. ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
  1411. if (ret < 0)
  1412. return ret;
  1413. }
  1414. return 0;
  1415. }
  1416. static void
  1417. sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
  1418. {
  1419. struct fb_info *info = ovl->info;
  1420. if (info == NULL || info->device == NULL)
  1421. return;
  1422. framebuffer_release(info);
  1423. }
  1424. static int __devinit
  1425. sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
  1426. {
  1427. struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
  1428. struct fb_var_screeninfo *var;
  1429. struct fb_info *info;
  1430. /* Allocate and initialize the frame buffer device. */
  1431. info = framebuffer_alloc(0, priv->dev);
  1432. if (info == NULL) {
  1433. dev_err(priv->dev, "unable to allocate fb_info\n");
  1434. return -ENOMEM;
  1435. }
  1436. ovl->info = info;
  1437. info->flags = FBINFO_FLAG_DEFAULT;
  1438. info->fbops = &sh_mobile_lcdc_overlay_ops;
  1439. info->device = priv->dev;
  1440. info->screen_base = ovl->fb_mem;
  1441. info->par = ovl;
  1442. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1443. * for NV12 and NV21.
  1444. */
  1445. info->fix = sh_mobile_lcdc_overlay_fix;
  1446. snprintf(info->fix.id, sizeof(info->fix.id),
  1447. "SH Mobile LCDC Overlay %u", ovl->index);
  1448. info->fix.smem_start = ovl->dma_handle;
  1449. info->fix.smem_len = ovl->fb_size;
  1450. info->fix.line_length = ovl->pitch;
  1451. if (ovl->format->yuv)
  1452. info->fix.visual = FB_VISUAL_FOURCC;
  1453. else
  1454. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1455. switch (ovl->format->fourcc) {
  1456. case V4L2_PIX_FMT_NV12:
  1457. case V4L2_PIX_FMT_NV21:
  1458. info->fix.ypanstep = 2;
  1459. case V4L2_PIX_FMT_NV16:
  1460. case V4L2_PIX_FMT_NV61:
  1461. info->fix.xpanstep = 2;
  1462. }
  1463. /* Initialize variable screen information. */
  1464. var = &info->var;
  1465. memset(var, 0, sizeof(*var));
  1466. var->xres = ovl->xres;
  1467. var->yres = ovl->yres;
  1468. var->xres_virtual = ovl->xres_virtual;
  1469. var->yres_virtual = ovl->yres_virtual;
  1470. var->activate = FB_ACTIVATE_NOW;
  1471. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1472. * for YUV formats.
  1473. */
  1474. if (!ovl->format->yuv)
  1475. var->bits_per_pixel = ovl->format->bpp;
  1476. else
  1477. var->grayscale = ovl->format->fourcc;
  1478. return sh_mobile_lcdc_overlay_check_var(var, info);
  1479. }
  1480. /* -----------------------------------------------------------------------------
  1481. * Frame buffer operations - main frame buffer
  1482. */
  1483. static int sh_mobile_lcdc_setcolreg(u_int regno,
  1484. u_int red, u_int green, u_int blue,
  1485. u_int transp, struct fb_info *info)
  1486. {
  1487. u32 *palette = info->pseudo_palette;
  1488. if (regno >= PALETTE_NR)
  1489. return -EINVAL;
  1490. /* only FB_VISUAL_TRUECOLOR supported */
  1491. red >>= 16 - info->var.red.length;
  1492. green >>= 16 - info->var.green.length;
  1493. blue >>= 16 - info->var.blue.length;
  1494. transp >>= 16 - info->var.transp.length;
  1495. palette[regno] = (red << info->var.red.offset) |
  1496. (green << info->var.green.offset) |
  1497. (blue << info->var.blue.offset) |
  1498. (transp << info->var.transp.offset);
  1499. return 0;
  1500. }
  1501. static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  1502. .id = "SH Mobile LCDC",
  1503. .type = FB_TYPE_PACKED_PIXELS,
  1504. .visual = FB_VISUAL_TRUECOLOR,
  1505. .accel = FB_ACCEL_NONE,
  1506. .xpanstep = 1,
  1507. .ypanstep = 1,
  1508. .ywrapstep = 0,
  1509. .capabilities = FB_CAP_FOURCC,
  1510. };
  1511. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  1512. const struct fb_fillrect *rect)
  1513. {
  1514. sys_fillrect(info, rect);
  1515. sh_mobile_lcdc_deferred_io_touch(info);
  1516. }
  1517. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  1518. const struct fb_copyarea *area)
  1519. {
  1520. sys_copyarea(info, area);
  1521. sh_mobile_lcdc_deferred_io_touch(info);
  1522. }
  1523. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  1524. const struct fb_image *image)
  1525. {
  1526. sys_imageblit(info, image);
  1527. sh_mobile_lcdc_deferred_io_touch(info);
  1528. }
  1529. static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
  1530. struct fb_info *info)
  1531. {
  1532. struct sh_mobile_lcdc_chan *ch = info->par;
  1533. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1534. unsigned long ldrcntr;
  1535. unsigned long base_addr_y, base_addr_c;
  1536. unsigned long y_offset;
  1537. unsigned long c_offset;
  1538. if (!ch->format->yuv) {
  1539. y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
  1540. * ch->format->bpp / 8;
  1541. c_offset = 0;
  1542. } else {
  1543. unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
  1544. unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
  1545. y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
  1546. c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
  1547. + var->xoffset * 2 / xsub;
  1548. }
  1549. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1550. * nothing to do in that case.
  1551. */
  1552. if (y_offset == ch->pan_y_offset)
  1553. return 0;
  1554. /* Set the source address for the next refresh */
  1555. base_addr_y = ch->dma_handle + y_offset;
  1556. base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
  1557. + c_offset;
  1558. if (ch->cache)
  1559. sh_mobile_meram_cache_update(priv->meram_dev, ch->cache,
  1560. base_addr_y, base_addr_c,
  1561. &base_addr_y, &base_addr_c);
  1562. ch->base_addr_y = base_addr_y;
  1563. ch->base_addr_c = base_addr_c;
  1564. ch->pan_y_offset = y_offset;
  1565. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  1566. if (ch->format->yuv)
  1567. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  1568. ldrcntr = lcdc_read(priv, _LDRCNTR);
  1569. if (lcdc_chan_is_sublcd(ch))
  1570. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  1571. else
  1572. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  1573. sh_mobile_lcdc_deferred_io_touch(info);
  1574. return 0;
  1575. }
  1576. static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
  1577. unsigned long arg)
  1578. {
  1579. struct sh_mobile_lcdc_chan *ch = info->par;
  1580. int retval;
  1581. switch (cmd) {
  1582. case FBIO_WAITFORVSYNC:
  1583. retval = sh_mobile_lcdc_wait_for_vsync(ch);
  1584. break;
  1585. default:
  1586. retval = -ENOIOCTLCMD;
  1587. break;
  1588. }
  1589. return retval;
  1590. }
  1591. static void sh_mobile_fb_reconfig(struct fb_info *info)
  1592. {
  1593. struct sh_mobile_lcdc_chan *ch = info->par;
  1594. struct fb_var_screeninfo var;
  1595. struct fb_videomode mode;
  1596. struct fb_event event;
  1597. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  1598. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  1599. /* More framebuffer users are active */
  1600. return;
  1601. fb_var_to_videomode(&mode, &info->var);
  1602. if (fb_mode_is_equal(&ch->display.mode, &mode))
  1603. return;
  1604. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  1605. var = info->var;
  1606. fb_videomode_to_var(&var, &ch->display.mode);
  1607. var.width = ch->display.width;
  1608. var.height = ch->display.height;
  1609. var.activate = FB_ACTIVATE_NOW;
  1610. if (fb_set_var(info, &var) < 0)
  1611. /* Couldn't reconfigure, hopefully, can continue as before */
  1612. return;
  1613. /*
  1614. * fb_set_var() calls the notifier change internally, only if
  1615. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  1616. * user event, we have to call the chain ourselves.
  1617. */
  1618. event.info = info;
  1619. event.data = &ch->display.mode;
  1620. fb_notifier_call_chain(evnt, &event);
  1621. }
  1622. /*
  1623. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  1624. * user == 1, or with console sem held, if user == 0.
  1625. */
  1626. static int sh_mobile_lcdc_release(struct fb_info *info, int user)
  1627. {
  1628. struct sh_mobile_lcdc_chan *ch = info->par;
  1629. mutex_lock(&ch->open_lock);
  1630. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1631. ch->use_count--;
  1632. /* Nothing to reconfigure, when called from fbcon */
  1633. if (user) {
  1634. console_lock();
  1635. sh_mobile_fb_reconfig(info);
  1636. console_unlock();
  1637. }
  1638. mutex_unlock(&ch->open_lock);
  1639. return 0;
  1640. }
  1641. static int sh_mobile_lcdc_open(struct fb_info *info, int user)
  1642. {
  1643. struct sh_mobile_lcdc_chan *ch = info->par;
  1644. mutex_lock(&ch->open_lock);
  1645. ch->use_count++;
  1646. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1647. mutex_unlock(&ch->open_lock);
  1648. return 0;
  1649. }
  1650. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1651. struct fb_info *info)
  1652. {
  1653. struct sh_mobile_lcdc_chan *ch = info->par;
  1654. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1655. unsigned int best_dist = (unsigned int)-1;
  1656. unsigned int best_xres = 0;
  1657. unsigned int best_yres = 0;
  1658. unsigned int i;
  1659. int ret;
  1660. /* If board code provides us with a list of available modes, make sure
  1661. * we use one of them. Find the mode closest to the requested one. The
  1662. * distance between two modes is defined as the size of the
  1663. * non-overlapping parts of the two rectangles.
  1664. */
  1665. for (i = 0; i < ch->cfg->num_modes; ++i) {
  1666. const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
  1667. unsigned int dist;
  1668. /* We can only round up. */
  1669. if (var->xres > mode->xres || var->yres > mode->yres)
  1670. continue;
  1671. dist = var->xres * var->yres + mode->xres * mode->yres
  1672. - 2 * min(var->xres, mode->xres)
  1673. * min(var->yres, mode->yres);
  1674. if (dist < best_dist) {
  1675. best_xres = mode->xres;
  1676. best_yres = mode->yres;
  1677. best_dist = dist;
  1678. }
  1679. }
  1680. /* If no available mode can be used, return an error. */
  1681. if (ch->cfg->num_modes != 0) {
  1682. if (best_dist == (unsigned int)-1)
  1683. return -EINVAL;
  1684. var->xres = best_xres;
  1685. var->yres = best_yres;
  1686. }
  1687. ret = __sh_mobile_lcdc_check_var(var, info);
  1688. if (ret < 0)
  1689. return ret;
  1690. /* only accept the forced_fourcc for dual channel configurations */
  1691. if (p->forced_fourcc &&
  1692. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1693. return -EINVAL;
  1694. return 0;
  1695. }
  1696. static int sh_mobile_lcdc_set_par(struct fb_info *info)
  1697. {
  1698. struct sh_mobile_lcdc_chan *ch = info->par;
  1699. int ret;
  1700. sh_mobile_lcdc_stop(ch->lcdc);
  1701. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1702. ch->colorspace = info->var.colorspace;
  1703. ch->xres = info->var.xres;
  1704. ch->xres_virtual = info->var.xres_virtual;
  1705. ch->yres = info->var.yres;
  1706. ch->yres_virtual = info->var.yres_virtual;
  1707. if (ch->format->yuv)
  1708. ch->pitch = info->var.xres_virtual;
  1709. else
  1710. ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
  1711. ret = sh_mobile_lcdc_start(ch->lcdc);
  1712. if (ret < 0)
  1713. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1714. info->fix.line_length = ch->pitch;
  1715. if (sh_mobile_format_is_fourcc(&info->var)) {
  1716. info->fix.type = FB_TYPE_FOURCC;
  1717. info->fix.visual = FB_VISUAL_FOURCC;
  1718. } else {
  1719. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1720. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1721. }
  1722. return ret;
  1723. }
  1724. /*
  1725. * Screen blanking. Behavior is as follows:
  1726. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1727. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1728. * FB_BLANK_VSYNC,
  1729. * FB_BLANK_HSYNC,
  1730. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1731. */
  1732. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1733. {
  1734. struct sh_mobile_lcdc_chan *ch = info->par;
  1735. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1736. /* blank the screen? */
  1737. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1738. struct fb_fillrect rect = {
  1739. .width = ch->xres,
  1740. .height = ch->yres,
  1741. };
  1742. sh_mobile_lcdc_fillrect(info, &rect);
  1743. }
  1744. /* turn clocks on? */
  1745. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1746. sh_mobile_lcdc_clk_on(p);
  1747. }
  1748. /* turn clocks off? */
  1749. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1750. /* make sure the screen is updated with the black fill before
  1751. * switching the clocks off. one vsync is not enough since
  1752. * blanking may occur in the middle of a refresh. deferred io
  1753. * mode will reenable the clocks and update the screen in time,
  1754. * so it does not need this. */
  1755. if (!info->fbdefio) {
  1756. sh_mobile_lcdc_wait_for_vsync(ch);
  1757. sh_mobile_lcdc_wait_for_vsync(ch);
  1758. }
  1759. sh_mobile_lcdc_clk_off(p);
  1760. }
  1761. ch->blank_status = blank;
  1762. return 0;
  1763. }
  1764. static struct fb_ops sh_mobile_lcdc_ops = {
  1765. .owner = THIS_MODULE,
  1766. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1767. .fb_read = fb_sys_read,
  1768. .fb_write = fb_sys_write,
  1769. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1770. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1771. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1772. .fb_blank = sh_mobile_lcdc_blank,
  1773. .fb_pan_display = sh_mobile_lcdc_pan,
  1774. .fb_ioctl = sh_mobile_lcdc_ioctl,
  1775. .fb_open = sh_mobile_lcdc_open,
  1776. .fb_release = sh_mobile_lcdc_release,
  1777. .fb_check_var = sh_mobile_lcdc_check_var,
  1778. .fb_set_par = sh_mobile_lcdc_set_par,
  1779. };
  1780. static void
  1781. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1782. {
  1783. if (ch->info && ch->info->dev)
  1784. unregister_framebuffer(ch->info);
  1785. }
  1786. static int __devinit
  1787. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1788. {
  1789. struct fb_info *info = ch->info;
  1790. int ret;
  1791. if (info->fbdefio) {
  1792. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1793. ch->fb_size >> PAGE_SHIFT);
  1794. if (!ch->sglist) {
  1795. dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
  1796. return -ENOMEM;
  1797. }
  1798. }
  1799. info->bl_dev = ch->bl;
  1800. ret = register_framebuffer(info);
  1801. if (ret < 0)
  1802. return ret;
  1803. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1804. dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
  1805. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1806. info->var.bits_per_pixel);
  1807. /* deferred io mode: disable clock to save power */
  1808. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1809. sh_mobile_lcdc_clk_off(ch->lcdc);
  1810. return ret;
  1811. }
  1812. static void
  1813. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1814. {
  1815. struct fb_info *info = ch->info;
  1816. if (!info || !info->device)
  1817. return;
  1818. if (ch->sglist)
  1819. vfree(ch->sglist);
  1820. fb_dealloc_cmap(&info->cmap);
  1821. framebuffer_release(info);
  1822. }
  1823. static int __devinit
  1824. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1825. const struct fb_videomode *mode,
  1826. unsigned int num_modes)
  1827. {
  1828. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1829. struct fb_var_screeninfo *var;
  1830. struct fb_info *info;
  1831. int ret;
  1832. /* Allocate and initialize the frame buffer device. Create the modes
  1833. * list and allocate the color map.
  1834. */
  1835. info = framebuffer_alloc(0, priv->dev);
  1836. if (info == NULL) {
  1837. dev_err(priv->dev, "unable to allocate fb_info\n");
  1838. return -ENOMEM;
  1839. }
  1840. ch->info = info;
  1841. info->flags = FBINFO_FLAG_DEFAULT;
  1842. info->fbops = &sh_mobile_lcdc_ops;
  1843. info->device = priv->dev;
  1844. info->screen_base = ch->fb_mem;
  1845. info->pseudo_palette = &ch->pseudo_palette;
  1846. info->par = ch;
  1847. fb_videomode_to_modelist(mode, num_modes, &info->modelist);
  1848. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1849. if (ret < 0) {
  1850. dev_err(priv->dev, "unable to allocate cmap\n");
  1851. return ret;
  1852. }
  1853. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1854. * for NV12 and NV21.
  1855. */
  1856. info->fix = sh_mobile_lcdc_fix;
  1857. info->fix.smem_start = ch->dma_handle;
  1858. info->fix.smem_len = ch->fb_size;
  1859. info->fix.line_length = ch->pitch;
  1860. if (ch->format->yuv)
  1861. info->fix.visual = FB_VISUAL_FOURCC;
  1862. else
  1863. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1864. switch (ch->format->fourcc) {
  1865. case V4L2_PIX_FMT_NV12:
  1866. case V4L2_PIX_FMT_NV21:
  1867. info->fix.ypanstep = 2;
  1868. case V4L2_PIX_FMT_NV16:
  1869. case V4L2_PIX_FMT_NV61:
  1870. info->fix.xpanstep = 2;
  1871. }
  1872. /* Initialize variable screen information using the first mode as
  1873. * default.
  1874. */
  1875. var = &info->var;
  1876. fb_videomode_to_var(var, mode);
  1877. var->width = ch->cfg->panel_cfg.width;
  1878. var->height = ch->cfg->panel_cfg.height;
  1879. var->xres_virtual = ch->xres_virtual;
  1880. var->yres_virtual = ch->yres_virtual;
  1881. var->activate = FB_ACTIVATE_NOW;
  1882. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1883. * for YUV formats.
  1884. */
  1885. if (!ch->format->yuv)
  1886. var->bits_per_pixel = ch->format->bpp;
  1887. else
  1888. var->grayscale = ch->format->fourcc;
  1889. ret = sh_mobile_lcdc_check_var(var, info);
  1890. if (ret)
  1891. return ret;
  1892. return 0;
  1893. }
  1894. /* -----------------------------------------------------------------------------
  1895. * Backlight
  1896. */
  1897. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1898. {
  1899. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1900. int brightness = bdev->props.brightness;
  1901. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1902. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1903. brightness = 0;
  1904. return ch->cfg->bl_info.set_brightness(brightness);
  1905. }
  1906. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1907. {
  1908. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1909. return ch->cfg->bl_info.get_brightness();
  1910. }
  1911. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1912. struct fb_info *info)
  1913. {
  1914. return (info->bl_dev == bdev);
  1915. }
  1916. static struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1917. .options = BL_CORE_SUSPENDRESUME,
  1918. .update_status = sh_mobile_lcdc_update_bl,
  1919. .get_brightness = sh_mobile_lcdc_get_brightness,
  1920. .check_fb = sh_mobile_lcdc_check_fb,
  1921. };
  1922. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1923. struct sh_mobile_lcdc_chan *ch)
  1924. {
  1925. struct backlight_device *bl;
  1926. bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
  1927. &sh_mobile_lcdc_bl_ops, NULL);
  1928. if (IS_ERR(bl)) {
  1929. dev_err(parent, "unable to register backlight device: %ld\n",
  1930. PTR_ERR(bl));
  1931. return NULL;
  1932. }
  1933. bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
  1934. bl->props.brightness = bl->props.max_brightness;
  1935. backlight_update_status(bl);
  1936. return bl;
  1937. }
  1938. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1939. {
  1940. backlight_device_unregister(bdev);
  1941. }
  1942. /* -----------------------------------------------------------------------------
  1943. * Power management
  1944. */
  1945. static int sh_mobile_lcdc_suspend(struct device *dev)
  1946. {
  1947. struct platform_device *pdev = to_platform_device(dev);
  1948. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1949. return 0;
  1950. }
  1951. static int sh_mobile_lcdc_resume(struct device *dev)
  1952. {
  1953. struct platform_device *pdev = to_platform_device(dev);
  1954. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1955. }
  1956. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1957. {
  1958. struct platform_device *pdev = to_platform_device(dev);
  1959. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1960. /* turn off LCDC hardware */
  1961. lcdc_write(priv, _LDCNT1R, 0);
  1962. return 0;
  1963. }
  1964. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1965. {
  1966. struct platform_device *pdev = to_platform_device(dev);
  1967. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1968. __sh_mobile_lcdc_start(priv);
  1969. return 0;
  1970. }
  1971. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1972. .suspend = sh_mobile_lcdc_suspend,
  1973. .resume = sh_mobile_lcdc_resume,
  1974. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1975. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1976. };
  1977. /* -----------------------------------------------------------------------------
  1978. * Framebuffer notifier
  1979. */
  1980. /* locking: called with info->lock held */
  1981. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1982. unsigned long action, void *data)
  1983. {
  1984. struct fb_event *event = data;
  1985. struct fb_info *info = event->info;
  1986. struct sh_mobile_lcdc_chan *ch = info->par;
  1987. if (&ch->lcdc->notifier != nb)
  1988. return NOTIFY_DONE;
  1989. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1990. __func__, action, event->data);
  1991. switch(action) {
  1992. case FB_EVENT_SUSPEND:
  1993. sh_mobile_lcdc_display_off(ch);
  1994. sh_mobile_lcdc_stop(ch->lcdc);
  1995. break;
  1996. case FB_EVENT_RESUME:
  1997. mutex_lock(&ch->open_lock);
  1998. sh_mobile_fb_reconfig(info);
  1999. mutex_unlock(&ch->open_lock);
  2000. sh_mobile_lcdc_display_on(ch);
  2001. sh_mobile_lcdc_start(ch->lcdc);
  2002. }
  2003. return NOTIFY_OK;
  2004. }
  2005. /* -----------------------------------------------------------------------------
  2006. * Probe/remove and driver init/exit
  2007. */
  2008. static const struct fb_videomode default_720p __devinitconst = {
  2009. .name = "HDMI 720p",
  2010. .xres = 1280,
  2011. .yres = 720,
  2012. .left_margin = 220,
  2013. .right_margin = 110,
  2014. .hsync_len = 40,
  2015. .upper_margin = 20,
  2016. .lower_margin = 5,
  2017. .vsync_len = 5,
  2018. .pixclock = 13468,
  2019. .refresh = 60,
  2020. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  2021. };
  2022. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  2023. {
  2024. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  2025. unsigned int i;
  2026. fb_unregister_client(&priv->notifier);
  2027. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
  2028. sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
  2029. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  2030. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  2031. sh_mobile_lcdc_stop(priv);
  2032. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
  2033. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2034. sh_mobile_lcdc_overlay_fb_cleanup(ovl);
  2035. if (ovl->fb_mem)
  2036. dma_free_coherent(&pdev->dev, ovl->fb_size,
  2037. ovl->fb_mem, ovl->dma_handle);
  2038. }
  2039. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2040. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2041. if (ch->tx_dev) {
  2042. ch->tx_dev->lcdc = NULL;
  2043. module_put(ch->cfg->tx_dev->dev.driver->owner);
  2044. }
  2045. sh_mobile_lcdc_channel_fb_cleanup(ch);
  2046. if (ch->fb_mem)
  2047. dma_free_coherent(&pdev->dev, ch->fb_size,
  2048. ch->fb_mem, ch->dma_handle);
  2049. }
  2050. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2051. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2052. if (ch->bl)
  2053. sh_mobile_lcdc_bl_remove(ch->bl);
  2054. mutex_destroy(&ch->open_lock);
  2055. }
  2056. if (priv->dot_clk) {
  2057. pm_runtime_disable(&pdev->dev);
  2058. clk_put(priv->dot_clk);
  2059. }
  2060. if (priv->base)
  2061. iounmap(priv->base);
  2062. if (priv->irq)
  2063. free_irq(priv->irq, priv);
  2064. kfree(priv);
  2065. return 0;
  2066. }
  2067. static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  2068. {
  2069. int interface_type = ch->cfg->interface_type;
  2070. switch (interface_type) {
  2071. case RGB8:
  2072. case RGB9:
  2073. case RGB12A:
  2074. case RGB12B:
  2075. case RGB16:
  2076. case RGB18:
  2077. case RGB24:
  2078. case SYS8A:
  2079. case SYS8B:
  2080. case SYS8C:
  2081. case SYS8D:
  2082. case SYS9:
  2083. case SYS12:
  2084. case SYS16A:
  2085. case SYS16B:
  2086. case SYS16C:
  2087. case SYS18:
  2088. case SYS24:
  2089. break;
  2090. default:
  2091. return -EINVAL;
  2092. }
  2093. /* SUBLCD only supports SYS interface */
  2094. if (lcdc_chan_is_sublcd(ch)) {
  2095. if (!(interface_type & LDMT1R_IFM))
  2096. return -EINVAL;
  2097. interface_type &= ~LDMT1R_IFM;
  2098. }
  2099. ch->ldmt1r_value = interface_type;
  2100. return 0;
  2101. }
  2102. static int __devinit
  2103. sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_priv *priv,
  2104. struct sh_mobile_lcdc_overlay *ovl)
  2105. {
  2106. const struct sh_mobile_lcdc_format_info *format;
  2107. int ret;
  2108. if (ovl->cfg->fourcc == 0)
  2109. return 0;
  2110. /* Validate the format. */
  2111. format = sh_mobile_format_info(ovl->cfg->fourcc);
  2112. if (format == NULL) {
  2113. dev_err(priv->dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
  2114. return -EINVAL;
  2115. }
  2116. ovl->enabled = false;
  2117. ovl->mode = LCDC_OVERLAY_BLEND;
  2118. ovl->alpha = 255;
  2119. ovl->rop3 = 0;
  2120. ovl->pos_x = 0;
  2121. ovl->pos_y = 0;
  2122. /* The default Y virtual resolution is twice the panel size to allow for
  2123. * double-buffering.
  2124. */
  2125. ovl->format = format;
  2126. ovl->xres = ovl->cfg->max_xres;
  2127. ovl->xres_virtual = ovl->xres;
  2128. ovl->yres = ovl->cfg->max_yres;
  2129. ovl->yres_virtual = ovl->yres * 2;
  2130. if (!format->yuv)
  2131. ovl->pitch = ovl->xres_virtual * format->bpp / 8;
  2132. else
  2133. ovl->pitch = ovl->xres_virtual;
  2134. /* Allocate frame buffer memory. */
  2135. ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
  2136. * format->bpp / 8 * 2;
  2137. ovl->fb_mem = dma_alloc_coherent(priv->dev, ovl->fb_size,
  2138. &ovl->dma_handle, GFP_KERNEL);
  2139. if (!ovl->fb_mem) {
  2140. dev_err(priv->dev, "unable to allocate buffer\n");
  2141. return -ENOMEM;
  2142. }
  2143. ret = sh_mobile_lcdc_overlay_fb_init(ovl);
  2144. if (ret < 0)
  2145. return ret;
  2146. return 0;
  2147. }
  2148. static int __devinit
  2149. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
  2150. struct sh_mobile_lcdc_chan *ch)
  2151. {
  2152. const struct sh_mobile_lcdc_format_info *format;
  2153. const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
  2154. const struct fb_videomode *max_mode;
  2155. const struct fb_videomode *mode;
  2156. unsigned int num_modes;
  2157. unsigned int max_size;
  2158. unsigned int i;
  2159. mutex_init(&ch->open_lock);
  2160. ch->notify = sh_mobile_lcdc_display_notify;
  2161. /* Validate the format. */
  2162. format = sh_mobile_format_info(cfg->fourcc);
  2163. if (format == NULL) {
  2164. dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  2165. return -EINVAL;
  2166. }
  2167. /* Iterate through the modes to validate them and find the highest
  2168. * resolution.
  2169. */
  2170. max_mode = NULL;
  2171. max_size = 0;
  2172. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  2173. unsigned int size = mode->yres * mode->xres;
  2174. /* NV12/NV21 buffers must have even number of lines */
  2175. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  2176. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  2177. dev_err(priv->dev, "yres must be multiple of 2 for "
  2178. "YCbCr420 mode.\n");
  2179. return -EINVAL;
  2180. }
  2181. if (size > max_size) {
  2182. max_mode = mode;
  2183. max_size = size;
  2184. }
  2185. }
  2186. if (!max_size)
  2187. max_size = MAX_XRES * MAX_YRES;
  2188. else
  2189. dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
  2190. max_mode->xres, max_mode->yres);
  2191. if (cfg->lcd_modes == NULL) {
  2192. mode = &default_720p;
  2193. num_modes = 1;
  2194. } else {
  2195. mode = cfg->lcd_modes;
  2196. num_modes = cfg->num_modes;
  2197. }
  2198. /* Use the first mode as default. The default Y virtual resolution is
  2199. * twice the panel size to allow for double-buffering.
  2200. */
  2201. ch->format = format;
  2202. ch->xres = mode->xres;
  2203. ch->xres_virtual = mode->xres;
  2204. ch->yres = mode->yres;
  2205. ch->yres_virtual = mode->yres * 2;
  2206. if (!format->yuv) {
  2207. ch->colorspace = V4L2_COLORSPACE_SRGB;
  2208. ch->pitch = ch->xres_virtual * format->bpp / 8;
  2209. } else {
  2210. ch->colorspace = V4L2_COLORSPACE_REC709;
  2211. ch->pitch = ch->xres_virtual;
  2212. }
  2213. ch->display.width = cfg->panel_cfg.width;
  2214. ch->display.height = cfg->panel_cfg.height;
  2215. ch->display.mode = *mode;
  2216. /* Allocate frame buffer memory. */
  2217. ch->fb_size = max_size * format->bpp / 8 * 2;
  2218. ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
  2219. GFP_KERNEL);
  2220. if (ch->fb_mem == NULL) {
  2221. dev_err(priv->dev, "unable to allocate buffer\n");
  2222. return -ENOMEM;
  2223. }
  2224. /* Initialize the transmitter device if present. */
  2225. if (cfg->tx_dev) {
  2226. if (!cfg->tx_dev->dev.driver ||
  2227. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  2228. dev_warn(priv->dev,
  2229. "unable to get transmitter device\n");
  2230. return -EINVAL;
  2231. }
  2232. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  2233. ch->tx_dev->lcdc = ch;
  2234. ch->tx_dev->def_mode = *mode;
  2235. }
  2236. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  2237. }
  2238. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  2239. {
  2240. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  2241. struct sh_mobile_lcdc_priv *priv;
  2242. struct resource *res;
  2243. int num_channels;
  2244. int error;
  2245. int i;
  2246. if (!pdata) {
  2247. dev_err(&pdev->dev, "no platform data defined\n");
  2248. return -EINVAL;
  2249. }
  2250. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2251. i = platform_get_irq(pdev, 0);
  2252. if (!res || i < 0) {
  2253. dev_err(&pdev->dev, "cannot get platform resources\n");
  2254. return -ENOENT;
  2255. }
  2256. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2257. if (!priv) {
  2258. dev_err(&pdev->dev, "cannot allocate device data\n");
  2259. return -ENOMEM;
  2260. }
  2261. priv->dev = &pdev->dev;
  2262. priv->meram_dev = pdata->meram_dev;
  2263. platform_set_drvdata(pdev, priv);
  2264. error = request_irq(i, sh_mobile_lcdc_irq, 0,
  2265. dev_name(&pdev->dev), priv);
  2266. if (error) {
  2267. dev_err(&pdev->dev, "unable to request irq\n");
  2268. goto err1;
  2269. }
  2270. priv->irq = i;
  2271. atomic_set(&priv->hw_usecnt, -1);
  2272. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  2273. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  2274. ch->lcdc = priv;
  2275. ch->cfg = &pdata->ch[i];
  2276. error = sh_mobile_lcdc_check_interface(ch);
  2277. if (error) {
  2278. dev_err(&pdev->dev, "unsupported interface type\n");
  2279. goto err1;
  2280. }
  2281. init_waitqueue_head(&ch->frame_end_wait);
  2282. init_completion(&ch->vsync_completion);
  2283. /* probe the backlight is there is one defined */
  2284. if (ch->cfg->bl_info.max_brightness)
  2285. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  2286. switch (pdata->ch[i].chan) {
  2287. case LCDC_CHAN_MAINLCD:
  2288. ch->enabled = LDCNT2R_ME;
  2289. ch->reg_offs = lcdc_offs_mainlcd;
  2290. num_channels++;
  2291. break;
  2292. case LCDC_CHAN_SUBLCD:
  2293. ch->enabled = LDCNT2R_SE;
  2294. ch->reg_offs = lcdc_offs_sublcd;
  2295. num_channels++;
  2296. break;
  2297. }
  2298. }
  2299. if (!num_channels) {
  2300. dev_err(&pdev->dev, "no channels defined\n");
  2301. error = -EINVAL;
  2302. goto err1;
  2303. }
  2304. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  2305. if (num_channels == 2)
  2306. priv->forced_fourcc = pdata->ch[0].fourcc;
  2307. priv->base = ioremap_nocache(res->start, resource_size(res));
  2308. if (!priv->base)
  2309. goto err1;
  2310. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  2311. if (error) {
  2312. dev_err(&pdev->dev, "unable to setup clocks\n");
  2313. goto err1;
  2314. }
  2315. /* Enable runtime PM. */
  2316. pm_runtime_enable(&pdev->dev);
  2317. for (i = 0; i < num_channels; i++) {
  2318. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2319. error = sh_mobile_lcdc_channel_init(priv, ch);
  2320. if (error)
  2321. goto err1;
  2322. }
  2323. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2324. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2325. ovl->cfg = &pdata->overlays[i];
  2326. ovl->channel = &priv->ch[0];
  2327. error = sh_mobile_lcdc_overlay_init(priv, ovl);
  2328. if (error)
  2329. goto err1;
  2330. }
  2331. error = sh_mobile_lcdc_start(priv);
  2332. if (error) {
  2333. dev_err(&pdev->dev, "unable to start hardware\n");
  2334. goto err1;
  2335. }
  2336. for (i = 0; i < num_channels; i++) {
  2337. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2338. error = sh_mobile_lcdc_channel_fb_register(ch);
  2339. if (error)
  2340. goto err1;
  2341. }
  2342. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2343. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2344. error = sh_mobile_lcdc_overlay_fb_register(ovl);
  2345. if (error)
  2346. goto err1;
  2347. }
  2348. /* Failure ignored */
  2349. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  2350. fb_register_client(&priv->notifier);
  2351. return 0;
  2352. err1:
  2353. sh_mobile_lcdc_remove(pdev);
  2354. return error;
  2355. }
  2356. static struct platform_driver sh_mobile_lcdc_driver = {
  2357. .driver = {
  2358. .name = "sh_mobile_lcdc_fb",
  2359. .owner = THIS_MODULE,
  2360. .pm = &sh_mobile_lcdc_dev_pm_ops,
  2361. },
  2362. .probe = sh_mobile_lcdc_probe,
  2363. .remove = sh_mobile_lcdc_remove,
  2364. };
  2365. module_platform_driver(sh_mobile_lcdc_driver);
  2366. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  2367. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  2368. MODULE_LICENSE("GPL v2");