ltdc.c 63 KB

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