ltdc.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2017
  4. *
  5. * Authors: Philippe Cornu <philippe.cornu@st.com>
  6. * Yannick Fertre <yannick.fertre@st.com>
  7. * Fabien Dessenne <fabien.dessenne@st.com>
  8. * Mickael Reulier <mickael.reulier@st.com>
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/component.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/media-bus-format.h>
  15. #include <linux/module.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/regmap.h>
  21. #include <linux/reset.h>
  22. #include <drm/drm_atomic.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_blend.h>
  25. #include <drm/drm_bridge.h>
  26. #include <drm/drm_device.h>
  27. #include <drm/drm_edid.h>
  28. #include <drm/drm_fb_dma_helper.h>
  29. #include <drm/drm_fourcc.h>
  30. #include <drm/drm_framebuffer.h>
  31. #include <drm/drm_gem_atomic_helper.h>
  32. #include <drm/drm_gem_dma_helper.h>
  33. #include <drm/drm_of.h>
  34. #include <drm/drm_probe_helper.h>
  35. #include <drm/drm_simple_kms_helper.h>
  36. #include <drm/drm_vblank.h>
  37. #include <drm/drm_managed.h>
  38. #include <video/videomode.h>
  39. #include "ltdc.h"
  40. #define NB_CRTC 1
  41. #define CRTC_MASK GENMASK(NB_CRTC - 1, 0)
  42. #define MAX_IRQ 4
  43. #define HWVER_10200 0x010200
  44. #define HWVER_10300 0x010300
  45. #define HWVER_20101 0x020101
  46. #define HWVER_40100 0x040100
  47. /*
  48. * The address of some registers depends on the HW version: such registers have
  49. * an extra offset specified with layer_ofs.
  50. */
  51. #define LAY_OFS_0 0x80
  52. #define LAY_OFS_1 0x100
  53. #define LAY_OFS (ldev->caps.layer_ofs)
  54. /* Global register offsets */
  55. #define LTDC_IDR 0x0000 /* IDentification */
  56. #define LTDC_LCR 0x0004 /* Layer Count */
  57. #define LTDC_SSCR 0x0008 /* Synchronization Size Configuration */
  58. #define LTDC_BPCR 0x000C /* Back Porch Configuration */
  59. #define LTDC_AWCR 0x0010 /* Active Width Configuration */
  60. #define LTDC_TWCR 0x0014 /* Total Width Configuration */
  61. #define LTDC_GCR 0x0018 /* Global Control */
  62. #define LTDC_GC1R 0x001C /* Global Configuration 1 */
  63. #define LTDC_GC2R 0x0020 /* Global Configuration 2 */
  64. #define LTDC_SRCR 0x0024 /* Shadow Reload Configuration */
  65. #define LTDC_GACR 0x0028 /* GAmma Correction */
  66. #define LTDC_BCCR 0x002C /* Background Color Configuration */
  67. #define LTDC_IER 0x0034 /* Interrupt Enable */
  68. #define LTDC_ISR 0x0038 /* Interrupt Status */
  69. #define LTDC_ICR 0x003C /* Interrupt Clear */
  70. #define LTDC_LIPCR 0x0040 /* Line Interrupt Position Conf. */
  71. #define LTDC_CPSR 0x0044 /* Current Position Status */
  72. #define LTDC_CDSR 0x0048 /* Current Display Status */
  73. #define LTDC_EDCR 0x0060 /* External Display Control */
  74. #define LTDC_CCRCR 0x007C /* Computed CRC value */
  75. #define LTDC_FUT 0x0090 /* Fifo underrun Threshold */
  76. /* Layer register offsets */
  77. #define LTDC_L1C0R (ldev->caps.layer_regs[0]) /* L1 configuration 0 */
  78. #define LTDC_L1C1R (ldev->caps.layer_regs[1]) /* L1 configuration 1 */
  79. #define LTDC_L1RCR (ldev->caps.layer_regs[2]) /* L1 reload control */
  80. #define LTDC_L1CR (ldev->caps.layer_regs[3]) /* L1 control register */
  81. #define LTDC_L1WHPCR (ldev->caps.layer_regs[4]) /* L1 window horizontal position configuration */
  82. #define LTDC_L1WVPCR (ldev->caps.layer_regs[5]) /* L1 window vertical position configuration */
  83. #define LTDC_L1CKCR (ldev->caps.layer_regs[6]) /* L1 color keying configuration */
  84. #define LTDC_L1PFCR (ldev->caps.layer_regs[7]) /* L1 pixel format configuration */
  85. #define LTDC_L1CACR (ldev->caps.layer_regs[8]) /* L1 constant alpha configuration */
  86. #define LTDC_L1DCCR (ldev->caps.layer_regs[9]) /* L1 default color configuration */
  87. #define LTDC_L1BFCR (ldev->caps.layer_regs[10]) /* L1 blending factors configuration */
  88. #define LTDC_L1BLCR (ldev->caps.layer_regs[11]) /* L1 burst length configuration */
  89. #define LTDC_L1PCR (ldev->caps.layer_regs[12]) /* L1 planar configuration */
  90. #define LTDC_L1CFBAR (ldev->caps.layer_regs[13]) /* L1 color frame buffer address */
  91. #define LTDC_L1CFBLR (ldev->caps.layer_regs[14]) /* L1 color frame buffer length */
  92. #define LTDC_L1CFBLNR (ldev->caps.layer_regs[15]) /* L1 color frame buffer line number */
  93. #define LTDC_L1AFBA0R (ldev->caps.layer_regs[16]) /* L1 auxiliary frame buffer address 0 */
  94. #define LTDC_L1AFBA1R (ldev->caps.layer_regs[17]) /* L1 auxiliary frame buffer address 1 */
  95. #define LTDC_L1AFBLR (ldev->caps.layer_regs[18]) /* L1 auxiliary frame buffer length */
  96. #define LTDC_L1AFBLNR (ldev->caps.layer_regs[19]) /* L1 auxiliary frame buffer line number */
  97. #define LTDC_L1CLUTWR (ldev->caps.layer_regs[20]) /* L1 CLUT write */
  98. #define LTDC_L1CYR0R (ldev->caps.layer_regs[21]) /* L1 Conversion YCbCr RGB 0 */
  99. #define LTDC_L1CYR1R (ldev->caps.layer_regs[22]) /* L1 Conversion YCbCr RGB 1 */
  100. #define LTDC_L1FPF0R (ldev->caps.layer_regs[23]) /* L1 Flexible Pixel Format 0 */
  101. #define LTDC_L1FPF1R (ldev->caps.layer_regs[24]) /* L1 Flexible Pixel Format 1 */
  102. /* Bit definitions */
  103. #define SSCR_VSH GENMASK(10, 0) /* Vertical Synchronization Height */
  104. #define SSCR_HSW GENMASK(27, 16) /* Horizontal Synchronization Width */
  105. #define BPCR_AVBP GENMASK(10, 0) /* Accumulated Vertical Back Porch */
  106. #define BPCR_AHBP GENMASK(27, 16) /* Accumulated Horizontal Back Porch */
  107. #define AWCR_AAH GENMASK(10, 0) /* Accumulated Active Height */
  108. #define AWCR_AAW GENMASK(27, 16) /* Accumulated Active Width */
  109. #define TWCR_TOTALH GENMASK(10, 0) /* TOTAL Height */
  110. #define TWCR_TOTALW GENMASK(27, 16) /* TOTAL Width */
  111. #define GCR_LTDCEN BIT(0) /* LTDC ENable */
  112. #define GCR_DEN BIT(16) /* Dither ENable */
  113. #define GCR_CRCEN BIT(19) /* CRC ENable */
  114. #define GCR_PCPOL BIT(28) /* Pixel Clock POLarity-Inverted */
  115. #define GCR_DEPOL BIT(29) /* Data Enable POLarity-High */
  116. #define GCR_VSPOL BIT(30) /* Vertical Synchro POLarity-High */
  117. #define GCR_HSPOL BIT(31) /* Horizontal Synchro POLarity-High */
  118. #define GC1R_WBCH GENMASK(3, 0) /* Width of Blue CHannel output */
  119. #define GC1R_WGCH GENMASK(7, 4) /* Width of Green Channel output */
  120. #define GC1R_WRCH GENMASK(11, 8) /* Width of Red Channel output */
  121. #define GC1R_PBEN BIT(12) /* Precise Blending ENable */
  122. #define GC1R_DT GENMASK(15, 14) /* Dithering Technique */
  123. #define GC1R_GCT GENMASK(19, 17) /* Gamma Correction Technique */
  124. #define GC1R_SHREN BIT(21) /* SHadow Registers ENabled */
  125. #define GC1R_BCP BIT(22) /* Background Colour Programmable */
  126. #define GC1R_BBEN BIT(23) /* Background Blending ENabled */
  127. #define GC1R_LNIP BIT(24) /* Line Number IRQ Position */
  128. #define GC1R_TP BIT(25) /* Timing Programmable */
  129. #define GC1R_IPP BIT(26) /* IRQ Polarity Programmable */
  130. #define GC1R_SPP BIT(27) /* Sync Polarity Programmable */
  131. #define GC1R_DWP BIT(28) /* Dither Width Programmable */
  132. #define GC1R_STREN BIT(29) /* STatus Registers ENabled */
  133. #define GC1R_BMEN BIT(31) /* Blind Mode ENabled */
  134. #define GC2R_EDCA BIT(0) /* External Display Control Ability */
  135. #define GC2R_STSAEN BIT(1) /* Slave Timing Sync Ability ENabled */
  136. #define GC2R_DVAEN BIT(2) /* Dual-View Ability ENabled */
  137. #define GC2R_DPAEN BIT(3) /* Dual-Port Ability ENabled */
  138. #define GC2R_BW GENMASK(6, 4) /* Bus Width (log2 of nb of bytes) */
  139. #define GC2R_EDCEN BIT(7) /* External Display Control ENabled */
  140. #define SRCR_IMR BIT(0) /* IMmediate Reload */
  141. #define SRCR_VBR BIT(1) /* Vertical Blanking Reload */
  142. #define BCCR_BCBLACK 0x00 /* Background Color BLACK */
  143. #define BCCR_BCBLUE GENMASK(7, 0) /* Background Color BLUE */
  144. #define BCCR_BCGREEN GENMASK(15, 8) /* Background Color GREEN */
  145. #define BCCR_BCRED GENMASK(23, 16) /* Background Color RED */
  146. #define BCCR_BCWHITE GENMASK(23, 0) /* Background Color WHITE */
  147. #define IER_LIE BIT(0) /* Line Interrupt Enable */
  148. #define IER_FUWIE BIT(1) /* Fifo Underrun Warning Interrupt Enable */
  149. #define IER_TERRIE BIT(2) /* Transfer ERRor Interrupt Enable */
  150. #define IER_RRIE BIT(3) /* Register Reload Interrupt Enable */
  151. #define IER_FUEIE BIT(6) /* Fifo Underrun Error Interrupt Enable */
  152. #define IER_CRCIE BIT(7) /* CRC Error Interrupt Enable */
  153. #define IER_MASK (IER_LIE | IER_FUWIE | IER_TERRIE | IER_RRIE | IER_FUEIE | IER_CRCIE)
  154. #define CPSR_CYPOS GENMASK(15, 0) /* Current Y position */
  155. #define ISR_LIF BIT(0) /* Line Interrupt Flag */
  156. #define ISR_FUWIF BIT(1) /* Fifo Underrun Warning Interrupt Flag */
  157. #define ISR_TERRIF BIT(2) /* Transfer ERRor Interrupt Flag */
  158. #define ISR_RRIF BIT(3) /* Register Reload Interrupt Flag */
  159. #define ISR_FUEIF BIT(6) /* Fifo Underrun Error Interrupt Flag */
  160. #define ISR_CRCIF BIT(7) /* CRC Error Interrupt Flag */
  161. #define EDCR_OCYEN BIT(25) /* Output Conversion to YCbCr 422: ENable */
  162. #define EDCR_OCYSEL BIT(26) /* Output Conversion to YCbCr 422: SELection of the CCIR */
  163. #define EDCR_OCYCO BIT(27) /* Output Conversion to YCbCr 422: Chrominance Order */
  164. #define LXCR_LEN BIT(0) /* Layer ENable */
  165. #define LXCR_COLKEN BIT(1) /* Color Keying Enable */
  166. #define LXCR_CLUTEN BIT(4) /* Color Look-Up Table ENable */
  167. #define LXCR_HMEN BIT(8) /* Horizontal Mirroring ENable */
  168. #define LXCR_MASK (LXCR_LEN | LXCR_COLKEN | LXCR_CLUTEN | LXCR_HMEN)
  169. #define LXWHPCR_WHSTPOS GENMASK(11, 0) /* Window Horizontal StarT POSition */
  170. #define LXWHPCR_WHSPPOS GENMASK(27, 16) /* Window Horizontal StoP POSition */
  171. #define LXWVPCR_WVSTPOS GENMASK(10, 0) /* Window Vertical StarT POSition */
  172. #define LXWVPCR_WVSPPOS GENMASK(26, 16) /* Window Vertical StoP POSition */
  173. #define LXPFCR_PF GENMASK(2, 0) /* Pixel Format */
  174. #define PF_FLEXIBLE 0x7 /* Flexible Pixel Format selected */
  175. #define LXCACR_CONSTA GENMASK(7, 0) /* CONSTant Alpha */
  176. #define LXBFCR_BF2 GENMASK(2, 0) /* Blending Factor 2 */
  177. #define LXBFCR_BF1 GENMASK(10, 8) /* Blending Factor 1 */
  178. #define LXBFCR_BOR GENMASK(18, 16) /* Blending ORder */
  179. #define LXCFBLR_CFBLL GENMASK(12, 0) /* Color Frame Buffer Line Length */
  180. #define LXCFBLR_CFBP GENMASK(31, 16) /* Color Frame Buffer Pitch in bytes */
  181. #define LXCFBLNR_CFBLN GENMASK(10, 0) /* Color Frame Buffer Line Number */
  182. #define LXCR_C1R_YIA BIT(0) /* Ycbcr 422 Interleaved Ability */
  183. #define LXCR_C1R_YSPA BIT(1) /* Ycbcr 420 Semi-Planar Ability */
  184. #define LXCR_C1R_YFPA BIT(2) /* Ycbcr 420 Full-Planar Ability */
  185. #define LXCR_C1R_SCA BIT(31) /* SCaling Ability*/
  186. #define LxPCR_YREN BIT(9) /* Y Rescale Enable for the color dynamic range */
  187. #define LxPCR_OF BIT(8) /* Odd pixel First */
  188. #define LxPCR_CBF BIT(7) /* CB component First */
  189. #define LxPCR_YF BIT(6) /* Y component First */
  190. #define LxPCR_YCM GENMASK(5, 4) /* Ycbcr Conversion Mode */
  191. #define YCM_I 0x0 /* Interleaved 422 */
  192. #define YCM_SP 0x1 /* Semi-Planar 420 */
  193. #define YCM_FP 0x2 /* Full-Planar 420 */
  194. #define LxPCR_YCEN BIT(3) /* YCbCr-to-RGB Conversion Enable */
  195. #define LXRCR_IMR BIT(0) /* IMmediate Reload */
  196. #define LXRCR_VBR BIT(1) /* Vertical Blanking Reload */
  197. #define LXRCR_GRMSK BIT(2) /* Global (centralized) Reload MaSKed */
  198. #define CLUT_SIZE 256
  199. #define CONSTA_MAX 0xFF /* CONSTant Alpha MAX= 1.0 */
  200. #define BF1_PAXCA 0x600 /* Pixel Alpha x Constant Alpha */
  201. #define BF1_CA 0x400 /* Constant Alpha */
  202. #define BF2_1PAXCA 0x007 /* 1 - (Pixel Alpha x Constant Alpha) */
  203. #define BF2_1CA 0x005 /* 1 - Constant Alpha */
  204. #define NB_PF 8 /* Max nb of HW pixel format */
  205. #define FUT_DFT 128 /* Default value of fifo underrun threshold */
  206. /*
  207. * Skip the first value and the second in case CRC was enabled during
  208. * the thread irq. This is to be sure CRC value is relevant for the
  209. * frame.
  210. */
  211. #define CRC_SKIP_FRAMES 2
  212. enum ltdc_pix_fmt {
  213. PF_NONE,
  214. /* RGB formats */
  215. PF_ARGB8888, /* ARGB [32 bits] */
  216. PF_RGBA8888, /* RGBA [32 bits] */
  217. PF_ABGR8888, /* ABGR [32 bits] */
  218. PF_BGRA8888, /* BGRA [32 bits] */
  219. PF_RGB888, /* RGB [24 bits] */
  220. PF_BGR888, /* BGR [24 bits] */
  221. PF_RGB565, /* RGB [16 bits] */
  222. PF_BGR565, /* BGR [16 bits] */
  223. PF_ARGB1555, /* ARGB A:1 bit RGB:15 bits [16 bits] */
  224. PF_ARGB4444, /* ARGB A:4 bits R/G/B: 4 bits each [16 bits] */
  225. /* Indexed formats */
  226. PF_L8, /* Indexed 8 bits [8 bits] */
  227. PF_AL44, /* Alpha:4 bits + indexed 4 bits [8 bits] */
  228. PF_AL88 /* Alpha:8 bits + indexed 8 bits [16 bits] */
  229. };
  230. /* The index gives the encoding of the pixel format for an HW version */
  231. static const enum ltdc_pix_fmt ltdc_pix_fmt_a0[NB_PF] = {
  232. PF_ARGB8888, /* 0x00 */
  233. PF_RGB888, /* 0x01 */
  234. PF_RGB565, /* 0x02 */
  235. PF_ARGB1555, /* 0x03 */
  236. PF_ARGB4444, /* 0x04 */
  237. PF_L8, /* 0x05 */
  238. PF_AL44, /* 0x06 */
  239. PF_AL88 /* 0x07 */
  240. };
  241. static const enum ltdc_pix_fmt ltdc_pix_fmt_a1[NB_PF] = {
  242. PF_ARGB8888, /* 0x00 */
  243. PF_RGB888, /* 0x01 */
  244. PF_RGB565, /* 0x02 */
  245. PF_RGBA8888, /* 0x03 */
  246. PF_AL44, /* 0x04 */
  247. PF_L8, /* 0x05 */
  248. PF_ARGB1555, /* 0x06 */
  249. PF_ARGB4444 /* 0x07 */
  250. };
  251. static const enum ltdc_pix_fmt ltdc_pix_fmt_a2[NB_PF] = {
  252. PF_ARGB8888, /* 0x00 */
  253. PF_ABGR8888, /* 0x01 */
  254. PF_RGBA8888, /* 0x02 */
  255. PF_BGRA8888, /* 0x03 */
  256. PF_RGB565, /* 0x04 */
  257. PF_BGR565, /* 0x05 */
  258. PF_RGB888, /* 0x06 */
  259. PF_NONE /* 0x07 */
  260. };
  261. static const u32 ltdc_drm_fmt_a0[] = {
  262. DRM_FORMAT_ARGB8888,
  263. DRM_FORMAT_XRGB8888,
  264. DRM_FORMAT_RGB888,
  265. DRM_FORMAT_RGB565,
  266. DRM_FORMAT_ARGB1555,
  267. DRM_FORMAT_XRGB1555,
  268. DRM_FORMAT_ARGB4444,
  269. DRM_FORMAT_XRGB4444,
  270. DRM_FORMAT_C8
  271. };
  272. static const u32 ltdc_drm_fmt_a1[] = {
  273. DRM_FORMAT_ARGB8888,
  274. DRM_FORMAT_XRGB8888,
  275. DRM_FORMAT_RGB888,
  276. DRM_FORMAT_RGB565,
  277. DRM_FORMAT_RGBA8888,
  278. DRM_FORMAT_RGBX8888,
  279. DRM_FORMAT_ARGB1555,
  280. DRM_FORMAT_XRGB1555,
  281. DRM_FORMAT_ARGB4444,
  282. DRM_FORMAT_XRGB4444,
  283. DRM_FORMAT_C8
  284. };
  285. static const u32 ltdc_drm_fmt_a2[] = {
  286. DRM_FORMAT_ARGB8888,
  287. DRM_FORMAT_XRGB8888,
  288. DRM_FORMAT_ABGR8888,
  289. DRM_FORMAT_XBGR8888,
  290. DRM_FORMAT_RGBA8888,
  291. DRM_FORMAT_RGBX8888,
  292. DRM_FORMAT_BGRA8888,
  293. DRM_FORMAT_BGRX8888,
  294. DRM_FORMAT_RGB565,
  295. DRM_FORMAT_BGR565,
  296. DRM_FORMAT_RGB888,
  297. DRM_FORMAT_BGR888,
  298. DRM_FORMAT_ARGB1555,
  299. DRM_FORMAT_XRGB1555,
  300. DRM_FORMAT_ARGB4444,
  301. DRM_FORMAT_XRGB4444,
  302. DRM_FORMAT_C8
  303. };
  304. static const u32 ltdc_drm_fmt_ycbcr_cp[] = {
  305. DRM_FORMAT_YUYV,
  306. DRM_FORMAT_YVYU,
  307. DRM_FORMAT_UYVY,
  308. DRM_FORMAT_VYUY
  309. };
  310. static const u32 ltdc_drm_fmt_ycbcr_sp[] = {
  311. DRM_FORMAT_NV12,
  312. DRM_FORMAT_NV21
  313. };
  314. static const u32 ltdc_drm_fmt_ycbcr_fp[] = {
  315. DRM_FORMAT_YUV420,
  316. DRM_FORMAT_YVU420
  317. };
  318. /* Layer register offsets */
  319. static const u32 ltdc_layer_regs_a0[] = {
  320. 0x80, /* L1 configuration 0 */
  321. 0x00, /* not available */
  322. 0x00, /* not available */
  323. 0x84, /* L1 control register */
  324. 0x88, /* L1 window horizontal position configuration */
  325. 0x8c, /* L1 window vertical position configuration */
  326. 0x90, /* L1 color keying configuration */
  327. 0x94, /* L1 pixel format configuration */
  328. 0x98, /* L1 constant alpha configuration */
  329. 0x9c, /* L1 default color configuration */
  330. 0xa0, /* L1 blending factors configuration */
  331. 0x00, /* not available */
  332. 0x00, /* not available */
  333. 0xac, /* L1 color frame buffer address */
  334. 0xb0, /* L1 color frame buffer length */
  335. 0xb4, /* L1 color frame buffer line number */
  336. 0x00, /* not available */
  337. 0x00, /* not available */
  338. 0x00, /* not available */
  339. 0x00, /* not available */
  340. 0xc4, /* L1 CLUT write */
  341. 0x00, /* not available */
  342. 0x00, /* not available */
  343. 0x00, /* not available */
  344. 0x00 /* not available */
  345. };
  346. static const u32 ltdc_layer_regs_a1[] = {
  347. 0x80, /* L1 configuration 0 */
  348. 0x84, /* L1 configuration 1 */
  349. 0x00, /* L1 reload control */
  350. 0x88, /* L1 control register */
  351. 0x8c, /* L1 window horizontal position configuration */
  352. 0x90, /* L1 window vertical position configuration */
  353. 0x94, /* L1 color keying configuration */
  354. 0x98, /* L1 pixel format configuration */
  355. 0x9c, /* L1 constant alpha configuration */
  356. 0xa0, /* L1 default color configuration */
  357. 0xa4, /* L1 blending factors configuration */
  358. 0xa8, /* L1 burst length configuration */
  359. 0x00, /* not available */
  360. 0xac, /* L1 color frame buffer address */
  361. 0xb0, /* L1 color frame buffer length */
  362. 0xb4, /* L1 color frame buffer line number */
  363. 0xb8, /* L1 auxiliary frame buffer address 0 */
  364. 0xbc, /* L1 auxiliary frame buffer address 1 */
  365. 0xc0, /* L1 auxiliary frame buffer length */
  366. 0xc4, /* L1 auxiliary frame buffer line number */
  367. 0xc8, /* L1 CLUT write */
  368. 0x00, /* not available */
  369. 0x00, /* not available */
  370. 0x00, /* not available */
  371. 0x00 /* not available */
  372. };
  373. static const u32 ltdc_layer_regs_a2[] = {
  374. 0x100, /* L1 configuration 0 */
  375. 0x104, /* L1 configuration 1 */
  376. 0x108, /* L1 reload control */
  377. 0x10c, /* L1 control register */
  378. 0x110, /* L1 window horizontal position configuration */
  379. 0x114, /* L1 window vertical position configuration */
  380. 0x118, /* L1 color keying configuration */
  381. 0x11c, /* L1 pixel format configuration */
  382. 0x120, /* L1 constant alpha configuration */
  383. 0x124, /* L1 default color configuration */
  384. 0x128, /* L1 blending factors configuration */
  385. 0x12c, /* L1 burst length configuration */
  386. 0x130, /* L1 planar configuration */
  387. 0x134, /* L1 color frame buffer address */
  388. 0x138, /* L1 color frame buffer length */
  389. 0x13c, /* L1 color frame buffer line number */
  390. 0x140, /* L1 auxiliary frame buffer address 0 */
  391. 0x144, /* L1 auxiliary frame buffer address 1 */
  392. 0x148, /* L1 auxiliary frame buffer length */
  393. 0x14c, /* L1 auxiliary frame buffer line number */
  394. 0x150, /* L1 CLUT write */
  395. 0x16c, /* L1 Conversion YCbCr RGB 0 */
  396. 0x170, /* L1 Conversion YCbCr RGB 1 */
  397. 0x174, /* L1 Flexible Pixel Format 0 */
  398. 0x178 /* L1 Flexible Pixel Format 1 */
  399. };
  400. static const u64 ltdc_format_modifiers[] = {
  401. DRM_FORMAT_MOD_LINEAR,
  402. DRM_FORMAT_MOD_INVALID
  403. };
  404. static const struct regmap_config stm32_ltdc_regmap_cfg = {
  405. .reg_bits = 32,
  406. .val_bits = 32,
  407. .reg_stride = sizeof(u32),
  408. .max_register = 0x400,
  409. .use_relaxed_mmio = true,
  410. .cache_type = REGCACHE_NONE,
  411. };
  412. static const u32 ltdc_ycbcr2rgb_coeffs[DRM_COLOR_ENCODING_MAX][DRM_COLOR_RANGE_MAX][2] = {
  413. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  414. 0x02040199, /* (b_cb = 516 / r_cr = 409) */
  415. 0x006400D0 /* (g_cb = 100 / g_cr = 208) */
  416. },
  417. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_FULL_RANGE] = {
  418. 0x01C60167, /* (b_cb = 454 / r_cr = 359) */
  419. 0x005800B7 /* (g_cb = 88 / g_cr = 183) */
  420. },
  421. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  422. 0x021D01CB, /* (b_cb = 541 / r_cr = 459) */
  423. 0x00370089 /* (g_cb = 55 / g_cr = 137) */
  424. },
  425. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_FULL_RANGE] = {
  426. 0x01DB0193, /* (b_cb = 475 / r_cr = 403) */
  427. 0x00300078 /* (g_cb = 48 / g_cr = 120) */
  428. }
  429. /* BT2020 not supported */
  430. };
  431. static inline struct ltdc_device *crtc_to_ltdc(struct drm_crtc *crtc)
  432. {
  433. return (struct ltdc_device *)crtc->dev->dev_private;
  434. }
  435. static inline struct ltdc_device *plane_to_ltdc(struct drm_plane *plane)
  436. {
  437. return (struct ltdc_device *)plane->dev->dev_private;
  438. }
  439. static inline enum ltdc_pix_fmt to_ltdc_pixelformat(u32 drm_fmt)
  440. {
  441. enum ltdc_pix_fmt pf;
  442. switch (drm_fmt) {
  443. case DRM_FORMAT_ARGB8888:
  444. case DRM_FORMAT_XRGB8888:
  445. pf = PF_ARGB8888;
  446. break;
  447. case DRM_FORMAT_ABGR8888:
  448. case DRM_FORMAT_XBGR8888:
  449. pf = PF_ABGR8888;
  450. break;
  451. case DRM_FORMAT_RGBA8888:
  452. case DRM_FORMAT_RGBX8888:
  453. pf = PF_RGBA8888;
  454. break;
  455. case DRM_FORMAT_BGRA8888:
  456. case DRM_FORMAT_BGRX8888:
  457. pf = PF_BGRA8888;
  458. break;
  459. case DRM_FORMAT_RGB888:
  460. pf = PF_RGB888;
  461. break;
  462. case DRM_FORMAT_BGR888:
  463. pf = PF_BGR888;
  464. break;
  465. case DRM_FORMAT_RGB565:
  466. pf = PF_RGB565;
  467. break;
  468. case DRM_FORMAT_BGR565:
  469. pf = PF_BGR565;
  470. break;
  471. case DRM_FORMAT_ARGB1555:
  472. case DRM_FORMAT_XRGB1555:
  473. pf = PF_ARGB1555;
  474. break;
  475. case DRM_FORMAT_ARGB4444:
  476. case DRM_FORMAT_XRGB4444:
  477. pf = PF_ARGB4444;
  478. break;
  479. case DRM_FORMAT_C8:
  480. pf = PF_L8;
  481. break;
  482. default:
  483. pf = PF_NONE;
  484. break;
  485. /* Note: There are no DRM_FORMAT for AL44 and AL88 */
  486. }
  487. return pf;
  488. }
  489. static inline u32 ltdc_set_flexible_pixel_format(struct drm_plane *plane, enum ltdc_pix_fmt pix_fmt)
  490. {
  491. struct ltdc_device *ldev = plane_to_ltdc(plane);
  492. u32 lofs = plane->index * LAY_OFS, ret = PF_FLEXIBLE;
  493. int psize, alen, apos, rlen, rpos, glen, gpos, blen, bpos;
  494. switch (pix_fmt) {
  495. case PF_BGR888:
  496. psize = 3;
  497. alen = 0; apos = 0; rlen = 8; rpos = 0;
  498. glen = 8; gpos = 8; blen = 8; bpos = 16;
  499. break;
  500. case PF_ARGB1555:
  501. psize = 2;
  502. alen = 1; apos = 15; rlen = 5; rpos = 10;
  503. glen = 5; gpos = 5; blen = 5; bpos = 0;
  504. break;
  505. case PF_ARGB4444:
  506. psize = 2;
  507. alen = 4; apos = 12; rlen = 4; rpos = 8;
  508. glen = 4; gpos = 4; blen = 4; bpos = 0;
  509. break;
  510. case PF_L8:
  511. psize = 1;
  512. alen = 0; apos = 0; rlen = 8; rpos = 0;
  513. glen = 8; gpos = 0; blen = 8; bpos = 0;
  514. break;
  515. case PF_AL44:
  516. psize = 1;
  517. alen = 4; apos = 4; rlen = 4; rpos = 0;
  518. glen = 4; gpos = 0; blen = 4; bpos = 0;
  519. break;
  520. case PF_AL88:
  521. psize = 2;
  522. alen = 8; apos = 8; rlen = 8; rpos = 0;
  523. glen = 8; gpos = 0; blen = 8; bpos = 0;
  524. break;
  525. default:
  526. ret = NB_PF; /* error case, trace msg is handled by the caller */
  527. break;
  528. }
  529. if (ret == PF_FLEXIBLE) {
  530. regmap_write(ldev->regmap, LTDC_L1FPF0R + lofs,
  531. (rlen << 14) + (rpos << 9) + (alen << 5) + apos);
  532. regmap_write(ldev->regmap, LTDC_L1FPF1R + lofs,
  533. (psize << 18) + (blen << 14) + (bpos << 9) + (glen << 5) + gpos);
  534. }
  535. return ret;
  536. }
  537. /*
  538. * All non-alpha color formats derived from native alpha color formats are
  539. * either characterized by a FourCC format code
  540. */
  541. static inline u32 is_xrgb(u32 drm)
  542. {
  543. return ((drm & 0xFF) == 'X' || ((drm >> 8) & 0xFF) == 'X');
  544. }
  545. static inline void ltdc_set_ycbcr_config(struct drm_plane *plane, u32 drm_pix_fmt)
  546. {
  547. struct ltdc_device *ldev = plane_to_ltdc(plane);
  548. struct drm_plane_state *state = plane->state;
  549. u32 lofs = plane->index * LAY_OFS;
  550. u32 val;
  551. switch (drm_pix_fmt) {
  552. case DRM_FORMAT_YUYV:
  553. val = (YCM_I << 4) | LxPCR_YF | LxPCR_CBF;
  554. break;
  555. case DRM_FORMAT_YVYU:
  556. val = (YCM_I << 4) | LxPCR_YF;
  557. break;
  558. case DRM_FORMAT_UYVY:
  559. val = (YCM_I << 4) | LxPCR_CBF;
  560. break;
  561. case DRM_FORMAT_VYUY:
  562. val = (YCM_I << 4);
  563. break;
  564. case DRM_FORMAT_NV12:
  565. val = (YCM_SP << 4) | LxPCR_CBF;
  566. break;
  567. case DRM_FORMAT_NV21:
  568. val = (YCM_SP << 4);
  569. break;
  570. case DRM_FORMAT_YUV420:
  571. case DRM_FORMAT_YVU420:
  572. val = (YCM_FP << 4);
  573. break;
  574. default:
  575. /* RGB or not a YCbCr supported format */
  576. DRM_ERROR("Unsupported pixel format: %u\n", drm_pix_fmt);
  577. return;
  578. }
  579. /* Enable limited range */
  580. if (state->color_range == DRM_COLOR_YCBCR_LIMITED_RANGE)
  581. val |= LxPCR_YREN;
  582. /* enable ycbcr conversion */
  583. val |= LxPCR_YCEN;
  584. regmap_write(ldev->regmap, LTDC_L1PCR + lofs, val);
  585. }
  586. static inline void ltdc_set_ycbcr_coeffs(struct drm_plane *plane)
  587. {
  588. struct ltdc_device *ldev = plane_to_ltdc(plane);
  589. struct drm_plane_state *state = plane->state;
  590. enum drm_color_encoding enc = state->color_encoding;
  591. enum drm_color_range ran = state->color_range;
  592. u32 lofs = plane->index * LAY_OFS;
  593. if (enc != DRM_COLOR_YCBCR_BT601 && enc != DRM_COLOR_YCBCR_BT709) {
  594. DRM_ERROR("color encoding %d not supported, use bt601 by default\n", enc);
  595. /* set by default color encoding to DRM_COLOR_YCBCR_BT601 */
  596. enc = DRM_COLOR_YCBCR_BT601;
  597. }
  598. if (ran != DRM_COLOR_YCBCR_LIMITED_RANGE && ran != DRM_COLOR_YCBCR_FULL_RANGE) {
  599. DRM_ERROR("color range %d not supported, use limited range by default\n", ran);
  600. /* set by default color range to DRM_COLOR_YCBCR_LIMITED_RANGE */
  601. ran = DRM_COLOR_YCBCR_LIMITED_RANGE;
  602. }
  603. DRM_DEBUG_DRIVER("Color encoding=%d, range=%d\n", enc, ran);
  604. regmap_write(ldev->regmap, LTDC_L1CYR0R + lofs,
  605. ltdc_ycbcr2rgb_coeffs[enc][ran][0]);
  606. regmap_write(ldev->regmap, LTDC_L1CYR1R + lofs,
  607. ltdc_ycbcr2rgb_coeffs[enc][ran][1]);
  608. }
  609. static inline void ltdc_irq_crc_handle(struct ltdc_device *ldev,
  610. struct drm_crtc *crtc)
  611. {
  612. u32 crc;
  613. int ret;
  614. if (ldev->crc_skip_count < CRC_SKIP_FRAMES) {
  615. ldev->crc_skip_count++;
  616. return;
  617. }
  618. /* Get the CRC of the frame */
  619. ret = regmap_read(ldev->regmap, LTDC_CCRCR, &crc);
  620. if (ret)
  621. return;
  622. /* Report to DRM the CRC (hw dependent feature) */
  623. drm_crtc_add_crc_entry(crtc, true, drm_crtc_accurate_vblank_count(crtc), &crc);
  624. }
  625. static irqreturn_t ltdc_irq_thread(int irq, void *arg)
  626. {
  627. struct drm_device *ddev = arg;
  628. struct ltdc_device *ldev = ddev->dev_private;
  629. struct drm_crtc *crtc = drm_crtc_from_index(ddev, 0);
  630. /* Line IRQ : trigger the vblank event */
  631. if (ldev->irq_status & ISR_LIF) {
  632. drm_crtc_handle_vblank(crtc);
  633. /* Early return if CRC is not active */
  634. if (ldev->crc_active)
  635. ltdc_irq_crc_handle(ldev, crtc);
  636. }
  637. mutex_lock(&ldev->err_lock);
  638. if (ldev->irq_status & ISR_TERRIF)
  639. ldev->transfer_err++;
  640. if (ldev->irq_status & ISR_FUEIF)
  641. ldev->fifo_err++;
  642. if (ldev->irq_status & ISR_FUWIF)
  643. ldev->fifo_warn++;
  644. mutex_unlock(&ldev->err_lock);
  645. return IRQ_HANDLED;
  646. }
  647. static irqreturn_t ltdc_irq(int irq, void *arg)
  648. {
  649. struct drm_device *ddev = arg;
  650. struct ltdc_device *ldev = ddev->dev_private;
  651. /*
  652. * Read & Clear the interrupt status
  653. * In order to write / read registers in this critical section
  654. * very quickly, the regmap functions are not used.
  655. */
  656. ldev->irq_status = readl_relaxed(ldev->regs + LTDC_ISR);
  657. writel_relaxed(ldev->irq_status, ldev->regs + LTDC_ICR);
  658. return IRQ_WAKE_THREAD;
  659. }
  660. /*
  661. * DRM_CRTC
  662. */
  663. static void ltdc_crtc_update_clut(struct drm_crtc *crtc)
  664. {
  665. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  666. struct drm_color_lut *lut;
  667. u32 val;
  668. int i;
  669. if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
  670. return;
  671. lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
  672. for (i = 0; i < CLUT_SIZE; i++, lut++) {
  673. val = ((lut->red << 8) & 0xff0000) | (lut->green & 0xff00) |
  674. (lut->blue >> 8) | (i << 24);
  675. regmap_write(ldev->regmap, LTDC_L1CLUTWR, val);
  676. }
  677. }
  678. static void ltdc_crtc_atomic_enable(struct drm_crtc *crtc,
  679. struct drm_atomic_state *state)
  680. {
  681. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  682. struct drm_device *ddev = crtc->dev;
  683. DRM_DEBUG_DRIVER("\n");
  684. pm_runtime_get_sync(ddev->dev);
  685. /* Sets the background color value */
  686. regmap_write(ldev->regmap, LTDC_BCCR, BCCR_BCBLACK);
  687. /* Enable IRQ */
  688. regmap_set_bits(ldev->regmap, LTDC_IER, IER_FUWIE | IER_FUEIE | IER_TERRIE);
  689. /* Commit shadow registers = update planes at next vblank */
  690. if (!ldev->caps.plane_reg_shadow)
  691. regmap_set_bits(ldev->regmap, LTDC_SRCR, SRCR_VBR);
  692. drm_crtc_vblank_on(crtc);
  693. }
  694. static void ltdc_crtc_atomic_disable(struct drm_crtc *crtc,
  695. struct drm_atomic_state *state)
  696. {
  697. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  698. struct drm_device *ddev = crtc->dev;
  699. int layer_index = 0;
  700. DRM_DEBUG_DRIVER("\n");
  701. drm_crtc_vblank_off(crtc);
  702. /* Disable all layers */
  703. for (layer_index = 0; layer_index < ldev->caps.nb_layers; layer_index++)
  704. regmap_write_bits(ldev->regmap, LTDC_L1CR + layer_index * LAY_OFS, LXCR_MASK, 0);
  705. /* Disable IRQ */
  706. regmap_clear_bits(ldev->regmap, LTDC_IER, IER_FUWIE | IER_FUEIE | IER_TERRIE);
  707. /* immediately commit disable of layers before switching off LTDC */
  708. if (!ldev->caps.plane_reg_shadow)
  709. regmap_set_bits(ldev->regmap, LTDC_SRCR, SRCR_IMR);
  710. pm_runtime_put_sync(ddev->dev);
  711. /* clear interrupt error counters */
  712. mutex_lock(&ldev->err_lock);
  713. ldev->transfer_err = 0;
  714. ldev->fifo_err = 0;
  715. ldev->fifo_warn = 0;
  716. mutex_unlock(&ldev->err_lock);
  717. }
  718. #define CLK_TOLERANCE_HZ 50
  719. static enum drm_mode_status
  720. ltdc_crtc_mode_valid(struct drm_crtc *crtc,
  721. const struct drm_display_mode *mode)
  722. {
  723. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  724. int target = mode->clock * 1000;
  725. int target_min = target - CLK_TOLERANCE_HZ;
  726. int target_max = target + CLK_TOLERANCE_HZ;
  727. int result;
  728. result = clk_round_rate(ldev->pixel_clk, target);
  729. DRM_DEBUG_DRIVER("clk rate target %d, available %d\n", target, result);
  730. /* Filter modes according to the max frequency supported by the pads */
  731. if (result > ldev->caps.pad_max_freq_hz)
  732. return MODE_CLOCK_HIGH;
  733. /*
  734. * Accept all "preferred" modes:
  735. * - this is important for panels because panel clock tolerances are
  736. * bigger than hdmi ones and there is no reason to not accept them
  737. * (the fps may vary a little but it is not a problem).
  738. * - the hdmi preferred mode will be accepted too, but userland will
  739. * be able to use others hdmi "valid" modes if necessary.
  740. */
  741. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  742. return MODE_OK;
  743. /*
  744. * Filter modes according to the clock value, particularly useful for
  745. * hdmi modes that require precise pixel clocks.
  746. */
  747. if (result < target_min || result > target_max)
  748. return MODE_CLOCK_RANGE;
  749. return MODE_OK;
  750. }
  751. static bool ltdc_crtc_mode_fixup(struct drm_crtc *crtc,
  752. const struct drm_display_mode *mode,
  753. struct drm_display_mode *adjusted_mode)
  754. {
  755. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  756. int rate = mode->clock * 1000;
  757. if (clk_set_rate(ldev->pixel_clk, rate) < 0) {
  758. DRM_ERROR("Cannot set rate (%dHz) for pixel clk\n", rate);
  759. return false;
  760. }
  761. adjusted_mode->clock = clk_get_rate(ldev->pixel_clk) / 1000;
  762. DRM_DEBUG_DRIVER("requested clock %dkHz, adjusted clock %dkHz\n",
  763. mode->clock, adjusted_mode->clock);
  764. return true;
  765. }
  766. static void ltdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
  767. {
  768. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  769. struct drm_device *ddev = crtc->dev;
  770. struct drm_connector_list_iter iter;
  771. struct drm_connector *connector = NULL;
  772. struct drm_encoder *encoder = NULL, *en_iter;
  773. struct drm_bridge *bridge = NULL, *br_iter;
  774. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  775. u32 hsync, vsync, accum_hbp, accum_vbp, accum_act_w, accum_act_h;
  776. u32 total_width, total_height;
  777. u32 bus_formats = MEDIA_BUS_FMT_RGB888_1X24;
  778. u32 bus_flags = 0;
  779. u32 val;
  780. int ret;
  781. /* get encoder from crtc */
  782. drm_for_each_encoder(en_iter, ddev)
  783. if (en_iter->crtc == crtc) {
  784. encoder = en_iter;
  785. break;
  786. }
  787. if (encoder) {
  788. /* get bridge from encoder */
  789. list_for_each_entry(br_iter, &encoder->bridge_chain, chain_node)
  790. if (br_iter->encoder == encoder) {
  791. bridge = br_iter;
  792. break;
  793. }
  794. /* Get the connector from encoder */
  795. drm_connector_list_iter_begin(ddev, &iter);
  796. drm_for_each_connector_iter(connector, &iter)
  797. if (connector->encoder == encoder)
  798. break;
  799. drm_connector_list_iter_end(&iter);
  800. }
  801. if (bridge && bridge->timings) {
  802. bus_flags = bridge->timings->input_bus_flags;
  803. } else if (connector) {
  804. bus_flags = connector->display_info.bus_flags;
  805. if (connector->display_info.num_bus_formats)
  806. bus_formats = connector->display_info.bus_formats[0];
  807. }
  808. if (!pm_runtime_active(ddev->dev)) {
  809. ret = pm_runtime_get_sync(ddev->dev);
  810. if (ret) {
  811. DRM_ERROR("Failed to set mode, cannot get sync\n");
  812. return;
  813. }
  814. }
  815. DRM_DEBUG_DRIVER("CRTC:%d mode:%s\n", crtc->base.id, mode->name);
  816. DRM_DEBUG_DRIVER("Video mode: %dx%d", mode->hdisplay, mode->vdisplay);
  817. DRM_DEBUG_DRIVER(" hfp %d hbp %d hsl %d vfp %d vbp %d vsl %d\n",
  818. mode->hsync_start - mode->hdisplay,
  819. mode->htotal - mode->hsync_end,
  820. mode->hsync_end - mode->hsync_start,
  821. mode->vsync_start - mode->vdisplay,
  822. mode->vtotal - mode->vsync_end,
  823. mode->vsync_end - mode->vsync_start);
  824. /* Convert video timings to ltdc timings */
  825. hsync = mode->hsync_end - mode->hsync_start - 1;
  826. vsync = mode->vsync_end - mode->vsync_start - 1;
  827. accum_hbp = mode->htotal - mode->hsync_start - 1;
  828. accum_vbp = mode->vtotal - mode->vsync_start - 1;
  829. accum_act_w = accum_hbp + mode->hdisplay;
  830. accum_act_h = accum_vbp + mode->vdisplay;
  831. total_width = mode->htotal - 1;
  832. total_height = mode->vtotal - 1;
  833. /* Configures the HS, VS, DE and PC polarities. Default Active Low */
  834. val = 0;
  835. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  836. val |= GCR_HSPOL;
  837. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  838. val |= GCR_VSPOL;
  839. if (bus_flags & DRM_BUS_FLAG_DE_LOW)
  840. val |= GCR_DEPOL;
  841. if (bus_flags & DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
  842. val |= GCR_PCPOL;
  843. regmap_update_bits(ldev->regmap, LTDC_GCR,
  844. GCR_HSPOL | GCR_VSPOL | GCR_DEPOL | GCR_PCPOL, val);
  845. /* Set Synchronization size */
  846. val = (hsync << 16) | vsync;
  847. regmap_update_bits(ldev->regmap, LTDC_SSCR, SSCR_VSH | SSCR_HSW, val);
  848. /* Set Accumulated Back porch */
  849. val = (accum_hbp << 16) | accum_vbp;
  850. regmap_update_bits(ldev->regmap, LTDC_BPCR, BPCR_AVBP | BPCR_AHBP, val);
  851. /* Set Accumulated Active Width */
  852. val = (accum_act_w << 16) | accum_act_h;
  853. regmap_update_bits(ldev->regmap, LTDC_AWCR, AWCR_AAW | AWCR_AAH, val);
  854. /* Set total width & height */
  855. val = (total_width << 16) | total_height;
  856. regmap_update_bits(ldev->regmap, LTDC_TWCR, TWCR_TOTALH | TWCR_TOTALW, val);
  857. regmap_write(ldev->regmap, LTDC_LIPCR, (accum_act_h + 1));
  858. /* Configure the output format (hw version dependent) */
  859. if (ldev->caps.ycbcr_output) {
  860. /* Input video dynamic_range & colorimetry */
  861. int vic = drm_match_cea_mode(mode);
  862. u32 val;
  863. if (vic == 6 || vic == 7 || vic == 21 || vic == 22 ||
  864. vic == 2 || vic == 3 || vic == 17 || vic == 18)
  865. /* ITU-R BT.601 */
  866. val = 0;
  867. else
  868. /* ITU-R BT.709 */
  869. val = EDCR_OCYSEL;
  870. switch (bus_formats) {
  871. case MEDIA_BUS_FMT_YUYV8_1X16:
  872. /* enable ycbcr output converter */
  873. regmap_write(ldev->regmap, LTDC_EDCR, EDCR_OCYEN | val);
  874. break;
  875. case MEDIA_BUS_FMT_YVYU8_1X16:
  876. /* enable ycbcr output converter & invert chrominance order */
  877. regmap_write(ldev->regmap, LTDC_EDCR, EDCR_OCYEN | EDCR_OCYCO | val);
  878. break;
  879. default:
  880. /* disable ycbcr output converter */
  881. regmap_write(ldev->regmap, LTDC_EDCR, 0);
  882. break;
  883. }
  884. }
  885. }
  886. static void ltdc_crtc_atomic_flush(struct drm_crtc *crtc,
  887. struct drm_atomic_state *state)
  888. {
  889. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  890. struct drm_device *ddev = crtc->dev;
  891. struct drm_pending_vblank_event *event = crtc->state->event;
  892. DRM_DEBUG_ATOMIC("\n");
  893. ltdc_crtc_update_clut(crtc);
  894. /* Commit shadow registers = update planes at next vblank */
  895. if (!ldev->caps.plane_reg_shadow)
  896. regmap_set_bits(ldev->regmap, LTDC_SRCR, SRCR_VBR);
  897. if (event) {
  898. crtc->state->event = NULL;
  899. spin_lock_irq(&ddev->event_lock);
  900. if (drm_crtc_vblank_get(crtc) == 0)
  901. drm_crtc_arm_vblank_event(crtc, event);
  902. else
  903. drm_crtc_send_vblank_event(crtc, event);
  904. spin_unlock_irq(&ddev->event_lock);
  905. }
  906. }
  907. static bool ltdc_crtc_get_scanout_position(struct drm_crtc *crtc,
  908. bool in_vblank_irq,
  909. int *vpos, int *hpos,
  910. ktime_t *stime, ktime_t *etime,
  911. const struct drm_display_mode *mode)
  912. {
  913. struct drm_device *ddev = crtc->dev;
  914. struct ltdc_device *ldev = ddev->dev_private;
  915. int line, vactive_start, vactive_end, vtotal;
  916. if (stime)
  917. *stime = ktime_get();
  918. /* The active area starts after vsync + front porch and ends
  919. * at vsync + front porc + display size.
  920. * The total height also include back porch.
  921. * We have 3 possible cases to handle:
  922. * - line < vactive_start: vpos = line - vactive_start and will be
  923. * negative
  924. * - vactive_start < line < vactive_end: vpos = line - vactive_start
  925. * and will be positive
  926. * - line > vactive_end: vpos = line - vtotal - vactive_start
  927. * and will negative
  928. *
  929. * Computation for the two first cases are identical so we can
  930. * simplify the code and only test if line > vactive_end
  931. */
  932. if (pm_runtime_active(ddev->dev)) {
  933. regmap_read(ldev->regmap, LTDC_CPSR, &line);
  934. line &= CPSR_CYPOS;
  935. regmap_read(ldev->regmap, LTDC_BPCR, &vactive_start);
  936. vactive_start &= BPCR_AVBP;
  937. regmap_read(ldev->regmap, LTDC_AWCR, &vactive_end);
  938. vactive_end &= AWCR_AAH;
  939. regmap_read(ldev->regmap, LTDC_TWCR, &vtotal);
  940. vtotal &= TWCR_TOTALH;
  941. if (line > vactive_end)
  942. *vpos = line - vtotal - vactive_start;
  943. else
  944. *vpos = line - vactive_start;
  945. } else {
  946. *vpos = 0;
  947. }
  948. *hpos = 0;
  949. if (etime)
  950. *etime = ktime_get();
  951. return true;
  952. }
  953. static const struct drm_crtc_helper_funcs ltdc_crtc_helper_funcs = {
  954. .mode_valid = ltdc_crtc_mode_valid,
  955. .mode_fixup = ltdc_crtc_mode_fixup,
  956. .mode_set_nofb = ltdc_crtc_mode_set_nofb,
  957. .atomic_flush = ltdc_crtc_atomic_flush,
  958. .atomic_enable = ltdc_crtc_atomic_enable,
  959. .atomic_disable = ltdc_crtc_atomic_disable,
  960. .get_scanout_position = ltdc_crtc_get_scanout_position,
  961. };
  962. static int ltdc_crtc_enable_vblank(struct drm_crtc *crtc)
  963. {
  964. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  965. struct drm_crtc_state *state = crtc->state;
  966. DRM_DEBUG_DRIVER("\n");
  967. if (state->enable)
  968. regmap_set_bits(ldev->regmap, LTDC_IER, IER_LIE);
  969. else
  970. return -EPERM;
  971. return 0;
  972. }
  973. static void ltdc_crtc_disable_vblank(struct drm_crtc *crtc)
  974. {
  975. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  976. DRM_DEBUG_DRIVER("\n");
  977. regmap_clear_bits(ldev->regmap, LTDC_IER, IER_LIE);
  978. }
  979. static int ltdc_crtc_set_crc_source(struct drm_crtc *crtc, const char *source)
  980. {
  981. struct ltdc_device *ldev;
  982. int ret;
  983. DRM_DEBUG_DRIVER("\n");
  984. if (!crtc)
  985. return -ENODEV;
  986. ldev = crtc_to_ltdc(crtc);
  987. if (source && strcmp(source, "auto") == 0) {
  988. ldev->crc_active = true;
  989. ret = regmap_set_bits(ldev->regmap, LTDC_GCR, GCR_CRCEN);
  990. } else if (!source) {
  991. ldev->crc_active = false;
  992. ret = regmap_clear_bits(ldev->regmap, LTDC_GCR, GCR_CRCEN);
  993. } else {
  994. ret = -EINVAL;
  995. }
  996. ldev->crc_skip_count = 0;
  997. return ret;
  998. }
  999. static int ltdc_crtc_verify_crc_source(struct drm_crtc *crtc,
  1000. const char *source, size_t *values_cnt)
  1001. {
  1002. DRM_DEBUG_DRIVER("\n");
  1003. if (!crtc)
  1004. return -ENODEV;
  1005. if (source && strcmp(source, "auto") != 0) {
  1006. DRM_DEBUG_DRIVER("Unknown CRC source %s for %s\n",
  1007. source, crtc->name);
  1008. return -EINVAL;
  1009. }
  1010. *values_cnt = 1;
  1011. return 0;
  1012. }
  1013. static void ltdc_crtc_atomic_print_state(struct drm_printer *p,
  1014. const struct drm_crtc_state *state)
  1015. {
  1016. struct drm_crtc *crtc = state->crtc;
  1017. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  1018. drm_printf(p, "\ttransfer_error=%d\n", ldev->transfer_err);
  1019. drm_printf(p, "\tfifo_underrun_error=%d\n", ldev->fifo_err);
  1020. drm_printf(p, "\tfifo_underrun_warning=%d\n", ldev->fifo_warn);
  1021. drm_printf(p, "\tfifo_underrun_threshold=%d\n", ldev->fifo_threshold);
  1022. }
  1023. static const struct drm_crtc_funcs ltdc_crtc_funcs = {
  1024. .set_config = drm_atomic_helper_set_config,
  1025. .page_flip = drm_atomic_helper_page_flip,
  1026. .reset = drm_atomic_helper_crtc_reset,
  1027. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  1028. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  1029. .enable_vblank = ltdc_crtc_enable_vblank,
  1030. .disable_vblank = ltdc_crtc_disable_vblank,
  1031. .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
  1032. .atomic_print_state = ltdc_crtc_atomic_print_state,
  1033. };
  1034. static const struct drm_crtc_funcs ltdc_crtc_with_crc_support_funcs = {
  1035. .set_config = drm_atomic_helper_set_config,
  1036. .page_flip = drm_atomic_helper_page_flip,
  1037. .reset = drm_atomic_helper_crtc_reset,
  1038. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  1039. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  1040. .enable_vblank = ltdc_crtc_enable_vblank,
  1041. .disable_vblank = ltdc_crtc_disable_vblank,
  1042. .get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
  1043. .set_crc_source = ltdc_crtc_set_crc_source,
  1044. .verify_crc_source = ltdc_crtc_verify_crc_source,
  1045. .atomic_print_state = ltdc_crtc_atomic_print_state,
  1046. };
  1047. /*
  1048. * DRM_PLANE
  1049. */
  1050. static int ltdc_plane_atomic_check(struct drm_plane *plane,
  1051. struct drm_atomic_state *state)
  1052. {
  1053. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  1054. plane);
  1055. struct drm_framebuffer *fb = new_plane_state->fb;
  1056. u32 src_w, src_h;
  1057. DRM_DEBUG_DRIVER("\n");
  1058. if (!fb)
  1059. return 0;
  1060. /* convert src_ from 16:16 format */
  1061. src_w = new_plane_state->src_w >> 16;
  1062. src_h = new_plane_state->src_h >> 16;
  1063. /* Reject scaling */
  1064. if (src_w != new_plane_state->crtc_w || src_h != new_plane_state->crtc_h) {
  1065. DRM_DEBUG_DRIVER("Scaling is not supported");
  1066. return -EINVAL;
  1067. }
  1068. return 0;
  1069. }
  1070. static void ltdc_plane_atomic_update(struct drm_plane *plane,
  1071. struct drm_atomic_state *state)
  1072. {
  1073. struct ltdc_device *ldev = plane_to_ltdc(plane);
  1074. struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state,
  1075. plane);
  1076. struct drm_framebuffer *fb = newstate->fb;
  1077. u32 lofs = plane->index * LAY_OFS;
  1078. u32 x0 = newstate->crtc_x;
  1079. u32 x1 = newstate->crtc_x + newstate->crtc_w - 1;
  1080. u32 y0 = newstate->crtc_y;
  1081. u32 y1 = newstate->crtc_y + newstate->crtc_h - 1;
  1082. u32 src_x, src_y, src_w, src_h;
  1083. u32 val, pitch_in_bytes, line_length, line_number, ahbp, avbp, bpcr;
  1084. u32 paddr, paddr1, paddr2;
  1085. enum ltdc_pix_fmt pf;
  1086. if (!newstate->crtc || !fb) {
  1087. DRM_DEBUG_DRIVER("fb or crtc NULL");
  1088. return;
  1089. }
  1090. /* convert src_ from 16:16 format */
  1091. src_x = newstate->src_x >> 16;
  1092. src_y = newstate->src_y >> 16;
  1093. src_w = newstate->src_w >> 16;
  1094. src_h = newstate->src_h >> 16;
  1095. DRM_DEBUG_DRIVER("plane:%d fb:%d (%dx%d)@(%d,%d) -> (%dx%d)@(%d,%d)\n",
  1096. plane->base.id, fb->base.id,
  1097. src_w, src_h, src_x, src_y,
  1098. newstate->crtc_w, newstate->crtc_h,
  1099. newstate->crtc_x, newstate->crtc_y);
  1100. regmap_read(ldev->regmap, LTDC_BPCR, &bpcr);
  1101. ahbp = (bpcr & BPCR_AHBP) >> 16;
  1102. avbp = bpcr & BPCR_AVBP;
  1103. /* Configures the horizontal start and stop position */
  1104. val = ((x1 + 1 + ahbp) << 16) + (x0 + 1 + ahbp);
  1105. regmap_write_bits(ldev->regmap, LTDC_L1WHPCR + lofs,
  1106. LXWHPCR_WHSTPOS | LXWHPCR_WHSPPOS, val);
  1107. /* Configures the vertical start and stop position */
  1108. val = ((y1 + 1 + avbp) << 16) + (y0 + 1 + avbp);
  1109. regmap_write_bits(ldev->regmap, LTDC_L1WVPCR + lofs,
  1110. LXWVPCR_WVSTPOS | LXWVPCR_WVSPPOS, val);
  1111. /* Specifies the pixel format */
  1112. pf = to_ltdc_pixelformat(fb->format->format);
  1113. for (val = 0; val < NB_PF; val++)
  1114. if (ldev->caps.pix_fmt_hw[val] == pf)
  1115. break;
  1116. /* Use the flexible color format feature if necessary and available */
  1117. if (ldev->caps.pix_fmt_flex && val == NB_PF)
  1118. val = ltdc_set_flexible_pixel_format(plane, pf);
  1119. if (val == NB_PF) {
  1120. DRM_ERROR("Pixel format %.4s not supported\n",
  1121. (char *)&fb->format->format);
  1122. val = 0; /* set by default ARGB 32 bits */
  1123. }
  1124. regmap_write_bits(ldev->regmap, LTDC_L1PFCR + lofs, LXPFCR_PF, val);
  1125. /* Specifies the constant alpha value */
  1126. val = newstate->alpha >> 8;
  1127. regmap_write_bits(ldev->regmap, LTDC_L1CACR + lofs, LXCACR_CONSTA, val);
  1128. /* Specifies the blending factors */
  1129. val = BF1_PAXCA | BF2_1PAXCA;
  1130. if (!fb->format->has_alpha)
  1131. val = BF1_CA | BF2_1CA;
  1132. /* Manage hw-specific capabilities */
  1133. if (ldev->caps.non_alpha_only_l1 &&
  1134. plane->type != DRM_PLANE_TYPE_PRIMARY)
  1135. val = BF1_PAXCA | BF2_1PAXCA;
  1136. if (ldev->caps.dynamic_zorder) {
  1137. val |= (newstate->normalized_zpos << 16);
  1138. regmap_write_bits(ldev->regmap, LTDC_L1BFCR + lofs,
  1139. LXBFCR_BF2 | LXBFCR_BF1 | LXBFCR_BOR, val);
  1140. } else {
  1141. regmap_write_bits(ldev->regmap, LTDC_L1BFCR + lofs,
  1142. LXBFCR_BF2 | LXBFCR_BF1, val);
  1143. }
  1144. /* Sets the FB address */
  1145. paddr = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 0);
  1146. if (newstate->rotation & DRM_MODE_REFLECT_X)
  1147. paddr += (fb->format->cpp[0] * (x1 - x0 + 1)) - 1;
  1148. if (newstate->rotation & DRM_MODE_REFLECT_Y)
  1149. paddr += (fb->pitches[0] * (y1 - y0));
  1150. DRM_DEBUG_DRIVER("fb: phys 0x%08x", paddr);
  1151. regmap_write(ldev->regmap, LTDC_L1CFBAR + lofs, paddr);
  1152. /* Configures the color frame buffer pitch in bytes & line length */
  1153. line_length = fb->format->cpp[0] *
  1154. (x1 - x0 + 1) + (ldev->caps.bus_width >> 3) - 1;
  1155. if (newstate->rotation & DRM_MODE_REFLECT_Y)
  1156. /* Compute negative value (signed on 16 bits) for the picth */
  1157. pitch_in_bytes = 0x10000 - fb->pitches[0];
  1158. else
  1159. pitch_in_bytes = fb->pitches[0];
  1160. val = (pitch_in_bytes << 16) | line_length;
  1161. regmap_write_bits(ldev->regmap, LTDC_L1CFBLR + lofs, LXCFBLR_CFBLL | LXCFBLR_CFBP, val);
  1162. /* Configures the frame buffer line number */
  1163. line_number = y1 - y0 + 1;
  1164. regmap_write_bits(ldev->regmap, LTDC_L1CFBLNR + lofs, LXCFBLNR_CFBLN, line_number);
  1165. if (ldev->caps.ycbcr_input) {
  1166. if (fb->format->is_yuv) {
  1167. switch (fb->format->format) {
  1168. case DRM_FORMAT_NV12:
  1169. case DRM_FORMAT_NV21:
  1170. /* Configure the auxiliary frame buffer address 0 */
  1171. paddr1 = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 1);
  1172. if (newstate->rotation & DRM_MODE_REFLECT_X)
  1173. paddr1 += ((fb->format->cpp[1] * (x1 - x0 + 1)) >> 1) - 1;
  1174. if (newstate->rotation & DRM_MODE_REFLECT_Y)
  1175. paddr1 += (fb->pitches[1] * (y1 - y0 - 1)) >> 1;
  1176. regmap_write(ldev->regmap, LTDC_L1AFBA0R + lofs, paddr1);
  1177. break;
  1178. case DRM_FORMAT_YUV420:
  1179. /* Configure the auxiliary frame buffer address 0 & 1 */
  1180. paddr1 = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 1);
  1181. paddr2 = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 2);
  1182. if (newstate->rotation & DRM_MODE_REFLECT_X) {
  1183. paddr1 += ((fb->format->cpp[1] * (x1 - x0 + 1)) >> 1) - 1;
  1184. paddr2 += ((fb->format->cpp[2] * (x1 - x0 + 1)) >> 1) - 1;
  1185. }
  1186. if (newstate->rotation & DRM_MODE_REFLECT_Y) {
  1187. paddr1 += (fb->pitches[1] * (y1 - y0 - 1)) >> 1;
  1188. paddr2 += (fb->pitches[2] * (y1 - y0 - 1)) >> 1;
  1189. }
  1190. regmap_write(ldev->regmap, LTDC_L1AFBA0R + lofs, paddr1);
  1191. regmap_write(ldev->regmap, LTDC_L1AFBA1R + lofs, paddr2);
  1192. break;
  1193. case DRM_FORMAT_YVU420:
  1194. /* Configure the auxiliary frame buffer address 0 & 1 */
  1195. paddr1 = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 2);
  1196. paddr2 = (u32)drm_fb_dma_get_gem_addr(fb, newstate, 1);
  1197. if (newstate->rotation & DRM_MODE_REFLECT_X) {
  1198. paddr1 += ((fb->format->cpp[1] * (x1 - x0 + 1)) >> 1) - 1;
  1199. paddr2 += ((fb->format->cpp[2] * (x1 - x0 + 1)) >> 1) - 1;
  1200. }
  1201. if (newstate->rotation & DRM_MODE_REFLECT_Y) {
  1202. paddr1 += (fb->pitches[1] * (y1 - y0 - 1)) >> 1;
  1203. paddr2 += (fb->pitches[2] * (y1 - y0 - 1)) >> 1;
  1204. }
  1205. regmap_write(ldev->regmap, LTDC_L1AFBA0R + lofs, paddr1);
  1206. regmap_write(ldev->regmap, LTDC_L1AFBA1R + lofs, paddr2);
  1207. break;
  1208. }
  1209. /*
  1210. * Set the length and the number of lines of the auxiliary
  1211. * buffers if the framebuffer contains more than one plane.
  1212. */
  1213. if (fb->format->num_planes > 1) {
  1214. if (newstate->rotation & DRM_MODE_REFLECT_Y)
  1215. /*
  1216. * Compute negative value (signed on 16 bits)
  1217. * for the picth
  1218. */
  1219. pitch_in_bytes = 0x10000 - fb->pitches[1];
  1220. else
  1221. pitch_in_bytes = fb->pitches[1];
  1222. line_length = ((fb->format->cpp[1] * (x1 - x0 + 1)) >> 1) +
  1223. (ldev->caps.bus_width >> 3) - 1;
  1224. /* Configure the auxiliary buffer length */
  1225. val = (pitch_in_bytes << 16) | line_length;
  1226. regmap_write(ldev->regmap, LTDC_L1AFBLR + lofs, val);
  1227. /* Configure the auxiliary frame buffer line number */
  1228. val = line_number >> 1;
  1229. regmap_write(ldev->regmap, LTDC_L1AFBLNR + lofs, val);
  1230. }
  1231. /* Configure YCbC conversion coefficient */
  1232. ltdc_set_ycbcr_coeffs(plane);
  1233. /* Configure YCbCr format and enable/disable conversion */
  1234. ltdc_set_ycbcr_config(plane, fb->format->format);
  1235. } else {
  1236. /* disable ycbcr conversion */
  1237. regmap_write(ldev->regmap, LTDC_L1PCR + lofs, 0);
  1238. }
  1239. }
  1240. /* Enable layer and CLUT if needed */
  1241. val = fb->format->format == DRM_FORMAT_C8 ? LXCR_CLUTEN : 0;
  1242. val |= LXCR_LEN;
  1243. /* Enable horizontal mirroring if requested */
  1244. if (newstate->rotation & DRM_MODE_REFLECT_X)
  1245. val |= LXCR_HMEN;
  1246. regmap_write_bits(ldev->regmap, LTDC_L1CR + lofs, LXCR_MASK, val);
  1247. /* Commit shadow registers = update plane at next vblank */
  1248. if (ldev->caps.plane_reg_shadow)
  1249. regmap_write_bits(ldev->regmap, LTDC_L1RCR + lofs,
  1250. LXRCR_IMR | LXRCR_VBR | LXRCR_GRMSK, LXRCR_VBR);
  1251. ldev->plane_fpsi[plane->index].counter++;
  1252. mutex_lock(&ldev->err_lock);
  1253. if (ldev->transfer_err) {
  1254. DRM_WARN("ltdc transfer error: %d\n", ldev->transfer_err);
  1255. ldev->transfer_err = 0;
  1256. }
  1257. if (ldev->caps.fifo_threshold) {
  1258. if (ldev->fifo_err) {
  1259. DRM_WARN("ltdc fifo underrun: please verify display mode\n");
  1260. ldev->fifo_err = 0;
  1261. }
  1262. } else {
  1263. if (ldev->fifo_warn >= ldev->fifo_threshold) {
  1264. DRM_WARN("ltdc fifo underrun: please verify display mode\n");
  1265. ldev->fifo_warn = 0;
  1266. }
  1267. }
  1268. mutex_unlock(&ldev->err_lock);
  1269. }
  1270. static void ltdc_plane_atomic_disable(struct drm_plane *plane,
  1271. struct drm_atomic_state *state)
  1272. {
  1273. struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state,
  1274. plane);
  1275. struct ltdc_device *ldev = plane_to_ltdc(plane);
  1276. u32 lofs = plane->index * LAY_OFS;
  1277. /* Disable layer */
  1278. regmap_write_bits(ldev->regmap, LTDC_L1CR + lofs, LXCR_MASK, 0);
  1279. /* Reset the layer transparency to hide any related background color */
  1280. regmap_write_bits(ldev->regmap, LTDC_L1CACR + lofs, LXCACR_CONSTA, 0x00);
  1281. /* Commit shadow registers = update plane at next vblank */
  1282. if (ldev->caps.plane_reg_shadow)
  1283. regmap_write_bits(ldev->regmap, LTDC_L1RCR + lofs,
  1284. LXRCR_IMR | LXRCR_VBR | LXRCR_GRMSK, LXRCR_VBR);
  1285. DRM_DEBUG_DRIVER("CRTC:%d plane:%d\n",
  1286. oldstate->crtc->base.id, plane->base.id);
  1287. }
  1288. static void ltdc_plane_atomic_print_state(struct drm_printer *p,
  1289. const struct drm_plane_state *state)
  1290. {
  1291. struct drm_plane *plane = state->plane;
  1292. struct ltdc_device *ldev = plane_to_ltdc(plane);
  1293. struct fps_info *fpsi = &ldev->plane_fpsi[plane->index];
  1294. int ms_since_last;
  1295. ktime_t now;
  1296. now = ktime_get();
  1297. ms_since_last = ktime_to_ms(ktime_sub(now, fpsi->last_timestamp));
  1298. drm_printf(p, "\tuser_updates=%dfps\n",
  1299. DIV_ROUND_CLOSEST(fpsi->counter * 1000, ms_since_last));
  1300. fpsi->last_timestamp = now;
  1301. fpsi->counter = 0;
  1302. }
  1303. static const struct drm_plane_funcs ltdc_plane_funcs = {
  1304. .update_plane = drm_atomic_helper_update_plane,
  1305. .disable_plane = drm_atomic_helper_disable_plane,
  1306. .reset = drm_atomic_helper_plane_reset,
  1307. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  1308. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  1309. .atomic_print_state = ltdc_plane_atomic_print_state,
  1310. };
  1311. static const struct drm_plane_helper_funcs ltdc_plane_helper_funcs = {
  1312. .atomic_check = ltdc_plane_atomic_check,
  1313. .atomic_update = ltdc_plane_atomic_update,
  1314. .atomic_disable = ltdc_plane_atomic_disable,
  1315. };
  1316. static struct drm_plane *ltdc_plane_create(struct drm_device *ddev,
  1317. enum drm_plane_type type,
  1318. int index)
  1319. {
  1320. unsigned long possible_crtcs = CRTC_MASK;
  1321. struct ltdc_device *ldev = ddev->dev_private;
  1322. struct device *dev = ddev->dev;
  1323. struct drm_plane *plane;
  1324. unsigned int i, nb_fmt = 0;
  1325. u32 *formats;
  1326. u32 drm_fmt;
  1327. const u64 *modifiers = ltdc_format_modifiers;
  1328. u32 lofs = index * LAY_OFS;
  1329. u32 val;
  1330. /* Allocate the biggest size according to supported color formats */
  1331. formats = devm_kzalloc(dev, (ldev->caps.pix_fmt_nb +
  1332. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_cp) +
  1333. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_sp) +
  1334. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_fp)) *
  1335. sizeof(*formats), GFP_KERNEL);
  1336. if (!formats)
  1337. return NULL;
  1338. for (i = 0; i < ldev->caps.pix_fmt_nb; i++) {
  1339. drm_fmt = ldev->caps.pix_fmt_drm[i];
  1340. /* Manage hw-specific capabilities */
  1341. if (ldev->caps.non_alpha_only_l1)
  1342. /* XR24 & RX24 like formats supported only on primary layer */
  1343. if (type != DRM_PLANE_TYPE_PRIMARY && is_xrgb(drm_fmt))
  1344. continue;
  1345. formats[nb_fmt++] = drm_fmt;
  1346. }
  1347. /* Add YCbCr supported pixel formats */
  1348. if (ldev->caps.ycbcr_input) {
  1349. regmap_read(ldev->regmap, LTDC_L1C1R + lofs, &val);
  1350. if (val & LXCR_C1R_YIA) {
  1351. memcpy(&formats[nb_fmt], ltdc_drm_fmt_ycbcr_cp,
  1352. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_cp) * sizeof(*formats));
  1353. nb_fmt += ARRAY_SIZE(ltdc_drm_fmt_ycbcr_cp);
  1354. }
  1355. if (val & LXCR_C1R_YSPA) {
  1356. memcpy(&formats[nb_fmt], ltdc_drm_fmt_ycbcr_sp,
  1357. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_sp) * sizeof(*formats));
  1358. nb_fmt += ARRAY_SIZE(ltdc_drm_fmt_ycbcr_sp);
  1359. }
  1360. if (val & LXCR_C1R_YFPA) {
  1361. memcpy(&formats[nb_fmt], ltdc_drm_fmt_ycbcr_fp,
  1362. ARRAY_SIZE(ltdc_drm_fmt_ycbcr_fp) * sizeof(*formats));
  1363. nb_fmt += ARRAY_SIZE(ltdc_drm_fmt_ycbcr_fp);
  1364. }
  1365. }
  1366. plane = drmm_universal_plane_alloc(ddev, struct drm_plane, dev,
  1367. possible_crtcs, &ltdc_plane_funcs, formats,
  1368. nb_fmt, modifiers, type, NULL);
  1369. if (IS_ERR(plane))
  1370. return NULL;
  1371. if (ldev->caps.ycbcr_input) {
  1372. if (val & (LXCR_C1R_YIA | LXCR_C1R_YSPA | LXCR_C1R_YFPA))
  1373. drm_plane_create_color_properties(plane,
  1374. BIT(DRM_COLOR_YCBCR_BT601) |
  1375. BIT(DRM_COLOR_YCBCR_BT709),
  1376. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) |
  1377. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  1378. DRM_COLOR_YCBCR_BT601,
  1379. DRM_COLOR_YCBCR_LIMITED_RANGE);
  1380. }
  1381. drm_plane_helper_add(plane, &ltdc_plane_helper_funcs);
  1382. drm_plane_create_alpha_property(plane);
  1383. DRM_DEBUG_DRIVER("plane:%d created\n", plane->base.id);
  1384. return plane;
  1385. }
  1386. static int ltdc_crtc_init(struct drm_device *ddev, struct drm_crtc *crtc)
  1387. {
  1388. struct ltdc_device *ldev = ddev->dev_private;
  1389. struct drm_plane *primary, *overlay;
  1390. int supported_rotations = DRM_MODE_ROTATE_0 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  1391. unsigned int i;
  1392. int ret;
  1393. primary = ltdc_plane_create(ddev, DRM_PLANE_TYPE_PRIMARY, 0);
  1394. if (!primary) {
  1395. DRM_ERROR("Can not create primary plane\n");
  1396. return -EINVAL;
  1397. }
  1398. if (ldev->caps.dynamic_zorder)
  1399. drm_plane_create_zpos_property(primary, 0, 0, ldev->caps.nb_layers - 1);
  1400. else
  1401. drm_plane_create_zpos_immutable_property(primary, 0);
  1402. if (ldev->caps.plane_rotation)
  1403. drm_plane_create_rotation_property(primary, DRM_MODE_ROTATE_0,
  1404. supported_rotations);
  1405. /* Init CRTC according to its hardware features */
  1406. if (ldev->caps.crc)
  1407. ret = drmm_crtc_init_with_planes(ddev, crtc, primary, NULL,
  1408. &ltdc_crtc_with_crc_support_funcs, NULL);
  1409. else
  1410. ret = drmm_crtc_init_with_planes(ddev, crtc, primary, NULL,
  1411. &ltdc_crtc_funcs, NULL);
  1412. if (ret) {
  1413. DRM_ERROR("Can not initialize CRTC\n");
  1414. return ret;
  1415. }
  1416. drm_crtc_helper_add(crtc, &ltdc_crtc_helper_funcs);
  1417. drm_mode_crtc_set_gamma_size(crtc, CLUT_SIZE);
  1418. drm_crtc_enable_color_mgmt(crtc, 0, false, CLUT_SIZE);
  1419. DRM_DEBUG_DRIVER("CRTC:%d created\n", crtc->base.id);
  1420. /* Add planes. Note : the first layer is used by primary plane */
  1421. for (i = 1; i < ldev->caps.nb_layers; i++) {
  1422. overlay = ltdc_plane_create(ddev, DRM_PLANE_TYPE_OVERLAY, i);
  1423. if (!overlay) {
  1424. DRM_ERROR("Can not create overlay plane %d\n", i);
  1425. return -ENOMEM;
  1426. }
  1427. if (ldev->caps.dynamic_zorder)
  1428. drm_plane_create_zpos_property(overlay, i, 0, ldev->caps.nb_layers - 1);
  1429. else
  1430. drm_plane_create_zpos_immutable_property(overlay, i);
  1431. if (ldev->caps.plane_rotation)
  1432. drm_plane_create_rotation_property(overlay, DRM_MODE_ROTATE_0,
  1433. supported_rotations);
  1434. }
  1435. return 0;
  1436. }
  1437. static void ltdc_encoder_disable(struct drm_encoder *encoder)
  1438. {
  1439. struct drm_device *ddev = encoder->dev;
  1440. struct ltdc_device *ldev = ddev->dev_private;
  1441. DRM_DEBUG_DRIVER("\n");
  1442. /* Disable LTDC */
  1443. regmap_clear_bits(ldev->regmap, LTDC_GCR, GCR_LTDCEN);
  1444. /* Set to sleep state the pinctrl whatever type of encoder */
  1445. pinctrl_pm_select_sleep_state(ddev->dev);
  1446. }
  1447. static void ltdc_encoder_enable(struct drm_encoder *encoder)
  1448. {
  1449. struct drm_device *ddev = encoder->dev;
  1450. struct ltdc_device *ldev = ddev->dev_private;
  1451. DRM_DEBUG_DRIVER("\n");
  1452. /* set fifo underrun threshold register */
  1453. if (ldev->caps.fifo_threshold)
  1454. regmap_write(ldev->regmap, LTDC_FUT, ldev->fifo_threshold);
  1455. /* Enable LTDC */
  1456. regmap_set_bits(ldev->regmap, LTDC_GCR, GCR_LTDCEN);
  1457. }
  1458. static void ltdc_encoder_mode_set(struct drm_encoder *encoder,
  1459. struct drm_display_mode *mode,
  1460. struct drm_display_mode *adjusted_mode)
  1461. {
  1462. struct drm_device *ddev = encoder->dev;
  1463. DRM_DEBUG_DRIVER("\n");
  1464. /*
  1465. * Set to default state the pinctrl only with DPI type.
  1466. * Others types like DSI, don't need pinctrl due to
  1467. * internal bridge (the signals do not come out of the chipset).
  1468. */
  1469. if (encoder->encoder_type == DRM_MODE_ENCODER_DPI)
  1470. pinctrl_pm_select_default_state(ddev->dev);
  1471. }
  1472. static const struct drm_encoder_helper_funcs ltdc_encoder_helper_funcs = {
  1473. .disable = ltdc_encoder_disable,
  1474. .enable = ltdc_encoder_enable,
  1475. .mode_set = ltdc_encoder_mode_set,
  1476. };
  1477. static int ltdc_encoder_init(struct drm_device *ddev, struct drm_bridge *bridge)
  1478. {
  1479. struct drm_encoder *encoder;
  1480. int ret;
  1481. encoder = drmm_simple_encoder_alloc(ddev, struct drm_encoder, dev,
  1482. DRM_MODE_ENCODER_DPI);
  1483. if (IS_ERR(encoder))
  1484. return PTR_ERR(encoder);
  1485. encoder->possible_crtcs = CRTC_MASK;
  1486. encoder->possible_clones = 0; /* No cloning support */
  1487. drm_encoder_helper_add(encoder, &ltdc_encoder_helper_funcs);
  1488. ret = drm_bridge_attach(encoder, bridge, NULL, 0);
  1489. if (ret)
  1490. return ret;
  1491. DRM_DEBUG_DRIVER("Bridge encoder:%d created\n", encoder->base.id);
  1492. return 0;
  1493. }
  1494. static int ltdc_get_caps(struct drm_device *ddev)
  1495. {
  1496. struct ltdc_device *ldev = ddev->dev_private;
  1497. u32 bus_width_log2, lcr, gc2r;
  1498. /*
  1499. * at least 1 layer must be managed & the number of layers
  1500. * must not exceed LTDC_MAX_LAYER
  1501. */
  1502. regmap_read(ldev->regmap, LTDC_LCR, &lcr);
  1503. ldev->caps.nb_layers = clamp((int)lcr, 1, LTDC_MAX_LAYER);
  1504. /* set data bus width */
  1505. regmap_read(ldev->regmap, LTDC_GC2R, &gc2r);
  1506. bus_width_log2 = (gc2r & GC2R_BW) >> 4;
  1507. ldev->caps.bus_width = 8 << bus_width_log2;
  1508. regmap_read(ldev->regmap, LTDC_IDR, &ldev->caps.hw_version);
  1509. switch (ldev->caps.hw_version) {
  1510. case HWVER_10200:
  1511. case HWVER_10300:
  1512. ldev->caps.layer_ofs = LAY_OFS_0;
  1513. ldev->caps.layer_regs = ltdc_layer_regs_a0;
  1514. ldev->caps.pix_fmt_hw = ltdc_pix_fmt_a0;
  1515. ldev->caps.pix_fmt_drm = ltdc_drm_fmt_a0;
  1516. ldev->caps.pix_fmt_nb = ARRAY_SIZE(ltdc_drm_fmt_a0);
  1517. ldev->caps.pix_fmt_flex = false;
  1518. /*
  1519. * Hw older versions support non-alpha color formats derived
  1520. * from native alpha color formats only on the primary layer.
  1521. * For instance, RG16 native format without alpha works fine
  1522. * on 2nd layer but XR24 (derived color format from AR24)
  1523. * does not work on 2nd layer.
  1524. */
  1525. ldev->caps.non_alpha_only_l1 = true;
  1526. ldev->caps.pad_max_freq_hz = 90000000;
  1527. if (ldev->caps.hw_version == HWVER_10200)
  1528. ldev->caps.pad_max_freq_hz = 65000000;
  1529. ldev->caps.nb_irq = 2;
  1530. ldev->caps.ycbcr_input = false;
  1531. ldev->caps.ycbcr_output = false;
  1532. ldev->caps.plane_reg_shadow = false;
  1533. ldev->caps.crc = false;
  1534. ldev->caps.dynamic_zorder = false;
  1535. ldev->caps.plane_rotation = false;
  1536. ldev->caps.fifo_threshold = false;
  1537. break;
  1538. case HWVER_20101:
  1539. ldev->caps.layer_ofs = LAY_OFS_0;
  1540. ldev->caps.layer_regs = ltdc_layer_regs_a1;
  1541. ldev->caps.pix_fmt_hw = ltdc_pix_fmt_a1;
  1542. ldev->caps.pix_fmt_drm = ltdc_drm_fmt_a1;
  1543. ldev->caps.pix_fmt_nb = ARRAY_SIZE(ltdc_drm_fmt_a1);
  1544. ldev->caps.pix_fmt_flex = false;
  1545. ldev->caps.non_alpha_only_l1 = false;
  1546. ldev->caps.pad_max_freq_hz = 150000000;
  1547. ldev->caps.nb_irq = 4;
  1548. ldev->caps.ycbcr_input = false;
  1549. ldev->caps.ycbcr_output = false;
  1550. ldev->caps.plane_reg_shadow = false;
  1551. ldev->caps.crc = false;
  1552. ldev->caps.dynamic_zorder = false;
  1553. ldev->caps.plane_rotation = false;
  1554. ldev->caps.fifo_threshold = false;
  1555. break;
  1556. case HWVER_40100:
  1557. ldev->caps.layer_ofs = LAY_OFS_1;
  1558. ldev->caps.layer_regs = ltdc_layer_regs_a2;
  1559. ldev->caps.pix_fmt_hw = ltdc_pix_fmt_a2;
  1560. ldev->caps.pix_fmt_drm = ltdc_drm_fmt_a2;
  1561. ldev->caps.pix_fmt_nb = ARRAY_SIZE(ltdc_drm_fmt_a2);
  1562. ldev->caps.pix_fmt_flex = true;
  1563. ldev->caps.non_alpha_only_l1 = false;
  1564. ldev->caps.pad_max_freq_hz = 90000000;
  1565. ldev->caps.nb_irq = 2;
  1566. ldev->caps.ycbcr_input = true;
  1567. ldev->caps.ycbcr_output = true;
  1568. ldev->caps.plane_reg_shadow = true;
  1569. ldev->caps.crc = true;
  1570. ldev->caps.dynamic_zorder = true;
  1571. ldev->caps.plane_rotation = true;
  1572. ldev->caps.fifo_threshold = true;
  1573. break;
  1574. default:
  1575. return -ENODEV;
  1576. }
  1577. return 0;
  1578. }
  1579. void ltdc_suspend(struct drm_device *ddev)
  1580. {
  1581. struct ltdc_device *ldev = ddev->dev_private;
  1582. DRM_DEBUG_DRIVER("\n");
  1583. clk_disable_unprepare(ldev->pixel_clk);
  1584. }
  1585. int ltdc_resume(struct drm_device *ddev)
  1586. {
  1587. struct ltdc_device *ldev = ddev->dev_private;
  1588. int ret;
  1589. DRM_DEBUG_DRIVER("\n");
  1590. ret = clk_prepare_enable(ldev->pixel_clk);
  1591. if (ret) {
  1592. DRM_ERROR("failed to enable pixel clock (%d)\n", ret);
  1593. return ret;
  1594. }
  1595. return 0;
  1596. }
  1597. int ltdc_load(struct drm_device *ddev)
  1598. {
  1599. struct platform_device *pdev = to_platform_device(ddev->dev);
  1600. struct ltdc_device *ldev = ddev->dev_private;
  1601. struct device *dev = ddev->dev;
  1602. struct device_node *np = dev->of_node;
  1603. struct drm_bridge *bridge;
  1604. struct drm_panel *panel;
  1605. struct drm_crtc *crtc;
  1606. struct reset_control *rstc;
  1607. struct resource *res;
  1608. int irq, i, nb_endpoints;
  1609. int ret = -ENODEV;
  1610. DRM_DEBUG_DRIVER("\n");
  1611. /* Get number of endpoints */
  1612. nb_endpoints = of_graph_get_endpoint_count(np);
  1613. if (!nb_endpoints)
  1614. return -ENODEV;
  1615. ldev->pixel_clk = devm_clk_get(dev, "lcd");
  1616. if (IS_ERR(ldev->pixel_clk)) {
  1617. if (PTR_ERR(ldev->pixel_clk) != -EPROBE_DEFER)
  1618. DRM_ERROR("Unable to get lcd clock\n");
  1619. return PTR_ERR(ldev->pixel_clk);
  1620. }
  1621. if (clk_prepare_enable(ldev->pixel_clk)) {
  1622. DRM_ERROR("Unable to prepare pixel clock\n");
  1623. return -ENODEV;
  1624. }
  1625. /* Get endpoints if any */
  1626. for (i = 0; i < nb_endpoints; i++) {
  1627. ret = drm_of_find_panel_or_bridge(np, 0, i, &panel, &bridge);
  1628. /*
  1629. * If at least one endpoint is -ENODEV, continue probing,
  1630. * else if at least one endpoint returned an error
  1631. * (ie -EPROBE_DEFER) then stop probing.
  1632. */
  1633. if (ret == -ENODEV)
  1634. continue;
  1635. else if (ret)
  1636. goto err;
  1637. if (panel) {
  1638. bridge = drmm_panel_bridge_add(ddev, panel);
  1639. if (IS_ERR(bridge)) {
  1640. DRM_ERROR("panel-bridge endpoint %d\n", i);
  1641. ret = PTR_ERR(bridge);
  1642. goto err;
  1643. }
  1644. }
  1645. if (bridge) {
  1646. ret = ltdc_encoder_init(ddev, bridge);
  1647. if (ret) {
  1648. if (ret != -EPROBE_DEFER)
  1649. DRM_ERROR("init encoder endpoint %d\n", i);
  1650. goto err;
  1651. }
  1652. }
  1653. }
  1654. rstc = devm_reset_control_get_exclusive(dev, NULL);
  1655. mutex_init(&ldev->err_lock);
  1656. if (!IS_ERR(rstc)) {
  1657. reset_control_assert(rstc);
  1658. usleep_range(10, 20);
  1659. reset_control_deassert(rstc);
  1660. }
  1661. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1662. ldev->regs = devm_ioremap_resource(dev, res);
  1663. if (IS_ERR(ldev->regs)) {
  1664. DRM_ERROR("Unable to get ltdc registers\n");
  1665. ret = PTR_ERR(ldev->regs);
  1666. goto err;
  1667. }
  1668. ldev->regmap = devm_regmap_init_mmio(&pdev->dev, ldev->regs, &stm32_ltdc_regmap_cfg);
  1669. if (IS_ERR(ldev->regmap)) {
  1670. DRM_ERROR("Unable to regmap ltdc registers\n");
  1671. ret = PTR_ERR(ldev->regmap);
  1672. goto err;
  1673. }
  1674. ret = ltdc_get_caps(ddev);
  1675. if (ret) {
  1676. DRM_ERROR("hardware identifier (0x%08x) not supported!\n",
  1677. ldev->caps.hw_version);
  1678. goto err;
  1679. }
  1680. /* Disable all interrupts */
  1681. regmap_clear_bits(ldev->regmap, LTDC_IER, IER_MASK);
  1682. DRM_DEBUG_DRIVER("ltdc hw version 0x%08x\n", ldev->caps.hw_version);
  1683. /* initialize default value for fifo underrun threshold & clear interrupt error counters */
  1684. ldev->transfer_err = 0;
  1685. ldev->fifo_err = 0;
  1686. ldev->fifo_warn = 0;
  1687. ldev->fifo_threshold = FUT_DFT;
  1688. for (i = 0; i < ldev->caps.nb_irq; i++) {
  1689. irq = platform_get_irq(pdev, i);
  1690. if (irq < 0) {
  1691. ret = irq;
  1692. goto err;
  1693. }
  1694. ret = devm_request_threaded_irq(dev, irq, ltdc_irq,
  1695. ltdc_irq_thread, IRQF_ONESHOT,
  1696. dev_name(dev), ddev);
  1697. if (ret) {
  1698. DRM_ERROR("Failed to register LTDC interrupt\n");
  1699. goto err;
  1700. }
  1701. }
  1702. crtc = drmm_kzalloc(ddev, sizeof(*crtc), GFP_KERNEL);
  1703. if (!crtc) {
  1704. DRM_ERROR("Failed to allocate crtc\n");
  1705. ret = -ENOMEM;
  1706. goto err;
  1707. }
  1708. ret = ltdc_crtc_init(ddev, crtc);
  1709. if (ret) {
  1710. DRM_ERROR("Failed to init crtc\n");
  1711. goto err;
  1712. }
  1713. ret = drm_vblank_init(ddev, NB_CRTC);
  1714. if (ret) {
  1715. DRM_ERROR("Failed calling drm_vblank_init()\n");
  1716. goto err;
  1717. }
  1718. clk_disable_unprepare(ldev->pixel_clk);
  1719. pinctrl_pm_select_sleep_state(ddev->dev);
  1720. pm_runtime_enable(ddev->dev);
  1721. return 0;
  1722. err:
  1723. clk_disable_unprepare(ldev->pixel_clk);
  1724. return ret;
  1725. }
  1726. void ltdc_unload(struct drm_device *ddev)
  1727. {
  1728. DRM_DEBUG_DRIVER("\n");
  1729. pm_runtime_disable(ddev->dev);
  1730. }
  1731. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  1732. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  1733. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  1734. MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
  1735. MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
  1736. MODULE_LICENSE("GPL v2");