tidss_dispc.c 75 KB

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