sh_mobile_lcdcfb.c 66 KB

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