sh_mobile_lcdcfb.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  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 | LDBBSIFR_RPKF_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 = BACKLIGHT_POWER_ON;
  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 = BACKLIGHT_POWER_OFF;
  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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 DEVICE_ATTR_RW(overlay_alpha);
  1145. static DEVICE_ATTR_RW(overlay_mode);
  1146. static DEVICE_ATTR_RW(overlay_position);
  1147. static DEVICE_ATTR_RW(overlay_rop3);
  1148. static struct attribute *overlay_sysfs_attrs[] __maybe_unused = {
  1149. &dev_attr_overlay_alpha.attr,
  1150. &dev_attr_overlay_mode.attr,
  1151. &dev_attr_overlay_position.attr,
  1152. &dev_attr_overlay_rop3.attr,
  1153. NULL,
  1154. };
  1155. #ifdef CONFIG_FB_DEVICE
  1156. ATTRIBUTE_GROUPS(overlay_sysfs);
  1157. #endif
  1158. static const struct fb_fix_screeninfo sh_mobile_lcdc_overlay_fix = {
  1159. .id = "SH Mobile LCDC",
  1160. .type = FB_TYPE_PACKED_PIXELS,
  1161. .visual = FB_VISUAL_TRUECOLOR,
  1162. .accel = FB_ACCEL_NONE,
  1163. .xpanstep = 1,
  1164. .ypanstep = 1,
  1165. .ywrapstep = 0,
  1166. .capabilities = FB_CAP_FOURCC,
  1167. };
  1168. static int sh_mobile_lcdc_overlay_pan(struct fb_var_screeninfo *var,
  1169. struct fb_info *info)
  1170. {
  1171. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1172. unsigned long base_addr_y;
  1173. unsigned long base_addr_c;
  1174. unsigned long y_offset;
  1175. unsigned long c_offset;
  1176. if (!ovl->format->yuv) {
  1177. y_offset = (var->yoffset * ovl->xres_virtual + var->xoffset)
  1178. * ovl->format->bpp / 8;
  1179. c_offset = 0;
  1180. } else {
  1181. unsigned int xsub = ovl->format->bpp < 24 ? 2 : 1;
  1182. unsigned int ysub = ovl->format->bpp < 16 ? 2 : 1;
  1183. y_offset = var->yoffset * ovl->xres_virtual + var->xoffset;
  1184. c_offset = var->yoffset / ysub * ovl->xres_virtual * 2 / xsub
  1185. + var->xoffset * 2 / xsub;
  1186. }
  1187. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1188. * nothing to do in that case.
  1189. */
  1190. if (y_offset == ovl->pan_y_offset)
  1191. return 0;
  1192. /* Set the source address for the next refresh */
  1193. base_addr_y = ovl->dma_handle + y_offset;
  1194. base_addr_c = ovl->dma_handle + ovl->xres_virtual * ovl->yres_virtual
  1195. + c_offset;
  1196. ovl->base_addr_y = base_addr_y;
  1197. ovl->base_addr_c = base_addr_c;
  1198. ovl->pan_y_offset = y_offset;
  1199. lcdc_write(ovl->channel->lcdc, LDBCR, LDBCR_UPC(ovl->index));
  1200. lcdc_write_overlay(ovl, LDBnBSAYR(ovl->index), ovl->base_addr_y);
  1201. lcdc_write_overlay(ovl, LDBnBSACR(ovl->index), ovl->base_addr_c);
  1202. lcdc_write(ovl->channel->lcdc, LDBCR,
  1203. LDBCR_UPF(ovl->index) | LDBCR_UPD(ovl->index));
  1204. return 0;
  1205. }
  1206. static int sh_mobile_lcdc_overlay_ioctl(struct fb_info *info, unsigned int cmd,
  1207. unsigned long arg)
  1208. {
  1209. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1210. switch (cmd) {
  1211. case FBIO_WAITFORVSYNC:
  1212. return sh_mobile_lcdc_wait_for_vsync(ovl->channel);
  1213. default:
  1214. return -ENOIOCTLCMD;
  1215. }
  1216. }
  1217. static int sh_mobile_lcdc_overlay_check_var(struct fb_var_screeninfo *var,
  1218. struct fb_info *info)
  1219. {
  1220. return __sh_mobile_lcdc_check_var(var, info);
  1221. }
  1222. static int sh_mobile_lcdc_overlay_set_par(struct fb_info *info)
  1223. {
  1224. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1225. ovl->format =
  1226. sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1227. ovl->xres = info->var.xres;
  1228. ovl->xres_virtual = info->var.xres_virtual;
  1229. ovl->yres = info->var.yres;
  1230. ovl->yres_virtual = info->var.yres_virtual;
  1231. if (ovl->format->yuv)
  1232. ovl->pitch = info->var.xres_virtual;
  1233. else
  1234. ovl->pitch = info->var.xres_virtual * ovl->format->bpp / 8;
  1235. sh_mobile_lcdc_overlay_setup(ovl);
  1236. info->fix.line_length = ovl->pitch;
  1237. if (sh_mobile_format_is_fourcc(&info->var)) {
  1238. info->fix.type = FB_TYPE_FOURCC;
  1239. info->fix.visual = FB_VISUAL_FOURCC;
  1240. } else {
  1241. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1242. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1243. }
  1244. return 0;
  1245. }
  1246. /* Overlay blanking. Disable the overlay when blanked. */
  1247. static int sh_mobile_lcdc_overlay_blank(int blank, struct fb_info *info)
  1248. {
  1249. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1250. ovl->enabled = !blank;
  1251. sh_mobile_lcdc_overlay_setup(ovl);
  1252. /* Prevent the backlight from receiving a blanking event by returning
  1253. * a non-zero value.
  1254. */
  1255. return 1;
  1256. }
  1257. static int
  1258. sh_mobile_lcdc_overlay_mmap(struct fb_info *info, struct vm_area_struct *vma)
  1259. {
  1260. struct sh_mobile_lcdc_overlay *ovl = info->par;
  1261. if (info->fbdefio)
  1262. return fb_deferred_io_mmap(info, vma);
  1263. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  1264. return dma_mmap_coherent(ovl->channel->lcdc->dev, vma, ovl->fb_mem,
  1265. ovl->dma_handle, ovl->fb_size);
  1266. }
  1267. static const struct fb_ops sh_mobile_lcdc_overlay_ops = {
  1268. .owner = THIS_MODULE,
  1269. __FB_DEFAULT_DMAMEM_OPS_RDWR,
  1270. .fb_blank = sh_mobile_lcdc_overlay_blank,
  1271. .fb_pan_display = sh_mobile_lcdc_overlay_pan,
  1272. __FB_DEFAULT_DMAMEM_OPS_DRAW,
  1273. .fb_ioctl = sh_mobile_lcdc_overlay_ioctl,
  1274. .fb_check_var = sh_mobile_lcdc_overlay_check_var,
  1275. .fb_set_par = sh_mobile_lcdc_overlay_set_par,
  1276. .fb_mmap = sh_mobile_lcdc_overlay_mmap,
  1277. };
  1278. static void
  1279. sh_mobile_lcdc_overlay_fb_unregister(struct sh_mobile_lcdc_overlay *ovl)
  1280. {
  1281. struct fb_info *info = ovl->info;
  1282. if (info == NULL || info->dev == NULL)
  1283. return;
  1284. unregister_framebuffer(ovl->info);
  1285. }
  1286. static int
  1287. sh_mobile_lcdc_overlay_fb_register(struct sh_mobile_lcdc_overlay *ovl)
  1288. {
  1289. struct sh_mobile_lcdc_priv *lcdc = ovl->channel->lcdc;
  1290. struct fb_info *info = ovl->info;
  1291. int ret;
  1292. if (info == NULL)
  1293. return 0;
  1294. ret = register_framebuffer(info);
  1295. if (ret < 0)
  1296. return ret;
  1297. dev_info(lcdc->dev, "registered %s/overlay %u as %dx%d %dbpp.\n",
  1298. dev_name(lcdc->dev), ovl->index, info->var.xres,
  1299. info->var.yres, info->var.bits_per_pixel);
  1300. return 0;
  1301. }
  1302. static void
  1303. sh_mobile_lcdc_overlay_fb_cleanup(struct sh_mobile_lcdc_overlay *ovl)
  1304. {
  1305. struct fb_info *info = ovl->info;
  1306. if (info == NULL || info->device == NULL)
  1307. return;
  1308. framebuffer_release(info);
  1309. }
  1310. static int
  1311. sh_mobile_lcdc_overlay_fb_init(struct sh_mobile_lcdc_overlay *ovl)
  1312. {
  1313. struct sh_mobile_lcdc_priv *priv = ovl->channel->lcdc;
  1314. struct fb_var_screeninfo *var;
  1315. struct fb_info *info;
  1316. /* Allocate and initialize the frame buffer device. */
  1317. info = framebuffer_alloc(0, priv->dev);
  1318. if (!info)
  1319. return -ENOMEM;
  1320. ovl->info = info;
  1321. info->fbops = &sh_mobile_lcdc_overlay_ops;
  1322. info->device = priv->dev;
  1323. info->flags |= FBINFO_VIRTFB;
  1324. info->screen_buffer = ovl->fb_mem;
  1325. info->par = ovl;
  1326. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1327. * for NV12 and NV21.
  1328. */
  1329. info->fix = sh_mobile_lcdc_overlay_fix;
  1330. snprintf(info->fix.id, sizeof(info->fix.id),
  1331. "SHMobile ovl %u", ovl->index);
  1332. info->fix.smem_start = ovl->dma_handle;
  1333. info->fix.smem_len = ovl->fb_size;
  1334. info->fix.line_length = ovl->pitch;
  1335. if (ovl->format->yuv)
  1336. info->fix.visual = FB_VISUAL_FOURCC;
  1337. else
  1338. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1339. switch (ovl->format->fourcc) {
  1340. case V4L2_PIX_FMT_NV12:
  1341. case V4L2_PIX_FMT_NV21:
  1342. info->fix.ypanstep = 2;
  1343. fallthrough;
  1344. case V4L2_PIX_FMT_NV16:
  1345. case V4L2_PIX_FMT_NV61:
  1346. info->fix.xpanstep = 2;
  1347. }
  1348. /* Initialize variable screen information. */
  1349. var = &info->var;
  1350. memset(var, 0, sizeof(*var));
  1351. var->xres = ovl->xres;
  1352. var->yres = ovl->yres;
  1353. var->xres_virtual = ovl->xres_virtual;
  1354. var->yres_virtual = ovl->yres_virtual;
  1355. var->activate = FB_ACTIVATE_NOW;
  1356. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1357. * for YUV formats.
  1358. */
  1359. if (!ovl->format->yuv)
  1360. var->bits_per_pixel = ovl->format->bpp;
  1361. else
  1362. var->grayscale = ovl->format->fourcc;
  1363. return sh_mobile_lcdc_overlay_check_var(var, info);
  1364. }
  1365. /* -----------------------------------------------------------------------------
  1366. * Frame buffer operations - main frame buffer
  1367. */
  1368. static int sh_mobile_lcdc_setcolreg(u_int regno,
  1369. u_int red, u_int green, u_int blue,
  1370. u_int transp, struct fb_info *info)
  1371. {
  1372. u32 *palette = info->pseudo_palette;
  1373. if (regno >= PALETTE_NR)
  1374. return -EINVAL;
  1375. /* only FB_VISUAL_TRUECOLOR supported */
  1376. red >>= 16 - info->var.red.length;
  1377. green >>= 16 - info->var.green.length;
  1378. blue >>= 16 - info->var.blue.length;
  1379. transp >>= 16 - info->var.transp.length;
  1380. palette[regno] = (red << info->var.red.offset) |
  1381. (green << info->var.green.offset) |
  1382. (blue << info->var.blue.offset) |
  1383. (transp << info->var.transp.offset);
  1384. return 0;
  1385. }
  1386. static const struct fb_fix_screeninfo sh_mobile_lcdc_fix = {
  1387. .id = "SH Mobile LCDC",
  1388. .type = FB_TYPE_PACKED_PIXELS,
  1389. .visual = FB_VISUAL_TRUECOLOR,
  1390. .accel = FB_ACCEL_NONE,
  1391. .xpanstep = 1,
  1392. .ypanstep = 1,
  1393. .ywrapstep = 0,
  1394. .capabilities = FB_CAP_FOURCC,
  1395. };
  1396. static void sh_mobile_lcdc_fillrect(struct fb_info *info,
  1397. const struct fb_fillrect *rect)
  1398. {
  1399. sys_fillrect(info, rect);
  1400. sh_mobile_lcdc_deferred_io_touch(info);
  1401. }
  1402. static void sh_mobile_lcdc_copyarea(struct fb_info *info,
  1403. const struct fb_copyarea *area)
  1404. {
  1405. sys_copyarea(info, area);
  1406. sh_mobile_lcdc_deferred_io_touch(info);
  1407. }
  1408. static void sh_mobile_lcdc_imageblit(struct fb_info *info,
  1409. const struct fb_image *image)
  1410. {
  1411. sys_imageblit(info, image);
  1412. sh_mobile_lcdc_deferred_io_touch(info);
  1413. }
  1414. static int sh_mobile_lcdc_pan(struct fb_var_screeninfo *var,
  1415. struct fb_info *info)
  1416. {
  1417. struct sh_mobile_lcdc_chan *ch = info->par;
  1418. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1419. unsigned long ldrcntr;
  1420. unsigned long base_addr_y, base_addr_c;
  1421. unsigned long y_offset;
  1422. unsigned long c_offset;
  1423. if (!ch->format->yuv) {
  1424. y_offset = (var->yoffset * ch->xres_virtual + var->xoffset)
  1425. * ch->format->bpp / 8;
  1426. c_offset = 0;
  1427. } else {
  1428. unsigned int xsub = ch->format->bpp < 24 ? 2 : 1;
  1429. unsigned int ysub = ch->format->bpp < 16 ? 2 : 1;
  1430. y_offset = var->yoffset * ch->xres_virtual + var->xoffset;
  1431. c_offset = var->yoffset / ysub * ch->xres_virtual * 2 / xsub
  1432. + var->xoffset * 2 / xsub;
  1433. }
  1434. /* If the Y offset hasn't changed, the C offset hasn't either. There's
  1435. * nothing to do in that case.
  1436. */
  1437. if (y_offset == ch->pan_y_offset)
  1438. return 0;
  1439. /* Set the source address for the next refresh */
  1440. base_addr_y = ch->dma_handle + y_offset;
  1441. base_addr_c = ch->dma_handle + ch->xres_virtual * ch->yres_virtual
  1442. + c_offset;
  1443. ch->base_addr_y = base_addr_y;
  1444. ch->base_addr_c = base_addr_c;
  1445. ch->pan_y_offset = y_offset;
  1446. lcdc_write_chan_mirror(ch, LDSA1R, base_addr_y);
  1447. if (ch->format->yuv)
  1448. lcdc_write_chan_mirror(ch, LDSA2R, base_addr_c);
  1449. ldrcntr = lcdc_read(priv, _LDRCNTR);
  1450. if (lcdc_chan_is_sublcd(ch))
  1451. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_SRS);
  1452. else
  1453. lcdc_write(ch->lcdc, _LDRCNTR, ldrcntr ^ LDRCNTR_MRS);
  1454. sh_mobile_lcdc_deferred_io_touch(info);
  1455. return 0;
  1456. }
  1457. static int sh_mobile_lcdc_ioctl(struct fb_info *info, unsigned int cmd,
  1458. unsigned long arg)
  1459. {
  1460. struct sh_mobile_lcdc_chan *ch = info->par;
  1461. int retval;
  1462. switch (cmd) {
  1463. case FBIO_WAITFORVSYNC:
  1464. retval = sh_mobile_lcdc_wait_for_vsync(ch);
  1465. break;
  1466. default:
  1467. retval = -ENOIOCTLCMD;
  1468. break;
  1469. }
  1470. return retval;
  1471. }
  1472. static void sh_mobile_fb_reconfig(struct fb_info *info)
  1473. {
  1474. struct sh_mobile_lcdc_chan *ch = info->par;
  1475. struct fb_var_screeninfo var;
  1476. struct fb_videomode mode;
  1477. if (ch->use_count > 1 || (ch->use_count == 1 && !info->fbcon_par))
  1478. /* More framebuffer users are active */
  1479. return;
  1480. fb_var_to_videomode(&mode, &info->var);
  1481. if (fb_mode_is_equal(&ch->display.mode, &mode))
  1482. return;
  1483. /* Display has been re-plugged, framebuffer is free now, reconfigure */
  1484. var = info->var;
  1485. fb_videomode_to_var(&var, &ch->display.mode);
  1486. var.width = ch->display.width;
  1487. var.height = ch->display.height;
  1488. var.activate = FB_ACTIVATE_NOW;
  1489. if (fb_set_var(info, &var) < 0)
  1490. /* Couldn't reconfigure, hopefully, can continue as before */
  1491. return;
  1492. fbcon_update_vcs(info, true);
  1493. }
  1494. /*
  1495. * Locking: both .fb_release() and .fb_open() are called with info->lock held if
  1496. * user == 1, or with console sem held, if user == 0.
  1497. */
  1498. static int sh_mobile_lcdc_release(struct fb_info *info, int user)
  1499. {
  1500. struct sh_mobile_lcdc_chan *ch = info->par;
  1501. mutex_lock(&ch->open_lock);
  1502. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1503. ch->use_count--;
  1504. /* Nothing to reconfigure, when called from fbcon */
  1505. if (user) {
  1506. console_lock();
  1507. sh_mobile_fb_reconfig(info);
  1508. console_unlock();
  1509. }
  1510. mutex_unlock(&ch->open_lock);
  1511. return 0;
  1512. }
  1513. static int sh_mobile_lcdc_open(struct fb_info *info, int user)
  1514. {
  1515. struct sh_mobile_lcdc_chan *ch = info->par;
  1516. mutex_lock(&ch->open_lock);
  1517. ch->use_count++;
  1518. dev_dbg(info->dev, "%s(): %d users\n", __func__, ch->use_count);
  1519. mutex_unlock(&ch->open_lock);
  1520. return 0;
  1521. }
  1522. static int sh_mobile_lcdc_check_var(struct fb_var_screeninfo *var,
  1523. struct fb_info *info)
  1524. {
  1525. struct sh_mobile_lcdc_chan *ch = info->par;
  1526. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1527. unsigned int best_dist = (unsigned int)-1;
  1528. unsigned int best_xres = 0;
  1529. unsigned int best_yres = 0;
  1530. unsigned int i;
  1531. int ret;
  1532. /* If board code provides us with a list of available modes, make sure
  1533. * we use one of them. Find the mode closest to the requested one. The
  1534. * distance between two modes is defined as the size of the
  1535. * non-overlapping parts of the two rectangles.
  1536. */
  1537. for (i = 0; i < ch->cfg->num_modes; ++i) {
  1538. const struct fb_videomode *mode = &ch->cfg->lcd_modes[i];
  1539. unsigned int dist;
  1540. /* We can only round up. */
  1541. if (var->xres > mode->xres || var->yres > mode->yres)
  1542. continue;
  1543. dist = var->xres * var->yres + mode->xres * mode->yres
  1544. - 2 * min(var->xres, mode->xres)
  1545. * min(var->yres, mode->yres);
  1546. if (dist < best_dist) {
  1547. best_xres = mode->xres;
  1548. best_yres = mode->yres;
  1549. best_dist = dist;
  1550. }
  1551. }
  1552. /* If no available mode can be used, return an error. */
  1553. if (ch->cfg->num_modes != 0) {
  1554. if (best_dist == (unsigned int)-1)
  1555. return -EINVAL;
  1556. var->xres = best_xres;
  1557. var->yres = best_yres;
  1558. }
  1559. ret = __sh_mobile_lcdc_check_var(var, info);
  1560. if (ret < 0)
  1561. return ret;
  1562. /* only accept the forced_fourcc for dual channel configurations */
  1563. if (p->forced_fourcc &&
  1564. p->forced_fourcc != sh_mobile_format_fourcc(var))
  1565. return -EINVAL;
  1566. return 0;
  1567. }
  1568. static int sh_mobile_lcdc_set_par(struct fb_info *info)
  1569. {
  1570. struct sh_mobile_lcdc_chan *ch = info->par;
  1571. int ret;
  1572. sh_mobile_lcdc_stop(ch->lcdc);
  1573. ch->format = sh_mobile_format_info(sh_mobile_format_fourcc(&info->var));
  1574. ch->colorspace = info->var.colorspace;
  1575. ch->xres = info->var.xres;
  1576. ch->xres_virtual = info->var.xres_virtual;
  1577. ch->yres = info->var.yres;
  1578. ch->yres_virtual = info->var.yres_virtual;
  1579. if (ch->format->yuv)
  1580. ch->pitch = info->var.xres_virtual;
  1581. else
  1582. ch->pitch = info->var.xres_virtual * ch->format->bpp / 8;
  1583. ret = sh_mobile_lcdc_start(ch->lcdc);
  1584. if (ret < 0)
  1585. dev_err(info->dev, "%s: unable to restart LCDC\n", __func__);
  1586. info->fix.line_length = ch->pitch;
  1587. if (sh_mobile_format_is_fourcc(&info->var)) {
  1588. info->fix.type = FB_TYPE_FOURCC;
  1589. info->fix.visual = FB_VISUAL_FOURCC;
  1590. } else {
  1591. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1592. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1593. }
  1594. return ret;
  1595. }
  1596. /*
  1597. * Screen blanking. Behavior is as follows:
  1598. * FB_BLANK_UNBLANK: screen unblanked, clocks enabled
  1599. * FB_BLANK_NORMAL: screen blanked, clocks enabled
  1600. * FB_BLANK_VSYNC,
  1601. * FB_BLANK_HSYNC,
  1602. * FB_BLANK_POWEROFF: screen blanked, clocks disabled
  1603. */
  1604. static int sh_mobile_lcdc_blank(int blank, struct fb_info *info)
  1605. {
  1606. struct sh_mobile_lcdc_chan *ch = info->par;
  1607. struct sh_mobile_lcdc_priv *p = ch->lcdc;
  1608. /* blank the screen? */
  1609. if (blank > FB_BLANK_UNBLANK && ch->blank_status == FB_BLANK_UNBLANK) {
  1610. struct fb_fillrect rect = {
  1611. .width = ch->xres,
  1612. .height = ch->yres,
  1613. };
  1614. sh_mobile_lcdc_fillrect(info, &rect);
  1615. }
  1616. /* turn clocks on? */
  1617. if (blank <= FB_BLANK_NORMAL && ch->blank_status > FB_BLANK_NORMAL) {
  1618. sh_mobile_lcdc_clk_on(p);
  1619. }
  1620. /* turn clocks off? */
  1621. if (blank > FB_BLANK_NORMAL && ch->blank_status <= FB_BLANK_NORMAL) {
  1622. /* make sure the screen is updated with the black fill before
  1623. * switching the clocks off. one vsync is not enough since
  1624. * blanking may occur in the middle of a refresh. deferred io
  1625. * mode will reenable the clocks and update the screen in time,
  1626. * so it does not need this. */
  1627. if (!info->fbdefio) {
  1628. sh_mobile_lcdc_wait_for_vsync(ch);
  1629. sh_mobile_lcdc_wait_for_vsync(ch);
  1630. }
  1631. sh_mobile_lcdc_clk_off(p);
  1632. }
  1633. ch->blank_status = blank;
  1634. return 0;
  1635. }
  1636. static int
  1637. sh_mobile_lcdc_mmap(struct fb_info *info, struct vm_area_struct *vma)
  1638. {
  1639. struct sh_mobile_lcdc_chan *ch = info->par;
  1640. if (info->fbdefio)
  1641. return fb_deferred_io_mmap(info, vma);
  1642. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  1643. return dma_mmap_coherent(ch->lcdc->dev, vma, ch->fb_mem,
  1644. ch->dma_handle, ch->fb_size);
  1645. }
  1646. static const struct fb_ops sh_mobile_lcdc_ops = {
  1647. .owner = THIS_MODULE,
  1648. .fb_setcolreg = sh_mobile_lcdc_setcolreg,
  1649. __FB_DEFAULT_DMAMEM_OPS_RDWR,
  1650. .fb_fillrect = sh_mobile_lcdc_fillrect,
  1651. .fb_copyarea = sh_mobile_lcdc_copyarea,
  1652. .fb_imageblit = sh_mobile_lcdc_imageblit,
  1653. .fb_blank = sh_mobile_lcdc_blank,
  1654. .fb_pan_display = sh_mobile_lcdc_pan,
  1655. .fb_ioctl = sh_mobile_lcdc_ioctl,
  1656. .fb_open = sh_mobile_lcdc_open,
  1657. .fb_release = sh_mobile_lcdc_release,
  1658. .fb_check_var = sh_mobile_lcdc_check_var,
  1659. .fb_set_par = sh_mobile_lcdc_set_par,
  1660. .fb_mmap = sh_mobile_lcdc_mmap,
  1661. };
  1662. static void
  1663. sh_mobile_lcdc_channel_fb_unregister(struct sh_mobile_lcdc_chan *ch)
  1664. {
  1665. if (ch->info && ch->info->dev)
  1666. unregister_framebuffer(ch->info);
  1667. }
  1668. static int
  1669. sh_mobile_lcdc_channel_fb_register(struct sh_mobile_lcdc_chan *ch)
  1670. {
  1671. struct fb_info *info = ch->info;
  1672. int ret;
  1673. if (info->fbdefio) {
  1674. ch->sglist = vmalloc(sizeof(struct scatterlist) *
  1675. ch->fb_size >> PAGE_SHIFT);
  1676. if (!ch->sglist)
  1677. return -ENOMEM;
  1678. }
  1679. info->bl_dev = ch->bl;
  1680. ret = register_framebuffer(info);
  1681. if (ret < 0)
  1682. return ret;
  1683. dev_info(ch->lcdc->dev, "registered %s/%s as %dx%d %dbpp.\n",
  1684. dev_name(ch->lcdc->dev), (ch->cfg->chan == LCDC_CHAN_MAINLCD) ?
  1685. "mainlcd" : "sublcd", info->var.xres, info->var.yres,
  1686. info->var.bits_per_pixel);
  1687. /* deferred io mode: disable clock to save power */
  1688. if (info->fbdefio || info->state == FBINFO_STATE_SUSPENDED)
  1689. sh_mobile_lcdc_clk_off(ch->lcdc);
  1690. return ret;
  1691. }
  1692. static void
  1693. sh_mobile_lcdc_channel_fb_cleanup(struct sh_mobile_lcdc_chan *ch)
  1694. {
  1695. struct fb_info *info = ch->info;
  1696. if (!info || !info->device)
  1697. return;
  1698. vfree(ch->sglist);
  1699. fb_dealloc_cmap(&info->cmap);
  1700. framebuffer_release(info);
  1701. }
  1702. static int
  1703. sh_mobile_lcdc_channel_fb_init(struct sh_mobile_lcdc_chan *ch,
  1704. const struct fb_videomode *modes,
  1705. unsigned int num_modes)
  1706. {
  1707. struct sh_mobile_lcdc_priv *priv = ch->lcdc;
  1708. struct fb_var_screeninfo *var;
  1709. struct fb_info *info;
  1710. int ret;
  1711. /* Allocate and initialize the frame buffer device. Create the modes
  1712. * list and allocate the color map.
  1713. */
  1714. info = framebuffer_alloc(0, priv->dev);
  1715. if (!info)
  1716. return -ENOMEM;
  1717. ch->info = info;
  1718. info->fbops = &sh_mobile_lcdc_ops;
  1719. info->device = priv->dev;
  1720. info->flags |= FBINFO_VIRTFB;
  1721. info->screen_buffer = ch->fb_mem;
  1722. info->pseudo_palette = &ch->pseudo_palette;
  1723. info->par = ch;
  1724. fb_videomode_to_modelist(modes, num_modes, &info->modelist);
  1725. ret = fb_alloc_cmap(&info->cmap, PALETTE_NR, 0);
  1726. if (ret < 0) {
  1727. dev_err(priv->dev, "unable to allocate cmap\n");
  1728. return ret;
  1729. }
  1730. /* Initialize fixed screen information. Restrict pan to 2 lines steps
  1731. * for NV12 and NV21.
  1732. */
  1733. info->fix = sh_mobile_lcdc_fix;
  1734. info->fix.smem_start = ch->dma_handle;
  1735. info->fix.smem_len = ch->fb_size;
  1736. info->fix.line_length = ch->pitch;
  1737. if (ch->format->yuv)
  1738. info->fix.visual = FB_VISUAL_FOURCC;
  1739. else
  1740. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1741. switch (ch->format->fourcc) {
  1742. case V4L2_PIX_FMT_NV12:
  1743. case V4L2_PIX_FMT_NV21:
  1744. info->fix.ypanstep = 2;
  1745. fallthrough;
  1746. case V4L2_PIX_FMT_NV16:
  1747. case V4L2_PIX_FMT_NV61:
  1748. info->fix.xpanstep = 2;
  1749. }
  1750. /* Initialize variable screen information using the first mode as
  1751. * default.
  1752. */
  1753. var = &info->var;
  1754. fb_videomode_to_var(var, modes);
  1755. var->width = ch->display.width;
  1756. var->height = ch->display.height;
  1757. var->xres_virtual = ch->xres_virtual;
  1758. var->yres_virtual = ch->yres_virtual;
  1759. var->activate = FB_ACTIVATE_NOW;
  1760. /* Use the legacy API by default for RGB formats, and the FOURCC API
  1761. * for YUV formats.
  1762. */
  1763. if (!ch->format->yuv)
  1764. var->bits_per_pixel = ch->format->bpp;
  1765. else
  1766. var->grayscale = ch->format->fourcc;
  1767. ret = sh_mobile_lcdc_check_var(var, info);
  1768. if (ret)
  1769. return ret;
  1770. return 0;
  1771. }
  1772. /* -----------------------------------------------------------------------------
  1773. * Backlight
  1774. */
  1775. static int sh_mobile_lcdc_update_bl(struct backlight_device *bdev)
  1776. {
  1777. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1778. int brightness = backlight_get_brightness(bdev);
  1779. ch->bl_brightness = brightness;
  1780. return ch->cfg->bl_info.set_brightness(brightness);
  1781. }
  1782. static int sh_mobile_lcdc_get_brightness(struct backlight_device *bdev)
  1783. {
  1784. struct sh_mobile_lcdc_chan *ch = bl_get_data(bdev);
  1785. return ch->bl_brightness;
  1786. }
  1787. static const struct backlight_ops sh_mobile_lcdc_bl_ops = {
  1788. .options = BL_CORE_SUSPENDRESUME,
  1789. .update_status = sh_mobile_lcdc_update_bl,
  1790. .get_brightness = sh_mobile_lcdc_get_brightness,
  1791. };
  1792. static struct backlight_device *sh_mobile_lcdc_bl_probe(struct device *parent,
  1793. struct sh_mobile_lcdc_chan *ch)
  1794. {
  1795. struct backlight_device *bl;
  1796. bl = backlight_device_register(ch->cfg->bl_info.name, parent, ch,
  1797. &sh_mobile_lcdc_bl_ops, NULL);
  1798. if (IS_ERR(bl)) {
  1799. dev_err(parent, "unable to register backlight device: %ld\n",
  1800. PTR_ERR(bl));
  1801. return NULL;
  1802. }
  1803. bl->props.max_brightness = ch->cfg->bl_info.max_brightness;
  1804. bl->props.brightness = bl->props.max_brightness;
  1805. backlight_update_status(bl);
  1806. return bl;
  1807. }
  1808. static void sh_mobile_lcdc_bl_remove(struct backlight_device *bdev)
  1809. {
  1810. backlight_device_unregister(bdev);
  1811. }
  1812. /* -----------------------------------------------------------------------------
  1813. * Power management
  1814. */
  1815. static int sh_mobile_lcdc_suspend(struct device *dev)
  1816. {
  1817. struct platform_device *pdev = to_platform_device(dev);
  1818. sh_mobile_lcdc_stop(platform_get_drvdata(pdev));
  1819. return 0;
  1820. }
  1821. static int sh_mobile_lcdc_resume(struct device *dev)
  1822. {
  1823. struct platform_device *pdev = to_platform_device(dev);
  1824. return sh_mobile_lcdc_start(platform_get_drvdata(pdev));
  1825. }
  1826. static int sh_mobile_lcdc_runtime_suspend(struct device *dev)
  1827. {
  1828. struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
  1829. /* turn off LCDC hardware */
  1830. lcdc_write(priv, _LDCNT1R, 0);
  1831. return 0;
  1832. }
  1833. static int sh_mobile_lcdc_runtime_resume(struct device *dev)
  1834. {
  1835. struct sh_mobile_lcdc_priv *priv = dev_get_drvdata(dev);
  1836. __sh_mobile_lcdc_start(priv);
  1837. return 0;
  1838. }
  1839. static const struct dev_pm_ops sh_mobile_lcdc_dev_pm_ops = {
  1840. .suspend = sh_mobile_lcdc_suspend,
  1841. .resume = sh_mobile_lcdc_resume,
  1842. .runtime_suspend = sh_mobile_lcdc_runtime_suspend,
  1843. .runtime_resume = sh_mobile_lcdc_runtime_resume,
  1844. };
  1845. /* -----------------------------------------------------------------------------
  1846. * Framebuffer notifier
  1847. */
  1848. /* -----------------------------------------------------------------------------
  1849. * Probe/remove and driver init/exit
  1850. */
  1851. static const struct fb_videomode default_720p = {
  1852. .name = "HDMI 720p",
  1853. .xres = 1280,
  1854. .yres = 720,
  1855. .left_margin = 220,
  1856. .right_margin = 110,
  1857. .hsync_len = 40,
  1858. .upper_margin = 20,
  1859. .lower_margin = 5,
  1860. .vsync_len = 5,
  1861. .pixclock = 13468,
  1862. .refresh = 60,
  1863. .sync = FB_SYNC_VERT_HIGH_ACT | FB_SYNC_HOR_HIGH_ACT,
  1864. };
  1865. static void sh_mobile_lcdc_remove(struct platform_device *pdev)
  1866. {
  1867. struct sh_mobile_lcdc_priv *priv = platform_get_drvdata(pdev);
  1868. unsigned int i;
  1869. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++)
  1870. sh_mobile_lcdc_overlay_fb_unregister(&priv->overlays[i]);
  1871. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  1872. sh_mobile_lcdc_channel_fb_unregister(&priv->ch[i]);
  1873. sh_mobile_lcdc_stop(priv);
  1874. for (i = 0; i < ARRAY_SIZE(priv->overlays); i++) {
  1875. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  1876. sh_mobile_lcdc_overlay_fb_cleanup(ovl);
  1877. if (ovl->fb_mem)
  1878. dma_free_coherent(&pdev->dev, ovl->fb_size,
  1879. ovl->fb_mem, ovl->dma_handle);
  1880. }
  1881. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1882. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  1883. if (ch->tx_dev) {
  1884. ch->tx_dev->lcdc = NULL;
  1885. module_put(ch->cfg->tx_dev->dev.driver->owner);
  1886. }
  1887. sh_mobile_lcdc_channel_fb_cleanup(ch);
  1888. if (ch->fb_mem)
  1889. dma_free_coherent(&pdev->dev, ch->fb_size,
  1890. ch->fb_mem, ch->dma_handle);
  1891. }
  1892. for (i = 0; i < ARRAY_SIZE(priv->ch); i++) {
  1893. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  1894. if (ch->bl)
  1895. sh_mobile_lcdc_bl_remove(ch->bl);
  1896. mutex_destroy(&ch->open_lock);
  1897. }
  1898. if (priv->dot_clk) {
  1899. pm_runtime_disable(&pdev->dev);
  1900. clk_put(priv->dot_clk);
  1901. }
  1902. if (priv->base)
  1903. iounmap(priv->base);
  1904. if (priv->irq)
  1905. free_irq(priv->irq, priv);
  1906. kfree(priv);
  1907. }
  1908. static int sh_mobile_lcdc_check_interface(struct sh_mobile_lcdc_chan *ch)
  1909. {
  1910. int interface_type = ch->cfg->interface_type;
  1911. switch (interface_type) {
  1912. case RGB8:
  1913. case RGB9:
  1914. case RGB12A:
  1915. case RGB12B:
  1916. case RGB16:
  1917. case RGB18:
  1918. case RGB24:
  1919. case SYS8A:
  1920. case SYS8B:
  1921. case SYS8C:
  1922. case SYS8D:
  1923. case SYS9:
  1924. case SYS12:
  1925. case SYS16A:
  1926. case SYS16B:
  1927. case SYS16C:
  1928. case SYS18:
  1929. case SYS24:
  1930. break;
  1931. default:
  1932. return -EINVAL;
  1933. }
  1934. /* SUBLCD only supports SYS interface */
  1935. if (lcdc_chan_is_sublcd(ch)) {
  1936. if (!(interface_type & LDMT1R_IFM))
  1937. return -EINVAL;
  1938. interface_type &= ~LDMT1R_IFM;
  1939. }
  1940. ch->ldmt1r_value = interface_type;
  1941. return 0;
  1942. }
  1943. static int
  1944. sh_mobile_lcdc_overlay_init(struct sh_mobile_lcdc_overlay *ovl)
  1945. {
  1946. const struct sh_mobile_lcdc_format_info *format;
  1947. struct device *dev = ovl->channel->lcdc->dev;
  1948. int ret;
  1949. if (ovl->cfg->fourcc == 0)
  1950. return 0;
  1951. /* Validate the format. */
  1952. format = sh_mobile_format_info(ovl->cfg->fourcc);
  1953. if (format == NULL) {
  1954. dev_err(dev, "Invalid FOURCC %08x\n", ovl->cfg->fourcc);
  1955. return -EINVAL;
  1956. }
  1957. ovl->enabled = false;
  1958. ovl->mode = LCDC_OVERLAY_BLEND;
  1959. ovl->alpha = 255;
  1960. ovl->rop3 = 0;
  1961. ovl->pos_x = 0;
  1962. ovl->pos_y = 0;
  1963. /* The default Y virtual resolution is twice the panel size to allow for
  1964. * double-buffering.
  1965. */
  1966. ovl->format = format;
  1967. ovl->xres = ovl->cfg->max_xres;
  1968. ovl->xres_virtual = ovl->xres;
  1969. ovl->yres = ovl->cfg->max_yres;
  1970. ovl->yres_virtual = ovl->yres * 2;
  1971. if (!format->yuv)
  1972. ovl->pitch = ovl->xres_virtual * format->bpp / 8;
  1973. else
  1974. ovl->pitch = ovl->xres_virtual;
  1975. /* Allocate frame buffer memory. */
  1976. ovl->fb_size = ovl->cfg->max_xres * ovl->cfg->max_yres
  1977. * format->bpp / 8 * 2;
  1978. ovl->fb_mem = dma_alloc_coherent(dev, ovl->fb_size, &ovl->dma_handle,
  1979. GFP_KERNEL);
  1980. if (!ovl->fb_mem) {
  1981. dev_err(dev, "unable to allocate buffer\n");
  1982. return -ENOMEM;
  1983. }
  1984. ret = sh_mobile_lcdc_overlay_fb_init(ovl);
  1985. if (ret < 0)
  1986. return ret;
  1987. return 0;
  1988. }
  1989. static int
  1990. sh_mobile_lcdc_channel_init(struct sh_mobile_lcdc_chan *ch)
  1991. {
  1992. const struct sh_mobile_lcdc_format_info *format;
  1993. const struct sh_mobile_lcdc_chan_cfg *cfg = ch->cfg;
  1994. struct device *dev = ch->lcdc->dev;
  1995. const struct fb_videomode *max_mode;
  1996. const struct fb_videomode *mode;
  1997. unsigned int num_modes;
  1998. unsigned int max_size;
  1999. unsigned int i;
  2000. /* Validate the format. */
  2001. format = sh_mobile_format_info(cfg->fourcc);
  2002. if (format == NULL) {
  2003. dev_err(dev, "Invalid FOURCC %08x.\n", cfg->fourcc);
  2004. return -EINVAL;
  2005. }
  2006. /* Iterate through the modes to validate them and find the highest
  2007. * resolution.
  2008. */
  2009. max_mode = NULL;
  2010. max_size = 0;
  2011. for (i = 0, mode = cfg->lcd_modes; i < cfg->num_modes; i++, mode++) {
  2012. unsigned int size = mode->yres * mode->xres;
  2013. /* NV12/NV21 buffers must have even number of lines */
  2014. if ((cfg->fourcc == V4L2_PIX_FMT_NV12 ||
  2015. cfg->fourcc == V4L2_PIX_FMT_NV21) && (mode->yres & 0x1)) {
  2016. dev_err(dev, "yres must be multiple of 2 for "
  2017. "YCbCr420 mode.\n");
  2018. return -EINVAL;
  2019. }
  2020. if (size > max_size) {
  2021. max_mode = mode;
  2022. max_size = size;
  2023. }
  2024. }
  2025. if (!max_size)
  2026. max_size = MAX_XRES * MAX_YRES;
  2027. else
  2028. dev_dbg(dev, "Found largest videomode %ux%u\n",
  2029. max_mode->xres, max_mode->yres);
  2030. if (cfg->lcd_modes == NULL) {
  2031. mode = &default_720p;
  2032. num_modes = 1;
  2033. } else {
  2034. mode = cfg->lcd_modes;
  2035. num_modes = cfg->num_modes;
  2036. }
  2037. /* Use the first mode as default. The default Y virtual resolution is
  2038. * twice the panel size to allow for double-buffering.
  2039. */
  2040. ch->format = format;
  2041. ch->xres = mode->xres;
  2042. ch->xres_virtual = mode->xres;
  2043. ch->yres = mode->yres;
  2044. ch->yres_virtual = mode->yres * 2;
  2045. if (!format->yuv) {
  2046. ch->colorspace = V4L2_COLORSPACE_SRGB;
  2047. ch->pitch = ch->xres_virtual * format->bpp / 8;
  2048. } else {
  2049. ch->colorspace = V4L2_COLORSPACE_REC709;
  2050. ch->pitch = ch->xres_virtual;
  2051. }
  2052. ch->display.width = cfg->panel_cfg.width;
  2053. ch->display.height = cfg->panel_cfg.height;
  2054. ch->display.mode = *mode;
  2055. /* Allocate frame buffer memory. */
  2056. ch->fb_size = max_size * format->bpp / 8 * 2;
  2057. ch->fb_mem = dma_alloc_coherent(dev, ch->fb_size, &ch->dma_handle,
  2058. GFP_KERNEL);
  2059. if (ch->fb_mem == NULL) {
  2060. dev_err(dev, "unable to allocate buffer\n");
  2061. return -ENOMEM;
  2062. }
  2063. /* Initialize the transmitter device if present. */
  2064. if (cfg->tx_dev) {
  2065. if (!cfg->tx_dev->dev.driver ||
  2066. !try_module_get(cfg->tx_dev->dev.driver->owner)) {
  2067. dev_warn(dev, "unable to get transmitter device\n");
  2068. return -EINVAL;
  2069. }
  2070. ch->tx_dev = platform_get_drvdata(cfg->tx_dev);
  2071. ch->tx_dev->lcdc = ch;
  2072. ch->tx_dev->def_mode = *mode;
  2073. }
  2074. return sh_mobile_lcdc_channel_fb_init(ch, mode, num_modes);
  2075. }
  2076. static int sh_mobile_lcdc_probe(struct platform_device *pdev)
  2077. {
  2078. struct sh_mobile_lcdc_info *pdata = pdev->dev.platform_data;
  2079. struct sh_mobile_lcdc_priv *priv;
  2080. struct resource *res;
  2081. int num_channels;
  2082. int error;
  2083. int irq, i;
  2084. if (!pdata) {
  2085. dev_err(&pdev->dev, "no platform data defined\n");
  2086. return -EINVAL;
  2087. }
  2088. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2089. irq = platform_get_irq(pdev, 0);
  2090. if (!res || irq < 0) {
  2091. dev_err(&pdev->dev, "cannot get platform resources\n");
  2092. return -ENOENT;
  2093. }
  2094. priv = kzalloc_obj(*priv);
  2095. if (!priv)
  2096. return -ENOMEM;
  2097. priv->dev = &pdev->dev;
  2098. for (i = 0; i < ARRAY_SIZE(priv->ch); i++)
  2099. mutex_init(&priv->ch[i].open_lock);
  2100. platform_set_drvdata(pdev, priv);
  2101. error = request_irq(irq, sh_mobile_lcdc_irq, 0,
  2102. dev_name(&pdev->dev), priv);
  2103. if (error) {
  2104. dev_err(&pdev->dev, "unable to request irq\n");
  2105. goto err1;
  2106. }
  2107. priv->irq = irq;
  2108. atomic_set(&priv->hw_usecnt, -1);
  2109. for (i = 0, num_channels = 0; i < ARRAY_SIZE(pdata->ch); i++) {
  2110. struct sh_mobile_lcdc_chan *ch = priv->ch + num_channels;
  2111. ch->lcdc = priv;
  2112. ch->cfg = &pdata->ch[i];
  2113. error = sh_mobile_lcdc_check_interface(ch);
  2114. if (error) {
  2115. dev_err(&pdev->dev, "unsupported interface type\n");
  2116. goto err1;
  2117. }
  2118. init_waitqueue_head(&ch->frame_end_wait);
  2119. init_completion(&ch->vsync_completion);
  2120. /* probe the backlight is there is one defined */
  2121. if (ch->cfg->bl_info.max_brightness)
  2122. ch->bl = sh_mobile_lcdc_bl_probe(&pdev->dev, ch);
  2123. switch (pdata->ch[i].chan) {
  2124. case LCDC_CHAN_MAINLCD:
  2125. ch->enabled = LDCNT2R_ME;
  2126. ch->reg_offs = lcdc_offs_mainlcd;
  2127. num_channels++;
  2128. break;
  2129. case LCDC_CHAN_SUBLCD:
  2130. ch->enabled = LDCNT2R_SE;
  2131. ch->reg_offs = lcdc_offs_sublcd;
  2132. num_channels++;
  2133. break;
  2134. }
  2135. }
  2136. if (!num_channels) {
  2137. dev_err(&pdev->dev, "no channels defined\n");
  2138. error = -EINVAL;
  2139. goto err1;
  2140. }
  2141. /* for dual channel LCDC (MAIN + SUB) force shared format setting */
  2142. if (num_channels == 2)
  2143. priv->forced_fourcc = pdata->ch[0].fourcc;
  2144. priv->base = ioremap(res->start, resource_size(res));
  2145. if (!priv->base) {
  2146. error = -ENOMEM;
  2147. goto err1;
  2148. }
  2149. error = sh_mobile_lcdc_setup_clocks(priv, pdata->clock_source);
  2150. if (error) {
  2151. dev_err(&pdev->dev, "unable to setup clocks\n");
  2152. goto err1;
  2153. }
  2154. /* Enable runtime PM. */
  2155. pm_runtime_enable(&pdev->dev);
  2156. for (i = 0; i < num_channels; i++) {
  2157. struct sh_mobile_lcdc_chan *ch = &priv->ch[i];
  2158. error = sh_mobile_lcdc_channel_init(ch);
  2159. if (error)
  2160. goto err1;
  2161. }
  2162. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2163. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2164. ovl->cfg = &pdata->overlays[i];
  2165. ovl->channel = &priv->ch[0];
  2166. error = sh_mobile_lcdc_overlay_init(ovl);
  2167. if (error)
  2168. goto err1;
  2169. }
  2170. error = sh_mobile_lcdc_start(priv);
  2171. if (error) {
  2172. dev_err(&pdev->dev, "unable to start hardware\n");
  2173. goto err1;
  2174. }
  2175. for (i = 0; i < num_channels; i++) {
  2176. struct sh_mobile_lcdc_chan *ch = priv->ch + i;
  2177. error = sh_mobile_lcdc_channel_fb_register(ch);
  2178. if (error)
  2179. goto err1;
  2180. }
  2181. for (i = 0; i < ARRAY_SIZE(pdata->overlays); i++) {
  2182. struct sh_mobile_lcdc_overlay *ovl = &priv->overlays[i];
  2183. error = sh_mobile_lcdc_overlay_fb_register(ovl);
  2184. if (error)
  2185. goto err1;
  2186. }
  2187. return 0;
  2188. err1:
  2189. sh_mobile_lcdc_remove(pdev);
  2190. return error;
  2191. }
  2192. static struct platform_driver sh_mobile_lcdc_driver = {
  2193. .driver = {
  2194. .name = "sh_mobile_lcdc_fb",
  2195. .dev_groups = overlay_sysfs_groups,
  2196. .pm = &sh_mobile_lcdc_dev_pm_ops,
  2197. },
  2198. .probe = sh_mobile_lcdc_probe,
  2199. .remove = sh_mobile_lcdc_remove,
  2200. };
  2201. module_platform_driver(sh_mobile_lcdc_driver);
  2202. MODULE_DESCRIPTION("SuperH Mobile LCDC Framebuffer driver");
  2203. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  2204. MODULE_LICENSE("GPL v2");