sti_gdp.c 25 KB

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