tidss_dispc.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016-2018 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Jyri Sarha <jsarha@ti.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/err.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/media-bus-format.h>
  15. #include <linux/module.h>
  16. #include <linux/mfd/syscon.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/sys_soc.h>
  22. #include <drm/drm_blend.h>
  23. #include <drm/drm_fourcc.h>
  24. #include <drm/drm_fb_dma_helper.h>
  25. #include <drm/drm_framebuffer.h>
  26. #include <drm/drm_gem_dma_helper.h>
  27. #include <drm/drm_panel.h>
  28. #include <drm/drm_print.h>
  29. #include "tidss_crtc.h"
  30. #include "tidss_dispc.h"
  31. #include "tidss_drv.h"
  32. #include "tidss_irq.h"
  33. #include "tidss_plane.h"
  34. #include "tidss_dispc_regs.h"
  35. #include "tidss_scale_coefs.h"
  36. static const u16 tidss_k2g_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
  37. [DSS_REVISION_OFF] = 0x00,
  38. [DSS_SYSCONFIG_OFF] = 0x04,
  39. [DSS_SYSSTATUS_OFF] = 0x08,
  40. [DISPC_IRQ_EOI_OFF] = 0x20,
  41. [DISPC_IRQSTATUS_RAW_OFF] = 0x24,
  42. [DISPC_IRQSTATUS_OFF] = 0x28,
  43. [DISPC_IRQENABLE_SET_OFF] = 0x2c,
  44. [DISPC_IRQENABLE_CLR_OFF] = 0x30,
  45. [DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] = 0x40,
  46. [DISPC_GLOBAL_BUFFER_OFF] = 0x44,
  47. [DISPC_DBG_CONTROL_OFF] = 0x4c,
  48. [DISPC_DBG_STATUS_OFF] = 0x50,
  49. [DISPC_CLKGATING_DISABLE_OFF] = 0x54,
  50. };
  51. const struct dispc_features dispc_k2g_feats = {
  52. /*
  53. * XXX According TRM the RGB input buffer width up to 2560 should
  54. * work on 3 taps, but in practice it only works up to 1280.
  55. */
  56. .scaling = {
  57. .in_width_max_5tap_rgb = 1280,
  58. .in_width_max_3tap_rgb = 1280,
  59. .in_width_max_5tap_yuv = 2560,
  60. .in_width_max_3tap_yuv = 2560,
  61. .upscale_limit = 16,
  62. .downscale_limit_5tap = 4,
  63. .downscale_limit_3tap = 2,
  64. /*
  65. * The max supported pixel inc value is 255. The value
  66. * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
  67. * The maximum bpp of all formats supported by the HW
  68. * is 8. So the maximum supported xinc value is 32,
  69. * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
  70. */
  71. .xinc_max = 32,
  72. },
  73. .subrev = DISPC_K2G,
  74. .common = "common",
  75. .common_regs = tidss_k2g_common_regs,
  76. .num_vps = 1,
  77. .vp_name = { "vp1" },
  78. .ovr_name = { "ovr1" },
  79. .vpclk_name = { "vp1" },
  80. .vp_bus_type = { DISPC_VP_DPI },
  81. .vp_feat = { .color = {
  82. .has_ctm = true,
  83. .gamma_size = 256,
  84. .gamma_type = TIDSS_GAMMA_8BIT,
  85. },
  86. },
  87. .num_vids = 1,
  88. .vid_info = {
  89. {
  90. .name = "vid1",
  91. .is_lite = false,
  92. .hw_id = 0,
  93. },
  94. },
  95. .vid_order = { 0 },
  96. };
  97. static const u16 tidss_am65x_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
  98. [DSS_REVISION_OFF] = 0x4,
  99. [DSS_SYSCONFIG_OFF] = 0x8,
  100. [DSS_SYSSTATUS_OFF] = 0x20,
  101. [DISPC_IRQ_EOI_OFF] = 0x24,
  102. [DISPC_IRQSTATUS_RAW_OFF] = 0x28,
  103. [DISPC_IRQSTATUS_OFF] = 0x2c,
  104. [DISPC_IRQENABLE_SET_OFF] = 0x30,
  105. [DISPC_IRQENABLE_CLR_OFF] = 0x40,
  106. [DISPC_VID_IRQENABLE_OFF] = 0x44,
  107. [DISPC_VID_IRQSTATUS_OFF] = 0x58,
  108. [DISPC_VP_IRQENABLE_OFF] = 0x70,
  109. [DISPC_VP_IRQSTATUS_OFF] = 0x7c,
  110. [WB_IRQENABLE_OFF] = 0x88,
  111. [WB_IRQSTATUS_OFF] = 0x8c,
  112. [DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] = 0x90,
  113. [DISPC_GLOBAL_OUTPUT_ENABLE_OFF] = 0x94,
  114. [DISPC_GLOBAL_BUFFER_OFF] = 0x98,
  115. [DSS_CBA_CFG_OFF] = 0x9c,
  116. [DISPC_DBG_CONTROL_OFF] = 0xa0,
  117. [DISPC_DBG_STATUS_OFF] = 0xa4,
  118. [DISPC_CLKGATING_DISABLE_OFF] = 0xa8,
  119. [DISPC_SECURE_DISABLE_OFF] = 0xac,
  120. };
  121. const struct dispc_features dispc_am65x_feats = {
  122. .scaling = {
  123. .in_width_max_5tap_rgb = 1280,
  124. .in_width_max_3tap_rgb = 2560,
  125. .in_width_max_5tap_yuv = 2560,
  126. .in_width_max_3tap_yuv = 4096,
  127. .upscale_limit = 16,
  128. .downscale_limit_5tap = 4,
  129. .downscale_limit_3tap = 2,
  130. /*
  131. * The max supported pixel inc value is 255. The value
  132. * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
  133. * The maximum bpp of all formats supported by the HW
  134. * is 8. So the maximum supported xinc value is 32,
  135. * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
  136. */
  137. .xinc_max = 32,
  138. },
  139. .subrev = DISPC_AM65X,
  140. .common = "common",
  141. .common_regs = tidss_am65x_common_regs,
  142. .num_vps = 2,
  143. .vp_name = { "vp1", "vp2" },
  144. .ovr_name = { "ovr1", "ovr2" },
  145. .vpclk_name = { "vp1", "vp2" },
  146. .vp_bus_type = { DISPC_VP_OLDI_AM65X, DISPC_VP_DPI },
  147. .vp_feat = { .color = {
  148. .has_ctm = true,
  149. .gamma_size = 256,
  150. .gamma_type = TIDSS_GAMMA_8BIT,
  151. },
  152. },
  153. .num_vids = 2,
  154. /* note: vid is plane_id 0 and vidl1 is plane_id 1 */
  155. .vid_info = {
  156. {
  157. .name = "vid",
  158. .is_lite = false,
  159. .hw_id = 0,
  160. },
  161. {
  162. .name = "vidl1",
  163. .is_lite = true,
  164. .hw_id = 1,
  165. },
  166. },
  167. .vid_order = {1, 0},
  168. };
  169. static const u16 tidss_j721e_common_regs[DISPC_COMMON_REG_TABLE_LEN] = {
  170. [DSS_REVISION_OFF] = 0x4,
  171. [DSS_SYSCONFIG_OFF] = 0x8,
  172. [DSS_SYSSTATUS_OFF] = 0x20,
  173. [DISPC_IRQ_EOI_OFF] = 0x80,
  174. [DISPC_IRQSTATUS_RAW_OFF] = 0x28,
  175. [DISPC_IRQSTATUS_OFF] = 0x2c,
  176. [DISPC_IRQENABLE_SET_OFF] = 0x30,
  177. [DISPC_IRQENABLE_CLR_OFF] = 0x34,
  178. [DISPC_VID_IRQENABLE_OFF] = 0x38,
  179. [DISPC_VID_IRQSTATUS_OFF] = 0x48,
  180. [DISPC_VP_IRQENABLE_OFF] = 0x58,
  181. [DISPC_VP_IRQSTATUS_OFF] = 0x68,
  182. [WB_IRQENABLE_OFF] = 0x78,
  183. [WB_IRQSTATUS_OFF] = 0x7c,
  184. [DISPC_GLOBAL_MFLAG_ATTRIBUTE_OFF] = 0x98,
  185. [DISPC_GLOBAL_OUTPUT_ENABLE_OFF] = 0x9c,
  186. [DISPC_GLOBAL_BUFFER_OFF] = 0xa0,
  187. [DSS_CBA_CFG_OFF] = 0xa4,
  188. [DISPC_DBG_CONTROL_OFF] = 0xa8,
  189. [DISPC_DBG_STATUS_OFF] = 0xac,
  190. [DISPC_CLKGATING_DISABLE_OFF] = 0xb0,
  191. [DISPC_SECURE_DISABLE_OFF] = 0x90,
  192. [FBDC_REVISION_1_OFF] = 0xb8,
  193. [FBDC_REVISION_2_OFF] = 0xbc,
  194. [FBDC_REVISION_3_OFF] = 0xc0,
  195. [FBDC_REVISION_4_OFF] = 0xc4,
  196. [FBDC_REVISION_5_OFF] = 0xc8,
  197. [FBDC_REVISION_6_OFF] = 0xcc,
  198. [FBDC_COMMON_CONTROL_OFF] = 0xd0,
  199. [FBDC_CONSTANT_COLOR_0_OFF] = 0xd4,
  200. [FBDC_CONSTANT_COLOR_1_OFF] = 0xd8,
  201. [DISPC_CONNECTIONS_OFF] = 0xe4,
  202. [DISPC_MSS_VP1_OFF] = 0xe8,
  203. [DISPC_MSS_VP3_OFF] = 0xec,
  204. };
  205. const struct dispc_features dispc_j721e_feats = {
  206. .scaling = {
  207. .in_width_max_5tap_rgb = 2048,
  208. .in_width_max_3tap_rgb = 4096,
  209. .in_width_max_5tap_yuv = 4096,
  210. .in_width_max_3tap_yuv = 4096,
  211. .upscale_limit = 16,
  212. .downscale_limit_5tap = 4,
  213. .downscale_limit_3tap = 2,
  214. /*
  215. * The max supported pixel inc value is 255. The value
  216. * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
  217. * The maximum bpp of all formats supported by the HW
  218. * is 8. So the maximum supported xinc value is 32,
  219. * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
  220. */
  221. .xinc_max = 32,
  222. },
  223. .subrev = DISPC_J721E,
  224. .common = "common_m",
  225. .common_regs = tidss_j721e_common_regs,
  226. .num_vps = 4,
  227. .vp_name = { "vp1", "vp2", "vp3", "vp4" },
  228. .ovr_name = { "ovr1", "ovr2", "ovr3", "ovr4" },
  229. .vpclk_name = { "vp1", "vp2", "vp3", "vp4" },
  230. /* Currently hard coded VP routing (see dispc_initial_config()) */
  231. .vp_bus_type = { DISPC_VP_INTERNAL, DISPC_VP_DPI,
  232. DISPC_VP_INTERNAL, DISPC_VP_DPI, },
  233. .vp_feat = { .color = {
  234. .has_ctm = true,
  235. .gamma_size = 1024,
  236. .gamma_type = TIDSS_GAMMA_10BIT,
  237. },
  238. },
  239. .num_vids = 4,
  240. .vid_info = {
  241. {
  242. .name = "vid1",
  243. .is_lite = false,
  244. .hw_id = 0,
  245. },
  246. {
  247. .name = "vidl1",
  248. .is_lite = true,
  249. .hw_id = 1,
  250. },
  251. {
  252. .name = "vid2",
  253. .is_lite = false,
  254. .hw_id = 2,
  255. },
  256. {
  257. .name = "vidl2",
  258. .is_lite = true,
  259. .hw_id = 3,
  260. },
  261. },
  262. .vid_order = { 1, 3, 0, 2 },
  263. };
  264. const struct dispc_features dispc_am625_feats = {
  265. .scaling = {
  266. .in_width_max_5tap_rgb = 1280,
  267. .in_width_max_3tap_rgb = 2560,
  268. .in_width_max_5tap_yuv = 2560,
  269. .in_width_max_3tap_yuv = 4096,
  270. .upscale_limit = 16,
  271. .downscale_limit_5tap = 4,
  272. .downscale_limit_3tap = 2,
  273. /*
  274. * The max supported pixel inc value is 255. The value
  275. * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
  276. * The maximum bpp of all formats supported by the HW
  277. * is 8. So the maximum supported xinc value is 32,
  278. * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
  279. */
  280. .xinc_max = 32,
  281. },
  282. .subrev = DISPC_AM625,
  283. .common = "common",
  284. .common_regs = tidss_am65x_common_regs,
  285. .num_vps = 2,
  286. .vp_name = { "vp1", "vp2" },
  287. .ovr_name = { "ovr1", "ovr2" },
  288. .vpclk_name = { "vp1", "vp2" },
  289. .vp_bus_type = { DISPC_VP_INTERNAL, DISPC_VP_DPI },
  290. .vp_feat = { .color = {
  291. .has_ctm = true,
  292. .gamma_size = 256,
  293. .gamma_type = TIDSS_GAMMA_8BIT,
  294. },
  295. },
  296. .num_vids = 2,
  297. /* note: vid is plane_id 0 and vidl1 is plane_id 1 */
  298. .vid_info = {
  299. {
  300. .name = "vid",
  301. .is_lite = false,
  302. .hw_id = 0,
  303. },
  304. {
  305. .name = "vidl1",
  306. .is_lite = true,
  307. .hw_id = 1,
  308. }
  309. },
  310. .vid_order = {1, 0},
  311. };
  312. const struct dispc_features dispc_am62a7_feats = {
  313. .scaling = {
  314. .in_width_max_5tap_rgb = 1280,
  315. .in_width_max_3tap_rgb = 2560,
  316. .in_width_max_5tap_yuv = 2560,
  317. .in_width_max_3tap_yuv = 4096,
  318. .upscale_limit = 16,
  319. .downscale_limit_5tap = 4,
  320. .downscale_limit_3tap = 2,
  321. /*
  322. * The max supported pixel inc value is 255. The value
  323. * of pixel inc is calculated like this: 1+(xinc-1)*bpp.
  324. * The maximum bpp of all formats supported by the HW
  325. * is 8. So the maximum supported xinc value is 32,
  326. * because 1+(32-1)*8 < 255 < 1+(33-1)*4.
  327. */
  328. .xinc_max = 32,
  329. },
  330. .subrev = DISPC_AM62A7,
  331. .common = "common",
  332. .common_regs = tidss_am65x_common_regs,
  333. .num_vps = 2,
  334. .vp_name = { "vp1", "vp2" },
  335. .ovr_name = { "ovr1", "ovr2" },
  336. .vpclk_name = { "vp1", "vp2" },
  337. /* VP1 of the DSS in AM62A7 SoC is tied off internally */
  338. .vp_bus_type = { DISPC_VP_TIED_OFF, DISPC_VP_DPI },
  339. .vp_feat = { .color = {
  340. .has_ctm = true,
  341. .gamma_size = 256,
  342. .gamma_type = TIDSS_GAMMA_8BIT,
  343. },
  344. },
  345. .num_vids = 2,
  346. .vid_info = {
  347. {
  348. .name = "vid",
  349. .is_lite = false,
  350. .hw_id = 0,
  351. },
  352. {
  353. .name = "vidl1",
  354. .is_lite = true,
  355. .hw_id = 1,
  356. }
  357. },
  358. .vid_order = {1, 0},
  359. };
  360. const struct dispc_features dispc_am62l_feats = {
  361. .subrev = DISPC_AM62L,
  362. .common = "common",
  363. .common_regs = tidss_am65x_common_regs,
  364. .num_vps = 1,
  365. .vp_name = { "vp1" },
  366. .ovr_name = { "ovr1" },
  367. .vpclk_name = { "vp1" },
  368. .vp_bus_type = { DISPC_VP_DPI },
  369. .vp_feat = { .color = {
  370. .has_ctm = true,
  371. .gamma_size = 256,
  372. .gamma_type = TIDSS_GAMMA_8BIT,
  373. },
  374. },
  375. .num_vids = 1,
  376. .vid_info = {
  377. {
  378. .name = "vidl1",
  379. .is_lite = true,
  380. .hw_id = 1,
  381. }
  382. },
  383. .vid_order = {0},
  384. };
  385. static const u16 *dispc_common_regmap;
  386. struct dss_vp_data {
  387. u32 *gamma_table;
  388. };
  389. struct dispc_device {
  390. struct tidss_device *tidss;
  391. struct device *dev;
  392. void __iomem *base_common;
  393. void __iomem *base_vid[TIDSS_MAX_PLANES];
  394. void __iomem *base_ovr[TIDSS_MAX_PORTS];
  395. void __iomem *base_vp[TIDSS_MAX_PORTS];
  396. struct regmap *am65x_oldi_io_ctrl;
  397. struct clk *vp_clk[TIDSS_MAX_PORTS];
  398. const struct dispc_features *feat;
  399. struct clk *fclk;
  400. bool is_enabled;
  401. struct dss_vp_data vp_data[TIDSS_MAX_PORTS];
  402. u32 *fourccs;
  403. u32 num_fourccs;
  404. u32 memory_bandwidth_limit;
  405. struct dispc_errata errata;
  406. };
  407. static void dispc_write(struct dispc_device *dispc, u16 reg, u32 val)
  408. {
  409. iowrite32(val, dispc->base_common + reg);
  410. }
  411. static u32 dispc_read(struct dispc_device *dispc, u16 reg)
  412. {
  413. return ioread32(dispc->base_common + reg);
  414. }
  415. static
  416. void dispc_vid_write(struct dispc_device *dispc, u32 hw_plane, u16 reg, u32 val)
  417. {
  418. void __iomem *base = dispc->base_vid[hw_plane];
  419. iowrite32(val, base + reg);
  420. }
  421. static u32 dispc_vid_read(struct dispc_device *dispc, u32 hw_plane, u16 reg)
  422. {
  423. void __iomem *base = dispc->base_vid[hw_plane];
  424. return ioread32(base + reg);
  425. }
  426. static void dispc_ovr_write(struct dispc_device *dispc, u32 hw_videoport,
  427. u16 reg, u32 val)
  428. {
  429. void __iomem *base = dispc->base_ovr[hw_videoport];
  430. iowrite32(val, base + reg);
  431. }
  432. static u32 dispc_ovr_read(struct dispc_device *dispc, u32 hw_videoport, u16 reg)
  433. {
  434. void __iomem *base = dispc->base_ovr[hw_videoport];
  435. return ioread32(base + reg);
  436. }
  437. static void dispc_vp_write(struct dispc_device *dispc, u32 hw_videoport,
  438. u16 reg, u32 val)
  439. {
  440. void __iomem *base = dispc->base_vp[hw_videoport];
  441. iowrite32(val, base + reg);
  442. }
  443. static u32 dispc_vp_read(struct dispc_device *dispc, u32 hw_videoport, u16 reg)
  444. {
  445. void __iomem *base = dispc->base_vp[hw_videoport];
  446. return ioread32(base + reg);
  447. }
  448. int tidss_configure_oldi(struct tidss_device *tidss, u32 hw_videoport,
  449. u32 oldi_cfg)
  450. {
  451. u32 count = 0;
  452. u32 oldi_reset_bit = BIT(5 + hw_videoport);
  453. dispc_vp_write(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, oldi_cfg);
  454. while (!(oldi_reset_bit & dispc_read(tidss->dispc, DSS_SYSSTATUS)) &&
  455. count < 10000)
  456. count++;
  457. if (!(oldi_reset_bit & dispc_read(tidss->dispc, DSS_SYSSTATUS)))
  458. return -ETIMEDOUT;
  459. return 0;
  460. }
  461. void tidss_disable_oldi(struct tidss_device *tidss, u32 hw_videoport)
  462. {
  463. dispc_vp_write(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
  464. }
  465. /*
  466. * TRM gives bitfields as start:end, where start is the higher bit
  467. * number. For example 7:0
  468. */
  469. #define REG_GET(dispc, idx, mask) \
  470. ((u32)FIELD_GET((mask), dispc_read((dispc), (idx))))
  471. #define REG_FLD_MOD(dispc, idx, val, mask) \
  472. ({ \
  473. struct dispc_device *_dispc = (dispc); \
  474. u32 _idx = (idx); \
  475. u32 _reg = dispc_read(_dispc, _idx); \
  476. FIELD_MODIFY((mask), &_reg, (val)); \
  477. dispc_write(_dispc, _idx, _reg); \
  478. })
  479. #define VID_REG_GET(dispc, hw_plane, idx, mask) \
  480. ((u32)FIELD_GET((mask), dispc_vid_read((dispc), (hw_plane), (idx))))
  481. #define VID_REG_FLD_MOD(dispc, hw_plane, idx, val, mask) \
  482. ({ \
  483. struct dispc_device *_dispc = (dispc); \
  484. u32 _hw_plane = (hw_plane); \
  485. u32 _idx = (idx); \
  486. u32 _reg = dispc_vid_read(_dispc, _hw_plane, _idx); \
  487. FIELD_MODIFY((mask), &_reg, (val)); \
  488. dispc_vid_write(_dispc, _hw_plane, _idx, _reg); \
  489. })
  490. #define VP_REG_GET(dispc, vp, idx, mask) \
  491. ((u32)FIELD_GET((mask), dispc_vp_read((dispc), (vp), (idx))))
  492. #define VP_REG_FLD_MOD(dispc, vp, idx, val, mask) \
  493. ({ \
  494. struct dispc_device *_dispc = (dispc); \
  495. u32 _vp = (vp); \
  496. u32 _idx = (idx); \
  497. u32 _reg = dispc_vp_read(_dispc, _vp, _idx); \
  498. FIELD_MODIFY((mask), &_reg, (val)); \
  499. dispc_vp_write(_dispc, _vp, _idx, _reg); \
  500. })
  501. #define OVR_REG_FLD_MOD(dispc, ovr, idx, val, mask) \
  502. ({ \
  503. struct dispc_device *_dispc = (dispc); \
  504. u32 _ovr = (ovr); \
  505. u32 _idx = (idx); \
  506. u32 _reg = dispc_ovr_read(_dispc, _ovr, _idx); \
  507. FIELD_MODIFY((mask), &_reg, (val)); \
  508. dispc_ovr_write(_dispc, _ovr, _idx, _reg); \
  509. })
  510. static dispc_irq_t dispc_vp_irq_from_raw(u32 stat, u32 hw_videoport)
  511. {
  512. dispc_irq_t vp_stat = 0;
  513. if (stat & BIT(0))
  514. vp_stat |= DSS_IRQ_VP_FRAME_DONE(hw_videoport);
  515. if (stat & BIT(1))
  516. vp_stat |= DSS_IRQ_VP_VSYNC_EVEN(hw_videoport);
  517. if (stat & BIT(2))
  518. vp_stat |= DSS_IRQ_VP_VSYNC_ODD(hw_videoport);
  519. if (stat & BIT(4))
  520. vp_stat |= DSS_IRQ_VP_SYNC_LOST(hw_videoport);
  521. return vp_stat;
  522. }
  523. static u32 dispc_vp_irq_to_raw(dispc_irq_t vpstat, u32 hw_videoport)
  524. {
  525. u32 stat = 0;
  526. if (vpstat & DSS_IRQ_VP_FRAME_DONE(hw_videoport))
  527. stat |= BIT(0);
  528. if (vpstat & DSS_IRQ_VP_VSYNC_EVEN(hw_videoport))
  529. stat |= BIT(1);
  530. if (vpstat & DSS_IRQ_VP_VSYNC_ODD(hw_videoport))
  531. stat |= BIT(2);
  532. if (vpstat & DSS_IRQ_VP_SYNC_LOST(hw_videoport))
  533. stat |= BIT(4);
  534. return stat;
  535. }
  536. static dispc_irq_t dispc_vid_irq_from_raw(u32 stat, u32 hw_plane)
  537. {
  538. dispc_irq_t vid_stat = 0;
  539. if (stat & BIT(0))
  540. vid_stat |= DSS_IRQ_PLANE_FIFO_UNDERFLOW(hw_plane);
  541. return vid_stat;
  542. }
  543. static u32 dispc_vid_irq_to_raw(dispc_irq_t vidstat, u32 hw_plane)
  544. {
  545. u32 stat = 0;
  546. if (vidstat & DSS_IRQ_PLANE_FIFO_UNDERFLOW(hw_plane))
  547. stat |= BIT(0);
  548. return stat;
  549. }
  550. static dispc_irq_t dispc_k2g_vp_read_irqstatus(struct dispc_device *dispc,
  551. u32 hw_videoport)
  552. {
  553. u32 stat = dispc_vp_read(dispc, hw_videoport, DISPC_VP_K2G_IRQSTATUS);
  554. return dispc_vp_irq_from_raw(stat, hw_videoport);
  555. }
  556. static void dispc_k2g_vp_write_irqstatus(struct dispc_device *dispc,
  557. u32 hw_videoport, dispc_irq_t vpstat)
  558. {
  559. u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
  560. dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_IRQSTATUS, stat);
  561. }
  562. static dispc_irq_t dispc_k2g_vid_read_irqstatus(struct dispc_device *dispc,
  563. u32 hw_plane)
  564. {
  565. u32 stat = dispc_vid_read(dispc, hw_plane, DISPC_VID_K2G_IRQSTATUS);
  566. return dispc_vid_irq_from_raw(stat, hw_plane);
  567. }
  568. static void dispc_k2g_vid_write_irqstatus(struct dispc_device *dispc,
  569. u32 hw_plane, dispc_irq_t vidstat)
  570. {
  571. u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
  572. dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_IRQSTATUS, stat);
  573. }
  574. static dispc_irq_t dispc_k2g_vp_read_irqenable(struct dispc_device *dispc,
  575. u32 hw_videoport)
  576. {
  577. u32 stat = dispc_vp_read(dispc, hw_videoport, DISPC_VP_K2G_IRQENABLE);
  578. return dispc_vp_irq_from_raw(stat, hw_videoport);
  579. }
  580. static void dispc_k2g_vp_set_irqenable(struct dispc_device *dispc,
  581. u32 hw_videoport, dispc_irq_t vpstat)
  582. {
  583. u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
  584. dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_IRQENABLE, stat);
  585. }
  586. static dispc_irq_t dispc_k2g_vid_read_irqenable(struct dispc_device *dispc,
  587. u32 hw_plane)
  588. {
  589. u32 stat = dispc_vid_read(dispc, hw_plane, DISPC_VID_K2G_IRQENABLE);
  590. return dispc_vid_irq_from_raw(stat, hw_plane);
  591. }
  592. static void dispc_k2g_vid_set_irqenable(struct dispc_device *dispc,
  593. u32 hw_plane, dispc_irq_t vidstat)
  594. {
  595. u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
  596. dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_IRQENABLE, stat);
  597. }
  598. static void dispc_k2g_clear_irqstatus(struct dispc_device *dispc,
  599. dispc_irq_t mask)
  600. {
  601. dispc_k2g_vp_write_irqstatus(dispc, 0, mask);
  602. dispc_k2g_vid_write_irqstatus(dispc, 0, mask);
  603. }
  604. static
  605. dispc_irq_t dispc_k2g_read_and_clear_irqstatus(struct dispc_device *dispc)
  606. {
  607. dispc_irq_t stat = 0;
  608. /* always clear the top level irqstatus */
  609. dispc_write(dispc, DISPC_IRQSTATUS,
  610. dispc_read(dispc, DISPC_IRQSTATUS));
  611. stat |= dispc_k2g_vp_read_irqstatus(dispc, 0);
  612. stat |= dispc_k2g_vid_read_irqstatus(dispc, 0);
  613. dispc_k2g_clear_irqstatus(dispc, stat);
  614. return stat;
  615. }
  616. static dispc_irq_t dispc_k2g_read_irqenable(struct dispc_device *dispc)
  617. {
  618. dispc_irq_t stat = 0;
  619. stat |= dispc_k2g_vp_read_irqenable(dispc, 0);
  620. stat |= dispc_k2g_vid_read_irqenable(dispc, 0);
  621. return stat;
  622. }
  623. static
  624. void dispc_k2g_set_irqenable(struct dispc_device *dispc, dispc_irq_t mask)
  625. {
  626. dispc_irq_t old_mask = dispc_k2g_read_irqenable(dispc);
  627. /* clear the irqstatus for irqs that will be enabled */
  628. dispc_k2g_clear_irqstatus(dispc, (mask ^ old_mask) & mask);
  629. dispc_k2g_vp_set_irqenable(dispc, 0, mask);
  630. dispc_k2g_vid_set_irqenable(dispc, 0, mask);
  631. dispc_write(dispc, DISPC_IRQENABLE_SET, (1 << 0) | (1 << 7));
  632. /* clear the irqstatus for irqs that were disabled */
  633. dispc_k2g_clear_irqstatus(dispc, (mask ^ old_mask) & old_mask);
  634. /* flush posted write */
  635. dispc_k2g_read_irqenable(dispc);
  636. }
  637. static dispc_irq_t dispc_k3_vp_read_irqstatus(struct dispc_device *dispc,
  638. u32 hw_videoport)
  639. {
  640. u32 stat = dispc_read(dispc, DISPC_VP_IRQSTATUS(hw_videoport));
  641. return dispc_vp_irq_from_raw(stat, hw_videoport);
  642. }
  643. static void dispc_k3_vp_write_irqstatus(struct dispc_device *dispc,
  644. u32 hw_videoport, dispc_irq_t vpstat)
  645. {
  646. u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
  647. dispc_write(dispc, DISPC_VP_IRQSTATUS(hw_videoport), stat);
  648. }
  649. static dispc_irq_t dispc_k3_vid_read_irqstatus(struct dispc_device *dispc,
  650. u32 hw_plane)
  651. {
  652. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  653. u32 stat = dispc_read(dispc, DISPC_VID_IRQSTATUS(hw_id));
  654. return dispc_vid_irq_from_raw(stat, hw_plane);
  655. }
  656. static void dispc_k3_vid_write_irqstatus(struct dispc_device *dispc,
  657. u32 hw_plane, dispc_irq_t vidstat)
  658. {
  659. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  660. u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
  661. dispc_write(dispc, DISPC_VID_IRQSTATUS(hw_id), stat);
  662. }
  663. static dispc_irq_t dispc_k3_vp_read_irqenable(struct dispc_device *dispc,
  664. u32 hw_videoport)
  665. {
  666. u32 stat = dispc_read(dispc, DISPC_VP_IRQENABLE(hw_videoport));
  667. return dispc_vp_irq_from_raw(stat, hw_videoport);
  668. }
  669. static void dispc_k3_vp_set_irqenable(struct dispc_device *dispc,
  670. u32 hw_videoport, dispc_irq_t vpstat)
  671. {
  672. u32 stat = dispc_vp_irq_to_raw(vpstat, hw_videoport);
  673. dispc_write(dispc, DISPC_VP_IRQENABLE(hw_videoport), stat);
  674. }
  675. static dispc_irq_t dispc_k3_vid_read_irqenable(struct dispc_device *dispc,
  676. u32 hw_plane)
  677. {
  678. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  679. u32 stat = dispc_read(dispc, DISPC_VID_IRQENABLE(hw_id));
  680. return dispc_vid_irq_from_raw(stat, hw_plane);
  681. }
  682. static void dispc_k3_vid_set_irqenable(struct dispc_device *dispc,
  683. u32 hw_plane, dispc_irq_t vidstat)
  684. {
  685. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  686. u32 stat = dispc_vid_irq_to_raw(vidstat, hw_plane);
  687. dispc_write(dispc, DISPC_VID_IRQENABLE(hw_id), stat);
  688. }
  689. static
  690. void dispc_k3_clear_irqstatus(struct dispc_device *dispc, dispc_irq_t clearmask)
  691. {
  692. unsigned int i;
  693. for (i = 0; i < dispc->feat->num_vps; ++i) {
  694. if (clearmask & DSS_IRQ_VP_MASK(i))
  695. dispc_k3_vp_write_irqstatus(dispc, i, clearmask);
  696. }
  697. for (i = 0; i < dispc->feat->num_vids; ++i) {
  698. if (clearmask & DSS_IRQ_PLANE_MASK(i))
  699. dispc_k3_vid_write_irqstatus(dispc, i, clearmask);
  700. }
  701. /* always clear the top level irqstatus */
  702. dispc_write(dispc, DISPC_IRQSTATUS, dispc_read(dispc, DISPC_IRQSTATUS));
  703. /* Flush posted writes */
  704. dispc_read(dispc, DISPC_IRQSTATUS);
  705. }
  706. static
  707. dispc_irq_t dispc_k3_read_and_clear_irqstatus(struct dispc_device *dispc)
  708. {
  709. dispc_irq_t status = 0;
  710. unsigned int i;
  711. for (i = 0; i < dispc->feat->num_vps; ++i)
  712. status |= dispc_k3_vp_read_irqstatus(dispc, i);
  713. for (i = 0; i < dispc->feat->num_vids; ++i)
  714. status |= dispc_k3_vid_read_irqstatus(dispc, i);
  715. dispc_k3_clear_irqstatus(dispc, status);
  716. return status;
  717. }
  718. static dispc_irq_t dispc_k3_read_irqenable(struct dispc_device *dispc)
  719. {
  720. dispc_irq_t enable = 0;
  721. unsigned int i;
  722. for (i = 0; i < dispc->feat->num_vps; ++i)
  723. enable |= dispc_k3_vp_read_irqenable(dispc, i);
  724. for (i = 0; i < dispc->feat->num_vids; ++i)
  725. enable |= dispc_k3_vid_read_irqenable(dispc, i);
  726. return enable;
  727. }
  728. static void dispc_k3_set_irqenable(struct dispc_device *dispc,
  729. dispc_irq_t mask)
  730. {
  731. unsigned int i;
  732. u32 main_enable = 0, main_disable = 0;
  733. dispc_irq_t old_mask;
  734. old_mask = dispc_k3_read_irqenable(dispc);
  735. /* clear the irqstatus for irqs that will be enabled */
  736. dispc_k3_clear_irqstatus(dispc, (old_mask ^ mask) & mask);
  737. for (i = 0; i < dispc->feat->num_vps; ++i) {
  738. dispc_k3_vp_set_irqenable(dispc, i, mask);
  739. if (mask & DSS_IRQ_VP_MASK(i))
  740. main_enable |= BIT(i); /* VP IRQ */
  741. else
  742. main_disable |= BIT(i); /* VP IRQ */
  743. }
  744. for (i = 0; i < dispc->feat->num_vids; ++i) {
  745. u32 hw_id = dispc->feat->vid_info[i].hw_id;
  746. dispc_k3_vid_set_irqenable(dispc, i, mask);
  747. if (mask & DSS_IRQ_PLANE_MASK(i))
  748. main_enable |= BIT(hw_id + 4); /* VID IRQ */
  749. else
  750. main_disable |= BIT(hw_id + 4); /* VID IRQ */
  751. }
  752. if (main_enable)
  753. dispc_write(dispc, DISPC_IRQENABLE_SET, main_enable);
  754. if (main_disable)
  755. dispc_write(dispc, DISPC_IRQENABLE_CLR, main_disable);
  756. /* clear the irqstatus for irqs that were disabled */
  757. dispc_k3_clear_irqstatus(dispc, (old_mask ^ mask) & old_mask);
  758. /* Flush posted writes */
  759. dispc_read(dispc, DISPC_IRQENABLE_SET);
  760. }
  761. dispc_irq_t dispc_read_and_clear_irqstatus(struct dispc_device *dispc)
  762. {
  763. switch (dispc->feat->subrev) {
  764. case DISPC_K2G:
  765. return dispc_k2g_read_and_clear_irqstatus(dispc);
  766. case DISPC_AM625:
  767. case DISPC_AM62A7:
  768. case DISPC_AM62L:
  769. case DISPC_AM65X:
  770. case DISPC_J721E:
  771. return dispc_k3_read_and_clear_irqstatus(dispc);
  772. default:
  773. WARN_ON(1);
  774. return 0;
  775. }
  776. }
  777. void dispc_set_irqenable(struct dispc_device *dispc, dispc_irq_t mask)
  778. {
  779. switch (dispc->feat->subrev) {
  780. case DISPC_K2G:
  781. dispc_k2g_set_irqenable(dispc, mask);
  782. break;
  783. case DISPC_AM625:
  784. case DISPC_AM62A7:
  785. case DISPC_AM62L:
  786. case DISPC_AM65X:
  787. case DISPC_J721E:
  788. dispc_k3_set_irqenable(dispc, mask);
  789. break;
  790. default:
  791. WARN_ON(1);
  792. break;
  793. }
  794. }
  795. struct dispc_bus_format {
  796. u32 bus_fmt;
  797. u32 data_width;
  798. bool is_oldi_fmt;
  799. enum oldi_mode_reg_val am65x_oldi_mode_reg_val;
  800. };
  801. static const struct dispc_bus_format dispc_bus_formats[] = {
  802. { MEDIA_BUS_FMT_RGB444_1X12, 12, false, 0 },
  803. { MEDIA_BUS_FMT_RGB565_1X16, 16, false, 0 },
  804. { MEDIA_BUS_FMT_RGB666_1X18, 18, false, 0 },
  805. { MEDIA_BUS_FMT_RGB888_1X24, 24, false, 0 },
  806. { MEDIA_BUS_FMT_RGB101010_1X30, 30, false, 0 },
  807. { MEDIA_BUS_FMT_RGB121212_1X36, 36, false, 0 },
  808. { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, true, SPWG_18 },
  809. { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, true, SPWG_24 },
  810. { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, true, JEIDA_24 },
  811. };
  812. static const
  813. struct dispc_bus_format *dispc_vp_find_bus_fmt(struct dispc_device *dispc,
  814. u32 hw_videoport,
  815. u32 bus_fmt, u32 bus_flags)
  816. {
  817. unsigned int i;
  818. for (i = 0; i < ARRAY_SIZE(dispc_bus_formats); ++i) {
  819. if (dispc_bus_formats[i].bus_fmt == bus_fmt)
  820. return &dispc_bus_formats[i];
  821. }
  822. return NULL;
  823. }
  824. int dispc_vp_bus_check(struct dispc_device *dispc, u32 hw_videoport,
  825. const struct drm_crtc_state *state)
  826. {
  827. struct tidss_device *tidss = dispc->tidss;
  828. struct drm_device *dev = &tidss->ddev;
  829. const struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
  830. const struct dispc_bus_format *fmt;
  831. fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
  832. tstate->bus_flags);
  833. if (!fmt) {
  834. drm_dbg(dev, "%s: Unsupported bus format: %u\n",
  835. __func__, tstate->bus_format);
  836. return -EINVAL;
  837. }
  838. if (dispc->feat->vp_bus_type[hw_videoport] != DISPC_VP_OLDI_AM65X &&
  839. fmt->is_oldi_fmt) {
  840. drm_dbg(dev, "%s: %s is not OLDI-port\n",
  841. __func__, dispc->feat->vp_name[hw_videoport]);
  842. return -EINVAL;
  843. }
  844. return 0;
  845. }
  846. static void dispc_am65x_oldi_tx_power(struct dispc_device *dispc, bool power)
  847. {
  848. u32 val = power ? 0 : AM65X_OLDI_PWRDN_TX;
  849. if (WARN_ON(!dispc->am65x_oldi_io_ctrl))
  850. return;
  851. regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT0_IO_CTRL,
  852. AM65X_OLDI_PWRDN_TX, val);
  853. regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT1_IO_CTRL,
  854. AM65X_OLDI_PWRDN_TX, val);
  855. regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT2_IO_CTRL,
  856. AM65X_OLDI_PWRDN_TX, val);
  857. regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_DAT3_IO_CTRL,
  858. AM65X_OLDI_PWRDN_TX, val);
  859. regmap_update_bits(dispc->am65x_oldi_io_ctrl, AM65X_OLDI_CLK_IO_CTRL,
  860. AM65X_OLDI_PWRDN_TX, val);
  861. }
  862. static void dispc_set_num_datalines(struct dispc_device *dispc,
  863. u32 hw_videoport, int num_lines)
  864. {
  865. int v;
  866. switch (num_lines) {
  867. case 12:
  868. v = 0; break;
  869. case 16:
  870. v = 1; break;
  871. case 18:
  872. v = 2; break;
  873. case 24:
  874. v = 3; break;
  875. case 30:
  876. v = 4; break;
  877. case 36:
  878. v = 5; break;
  879. default:
  880. WARN_ON(1);
  881. v = 3;
  882. }
  883. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, v,
  884. DISPC_VP_CONTROL_DATALINES_MASK);
  885. }
  886. static void dispc_enable_am65x_oldi(struct dispc_device *dispc, u32 hw_videoport,
  887. const struct dispc_bus_format *fmt)
  888. {
  889. u32 oldi_cfg = 0;
  890. u32 oldi_reset_bit = BIT(5 + hw_videoport);
  891. int count = 0;
  892. /*
  893. * For the moment DUALMODESYNC, MASTERSLAVE, MODE, and SRC
  894. * bits of DISPC_VP_DSS_OLDI_CFG are set statically to 0.
  895. */
  896. if (fmt->data_width == 24)
  897. oldi_cfg |= BIT(8); /* MSB */
  898. else if (fmt->data_width != 18)
  899. dev_warn(dispc->dev, "%s: %d port width not supported\n",
  900. __func__, fmt->data_width);
  901. oldi_cfg |= BIT(7); /* DEPOL */
  902. FIELD_MODIFY(DISPC_VP_DSS_OLDI_CFG_MAP_MASK, &oldi_cfg,
  903. fmt->am65x_oldi_mode_reg_val);
  904. oldi_cfg |= BIT(12); /* SOFTRST */
  905. oldi_cfg |= BIT(0); /* ENABLE */
  906. dispc_vp_write(dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, oldi_cfg);
  907. while (!(oldi_reset_bit & dispc_read(dispc, DSS_SYSSTATUS)) &&
  908. count < 10000)
  909. count++;
  910. if (!(oldi_reset_bit & dispc_read(dispc, DSS_SYSSTATUS)))
  911. dev_warn(dispc->dev, "%s: timeout waiting OLDI reset done\n",
  912. __func__);
  913. }
  914. void dispc_vp_prepare(struct dispc_device *dispc, u32 hw_videoport,
  915. const struct drm_crtc_state *state)
  916. {
  917. const struct tidss_crtc_state *tstate = to_tidss_crtc_state(state);
  918. const struct dispc_bus_format *fmt;
  919. const struct drm_display_mode *mode = &state->adjusted_mode;
  920. bool align, onoff, rf, ieo, ipc, ihs, ivs;
  921. u32 hsw, hfp, hbp, vsw, vfp, vbp;
  922. fmt = dispc_vp_find_bus_fmt(dispc, hw_videoport, tstate->bus_format,
  923. tstate->bus_flags);
  924. if (WARN_ON(!fmt))
  925. return;
  926. if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
  927. dispc_am65x_oldi_tx_power(dispc, true);
  928. dispc_enable_am65x_oldi(dispc, hw_videoport, fmt);
  929. }
  930. dispc_set_num_datalines(dispc, hw_videoport, fmt->data_width);
  931. hfp = mode->crtc_hsync_start - mode->crtc_hdisplay;
  932. hsw = mode->crtc_hsync_end - mode->crtc_hsync_start;
  933. hbp = mode->crtc_htotal - mode->crtc_hsync_end;
  934. vfp = mode->crtc_vsync_start - mode->crtc_vdisplay;
  935. vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
  936. vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
  937. dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_H,
  938. FIELD_PREP(DISPC_VP_TIMING_H_SYNC_PULSE_MASK, hsw - 1) |
  939. FIELD_PREP(DISPC_VP_TIMING_H_FRONT_PORCH_MASK, hfp - 1) |
  940. FIELD_PREP(DISPC_VP_TIMING_H_BACK_PORCH_MASK, hbp - 1));
  941. dispc_vp_write(dispc, hw_videoport, DISPC_VP_TIMING_V,
  942. FIELD_PREP(DISPC_VP_TIMING_V_SYNC_PULSE_MASK, vsw - 1) |
  943. FIELD_PREP(DISPC_VP_TIMING_V_FRONT_PORCH_MASK, vfp) |
  944. FIELD_PREP(DISPC_VP_TIMING_V_BACK_PORCH_MASK, vbp));
  945. ivs = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
  946. ihs = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
  947. ieo = !!(tstate->bus_flags & DRM_BUS_FLAG_DE_LOW);
  948. ipc = !!(tstate->bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE);
  949. /* always use the 'rf' setting */
  950. onoff = true;
  951. rf = !!(tstate->bus_flags & DRM_BUS_FLAG_SYNC_DRIVE_POSEDGE);
  952. /* always use aligned syncs */
  953. align = true;
  954. /* always use DE_HIGH for OLDI */
  955. if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X)
  956. ieo = false;
  957. dispc_vp_write(dispc, hw_videoport, DISPC_VP_POL_FREQ,
  958. FIELD_PREP(DISPC_VP_POL_FREQ_ALIGN_MASK, align) |
  959. FIELD_PREP(DISPC_VP_POL_FREQ_ONOFF_MASK, onoff) |
  960. FIELD_PREP(DISPC_VP_POL_FREQ_RF_MASK, rf) |
  961. FIELD_PREP(DISPC_VP_POL_FREQ_IEO_MASK, ieo) |
  962. FIELD_PREP(DISPC_VP_POL_FREQ_IPC_MASK, ipc) |
  963. FIELD_PREP(DISPC_VP_POL_FREQ_IHS_MASK, ihs) |
  964. FIELD_PREP(DISPC_VP_POL_FREQ_IVS_MASK, ivs));
  965. dispc_vp_write(dispc, hw_videoport, DISPC_VP_SIZE_SCREEN,
  966. FIELD_PREP(DISPC_VP_SIZE_SCREEN_HDISPLAY_MASK,
  967. mode->crtc_hdisplay - 1) |
  968. FIELD_PREP(DISPC_VP_SIZE_SCREEN_VDISPLAY_MASK,
  969. mode->crtc_vdisplay - 1));
  970. }
  971. void dispc_vp_enable(struct dispc_device *dispc, u32 hw_videoport)
  972. {
  973. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
  974. DISPC_VP_CONTROL_ENABLE_MASK);
  975. }
  976. void dispc_vp_disable(struct dispc_device *dispc, u32 hw_videoport)
  977. {
  978. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 0,
  979. DISPC_VP_CONTROL_ENABLE_MASK);
  980. }
  981. void dispc_vp_unprepare(struct dispc_device *dispc, u32 hw_videoport)
  982. {
  983. if (dispc->feat->vp_bus_type[hw_videoport] == DISPC_VP_OLDI_AM65X) {
  984. dispc_vp_write(dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG, 0);
  985. dispc_am65x_oldi_tx_power(dispc, false);
  986. }
  987. }
  988. bool dispc_vp_go_busy(struct dispc_device *dispc, u32 hw_videoport)
  989. {
  990. return VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
  991. DISPC_VP_CONTROL_GOBIT_MASK);
  992. }
  993. void dispc_vp_go(struct dispc_device *dispc, u32 hw_videoport)
  994. {
  995. WARN_ON(VP_REG_GET(dispc, hw_videoport, DISPC_VP_CONTROL,
  996. DISPC_VP_CONTROL_GOBIT_MASK));
  997. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONTROL, 1,
  998. DISPC_VP_CONTROL_GOBIT_MASK);
  999. }
  1000. enum c8_to_c12_mode { C8_TO_C12_REPLICATE, C8_TO_C12_MAX, C8_TO_C12_MIN };
  1001. static u16 c8_to_c12(u8 c8, enum c8_to_c12_mode mode)
  1002. {
  1003. u16 c12;
  1004. c12 = c8 << 4;
  1005. switch (mode) {
  1006. case C8_TO_C12_REPLICATE:
  1007. /* Copy c8 4 MSB to 4 LSB for full scale c12 */
  1008. c12 |= c8 >> 4;
  1009. break;
  1010. case C8_TO_C12_MAX:
  1011. c12 |= 0xF;
  1012. break;
  1013. default:
  1014. case C8_TO_C12_MIN:
  1015. break;
  1016. }
  1017. return c12;
  1018. }
  1019. static u64 argb8888_to_argb12121212(u32 argb8888, enum c8_to_c12_mode m)
  1020. {
  1021. u8 a, r, g, b;
  1022. u64 v;
  1023. a = (argb8888 >> 24) & 0xff;
  1024. r = (argb8888 >> 16) & 0xff;
  1025. g = (argb8888 >> 8) & 0xff;
  1026. b = (argb8888 >> 0) & 0xff;
  1027. v = ((u64)c8_to_c12(a, m) << 36) | ((u64)c8_to_c12(r, m) << 24) |
  1028. ((u64)c8_to_c12(g, m) << 12) | (u64)c8_to_c12(b, m);
  1029. return v;
  1030. }
  1031. static void dispc_vp_set_default_color(struct dispc_device *dispc,
  1032. u32 hw_videoport, u32 default_color)
  1033. {
  1034. u64 v;
  1035. v = argb8888_to_argb12121212(default_color, C8_TO_C12_REPLICATE);
  1036. dispc_ovr_write(dispc, hw_videoport,
  1037. DISPC_OVR_DEFAULT_COLOR, v & 0xffffffff);
  1038. dispc_ovr_write(dispc, hw_videoport,
  1039. DISPC_OVR_DEFAULT_COLOR2, (v >> 32) & 0xffff);
  1040. }
  1041. /*
  1042. * Calculate the percentage difference between the requested pixel clock rate
  1043. * and the effective rate resulting from calculating the clock divider value.
  1044. */
  1045. unsigned int dispc_pclk_diff(unsigned long rate, unsigned long real_rate)
  1046. {
  1047. int r = rate / 100, rr = real_rate / 100;
  1048. return (unsigned int)(abs(((rr - r) * 100) / r));
  1049. }
  1050. static int check_pixel_clock(struct dispc_device *dispc, u32 hw_videoport,
  1051. unsigned long clock)
  1052. {
  1053. unsigned long round_clock;
  1054. /*
  1055. * For VP's with external clocking, clock operations must be
  1056. * delegated to respective driver, so we skip the check here.
  1057. */
  1058. if (dispc->tidss->is_ext_vp_clk[hw_videoport])
  1059. return 0;
  1060. round_clock = clk_round_rate(dispc->vp_clk[hw_videoport], clock);
  1061. /*
  1062. * To keep the check consistent with dispc_vp_set_clk_rate(), we
  1063. * use the same 5% check here.
  1064. */
  1065. if (dispc_pclk_diff(clock, round_clock) > 5)
  1066. return -EINVAL;
  1067. return 0;
  1068. }
  1069. enum drm_mode_status dispc_vp_mode_valid(struct dispc_device *dispc,
  1070. u32 hw_videoport,
  1071. const struct drm_display_mode *mode)
  1072. {
  1073. u32 hsw, hfp, hbp, vsw, vfp, vbp;
  1074. enum dispc_vp_bus_type bus_type;
  1075. bus_type = dispc->feat->vp_bus_type[hw_videoport];
  1076. if (WARN_ON(bus_type == DISPC_VP_TIED_OFF))
  1077. return MODE_BAD;
  1078. if (mode->hdisplay > 4096)
  1079. return MODE_BAD;
  1080. if (mode->vdisplay > 4096)
  1081. return MODE_BAD;
  1082. if (check_pixel_clock(dispc, hw_videoport, mode->clock * 1000))
  1083. return MODE_CLOCK_RANGE;
  1084. /* TODO: add interlace support */
  1085. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1086. return MODE_NO_INTERLACE;
  1087. /*
  1088. * Enforce the output width is divisible by 2. Actually this
  1089. * is only needed in following cases:
  1090. * - YUV output selected (BT656, BT1120)
  1091. * - Dithering enabled
  1092. * - TDM with TDMCycleFormat == 3
  1093. * But for simplicity we enforce that always.
  1094. */
  1095. if ((mode->hdisplay % 2) != 0)
  1096. return MODE_BAD_HVALUE;
  1097. hfp = mode->hsync_start - mode->hdisplay;
  1098. hsw = mode->hsync_end - mode->hsync_start;
  1099. hbp = mode->htotal - mode->hsync_end;
  1100. vfp = mode->vsync_start - mode->vdisplay;
  1101. vsw = mode->vsync_end - mode->vsync_start;
  1102. vbp = mode->vtotal - mode->vsync_end;
  1103. if (hsw < 1 || hsw > 256 ||
  1104. hfp < 1 || hfp > 4096 ||
  1105. hbp < 1 || hbp > 4096)
  1106. return MODE_BAD_HVALUE;
  1107. if (vsw < 1 || vsw > 256 ||
  1108. vfp > 4095 || vbp > 4095)
  1109. return MODE_BAD_VVALUE;
  1110. if (dispc->memory_bandwidth_limit) {
  1111. const unsigned int bpp = 4;
  1112. u64 bandwidth;
  1113. bandwidth = 1000 * mode->clock;
  1114. bandwidth = bandwidth * mode->hdisplay * mode->vdisplay * bpp;
  1115. bandwidth = div_u64(bandwidth, mode->htotal * mode->vtotal);
  1116. if (dispc->memory_bandwidth_limit < bandwidth)
  1117. return MODE_BAD;
  1118. }
  1119. return MODE_OK;
  1120. }
  1121. int dispc_vp_enable_clk(struct dispc_device *dispc, u32 hw_videoport)
  1122. {
  1123. int ret = clk_prepare_enable(dispc->vp_clk[hw_videoport]);
  1124. if (ret)
  1125. dev_err(dispc->dev, "%s: enabling clk failed: %d\n", __func__,
  1126. ret);
  1127. return ret;
  1128. }
  1129. void dispc_vp_disable_clk(struct dispc_device *dispc, u32 hw_videoport)
  1130. {
  1131. clk_disable_unprepare(dispc->vp_clk[hw_videoport]);
  1132. }
  1133. int dispc_vp_set_clk_rate(struct dispc_device *dispc, u32 hw_videoport,
  1134. unsigned long rate)
  1135. {
  1136. int r;
  1137. unsigned long new_rate;
  1138. r = clk_set_rate(dispc->vp_clk[hw_videoport], rate);
  1139. if (r) {
  1140. dev_err(dispc->dev, "vp%d: failed to set clk rate to %lu\n",
  1141. hw_videoport, rate);
  1142. return r;
  1143. }
  1144. new_rate = clk_get_rate(dispc->vp_clk[hw_videoport]);
  1145. if (dispc_pclk_diff(rate, new_rate) > 5)
  1146. dev_warn(dispc->dev,
  1147. "vp%d: Clock rate %lu differs over 5%% from requested %lu\n",
  1148. hw_videoport, new_rate, rate);
  1149. dev_dbg(dispc->dev, "vp%d: new rate %lu Hz (requested %lu Hz)\n",
  1150. hw_videoport, clk_get_rate(dispc->vp_clk[hw_videoport]), rate);
  1151. return 0;
  1152. }
  1153. /* OVR */
  1154. static void dispc_k2g_ovr_set_plane(struct dispc_device *dispc,
  1155. u32 hw_plane, u32 hw_videoport,
  1156. u32 x, u32 y, u32 layer)
  1157. {
  1158. /* On k2g there is only one plane and no need for ovr */
  1159. dispc_vid_write(dispc, hw_plane, DISPC_VID_K2G_POSITION,
  1160. x | (y << 16));
  1161. }
  1162. static void dispc_am65x_ovr_set_plane(struct dispc_device *dispc,
  1163. u32 hw_plane, u32 hw_videoport,
  1164. u32 x, u32 y, u32 layer)
  1165. {
  1166. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  1167. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
  1168. hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
  1169. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), x,
  1170. DISPC_OVR_ATTRIBUTES_POSX_MASK);
  1171. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer), y,
  1172. DISPC_OVR_ATTRIBUTES_POSY_MASK);
  1173. }
  1174. static void dispc_j721e_ovr_set_plane(struct dispc_device *dispc,
  1175. u32 hw_plane, u32 hw_videoport,
  1176. u32 x, u32 y, u32 layer)
  1177. {
  1178. u32 hw_id = dispc->feat->vid_info[hw_plane].hw_id;
  1179. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
  1180. hw_id, DISPC_OVR_ATTRIBUTES_CHANNELIN_MASK);
  1181. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), x,
  1182. DISPC_OVR_ATTRIBUTES2_POSX_MASK);
  1183. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES2(layer), y,
  1184. DISPC_OVR_ATTRIBUTES2_POSY_MASK);
  1185. }
  1186. void dispc_ovr_set_plane(struct dispc_device *dispc, u32 hw_plane,
  1187. u32 hw_videoport, u32 x, u32 y, u32 layer)
  1188. {
  1189. switch (dispc->feat->subrev) {
  1190. case DISPC_K2G:
  1191. dispc_k2g_ovr_set_plane(dispc, hw_plane, hw_videoport,
  1192. x, y, layer);
  1193. break;
  1194. case DISPC_AM625:
  1195. case DISPC_AM62A7:
  1196. case DISPC_AM62L:
  1197. case DISPC_AM65X:
  1198. dispc_am65x_ovr_set_plane(dispc, hw_plane, hw_videoport,
  1199. x, y, layer);
  1200. break;
  1201. case DISPC_J721E:
  1202. dispc_j721e_ovr_set_plane(dispc, hw_plane, hw_videoport,
  1203. x, y, layer);
  1204. break;
  1205. default:
  1206. WARN_ON(1);
  1207. break;
  1208. }
  1209. }
  1210. void dispc_ovr_enable_layer(struct dispc_device *dispc,
  1211. u32 hw_videoport, u32 layer, bool enable)
  1212. {
  1213. if (dispc->feat->subrev == DISPC_K2G)
  1214. return;
  1215. OVR_REG_FLD_MOD(dispc, hw_videoport, DISPC_OVR_ATTRIBUTES(layer),
  1216. !!enable, DISPC_OVR_ATTRIBUTES_ENABLE_MASK);
  1217. }
  1218. /* CSC */
  1219. enum csc_ctm {
  1220. CSC_RR, CSC_RG, CSC_RB,
  1221. CSC_GR, CSC_GG, CSC_GB,
  1222. CSC_BR, CSC_BG, CSC_BB,
  1223. };
  1224. enum csc_yuv2rgb {
  1225. CSC_RY, CSC_RCB, CSC_RCR,
  1226. CSC_GY, CSC_GCB, CSC_GCR,
  1227. CSC_BY, CSC_BCB, CSC_BCR,
  1228. };
  1229. enum csc_rgb2yuv {
  1230. CSC_YR, CSC_YG, CSC_YB,
  1231. CSC_CBR, CSC_CBG, CSC_CBB,
  1232. CSC_CRR, CSC_CRG, CSC_CRB,
  1233. };
  1234. struct dispc_csc_coef {
  1235. void (*to_regval)(const struct dispc_csc_coef *csc, u32 *regval);
  1236. int m[9];
  1237. int preoffset[3];
  1238. int postoffset[3];
  1239. enum { CLIP_LIMITED_RANGE = 0, CLIP_FULL_RANGE = 1, } cliping;
  1240. const char *name;
  1241. };
  1242. #define DISPC_CSC_REGVAL_LEN 8
  1243. static
  1244. void dispc_csc_offset_regval(const struct dispc_csc_coef *csc, u32 *regval)
  1245. {
  1246. #define OVAL(x, y) (FIELD_PREP(GENMASK(15, 3), x) | FIELD_PREP(GENMASK(31, 19), y))
  1247. regval[5] = OVAL(csc->preoffset[0], csc->preoffset[1]);
  1248. regval[6] = OVAL(csc->preoffset[2], csc->postoffset[0]);
  1249. regval[7] = OVAL(csc->postoffset[1], csc->postoffset[2]);
  1250. #undef OVAL
  1251. }
  1252. #define CVAL(x, y) (FIELD_PREP(GENMASK(10, 0), x) | FIELD_PREP(GENMASK(26, 16), y))
  1253. static
  1254. void dispc_csc_yuv2rgb_regval(const struct dispc_csc_coef *csc, u32 *regval)
  1255. {
  1256. regval[0] = CVAL(csc->m[CSC_RY], csc->m[CSC_RCR]);
  1257. regval[1] = CVAL(csc->m[CSC_RCB], csc->m[CSC_GY]);
  1258. regval[2] = CVAL(csc->m[CSC_GCR], csc->m[CSC_GCB]);
  1259. regval[3] = CVAL(csc->m[CSC_BY], csc->m[CSC_BCR]);
  1260. regval[4] = CVAL(csc->m[CSC_BCB], 0);
  1261. dispc_csc_offset_regval(csc, regval);
  1262. }
  1263. __maybe_unused static
  1264. void dispc_csc_rgb2yuv_regval(const struct dispc_csc_coef *csc, u32 *regval)
  1265. {
  1266. regval[0] = CVAL(csc->m[CSC_YR], csc->m[CSC_YG]);
  1267. regval[1] = CVAL(csc->m[CSC_YB], csc->m[CSC_CRR]);
  1268. regval[2] = CVAL(csc->m[CSC_CRG], csc->m[CSC_CRB]);
  1269. regval[3] = CVAL(csc->m[CSC_CBR], csc->m[CSC_CBG]);
  1270. regval[4] = CVAL(csc->m[CSC_CBB], 0);
  1271. dispc_csc_offset_regval(csc, regval);
  1272. }
  1273. static void dispc_csc_cpr_regval(const struct dispc_csc_coef *csc,
  1274. u32 *regval)
  1275. {
  1276. regval[0] = CVAL(csc->m[CSC_RR], csc->m[CSC_RG]);
  1277. regval[1] = CVAL(csc->m[CSC_RB], csc->m[CSC_GR]);
  1278. regval[2] = CVAL(csc->m[CSC_GG], csc->m[CSC_GB]);
  1279. regval[3] = CVAL(csc->m[CSC_BR], csc->m[CSC_BG]);
  1280. regval[4] = CVAL(csc->m[CSC_BB], 0);
  1281. dispc_csc_offset_regval(csc, regval);
  1282. }
  1283. #undef CVAL
  1284. static void dispc_k2g_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
  1285. const struct dispc_csc_coef *csc)
  1286. {
  1287. static const u16 dispc_vid_csc_coef_reg[] = {
  1288. DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
  1289. DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
  1290. DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
  1291. DISPC_VID_CSC_COEF(6), /* K2G has no post offset support */
  1292. };
  1293. u32 regval[DISPC_CSC_REGVAL_LEN];
  1294. unsigned int i;
  1295. csc->to_regval(csc, regval);
  1296. if (regval[7] != 0)
  1297. dev_warn(dispc->dev, "%s: No post offset support for %s\n",
  1298. __func__, csc->name);
  1299. for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
  1300. dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
  1301. regval[i]);
  1302. }
  1303. static void dispc_k3_vid_write_csc(struct dispc_device *dispc, u32 hw_plane,
  1304. const struct dispc_csc_coef *csc)
  1305. {
  1306. static const u16 dispc_vid_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
  1307. DISPC_VID_CSC_COEF(0), DISPC_VID_CSC_COEF(1),
  1308. DISPC_VID_CSC_COEF(2), DISPC_VID_CSC_COEF(3),
  1309. DISPC_VID_CSC_COEF(4), DISPC_VID_CSC_COEF(5),
  1310. DISPC_VID_CSC_COEF(6), DISPC_VID_CSC_COEF7,
  1311. };
  1312. u32 regval[DISPC_CSC_REGVAL_LEN];
  1313. unsigned int i;
  1314. csc->to_regval(csc, regval);
  1315. for (i = 0; i < ARRAY_SIZE(dispc_vid_csc_coef_reg); i++)
  1316. dispc_vid_write(dispc, hw_plane, dispc_vid_csc_coef_reg[i],
  1317. regval[i]);
  1318. }
  1319. /* YUV -> RGB, ITU-R BT.601, full range */
  1320. static const struct dispc_csc_coef csc_yuv2rgb_bt601_full = {
  1321. dispc_csc_yuv2rgb_regval,
  1322. { 256, 0, 358, /* ry, rcb, rcr |1.000 0.000 1.402|*/
  1323. 256, -88, -182, /* gy, gcb, gcr |1.000 -0.344 -0.714|*/
  1324. 256, 452, 0, }, /* by, bcb, bcr |1.000 1.772 0.000|*/
  1325. { 0, -2048, -2048, }, /* full range */
  1326. { 0, 0, 0, },
  1327. CLIP_FULL_RANGE,
  1328. "BT.601 Full",
  1329. };
  1330. /* YUV -> RGB, ITU-R BT.601, limited range */
  1331. static const struct dispc_csc_coef csc_yuv2rgb_bt601_lim = {
  1332. dispc_csc_yuv2rgb_regval,
  1333. { 298, 0, 409, /* ry, rcb, rcr |1.164 0.000 1.596|*/
  1334. 298, -100, -208, /* gy, gcb, gcr |1.164 -0.392 -0.813|*/
  1335. 298, 516, 0, }, /* by, bcb, bcr |1.164 2.017 0.000|*/
  1336. { -256, -2048, -2048, }, /* limited range */
  1337. { 0, 0, 0, },
  1338. CLIP_FULL_RANGE,
  1339. "BT.601 Limited",
  1340. };
  1341. /* YUV -> RGB, ITU-R BT.709, full range */
  1342. static const struct dispc_csc_coef csc_yuv2rgb_bt709_full = {
  1343. dispc_csc_yuv2rgb_regval,
  1344. { 256, 0, 402, /* ry, rcb, rcr |1.000 0.000 1.570|*/
  1345. 256, -48, -120, /* gy, gcb, gcr |1.000 -0.187 -0.467|*/
  1346. 256, 475, 0, }, /* by, bcb, bcr |1.000 1.856 0.000|*/
  1347. { 0, -2048, -2048, }, /* full range */
  1348. { 0, 0, 0, },
  1349. CLIP_FULL_RANGE,
  1350. "BT.709 Full",
  1351. };
  1352. /* YUV -> RGB, ITU-R BT.709, limited range */
  1353. static const struct dispc_csc_coef csc_yuv2rgb_bt709_lim = {
  1354. dispc_csc_yuv2rgb_regval,
  1355. { 298, 0, 459, /* ry, rcb, rcr |1.164 0.000 1.793|*/
  1356. 298, -55, -136, /* gy, gcb, gcr |1.164 -0.213 -0.533|*/
  1357. 298, 541, 0, }, /* by, bcb, bcr |1.164 2.112 0.000|*/
  1358. { -256, -2048, -2048, }, /* limited range */
  1359. { 0, 0, 0, },
  1360. CLIP_FULL_RANGE,
  1361. "BT.709 Limited",
  1362. };
  1363. static const struct {
  1364. enum drm_color_encoding encoding;
  1365. enum drm_color_range range;
  1366. const struct dispc_csc_coef *csc;
  1367. } dispc_csc_table[] = {
  1368. { DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_FULL_RANGE,
  1369. &csc_yuv2rgb_bt601_full, },
  1370. { DRM_COLOR_YCBCR_BT601, DRM_COLOR_YCBCR_LIMITED_RANGE,
  1371. &csc_yuv2rgb_bt601_lim, },
  1372. { DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_FULL_RANGE,
  1373. &csc_yuv2rgb_bt709_full, },
  1374. { DRM_COLOR_YCBCR_BT709, DRM_COLOR_YCBCR_LIMITED_RANGE,
  1375. &csc_yuv2rgb_bt709_lim, },
  1376. };
  1377. static const
  1378. struct dispc_csc_coef *dispc_find_csc(enum drm_color_encoding encoding,
  1379. enum drm_color_range range)
  1380. {
  1381. unsigned int i;
  1382. for (i = 0; i < ARRAY_SIZE(dispc_csc_table); i++) {
  1383. if (dispc_csc_table[i].encoding == encoding &&
  1384. dispc_csc_table[i].range == range) {
  1385. return dispc_csc_table[i].csc;
  1386. }
  1387. }
  1388. return NULL;
  1389. }
  1390. static void dispc_vid_csc_setup(struct dispc_device *dispc, u32 hw_plane,
  1391. const struct drm_plane_state *state)
  1392. {
  1393. const struct dispc_csc_coef *coef;
  1394. coef = dispc_find_csc(state->color_encoding, state->color_range);
  1395. if (!coef) {
  1396. dev_err(dispc->dev, "%s: CSC (%u,%u) not found\n",
  1397. __func__, state->color_encoding, state->color_range);
  1398. return;
  1399. }
  1400. if (dispc->feat->subrev == DISPC_K2G)
  1401. dispc_k2g_vid_write_csc(dispc, hw_plane, coef);
  1402. else
  1403. dispc_k3_vid_write_csc(dispc, hw_plane, coef);
  1404. }
  1405. static void dispc_vid_csc_enable(struct dispc_device *dispc, u32 hw_plane,
  1406. bool enable)
  1407. {
  1408. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
  1409. DISPC_VID_ATTRIBUTES_COLORCONVENABLE_MASK);
  1410. }
  1411. /* SCALER */
  1412. static u32 dispc_calc_fir_inc(u32 in, u32 out)
  1413. {
  1414. return (u32)div_u64(0x200000ull * in, out);
  1415. }
  1416. enum dispc_vid_fir_coef_set {
  1417. DISPC_VID_FIR_COEF_HORIZ,
  1418. DISPC_VID_FIR_COEF_HORIZ_UV,
  1419. DISPC_VID_FIR_COEF_VERT,
  1420. DISPC_VID_FIR_COEF_VERT_UV,
  1421. };
  1422. static void dispc_vid_write_fir_coefs(struct dispc_device *dispc,
  1423. u32 hw_plane,
  1424. enum dispc_vid_fir_coef_set coef_set,
  1425. const struct tidss_scale_coefs *coefs)
  1426. {
  1427. static const u16 c0_regs[] = {
  1428. [DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H0,
  1429. [DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H0_C,
  1430. [DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V0,
  1431. [DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V0_C,
  1432. };
  1433. static const u16 c12_regs[] = {
  1434. [DISPC_VID_FIR_COEF_HORIZ] = DISPC_VID_FIR_COEFS_H12,
  1435. [DISPC_VID_FIR_COEF_HORIZ_UV] = DISPC_VID_FIR_COEFS_H12_C,
  1436. [DISPC_VID_FIR_COEF_VERT] = DISPC_VID_FIR_COEFS_V12,
  1437. [DISPC_VID_FIR_COEF_VERT_UV] = DISPC_VID_FIR_COEFS_V12_C,
  1438. };
  1439. const u16 c0_base = c0_regs[coef_set];
  1440. const u16 c12_base = c12_regs[coef_set];
  1441. int phase;
  1442. if (!coefs) {
  1443. dev_err(dispc->dev, "%s: No coefficients given.\n", __func__);
  1444. return;
  1445. }
  1446. for (phase = 0; phase <= 8; ++phase) {
  1447. u16 reg = c0_base + phase * 4;
  1448. u16 c0 = coefs->c0[phase];
  1449. dispc_vid_write(dispc, hw_plane, reg, c0);
  1450. }
  1451. for (phase = 0; phase <= 15; ++phase) {
  1452. u16 reg = c12_base + phase * 4;
  1453. s16 c1, c2;
  1454. u32 c12;
  1455. c1 = coefs->c1[phase];
  1456. c2 = coefs->c2[phase];
  1457. c12 = FIELD_PREP(GENMASK(19, 10), c1) | FIELD_PREP(GENMASK(29, 20),
  1458. c2);
  1459. dispc_vid_write(dispc, hw_plane, reg, c12);
  1460. }
  1461. }
  1462. static bool dispc_fourcc_is_yuv(u32 fourcc)
  1463. {
  1464. switch (fourcc) {
  1465. case DRM_FORMAT_YUYV:
  1466. case DRM_FORMAT_UYVY:
  1467. case DRM_FORMAT_NV12:
  1468. return true;
  1469. default:
  1470. return false;
  1471. }
  1472. }
  1473. struct dispc_scaling_params {
  1474. int xinc, yinc;
  1475. u32 in_w, in_h, in_w_uv, in_h_uv;
  1476. u32 fir_xinc, fir_yinc, fir_xinc_uv, fir_yinc_uv;
  1477. bool scale_x, scale_y;
  1478. const struct tidss_scale_coefs *xcoef, *ycoef, *xcoef_uv, *ycoef_uv;
  1479. bool five_taps;
  1480. };
  1481. static int dispc_vid_calc_scaling(struct dispc_device *dispc,
  1482. const struct drm_plane_state *state,
  1483. struct dispc_scaling_params *sp,
  1484. bool lite_plane)
  1485. {
  1486. const struct dispc_features_scaling *f = &dispc->feat->scaling;
  1487. u32 fourcc = state->fb->format->format;
  1488. u32 in_width_max_5tap = f->in_width_max_5tap_rgb;
  1489. u32 in_width_max_3tap = f->in_width_max_3tap_rgb;
  1490. u32 downscale_limit;
  1491. u32 in_width_max;
  1492. memset(sp, 0, sizeof(*sp));
  1493. sp->xinc = 1;
  1494. sp->yinc = 1;
  1495. sp->in_w = state->src_w >> 16;
  1496. sp->in_w_uv = sp->in_w;
  1497. sp->in_h = state->src_h >> 16;
  1498. sp->in_h_uv = sp->in_h;
  1499. sp->scale_x = sp->in_w != state->crtc_w;
  1500. sp->scale_y = sp->in_h != state->crtc_h;
  1501. if (dispc_fourcc_is_yuv(fourcc)) {
  1502. in_width_max_5tap = f->in_width_max_5tap_yuv;
  1503. in_width_max_3tap = f->in_width_max_3tap_yuv;
  1504. sp->in_w_uv >>= 1;
  1505. sp->scale_x = true;
  1506. if (fourcc == DRM_FORMAT_NV12) {
  1507. sp->in_h_uv >>= 1;
  1508. sp->scale_y = true;
  1509. }
  1510. }
  1511. /* Skip the rest if no scaling is used */
  1512. if ((!sp->scale_x && !sp->scale_y) || lite_plane)
  1513. return 0;
  1514. if (sp->in_w > in_width_max_5tap) {
  1515. sp->five_taps = false;
  1516. in_width_max = in_width_max_3tap;
  1517. downscale_limit = f->downscale_limit_3tap;
  1518. } else {
  1519. sp->five_taps = true;
  1520. in_width_max = in_width_max_5tap;
  1521. downscale_limit = f->downscale_limit_5tap;
  1522. }
  1523. if (sp->scale_x) {
  1524. sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
  1525. if (sp->fir_xinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
  1526. dev_dbg(dispc->dev,
  1527. "%s: X-scaling factor %u/%u > %u\n",
  1528. __func__, state->crtc_w, state->src_w >> 16,
  1529. f->upscale_limit);
  1530. return -EINVAL;
  1531. }
  1532. if (sp->fir_xinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
  1533. sp->xinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_w,
  1534. state->crtc_w),
  1535. downscale_limit);
  1536. if (sp->xinc > f->xinc_max) {
  1537. dev_dbg(dispc->dev,
  1538. "%s: X-scaling factor %u/%u < 1/%u\n",
  1539. __func__, state->crtc_w,
  1540. state->src_w >> 16,
  1541. downscale_limit * f->xinc_max);
  1542. return -EINVAL;
  1543. }
  1544. sp->in_w = (state->src_w >> 16) / sp->xinc;
  1545. }
  1546. while (sp->in_w > in_width_max) {
  1547. sp->xinc++;
  1548. sp->in_w = (state->src_w >> 16) / sp->xinc;
  1549. }
  1550. if (sp->xinc > f->xinc_max) {
  1551. dev_dbg(dispc->dev,
  1552. "%s: Too wide input buffer %u > %u\n", __func__,
  1553. state->src_w >> 16, in_width_max * f->xinc_max);
  1554. return -EINVAL;
  1555. }
  1556. /*
  1557. * We need even line length for YUV formats. Decimation
  1558. * can lead to odd length, so we need to make it even
  1559. * again.
  1560. */
  1561. if (dispc_fourcc_is_yuv(fourcc))
  1562. sp->in_w &= ~1;
  1563. sp->fir_xinc = dispc_calc_fir_inc(sp->in_w, state->crtc_w);
  1564. }
  1565. if (sp->scale_y) {
  1566. sp->fir_yinc = dispc_calc_fir_inc(sp->in_h, state->crtc_h);
  1567. if (sp->fir_yinc < dispc_calc_fir_inc(1, f->upscale_limit)) {
  1568. dev_dbg(dispc->dev,
  1569. "%s: Y-scaling factor %u/%u > %u\n",
  1570. __func__, state->crtc_h, state->src_h >> 16,
  1571. f->upscale_limit);
  1572. return -EINVAL;
  1573. }
  1574. if (sp->fir_yinc >= dispc_calc_fir_inc(downscale_limit, 1)) {
  1575. sp->yinc = DIV_ROUND_UP(DIV_ROUND_UP(sp->in_h,
  1576. state->crtc_h),
  1577. downscale_limit);
  1578. sp->in_h /= sp->yinc;
  1579. sp->fir_yinc = dispc_calc_fir_inc(sp->in_h,
  1580. state->crtc_h);
  1581. }
  1582. }
  1583. dev_dbg(dispc->dev,
  1584. "%s: %ux%u decim %ux%u -> %ux%u firinc %u.%03ux%u.%03u taps %u -> %ux%u\n",
  1585. __func__, state->src_w >> 16, state->src_h >> 16,
  1586. sp->xinc, sp->yinc, sp->in_w, sp->in_h,
  1587. sp->fir_xinc / 0x200000u,
  1588. ((sp->fir_xinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
  1589. sp->fir_yinc / 0x200000u,
  1590. ((sp->fir_yinc & 0x1FFFFFu) * 999u) / 0x1FFFFFu,
  1591. sp->five_taps ? 5 : 3,
  1592. state->crtc_w, state->crtc_h);
  1593. if (dispc_fourcc_is_yuv(fourcc)) {
  1594. if (sp->scale_x) {
  1595. sp->in_w_uv /= sp->xinc;
  1596. sp->fir_xinc_uv = dispc_calc_fir_inc(sp->in_w_uv,
  1597. state->crtc_w);
  1598. sp->xcoef_uv = tidss_get_scale_coefs(dispc->dev,
  1599. sp->fir_xinc_uv,
  1600. true);
  1601. }
  1602. if (sp->scale_y) {
  1603. sp->in_h_uv /= sp->yinc;
  1604. sp->fir_yinc_uv = dispc_calc_fir_inc(sp->in_h_uv,
  1605. state->crtc_h);
  1606. sp->ycoef_uv = tidss_get_scale_coefs(dispc->dev,
  1607. sp->fir_yinc_uv,
  1608. sp->five_taps);
  1609. }
  1610. }
  1611. if (sp->scale_x)
  1612. sp->xcoef = tidss_get_scale_coefs(dispc->dev, sp->fir_xinc,
  1613. true);
  1614. if (sp->scale_y)
  1615. sp->ycoef = tidss_get_scale_coefs(dispc->dev, sp->fir_yinc,
  1616. sp->five_taps);
  1617. return 0;
  1618. }
  1619. static void dispc_vid_set_scaling(struct dispc_device *dispc,
  1620. u32 hw_plane,
  1621. struct dispc_scaling_params *sp,
  1622. u32 fourcc)
  1623. {
  1624. /* HORIZONTAL RESIZE ENABLE */
  1625. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_x,
  1626. DISPC_VID_ATTRIBUTES_HRESIZEENABLE_MASK);
  1627. /* VERTICAL RESIZE ENABLE */
  1628. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->scale_y,
  1629. DISPC_VID_ATTRIBUTES_VRESIZEENABLE_MASK);
  1630. /* Skip the rest if no scaling is used */
  1631. if (!sp->scale_x && !sp->scale_y)
  1632. return;
  1633. /* VERTICAL 5-TAPS */
  1634. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, sp->five_taps,
  1635. DISPC_VID_ATTRIBUTES_VERTICALTAPS_MASK);
  1636. if (dispc_fourcc_is_yuv(fourcc)) {
  1637. if (sp->scale_x) {
  1638. dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH2,
  1639. sp->fir_xinc_uv);
  1640. dispc_vid_write_fir_coefs(dispc, hw_plane,
  1641. DISPC_VID_FIR_COEF_HORIZ_UV,
  1642. sp->xcoef_uv);
  1643. }
  1644. if (sp->scale_y) {
  1645. dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV2,
  1646. sp->fir_yinc_uv);
  1647. dispc_vid_write_fir_coefs(dispc, hw_plane,
  1648. DISPC_VID_FIR_COEF_VERT_UV,
  1649. sp->ycoef_uv);
  1650. }
  1651. }
  1652. if (sp->scale_x) {
  1653. dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRH, sp->fir_xinc);
  1654. dispc_vid_write_fir_coefs(dispc, hw_plane,
  1655. DISPC_VID_FIR_COEF_HORIZ,
  1656. sp->xcoef);
  1657. }
  1658. if (sp->scale_y) {
  1659. dispc_vid_write(dispc, hw_plane, DISPC_VID_FIRV, sp->fir_yinc);
  1660. dispc_vid_write_fir_coefs(dispc, hw_plane,
  1661. DISPC_VID_FIR_COEF_VERT, sp->ycoef);
  1662. }
  1663. }
  1664. /* OTHER */
  1665. static const struct {
  1666. u32 fourcc;
  1667. u8 dss_code;
  1668. } dispc_color_formats[] = {
  1669. { DRM_FORMAT_ARGB4444, 0x0, },
  1670. { DRM_FORMAT_ABGR4444, 0x1, },
  1671. { DRM_FORMAT_RGBA4444, 0x2, },
  1672. { DRM_FORMAT_RGB565, 0x3, },
  1673. { DRM_FORMAT_BGR565, 0x4, },
  1674. { DRM_FORMAT_ARGB1555, 0x5, },
  1675. { DRM_FORMAT_ABGR1555, 0x6, },
  1676. { DRM_FORMAT_ARGB8888, 0x7, },
  1677. { DRM_FORMAT_ABGR8888, 0x8, },
  1678. { DRM_FORMAT_RGBA8888, 0x9, },
  1679. { DRM_FORMAT_BGRA8888, 0xa, },
  1680. { DRM_FORMAT_RGB888, 0xb, },
  1681. { DRM_FORMAT_BGR888, 0xc, },
  1682. { DRM_FORMAT_ARGB2101010, 0xe, },
  1683. { DRM_FORMAT_ABGR2101010, 0xf, },
  1684. { DRM_FORMAT_XRGB4444, 0x20, },
  1685. { DRM_FORMAT_XBGR4444, 0x21, },
  1686. { DRM_FORMAT_RGBX4444, 0x22, },
  1687. { DRM_FORMAT_XRGB1555, 0x25, },
  1688. { DRM_FORMAT_XBGR1555, 0x26, },
  1689. { DRM_FORMAT_XRGB8888, 0x27, },
  1690. { DRM_FORMAT_XBGR8888, 0x28, },
  1691. { DRM_FORMAT_RGBX8888, 0x29, },
  1692. { DRM_FORMAT_BGRX8888, 0x2a, },
  1693. { DRM_FORMAT_XRGB2101010, 0x2e, },
  1694. { DRM_FORMAT_XBGR2101010, 0x2f, },
  1695. { DRM_FORMAT_YUYV, 0x3e, },
  1696. { DRM_FORMAT_UYVY, 0x3f, },
  1697. { DRM_FORMAT_NV12, 0x3d, },
  1698. };
  1699. static void dispc_plane_set_pixel_format(struct dispc_device *dispc,
  1700. u32 hw_plane, u32 fourcc)
  1701. {
  1702. unsigned int i;
  1703. for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
  1704. if (dispc_color_formats[i].fourcc == fourcc) {
  1705. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES,
  1706. dispc_color_formats[i].dss_code,
  1707. DISPC_VID_ATTRIBUTES_FORMAT_MASK);
  1708. return;
  1709. }
  1710. }
  1711. WARN_ON(1);
  1712. }
  1713. const u32 *dispc_plane_formats(struct dispc_device *dispc, unsigned int *len)
  1714. {
  1715. WARN_ON(!dispc->fourccs);
  1716. *len = dispc->num_fourccs;
  1717. return dispc->fourccs;
  1718. }
  1719. static s32 pixinc(int pixels, u8 ps)
  1720. {
  1721. if (pixels == 1)
  1722. return 1;
  1723. else if (pixels > 1)
  1724. return 1 + (pixels - 1) * ps;
  1725. else if (pixels < 0)
  1726. return 1 - (-pixels + 1) * ps;
  1727. WARN_ON(1);
  1728. return 0;
  1729. }
  1730. int dispc_plane_check(struct dispc_device *dispc, u32 hw_plane,
  1731. const struct drm_plane_state *state,
  1732. u32 hw_videoport)
  1733. {
  1734. bool lite = dispc->feat->vid_info[hw_plane].is_lite;
  1735. u32 fourcc = state->fb->format->format;
  1736. bool need_scaling = state->src_w >> 16 != state->crtc_w ||
  1737. state->src_h >> 16 != state->crtc_h;
  1738. struct dispc_scaling_params scaling;
  1739. int ret;
  1740. if (dispc_fourcc_is_yuv(fourcc)) {
  1741. if (!dispc_find_csc(state->color_encoding,
  1742. state->color_range)) {
  1743. dev_dbg(dispc->dev,
  1744. "%s: Unsupported CSC (%u,%u) for HW plane %u\n",
  1745. __func__, state->color_encoding,
  1746. state->color_range, hw_plane);
  1747. return -EINVAL;
  1748. }
  1749. }
  1750. if (need_scaling) {
  1751. if (lite) {
  1752. dev_dbg(dispc->dev,
  1753. "%s: Lite plane %u can't scale %ux%u!=%ux%u\n",
  1754. __func__, hw_plane,
  1755. state->src_w >> 16, state->src_h >> 16,
  1756. state->crtc_w, state->crtc_h);
  1757. return -EINVAL;
  1758. }
  1759. ret = dispc_vid_calc_scaling(dispc, state, &scaling, false);
  1760. if (ret)
  1761. return ret;
  1762. }
  1763. return 0;
  1764. }
  1765. static
  1766. dma_addr_t dispc_plane_state_dma_addr(const struct drm_plane_state *state)
  1767. {
  1768. struct drm_framebuffer *fb = state->fb;
  1769. struct drm_gem_dma_object *gem;
  1770. u32 x = state->src_x >> 16;
  1771. u32 y = state->src_y >> 16;
  1772. gem = drm_fb_dma_get_gem_obj(state->fb, 0);
  1773. return gem->dma_addr + fb->offsets[0] + x * fb->format->cpp[0] +
  1774. y * fb->pitches[0];
  1775. }
  1776. static
  1777. dma_addr_t dispc_plane_state_p_uv_addr(const struct drm_plane_state *state)
  1778. {
  1779. struct drm_framebuffer *fb = state->fb;
  1780. struct drm_gem_dma_object *gem;
  1781. u32 x = state->src_x >> 16;
  1782. u32 y = state->src_y >> 16;
  1783. if (WARN_ON(state->fb->format->num_planes != 2))
  1784. return 0;
  1785. gem = drm_fb_dma_get_gem_obj(fb, 1);
  1786. return gem->dma_addr + fb->offsets[1] +
  1787. (x * fb->format->cpp[1] / fb->format->hsub) +
  1788. (y * fb->pitches[1] / fb->format->vsub);
  1789. }
  1790. void dispc_plane_setup(struct dispc_device *dispc, u32 hw_plane,
  1791. const struct drm_plane_state *state,
  1792. u32 hw_videoport)
  1793. {
  1794. bool lite = dispc->feat->vid_info[hw_plane].is_lite;
  1795. u32 fourcc = state->fb->format->format;
  1796. u16 cpp = state->fb->format->cpp[0];
  1797. u32 fb_width = state->fb->pitches[0] / cpp;
  1798. dma_addr_t dma_addr = dispc_plane_state_dma_addr(state);
  1799. struct dispc_scaling_params scale;
  1800. dispc_vid_calc_scaling(dispc, state, &scale, lite);
  1801. dispc_plane_set_pixel_format(dispc, hw_plane, fourcc);
  1802. dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_0, dma_addr & 0xffffffff);
  1803. dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_0, (u64)dma_addr >> 32);
  1804. dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_1, dma_addr & 0xffffffff);
  1805. dispc_vid_write(dispc, hw_plane, DISPC_VID_BA_EXT_1, (u64)dma_addr >> 32);
  1806. dispc_vid_write(dispc, hw_plane, DISPC_VID_PICTURE_SIZE,
  1807. FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEY_MASK, scale.in_h - 1) |
  1808. FIELD_PREP(DISPC_VID_PICTURE_SIZE_MEMSIZEX_MASK, scale.in_w - 1));
  1809. /* For YUV422 format we use the macropixel size for pixel inc */
  1810. if (fourcc == DRM_FORMAT_YUYV || fourcc == DRM_FORMAT_UYVY)
  1811. dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
  1812. pixinc(scale.xinc, cpp * 2));
  1813. else
  1814. dispc_vid_write(dispc, hw_plane, DISPC_VID_PIXEL_INC,
  1815. pixinc(scale.xinc, cpp));
  1816. dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC,
  1817. pixinc(1 + (scale.yinc * fb_width -
  1818. scale.xinc * scale.in_w),
  1819. cpp));
  1820. if (state->fb->format->num_planes == 2) {
  1821. u16 cpp_uv = state->fb->format->cpp[1];
  1822. u32 fb_width_uv = state->fb->pitches[1] / cpp_uv;
  1823. dma_addr_t p_uv_addr = dispc_plane_state_p_uv_addr(state);
  1824. dispc_vid_write(dispc, hw_plane,
  1825. DISPC_VID_BA_UV_0, p_uv_addr & 0xffffffff);
  1826. dispc_vid_write(dispc, hw_plane,
  1827. DISPC_VID_BA_UV_EXT_0, (u64)p_uv_addr >> 32);
  1828. dispc_vid_write(dispc, hw_plane,
  1829. DISPC_VID_BA_UV_1, p_uv_addr & 0xffffffff);
  1830. dispc_vid_write(dispc, hw_plane,
  1831. DISPC_VID_BA_UV_EXT_1, (u64)p_uv_addr >> 32);
  1832. dispc_vid_write(dispc, hw_plane, DISPC_VID_ROW_INC_UV,
  1833. pixinc(1 + (scale.yinc * fb_width_uv -
  1834. scale.xinc * scale.in_w_uv),
  1835. cpp_uv));
  1836. }
  1837. if (!lite) {
  1838. dispc_vid_write(dispc, hw_plane, DISPC_VID_SIZE,
  1839. FIELD_PREP(DISPC_VID_SIZE_SIZEY_MASK,
  1840. state->crtc_h - 1) |
  1841. FIELD_PREP(DISPC_VID_SIZE_SIZEX_MASK,
  1842. state->crtc_w - 1));
  1843. dispc_vid_set_scaling(dispc, hw_plane, &scale, fourcc);
  1844. }
  1845. /* enable YUV->RGB color conversion */
  1846. if (dispc_fourcc_is_yuv(fourcc)) {
  1847. dispc_vid_csc_setup(dispc, hw_plane, state);
  1848. dispc_vid_csc_enable(dispc, hw_plane, true);
  1849. } else {
  1850. dispc_vid_csc_enable(dispc, hw_plane, false);
  1851. }
  1852. dispc_vid_write(dispc, hw_plane, DISPC_VID_GLOBAL_ALPHA,
  1853. FIELD_PREP(DISPC_VID_GLOBAL_ALPHA_GLOBALALPHA_MASK,
  1854. state->alpha >> 8));
  1855. if (state->pixel_blend_mode == DRM_MODE_BLEND_PREMULTI)
  1856. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
  1857. DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
  1858. else
  1859. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
  1860. DISPC_VID_ATTRIBUTES_PREMULTIPLYALPHA_MASK);
  1861. }
  1862. void dispc_plane_enable(struct dispc_device *dispc, u32 hw_plane, bool enable)
  1863. {
  1864. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, !!enable,
  1865. DISPC_VID_ATTRIBUTES_ENABLE_MASK);
  1866. }
  1867. static u32 dispc_vid_get_fifo_size(struct dispc_device *dispc, u32 hw_plane)
  1868. {
  1869. return VID_REG_GET(dispc, hw_plane, DISPC_VID_BUF_SIZE_STATUS,
  1870. DISPC_VID_BUF_SIZE_STATUS_BUFSIZE_MASK);
  1871. }
  1872. static void dispc_vid_set_mflag_threshold(struct dispc_device *dispc,
  1873. u32 hw_plane, u32 low, u32 high)
  1874. {
  1875. dispc_vid_write(dispc, hw_plane, DISPC_VID_MFLAG_THRESHOLD,
  1876. FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_HT_MFLAG_MASK, high) |
  1877. FIELD_PREP(DISPC_VID_MFLAG_THRESHOLD_LT_MFLAG_MASK, low));
  1878. }
  1879. static void dispc_vid_set_buf_threshold(struct dispc_device *dispc,
  1880. u32 hw_plane, u32 low, u32 high)
  1881. {
  1882. dispc_vid_write(dispc, hw_plane, DISPC_VID_BUF_THRESHOLD,
  1883. FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFHIGHTHRESHOLD_MASK,
  1884. high) |
  1885. FIELD_PREP(DISPC_VID_BUF_THRESHOLD_BUFLOWTHRESHOLD_MASK,
  1886. low));
  1887. }
  1888. static void dispc_k2g_plane_init(struct dispc_device *dispc)
  1889. {
  1890. unsigned int hw_plane;
  1891. dev_dbg(dispc->dev, "%s()\n", __func__);
  1892. /* MFLAG_CTRL = ENABLED */
  1893. REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
  1894. DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
  1895. /* MFLAG_START = MFLAGNORMALSTARTMODE */
  1896. REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
  1897. DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
  1898. for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
  1899. u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
  1900. u32 thr_low, thr_high;
  1901. u32 mflag_low, mflag_high;
  1902. u32 preload;
  1903. thr_high = size - 1;
  1904. thr_low = size / 2;
  1905. mflag_high = size * 2 / 3;
  1906. mflag_low = size / 3;
  1907. preload = thr_low;
  1908. dev_dbg(dispc->dev,
  1909. "%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
  1910. dispc->feat->vid_info[hw_plane].name,
  1911. size,
  1912. thr_high, thr_low,
  1913. mflag_high, mflag_low,
  1914. preload);
  1915. dispc_vid_set_buf_threshold(dispc, hw_plane,
  1916. thr_low, thr_high);
  1917. dispc_vid_set_mflag_threshold(dispc, hw_plane,
  1918. mflag_low, mflag_high);
  1919. dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
  1920. /*
  1921. * Prefetch up to fifo high-threshold value to minimize the
  1922. * possibility of underflows. Note that this means the PRELOAD
  1923. * register is ignored.
  1924. */
  1925. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 1,
  1926. DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
  1927. }
  1928. }
  1929. static void dispc_k3_plane_init(struct dispc_device *dispc)
  1930. {
  1931. unsigned int hw_plane;
  1932. u32 cba_lo_pri = 1;
  1933. u32 cba_hi_pri = 0;
  1934. dev_dbg(dispc->dev, "%s()\n", __func__);
  1935. REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_lo_pri, DSS_CBA_CFG_PRI_LO_MASK);
  1936. REG_FLD_MOD(dispc, DSS_CBA_CFG, cba_hi_pri, DSS_CBA_CFG_PRI_HI_MASK);
  1937. /* MFLAG_CTRL = ENABLED */
  1938. REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 2,
  1939. DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_CTRL_MASK);
  1940. /* MFLAG_START = MFLAGNORMALSTARTMODE */
  1941. REG_FLD_MOD(dispc, DISPC_GLOBAL_MFLAG_ATTRIBUTE, 0,
  1942. DISPC_GLOBAL_MFLAG_ATTRIBUTE_MFLAG_START_MASK);
  1943. for (hw_plane = 0; hw_plane < dispc->feat->num_vids; hw_plane++) {
  1944. u32 size = dispc_vid_get_fifo_size(dispc, hw_plane);
  1945. u32 thr_low, thr_high;
  1946. u32 mflag_low, mflag_high;
  1947. u32 preload;
  1948. thr_high = size - 1;
  1949. thr_low = size / 2;
  1950. mflag_high = size * 2 / 3;
  1951. mflag_low = size / 3;
  1952. preload = thr_low;
  1953. dev_dbg(dispc->dev,
  1954. "%s: bufsize %u, buf_threshold %u/%u, mflag threshold %u/%u preload %u\n",
  1955. dispc->feat->vid_info[hw_plane].name,
  1956. size,
  1957. thr_high, thr_low,
  1958. mflag_high, mflag_low,
  1959. preload);
  1960. dispc_vid_set_buf_threshold(dispc, hw_plane,
  1961. thr_low, thr_high);
  1962. dispc_vid_set_mflag_threshold(dispc, hw_plane,
  1963. mflag_low, mflag_high);
  1964. dispc_vid_write(dispc, hw_plane, DISPC_VID_PRELOAD, preload);
  1965. /* Prefech up to PRELOAD value */
  1966. VID_REG_FLD_MOD(dispc, hw_plane, DISPC_VID_ATTRIBUTES, 0,
  1967. DISPC_VID_ATTRIBUTES_BUFPRELOAD_MASK);
  1968. }
  1969. }
  1970. static void dispc_plane_init(struct dispc_device *dispc)
  1971. {
  1972. switch (dispc->feat->subrev) {
  1973. case DISPC_K2G:
  1974. dispc_k2g_plane_init(dispc);
  1975. break;
  1976. case DISPC_AM625:
  1977. case DISPC_AM62A7:
  1978. case DISPC_AM62L:
  1979. case DISPC_AM65X:
  1980. case DISPC_J721E:
  1981. dispc_k3_plane_init(dispc);
  1982. break;
  1983. default:
  1984. WARN_ON(1);
  1985. }
  1986. }
  1987. static void dispc_vp_init(struct dispc_device *dispc)
  1988. {
  1989. unsigned int i;
  1990. dev_dbg(dispc->dev, "%s()\n", __func__);
  1991. /* Enable the gamma Shadow bit-field for all VPs*/
  1992. for (i = 0; i < dispc->feat->num_vps; i++)
  1993. VP_REG_FLD_MOD(dispc, i, DISPC_VP_CONFIG, 1,
  1994. DISPC_VP_CONFIG_GAMMAENABLE_MASK);
  1995. }
  1996. static void dispc_initial_config(struct dispc_device *dispc)
  1997. {
  1998. dispc_plane_init(dispc);
  1999. dispc_vp_init(dispc);
  2000. /* Note: Hardcoded DPI routing on J721E for now */
  2001. if (dispc->feat->subrev == DISPC_J721E) {
  2002. dispc_write(dispc, DISPC_CONNECTIONS,
  2003. FIELD_PREP(DISPC_CONNECTIONS_DPI_0_CONN_MASK, 2) | /* VP1 to DPI0 */
  2004. FIELD_PREP(DISPC_CONNECTIONS_DPI_1_CONN_MASK, 8) /* VP3 to DPI1 */
  2005. );
  2006. }
  2007. }
  2008. static void dispc_k2g_vp_write_gamma_table(struct dispc_device *dispc,
  2009. u32 hw_videoport)
  2010. {
  2011. u32 *table = dispc->vp_data[hw_videoport].gamma_table;
  2012. u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
  2013. unsigned int i;
  2014. dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
  2015. if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
  2016. return;
  2017. for (i = 0; i < hwlen; ++i) {
  2018. u32 v = table[i];
  2019. v |= i << 24;
  2020. dispc_vp_write(dispc, hw_videoport, DISPC_VP_K2G_GAMMA_TABLE,
  2021. v);
  2022. }
  2023. }
  2024. static void dispc_am65x_vp_write_gamma_table(struct dispc_device *dispc,
  2025. u32 hw_videoport)
  2026. {
  2027. u32 *table = dispc->vp_data[hw_videoport].gamma_table;
  2028. u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
  2029. unsigned int i;
  2030. dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
  2031. if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_8BIT))
  2032. return;
  2033. for (i = 0; i < hwlen; ++i) {
  2034. u32 v = table[i];
  2035. v |= i << 24;
  2036. dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
  2037. }
  2038. }
  2039. static void dispc_j721e_vp_write_gamma_table(struct dispc_device *dispc,
  2040. u32 hw_videoport)
  2041. {
  2042. u32 *table = dispc->vp_data[hw_videoport].gamma_table;
  2043. u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
  2044. unsigned int i;
  2045. dev_dbg(dispc->dev, "%s: hw_videoport %d\n", __func__, hw_videoport);
  2046. if (WARN_ON(dispc->feat->vp_feat.color.gamma_type != TIDSS_GAMMA_10BIT))
  2047. return;
  2048. for (i = 0; i < hwlen; ++i) {
  2049. u32 v = table[i];
  2050. if (i == 0)
  2051. v |= 1 << 31;
  2052. dispc_vp_write(dispc, hw_videoport, DISPC_VP_GAMMA_TABLE, v);
  2053. }
  2054. }
  2055. static void dispc_vp_write_gamma_table(struct dispc_device *dispc,
  2056. u32 hw_videoport)
  2057. {
  2058. switch (dispc->feat->subrev) {
  2059. case DISPC_K2G:
  2060. dispc_k2g_vp_write_gamma_table(dispc, hw_videoport);
  2061. break;
  2062. case DISPC_AM625:
  2063. case DISPC_AM62A7:
  2064. case DISPC_AM62L:
  2065. case DISPC_AM65X:
  2066. dispc_am65x_vp_write_gamma_table(dispc, hw_videoport);
  2067. break;
  2068. case DISPC_J721E:
  2069. dispc_j721e_vp_write_gamma_table(dispc, hw_videoport);
  2070. break;
  2071. default:
  2072. WARN_ON(1);
  2073. break;
  2074. }
  2075. }
  2076. static const struct drm_color_lut dispc_vp_gamma_default_lut[] = {
  2077. { .red = 0, .green = 0, .blue = 0, },
  2078. { .red = U16_MAX, .green = U16_MAX, .blue = U16_MAX, },
  2079. };
  2080. static void dispc_vp_set_gamma(struct dispc_device *dispc,
  2081. u32 hw_videoport,
  2082. const struct drm_color_lut *lut,
  2083. unsigned int length)
  2084. {
  2085. u32 *table = dispc->vp_data[hw_videoport].gamma_table;
  2086. u32 hwlen = dispc->feat->vp_feat.color.gamma_size;
  2087. u32 hwbits;
  2088. unsigned int i;
  2089. dev_dbg(dispc->dev, "%s: hw_videoport %d, lut len %u, hw len %u\n",
  2090. __func__, hw_videoport, length, hwlen);
  2091. if (dispc->feat->vp_feat.color.gamma_type == TIDSS_GAMMA_10BIT)
  2092. hwbits = 10;
  2093. else
  2094. hwbits = 8;
  2095. if (!lut || length < 2) {
  2096. lut = dispc_vp_gamma_default_lut;
  2097. length = ARRAY_SIZE(dispc_vp_gamma_default_lut);
  2098. }
  2099. for (i = 0; i < length - 1; ++i) {
  2100. unsigned int first = i * (hwlen - 1) / (length - 1);
  2101. unsigned int last = (i + 1) * (hwlen - 1) / (length - 1);
  2102. unsigned int w = last - first;
  2103. u16 r, g, b;
  2104. unsigned int j;
  2105. if (w == 0)
  2106. continue;
  2107. for (j = 0; j <= w; j++) {
  2108. r = (lut[i].red * (w - j) + lut[i + 1].red * j) / w;
  2109. g = (lut[i].green * (w - j) + lut[i + 1].green * j) / w;
  2110. b = (lut[i].blue * (w - j) + lut[i + 1].blue * j) / w;
  2111. r >>= 16 - hwbits;
  2112. g >>= 16 - hwbits;
  2113. b >>= 16 - hwbits;
  2114. table[first + j] = (r << (hwbits * 2)) |
  2115. (g << hwbits) | b;
  2116. }
  2117. }
  2118. dispc_vp_write_gamma_table(dispc, hw_videoport);
  2119. }
  2120. static s16 dispc_S31_32_to_s2_8(s64 coef)
  2121. {
  2122. u64 sign_bit = 1ULL << 63;
  2123. u64 cbits = (u64)coef;
  2124. s16 ret;
  2125. if (cbits & sign_bit)
  2126. ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x200);
  2127. else
  2128. ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x1FF);
  2129. return ret;
  2130. }
  2131. static void dispc_k2g_cpr_from_ctm(const struct drm_color_ctm *ctm,
  2132. struct dispc_csc_coef *cpr)
  2133. {
  2134. memset(cpr, 0, sizeof(*cpr));
  2135. cpr->to_regval = dispc_csc_cpr_regval;
  2136. cpr->m[CSC_RR] = dispc_S31_32_to_s2_8(ctm->matrix[0]);
  2137. cpr->m[CSC_RG] = dispc_S31_32_to_s2_8(ctm->matrix[1]);
  2138. cpr->m[CSC_RB] = dispc_S31_32_to_s2_8(ctm->matrix[2]);
  2139. cpr->m[CSC_GR] = dispc_S31_32_to_s2_8(ctm->matrix[3]);
  2140. cpr->m[CSC_GG] = dispc_S31_32_to_s2_8(ctm->matrix[4]);
  2141. cpr->m[CSC_GB] = dispc_S31_32_to_s2_8(ctm->matrix[5]);
  2142. cpr->m[CSC_BR] = dispc_S31_32_to_s2_8(ctm->matrix[6]);
  2143. cpr->m[CSC_BG] = dispc_S31_32_to_s2_8(ctm->matrix[7]);
  2144. cpr->m[CSC_BB] = dispc_S31_32_to_s2_8(ctm->matrix[8]);
  2145. }
  2146. #define CVAL(xR, xG, xB) (FIELD_PREP(GENMASK(9, 0), xR) | FIELD_PREP(GENMASK(20, 11), xG) | \
  2147. FIELD_PREP(GENMASK(31, 22), xB))
  2148. static void dispc_k2g_vp_csc_cpr_regval(const struct dispc_csc_coef *csc,
  2149. u32 *regval)
  2150. {
  2151. regval[0] = CVAL(csc->m[CSC_BB], csc->m[CSC_BG], csc->m[CSC_BR]);
  2152. regval[1] = CVAL(csc->m[CSC_GB], csc->m[CSC_GG], csc->m[CSC_GR]);
  2153. regval[2] = CVAL(csc->m[CSC_RB], csc->m[CSC_RG], csc->m[CSC_RR]);
  2154. }
  2155. #undef CVAL
  2156. static void dispc_k2g_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
  2157. const struct dispc_csc_coef *csc)
  2158. {
  2159. static const u16 dispc_vp_cpr_coef_reg[] = {
  2160. DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
  2161. /* K2G CPR is packed to three registers. */
  2162. };
  2163. u32 regval[DISPC_CSC_REGVAL_LEN];
  2164. unsigned int i;
  2165. dispc_k2g_vp_csc_cpr_regval(csc, regval);
  2166. for (i = 0; i < ARRAY_SIZE(dispc_vp_cpr_coef_reg); i++)
  2167. dispc_vp_write(dispc, hw_videoport, dispc_vp_cpr_coef_reg[i],
  2168. regval[i]);
  2169. }
  2170. static void dispc_k2g_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
  2171. struct drm_color_ctm *ctm)
  2172. {
  2173. u32 cprenable = 0;
  2174. if (ctm) {
  2175. struct dispc_csc_coef cpr;
  2176. dispc_k2g_cpr_from_ctm(ctm, &cpr);
  2177. dispc_k2g_vp_write_csc(dispc, hw_videoport, &cpr);
  2178. cprenable = 1;
  2179. }
  2180. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, cprenable,
  2181. DISPC_VP_CONFIG_CPR_MASK);
  2182. }
  2183. static s16 dispc_S31_32_to_s3_8(s64 coef)
  2184. {
  2185. u64 sign_bit = 1ULL << 63;
  2186. u64 cbits = (u64)coef;
  2187. s16 ret;
  2188. if (cbits & sign_bit)
  2189. ret = -clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x400);
  2190. else
  2191. ret = clamp_val(((cbits & ~sign_bit) >> 24), 0, 0x3FF);
  2192. return ret;
  2193. }
  2194. static void dispc_csc_from_ctm(const struct drm_color_ctm *ctm,
  2195. struct dispc_csc_coef *cpr)
  2196. {
  2197. memset(cpr, 0, sizeof(*cpr));
  2198. cpr->to_regval = dispc_csc_cpr_regval;
  2199. cpr->m[CSC_RR] = dispc_S31_32_to_s3_8(ctm->matrix[0]);
  2200. cpr->m[CSC_RG] = dispc_S31_32_to_s3_8(ctm->matrix[1]);
  2201. cpr->m[CSC_RB] = dispc_S31_32_to_s3_8(ctm->matrix[2]);
  2202. cpr->m[CSC_GR] = dispc_S31_32_to_s3_8(ctm->matrix[3]);
  2203. cpr->m[CSC_GG] = dispc_S31_32_to_s3_8(ctm->matrix[4]);
  2204. cpr->m[CSC_GB] = dispc_S31_32_to_s3_8(ctm->matrix[5]);
  2205. cpr->m[CSC_BR] = dispc_S31_32_to_s3_8(ctm->matrix[6]);
  2206. cpr->m[CSC_BG] = dispc_S31_32_to_s3_8(ctm->matrix[7]);
  2207. cpr->m[CSC_BB] = dispc_S31_32_to_s3_8(ctm->matrix[8]);
  2208. }
  2209. static void dispc_k3_vp_write_csc(struct dispc_device *dispc, u32 hw_videoport,
  2210. const struct dispc_csc_coef *csc)
  2211. {
  2212. static const u16 dispc_vp_csc_coef_reg[DISPC_CSC_REGVAL_LEN] = {
  2213. DISPC_VP_CSC_COEF0, DISPC_VP_CSC_COEF1, DISPC_VP_CSC_COEF2,
  2214. DISPC_VP_CSC_COEF3, DISPC_VP_CSC_COEF4, DISPC_VP_CSC_COEF5,
  2215. DISPC_VP_CSC_COEF6, DISPC_VP_CSC_COEF7,
  2216. };
  2217. u32 regval[DISPC_CSC_REGVAL_LEN];
  2218. unsigned int i;
  2219. csc->to_regval(csc, regval);
  2220. for (i = 0; i < ARRAY_SIZE(regval); i++)
  2221. dispc_vp_write(dispc, hw_videoport, dispc_vp_csc_coef_reg[i],
  2222. regval[i]);
  2223. }
  2224. static void dispc_k3_vp_set_ctm(struct dispc_device *dispc, u32 hw_videoport,
  2225. struct drm_color_ctm *ctm)
  2226. {
  2227. u32 colorconvenable = 0;
  2228. if (ctm) {
  2229. struct dispc_csc_coef csc;
  2230. dispc_csc_from_ctm(ctm, &csc);
  2231. dispc_k3_vp_write_csc(dispc, hw_videoport, &csc);
  2232. colorconvenable = 1;
  2233. }
  2234. VP_REG_FLD_MOD(dispc, hw_videoport, DISPC_VP_CONFIG, colorconvenable,
  2235. DISPC_VP_CONFIG_COLORCONVENABLE_MASK);
  2236. }
  2237. static void dispc_vp_set_color_mgmt(struct dispc_device *dispc,
  2238. u32 hw_videoport,
  2239. const struct drm_crtc_state *state,
  2240. bool newmodeset)
  2241. {
  2242. struct drm_color_lut *lut = NULL;
  2243. struct drm_color_ctm *ctm = NULL;
  2244. unsigned int length = 0;
  2245. if (!(state->color_mgmt_changed || newmodeset))
  2246. return;
  2247. if (state->gamma_lut) {
  2248. lut = (struct drm_color_lut *)state->gamma_lut->data;
  2249. length = state->gamma_lut->length / sizeof(*lut);
  2250. }
  2251. dispc_vp_set_gamma(dispc, hw_videoport, lut, length);
  2252. if (state->ctm)
  2253. ctm = (struct drm_color_ctm *)state->ctm->data;
  2254. if (dispc->feat->subrev == DISPC_K2G)
  2255. dispc_k2g_vp_set_ctm(dispc, hw_videoport, ctm);
  2256. else
  2257. dispc_k3_vp_set_ctm(dispc, hw_videoport, ctm);
  2258. }
  2259. void dispc_vp_setup(struct dispc_device *dispc, u32 hw_videoport,
  2260. const struct drm_crtc_state *state, bool newmodeset)
  2261. {
  2262. dispc_vp_set_default_color(dispc, hw_videoport, 0);
  2263. dispc_vp_set_color_mgmt(dispc, hw_videoport, state, newmodeset);
  2264. }
  2265. int dispc_runtime_suspend(struct dispc_device *dispc)
  2266. {
  2267. dev_dbg(dispc->dev, "suspend\n");
  2268. dispc->is_enabled = false;
  2269. clk_disable_unprepare(dispc->fclk);
  2270. return 0;
  2271. }
  2272. int dispc_runtime_resume(struct dispc_device *dispc)
  2273. {
  2274. dev_dbg(dispc->dev, "resume\n");
  2275. clk_prepare_enable(dispc->fclk);
  2276. if (REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_FUNC_RESETDONE) == 0)
  2277. dev_warn(dispc->dev, "DSS FUNC RESET not done!\n");
  2278. dev_dbg(dispc->dev, "OMAP DSS7 rev 0x%x\n",
  2279. dispc_read(dispc, DSS_REVISION));
  2280. dev_dbg(dispc->dev, "VP RESETDONE %d,%d,%d\n",
  2281. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(1, 1)),
  2282. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(2, 2)),
  2283. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(3, 3)));
  2284. if (dispc->feat->subrev == DISPC_AM625 ||
  2285. dispc->feat->subrev == DISPC_AM65X)
  2286. dev_dbg(dispc->dev, "OLDI RESETDONE %d,%d,%d\n",
  2287. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(5, 5)),
  2288. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(6, 6)),
  2289. REG_GET(dispc, DSS_SYSSTATUS, GENMASK(7, 7)));
  2290. dev_dbg(dispc->dev, "DISPC IDLE %d\n",
  2291. REG_GET(dispc, DSS_SYSSTATUS, DSS_SYSSTATUS_DISPC_IDLE_STATUS));
  2292. dispc_initial_config(dispc);
  2293. dispc->is_enabled = true;
  2294. tidss_irq_resume(dispc->tidss);
  2295. return 0;
  2296. }
  2297. void dispc_remove(struct tidss_device *tidss)
  2298. {
  2299. tidss->dispc = NULL;
  2300. }
  2301. static int dispc_iomap_resource(struct platform_device *pdev, const char *name,
  2302. void __iomem **base)
  2303. {
  2304. void __iomem *b;
  2305. b = devm_platform_ioremap_resource_byname(pdev, name);
  2306. if (IS_ERR(b)) {
  2307. dev_err(&pdev->dev, "cannot ioremap resource '%s'\n", name);
  2308. return PTR_ERR(b);
  2309. }
  2310. *base = b;
  2311. return 0;
  2312. }
  2313. static int dispc_init_am65x_oldi_io_ctrl(struct device *dev,
  2314. struct dispc_device *dispc)
  2315. {
  2316. dispc->am65x_oldi_io_ctrl =
  2317. syscon_regmap_lookup_by_phandle(dev->of_node,
  2318. "ti,am65x-oldi-io-ctrl");
  2319. if (PTR_ERR(dispc->am65x_oldi_io_ctrl) == -ENODEV) {
  2320. dispc->am65x_oldi_io_ctrl = NULL;
  2321. } else if (IS_ERR(dispc->am65x_oldi_io_ctrl)) {
  2322. dev_err(dev, "%s: syscon_regmap_lookup_by_phandle failed %ld\n",
  2323. __func__, PTR_ERR(dispc->am65x_oldi_io_ctrl));
  2324. return PTR_ERR(dispc->am65x_oldi_io_ctrl);
  2325. }
  2326. return 0;
  2327. }
  2328. static void dispc_init_errata(struct dispc_device *dispc)
  2329. {
  2330. static const struct soc_device_attribute am65x_sr10_soc_devices[] = {
  2331. { .family = "AM65X", .revision = "SR1.0" },
  2332. { /* sentinel */ }
  2333. };
  2334. if (soc_device_match(am65x_sr10_soc_devices)) {
  2335. dispc->errata.i2000 = true;
  2336. dev_info(dispc->dev, "WA for erratum i2000: YUV formats disabled\n");
  2337. }
  2338. }
  2339. /*
  2340. * K2G display controller does not support soft reset, so we do a basic manual
  2341. * reset here: make sure the IRQs are masked and VPs are disabled.
  2342. */
  2343. static void dispc_softreset_k2g(struct dispc_device *dispc)
  2344. {
  2345. unsigned long flags;
  2346. spin_lock_irqsave(&dispc->tidss->irq_lock, flags);
  2347. dispc_set_irqenable(dispc, 0);
  2348. dispc_read_and_clear_irqstatus(dispc);
  2349. spin_unlock_irqrestore(&dispc->tidss->irq_lock, flags);
  2350. for (unsigned int vp_idx = 0; vp_idx < dispc->feat->num_vps; ++vp_idx)
  2351. VP_REG_FLD_MOD(dispc, vp_idx, DISPC_VP_CONTROL, 0,
  2352. DISPC_VP_CONTROL_ENABLE_MASK);
  2353. }
  2354. static int dispc_softreset(struct dispc_device *dispc)
  2355. {
  2356. u32 val;
  2357. int ret;
  2358. if (dispc->feat->subrev == DISPC_K2G) {
  2359. dispc_softreset_k2g(dispc);
  2360. return 0;
  2361. }
  2362. /* Soft reset */
  2363. REG_FLD_MOD(dispc, DSS_SYSCONFIG, 1, DSS_SYSCONFIG_SOFTRESET_MASK);
  2364. /* Wait for reset to complete */
  2365. ret = readl_poll_timeout(dispc->base_common + DSS_SYSSTATUS,
  2366. val, val & 1, 100, 5000);
  2367. if (ret) {
  2368. dev_err(dispc->dev, "failed to reset dispc\n");
  2369. return ret;
  2370. }
  2371. return 0;
  2372. }
  2373. static int dispc_init_hw(struct dispc_device *dispc)
  2374. {
  2375. struct device *dev = dispc->dev;
  2376. int ret;
  2377. ret = pm_runtime_set_active(dev);
  2378. if (ret) {
  2379. dev_err(dev, "Failed to set DSS PM to active\n");
  2380. return ret;
  2381. }
  2382. ret = clk_prepare_enable(dispc->fclk);
  2383. if (ret) {
  2384. dev_err(dev, "Failed to enable DSS fclk\n");
  2385. goto err_runtime_suspend;
  2386. }
  2387. ret = dispc_softreset(dispc);
  2388. if (ret)
  2389. goto err_clk_disable;
  2390. clk_disable_unprepare(dispc->fclk);
  2391. ret = pm_runtime_set_suspended(dev);
  2392. if (ret) {
  2393. dev_err(dev, "Failed to set DSS PM to suspended\n");
  2394. return ret;
  2395. }
  2396. return 0;
  2397. err_clk_disable:
  2398. clk_disable_unprepare(dispc->fclk);
  2399. err_runtime_suspend:
  2400. ret = pm_runtime_set_suspended(dev);
  2401. if (ret) {
  2402. dev_err(dev, "Failed to set DSS PM to suspended\n");
  2403. return ret;
  2404. }
  2405. return ret;
  2406. }
  2407. int dispc_init(struct tidss_device *tidss)
  2408. {
  2409. struct device *dev = tidss->dev;
  2410. struct platform_device *pdev = to_platform_device(dev);
  2411. struct dispc_device *dispc;
  2412. const struct dispc_features *feat;
  2413. unsigned int i, num_fourccs;
  2414. int r = 0;
  2415. feat = tidss->feat;
  2416. if (feat->subrev != DISPC_K2G) {
  2417. r = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
  2418. if (r)
  2419. dev_warn(dev, "cannot set DMA masks to 48-bit\n");
  2420. }
  2421. dma_set_max_seg_size(dev, UINT_MAX);
  2422. dispc = devm_kzalloc(dev, sizeof(*dispc), GFP_KERNEL);
  2423. if (!dispc)
  2424. return -ENOMEM;
  2425. dispc->tidss = tidss;
  2426. dispc->dev = dev;
  2427. dispc->feat = feat;
  2428. dispc_init_errata(dispc);
  2429. dispc->fourccs = devm_kcalloc(dev, ARRAY_SIZE(dispc_color_formats),
  2430. sizeof(*dispc->fourccs), GFP_KERNEL);
  2431. if (!dispc->fourccs)
  2432. return -ENOMEM;
  2433. num_fourccs = 0;
  2434. for (i = 0; i < ARRAY_SIZE(dispc_color_formats); ++i) {
  2435. if (dispc->errata.i2000 &&
  2436. dispc_fourcc_is_yuv(dispc_color_formats[i].fourcc)) {
  2437. continue;
  2438. }
  2439. dispc->fourccs[num_fourccs++] = dispc_color_formats[i].fourcc;
  2440. }
  2441. dispc->num_fourccs = num_fourccs;
  2442. dispc_common_regmap = dispc->feat->common_regs;
  2443. r = dispc_iomap_resource(pdev, dispc->feat->common,
  2444. &dispc->base_common);
  2445. if (r)
  2446. return r;
  2447. for (i = 0; i < dispc->feat->num_vids; i++) {
  2448. r = dispc_iomap_resource(pdev, dispc->feat->vid_info[i].name,
  2449. &dispc->base_vid[i]);
  2450. if (r)
  2451. return r;
  2452. }
  2453. for (i = 0; i < dispc->feat->num_vps; i++) {
  2454. u32 gamma_size = dispc->feat->vp_feat.color.gamma_size;
  2455. u32 *gamma_table;
  2456. struct clk *clk;
  2457. r = dispc_iomap_resource(pdev, dispc->feat->ovr_name[i],
  2458. &dispc->base_ovr[i]);
  2459. if (r)
  2460. return r;
  2461. r = dispc_iomap_resource(pdev, dispc->feat->vp_name[i],
  2462. &dispc->base_vp[i]);
  2463. if (r)
  2464. return r;
  2465. clk = devm_clk_get(dev, dispc->feat->vpclk_name[i]);
  2466. if (IS_ERR(clk)) {
  2467. dev_err(dev, "%s: Failed to get clk %s:%ld\n", __func__,
  2468. dispc->feat->vpclk_name[i], PTR_ERR(clk));
  2469. return PTR_ERR(clk);
  2470. }
  2471. dispc->vp_clk[i] = clk;
  2472. gamma_table = devm_kmalloc_array(dev, gamma_size,
  2473. sizeof(*gamma_table),
  2474. GFP_KERNEL);
  2475. if (!gamma_table)
  2476. return -ENOMEM;
  2477. dispc->vp_data[i].gamma_table = gamma_table;
  2478. }
  2479. if (feat->subrev == DISPC_AM65X) {
  2480. r = dispc_init_am65x_oldi_io_ctrl(dev, dispc);
  2481. if (r)
  2482. return r;
  2483. }
  2484. dispc->fclk = devm_clk_get(dev, "fck");
  2485. if (IS_ERR(dispc->fclk)) {
  2486. dev_err(dev, "%s: Failed to get fclk: %ld\n",
  2487. __func__, PTR_ERR(dispc->fclk));
  2488. return PTR_ERR(dispc->fclk);
  2489. }
  2490. dev_dbg(dev, "DSS fclk %lu Hz\n", clk_get_rate(dispc->fclk));
  2491. of_property_read_u32(dispc->dev->of_node, "max-memory-bandwidth",
  2492. &dispc->memory_bandwidth_limit);
  2493. r = dispc_init_hw(dispc);
  2494. if (r)
  2495. return r;
  2496. tidss->dispc = dispc;
  2497. return 0;
  2498. }