atmel_hlcdc_dc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) 2014 Traphandler
  3. * Copyright (C) 2014 Free Electrons
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  7. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef DRM_ATMEL_HLCDC_H
  22. #define DRM_ATMEL_HLCDC_H
  23. #include <linux/clk.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/mfd/atmel-hlcdc.h>
  27. #include <linux/pwm.h>
  28. #include <drm/drm_atomic.h>
  29. #include <drm/drm_atomic_helper.h>
  30. #include <drm/drm_crtc.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/drm_fb_helper.h>
  33. #include <drm/drm_fb_cma_helper.h>
  34. #include <drm/drm_gem_cma_helper.h>
  35. #include <drm/drm_gem_framebuffer_helper.h>
  36. #include <drm/drm_panel.h>
  37. #include <drm/drm_plane_helper.h>
  38. #include <drm/drmP.h>
  39. #define ATMEL_HLCDC_LAYER_CHER 0x0
  40. #define ATMEL_HLCDC_LAYER_CHDR 0x4
  41. #define ATMEL_HLCDC_LAYER_CHSR 0x8
  42. #define ATMEL_HLCDC_LAYER_EN BIT(0)
  43. #define ATMEL_HLCDC_LAYER_UPDATE BIT(1)
  44. #define ATMEL_HLCDC_LAYER_A2Q BIT(2)
  45. #define ATMEL_HLCDC_LAYER_RST BIT(8)
  46. #define ATMEL_HLCDC_LAYER_IER 0xc
  47. #define ATMEL_HLCDC_LAYER_IDR 0x10
  48. #define ATMEL_HLCDC_LAYER_IMR 0x14
  49. #define ATMEL_HLCDC_LAYER_ISR 0x18
  50. #define ATMEL_HLCDC_LAYER_DFETCH BIT(0)
  51. #define ATMEL_HLCDC_LAYER_LFETCH BIT(1)
  52. #define ATMEL_HLCDC_LAYER_DMA_IRQ(p) BIT(2 + (8 * (p)))
  53. #define ATMEL_HLCDC_LAYER_DSCR_IRQ(p) BIT(3 + (8 * (p)))
  54. #define ATMEL_HLCDC_LAYER_ADD_IRQ(p) BIT(4 + (8 * (p)))
  55. #define ATMEL_HLCDC_LAYER_DONE_IRQ(p) BIT(5 + (8 * (p)))
  56. #define ATMEL_HLCDC_LAYER_OVR_IRQ(p) BIT(6 + (8 * (p)))
  57. #define ATMEL_HLCDC_LAYER_PLANE_HEAD(p) (((p) * 0x10) + 0x1c)
  58. #define ATMEL_HLCDC_LAYER_PLANE_ADDR(p) (((p) * 0x10) + 0x20)
  59. #define ATMEL_HLCDC_LAYER_PLANE_CTRL(p) (((p) * 0x10) + 0x24)
  60. #define ATMEL_HLCDC_LAYER_PLANE_NEXT(p) (((p) * 0x10) + 0x28)
  61. #define ATMEL_HLCDC_LAYER_DMA_CFG 0
  62. #define ATMEL_HLCDC_LAYER_DMA_SIF BIT(0)
  63. #define ATMEL_HLCDC_LAYER_DMA_BLEN_MASK GENMASK(5, 4)
  64. #define ATMEL_HLCDC_LAYER_DMA_BLEN_SINGLE (0 << 4)
  65. #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR4 (1 << 4)
  66. #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR8 (2 << 4)
  67. #define ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16 (3 << 4)
  68. #define ATMEL_HLCDC_LAYER_DMA_DLBO BIT(8)
  69. #define ATMEL_HLCDC_LAYER_DMA_ROTDIS BIT(12)
  70. #define ATMEL_HLCDC_LAYER_DMA_LOCKDIS BIT(13)
  71. #define ATMEL_HLCDC_LAYER_FORMAT_CFG 1
  72. #define ATMEL_HLCDC_LAYER_RGB (0 << 0)
  73. #define ATMEL_HLCDC_LAYER_CLUT (1 << 0)
  74. #define ATMEL_HLCDC_LAYER_YUV (2 << 0)
  75. #define ATMEL_HLCDC_RGB_MODE(m) \
  76. (ATMEL_HLCDC_LAYER_RGB | (((m) & 0xf) << 4))
  77. #define ATMEL_HLCDC_CLUT_MODE(m) \
  78. (ATMEL_HLCDC_LAYER_CLUT | (((m) & 0x3) << 8))
  79. #define ATMEL_HLCDC_YUV_MODE(m) \
  80. (ATMEL_HLCDC_LAYER_YUV | (((m) & 0xf) << 12))
  81. #define ATMEL_HLCDC_YUV422ROT BIT(16)
  82. #define ATMEL_HLCDC_YUV422SWP BIT(17)
  83. #define ATMEL_HLCDC_DSCALEOPT BIT(20)
  84. #define ATMEL_HLCDC_C1_MODE ATMEL_HLCDC_CLUT_MODE(0)
  85. #define ATMEL_HLCDC_C2_MODE ATMEL_HLCDC_CLUT_MODE(1)
  86. #define ATMEL_HLCDC_C4_MODE ATMEL_HLCDC_CLUT_MODE(2)
  87. #define ATMEL_HLCDC_C8_MODE ATMEL_HLCDC_CLUT_MODE(3)
  88. #define ATMEL_HLCDC_XRGB4444_MODE ATMEL_HLCDC_RGB_MODE(0)
  89. #define ATMEL_HLCDC_ARGB4444_MODE ATMEL_HLCDC_RGB_MODE(1)
  90. #define ATMEL_HLCDC_RGBA4444_MODE ATMEL_HLCDC_RGB_MODE(2)
  91. #define ATMEL_HLCDC_RGB565_MODE ATMEL_HLCDC_RGB_MODE(3)
  92. #define ATMEL_HLCDC_ARGB1555_MODE ATMEL_HLCDC_RGB_MODE(4)
  93. #define ATMEL_HLCDC_XRGB8888_MODE ATMEL_HLCDC_RGB_MODE(9)
  94. #define ATMEL_HLCDC_RGB888_MODE ATMEL_HLCDC_RGB_MODE(10)
  95. #define ATMEL_HLCDC_ARGB8888_MODE ATMEL_HLCDC_RGB_MODE(12)
  96. #define ATMEL_HLCDC_RGBA8888_MODE ATMEL_HLCDC_RGB_MODE(13)
  97. #define ATMEL_HLCDC_AYUV_MODE ATMEL_HLCDC_YUV_MODE(0)
  98. #define ATMEL_HLCDC_YUYV_MODE ATMEL_HLCDC_YUV_MODE(1)
  99. #define ATMEL_HLCDC_UYVY_MODE ATMEL_HLCDC_YUV_MODE(2)
  100. #define ATMEL_HLCDC_YVYU_MODE ATMEL_HLCDC_YUV_MODE(3)
  101. #define ATMEL_HLCDC_VYUY_MODE ATMEL_HLCDC_YUV_MODE(4)
  102. #define ATMEL_HLCDC_NV61_MODE ATMEL_HLCDC_YUV_MODE(5)
  103. #define ATMEL_HLCDC_YUV422_MODE ATMEL_HLCDC_YUV_MODE(6)
  104. #define ATMEL_HLCDC_NV21_MODE ATMEL_HLCDC_YUV_MODE(7)
  105. #define ATMEL_HLCDC_YUV420_MODE ATMEL_HLCDC_YUV_MODE(8)
  106. #define ATMEL_HLCDC_LAYER_POS(x, y) ((x) | ((y) << 16))
  107. #define ATMEL_HLCDC_LAYER_SIZE(w, h) (((w) - 1) | (((h) - 1) << 16))
  108. #define ATMEL_HLCDC_LAYER_CRKEY BIT(0)
  109. #define ATMEL_HLCDC_LAYER_INV BIT(1)
  110. #define ATMEL_HLCDC_LAYER_ITER2BL BIT(2)
  111. #define ATMEL_HLCDC_LAYER_ITER BIT(3)
  112. #define ATMEL_HLCDC_LAYER_REVALPHA BIT(4)
  113. #define ATMEL_HLCDC_LAYER_GAEN BIT(5)
  114. #define ATMEL_HLCDC_LAYER_LAEN BIT(6)
  115. #define ATMEL_HLCDC_LAYER_OVR BIT(7)
  116. #define ATMEL_HLCDC_LAYER_DMA BIT(8)
  117. #define ATMEL_HLCDC_LAYER_REP BIT(9)
  118. #define ATMEL_HLCDC_LAYER_DSTKEY BIT(10)
  119. #define ATMEL_HLCDC_LAYER_DISCEN BIT(11)
  120. #define ATMEL_HLCDC_LAYER_GA_SHIFT 16
  121. #define ATMEL_HLCDC_LAYER_GA_MASK \
  122. GENMASK(23, ATMEL_HLCDC_LAYER_GA_SHIFT)
  123. #define ATMEL_HLCDC_LAYER_GA(x) \
  124. ((x) << ATMEL_HLCDC_LAYER_GA_SHIFT)
  125. #define ATMEL_HLCDC_LAYER_DISC_POS(x, y) ((x) | ((y) << 16))
  126. #define ATMEL_HLCDC_LAYER_DISC_SIZE(w, h) (((w) - 1) | (((h) - 1) << 16))
  127. #define ATMEL_HLCDC_LAYER_SCALER_FACTORS(x, y) ((x) | ((y) << 16))
  128. #define ATMEL_HLCDC_LAYER_SCALER_ENABLE BIT(31)
  129. #define ATMEL_HLCDC_LAYER_MAX_PLANES 3
  130. #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_RESERVED BIT(0)
  131. #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_LOADED BIT(1)
  132. #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_DONE BIT(2)
  133. #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_OVERRUN BIT(3)
  134. #define ATMEL_HLCDC_CLUT_SIZE 256
  135. #define ATMEL_HLCDC_MAX_LAYERS 6
  136. /**
  137. * Atmel HLCDC Layer registers layout structure
  138. *
  139. * Each HLCDC layer has its own register organization and a given register
  140. * can be placed differently on 2 different layers depending on its
  141. * capabilities.
  142. * This structure stores common registers layout for a given layer and is
  143. * used by HLCDC layer code to choose the appropriate register to write to
  144. * or to read from.
  145. *
  146. * For all fields, a value of zero means "unsupported".
  147. *
  148. * See Atmel's datasheet for a detailled description of these registers.
  149. *
  150. * @xstride: xstride registers
  151. * @pstride: pstride registers
  152. * @pos: position register
  153. * @size: displayed size register
  154. * @memsize: memory size register
  155. * @default_color: default color register
  156. * @chroma_key: chroma key register
  157. * @chroma_key_mask: chroma key mask register
  158. * @general_config: general layer config register
  159. * @sacler_config: scaler factors register
  160. * @phicoeffs: X/Y PHI coefficient registers
  161. * @disc_pos: discard area position register
  162. * @disc_size: discard area size register
  163. * @csc: color space conversion register
  164. */
  165. struct atmel_hlcdc_layer_cfg_layout {
  166. int xstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
  167. int pstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
  168. int pos;
  169. int size;
  170. int memsize;
  171. int default_color;
  172. int chroma_key;
  173. int chroma_key_mask;
  174. int general_config;
  175. int scaler_config;
  176. struct {
  177. int x;
  178. int y;
  179. } phicoeffs;
  180. int disc_pos;
  181. int disc_size;
  182. int csc;
  183. };
  184. /**
  185. * Atmel HLCDC DMA descriptor structure
  186. *
  187. * This structure is used by the HLCDC DMA engine to schedule a DMA transfer.
  188. *
  189. * The structure fields must remain in this specific order, because they're
  190. * used by the HLCDC DMA engine, which expect them in this order.
  191. * HLCDC DMA descriptors must be aligned on 64 bits.
  192. *
  193. * @addr: buffer DMA address
  194. * @ctrl: DMA transfer options
  195. * @next: next DMA descriptor to fetch
  196. * @self: descriptor DMA address
  197. */
  198. struct atmel_hlcdc_dma_channel_dscr {
  199. dma_addr_t addr;
  200. u32 ctrl;
  201. dma_addr_t next;
  202. dma_addr_t self;
  203. } __aligned(sizeof(u64));
  204. /**
  205. * Atmel HLCDC layer types
  206. */
  207. enum atmel_hlcdc_layer_type {
  208. ATMEL_HLCDC_NO_LAYER,
  209. ATMEL_HLCDC_BASE_LAYER,
  210. ATMEL_HLCDC_OVERLAY_LAYER,
  211. ATMEL_HLCDC_CURSOR_LAYER,
  212. ATMEL_HLCDC_PP_LAYER,
  213. };
  214. /**
  215. * Atmel HLCDC Supported formats structure
  216. *
  217. * This structure list all the formats supported by a given layer.
  218. *
  219. * @nformats: number of supported formats
  220. * @formats: supported formats
  221. */
  222. struct atmel_hlcdc_formats {
  223. int nformats;
  224. u32 *formats;
  225. };
  226. /**
  227. * Atmel HLCDC Layer description structure
  228. *
  229. * This structure describes the capabilities provided by a given layer.
  230. *
  231. * @name: layer name
  232. * @type: layer type
  233. * @id: layer id
  234. * @regs_offset: offset of the layer registers from the HLCDC registers base
  235. * @cfgs_offset: CFGX registers offset from the layer registers base
  236. * @formats: supported formats
  237. * @layout: config registers layout
  238. * @max_width: maximum width supported by this layer (0 means unlimited)
  239. * @max_height: maximum height supported by this layer (0 means unlimited)
  240. */
  241. struct atmel_hlcdc_layer_desc {
  242. const char *name;
  243. enum atmel_hlcdc_layer_type type;
  244. int id;
  245. int regs_offset;
  246. int cfgs_offset;
  247. int clut_offset;
  248. struct atmel_hlcdc_formats *formats;
  249. struct atmel_hlcdc_layer_cfg_layout layout;
  250. int max_width;
  251. int max_height;
  252. };
  253. /**
  254. * Atmel HLCDC Layer.
  255. *
  256. * A layer can be a DRM plane of a post processing layer used to render
  257. * HLCDC composition into memory.
  258. *
  259. * @desc: layer description
  260. * @regmap: pointer to the HLCDC regmap
  261. */
  262. struct atmel_hlcdc_layer {
  263. const struct atmel_hlcdc_layer_desc *desc;
  264. struct regmap *regmap;
  265. };
  266. /**
  267. * Atmel HLCDC Plane.
  268. *
  269. * @base: base DRM plane structure
  270. * @layer: HLCDC layer structure
  271. * @properties: pointer to the property definitions structure
  272. */
  273. struct atmel_hlcdc_plane {
  274. struct drm_plane base;
  275. struct atmel_hlcdc_layer layer;
  276. struct atmel_hlcdc_plane_properties *properties;
  277. };
  278. static inline struct atmel_hlcdc_plane *
  279. drm_plane_to_atmel_hlcdc_plane(struct drm_plane *p)
  280. {
  281. return container_of(p, struct atmel_hlcdc_plane, base);
  282. }
  283. static inline struct atmel_hlcdc_plane *
  284. atmel_hlcdc_layer_to_plane(struct atmel_hlcdc_layer *layer)
  285. {
  286. return container_of(layer, struct atmel_hlcdc_plane, layer);
  287. }
  288. /**
  289. * Atmel HLCDC Display Controller description structure.
  290. *
  291. * This structure describes the HLCDC IP capabilities and depends on the
  292. * HLCDC IP version (or Atmel SoC family).
  293. *
  294. * @min_width: minimum width supported by the Display Controller
  295. * @min_height: minimum height supported by the Display Controller
  296. * @max_width: maximum width supported by the Display Controller
  297. * @max_height: maximum height supported by the Display Controller
  298. * @max_spw: maximum vertical/horizontal pulse width
  299. * @max_vpw: maximum vertical back/front porch width
  300. * @max_hpw: maximum horizontal back/front porch width
  301. * @conflicting_output_formats: true if RGBXXX output formats conflict with
  302. * each other.
  303. * @layers: a layer description table describing available layers
  304. * @nlayers: layer description table size
  305. */
  306. struct atmel_hlcdc_dc_desc {
  307. int min_width;
  308. int min_height;
  309. int max_width;
  310. int max_height;
  311. int max_spw;
  312. int max_vpw;
  313. int max_hpw;
  314. bool conflicting_output_formats;
  315. const struct atmel_hlcdc_layer_desc *layers;
  316. int nlayers;
  317. };
  318. /**
  319. * Atmel HLCDC Plane properties.
  320. *
  321. * This structure stores plane property definitions.
  322. *
  323. * @alpha: alpha blending (or transparency) property
  324. * @rotation: rotation property
  325. */
  326. struct atmel_hlcdc_plane_properties {
  327. struct drm_property *alpha;
  328. };
  329. /**
  330. * Atmel HLCDC Display Controller.
  331. *
  332. * @desc: HLCDC Display Controller description
  333. * @dscrpool: DMA coherent pool used to allocate DMA descriptors
  334. * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device
  335. * @fbdev: framebuffer device attached to the Display Controller
  336. * @crtc: CRTC provided by the display controller
  337. * @planes: instantiated planes
  338. * @layers: active HLCDC layers
  339. * @wq: display controller workqueue
  340. * @suspend: used to store the HLCDC state when entering suspend
  341. * @commit: used for async commit handling
  342. */
  343. struct atmel_hlcdc_dc {
  344. const struct atmel_hlcdc_dc_desc *desc;
  345. struct dma_pool *dscrpool;
  346. struct atmel_hlcdc *hlcdc;
  347. struct drm_crtc *crtc;
  348. struct atmel_hlcdc_layer *layers[ATMEL_HLCDC_MAX_LAYERS];
  349. struct workqueue_struct *wq;
  350. struct {
  351. u32 imr;
  352. struct drm_atomic_state *state;
  353. } suspend;
  354. struct {
  355. wait_queue_head_t wait;
  356. bool pending;
  357. } commit;
  358. };
  359. extern struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats;
  360. extern struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats;
  361. static inline void atmel_hlcdc_layer_write_reg(struct atmel_hlcdc_layer *layer,
  362. unsigned int reg, u32 val)
  363. {
  364. regmap_write(layer->regmap, layer->desc->regs_offset + reg, val);
  365. }
  366. static inline u32 atmel_hlcdc_layer_read_reg(struct atmel_hlcdc_layer *layer,
  367. unsigned int reg)
  368. {
  369. u32 val;
  370. regmap_read(layer->regmap, layer->desc->regs_offset + reg, &val);
  371. return val;
  372. }
  373. static inline void atmel_hlcdc_layer_write_cfg(struct atmel_hlcdc_layer *layer,
  374. unsigned int cfgid, u32 val)
  375. {
  376. atmel_hlcdc_layer_write_reg(layer,
  377. layer->desc->cfgs_offset +
  378. (cfgid * sizeof(u32)), val);
  379. }
  380. static inline u32 atmel_hlcdc_layer_read_cfg(struct atmel_hlcdc_layer *layer,
  381. unsigned int cfgid)
  382. {
  383. return atmel_hlcdc_layer_read_reg(layer,
  384. layer->desc->cfgs_offset +
  385. (cfgid * sizeof(u32)));
  386. }
  387. static inline void atmel_hlcdc_layer_write_clut(struct atmel_hlcdc_layer *layer,
  388. unsigned int c, u32 val)
  389. {
  390. regmap_write(layer->regmap,
  391. layer->desc->clut_offset + c * sizeof(u32),
  392. val);
  393. }
  394. static inline void atmel_hlcdc_layer_init(struct atmel_hlcdc_layer *layer,
  395. const struct atmel_hlcdc_layer_desc *desc,
  396. struct regmap *regmap)
  397. {
  398. layer->desc = desc;
  399. layer->regmap = regmap;
  400. }
  401. enum drm_mode_status
  402. atmel_hlcdc_dc_mode_valid(struct atmel_hlcdc_dc *dc,
  403. const struct drm_display_mode *mode);
  404. int atmel_hlcdc_create_planes(struct drm_device *dev);
  405. void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane);
  406. int atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state);
  407. int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state);
  408. void atmel_hlcdc_crtc_irq(struct drm_crtc *c);
  409. int atmel_hlcdc_crtc_create(struct drm_device *dev);
  410. int atmel_hlcdc_create_outputs(struct drm_device *dev);
  411. #endif /* DRM_ATMEL_HLCDC_H */