sti_gdp.c 24 KB

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