ltdc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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/of_address.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/reset.h>
  15. #include <drm/drm_atomic.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_of.h>
  21. #include <drm/drm_bridge.h>
  22. #include <drm/drm_plane_helper.h>
  23. #include <video/videomode.h>
  24. #include "ltdc.h"
  25. #define NB_CRTC 1
  26. #define CRTC_MASK GENMASK(NB_CRTC - 1, 0)
  27. #define MAX_IRQ 4
  28. #define MAX_ENDPOINTS 2
  29. #define HWVER_10200 0x010200
  30. #define HWVER_10300 0x010300
  31. #define HWVER_20101 0x020101
  32. /*
  33. * The address of some registers depends on the HW version: such registers have
  34. * an extra offset specified with reg_ofs.
  35. */
  36. #define REG_OFS_NONE 0
  37. #define REG_OFS_4 4 /* Insertion of "Layer Conf. 2" reg */
  38. #define REG_OFS (ldev->caps.reg_ofs)
  39. #define LAY_OFS 0x80 /* Register Offset between 2 layers */
  40. /* Global register offsets */
  41. #define LTDC_IDR 0x0000 /* IDentification */
  42. #define LTDC_LCR 0x0004 /* Layer Count */
  43. #define LTDC_SSCR 0x0008 /* Synchronization Size Configuration */
  44. #define LTDC_BPCR 0x000C /* Back Porch Configuration */
  45. #define LTDC_AWCR 0x0010 /* Active Width Configuration */
  46. #define LTDC_TWCR 0x0014 /* Total Width Configuration */
  47. #define LTDC_GCR 0x0018 /* Global Control */
  48. #define LTDC_GC1R 0x001C /* Global Configuration 1 */
  49. #define LTDC_GC2R 0x0020 /* Global Configuration 2 */
  50. #define LTDC_SRCR 0x0024 /* Shadow Reload Configuration */
  51. #define LTDC_GACR 0x0028 /* GAmma Correction */
  52. #define LTDC_BCCR 0x002C /* Background Color Configuration */
  53. #define LTDC_IER 0x0034 /* Interrupt Enable */
  54. #define LTDC_ISR 0x0038 /* Interrupt Status */
  55. #define LTDC_ICR 0x003C /* Interrupt Clear */
  56. #define LTDC_LIPCR 0x0040 /* Line Interrupt Position Conf. */
  57. #define LTDC_CPSR 0x0044 /* Current Position Status */
  58. #define LTDC_CDSR 0x0048 /* Current Display Status */
  59. /* Layer register offsets */
  60. #define LTDC_L1LC1R (0x80) /* L1 Layer Configuration 1 */
  61. #define LTDC_L1LC2R (0x84) /* L1 Layer Configuration 2 */
  62. #define LTDC_L1CR (0x84 + REG_OFS)/* L1 Control */
  63. #define LTDC_L1WHPCR (0x88 + REG_OFS)/* L1 Window Hor Position Config */
  64. #define LTDC_L1WVPCR (0x8C + REG_OFS)/* L1 Window Vert Position Config */
  65. #define LTDC_L1CKCR (0x90 + REG_OFS)/* L1 Color Keying Configuration */
  66. #define LTDC_L1PFCR (0x94 + REG_OFS)/* L1 Pixel Format Configuration */
  67. #define LTDC_L1CACR (0x98 + REG_OFS)/* L1 Constant Alpha Config */
  68. #define LTDC_L1DCCR (0x9C + REG_OFS)/* L1 Default Color Configuration */
  69. #define LTDC_L1BFCR (0xA0 + REG_OFS)/* L1 Blend Factors Configuration */
  70. #define LTDC_L1FBBCR (0xA4 + REG_OFS)/* L1 FrameBuffer Bus Control */
  71. #define LTDC_L1AFBCR (0xA8 + REG_OFS)/* L1 AuxFB Control */
  72. #define LTDC_L1CFBAR (0xAC + REG_OFS)/* L1 Color FrameBuffer Address */
  73. #define LTDC_L1CFBLR (0xB0 + REG_OFS)/* L1 Color FrameBuffer Length */
  74. #define LTDC_L1CFBLNR (0xB4 + REG_OFS)/* L1 Color FrameBuffer Line Nb */
  75. #define LTDC_L1AFBAR (0xB8 + REG_OFS)/* L1 AuxFB Address */
  76. #define LTDC_L1AFBLR (0xBC + REG_OFS)/* L1 AuxFB Length */
  77. #define LTDC_L1AFBLNR (0xC0 + REG_OFS)/* L1 AuxFB Line Number */
  78. #define LTDC_L1CLUTWR (0xC4 + REG_OFS)/* L1 CLUT Write */
  79. #define LTDC_L1YS1R (0xE0 + REG_OFS)/* L1 YCbCr Scale 1 */
  80. #define LTDC_L1YS2R (0xE4 + REG_OFS)/* L1 YCbCr Scale 2 */
  81. /* Bit definitions */
  82. #define SSCR_VSH GENMASK(10, 0) /* Vertical Synchronization Height */
  83. #define SSCR_HSW GENMASK(27, 16) /* Horizontal Synchronization Width */
  84. #define BPCR_AVBP GENMASK(10, 0) /* Accumulated Vertical Back Porch */
  85. #define BPCR_AHBP GENMASK(27, 16) /* Accumulated Horizontal Back Porch */
  86. #define AWCR_AAH GENMASK(10, 0) /* Accumulated Active Height */
  87. #define AWCR_AAW GENMASK(27, 16) /* Accumulated Active Width */
  88. #define TWCR_TOTALH GENMASK(10, 0) /* TOTAL Height */
  89. #define TWCR_TOTALW GENMASK(27, 16) /* TOTAL Width */
  90. #define GCR_LTDCEN BIT(0) /* LTDC ENable */
  91. #define GCR_DEN BIT(16) /* Dither ENable */
  92. #define GCR_PCPOL BIT(28) /* Pixel Clock POLarity-Inverted */
  93. #define GCR_DEPOL BIT(29) /* Data Enable POLarity-High */
  94. #define GCR_VSPOL BIT(30) /* Vertical Synchro POLarity-High */
  95. #define GCR_HSPOL BIT(31) /* Horizontal Synchro POLarity-High */
  96. #define GC1R_WBCH GENMASK(3, 0) /* Width of Blue CHannel output */
  97. #define GC1R_WGCH GENMASK(7, 4) /* Width of Green Channel output */
  98. #define GC1R_WRCH GENMASK(11, 8) /* Width of Red Channel output */
  99. #define GC1R_PBEN BIT(12) /* Precise Blending ENable */
  100. #define GC1R_DT GENMASK(15, 14) /* Dithering Technique */
  101. #define GC1R_GCT GENMASK(19, 17) /* Gamma Correction Technique */
  102. #define GC1R_SHREN BIT(21) /* SHadow Registers ENabled */
  103. #define GC1R_BCP BIT(22) /* Background Colour Programmable */
  104. #define GC1R_BBEN BIT(23) /* Background Blending ENabled */
  105. #define GC1R_LNIP BIT(24) /* Line Number IRQ Position */
  106. #define GC1R_TP BIT(25) /* Timing Programmable */
  107. #define GC1R_IPP BIT(26) /* IRQ Polarity Programmable */
  108. #define GC1R_SPP BIT(27) /* Sync Polarity Programmable */
  109. #define GC1R_DWP BIT(28) /* Dither Width Programmable */
  110. #define GC1R_STREN BIT(29) /* STatus Registers ENabled */
  111. #define GC1R_BMEN BIT(31) /* Blind Mode ENabled */
  112. #define GC2R_EDCA BIT(0) /* External Display Control Ability */
  113. #define GC2R_STSAEN BIT(1) /* Slave Timing Sync Ability ENabled */
  114. #define GC2R_DVAEN BIT(2) /* Dual-View Ability ENabled */
  115. #define GC2R_DPAEN BIT(3) /* Dual-Port Ability ENabled */
  116. #define GC2R_BW GENMASK(6, 4) /* Bus Width (log2 of nb of bytes) */
  117. #define GC2R_EDCEN BIT(7) /* External Display Control ENabled */
  118. #define SRCR_IMR BIT(0) /* IMmediate Reload */
  119. #define SRCR_VBR BIT(1) /* Vertical Blanking Reload */
  120. #define BCCR_BCBLACK 0x00 /* Background Color BLACK */
  121. #define BCCR_BCBLUE GENMASK(7, 0) /* Background Color BLUE */
  122. #define BCCR_BCGREEN GENMASK(15, 8) /* Background Color GREEN */
  123. #define BCCR_BCRED GENMASK(23, 16) /* Background Color RED */
  124. #define BCCR_BCWHITE GENMASK(23, 0) /* Background Color WHITE */
  125. #define IER_LIE BIT(0) /* Line Interrupt Enable */
  126. #define IER_FUIE BIT(1) /* Fifo Underrun Interrupt Enable */
  127. #define IER_TERRIE BIT(2) /* Transfer ERRor Interrupt Enable */
  128. #define IER_RRIE BIT(3) /* Register Reload Interrupt enable */
  129. #define ISR_LIF BIT(0) /* Line Interrupt Flag */
  130. #define ISR_FUIF BIT(1) /* Fifo Underrun Interrupt Flag */
  131. #define ISR_TERRIF BIT(2) /* Transfer ERRor Interrupt Flag */
  132. #define ISR_RRIF BIT(3) /* Register Reload Interrupt Flag */
  133. #define LXCR_LEN BIT(0) /* Layer ENable */
  134. #define LXCR_COLKEN BIT(1) /* Color Keying Enable */
  135. #define LXCR_CLUTEN BIT(4) /* Color Look-Up Table ENable */
  136. #define LXWHPCR_WHSTPOS GENMASK(11, 0) /* Window Horizontal StarT POSition */
  137. #define LXWHPCR_WHSPPOS GENMASK(27, 16) /* Window Horizontal StoP POSition */
  138. #define LXWVPCR_WVSTPOS GENMASK(10, 0) /* Window Vertical StarT POSition */
  139. #define LXWVPCR_WVSPPOS GENMASK(26, 16) /* Window Vertical StoP POSition */
  140. #define LXPFCR_PF GENMASK(2, 0) /* Pixel Format */
  141. #define LXCACR_CONSTA GENMASK(7, 0) /* CONSTant Alpha */
  142. #define LXBFCR_BF2 GENMASK(2, 0) /* Blending Factor 2 */
  143. #define LXBFCR_BF1 GENMASK(10, 8) /* Blending Factor 1 */
  144. #define LXCFBLR_CFBLL GENMASK(12, 0) /* Color Frame Buffer Line Length */
  145. #define LXCFBLR_CFBP GENMASK(28, 16) /* Color Frame Buffer Pitch in bytes */
  146. #define LXCFBLNR_CFBLN GENMASK(10, 0) /* Color Frame Buffer Line Number */
  147. #define CLUT_SIZE 256
  148. #define CONSTA_MAX 0xFF /* CONSTant Alpha MAX= 1.0 */
  149. #define BF1_PAXCA 0x600 /* Pixel Alpha x Constant Alpha */
  150. #define BF1_CA 0x400 /* Constant Alpha */
  151. #define BF2_1PAXCA 0x007 /* 1 - (Pixel Alpha x Constant Alpha) */
  152. #define BF2_1CA 0x005 /* 1 - Constant Alpha */
  153. #define NB_PF 8 /* Max nb of HW pixel format */
  154. enum ltdc_pix_fmt {
  155. PF_NONE,
  156. /* RGB formats */
  157. PF_ARGB8888, /* ARGB [32 bits] */
  158. PF_RGBA8888, /* RGBA [32 bits] */
  159. PF_RGB888, /* RGB [24 bits] */
  160. PF_RGB565, /* RGB [16 bits] */
  161. PF_ARGB1555, /* ARGB A:1 bit RGB:15 bits [16 bits] */
  162. PF_ARGB4444, /* ARGB A:4 bits R/G/B: 4 bits each [16 bits] */
  163. /* Indexed formats */
  164. PF_L8, /* Indexed 8 bits [8 bits] */
  165. PF_AL44, /* Alpha:4 bits + indexed 4 bits [8 bits] */
  166. PF_AL88 /* Alpha:8 bits + indexed 8 bits [16 bits] */
  167. };
  168. /* The index gives the encoding of the pixel format for an HW version */
  169. static const enum ltdc_pix_fmt ltdc_pix_fmt_a0[NB_PF] = {
  170. PF_ARGB8888, /* 0x00 */
  171. PF_RGB888, /* 0x01 */
  172. PF_RGB565, /* 0x02 */
  173. PF_ARGB1555, /* 0x03 */
  174. PF_ARGB4444, /* 0x04 */
  175. PF_L8, /* 0x05 */
  176. PF_AL44, /* 0x06 */
  177. PF_AL88 /* 0x07 */
  178. };
  179. static const enum ltdc_pix_fmt ltdc_pix_fmt_a1[NB_PF] = {
  180. PF_ARGB8888, /* 0x00 */
  181. PF_RGB888, /* 0x01 */
  182. PF_RGB565, /* 0x02 */
  183. PF_RGBA8888, /* 0x03 */
  184. PF_AL44, /* 0x04 */
  185. PF_L8, /* 0x05 */
  186. PF_ARGB1555, /* 0x06 */
  187. PF_ARGB4444 /* 0x07 */
  188. };
  189. static inline u32 reg_read(void __iomem *base, u32 reg)
  190. {
  191. return readl_relaxed(base + reg);
  192. }
  193. static inline void reg_write(void __iomem *base, u32 reg, u32 val)
  194. {
  195. writel_relaxed(val, base + reg);
  196. }
  197. static inline void reg_set(void __iomem *base, u32 reg, u32 mask)
  198. {
  199. reg_write(base, reg, reg_read(base, reg) | mask);
  200. }
  201. static inline void reg_clear(void __iomem *base, u32 reg, u32 mask)
  202. {
  203. reg_write(base, reg, reg_read(base, reg) & ~mask);
  204. }
  205. static inline void reg_update_bits(void __iomem *base, u32 reg, u32 mask,
  206. u32 val)
  207. {
  208. reg_write(base, reg, (reg_read(base, reg) & ~mask) | val);
  209. }
  210. static inline struct ltdc_device *crtc_to_ltdc(struct drm_crtc *crtc)
  211. {
  212. return (struct ltdc_device *)crtc->dev->dev_private;
  213. }
  214. static inline struct ltdc_device *plane_to_ltdc(struct drm_plane *plane)
  215. {
  216. return (struct ltdc_device *)plane->dev->dev_private;
  217. }
  218. static inline struct ltdc_device *encoder_to_ltdc(struct drm_encoder *enc)
  219. {
  220. return (struct ltdc_device *)enc->dev->dev_private;
  221. }
  222. static inline enum ltdc_pix_fmt to_ltdc_pixelformat(u32 drm_fmt)
  223. {
  224. enum ltdc_pix_fmt pf;
  225. switch (drm_fmt) {
  226. case DRM_FORMAT_ARGB8888:
  227. case DRM_FORMAT_XRGB8888:
  228. pf = PF_ARGB8888;
  229. break;
  230. case DRM_FORMAT_RGBA8888:
  231. case DRM_FORMAT_RGBX8888:
  232. pf = PF_RGBA8888;
  233. break;
  234. case DRM_FORMAT_RGB888:
  235. pf = PF_RGB888;
  236. break;
  237. case DRM_FORMAT_RGB565:
  238. pf = PF_RGB565;
  239. break;
  240. case DRM_FORMAT_ARGB1555:
  241. case DRM_FORMAT_XRGB1555:
  242. pf = PF_ARGB1555;
  243. break;
  244. case DRM_FORMAT_ARGB4444:
  245. case DRM_FORMAT_XRGB4444:
  246. pf = PF_ARGB4444;
  247. break;
  248. case DRM_FORMAT_C8:
  249. pf = PF_L8;
  250. break;
  251. default:
  252. pf = PF_NONE;
  253. break;
  254. /* Note: There are no DRM_FORMAT for AL44 and AL88 */
  255. }
  256. return pf;
  257. }
  258. static inline u32 to_drm_pixelformat(enum ltdc_pix_fmt pf)
  259. {
  260. switch (pf) {
  261. case PF_ARGB8888:
  262. return DRM_FORMAT_ARGB8888;
  263. case PF_RGBA8888:
  264. return DRM_FORMAT_RGBA8888;
  265. case PF_RGB888:
  266. return DRM_FORMAT_RGB888;
  267. case PF_RGB565:
  268. return DRM_FORMAT_RGB565;
  269. case PF_ARGB1555:
  270. return DRM_FORMAT_ARGB1555;
  271. case PF_ARGB4444:
  272. return DRM_FORMAT_ARGB4444;
  273. case PF_L8:
  274. return DRM_FORMAT_C8;
  275. case PF_AL44: /* No DRM support */
  276. case PF_AL88: /* No DRM support */
  277. case PF_NONE:
  278. default:
  279. return 0;
  280. }
  281. }
  282. static irqreturn_t ltdc_irq_thread(int irq, void *arg)
  283. {
  284. struct drm_device *ddev = arg;
  285. struct ltdc_device *ldev = ddev->dev_private;
  286. struct drm_crtc *crtc = drm_crtc_from_index(ddev, 0);
  287. /* Line IRQ : trigger the vblank event */
  288. if (ldev->irq_status & ISR_LIF)
  289. drm_crtc_handle_vblank(crtc);
  290. /* Save FIFO Underrun & Transfer Error status */
  291. mutex_lock(&ldev->err_lock);
  292. if (ldev->irq_status & ISR_FUIF)
  293. ldev->error_status |= ISR_FUIF;
  294. if (ldev->irq_status & ISR_TERRIF)
  295. ldev->error_status |= ISR_TERRIF;
  296. mutex_unlock(&ldev->err_lock);
  297. return IRQ_HANDLED;
  298. }
  299. static irqreturn_t ltdc_irq(int irq, void *arg)
  300. {
  301. struct drm_device *ddev = arg;
  302. struct ltdc_device *ldev = ddev->dev_private;
  303. /* Read & Clear the interrupt status */
  304. ldev->irq_status = reg_read(ldev->regs, LTDC_ISR);
  305. reg_write(ldev->regs, LTDC_ICR, ldev->irq_status);
  306. return IRQ_WAKE_THREAD;
  307. }
  308. /*
  309. * DRM_CRTC
  310. */
  311. static void ltdc_crtc_update_clut(struct drm_crtc *crtc)
  312. {
  313. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  314. struct drm_color_lut *lut;
  315. u32 val;
  316. int i;
  317. if (!crtc || !crtc->state)
  318. return;
  319. if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
  320. return;
  321. lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
  322. for (i = 0; i < CLUT_SIZE; i++, lut++) {
  323. val = ((lut->red << 8) & 0xff0000) | (lut->green & 0xff00) |
  324. (lut->blue >> 8) | (i << 24);
  325. reg_write(ldev->regs, LTDC_L1CLUTWR, val);
  326. }
  327. }
  328. static void ltdc_crtc_atomic_enable(struct drm_crtc *crtc,
  329. struct drm_crtc_state *old_state)
  330. {
  331. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  332. DRM_DEBUG_DRIVER("\n");
  333. /* Sets the background color value */
  334. reg_write(ldev->regs, LTDC_BCCR, BCCR_BCBLACK);
  335. /* Enable IRQ */
  336. reg_set(ldev->regs, LTDC_IER, IER_RRIE | IER_FUIE | IER_TERRIE);
  337. /* Immediately commit the planes */
  338. reg_set(ldev->regs, LTDC_SRCR, SRCR_IMR);
  339. /* Enable LTDC */
  340. reg_set(ldev->regs, LTDC_GCR, GCR_LTDCEN);
  341. drm_crtc_vblank_on(crtc);
  342. }
  343. static void ltdc_crtc_atomic_disable(struct drm_crtc *crtc,
  344. struct drm_crtc_state *old_state)
  345. {
  346. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  347. DRM_DEBUG_DRIVER("\n");
  348. drm_crtc_vblank_off(crtc);
  349. /* disable LTDC */
  350. reg_clear(ldev->regs, LTDC_GCR, GCR_LTDCEN);
  351. /* disable IRQ */
  352. reg_clear(ldev->regs, LTDC_IER, IER_RRIE | IER_FUIE | IER_TERRIE);
  353. /* immediately commit disable of layers before switching off LTDC */
  354. reg_set(ldev->regs, LTDC_SRCR, SRCR_IMR);
  355. }
  356. static void ltdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
  357. {
  358. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  359. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  360. struct videomode vm;
  361. int rate = mode->clock * 1000;
  362. u32 hsync, vsync, accum_hbp, accum_vbp, accum_act_w, accum_act_h;
  363. u32 total_width, total_height;
  364. u32 val;
  365. drm_display_mode_to_videomode(mode, &vm);
  366. DRM_DEBUG_DRIVER("CRTC:%d mode:%s\n", crtc->base.id, mode->name);
  367. DRM_DEBUG_DRIVER("Video mode: %dx%d", vm.hactive, vm.vactive);
  368. DRM_DEBUG_DRIVER(" hfp %d hbp %d hsl %d vfp %d vbp %d vsl %d\n",
  369. vm.hfront_porch, vm.hback_porch, vm.hsync_len,
  370. vm.vfront_porch, vm.vback_porch, vm.vsync_len);
  371. /* Convert video timings to ltdc timings */
  372. hsync = vm.hsync_len - 1;
  373. vsync = vm.vsync_len - 1;
  374. accum_hbp = hsync + vm.hback_porch;
  375. accum_vbp = vsync + vm.vback_porch;
  376. accum_act_w = accum_hbp + vm.hactive;
  377. accum_act_h = accum_vbp + vm.vactive;
  378. total_width = accum_act_w + vm.hfront_porch;
  379. total_height = accum_act_h + vm.vfront_porch;
  380. clk_disable(ldev->pixel_clk);
  381. if (clk_set_rate(ldev->pixel_clk, rate) < 0) {
  382. DRM_ERROR("Cannot set rate (%dHz) for pixel clk\n", rate);
  383. return;
  384. }
  385. clk_enable(ldev->pixel_clk);
  386. /* Configures the HS, VS, DE and PC polarities. Default Active Low */
  387. val = 0;
  388. if (vm.flags & DISPLAY_FLAGS_HSYNC_HIGH)
  389. val |= GCR_HSPOL;
  390. if (vm.flags & DISPLAY_FLAGS_VSYNC_HIGH)
  391. val |= GCR_VSPOL;
  392. if (vm.flags & DISPLAY_FLAGS_DE_HIGH)
  393. val |= GCR_DEPOL;
  394. if (vm.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)
  395. val |= GCR_PCPOL;
  396. reg_update_bits(ldev->regs, LTDC_GCR,
  397. GCR_HSPOL | GCR_VSPOL | GCR_DEPOL | GCR_PCPOL, val);
  398. /* Set Synchronization size */
  399. val = (hsync << 16) | vsync;
  400. reg_update_bits(ldev->regs, LTDC_SSCR, SSCR_VSH | SSCR_HSW, val);
  401. /* Set Accumulated Back porch */
  402. val = (accum_hbp << 16) | accum_vbp;
  403. reg_update_bits(ldev->regs, LTDC_BPCR, BPCR_AVBP | BPCR_AHBP, val);
  404. /* Set Accumulated Active Width */
  405. val = (accum_act_w << 16) | accum_act_h;
  406. reg_update_bits(ldev->regs, LTDC_AWCR, AWCR_AAW | AWCR_AAH, val);
  407. /* Set total width & height */
  408. val = (total_width << 16) | total_height;
  409. reg_update_bits(ldev->regs, LTDC_TWCR, TWCR_TOTALH | TWCR_TOTALW, val);
  410. reg_write(ldev->regs, LTDC_LIPCR, (accum_act_h + 1));
  411. }
  412. static void ltdc_crtc_atomic_flush(struct drm_crtc *crtc,
  413. struct drm_crtc_state *old_crtc_state)
  414. {
  415. struct ltdc_device *ldev = crtc_to_ltdc(crtc);
  416. struct drm_pending_vblank_event *event = crtc->state->event;
  417. DRM_DEBUG_ATOMIC("\n");
  418. ltdc_crtc_update_clut(crtc);
  419. /* Commit shadow registers = update planes at next vblank */
  420. reg_set(ldev->regs, LTDC_SRCR, SRCR_VBR);
  421. if (event) {
  422. crtc->state->event = NULL;
  423. spin_lock_irq(&crtc->dev->event_lock);
  424. if (drm_crtc_vblank_get(crtc) == 0)
  425. drm_crtc_arm_vblank_event(crtc, event);
  426. else
  427. drm_crtc_send_vblank_event(crtc, event);
  428. spin_unlock_irq(&crtc->dev->event_lock);
  429. }
  430. }
  431. static const struct drm_crtc_helper_funcs ltdc_crtc_helper_funcs = {
  432. .mode_set_nofb = ltdc_crtc_mode_set_nofb,
  433. .atomic_flush = ltdc_crtc_atomic_flush,
  434. .atomic_enable = ltdc_crtc_atomic_enable,
  435. .atomic_disable = ltdc_crtc_atomic_disable,
  436. };
  437. int ltdc_crtc_enable_vblank(struct drm_device *ddev, unsigned int pipe)
  438. {
  439. struct ltdc_device *ldev = ddev->dev_private;
  440. DRM_DEBUG_DRIVER("\n");
  441. reg_set(ldev->regs, LTDC_IER, IER_LIE);
  442. return 0;
  443. }
  444. void ltdc_crtc_disable_vblank(struct drm_device *ddev, unsigned int pipe)
  445. {
  446. struct ltdc_device *ldev = ddev->dev_private;
  447. DRM_DEBUG_DRIVER("\n");
  448. reg_clear(ldev->regs, LTDC_IER, IER_LIE);
  449. }
  450. static const struct drm_crtc_funcs ltdc_crtc_funcs = {
  451. .destroy = drm_crtc_cleanup,
  452. .set_config = drm_atomic_helper_set_config,
  453. .page_flip = drm_atomic_helper_page_flip,
  454. .reset = drm_atomic_helper_crtc_reset,
  455. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  456. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  457. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  458. };
  459. /*
  460. * DRM_PLANE
  461. */
  462. static int ltdc_plane_atomic_check(struct drm_plane *plane,
  463. struct drm_plane_state *state)
  464. {
  465. struct drm_framebuffer *fb = state->fb;
  466. u32 src_x, src_y, src_w, src_h;
  467. DRM_DEBUG_DRIVER("\n");
  468. if (!fb)
  469. return 0;
  470. /* convert src_ from 16:16 format */
  471. src_x = state->src_x >> 16;
  472. src_y = state->src_y >> 16;
  473. src_w = state->src_w >> 16;
  474. src_h = state->src_h >> 16;
  475. /* Reject scaling */
  476. if (src_w != state->crtc_w || src_h != state->crtc_h) {
  477. DRM_ERROR("Scaling is not supported");
  478. return -EINVAL;
  479. }
  480. return 0;
  481. }
  482. static void ltdc_plane_atomic_update(struct drm_plane *plane,
  483. struct drm_plane_state *oldstate)
  484. {
  485. struct ltdc_device *ldev = plane_to_ltdc(plane);
  486. struct drm_plane_state *state = plane->state;
  487. struct drm_framebuffer *fb = state->fb;
  488. u32 lofs = plane->index * LAY_OFS;
  489. u32 x0 = state->crtc_x;
  490. u32 x1 = state->crtc_x + state->crtc_w - 1;
  491. u32 y0 = state->crtc_y;
  492. u32 y1 = state->crtc_y + state->crtc_h - 1;
  493. u32 src_x, src_y, src_w, src_h;
  494. u32 val, pitch_in_bytes, line_length, paddr, ahbp, avbp, bpcr;
  495. enum ltdc_pix_fmt pf;
  496. if (!state->crtc || !fb) {
  497. DRM_DEBUG_DRIVER("fb or crtc NULL");
  498. return;
  499. }
  500. /* convert src_ from 16:16 format */
  501. src_x = state->src_x >> 16;
  502. src_y = state->src_y >> 16;
  503. src_w = state->src_w >> 16;
  504. src_h = state->src_h >> 16;
  505. DRM_DEBUG_DRIVER("plane:%d fb:%d (%dx%d)@(%d,%d) -> (%dx%d)@(%d,%d)\n",
  506. plane->base.id, fb->base.id,
  507. src_w, src_h, src_x, src_y,
  508. state->crtc_w, state->crtc_h,
  509. state->crtc_x, state->crtc_y);
  510. bpcr = reg_read(ldev->regs, LTDC_BPCR);
  511. ahbp = (bpcr & BPCR_AHBP) >> 16;
  512. avbp = bpcr & BPCR_AVBP;
  513. /* Configures the horizontal start and stop position */
  514. val = ((x1 + 1 + ahbp) << 16) + (x0 + 1 + ahbp);
  515. reg_update_bits(ldev->regs, LTDC_L1WHPCR + lofs,
  516. LXWHPCR_WHSTPOS | LXWHPCR_WHSPPOS, val);
  517. /* Configures the vertical start and stop position */
  518. val = ((y1 + 1 + avbp) << 16) + (y0 + 1 + avbp);
  519. reg_update_bits(ldev->regs, LTDC_L1WVPCR + lofs,
  520. LXWVPCR_WVSTPOS | LXWVPCR_WVSPPOS, val);
  521. /* Specifies the pixel format */
  522. pf = to_ltdc_pixelformat(fb->format->format);
  523. for (val = 0; val < NB_PF; val++)
  524. if (ldev->caps.pix_fmt_hw[val] == pf)
  525. break;
  526. if (val == NB_PF) {
  527. DRM_ERROR("Pixel format %.4s not supported\n",
  528. (char *)&fb->format->format);
  529. val = 0; /* set by default ARGB 32 bits */
  530. }
  531. reg_update_bits(ldev->regs, LTDC_L1PFCR + lofs, LXPFCR_PF, val);
  532. /* Configures the color frame buffer pitch in bytes & line length */
  533. pitch_in_bytes = fb->pitches[0];
  534. line_length = drm_format_plane_cpp(fb->format->format, 0) *
  535. (x1 - x0 + 1) + (ldev->caps.bus_width >> 3) - 1;
  536. val = ((pitch_in_bytes << 16) | line_length);
  537. reg_update_bits(ldev->regs, LTDC_L1CFBLR + lofs,
  538. LXCFBLR_CFBLL | LXCFBLR_CFBP, val);
  539. /* Specifies the constant alpha value */
  540. val = CONSTA_MAX;
  541. reg_update_bits(ldev->regs, LTDC_L1CACR + lofs, LXCACR_CONSTA, val);
  542. /* Specifies the blending factors */
  543. val = BF1_PAXCA | BF2_1PAXCA;
  544. reg_update_bits(ldev->regs, LTDC_L1BFCR + lofs,
  545. LXBFCR_BF2 | LXBFCR_BF1, val);
  546. /* Configures the frame buffer line number */
  547. val = y1 - y0 + 1;
  548. reg_update_bits(ldev->regs, LTDC_L1CFBLNR + lofs, LXCFBLNR_CFBLN, val);
  549. /* Sets the FB address */
  550. paddr = (u32)drm_fb_cma_get_gem_addr(fb, state, 0);
  551. DRM_DEBUG_DRIVER("fb: phys 0x%08x", paddr);
  552. reg_write(ldev->regs, LTDC_L1CFBAR + lofs, paddr);
  553. /* Enable layer and CLUT if needed */
  554. val = fb->format->format == DRM_FORMAT_C8 ? LXCR_CLUTEN : 0;
  555. val |= LXCR_LEN;
  556. reg_update_bits(ldev->regs, LTDC_L1CR + lofs,
  557. LXCR_LEN | LXCR_CLUTEN, val);
  558. mutex_lock(&ldev->err_lock);
  559. if (ldev->error_status & ISR_FUIF) {
  560. DRM_DEBUG_DRIVER("Fifo underrun\n");
  561. ldev->error_status &= ~ISR_FUIF;
  562. }
  563. if (ldev->error_status & ISR_TERRIF) {
  564. DRM_DEBUG_DRIVER("Transfer error\n");
  565. ldev->error_status &= ~ISR_TERRIF;
  566. }
  567. mutex_unlock(&ldev->err_lock);
  568. }
  569. static void ltdc_plane_atomic_disable(struct drm_plane *plane,
  570. struct drm_plane_state *oldstate)
  571. {
  572. struct ltdc_device *ldev = plane_to_ltdc(plane);
  573. u32 lofs = plane->index * LAY_OFS;
  574. /* disable layer */
  575. reg_clear(ldev->regs, LTDC_L1CR + lofs, LXCR_LEN);
  576. DRM_DEBUG_DRIVER("CRTC:%d plane:%d\n",
  577. oldstate->crtc->base.id, plane->base.id);
  578. }
  579. static const struct drm_plane_funcs ltdc_plane_funcs = {
  580. .update_plane = drm_atomic_helper_update_plane,
  581. .disable_plane = drm_atomic_helper_disable_plane,
  582. .destroy = drm_plane_cleanup,
  583. .reset = drm_atomic_helper_plane_reset,
  584. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  585. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  586. };
  587. static const struct drm_plane_helper_funcs ltdc_plane_helper_funcs = {
  588. .atomic_check = ltdc_plane_atomic_check,
  589. .atomic_update = ltdc_plane_atomic_update,
  590. .atomic_disable = ltdc_plane_atomic_disable,
  591. };
  592. static struct drm_plane *ltdc_plane_create(struct drm_device *ddev,
  593. enum drm_plane_type type)
  594. {
  595. unsigned long possible_crtcs = CRTC_MASK;
  596. struct ltdc_device *ldev = ddev->dev_private;
  597. struct device *dev = ddev->dev;
  598. struct drm_plane *plane;
  599. unsigned int i, nb_fmt = 0;
  600. u32 formats[NB_PF];
  601. u32 drm_fmt;
  602. int ret;
  603. /* Get supported pixel formats */
  604. for (i = 0; i < NB_PF; i++) {
  605. drm_fmt = to_drm_pixelformat(ldev->caps.pix_fmt_hw[i]);
  606. if (!drm_fmt)
  607. continue;
  608. formats[nb_fmt++] = drm_fmt;
  609. }
  610. plane = devm_kzalloc(dev, sizeof(*plane), GFP_KERNEL);
  611. if (!plane)
  612. return 0;
  613. ret = drm_universal_plane_init(ddev, plane, possible_crtcs,
  614. &ltdc_plane_funcs, formats, nb_fmt,
  615. NULL, type, NULL);
  616. if (ret < 0)
  617. return 0;
  618. drm_plane_helper_add(plane, &ltdc_plane_helper_funcs);
  619. DRM_DEBUG_DRIVER("plane:%d created\n", plane->base.id);
  620. return plane;
  621. }
  622. static void ltdc_plane_destroy_all(struct drm_device *ddev)
  623. {
  624. struct drm_plane *plane, *plane_temp;
  625. list_for_each_entry_safe(plane, plane_temp,
  626. &ddev->mode_config.plane_list, head)
  627. drm_plane_cleanup(plane);
  628. }
  629. static int ltdc_crtc_init(struct drm_device *ddev, struct drm_crtc *crtc)
  630. {
  631. struct ltdc_device *ldev = ddev->dev_private;
  632. struct drm_plane *primary, *overlay;
  633. unsigned int i;
  634. int ret;
  635. primary = ltdc_plane_create(ddev, DRM_PLANE_TYPE_PRIMARY);
  636. if (!primary) {
  637. DRM_ERROR("Can not create primary plane\n");
  638. return -EINVAL;
  639. }
  640. ret = drm_crtc_init_with_planes(ddev, crtc, primary, NULL,
  641. &ltdc_crtc_funcs, NULL);
  642. if (ret) {
  643. DRM_ERROR("Can not initialize CRTC\n");
  644. goto cleanup;
  645. }
  646. drm_crtc_helper_add(crtc, &ltdc_crtc_helper_funcs);
  647. drm_mode_crtc_set_gamma_size(crtc, CLUT_SIZE);
  648. drm_crtc_enable_color_mgmt(crtc, 0, false, CLUT_SIZE);
  649. DRM_DEBUG_DRIVER("CRTC:%d created\n", crtc->base.id);
  650. /* Add planes. Note : the first layer is used by primary plane */
  651. for (i = 1; i < ldev->caps.nb_layers; i++) {
  652. overlay = ltdc_plane_create(ddev, DRM_PLANE_TYPE_OVERLAY);
  653. if (!overlay) {
  654. ret = -ENOMEM;
  655. DRM_ERROR("Can not create overlay plane %d\n", i);
  656. goto cleanup;
  657. }
  658. }
  659. return 0;
  660. cleanup:
  661. ltdc_plane_destroy_all(ddev);
  662. return ret;
  663. }
  664. /*
  665. * DRM_ENCODER
  666. */
  667. static const struct drm_encoder_funcs ltdc_encoder_funcs = {
  668. .destroy = drm_encoder_cleanup,
  669. };
  670. static int ltdc_encoder_init(struct drm_device *ddev, struct drm_bridge *bridge)
  671. {
  672. struct drm_encoder *encoder;
  673. int ret;
  674. encoder = devm_kzalloc(ddev->dev, sizeof(*encoder), GFP_KERNEL);
  675. if (!encoder)
  676. return -ENOMEM;
  677. encoder->possible_crtcs = CRTC_MASK;
  678. encoder->possible_clones = 0; /* No cloning support */
  679. drm_encoder_init(ddev, encoder, &ltdc_encoder_funcs,
  680. DRM_MODE_ENCODER_DPI, NULL);
  681. ret = drm_bridge_attach(encoder, bridge, NULL);
  682. if (ret) {
  683. drm_encoder_cleanup(encoder);
  684. return -EINVAL;
  685. }
  686. DRM_DEBUG_DRIVER("Bridge encoder:%d created\n", encoder->base.id);
  687. return 0;
  688. }
  689. static int ltdc_get_caps(struct drm_device *ddev)
  690. {
  691. struct ltdc_device *ldev = ddev->dev_private;
  692. u32 bus_width_log2, lcr, gc2r;
  693. /* at least 1 layer must be managed */
  694. lcr = reg_read(ldev->regs, LTDC_LCR);
  695. ldev->caps.nb_layers = max_t(int, lcr, 1);
  696. /* set data bus width */
  697. gc2r = reg_read(ldev->regs, LTDC_GC2R);
  698. bus_width_log2 = (gc2r & GC2R_BW) >> 4;
  699. ldev->caps.bus_width = 8 << bus_width_log2;
  700. ldev->caps.hw_version = reg_read(ldev->regs, LTDC_IDR);
  701. switch (ldev->caps.hw_version) {
  702. case HWVER_10200:
  703. case HWVER_10300:
  704. ldev->caps.reg_ofs = REG_OFS_NONE;
  705. ldev->caps.pix_fmt_hw = ltdc_pix_fmt_a0;
  706. break;
  707. case HWVER_20101:
  708. ldev->caps.reg_ofs = REG_OFS_4;
  709. ldev->caps.pix_fmt_hw = ltdc_pix_fmt_a1;
  710. break;
  711. default:
  712. return -ENODEV;
  713. }
  714. return 0;
  715. }
  716. int ltdc_load(struct drm_device *ddev)
  717. {
  718. struct platform_device *pdev = to_platform_device(ddev->dev);
  719. struct ltdc_device *ldev = ddev->dev_private;
  720. struct device *dev = ddev->dev;
  721. struct device_node *np = dev->of_node;
  722. struct drm_bridge *bridge[MAX_ENDPOINTS] = {NULL};
  723. struct drm_panel *panel[MAX_ENDPOINTS] = {NULL};
  724. struct drm_crtc *crtc;
  725. struct reset_control *rstc;
  726. struct resource *res;
  727. int irq, ret, i, endpoint_not_ready = -ENODEV;
  728. DRM_DEBUG_DRIVER("\n");
  729. /* Get endpoints if any */
  730. for (i = 0; i < MAX_ENDPOINTS; i++) {
  731. ret = drm_of_find_panel_or_bridge(np, 0, i, &panel[i],
  732. &bridge[i]);
  733. /*
  734. * If at least one endpoint is ready, continue probing,
  735. * else if at least one endpoint is -EPROBE_DEFER and
  736. * there is no previous ready endpoints, defer probing.
  737. */
  738. if (!ret)
  739. endpoint_not_ready = 0;
  740. else if (ret == -EPROBE_DEFER && endpoint_not_ready)
  741. endpoint_not_ready = -EPROBE_DEFER;
  742. }
  743. if (endpoint_not_ready)
  744. return endpoint_not_ready;
  745. rstc = devm_reset_control_get_exclusive(dev, NULL);
  746. mutex_init(&ldev->err_lock);
  747. ldev->pixel_clk = devm_clk_get(dev, "lcd");
  748. if (IS_ERR(ldev->pixel_clk)) {
  749. DRM_ERROR("Unable to get lcd clock\n");
  750. return -ENODEV;
  751. }
  752. if (clk_prepare_enable(ldev->pixel_clk)) {
  753. DRM_ERROR("Unable to prepare pixel clock\n");
  754. return -ENODEV;
  755. }
  756. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  757. ldev->regs = devm_ioremap_resource(dev, res);
  758. if (IS_ERR(ldev->regs)) {
  759. DRM_ERROR("Unable to get ltdc registers\n");
  760. ret = PTR_ERR(ldev->regs);
  761. goto err;
  762. }
  763. for (i = 0; i < MAX_IRQ; i++) {
  764. irq = platform_get_irq(pdev, i);
  765. if (irq < 0)
  766. continue;
  767. ret = devm_request_threaded_irq(dev, irq, ltdc_irq,
  768. ltdc_irq_thread, IRQF_ONESHOT,
  769. dev_name(dev), ddev);
  770. if (ret) {
  771. DRM_ERROR("Failed to register LTDC interrupt\n");
  772. goto err;
  773. }
  774. }
  775. if (!IS_ERR(rstc))
  776. reset_control_deassert(rstc);
  777. /* Disable interrupts */
  778. reg_clear(ldev->regs, LTDC_IER,
  779. IER_LIE | IER_RRIE | IER_FUIE | IER_TERRIE);
  780. ret = ltdc_get_caps(ddev);
  781. if (ret) {
  782. DRM_ERROR("hardware identifier (0x%08x) not supported!\n",
  783. ldev->caps.hw_version);
  784. goto err;
  785. }
  786. DRM_INFO("ltdc hw version 0x%08x - ready\n", ldev->caps.hw_version);
  787. /* Add endpoints panels or bridges if any */
  788. for (i = 0; i < MAX_ENDPOINTS; i++) {
  789. if (panel[i]) {
  790. bridge[i] = drm_panel_bridge_add(panel[i],
  791. DRM_MODE_CONNECTOR_DPI);
  792. if (IS_ERR(bridge[i])) {
  793. DRM_ERROR("panel-bridge endpoint %d\n", i);
  794. ret = PTR_ERR(bridge[i]);
  795. goto err;
  796. }
  797. }
  798. if (bridge[i]) {
  799. ret = ltdc_encoder_init(ddev, bridge[i]);
  800. if (ret) {
  801. DRM_ERROR("init encoder endpoint %d\n", i);
  802. goto err;
  803. }
  804. }
  805. }
  806. crtc = devm_kzalloc(dev, sizeof(*crtc), GFP_KERNEL);
  807. if (!crtc) {
  808. DRM_ERROR("Failed to allocate crtc\n");
  809. ret = -ENOMEM;
  810. goto err;
  811. }
  812. ret = ltdc_crtc_init(ddev, crtc);
  813. if (ret) {
  814. DRM_ERROR("Failed to init crtc\n");
  815. goto err;
  816. }
  817. ret = drm_vblank_init(ddev, NB_CRTC);
  818. if (ret) {
  819. DRM_ERROR("Failed calling drm_vblank_init()\n");
  820. goto err;
  821. }
  822. /* Allow usage of vblank without having to call drm_irq_install */
  823. ddev->irq_enabled = 1;
  824. return 0;
  825. err:
  826. for (i = 0; i < MAX_ENDPOINTS; i++)
  827. drm_panel_bridge_remove(bridge[i]);
  828. clk_disable_unprepare(ldev->pixel_clk);
  829. return ret;
  830. }
  831. void ltdc_unload(struct drm_device *ddev)
  832. {
  833. struct ltdc_device *ldev = ddev->dev_private;
  834. int i;
  835. DRM_DEBUG_DRIVER("\n");
  836. for (i = 0; i < MAX_ENDPOINTS; i++)
  837. drm_of_panel_bridge_remove(ddev->dev->of_node, 0, i);
  838. clk_disable_unprepare(ldev->pixel_clk);
  839. }
  840. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  841. MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
  842. MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
  843. MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
  844. MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
  845. MODULE_LICENSE("GPL v2");