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