atmel_hlcdc_dc.h 14 KB

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