sh_mobile_lcdcfb.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829
  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_offset: Current pan offset in bytes
  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_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->base_addr_y + ovl->xres
  761. * 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->base_addr_y + ch->xres * ch->yres_virtual;
  964. ch->line_size = ch->pitch;
  965. /* Enable MERAM if possible. */
  966. if (mdev == NULL || ch->cfg->meram_cfg == NULL)
  967. continue;
  968. /* Free the allocated MERAM cache. */
  969. if (ch->cache) {
  970. sh_mobile_meram_cache_free(mdev, ch->cache);
  971. ch->cache = NULL;
  972. }
  973. switch (ch->format->fourcc) {
  974. case V4L2_PIX_FMT_NV12:
  975. case V4L2_PIX_FMT_NV21:
  976. case V4L2_PIX_FMT_NV16:
  977. case V4L2_PIX_FMT_NV61:
  978. pixelformat = SH_MOBILE_MERAM_PF_NV;
  979. break;
  980. case V4L2_PIX_FMT_NV24:
  981. case V4L2_PIX_FMT_NV42:
  982. pixelformat = SH_MOBILE_MERAM_PF_NV24;
  983. break;
  984. case V4L2_PIX_FMT_RGB565:
  985. case V4L2_PIX_FMT_BGR24:
  986. case V4L2_PIX_FMT_BGR32:
  987. default:
  988. pixelformat = SH_MOBILE_MERAM_PF_RGB;
  989. break;
  990. }
  991. cache = sh_mobile_meram_cache_alloc(mdev, ch->cfg->meram_cfg,
  992. ch->pitch, ch->yres, pixelformat,
  993. &ch->line_size);
  994. if (!IS_ERR(cache)) {
  995. sh_mobile_meram_cache_update(mdev, cache,
  996. ch->base_addr_y, ch->base_addr_c,
  997. &ch->base_addr_y, &ch->base_addr_c);
  998. ch->cache = cache;
  999. }
  1000. }
  1001. for (k = 0; k < ARRAY_SIZE(priv->overlays); ++k) {
  1002. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[k];
  1003. sh_mobile_lcdc_overlay_setup(ovl);
  1004. }
  1005. /* Start the LCDC. */
  1006. __sh_mobile_lcdc_start(priv);
  1007. /* Setup deferred I/O, tell the board code to enable the panels, and
  1008. * turn backlight on.
  1009. */
  1010. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  1011. ch = &priv->ch[k];
  1012. if (!ch->enabled)
  1013. continue;
  1014. tmp = ch->cfg->sys_bus_cfg.deferred_io_msec;
  1015. if (ch->ldmt1r_value & LDMT1R_IFM && tmp) {
  1016. ch->defio.deferred_io = sh_mobile_lcdc_deferred_io;
  1017. ch->defio.delay = msecs_to_jiffies(tmp);
  1018. ch->info->fbdefio = &ch->defio;
  1019. fb_deferred_io_init(ch->info);
  1020. }
  1021. sh_mobile_lcdc_display_on(ch);
  1022. if (ch->bl) {
  1023. ch->bl->props.power = FB_BLANK_UNBLANK;
  1024. backlight_update_status(ch->bl);
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. static void sh_mobile_lcdc_stop(struct sh_mobile_lcdc_priv *priv)
  1030. {
  1031. struct sh_mobile_lcdc_chan *ch;
  1032. int k;
  1033. /* clean up deferred io and ask board code to disable panel */
  1034. for (k = 0; k < ARRAY_SIZE(priv->ch); k++) {
  1035. ch = &priv->ch[k];
  1036. if (!ch->enabled)
  1037. continue;
  1038. /* deferred io mode:
  1039. * flush frame, and wait for frame end interrupt
  1040. * clean up deferred io and enable clock
  1041. */
  1042. if (ch->info && ch->info->fbdefio) {
  1043. ch->frame_end = 0;
  1044. schedule_delayed_work(&ch->info->deferred_work, 0);
  1045. wait_event(ch->frame_end_wait, ch->frame_end);
  1046. fb_deferred_io_cleanup(ch->info);
  1047. ch->info->fbdefio = NULL;
  1048. sh_mobile_lcdc_clk_on(priv);
  1049. }
  1050. if (ch->bl) {
  1051. ch->bl->props.power = FB_BLANK_POWERDOWN;
  1052. backlight_update_status(ch->bl);
  1053. }
  1054. sh_mobile_lcdc_display_off(ch);
  1055. /* Free the MERAM cache. */
  1056. if (ch->cache) {
  1057. sh_mobile_meram_cache_free(priv->meram_dev, ch->cache);
  1058. ch->cache = 0;
  1059. }
  1060. }
  1061. /* stop the lcdc */
  1062. if (priv->started) {
  1063. sh_mobile_lcdc_start_stop(priv, 0);
  1064. priv->started = 0;
  1065. }
  1066. /* stop clocks */
  1067. for (k = 0; k < ARRAY_SIZE(priv->ch); k++)
  1068. if (priv->ch[k].enabled)
  1069. sh_mobile_lcdc_clk_off(priv);
  1070. }
  1071. static int __sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1072. struct fb_info *info)
  1073. {
  1074. if (var->xres > MAX_XRES || var->yres > MAX_YRES)
  1075. return -EINVAL;
  1076. /* Make sure the virtual resolution is at least as big as the visible
  1077. * resolution.
  1078. */
  1079. if (var->xres_virtual < var->xres)
  1080. var->xres_virtual = var->xres;
  1081. if (var->yres_virtual < var->yres)
  1082. var->yres_virtual = var->yres;
  1083. if (sh_mobile_format_is_fourcc(var)) {
  1084. const struct sh_mobile_lcdc_format_info *format;
  1085. format = sh_mobile_format_info(var->grayscale);
  1086. if (format == NULL)
  1087. return -EINVAL;
  1088. var->bits_per_pixel = format->bpp;
  1089. /* Default to RGB and JPEG color-spaces for RGB and YUV formats
  1090. * respectively.
  1091. */
  1092. if (!format->yuv)
  1093. var->colorspace = V4L2_COLORSPACE_SRGB;
  1094. else if (var->colorspace != V4L2_COLORSPACE_REC709)
  1095. var->colorspace = V4L2_COLORSPACE_JPEG;
  1096. } else {
  1097. if (var->bits_per_pixel <= 16) { /* RGB 565 */
  1098. var->bits_per_pixel = 16;
  1099. var->red.offset = 11;
  1100. var->red.length = 5;
  1101. var->green.offset = 5;
  1102. var->green.length = 6;
  1103. var->blue.offset = 0;
  1104. var->blue.length = 5;
  1105. var->transp.offset = 0;
  1106. var->transp.length = 0;
  1107. } else if (var->bits_per_pixel <= 24) { /* RGB 888 */
  1108. var->bits_per_pixel = 24;
  1109. var->red.offset = 16;
  1110. var->red.length = 8;
  1111. var->green.offset = 8;
  1112. var->green.length = 8;
  1113. var->blue.offset = 0;
  1114. var->blue.length = 8;
  1115. var->transp.offset = 0;
  1116. var->transp.length = 0;
  1117. } else if (var->bits_per_pixel <= 32) { /* RGBA 888 */
  1118. var->bits_per_pixel = 32;
  1119. var->red.offset = 16;
  1120. var->red.length = 8;
  1121. var->green.offset = 8;
  1122. var->green.length = 8;
  1123. var->blue.offset = 0;
  1124. var->blue.length = 8;
  1125. var->transp.offset = 24;
  1126. var->transp.length = 8;
  1127. } else
  1128. return -EINVAL;
  1129. var->red.msb_right = 0;
  1130. var->green.msb_right = 0;
  1131. var->blue.msb_right = 0;
  1132. var->transp.msb_right = 0;
  1133. }
  1134. /* Make sure we don't exceed our allocated memory. */
  1135. if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
  1136. info->fix.smem_len)
  1137. return -EINVAL;
  1138. return 0;
  1139. }
  1140. /* -----------------------------------------------------------------------------
  1141. * Frame buffer operations - Overlays
  1142. */
  1143. static ssize_t
  1144. overlay_alpha_show(struct device *dev, struct device_attribute *attr, char *buf)
  1145. {
  1146. struct fb_info *info = dev_get_drvdata(dev);
  1147. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1148. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->alpha);
  1149. }
  1150. static ssize_t
  1151. overlay_alpha_store(struct device *dev, struct device_attribute *attr,
  1152. const char *buf, size_t count)
  1153. {
  1154. struct fb_info *info = dev_get_drvdata(dev);
  1155. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1156. unsigned int alpha;
  1157. char *endp;
  1158. alpha = simple_strtoul(buf, &endp, 10);
  1159. if (isspace(*endp))
  1160. endp++;
  1161. if (endp - buf != count)
  1162. return -EINVAL;
  1163. if (alpha > 255)
  1164. return -EINVAL;
  1165. if (ovl->alpha != alpha) {
  1166. ovl->alpha = alpha;
  1167. if (ovl->mode == LCDC_OVERLAY_BLEND && ovl->enabled)
  1168. sh_mobile_lcdc_overlay_setup(ovl);
  1169. }
  1170. return count;
  1171. }
  1172. static ssize_t
  1173. overlay_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1174. {
  1175. struct fb_info *info = dev_get_drvdata(dev);
  1176. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1177. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->mode);
  1178. }
  1179. static ssize_t
  1180. overlay_mode_store(struct device *dev, struct device_attribute *attr,
  1181. const char *buf, size_t count)
  1182. {
  1183. struct fb_info *info = dev_get_drvdata(dev);
  1184. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1185. unsigned int mode;
  1186. char *endp;
  1187. mode = simple_strtoul(buf, &endp, 10);
  1188. if (isspace(*endp))
  1189. endp++;
  1190. if (endp - buf != count)
  1191. return -EINVAL;
  1192. if (mode != LCDC_OVERLAY_BLEND && mode != LCDC_OVERLAY_ROP3)
  1193. return -EINVAL;
  1194. if (ovl->mode != mode) {
  1195. ovl->mode = mode;
  1196. if (ovl->enabled)
  1197. sh_mobile_lcdc_overlay_setup(ovl);
  1198. }
  1199. return count;
  1200. }
  1201. static ssize_t
  1202. overlay_position_show(struct device *dev, struct device_attribute *attr,
  1203. char *buf)
  1204. {
  1205. struct fb_info *info = dev_get_drvdata(dev);
  1206. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1207. return scnprintf(buf, PAGE_SIZE, "%d,%d\n", ovl->pos_x, ovl->pos_y);
  1208. }
  1209. static ssize_t
  1210. overlay_position_store(struct device *dev, struct device_attribute *attr,
  1211. const char *buf, size_t count)
  1212. {
  1213. struct fb_info *info = dev_get_drvdata(dev);
  1214. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1215. char *endp;
  1216. int pos_x;
  1217. int pos_y;
  1218. pos_x = simple_strtol(buf, &endp, 10);
  1219. if (*endp != ',')
  1220. return -EINVAL;
  1221. pos_y = simple_strtol(endp + 1, &endp, 10);
  1222. if (isspace(*endp))
  1223. endp++;
  1224. if (endp - buf != count)
  1225. return -EINVAL;
  1226. if (ovl->pos_x != pos_x || ovl->pos_y != pos_y) {
  1227. ovl->pos_x = pos_x;
  1228. ovl->pos_y = pos_y;
  1229. if (ovl->enabled)
  1230. sh_mobile_lcdc_overlay_setup(ovl);
  1231. }
  1232. return count;
  1233. }
  1234. static ssize_t
  1235. overlay_rop3_show(struct device *dev, struct device_attribute *attr, char *buf)
  1236. {
  1237. struct fb_info *info = dev_get_drvdata(dev);
  1238. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1239. return scnprintf(buf, PAGE_SIZE, "%u\n", ovl->rop3);
  1240. }
  1241. static ssize_t
  1242. overlay_rop3_store(struct device *dev, struct device_attribute *attr,
  1243. const char *buf, size_t count)
  1244. {
  1245. struct fb_info *info = dev_get_drvdata(dev);
  1246. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1247. unsigned int rop3;
  1248. char *endp;
  1249. rop3 = !!simple_strtoul(buf, &endp, 10);
  1250. if (isspace(*endp))
  1251. endp++;
  1252. if (endp - buf != count)
  1253. return -EINVAL;
  1254. if (rop3 > 255)
  1255. return -EINVAL;
  1256. if (ovl->rop3 != rop3) {
  1257. ovl->rop3 = rop3;
  1258. if (ovl->mode == LCDC_OVERLAY_ROP3 && ovl->enabled)
  1259. sh_mobile_lcdc_overlay_setup(ovl);
  1260. }
  1261. return count;
  1262. }
  1263. static const struct device_attribute overlay_sysfs_attrs[] = {
  1264. __ATTR(ovl_alpha, S_IRUGO|S_IWUSR,
  1265. overlay_alpha_show, overlay_alpha_store),
  1266. __ATTR(ovl_mode, S_IRUGO|S_IWUSR,
  1267. overlay_mode_show, overlay_mode_store),
  1268. __ATTR(ovl_position, S_IRUGO|S_IWUSR,
  1269. overlay_position_show, overlay_position_store),
  1270. __ATTR(ovl_rop3, S_IRUGO|S_IWUSR,
  1271. overlay_rop3_show, overlay_rop3_store),
  1272. };
  1273. static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
  1274. .id = "SH Mobile LCDC",
  1275. .type = FB_TYPE_PACKED_PIXELS,
  1276. .visual = FB_VISUAL_TRUECOLOR,
  1277. .accel = FB_ACCEL_NONE,
  1278. .xpanstep = 0,
  1279. .ypanstep = 1,
  1280. .ywrapstep = 0,
  1281. .capabilities = FB_CAP_FOURCC,
  1282. };
  1283. static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
  1284. struct fb_info *info)
  1285. {
  1286. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1287. unsigned long base_addr_y;
  1288. unsigned long base_addr_c;
  1289. unsigned long pan_offset;
  1290. unsigned long c_offset;
  1291. if (!ovl->format->yuv)
  1292. pan_offset = var->yoffset * ovl->pitch
  1293. + var->xoffset * (ovl->format->bpp / 8);
  1294. else
  1295. pan_offset = var->yoffset * ovl->pitch + var->xoffset;
  1296. if (pan_offset == ovl->pan_offset)
  1297. return 0; /* No change, do nothing */
  1298. /* Set the source address for the next refresh */
  1299. base_addr_y = ovl->dma_handle + pan_offset;
  1300. ovl->base_addr_y = base_addr_y;
  1301. ovl->base_addr_c = base_addr_y;
  1302. if (ovl->format->yuv) {
  1303. /* Set Y offset */
  1304. c_offset = var->yoffset * ovl->pitch
  1305. * (ovl->format->bpp - 8) / 8;
  1306. base_addr_c = ovl->dma_handle
  1307. + ovl->xres * ovl->yres_virtual
  1308. + c_offset;
  1309. /* Set X offset */
  1310. if (ovl->format->fourcc == V4L2_PIX_FMT_NV24)
  1311. base_addr_c += 2 * var->xoffset;
  1312. else
  1313. base_addr_c += var->xoffset;
  1314. ovl->base_addr_c = base_addr_c;
  1315. }
  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. ovl->pan_offset = pan_offset;
  1319. return 0;
  1320. }
  1321. static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
  1322. unsigned long arg)
  1323. {
  1324. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1325. switch (cmd) {
  1326. case FBIO_WAITFORVSYNC:
  1327. return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
  1328. default:
  1329. return -ENOIOCTLCMD;
  1330. }
  1331. }
  1332. static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
  1333. struct fb_info *info)
  1334. {
  1335. return __sh_mobile_lcdc_check_var(var, info);
  1336. }
  1337. static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
  1338. {
  1339. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1340. ovl->format =
  1341. sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1342. ovl->xres = info->var.xres;
  1343. ovl->xres_virtual = info->var.xres_virtual;
  1344. ovl->yres = info->var.yres;
  1345. ovl->yres_virtual = info->var.yres_virtual;
  1346. if (ovl->format->yuv)
  1347. ovl->pitch = info->var.xres;
  1348. else
  1349. ovl->pitch = info->var.xres * ovl->format->bpp / 8;
  1350. sh_mobile_lcdc_overlay_setup(ovl);
  1351. info->fix.line_length = ovl->pitch;
  1352. if (sh_mobile_format_is_fourcc(&info->var)) {
  1353. info->fix.type = FB_TYPE_FOURCC;
  1354. info->fix.visual = FB_VISUAL_FOURCC;
  1355. } else {
  1356. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1357. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1358. }
  1359. return 0;
  1360. }
  1361. /* Overlay blanking. Disable the overlay when blanked. */
  1362. static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
  1363. {
  1364. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1365. ovl->enabled = !blank;
  1366. sh_mobile_lcdc_overlay_setup(ovl);
  1367. /* Prevent the backlight from receiving a blanking event by returning
  1368. * a non-zero value.
  1369. */
  1370. return 1;
  1371. }
  1372. static struct fb_ops sh_mobile_lcdc_overlay_ops = {
  1373. .owner = THIS_MODULE,
  1374. .fb_read = fb_sys_read,
  1375. .fb_write = fb_sys_write,
  1376. .fb_fillrect = sys_fillrect,
  1377. .fb_copyarea = sys_copyarea,
  1378. .fb_imageblit = sys_imageblit,
  1379. .fb_blank = sh_mobile_lcdc_overlay_blank,
  1380. .fb_pan_display = sh_mobile_lcdc_overlay_pan,
  1381. .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
  1382. .fb_check_var = sh_mobile_lcdc_overlay_check_var,
  1383. .fb_set_par = sh_mobile_lcdc_overlay_set_par,
  1384. };
  1385. static void
  1386. sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
  1387. {
  1388. struct fb_info *info = ovl->info;
  1389. if (info == NULL || info->dev == NULL)
  1390. return;
  1391. unregister_framebuffer(ovl->info);
  1392. }
  1393. static int __devinit
  1394. sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
  1395. {
  1396. struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
  1397. struct fb_info *info = ovl->info;
  1398. unsigned int i;
  1399. int ret;
  1400. if (info == NULL)
  1401. return 0;
  1402. ret = register_framebuffer(info);
  1403. if (ret < 0)
  1404. return ret;
  1405. dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
  1406. dev_name(lcdc->dev), ovl->index, info->var.xres,
  1407. info->var.yres, info->var.bits_per_pixel);
  1408. for (i = 0; i < ARRAY_SIZE(overlay_sysfs_attrs); ++i) {
  1409. ret = device_create_file(info->dev, &overlay_sysfs_attrs[i]);
  1410. if (ret < 0)
  1411. return ret;
  1412. }
  1413. return 0;
  1414. }
  1415. static void
  1416. sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
  1417. {
  1418. struct fb_info *info = ovl->info;
  1419. if (info == NULL || info->device == NULL)
  1420. return;
  1421. framebuffer_release(info);
  1422. }
  1423. static int __devinit
  1424. sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
  1425. {
  1426. struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
  1427. struct fb_var_screeninfo *var;
  1428. struct fb_info *info;
  1429. /* Allocate and initialize the frame buffer device. */
  1430. info = framebuffer_alloc(0, priv->dev);
  1431. if (info == NULL) {
  1432. dev_err(priv->dev, "unable to allocate fb_info\n");
  1433. return -ENOMEM;
  1434. }
  1435. ovl->info = info;
  1436. info->flags = FBINFO_FLAG_DEFAULT;
  1437. info->fbops = &sh_mobile_lcdc_overlay_ops;
  1438. info->device = priv->dev;
  1439. info->screen_base = ovl->fb_mem;
  1440. info->par = ovl;
  1441. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1442. * for NV12 and NV21.
  1443. */
  1444. info->fix = sh_mobile_lcdc_overlay_fix;
  1445. snprintf(info->fix.id, sizeof(info->fix.id),
  1446. "SH Mobile LCDC Overlay %u", ovl->index);
  1447. info->fix.smem_start = ovl->dma_handle;
  1448. info->fix.smem_len = ovl->fb_size;
  1449. info->fix.line_length = ovl->pitch;
  1450. if (ovl->format->yuv)
  1451. info->fix.visual = FB_VISUAL_FOURCC;
  1452. else
  1453. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1454. if (ovl->format->fourcc == V4L2_PIX_FMT_NV12 ||
  1455. ovl->format->fourcc == V4L2_PIX_FMT_NV21)
  1456. info->fix.ypanstep = 2;
  1457. /* Initialize variable screen information. */
  1458. var = &info->var;
  1459. memset(var, 0, sizeof(*var));
  1460. var->xres = ovl->xres;
  1461. var->yres = ovl->yres;
  1462. var->xres_virtual = ovl->xres_virtual;
  1463. var->yres_virtual = ovl->yres_virtual;
  1464. var->activate = FB_ACTIVATE_NOW;
  1465. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1466. * for YUV formats.
  1467. */
  1468. if (!ovl->format->yuv)
  1469. var->bits_per_pixel = ovl->format->bpp;
  1470. else
  1471. var->grayscale = ovl->format->fourcc;
  1472. return sh_mobile_lcdc_overlay_check_var(var, info);
  1473. }
  1474. /* -----------------------------------------------------------------------------
  1475. * Frame buffer operations - main frame buffer
  1476. */
  1477. static int sh_mobile_lcdc_setcolreg(u_int regno,
  1478. u_int red, u_int green, u_int blue,
  1479. u_int transp, struct fb_info *info)
  1480. {
  1481. u32 *palette = info->pseudo_palette;
  1482. if (regno >= PALETTE_NR)
  1483. return -EINVAL;
  1484. /* only FB_VISUAL_TRUECOLOR supported */
  1485. red >>= 16 - info->var.red.length;
  1486. green >>= 16 - info->var.green.length;
  1487. blue >>= 16 - info->var.blue.length;
  1488. transp >>= 16 - info->var.transp.length;
  1489. palette[regno] = (red << info->var.red.offset) |
  1490. (green << info->var.green.offset) |
  1491. (blue << info->var.blue.offset) |
  1492. (transp << info->var.transp.offset);
  1493. return 0;
  1494. }
  1495. static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  1496. .id = "SH Mobile LCDC",
  1497. .type = FB_TYPE_PACKED_PIXELS,
  1498. .visual = FB_VISUAL_TRUECOLOR,
  1499. .accel = FB_ACCEL_NONE,
  1500. .xpanstep = 0,
  1501. .ypanstep = 1,
  1502. .ywrapstep = 0,
  1503. .capabilities = FB_CAP_FOURCC,
  1504. };
  1505. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  1506. const struct fb_fillrect *rect)
  1507. {
  1508. sys_fillrect(info, rect);
  1509. sh_mobile_lcdc_deferred_io_touch(info);
  1510. }
  1511. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  1512. const struct fb_copyarea *area)
  1513. {
  1514. sys_copyarea(info, area);
  1515. sh_mobile_lcdc_deferred_io_touch(info);
  1516. }
  1517. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  1518. const struct fb_image *image)
  1519. {
  1520. sys_imageblit(info, image);
  1521. sh_mobile_lcdc_deferred_io_touch(info);
  1522. }
  1523. static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
  1524. struct fb_info *info)
  1525. {
  1526. struct sh_mobile_lcdc_chan *ch = info->par;
  1527. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1528. unsigned long ldrcntr;
  1529. unsigned long new_pan_offset;
  1530. unsigned long base_addr_y, base_addr_c = 0;
  1531. unsigned long c_offset;
  1532. if (!ch->format->yuv)
  1533. new_pan_offset = var->yoffset * ch->pitch
  1534. + var->xoffset * (ch->format->bpp / 8);
  1535. else
  1536. new_pan_offset = var->yoffset * ch->pitch + var->xoffset;
  1537. if (new_pan_offset == ch->pan_offset)
  1538. return 0; /* No change, do nothing */
  1539. ldrcntr = lcdc_read(priv, _LDRCNTR);
  1540. /* Set the source address for the next refresh */
  1541. base_addr_y = ch->dma_handle + new_pan_offset;
  1542. if (ch->format->yuv) {
  1543. /* Set y offset */
  1544. c_offset = var->yoffset * ch->pitch
  1545. * (ch->format->bpp - 8) / 8;
  1546. base_addr_c = ch->dma_handle + ch->xres * ch->yres_virtual
  1547. + c_offset;
  1548. /* Set x offset */
  1549. if (ch->format->fourcc == V4L2_PIX_FMT_NV24)
  1550. base_addr_c += 2 * var->xoffset;
  1551. else
  1552. base_addr_c += var->xoffset;
  1553. }
  1554. if (ch->cache)
  1555. sh_mobile_meram_cache_update(priv->meram_dev, ch->cache,
  1556. base_addr_y, base_addr_c,
  1557. &base_addr_y, &base_addr_c);
  1558. ch->base_addr_y = base_addr_y;
  1559. ch->base_addr_c = base_addr_c;
  1560. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  1561. if (ch->format->yuv)
  1562. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  1563. if (lcdc_chan_is_sublcd(ch))
  1564. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  1565. else
  1566. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  1567. ch->pan_offset = new_pan_offset;
  1568. sh_mobile_lcdc_deferred_io_touch(info);
  1569. return 0;
  1570. }
  1571. static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
  1572. unsigned long arg)
  1573. {
  1574. struct sh_mobile_lcdc_chan *ch = info->par;
  1575. int retval;
  1576. switch (cmd) {
  1577. case FBIO_WAITFORVSYNC:
  1578. retval = sh_mobile_lcdc_wait_for_vsync(ch);
  1579. break;
  1580. default:
  1581. retval = -ENOIOCTLCMD;
  1582. break;
  1583. }
  1584. return retval;
  1585. }
  1586. static void sh_mobile_fb_reconfig(struct fb_info *info)
  1587. {
  1588. struct sh_mobile_lcdc_chan *ch = info->par;
  1589. struct fb_var_screeninfo var;
  1590. struct fb_videomode mode;
  1591. struct fb_event event;
  1592. int evnt = FB_EVENT_MODE_CHANGE_ALL;
  1593. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  1594. /* More framebuffer users are active */
  1595. return;
  1596. fb_var_to_videomode(&mode, &info->var);
  1597. if (fb_mode_is_equal(&ch->display.mode, &mode))
  1598. return;
  1599. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  1600. var = info->var;
  1601. fb_videomode_to_var(&var, &ch->display.mode);
  1602. var.width = ch->display.width;
  1603. var.height = ch->display.height;
  1604. var.activate = FB_ACTIVATE_NOW;
  1605. if (fb_set_var(info, &var) < 0)
  1606. /* Couldn't reconfigure, hopefully, can continue as before */
  1607. return;
  1608. /*
  1609. * fb_set_var() calls the notifier change internally, only if
  1610. * FBINFO_MISC_USEREVENT flag is set. Since we do not want to fake a
  1611. * user event, we have to call the chain ourselves.
  1612. */
  1613. event.info = info;
  1614. event.data = &ch->display.mode;
  1615. fb_notifier_call_chain(evnt, &event);
  1616. }
  1617. /*
  1618. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  1619. * user == 1, or with console sem held, if user == 0.
  1620. */
  1621. static int sh_mobile_lcdc_release(struct fb_info *info, int user)
  1622. {
  1623. struct sh_mobile_lcdc_chan *ch = info->par;
  1624. mutex_lock(&ch->open_lock);
  1625. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1626. ch->use_count--;
  1627. /* Nothing to reconfigure, when called from fbcon */
  1628. if (user) {
  1629. console_lock();
  1630. sh_mobile_fb_reconfig(info);
  1631. console_unlock();
  1632. }
  1633. mutex_unlock(&ch->open_lock);
  1634. return 0;
  1635. }
  1636. static int sh_mobile_lcdc_open(struct fb_info *info, int user)
  1637. {
  1638. struct sh_mobile_lcdc_chan *ch = info->par;
  1639. mutex_lock(&ch->open_lock);
  1640. ch->use_count++;
  1641. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1642. mutex_unlock(&ch->open_lock);
  1643. return 0;
  1644. }
  1645. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1646. struct fb_info *info)
  1647. {
  1648. struct sh_mobile_lcdc_chan *ch = info->par;
  1649. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1650. unsigned int best_dist = (unsigned int)-1;
  1651. unsigned int best_xres = 0;
  1652. unsigned int best_yres = 0;
  1653. unsigned int i;
  1654. int ret;
  1655. /* If board code provides us with a list of available modes, make sure
  1656. * we use one of them. Find the mode closest to the requested one. The
  1657. * distance between two modes is defined as the size of the
  1658. * non-overlapping parts of the two rectangles.
  1659. */
  1660. for (i = 0; i < ch->cfg->num_modes; ++i) {
  1661. const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
  1662. unsigned int dist;
  1663. /* We can only round up. */
  1664. if (var->xres > mode->xres || var->yres > mode->yres)
  1665. continue;
  1666. dist = var->xres * var->yres + mode->xres * mode->yres
  1667. - 2 * min(var->xres, mode->xres)
  1668. * min(var->yres, mode->yres);
  1669. if (dist < best_dist) {
  1670. best_xres = mode->xres;
  1671. best_yres = mode->yres;
  1672. best_dist = dist;
  1673. }
  1674. }
  1675. /* If no available mode can be used, return an error. */
  1676. if (ch->cfg->num_modes != 0) {
  1677. if (best_dist == (unsigned int)-1)
  1678. return -EINVAL;
  1679. var->xres = best_xres;
  1680. var->yres = best_yres;
  1681. }
  1682. ret = __sh_mobile_lcdc_check_var(var, info);
  1683. if (ret < 0)
  1684. return ret;
  1685. /* only accept the forced_fourcc for dual channel configurations */
  1686. if (p->forced_fourcc &&
  1687. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1688. return -EINVAL;
  1689. return 0;
  1690. }
  1691. static int sh_mobile_lcdc_set_par(struct fb_info *info)
  1692. {
  1693. struct sh_mobile_lcdc_chan *ch = info->par;
  1694. int ret;
  1695. sh_mobile_lcdc_stop(ch->lcdc);
  1696. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1697. ch->colorspace = info->var.colorspace;
  1698. ch->xres = info->var.xres;
  1699. ch->xres_virtual = info->var.xres_virtual;
  1700. ch->yres = info->var.yres;
  1701. ch->yres_virtual = info->var.yres_virtual;
  1702. if (ch->format->yuv)
  1703. ch->pitch = info->var.xres;
  1704. else
  1705. ch->pitch = info->var.xres * ch->format->bpp / 8;
  1706. ret = sh_mobile_lcdc_start(ch->lcdc);
  1707. if (ret < 0)
  1708. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1709. info->fix.line_length = ch->pitch;
  1710. if (sh_mobile_format_is_fourcc(&info->var)) {
  1711. info->fix.type = FB_TYPE_FOURCC;
  1712. info->fix.visual = FB_VISUAL_FOURCC;
  1713. } else {
  1714. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1715. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1716. }
  1717. return ret;
  1718. }
  1719. /*
  1720. * Screen blanking. Behavior is as follows:
  1721. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1722. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1723. * FB_BLANK_VSYNC,
  1724. * FB_BLANK_HSYNC,
  1725. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1726. */
  1727. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1728. {
  1729. struct sh_mobile_lcdc_chan *ch = info->par;
  1730. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1731. /* blank the screen? */
  1732. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1733. struct fb_fillrect rect = {
  1734. .width = ch->xres,
  1735. .height = ch->yres,
  1736. };
  1737. sh_mobile_lcdc_fillrect(info, &rect);
  1738. }
  1739. /* turn clocks on? */
  1740. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1741. sh_mobile_lcdc_clk_on(p);
  1742. }
  1743. /* turn clocks off? */
  1744. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1745. /* make sure the screen is updated with the black fill before
  1746. * switching the clocks off. one vsync is not enough since
  1747. * blanking may occur in the middle of a refresh. deferred io
  1748. * mode will reenable the clocks and update the screen in time,
  1749. * so it does not need this. */
  1750. if (!info->fbdefio) {
  1751. sh_mobile_lcdc_wait_for_vsync(ch);
  1752. sh_mobile_lcdc_wait_for_vsync(ch);
  1753. }
  1754. sh_mobile_lcdc_clk_off(p);
  1755. }
  1756. ch->blank_status = blank;
  1757. return 0;
  1758. }
  1759. static struct fb_ops sh_mobile_lcdc_ops = {
  1760. .owner = THIS_MODULE,
  1761. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1762. .fb_read = fb_sys_read,
  1763. .fb_write = fb_sys_write,
  1764. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1765. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1766. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1767. .fb_blank = sh_mobile_lcdc_blank,
  1768. .fb_pan_display = sh_mobile_lcdc_pan,
  1769. .fb_ioctl = sh_mobile_lcdc_ioctl,
  1770. .fb_open = sh_mobile_lcdc_open,
  1771. .fb_release = sh_mobile_lcdc_release,
  1772. .fb_check_var = sh_mobile_lcdc_check_var,
  1773. .fb_set_par = sh_mobile_lcdc_set_par,
  1774. };
  1775. static void
  1776. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1777. {
  1778. if (ch->info && ch->info->dev)
  1779. unregister_framebuffer(ch->info);
  1780. }
  1781. static int __devinit
  1782. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1783. {
  1784. struct fb_info *info = ch->info;
  1785. int ret;
  1786. if (info->fbdefio) {
  1787. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1788. ch->fb_size >> PAGE_SHIFT);
  1789. if (!ch->sglist) {
  1790. dev_err(ch->lcdc->dev, "cannot allocate sglist\n");
  1791. return -ENOMEM;
  1792. }
  1793. }
  1794. info->bl_dev = ch->bl;
  1795. ret = register_framebuffer(info);
  1796. if (ret < 0)
  1797. return ret;
  1798. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1799. dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
  1800. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1801. info->var.bits_per_pixel);
  1802. /* deferred io mode: disable clock to save power */
  1803. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1804. sh_mobile_lcdc_clk_off(ch->lcdc);
  1805. return ret;
  1806. }
  1807. static void
  1808. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1809. {
  1810. struct fb_info *info = ch->info;
  1811. if (!info || !info->device)
  1812. return;
  1813. if (ch->sglist)
  1814. vfree(ch->sglist);
  1815. fb_dealloc_cmap(&info->cmap);
  1816. framebuffer_release(info);
  1817. }
  1818. static int __devinit
  1819. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1820. const struct fb_videomode *mode,
  1821. unsigned int num_modes)
  1822. {
  1823. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1824. struct fb_var_screeninfo *var;
  1825. struct fb_info *info;
  1826. int ret;
  1827. /* Allocate and initialize the frame buffer device. Create the modes
  1828. * list and allocate the color map.
  1829. */
  1830. info = framebuffer_alloc(0, priv->dev);
  1831. if (info == NULL) {
  1832. dev_err(priv->dev, "unable to allocate fb_info\n");
  1833. return -ENOMEM;
  1834. }
  1835. ch->info = info;
  1836. info->flags = FBINFO_FLAG_DEFAULT;
  1837. info->fbops = &sh_mobile_lcdc_ops;
  1838. info->device = priv->dev;
  1839. info->screen_base = ch->fb_mem;
  1840. info->pseudo_palette = &ch->pseudo_palette;
  1841. info->par = ch;
  1842. fb_videomode_to_modelist(mode, num_modes, &info->modelist);
  1843. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1844. if (ret < 0) {
  1845. dev_err(priv->dev, "unable to allocate cmap\n");
  1846. return ret;
  1847. }
  1848. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1849. * for NV12 and NV21.
  1850. */
  1851. info->fix = sh_mobile_lcdc_fix;
  1852. info->fix.smem_start = ch->dma_handle;
  1853. info->fix.smem_len = ch->fb_size;
  1854. info->fix.line_length = ch->pitch;
  1855. if (ch->format->yuv)
  1856. info->fix.visual = FB_VISUAL_FOURCC;
  1857. else
  1858. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1859. if (ch->format->fourcc == V4L2_PIX_FMT_NV12 ||
  1860. ch->format->fourcc == V4L2_PIX_FMT_NV21)
  1861. info->fix.ypanstep = 2;
  1862. /* Initialize variable screen information using the first mode as
  1863. * default. The default Y virtual resolution is twice the panel size to
  1864. * allow for double-buffering.
  1865. */
  1866. var = &info->var;
  1867. fb_videomode_to_var(var, mode);
  1868. var->width = ch->cfg->panel_cfg.width;
  1869. var->height = ch->cfg->panel_cfg.height;
  1870. var->yres_virtual = var->yres * 2;
  1871. var->activate = FB_ACTIVATE_NOW;
  1872. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1873. * for YUV formats.
  1874. */
  1875. if (!ch->format->yuv)
  1876. var->bits_per_pixel = ch->format->bpp;
  1877. else
  1878. var->grayscale = ch->format->fourcc;
  1879. ret = sh_mobile_lcdc_check_var(var, info);
  1880. if (ret)
  1881. return ret;
  1882. return 0;
  1883. }
  1884. /* -----------------------------------------------------------------------------
  1885. * Backlight
  1886. */
  1887. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1888. {
  1889. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1890. int brightness = bdev->props.brightness;
  1891. if (bdev->props.power != FB_BLANK_UNBLANK ||
  1892. bdev->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  1893. brightness = 0;
  1894. return ch->cfg->bl_info.set_brightness(brightness);
  1895. }
  1896. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1897. {
  1898. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1899. return ch->cfg->bl_info.get_brightness();
  1900. }
  1901. static int sh_mobile_lcdc_check_fb(struct backlight_device *bdev,
  1902. struct fb_info *info)
  1903. {
  1904. return (info->bl_dev == bdev);
  1905. }
  1906. static struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1907. .options = BL_CORE_SUSPENDRESUME,
  1908. .update_status = sh_mobile_lcdc_update_bl,
  1909. .get_brightness = sh_mobile_lcdc_get_brightness,
  1910. .check_fb = sh_mobile_lcdc_check_fb,
  1911. };
  1912. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1913. struct sh_mobile_lcdc_chan *ch)
  1914. {
  1915. struct backlight_device *bl;
  1916. bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
  1917. &sh_mobile_lcdc_bl_ops, NULL);
  1918. if (IS_ERR(bl)) {
  1919. dev_err(parent, "unable to register backlight device: %ld\n",
  1920. PTR_ERR(bl));
  1921. return NULL;
  1922. }
  1923. bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
  1924. bl->props.brightness = bl->props.max_brightness;
  1925. backlight_update_status(bl);
  1926. return bl;
  1927. }
  1928. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1929. {
  1930. backlight_device_unregister(bdev);
  1931. }
  1932. /* -----------------------------------------------------------------------------
  1933. * Power management
  1934. */
  1935. static int sh_mobile_lcdc_suspend(struct device *dev)
  1936. {
  1937. struct platform_device *pdev = to_platform_device(dev);
  1938. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1939. return 0;
  1940. }
  1941. static int sh_mobile_lcdc_resume(struct device *dev)
  1942. {
  1943. struct platform_device *pdev = to_platform_device(dev);
  1944. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1945. }
  1946. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1947. {
  1948. struct platform_device *pdev = to_platform_device(dev);
  1949. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1950. /* turn off LCDC hardware */
  1951. lcdc_write(priv, _LDCNT1R, 0);
  1952. return 0;
  1953. }
  1954. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1955. {
  1956. struct platform_device *pdev = to_platform_device(dev);
  1957. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1958. __sh_mobile_lcdc_start(priv);
  1959. return 0;
  1960. }
  1961. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1962. .suspend = sh_mobile_lcdc_suspend,
  1963. .resume = sh_mobile_lcdc_resume,
  1964. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1965. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1966. };
  1967. /* -----------------------------------------------------------------------------
  1968. * Framebuffer notifier
  1969. */
  1970. /* locking: called with info->lock held */
  1971. static int sh_mobile_lcdc_notify(struct notifier_block *nb,
  1972. unsigned long action, void *data)
  1973. {
  1974. struct fb_event *event = data;
  1975. struct fb_info *info = event->info;
  1976. struct sh_mobile_lcdc_chan *ch = info->par;
  1977. if (&ch->lcdc->notifier != nb)
  1978. return NOTIFY_DONE;
  1979. dev_dbg(info->dev, "%s(): action = %lu, data = %p\n",
  1980. __func__, action, event->data);
  1981. switch(action) {
  1982. case FB_EVENT_SUSPEND:
  1983. sh_mobile_lcdc_display_off(ch);
  1984. sh_mobile_lcdc_stop(ch->lcdc);
  1985. break;
  1986. case FB_EVENT_RESUME:
  1987. mutex_lock(&ch->open_lock);
  1988. sh_mobile_fb_reconfig(info);
  1989. mutex_unlock(&ch->open_lock);
  1990. sh_mobile_lcdc_display_on(ch);
  1991. sh_mobile_lcdc_start(ch->lcdc);
  1992. }
  1993. return NOTIFY_OK;
  1994. }
  1995. /* -----------------------------------------------------------------------------
  1996. * Probe/remove and driver init/exit
  1997. */
  1998. static const struct fb_videomode default_720p __devinitconst = {
  1999. .name = "HDMI 720p",
  2000. .xres = 1280,
  2001. .yres = 720,
  2002. .left_margin = 220,
  2003. .right_margin = 110,
  2004. .hsync_len = 40,
  2005. .upper_margin = 20,
  2006. .lower_margin = 5,
  2007. .vsync_len = 5,
  2008. .pixclock = 13468,
  2009. .refresh = 60,
  2010. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  2011. };
  2012. static int sh_mobile_lcdc_remove(struct platform_device *pdev)
  2013. {
  2014. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  2015. unsigned int i;
  2016. fb_unregister_client(&priv->notifier);
  2017. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
  2018. sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
  2019. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  2020. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  2021. sh_mobile_lcdc_stop(priv);
  2022. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
  2023. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2024. sh_mobile_lcdc_overlay_fb_cleanup(ovl);
  2025. if (ovl->fb_mem)
  2026. dma_free_coherent(&pdev->dev, ovl->fb_size,
  2027. ovl->fb_mem, ovl->dma_handle);
  2028. }
  2029. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2030. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2031. if (ch->tx_dev) {
  2032. ch->tx_dev->lcdc = NULL;
  2033. module_put(ch->cfg->tx_dev->dev.driver->owner);
  2034. }
  2035. sh_mobile_lcdc_channel_fb_cleanup(ch);
  2036. if (ch->fb_mem)
  2037. dma_free_coherent(&pdev->dev, ch->fb_size,
  2038. ch->fb_mem, ch->dma_handle);
  2039. }
  2040. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  2041. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2042. if (ch->bl)
  2043. sh_mobile_lcdc_bl_remove(ch->bl);
  2044. mutex_destroy(&ch->open_lock);
  2045. }
  2046. if (priv->dot_clk) {
  2047. pm_runtime_disable(&pdev->dev);
  2048. clk_put(priv->dot_clk);
  2049. }
  2050. if (priv->base)
  2051. iounmap(priv->base);
  2052. if (priv->irq)
  2053. free_irq(priv->irq, priv);
  2054. kfree(priv);
  2055. return 0;
  2056. }
  2057. static int __devinit sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  2058. {
  2059. int interface_type = ch->cfg->interface_type;
  2060. switch (interface_type) {
  2061. case RGB8:
  2062. case RGB9:
  2063. case RGB12A:
  2064. case RGB12B:
  2065. case RGB16:
  2066. case RGB18:
  2067. case RGB24:
  2068. case SYS8A:
  2069. case SYS8B:
  2070. case SYS8C:
  2071. case SYS8D:
  2072. case SYS9:
  2073. case SYS12:
  2074. case SYS16A:
  2075. case SYS16B:
  2076. case SYS16C:
  2077. case SYS18:
  2078. case SYS24:
  2079. break;
  2080. default:
  2081. return -EINVAL;
  2082. }
  2083. /* SUBLCD only supports SYS interface */
  2084. if (lcdc_chan_is_sublcd(ch)) {
  2085. if (!(interface_type & LDMT1R_IFM))
  2086. return -EINVAL;
  2087. interface_type &= ~LDMT1R_IFM;
  2088. }
  2089. ch->ldmt1r_value = interface_type;
  2090. return 0;
  2091. }
  2092. static int __devinit
  2093. sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_priv *priv,
  2094. struct sh_mobile_lcdc_overlay *ovl)
  2095. {
  2096. const struct sh_mobile_lcdc_format_info *format;
  2097. int ret;
  2098. if (ovl->cfg->fourcc == 0)
  2099. return 0;
  2100. /* Validate the format. */
  2101. format = sh_mobile_format_info(ovl->cfg->fourcc);
  2102. if (format == NULL) {
  2103. dev_err(priv->dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
  2104. return -EINVAL;
  2105. }
  2106. ovl->enabled = false;
  2107. ovl->mode = LCDC_OVERLAY_BLEND;
  2108. ovl->alpha = 255;
  2109. ovl->rop3 = 0;
  2110. ovl->pos_x = 0;
  2111. ovl->pos_y = 0;
  2112. /* The default Y virtual resolution is twice the panel size to allow for
  2113. * double-buffering.
  2114. */
  2115. ovl->format = format;
  2116. ovl->xres = ovl->cfg->max_xres;
  2117. ovl->xres_virtual = ovl->xres;
  2118. ovl->yres = ovl->cfg->max_yres;
  2119. ovl->yres_virtual = ovl->yres * 2;
  2120. if (!format->yuv)
  2121. ovl->pitch = ovl->xres * format->bpp / 8;
  2122. else
  2123. ovl->pitch = ovl->xres;
  2124. /* Allocate frame buffer memory. */
  2125. ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
  2126. * format->bpp / 8 * 2;
  2127. ovl->fb_mem = dma_alloc_coherent(priv->dev, ovl->fb_size,
  2128. &ovl->dma_handle, GFP_KERNEL);
  2129. if (!ovl->fb_mem) {
  2130. dev_err(priv->dev, "unable to allocate buffer\n");
  2131. return -ENOMEM;
  2132. }
  2133. ret = sh_mobile_lcdc_overlay_fb_init(ovl);
  2134. if (ret < 0)
  2135. return ret;
  2136. return 0;
  2137. }
  2138. static int __devinit
  2139. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_priv *priv,
  2140. struct sh_mobile_lcdc_chan *ch)
  2141. {
  2142. const struct sh_mobile_lcdc_format_info *format;
  2143. const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
  2144. const struct fb_videomode *max_mode;
  2145. const struct fb_videomode *mode;
  2146. unsigned int num_modes;
  2147. unsigned int max_size;
  2148. unsigned int i;
  2149. mutex_init(&ch->open_lock);
  2150. ch->notify = sh_mobile_lcdc_display_notify;
  2151. /* Validate the format. */
  2152. format = sh_mobile_format_info(cfg->fourcc);
  2153. if (format == NULL) {
  2154. dev_err(priv->dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  2155. return -EINVAL;
  2156. }
  2157. /* Iterate through the modes to validate them and find the highest
  2158. * resolution.
  2159. */
  2160. max_mode = NULL;
  2161. max_size = 0;
  2162. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  2163. unsigned int size = mode->yres * mode->xres;
  2164. /* NV12/NV21 buffers must have even number of lines */
  2165. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  2166. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  2167. dev_err(priv->dev, "yres must be multiple of 2 for "
  2168. "YCbCr420 mode.\n");
  2169. return -EINVAL;
  2170. }
  2171. if (size > max_size) {
  2172. max_mode = mode;
  2173. max_size = size;
  2174. }
  2175. }
  2176. if (!max_size)
  2177. max_size = MAX_XRES * MAX_YRES;
  2178. else
  2179. dev_dbg(priv->dev, "Found largest videomode %ux%u\n",
  2180. max_mode->xres, max_mode->yres);
  2181. if (cfg->lcd_modes == NULL) {
  2182. mode = &default_720p;
  2183. num_modes = 1;
  2184. } else {
  2185. mode = cfg->lcd_modes;
  2186. num_modes = cfg->num_modes;
  2187. }
  2188. /* Use the first mode as default. */
  2189. ch->format = format;
  2190. ch->xres = mode->xres;
  2191. ch->xres_virtual = mode->xres;
  2192. ch->yres = mode->yres;
  2193. ch->yres_virtual = mode->yres * 2;
  2194. if (!format->yuv) {
  2195. ch->colorspace = V4L2_COLORSPACE_SRGB;
  2196. ch->pitch = ch->xres * format->bpp / 8;
  2197. } else {
  2198. ch->colorspace = V4L2_COLORSPACE_REC709;
  2199. ch->pitch = ch->xres;
  2200. }
  2201. ch->display.width = cfg->panel_cfg.width;
  2202. ch->display.height = cfg->panel_cfg.height;
  2203. ch->display.mode = *mode;
  2204. /* Allocate frame buffer memory. */
  2205. ch->fb_size = max_size * format->bpp / 8 * 2;
  2206. ch->fb_mem = dma_alloc_coherent(priv->dev, ch->fb_size, &ch->dma_handle,
  2207. GFP_KERNEL);
  2208. if (ch->fb_mem == NULL) {
  2209. dev_err(priv->dev, "unable to allocate buffer\n");
  2210. return -ENOMEM;
  2211. }
  2212. /* Initialize the transmitter device if present. */
  2213. if (cfg->tx_dev) {
  2214. if (!cfg->tx_dev->dev.driver ||
  2215. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  2216. dev_warn(priv->dev,
  2217. "unable to get transmitter device\n");
  2218. return -EINVAL;
  2219. }
  2220. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  2221. ch->tx_dev->lcdc = ch;
  2222. ch->tx_dev->def_mode = *mode;
  2223. }
  2224. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  2225. }
  2226. static int __devinit sh_mobile_lcdc_probe(struct platform_device *pdev)
  2227. {
  2228. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  2229. struct sh_mobile_lcdc_priv *priv;
  2230. struct resource *res;
  2231. int num_channels;
  2232. int error;
  2233. int i;
  2234. if (!pdata) {
  2235. dev_err(&pdev->dev, "no platform data defined\n");
  2236. return -EINVAL;
  2237. }
  2238. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2239. i = platform_get_irq(pdev, 0);
  2240. if (!res || i < 0) {
  2241. dev_err(&pdev->dev, "cannot get platform resources\n");
  2242. return -ENOENT;
  2243. }
  2244. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  2245. if (!priv) {
  2246. dev_err(&pdev->dev, "cannot allocate device data\n");
  2247. return -ENOMEM;
  2248. }
  2249. priv->dev = &pdev->dev;
  2250. priv->meram_dev = pdata->meram_dev;
  2251. platform_set_drvdata(pdev, priv);
  2252. error = request_irq(i, sh_mobile_lcdc_irq, 0,
  2253. dev_name(&pdev->dev), priv);
  2254. if (error) {
  2255. dev_err(&pdev->dev, "unable to request irq\n");
  2256. goto err1;
  2257. }
  2258. priv->irq = i;
  2259. atomic_set(&priv->hw_usecnt, -1);
  2260. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  2261. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  2262. ch->lcdc = priv;
  2263. ch->cfg = &pdata->ch[i];
  2264. error = sh_mobile_lcdc_check_interface(ch);
  2265. if (error) {
  2266. dev_err(&pdev->dev, "unsupported interface type\n");
  2267. goto err1;
  2268. }
  2269. init_waitqueue_head(&ch->frame_end_wait);
  2270. init_completion(&ch->vsync_completion);
  2271. ch->pan_offset = 0;
  2272. /* probe the backlight is there is one defined */
  2273. if (ch->cfg->bl_info.max_brightness)
  2274. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  2275. switch (pdata->ch[i].chan) {
  2276. case LCDC_CHAN_MAINLCD:
  2277. ch->enabled = LDCNT2R_ME;
  2278. ch->reg_offs = lcdc_offs_mainlcd;
  2279. num_channels++;
  2280. break;
  2281. case LCDC_CHAN_SUBLCD:
  2282. ch->enabled = LDCNT2R_SE;
  2283. ch->reg_offs = lcdc_offs_sublcd;
  2284. num_channels++;
  2285. break;
  2286. }
  2287. }
  2288. if (!num_channels) {
  2289. dev_err(&pdev->dev, "no channels defined\n");
  2290. error = -EINVAL;
  2291. goto err1;
  2292. }
  2293. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  2294. if (num_channels == 2)
  2295. priv->forced_fourcc = pdata->ch[0].fourcc;
  2296. priv->base = ioremap_nocache(res->start, resource_size(res));
  2297. if (!priv->base)
  2298. goto err1;
  2299. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  2300. if (error) {
  2301. dev_err(&pdev->dev, "unable to setup clocks\n");
  2302. goto err1;
  2303. }
  2304. /* Enable runtime PM. */
  2305. pm_runtime_enable(&pdev->dev);
  2306. for (i = 0; i < num_channels; i++) {
  2307. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2308. error = sh_mobile_lcdc_channel_init(priv, ch);
  2309. if (error)
  2310. goto err1;
  2311. }
  2312. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2313. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2314. ovl->cfg = &pdata->overlays[i];
  2315. ovl->channel = &priv->ch[0];
  2316. error = sh_mobile_lcdc_overlay_init(priv, ovl);
  2317. if (error)
  2318. goto err1;
  2319. }
  2320. error = sh_mobile_lcdc_start(priv);
  2321. if (error) {
  2322. dev_err(&pdev->dev, "unable to start hardware\n");
  2323. goto err1;
  2324. }
  2325. for (i = 0; i < num_channels; i++) {
  2326. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2327. error = sh_mobile_lcdc_channel_fb_register(ch);
  2328. if (error)
  2329. goto err1;
  2330. }
  2331. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2332. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2333. error = sh_mobile_lcdc_overlay_fb_register(ovl);
  2334. if (error)
  2335. goto err1;
  2336. }
  2337. /* Failure ignored */
  2338. priv->notifier.notifier_call = sh_mobile_lcdc_notify;
  2339. fb_register_client(&priv->notifier);
  2340. return 0;
  2341. err1:
  2342. sh_mobile_lcdc_remove(pdev);
  2343. return error;
  2344. }
  2345. static struct platform_driver sh_mobile_lcdc_driver = {
  2346. .driver = {
  2347. .name = "sh_mobile_lcdc_fb",
  2348. .owner = THIS_MODULE,
  2349. .pm = &sh_mobile_lcdc_dev_pm_ops,
  2350. },
  2351. .probe = sh_mobile_lcdc_probe,
  2352. .remove = sh_mobile_lcdc_remove,
  2353. };
  2354. module_platform_driver(sh_mobile_lcdc_driver);
  2355. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  2356. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  2357. MODULE_LICENSE("GPL v2");