sti_gdp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) STMicroelectronics SA 2014
  4. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  5. * Fabien Dessenne <fabien.dessenne@st.com>
  6. * for STMicroelectronics.
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/of.h>
  10. #include <linux/seq_file.h>
  11. #include <drm/drm_atomic.h>
  12. #include <drm/drm_device.h>
  13. #include <drm/drm_fb_dma_helper.h>
  14. #include <drm/drm_fourcc.h>
  15. #include <drm/drm_framebuffer.h>
  16. #include <drm/drm_gem_dma_helper.h>
  17. #include <drm/drm_print.h>
  18. #include "sti_compositor.h"
  19. #include "sti_gdp.h"
  20. #include "sti_plane.h"
  21. #include "sti_vtg.h"
  22. #define ALPHASWITCH BIT(6)
  23. #define ENA_COLOR_FILL BIT(8)
  24. #define BIGNOTLITTLE BIT(23)
  25. #define WAIT_NEXT_VSYNC BIT(31)
  26. /* GDP color formats */
  27. #define GDP_RGB565 0x00
  28. #define GDP_RGB888 0x01
  29. #define GDP_RGB888_32 0x02
  30. #define GDP_XBGR8888 (GDP_RGB888_32 | BIGNOTLITTLE | ALPHASWITCH)
  31. #define GDP_ARGB8565 0x04
  32. #define GDP_ARGB8888 0x05
  33. #define GDP_ABGR8888 (GDP_ARGB8888 | BIGNOTLITTLE | ALPHASWITCH)
  34. #define GDP_ARGB1555 0x06
  35. #define GDP_ARGB4444 0x07
  36. #define GDP2STR(fmt) { GDP_ ## fmt, #fmt }
  37. static struct gdp_format_to_str {
  38. int format;
  39. char name[20];
  40. } gdp_format_to_str[] = {
  41. GDP2STR(RGB565),
  42. GDP2STR(RGB888),
  43. GDP2STR(RGB888_32),
  44. GDP2STR(XBGR8888),
  45. GDP2STR(ARGB8565),
  46. GDP2STR(ARGB8888),
  47. GDP2STR(ABGR8888),
  48. GDP2STR(ARGB1555),
  49. GDP2STR(ARGB4444)
  50. };
  51. #define GAM_GDP_CTL_OFFSET 0x00
  52. #define GAM_GDP_AGC_OFFSET 0x04
  53. #define GAM_GDP_VPO_OFFSET 0x0C
  54. #define GAM_GDP_VPS_OFFSET 0x10
  55. #define GAM_GDP_PML_OFFSET 0x14
  56. #define GAM_GDP_PMP_OFFSET 0x18
  57. #define GAM_GDP_SIZE_OFFSET 0x1C
  58. #define GAM_GDP_NVN_OFFSET 0x24
  59. #define GAM_GDP_KEY1_OFFSET 0x28
  60. #define GAM_GDP_KEY2_OFFSET 0x2C
  61. #define GAM_GDP_PPT_OFFSET 0x34
  62. #define GAM_GDP_CML_OFFSET 0x3C
  63. #define GAM_GDP_MST_OFFSET 0x68
  64. #define GAM_GDP_ALPHARANGE_255 BIT(5)
  65. #define GAM_GDP_AGC_FULL_RANGE 0x00808080
  66. #define GAM_GDP_PPT_IGNORE (BIT(1) | BIT(0))
  67. #define GAM_GDP_SIZE_MAX_WIDTH 3840
  68. #define GAM_GDP_SIZE_MAX_HEIGHT 2160
  69. #define GDP_NODE_NB_BANK 2
  70. #define GDP_NODE_PER_FIELD 2
  71. struct sti_gdp_node {
  72. u32 gam_gdp_ctl;
  73. u32 gam_gdp_agc;
  74. u32 reserved1;
  75. u32 gam_gdp_vpo;
  76. u32 gam_gdp_vps;
  77. u32 gam_gdp_pml;
  78. u32 gam_gdp_pmp;
  79. u32 gam_gdp_size;
  80. u32 reserved2;
  81. u32 gam_gdp_nvn;
  82. u32 gam_gdp_key1;
  83. u32 gam_gdp_key2;
  84. u32 reserved3;
  85. u32 gam_gdp_ppt;
  86. u32 reserved4;
  87. u32 gam_gdp_cml;
  88. };
  89. struct sti_gdp_node_list {
  90. struct sti_gdp_node *top_field;
  91. dma_addr_t top_field_paddr;
  92. struct sti_gdp_node *btm_field;
  93. dma_addr_t btm_field_paddr;
  94. };
  95. /*
  96. * STI GDP structure
  97. *
  98. * @sti_plane: sti_plane structure
  99. * @dev: driver device
  100. * @regs: gdp registers
  101. * @clk_pix: pixel clock for the current gdp
  102. * @clk_main_parent: gdp parent clock if main path used
  103. * @clk_aux_parent: gdp parent clock if aux path used
  104. * @vtg_field_nb: callback for VTG FIELD (top or bottom) notification
  105. * @is_curr_top: true if the current node processed is the top field
  106. * @node_list: array of node list
  107. * @vtg: registered vtg
  108. */
  109. struct sti_gdp {
  110. struct sti_plane plane;
  111. struct device *dev;
  112. void __iomem *regs;
  113. struct clk *clk_pix;
  114. struct clk *clk_main_parent;
  115. struct clk *clk_aux_parent;
  116. struct notifier_block vtg_field_nb;
  117. bool is_curr_top;
  118. struct sti_gdp_node_list node_list[GDP_NODE_NB_BANK];
  119. struct sti_vtg *vtg;
  120. };
  121. #define to_sti_gdp(x) container_of(x, struct sti_gdp, plane)
  122. static const uint32_t gdp_supported_formats[] = {
  123. DRM_FORMAT_XRGB8888,
  124. DRM_FORMAT_XBGR8888,
  125. DRM_FORMAT_ARGB8888,
  126. DRM_FORMAT_ABGR8888,
  127. DRM_FORMAT_ARGB4444,
  128. DRM_FORMAT_ARGB1555,
  129. DRM_FORMAT_RGB565,
  130. DRM_FORMAT_RGB888,
  131. };
  132. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  133. readl(gdp->regs + reg ## _OFFSET))
  134. static void gdp_dbg_ctl(struct seq_file *s, int val)
  135. {
  136. int i;
  137. seq_puts(s, "\tColor:");
  138. for (i = 0; i < ARRAY_SIZE(gdp_format_to_str); i++) {
  139. if (gdp_format_to_str[i].format == (val & 0x1F)) {
  140. seq_puts(s, gdp_format_to_str[i].name);
  141. break;
  142. }
  143. }
  144. if (i == ARRAY_SIZE(gdp_format_to_str))
  145. seq_puts(s, "<UNKNOWN>");
  146. seq_printf(s, "\tWaitNextVsync:%d", val & WAIT_NEXT_VSYNC ? 1 : 0);
  147. }
  148. static void gdp_dbg_vpo(struct seq_file *s, int val)
  149. {
  150. seq_printf(s, "\txdo:%4d\tydo:%4d", val & 0xFFFF, (val >> 16) & 0xFFFF);
  151. }
  152. static void gdp_dbg_vps(struct seq_file *s, int val)
  153. {
  154. seq_printf(s, "\txds:%4d\tyds:%4d", val & 0xFFFF, (val >> 16) & 0xFFFF);
  155. }
  156. static void gdp_dbg_size(struct seq_file *s, int val)
  157. {
  158. seq_printf(s, "\t%d x %d", val & 0xFFFF, (val >> 16) & 0xFFFF);
  159. }
  160. static void gdp_dbg_nvn(struct seq_file *s, struct sti_gdp *gdp, int val)
  161. {
  162. void *base = NULL;
  163. unsigned int i;
  164. for (i = 0; i < GDP_NODE_NB_BANK; i++) {
  165. if (gdp->node_list[i].top_field_paddr == val) {
  166. base = gdp->node_list[i].top_field;
  167. break;
  168. }
  169. if (gdp->node_list[i].btm_field_paddr == val) {
  170. base = gdp->node_list[i].btm_field;
  171. break;
  172. }
  173. }
  174. if (base)
  175. seq_printf(s, "\tVirt @: %p", base);
  176. }
  177. static void gdp_dbg_ppt(struct seq_file *s, int val)
  178. {
  179. if (val & GAM_GDP_PPT_IGNORE)
  180. seq_puts(s, "\tNot displayed on mixer!");
  181. }
  182. static void gdp_dbg_mst(struct seq_file *s, int val)
  183. {
  184. if (val & 1)
  185. seq_puts(s, "\tBUFFER UNDERFLOW!");
  186. }
  187. static int gdp_dbg_show(struct seq_file *s, void *data)
  188. {
  189. struct drm_info_node *node = s->private;
  190. struct sti_gdp *gdp = (struct sti_gdp *)node->info_ent->data;
  191. struct drm_plane *drm_plane = &gdp->plane.drm_plane;
  192. struct drm_crtc *crtc;
  193. drm_modeset_lock(&drm_plane->mutex, NULL);
  194. crtc = drm_plane->state->crtc;
  195. drm_modeset_unlock(&drm_plane->mutex);
  196. seq_printf(s, "%s: (vaddr = 0x%p)",
  197. sti_plane_to_str(&gdp->plane), gdp->regs);
  198. DBGFS_DUMP(GAM_GDP_CTL);
  199. gdp_dbg_ctl(s, readl(gdp->regs + GAM_GDP_CTL_OFFSET));
  200. DBGFS_DUMP(GAM_GDP_AGC);
  201. DBGFS_DUMP(GAM_GDP_VPO);
  202. gdp_dbg_vpo(s, readl(gdp->regs + GAM_GDP_VPO_OFFSET));
  203. DBGFS_DUMP(GAM_GDP_VPS);
  204. gdp_dbg_vps(s, readl(gdp->regs + GAM_GDP_VPS_OFFSET));
  205. DBGFS_DUMP(GAM_GDP_PML);
  206. DBGFS_DUMP(GAM_GDP_PMP);
  207. DBGFS_DUMP(GAM_GDP_SIZE);
  208. gdp_dbg_size(s, readl(gdp->regs + GAM_GDP_SIZE_OFFSET));
  209. DBGFS_DUMP(GAM_GDP_NVN);
  210. gdp_dbg_nvn(s, gdp, readl(gdp->regs + GAM_GDP_NVN_OFFSET));
  211. DBGFS_DUMP(GAM_GDP_KEY1);
  212. DBGFS_DUMP(GAM_GDP_KEY2);
  213. DBGFS_DUMP(GAM_GDP_PPT);
  214. gdp_dbg_ppt(s, readl(gdp->regs + GAM_GDP_PPT_OFFSET));
  215. DBGFS_DUMP(GAM_GDP_CML);
  216. DBGFS_DUMP(GAM_GDP_MST);
  217. gdp_dbg_mst(s, readl(gdp->regs + GAM_GDP_MST_OFFSET));
  218. seq_puts(s, "\n\n");
  219. if (!crtc)
  220. seq_puts(s, " Not connected to any DRM CRTC\n");
  221. else
  222. seq_printf(s, " Connected to DRM CRTC #%d (%s)\n",
  223. crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)));
  224. return 0;
  225. }
  226. static void gdp_node_dump_node(struct seq_file *s, struct sti_gdp_node *node)
  227. {
  228. seq_printf(s, "\t@:0x%p", node);
  229. seq_printf(s, "\n\tCTL 0x%08X", node->gam_gdp_ctl);
  230. gdp_dbg_ctl(s, node->gam_gdp_ctl);
  231. seq_printf(s, "\n\tAGC 0x%08X", node->gam_gdp_agc);
  232. seq_printf(s, "\n\tVPO 0x%08X", node->gam_gdp_vpo);
  233. gdp_dbg_vpo(s, node->gam_gdp_vpo);
  234. seq_printf(s, "\n\tVPS 0x%08X", node->gam_gdp_vps);
  235. gdp_dbg_vps(s, node->gam_gdp_vps);
  236. seq_printf(s, "\n\tPML 0x%08X", node->gam_gdp_pml);
  237. seq_printf(s, "\n\tPMP 0x%08X", node->gam_gdp_pmp);
  238. seq_printf(s, "\n\tSIZE 0x%08X", node->gam_gdp_size);
  239. gdp_dbg_size(s, node->gam_gdp_size);
  240. seq_printf(s, "\n\tNVN 0x%08X", node->gam_gdp_nvn);
  241. seq_printf(s, "\n\tKEY1 0x%08X", node->gam_gdp_key1);
  242. seq_printf(s, "\n\tKEY2 0x%08X", node->gam_gdp_key2);
  243. seq_printf(s, "\n\tPPT 0x%08X", node->gam_gdp_ppt);
  244. gdp_dbg_ppt(s, node->gam_gdp_ppt);
  245. seq_printf(s, "\n\tCML 0x%08X\n", node->gam_gdp_cml);
  246. }
  247. static int gdp_node_dbg_show(struct seq_file *s, void *arg)
  248. {
  249. struct drm_info_node *node = s->private;
  250. struct sti_gdp *gdp = (struct sti_gdp *)node->info_ent->data;
  251. unsigned int b;
  252. for (b = 0; b < GDP_NODE_NB_BANK; b++) {
  253. seq_printf(s, "\n%s[%d].top", sti_plane_to_str(&gdp->plane), b);
  254. gdp_node_dump_node(s, gdp->node_list[b].top_field);
  255. seq_printf(s, "\n%s[%d].btm", sti_plane_to_str(&gdp->plane), b);
  256. gdp_node_dump_node(s, gdp->node_list[b].btm_field);
  257. }
  258. return 0;
  259. }
  260. static struct drm_info_list gdp0_debugfs_files[] = {
  261. { "gdp0", gdp_dbg_show, 0, NULL },
  262. { "gdp0_node", gdp_node_dbg_show, 0, NULL },
  263. };
  264. static struct drm_info_list gdp1_debugfs_files[] = {
  265. { "gdp1", gdp_dbg_show, 0, NULL },
  266. { "gdp1_node", gdp_node_dbg_show, 0, NULL },
  267. };
  268. static struct drm_info_list gdp2_debugfs_files[] = {
  269. { "gdp2", gdp_dbg_show, 0, NULL },
  270. { "gdp2_node", gdp_node_dbg_show, 0, NULL },
  271. };
  272. static struct drm_info_list gdp3_debugfs_files[] = {
  273. { "gdp3", gdp_dbg_show, 0, NULL },
  274. { "gdp3_node", gdp_node_dbg_show, 0, NULL },
  275. };
  276. static int gdp_debugfs_init(struct sti_gdp *gdp, struct drm_minor *minor)
  277. {
  278. unsigned int i;
  279. struct drm_info_list *gdp_debugfs_files;
  280. int nb_files;
  281. switch (gdp->plane.desc) {
  282. case STI_GDP_0:
  283. gdp_debugfs_files = gdp0_debugfs_files;
  284. nb_files = ARRAY_SIZE(gdp0_debugfs_files);
  285. break;
  286. case STI_GDP_1:
  287. gdp_debugfs_files = gdp1_debugfs_files;
  288. nb_files = ARRAY_SIZE(gdp1_debugfs_files);
  289. break;
  290. case STI_GDP_2:
  291. gdp_debugfs_files = gdp2_debugfs_files;
  292. nb_files = ARRAY_SIZE(gdp2_debugfs_files);
  293. break;
  294. case STI_GDP_3:
  295. gdp_debugfs_files = gdp3_debugfs_files;
  296. nb_files = ARRAY_SIZE(gdp3_debugfs_files);
  297. break;
  298. default:
  299. return -EINVAL;
  300. }
  301. for (i = 0; i < nb_files; i++)
  302. gdp_debugfs_files[i].data = gdp;
  303. drm_debugfs_create_files(gdp_debugfs_files,
  304. nb_files,
  305. minor->debugfs_root, minor);
  306. return 0;
  307. }
  308. static int sti_gdp_fourcc2format(int fourcc)
  309. {
  310. switch (fourcc) {
  311. case DRM_FORMAT_XRGB8888:
  312. return GDP_RGB888_32;
  313. case DRM_FORMAT_XBGR8888:
  314. return GDP_XBGR8888;
  315. case DRM_FORMAT_ARGB8888:
  316. return GDP_ARGB8888;
  317. case DRM_FORMAT_ABGR8888:
  318. return GDP_ABGR8888;
  319. case DRM_FORMAT_ARGB4444:
  320. return GDP_ARGB4444;
  321. case DRM_FORMAT_ARGB1555:
  322. return GDP_ARGB1555;
  323. case DRM_FORMAT_RGB565:
  324. return GDP_RGB565;
  325. case DRM_FORMAT_RGB888:
  326. return GDP_RGB888;
  327. }
  328. return -1;
  329. }
  330. static int sti_gdp_get_alpharange(int format)
  331. {
  332. switch (format) {
  333. case GDP_ARGB8565:
  334. case GDP_ARGB8888:
  335. case GDP_ABGR8888:
  336. return GAM_GDP_ALPHARANGE_255;
  337. }
  338. return 0;
  339. }
  340. /**
  341. * sti_gdp_get_free_nodes
  342. * @gdp: gdp pointer
  343. *
  344. * Look for a GDP node list that is not currently read by the HW.
  345. *
  346. * RETURNS:
  347. * Pointer to the free GDP node list
  348. */
  349. static struct sti_gdp_node_list *sti_gdp_get_free_nodes(struct sti_gdp *gdp)
  350. {
  351. int hw_nvn;
  352. unsigned int i;
  353. hw_nvn = readl(gdp->regs + GAM_GDP_NVN_OFFSET);
  354. if (!hw_nvn)
  355. goto end;
  356. for (i = 0; i < GDP_NODE_NB_BANK; i++)
  357. if ((hw_nvn != gdp->node_list[i].btm_field_paddr) &&
  358. (hw_nvn != gdp->node_list[i].top_field_paddr))
  359. return &gdp->node_list[i];
  360. /* in hazardous cases restart with the first node */
  361. DRM_ERROR("inconsistent NVN for %s: 0x%08X\n",
  362. sti_plane_to_str(&gdp->plane), hw_nvn);
  363. end:
  364. return &gdp->node_list[0];
  365. }
  366. /**
  367. * sti_gdp_get_current_nodes
  368. * @gdp: gdp pointer
  369. *
  370. * Look for GDP nodes that are currently read by the HW.
  371. *
  372. * RETURNS:
  373. * Pointer to the current GDP node list
  374. */
  375. static
  376. struct sti_gdp_node_list *sti_gdp_get_current_nodes(struct sti_gdp *gdp)
  377. {
  378. int hw_nvn;
  379. unsigned int i;
  380. hw_nvn = readl(gdp->regs + GAM_GDP_NVN_OFFSET);
  381. if (!hw_nvn)
  382. goto end;
  383. for (i = 0; i < GDP_NODE_NB_BANK; i++)
  384. if ((hw_nvn == gdp->node_list[i].btm_field_paddr) ||
  385. (hw_nvn == gdp->node_list[i].top_field_paddr))
  386. return &gdp->node_list[i];
  387. end:
  388. DRM_DEBUG_DRIVER("Warning, NVN 0x%08X for %s does not match any node\n",
  389. hw_nvn, sti_plane_to_str(&gdp->plane));
  390. return NULL;
  391. }
  392. /**
  393. * sti_gdp_disable
  394. * @gdp: gdp pointer
  395. *
  396. * Disable a GDP.
  397. */
  398. static void sti_gdp_disable(struct sti_gdp *gdp)
  399. {
  400. unsigned int i;
  401. DRM_DEBUG_DRIVER("%s\n", sti_plane_to_str(&gdp->plane));
  402. /* Set the nodes as 'to be ignored on mixer' */
  403. for (i = 0; i < GDP_NODE_NB_BANK; i++) {
  404. gdp->node_list[i].top_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
  405. gdp->node_list[i].btm_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
  406. }
  407. if (sti_vtg_unregister_client(gdp->vtg, &gdp->vtg_field_nb))
  408. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  409. if (gdp->clk_pix)
  410. clk_disable_unprepare(gdp->clk_pix);
  411. gdp->plane.status = STI_PLANE_DISABLED;
  412. gdp->vtg = NULL;
  413. }
  414. /**
  415. * sti_gdp_field_cb
  416. * @nb: notifier block
  417. * @event: event message
  418. * @data: private data
  419. *
  420. * Handle VTG top field and bottom field event.
  421. *
  422. * RETURNS:
  423. * 0 on success.
  424. */
  425. static int sti_gdp_field_cb(struct notifier_block *nb,
  426. unsigned long event, void *data)
  427. {
  428. struct sti_gdp *gdp = container_of(nb, struct sti_gdp, vtg_field_nb);
  429. if (gdp->plane.status == STI_PLANE_FLUSHING) {
  430. /* disable need to be synchronize on vsync event */
  431. DRM_DEBUG_DRIVER("Vsync event received => disable %s\n",
  432. sti_plane_to_str(&gdp->plane));
  433. sti_gdp_disable(gdp);
  434. }
  435. switch (event) {
  436. case VTG_TOP_FIELD_EVENT:
  437. gdp->is_curr_top = true;
  438. break;
  439. case VTG_BOTTOM_FIELD_EVENT:
  440. gdp->is_curr_top = false;
  441. break;
  442. default:
  443. DRM_ERROR("unsupported event: %lu\n", event);
  444. break;
  445. }
  446. return 0;
  447. }
  448. static void sti_gdp_init(struct sti_gdp *gdp)
  449. {
  450. struct device_node *np = gdp->dev->of_node;
  451. dma_addr_t dma_addr;
  452. void *base;
  453. unsigned int i, size;
  454. /* Allocate all the nodes within a single memory page */
  455. size = sizeof(struct sti_gdp_node) *
  456. GDP_NODE_PER_FIELD * GDP_NODE_NB_BANK;
  457. base = dma_alloc_wc(gdp->dev, size, &dma_addr, GFP_KERNEL);
  458. if (!base) {
  459. DRM_ERROR("Failed to allocate memory for GDP node\n");
  460. return;
  461. }
  462. memset(base, 0, size);
  463. for (i = 0; i < GDP_NODE_NB_BANK; i++) {
  464. if (dma_addr & 0xF) {
  465. DRM_ERROR("Mem alignment failed\n");
  466. return;
  467. }
  468. gdp->node_list[i].top_field = base;
  469. gdp->node_list[i].top_field_paddr = dma_addr;
  470. DRM_DEBUG_DRIVER("node[%d].top_field=%p\n", i, base);
  471. base += sizeof(struct sti_gdp_node);
  472. dma_addr += sizeof(struct sti_gdp_node);
  473. if (dma_addr & 0xF) {
  474. DRM_ERROR("Mem alignment failed\n");
  475. return;
  476. }
  477. gdp->node_list[i].btm_field = base;
  478. gdp->node_list[i].btm_field_paddr = dma_addr;
  479. DRM_DEBUG_DRIVER("node[%d].btm_field=%p\n", i, base);
  480. base += sizeof(struct sti_gdp_node);
  481. dma_addr += sizeof(struct sti_gdp_node);
  482. }
  483. if (of_device_is_compatible(np, "st,stih407-compositor")) {
  484. /* GDP of STiH407 chip have its own pixel clock */
  485. char *clk_name;
  486. switch (gdp->plane.desc) {
  487. case STI_GDP_0:
  488. clk_name = "pix_gdp1";
  489. break;
  490. case STI_GDP_1:
  491. clk_name = "pix_gdp2";
  492. break;
  493. case STI_GDP_2:
  494. clk_name = "pix_gdp3";
  495. break;
  496. case STI_GDP_3:
  497. clk_name = "pix_gdp4";
  498. break;
  499. default:
  500. DRM_ERROR("GDP id not recognized\n");
  501. return;
  502. }
  503. gdp->clk_pix = devm_clk_get(gdp->dev, clk_name);
  504. if (IS_ERR(gdp->clk_pix))
  505. DRM_ERROR("Cannot get %s clock\n", clk_name);
  506. gdp->clk_main_parent = devm_clk_get(gdp->dev, "main_parent");
  507. if (IS_ERR(gdp->clk_main_parent))
  508. DRM_ERROR("Cannot get main_parent clock\n");
  509. gdp->clk_aux_parent = devm_clk_get(gdp->dev, "aux_parent");
  510. if (IS_ERR(gdp->clk_aux_parent))
  511. DRM_ERROR("Cannot get aux_parent clock\n");
  512. }
  513. }
  514. /**
  515. * sti_gdp_get_dst
  516. * @dev: device
  517. * @dst: requested destination size
  518. * @src: source size
  519. *
  520. * Return the cropped / clamped destination size
  521. *
  522. * RETURNS:
  523. * cropped / clamped destination size
  524. */
  525. static int sti_gdp_get_dst(struct device *dev, int dst, int src)
  526. {
  527. if (dst == src)
  528. return dst;
  529. if (dst < src) {
  530. dev_dbg(dev, "WARNING: GDP scale not supported, will crop\n");
  531. return dst;
  532. }
  533. dev_dbg(dev, "WARNING: GDP scale not supported, will clamp\n");
  534. return src;
  535. }
  536. static int sti_gdp_atomic_check(struct drm_plane *drm_plane,
  537. struct drm_atomic_state *state)
  538. {
  539. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  540. drm_plane);
  541. struct sti_plane *plane = to_sti_plane(drm_plane);
  542. struct sti_gdp *gdp = to_sti_gdp(plane);
  543. struct drm_crtc *crtc = new_plane_state->crtc;
  544. struct drm_framebuffer *fb = new_plane_state->fb;
  545. struct drm_crtc_state *crtc_state;
  546. struct sti_mixer *mixer;
  547. struct drm_display_mode *mode;
  548. int dst_x, dst_y, dst_w, dst_h;
  549. int src_x, src_y, src_w, src_h;
  550. int format;
  551. /* no need for further checks if the plane is being disabled */
  552. if (!crtc || !fb)
  553. return 0;
  554. mixer = to_sti_mixer(crtc);
  555. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  556. if (IS_ERR(crtc_state))
  557. return PTR_ERR(crtc_state);
  558. mode = &crtc_state->mode;
  559. dst_x = new_plane_state->crtc_x;
  560. dst_y = new_plane_state->crtc_y;
  561. dst_w = clamp_val(new_plane_state->crtc_w, 0, mode->hdisplay - dst_x);
  562. dst_h = clamp_val(new_plane_state->crtc_h, 0, mode->vdisplay - dst_y);
  563. /* src_x are in 16.16 format */
  564. src_x = new_plane_state->src_x >> 16;
  565. src_y = new_plane_state->src_y >> 16;
  566. src_w = clamp_val(new_plane_state->src_w >> 16, 0,
  567. GAM_GDP_SIZE_MAX_WIDTH);
  568. src_h = clamp_val(new_plane_state->src_h >> 16, 0,
  569. GAM_GDP_SIZE_MAX_HEIGHT);
  570. format = sti_gdp_fourcc2format(fb->format->format);
  571. if (format == -1) {
  572. DRM_ERROR("Format not supported by GDP %.4s\n",
  573. (char *)&fb->format->format);
  574. return -EINVAL;
  575. }
  576. if (!drm_fb_dma_get_gem_obj(fb, 0)) {
  577. DRM_ERROR("Can't get DMA GEM object for fb\n");
  578. return -EINVAL;
  579. }
  580. /* Set gdp clock */
  581. if (mode->clock && gdp->clk_pix) {
  582. struct clk *clkp;
  583. int rate = mode->clock * 1000;
  584. int res;
  585. /*
  586. * According to the mixer used, the gdp pixel clock
  587. * should have a different parent clock.
  588. */
  589. if (mixer->id == STI_MIXER_MAIN)
  590. clkp = gdp->clk_main_parent;
  591. else
  592. clkp = gdp->clk_aux_parent;
  593. if (clkp)
  594. clk_set_parent(gdp->clk_pix, clkp);
  595. res = clk_set_rate(gdp->clk_pix, rate);
  596. if (res < 0) {
  597. DRM_ERROR("Cannot set rate (%dHz) for gdp\n",
  598. rate);
  599. return -EINVAL;
  600. }
  601. }
  602. DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
  603. crtc->base.id, sti_mixer_to_str(mixer),
  604. drm_plane->base.id, sti_plane_to_str(plane));
  605. DRM_DEBUG_KMS("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
  606. sti_plane_to_str(plane),
  607. dst_w, dst_h, dst_x, dst_y,
  608. src_w, src_h, src_x, src_y);
  609. return 0;
  610. }
  611. static void sti_gdp_atomic_update(struct drm_plane *drm_plane,
  612. struct drm_atomic_state *state)
  613. {
  614. struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state,
  615. drm_plane);
  616. struct drm_plane_state *newstate = drm_atomic_get_new_plane_state(state,
  617. drm_plane);
  618. struct sti_plane *plane = to_sti_plane(drm_plane);
  619. struct sti_gdp *gdp = to_sti_gdp(plane);
  620. struct drm_crtc *crtc = newstate->crtc;
  621. struct drm_framebuffer *fb = newstate->fb;
  622. struct drm_display_mode *mode;
  623. int dst_x, dst_y, dst_w, dst_h;
  624. int src_x, src_y, src_w, src_h;
  625. struct drm_gem_dma_object *dma_obj;
  626. struct sti_gdp_node_list *list;
  627. struct sti_gdp_node_list *curr_list;
  628. struct sti_gdp_node *top_field, *btm_field;
  629. u32 dma_updated_top;
  630. u32 dma_updated_btm;
  631. int format;
  632. unsigned int bpp;
  633. u32 ydo, xdo, yds, xds;
  634. if (!crtc || !fb)
  635. return;
  636. if ((oldstate->fb == newstate->fb) &&
  637. (oldstate->crtc_x == newstate->crtc_x) &&
  638. (oldstate->crtc_y == newstate->crtc_y) &&
  639. (oldstate->crtc_w == newstate->crtc_w) &&
  640. (oldstate->crtc_h == newstate->crtc_h) &&
  641. (oldstate->src_x == newstate->src_x) &&
  642. (oldstate->src_y == newstate->src_y) &&
  643. (oldstate->src_w == newstate->src_w) &&
  644. (oldstate->src_h == newstate->src_h)) {
  645. /* No change since last update, do not post cmd */
  646. DRM_DEBUG_DRIVER("No change, not posting cmd\n");
  647. plane->status = STI_PLANE_UPDATED;
  648. return;
  649. }
  650. if (!gdp->vtg) {
  651. struct sti_compositor *compo = dev_get_drvdata(gdp->dev);
  652. struct sti_mixer *mixer = to_sti_mixer(crtc);
  653. /* Register gdp callback */
  654. gdp->vtg = compo->vtg[mixer->id];
  655. sti_vtg_register_client(gdp->vtg, &gdp->vtg_field_nb, crtc);
  656. clk_prepare_enable(gdp->clk_pix);
  657. }
  658. mode = &crtc->mode;
  659. dst_x = newstate->crtc_x;
  660. dst_y = newstate->crtc_y;
  661. dst_w = clamp_val(newstate->crtc_w, 0, mode->hdisplay - dst_x);
  662. dst_h = clamp_val(newstate->crtc_h, 0, mode->vdisplay - dst_y);
  663. /* src_x are in 16.16 format */
  664. src_x = newstate->src_x >> 16;
  665. src_y = newstate->src_y >> 16;
  666. src_w = clamp_val(newstate->src_w >> 16, 0, GAM_GDP_SIZE_MAX_WIDTH);
  667. src_h = clamp_val(newstate->src_h >> 16, 0, GAM_GDP_SIZE_MAX_HEIGHT);
  668. list = sti_gdp_get_free_nodes(gdp);
  669. top_field = list->top_field;
  670. btm_field = list->btm_field;
  671. dev_dbg(gdp->dev, "%s %s top_node:0x%p btm_node:0x%p\n", __func__,
  672. sti_plane_to_str(plane), top_field, btm_field);
  673. /* build the top field */
  674. top_field->gam_gdp_agc = GAM_GDP_AGC_FULL_RANGE;
  675. top_field->gam_gdp_ctl = WAIT_NEXT_VSYNC;
  676. format = sti_gdp_fourcc2format(fb->format->format);
  677. top_field->gam_gdp_ctl |= format;
  678. top_field->gam_gdp_ctl |= sti_gdp_get_alpharange(format);
  679. top_field->gam_gdp_ppt &= ~GAM_GDP_PPT_IGNORE;
  680. dma_obj = drm_fb_dma_get_gem_obj(fb, 0);
  681. DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
  682. (char *)&fb->format->format,
  683. (unsigned long) dma_obj->dma_addr);
  684. /* pixel memory location */
  685. bpp = fb->format->cpp[0];
  686. top_field->gam_gdp_pml = (u32) dma_obj->dma_addr + fb->offsets[0];
  687. top_field->gam_gdp_pml += src_x * bpp;
  688. top_field->gam_gdp_pml += src_y * fb->pitches[0];
  689. /* output parameters (clamped / cropped) */
  690. dst_w = sti_gdp_get_dst(gdp->dev, dst_w, src_w);
  691. dst_h = sti_gdp_get_dst(gdp->dev, dst_h, src_h);
  692. ydo = sti_vtg_get_line_number(*mode, dst_y);
  693. yds = sti_vtg_get_line_number(*mode, dst_y + dst_h - 1);
  694. xdo = sti_vtg_get_pixel_number(*mode, dst_x);
  695. xds = sti_vtg_get_pixel_number(*mode, dst_x + dst_w - 1);
  696. top_field->gam_gdp_vpo = (ydo << 16) | xdo;
  697. top_field->gam_gdp_vps = (yds << 16) | xds;
  698. /* input parameters */
  699. src_w = dst_w;
  700. top_field->gam_gdp_pmp = fb->pitches[0];
  701. top_field->gam_gdp_size = src_h << 16 | src_w;
  702. /* Same content and chained together */
  703. memcpy(btm_field, top_field, sizeof(*btm_field));
  704. top_field->gam_gdp_nvn = list->btm_field_paddr;
  705. btm_field->gam_gdp_nvn = list->top_field_paddr;
  706. /* Interlaced mode */
  707. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  708. btm_field->gam_gdp_pml = top_field->gam_gdp_pml +
  709. fb->pitches[0];
  710. /* Update the NVN field of the 'right' field of the current GDP node
  711. * (being used by the HW) with the address of the updated ('free') top
  712. * field GDP node.
  713. * - In interlaced mode the 'right' field is the bottom field as we
  714. * update frames starting from their top field
  715. * - In progressive mode, we update both bottom and top fields which
  716. * are equal nodes.
  717. * At the next VSYNC, the updated node list will be used by the HW.
  718. */
  719. curr_list = sti_gdp_get_current_nodes(gdp);
  720. dma_updated_top = list->top_field_paddr;
  721. dma_updated_btm = list->btm_field_paddr;
  722. dev_dbg(gdp->dev, "Current NVN:0x%X\n",
  723. readl(gdp->regs + GAM_GDP_NVN_OFFSET));
  724. dev_dbg(gdp->dev, "Posted buff: %lx current buff: %x\n",
  725. (unsigned long) dma_obj->dma_addr,
  726. readl(gdp->regs + GAM_GDP_PML_OFFSET));
  727. if (!curr_list) {
  728. /* First update or invalid node should directly write in the
  729. * hw register */
  730. DRM_DEBUG_DRIVER("%s first update (or invalid node)\n",
  731. sti_plane_to_str(plane));
  732. writel(gdp->is_curr_top ?
  733. dma_updated_btm : dma_updated_top,
  734. gdp->regs + GAM_GDP_NVN_OFFSET);
  735. goto end;
  736. }
  737. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  738. if (gdp->is_curr_top) {
  739. /* Do not update in the middle of the frame, but
  740. * postpone the update after the bottom field has
  741. * been displayed */
  742. curr_list->btm_field->gam_gdp_nvn = dma_updated_top;
  743. } else {
  744. /* Direct update to avoid one frame delay */
  745. writel(dma_updated_top,
  746. gdp->regs + GAM_GDP_NVN_OFFSET);
  747. }
  748. } else {
  749. /* Direct update for progressive to avoid one frame delay */
  750. writel(dma_updated_top, gdp->regs + GAM_GDP_NVN_OFFSET);
  751. }
  752. end:
  753. sti_plane_update_fps(plane, true, false);
  754. plane->status = STI_PLANE_UPDATED;
  755. }
  756. static void sti_gdp_atomic_disable(struct drm_plane *drm_plane,
  757. struct drm_atomic_state *state)
  758. {
  759. struct drm_plane_state *oldstate = drm_atomic_get_old_plane_state(state,
  760. drm_plane);
  761. struct sti_plane *plane = to_sti_plane(drm_plane);
  762. if (!oldstate->crtc) {
  763. DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
  764. drm_plane->base.id);
  765. return;
  766. }
  767. DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
  768. oldstate->crtc->base.id,
  769. sti_mixer_to_str(to_sti_mixer(oldstate->crtc)),
  770. drm_plane->base.id, sti_plane_to_str(plane));
  771. plane->status = STI_PLANE_DISABLING;
  772. }
  773. static const struct drm_plane_helper_funcs sti_gdp_helpers_funcs = {
  774. .atomic_check = sti_gdp_atomic_check,
  775. .atomic_update = sti_gdp_atomic_update,
  776. .atomic_disable = sti_gdp_atomic_disable,
  777. };
  778. static int sti_gdp_late_register(struct drm_plane *drm_plane)
  779. {
  780. struct sti_plane *plane = to_sti_plane(drm_plane);
  781. struct sti_gdp *gdp = to_sti_gdp(plane);
  782. return gdp_debugfs_init(gdp, drm_plane->dev->primary);
  783. }
  784. static const struct drm_plane_funcs sti_gdp_plane_helpers_funcs = {
  785. .update_plane = drm_atomic_helper_update_plane,
  786. .disable_plane = drm_atomic_helper_disable_plane,
  787. .destroy = drm_plane_cleanup,
  788. .reset = drm_atomic_helper_plane_reset,
  789. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  790. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  791. .late_register = sti_gdp_late_register,
  792. };
  793. struct drm_plane *sti_gdp_create(struct drm_device *drm_dev,
  794. struct device *dev, int desc,
  795. void __iomem *baseaddr,
  796. unsigned int possible_crtcs,
  797. enum drm_plane_type type)
  798. {
  799. struct sti_gdp *gdp;
  800. int res;
  801. gdp = devm_kzalloc(dev, sizeof(*gdp), GFP_KERNEL);
  802. if (!gdp) {
  803. DRM_ERROR("Failed to allocate memory for GDP\n");
  804. return NULL;
  805. }
  806. gdp->dev = dev;
  807. gdp->regs = baseaddr;
  808. gdp->plane.desc = desc;
  809. gdp->plane.status = STI_PLANE_DISABLED;
  810. gdp->vtg_field_nb.notifier_call = sti_gdp_field_cb;
  811. sti_gdp_init(gdp);
  812. res = drm_universal_plane_init(drm_dev, &gdp->plane.drm_plane,
  813. possible_crtcs,
  814. &sti_gdp_plane_helpers_funcs,
  815. gdp_supported_formats,
  816. ARRAY_SIZE(gdp_supported_formats),
  817. NULL, type, NULL);
  818. if (res) {
  819. DRM_ERROR("Failed to initialize universal plane\n");
  820. goto err;
  821. }
  822. drm_plane_helper_add(&gdp->plane.drm_plane, &sti_gdp_helpers_funcs);
  823. sti_plane_init_property(&gdp->plane, type);
  824. return &gdp->plane.drm_plane;
  825. err:
  826. devm_kfree(dev, gdp);
  827. return NULL;
  828. }