sti_gdp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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/clk.h>
  9. #include <linux/dma-mapping.h>
  10. #include "sti_compositor.h"
  11. #include "sti_gdp.h"
  12. #include "sti_layer.h"
  13. #include "sti_vtg.h"
  14. #define ENA_COLOR_FILL BIT(8)
  15. #define WAIT_NEXT_VSYNC BIT(31)
  16. /* GDP color formats */
  17. #define GDP_RGB565 0x00
  18. #define GDP_RGB888 0x01
  19. #define GDP_RGB888_32 0x02
  20. #define GDP_ARGB8565 0x04
  21. #define GDP_ARGB8888 0x05
  22. #define GDP_ARGB1555 0x06
  23. #define GDP_ARGB4444 0x07
  24. #define GDP_CLUT8 0x0B
  25. #define GDP_YCBR888 0x10
  26. #define GDP_YCBR422R 0x12
  27. #define GDP_AYCBR8888 0x15
  28. #define GAM_GDP_CTL_OFFSET 0x00
  29. #define GAM_GDP_AGC_OFFSET 0x04
  30. #define GAM_GDP_VPO_OFFSET 0x0C
  31. #define GAM_GDP_VPS_OFFSET 0x10
  32. #define GAM_GDP_PML_OFFSET 0x14
  33. #define GAM_GDP_PMP_OFFSET 0x18
  34. #define GAM_GDP_SIZE_OFFSET 0x1C
  35. #define GAM_GDP_NVN_OFFSET 0x24
  36. #define GAM_GDP_KEY1_OFFSET 0x28
  37. #define GAM_GDP_KEY2_OFFSET 0x2C
  38. #define GAM_GDP_PPT_OFFSET 0x34
  39. #define GAM_GDP_CML_OFFSET 0x3C
  40. #define GAM_GDP_MST_OFFSET 0x68
  41. #define GAM_GDP_ALPHARANGE_255 BIT(5)
  42. #define GAM_GDP_AGC_FULL_RANGE 0x00808080
  43. #define GAM_GDP_PPT_IGNORE (BIT(1) | BIT(0))
  44. #define GAM_GDP_SIZE_MAX 0x7FF
  45. #define GDP_NODE_NB_BANK 2
  46. #define GDP_NODE_PER_FIELD 2
  47. struct sti_gdp_node {
  48. u32 gam_gdp_ctl;
  49. u32 gam_gdp_agc;
  50. u32 reserved1;
  51. u32 gam_gdp_vpo;
  52. u32 gam_gdp_vps;
  53. u32 gam_gdp_pml;
  54. u32 gam_gdp_pmp;
  55. u32 gam_gdp_size;
  56. u32 reserved2;
  57. u32 gam_gdp_nvn;
  58. u32 gam_gdp_key1;
  59. u32 gam_gdp_key2;
  60. u32 reserved3;
  61. u32 gam_gdp_ppt;
  62. u32 reserved4;
  63. u32 gam_gdp_cml;
  64. };
  65. struct sti_gdp_node_list {
  66. struct sti_gdp_node *top_field;
  67. dma_addr_t top_field_paddr;
  68. struct sti_gdp_node *btm_field;
  69. dma_addr_t btm_field_paddr;
  70. };
  71. /**
  72. * STI GDP structure
  73. *
  74. * @layer: layer structure
  75. * @clk_pix: pixel clock for the current gdp
  76. * @clk_main_parent: gdp parent clock if main path used
  77. * @clk_aux_parent: gdp parent clock if aux path used
  78. * @vtg_field_nb: callback for VTG FIELD (top or bottom) notification
  79. * @is_curr_top: true if the current node processed is the top field
  80. * @node_list: array of node list
  81. */
  82. struct sti_gdp {
  83. struct sti_layer layer;
  84. struct clk *clk_pix;
  85. struct clk *clk_main_parent;
  86. struct clk *clk_aux_parent;
  87. struct notifier_block vtg_field_nb;
  88. bool is_curr_top;
  89. struct sti_gdp_node_list node_list[GDP_NODE_NB_BANK];
  90. };
  91. #define to_sti_gdp(x) container_of(x, struct sti_gdp, layer)
  92. static const uint32_t gdp_supported_formats[] = {
  93. DRM_FORMAT_XRGB8888,
  94. DRM_FORMAT_ARGB8888,
  95. DRM_FORMAT_ARGB4444,
  96. DRM_FORMAT_ARGB1555,
  97. DRM_FORMAT_RGB565,
  98. DRM_FORMAT_RGB888,
  99. DRM_FORMAT_AYUV,
  100. DRM_FORMAT_YUV444,
  101. DRM_FORMAT_VYUY,
  102. DRM_FORMAT_C8,
  103. };
  104. static const uint32_t *sti_gdp_get_formats(struct sti_layer *layer)
  105. {
  106. return gdp_supported_formats;
  107. }
  108. static unsigned int sti_gdp_get_nb_formats(struct sti_layer *layer)
  109. {
  110. return ARRAY_SIZE(gdp_supported_formats);
  111. }
  112. static int sti_gdp_fourcc2format(int fourcc)
  113. {
  114. switch (fourcc) {
  115. case DRM_FORMAT_XRGB8888:
  116. return GDP_RGB888_32;
  117. case DRM_FORMAT_ARGB8888:
  118. return GDP_ARGB8888;
  119. case DRM_FORMAT_ARGB4444:
  120. return GDP_ARGB4444;
  121. case DRM_FORMAT_ARGB1555:
  122. return GDP_ARGB1555;
  123. case DRM_FORMAT_RGB565:
  124. return GDP_RGB565;
  125. case DRM_FORMAT_RGB888:
  126. return GDP_RGB888;
  127. case DRM_FORMAT_AYUV:
  128. return GDP_AYCBR8888;
  129. case DRM_FORMAT_YUV444:
  130. return GDP_YCBR888;
  131. case DRM_FORMAT_VYUY:
  132. return GDP_YCBR422R;
  133. case DRM_FORMAT_C8:
  134. return GDP_CLUT8;
  135. }
  136. return -1;
  137. }
  138. static int sti_gdp_get_alpharange(int format)
  139. {
  140. switch (format) {
  141. case GDP_ARGB8565:
  142. case GDP_ARGB8888:
  143. case GDP_AYCBR8888:
  144. return GAM_GDP_ALPHARANGE_255;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * sti_gdp_get_free_nodes
  150. * @layer: gdp layer
  151. *
  152. * Look for a GDP node list that is not currently read by the HW.
  153. *
  154. * RETURNS:
  155. * Pointer to the free GDP node list
  156. */
  157. static struct sti_gdp_node_list *sti_gdp_get_free_nodes(struct sti_layer *layer)
  158. {
  159. int hw_nvn;
  160. struct sti_gdp *gdp = to_sti_gdp(layer);
  161. unsigned int i;
  162. hw_nvn = readl(layer->regs + GAM_GDP_NVN_OFFSET);
  163. if (!hw_nvn)
  164. goto end;
  165. for (i = 0; i < GDP_NODE_NB_BANK; i++)
  166. if ((hw_nvn != gdp->node_list[i].btm_field_paddr) &&
  167. (hw_nvn != gdp->node_list[i].top_field_paddr))
  168. return &gdp->node_list[i];
  169. /* in hazardious cases restart with the first node */
  170. DRM_ERROR("inconsistent NVN for %s: 0x%08X\n",
  171. sti_layer_to_str(layer), hw_nvn);
  172. end:
  173. return &gdp->node_list[0];
  174. }
  175. /**
  176. * sti_gdp_get_current_nodes
  177. * @layer: GDP layer
  178. *
  179. * Look for GDP nodes that are currently read by the HW.
  180. *
  181. * RETURNS:
  182. * Pointer to the current GDP node list
  183. */
  184. static
  185. struct sti_gdp_node_list *sti_gdp_get_current_nodes(struct sti_layer *layer)
  186. {
  187. int hw_nvn;
  188. struct sti_gdp *gdp = to_sti_gdp(layer);
  189. unsigned int i;
  190. hw_nvn = readl(layer->regs + GAM_GDP_NVN_OFFSET);
  191. if (!hw_nvn)
  192. goto end;
  193. for (i = 0; i < GDP_NODE_NB_BANK; i++)
  194. if ((hw_nvn == gdp->node_list[i].btm_field_paddr) ||
  195. (hw_nvn == gdp->node_list[i].top_field_paddr))
  196. return &gdp->node_list[i];
  197. end:
  198. DRM_DEBUG_DRIVER("Warning, NVN 0x%08X for %s does not match any node\n",
  199. hw_nvn, sti_layer_to_str(layer));
  200. return NULL;
  201. }
  202. /**
  203. * sti_gdp_prepare_layer
  204. * @lay: gdp layer
  205. * @first_prepare: true if it is the first time this function is called
  206. *
  207. * Update the free GDP node list according to the layer properties.
  208. *
  209. * RETURNS:
  210. * 0 on success.
  211. */
  212. static int sti_gdp_prepare_layer(struct sti_layer *layer, bool first_prepare)
  213. {
  214. struct sti_gdp_node_list *list;
  215. struct sti_gdp_node *top_field, *btm_field;
  216. struct drm_display_mode *mode = layer->mode;
  217. struct device *dev = layer->dev;
  218. struct sti_gdp *gdp = to_sti_gdp(layer);
  219. struct sti_compositor *compo = dev_get_drvdata(dev);
  220. int format;
  221. unsigned int depth, bpp;
  222. int rate = mode->clock * 1000;
  223. int res;
  224. u32 ydo, xdo, yds, xds;
  225. list = sti_gdp_get_free_nodes(layer);
  226. top_field = list->top_field;
  227. btm_field = list->btm_field;
  228. dev_dbg(dev, "%s %s top_node:0x%p btm_node:0x%p\n", __func__,
  229. sti_layer_to_str(layer), top_field, btm_field);
  230. /* Build the top field from layer params */
  231. top_field->gam_gdp_agc = GAM_GDP_AGC_FULL_RANGE;
  232. top_field->gam_gdp_ctl = WAIT_NEXT_VSYNC;
  233. format = sti_gdp_fourcc2format(layer->format);
  234. if (format == -1) {
  235. DRM_ERROR("Format not supported by GDP %.4s\n",
  236. (char *)&layer->format);
  237. return 1;
  238. }
  239. top_field->gam_gdp_ctl |= format;
  240. top_field->gam_gdp_ctl |= sti_gdp_get_alpharange(format);
  241. top_field->gam_gdp_ppt &= ~GAM_GDP_PPT_IGNORE;
  242. /* pixel memory location */
  243. drm_fb_get_bpp_depth(layer->format, &depth, &bpp);
  244. top_field->gam_gdp_pml = (u32) layer->paddr + layer->offsets[0];
  245. top_field->gam_gdp_pml += layer->src_x * (bpp >> 3);
  246. top_field->gam_gdp_pml += layer->src_y * layer->pitches[0];
  247. /* input parameters */
  248. top_field->gam_gdp_pmp = layer->pitches[0];
  249. top_field->gam_gdp_size =
  250. clamp_val(layer->src_h, 0, GAM_GDP_SIZE_MAX) << 16 |
  251. clamp_val(layer->src_w, 0, GAM_GDP_SIZE_MAX);
  252. /* output parameters */
  253. ydo = sti_vtg_get_line_number(*mode, layer->dst_y);
  254. yds = sti_vtg_get_line_number(*mode, layer->dst_y + layer->dst_h - 1);
  255. xdo = sti_vtg_get_pixel_number(*mode, layer->dst_x);
  256. xds = sti_vtg_get_pixel_number(*mode, layer->dst_x + layer->dst_w - 1);
  257. top_field->gam_gdp_vpo = (ydo << 16) | xdo;
  258. top_field->gam_gdp_vps = (yds << 16) | xds;
  259. /* Same content and chained together */
  260. memcpy(btm_field, top_field, sizeof(*btm_field));
  261. top_field->gam_gdp_nvn = list->btm_field_paddr;
  262. btm_field->gam_gdp_nvn = list->top_field_paddr;
  263. /* Interlaced mode */
  264. if (layer->mode->flags & DRM_MODE_FLAG_INTERLACE)
  265. btm_field->gam_gdp_pml = top_field->gam_gdp_pml +
  266. layer->pitches[0];
  267. if (first_prepare) {
  268. /* Register gdp callback */
  269. if (sti_vtg_register_client(layer->mixer_id == STI_MIXER_MAIN ?
  270. compo->vtg_main : compo->vtg_aux,
  271. &gdp->vtg_field_nb, layer->mixer_id)) {
  272. DRM_ERROR("Cannot register VTG notifier\n");
  273. return 1;
  274. }
  275. /* Set and enable gdp clock */
  276. if (gdp->clk_pix) {
  277. struct clk *clkp;
  278. /* According to the mixer used, the gdp pixel clock
  279. * should have a different parent clock. */
  280. if (layer->mixer_id == STI_MIXER_MAIN)
  281. clkp = gdp->clk_main_parent;
  282. else
  283. clkp = gdp->clk_aux_parent;
  284. if (clkp)
  285. clk_set_parent(gdp->clk_pix, clkp);
  286. res = clk_set_rate(gdp->clk_pix, rate);
  287. if (res < 0) {
  288. DRM_ERROR("Cannot set rate (%dHz) for gdp\n",
  289. rate);
  290. return 1;
  291. }
  292. if (clk_prepare_enable(gdp->clk_pix)) {
  293. DRM_ERROR("Failed to prepare/enable gdp\n");
  294. return 1;
  295. }
  296. }
  297. }
  298. return 0;
  299. }
  300. /**
  301. * sti_gdp_commit_layer
  302. * @lay: gdp layer
  303. *
  304. * Update the NVN field of the 'right' field of the current GDP node (being
  305. * used by the HW) with the address of the updated ('free') top field GDP node.
  306. * - In interlaced mode the 'right' field is the bottom field as we update
  307. * frames starting from their top field
  308. * - In progressive mode, we update both bottom and top fields which are
  309. * equal nodes.
  310. * At the next VSYNC, the updated node list will be used by the HW.
  311. *
  312. * RETURNS:
  313. * 0 on success.
  314. */
  315. static int sti_gdp_commit_layer(struct sti_layer *layer)
  316. {
  317. struct sti_gdp_node_list *updated_list = sti_gdp_get_free_nodes(layer);
  318. struct sti_gdp_node *updated_top_node = updated_list->top_field;
  319. struct sti_gdp_node *updated_btm_node = updated_list->btm_field;
  320. struct sti_gdp *gdp = to_sti_gdp(layer);
  321. u32 dma_updated_top = updated_list->top_field_paddr;
  322. u32 dma_updated_btm = updated_list->btm_field_paddr;
  323. struct sti_gdp_node_list *curr_list = sti_gdp_get_current_nodes(layer);
  324. dev_dbg(layer->dev, "%s %s top/btm_node:0x%p/0x%p\n", __func__,
  325. sti_layer_to_str(layer),
  326. updated_top_node, updated_btm_node);
  327. dev_dbg(layer->dev, "Current NVN:0x%X\n",
  328. readl(layer->regs + GAM_GDP_NVN_OFFSET));
  329. dev_dbg(layer->dev, "Posted buff: %lx current buff: %x\n",
  330. (unsigned long)layer->paddr,
  331. readl(layer->regs + GAM_GDP_PML_OFFSET));
  332. if (curr_list == NULL) {
  333. /* First update or invalid node should directly write in the
  334. * hw register */
  335. DRM_DEBUG_DRIVER("%s first update (or invalid node)",
  336. sti_layer_to_str(layer));
  337. writel(gdp->is_curr_top == true ?
  338. dma_updated_btm : dma_updated_top,
  339. layer->regs + GAM_GDP_NVN_OFFSET);
  340. return 0;
  341. }
  342. if (layer->mode->flags & DRM_MODE_FLAG_INTERLACE) {
  343. if (gdp->is_curr_top == true) {
  344. /* Do not update in the middle of the frame, but
  345. * postpone the update after the bottom field has
  346. * been displayed */
  347. curr_list->btm_field->gam_gdp_nvn = dma_updated_top;
  348. } else {
  349. /* Direct update to avoid one frame delay */
  350. writel(dma_updated_top,
  351. layer->regs + GAM_GDP_NVN_OFFSET);
  352. }
  353. } else {
  354. /* Direct update for progressive to avoid one frame delay */
  355. writel(dma_updated_top, layer->regs + GAM_GDP_NVN_OFFSET);
  356. }
  357. return 0;
  358. }
  359. /**
  360. * sti_gdp_disable_layer
  361. * @lay: gdp layer
  362. *
  363. * Disable a GDP.
  364. *
  365. * RETURNS:
  366. * 0 on success.
  367. */
  368. static int sti_gdp_disable_layer(struct sti_layer *layer)
  369. {
  370. unsigned int i;
  371. struct sti_gdp *gdp = to_sti_gdp(layer);
  372. struct sti_compositor *compo = dev_get_drvdata(layer->dev);
  373. DRM_DEBUG_DRIVER("%s\n", sti_layer_to_str(layer));
  374. /* Set the nodes as 'to be ignored on mixer' */
  375. for (i = 0; i < GDP_NODE_NB_BANK; i++) {
  376. gdp->node_list[i].top_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
  377. gdp->node_list[i].btm_field->gam_gdp_ppt |= GAM_GDP_PPT_IGNORE;
  378. }
  379. if (sti_vtg_unregister_client(layer->mixer_id == STI_MIXER_MAIN ?
  380. compo->vtg_main : compo->vtg_aux, &gdp->vtg_field_nb))
  381. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  382. if (gdp->clk_pix)
  383. clk_disable_unprepare(gdp->clk_pix);
  384. return 0;
  385. }
  386. /**
  387. * sti_gdp_field_cb
  388. * @nb: notifier block
  389. * @event: event message
  390. * @data: private data
  391. *
  392. * Handle VTG top field and bottom field event.
  393. *
  394. * RETURNS:
  395. * 0 on success.
  396. */
  397. int sti_gdp_field_cb(struct notifier_block *nb,
  398. unsigned long event, void *data)
  399. {
  400. struct sti_gdp *gdp = container_of(nb, struct sti_gdp, vtg_field_nb);
  401. switch (event) {
  402. case VTG_TOP_FIELD_EVENT:
  403. gdp->is_curr_top = true;
  404. break;
  405. case VTG_BOTTOM_FIELD_EVENT:
  406. gdp->is_curr_top = false;
  407. break;
  408. default:
  409. DRM_ERROR("unsupported event: %lu\n", event);
  410. break;
  411. }
  412. return 0;
  413. }
  414. static void sti_gdp_init(struct sti_layer *layer)
  415. {
  416. struct sti_gdp *gdp = to_sti_gdp(layer);
  417. struct device_node *np = layer->dev->of_node;
  418. dma_addr_t dma_addr;
  419. void *base;
  420. unsigned int i, size;
  421. /* Allocate all the nodes within a single memory page */
  422. size = sizeof(struct sti_gdp_node) *
  423. GDP_NODE_PER_FIELD * GDP_NODE_NB_BANK;
  424. base = dma_alloc_writecombine(layer->dev,
  425. size, &dma_addr, GFP_KERNEL | GFP_DMA);
  426. if (!base) {
  427. DRM_ERROR("Failed to allocate memory for GDP node\n");
  428. return;
  429. }
  430. memset(base, 0, size);
  431. for (i = 0; i < GDP_NODE_NB_BANK; i++) {
  432. if (dma_addr & 0xF) {
  433. DRM_ERROR("Mem alignment failed\n");
  434. return;
  435. }
  436. gdp->node_list[i].top_field = base;
  437. gdp->node_list[i].top_field_paddr = dma_addr;
  438. DRM_DEBUG_DRIVER("node[%d].top_field=%p\n", i, base);
  439. base += sizeof(struct sti_gdp_node);
  440. dma_addr += sizeof(struct sti_gdp_node);
  441. if (dma_addr & 0xF) {
  442. DRM_ERROR("Mem alignment failed\n");
  443. return;
  444. }
  445. gdp->node_list[i].btm_field = base;
  446. gdp->node_list[i].btm_field_paddr = dma_addr;
  447. DRM_DEBUG_DRIVER("node[%d].btm_field=%p\n", i, base);
  448. base += sizeof(struct sti_gdp_node);
  449. dma_addr += sizeof(struct sti_gdp_node);
  450. }
  451. if (of_device_is_compatible(np, "st,stih407-compositor")) {
  452. /* GDP of STiH407 chip have its own pixel clock */
  453. char *clk_name;
  454. switch (layer->desc) {
  455. case STI_GDP_0:
  456. clk_name = "pix_gdp1";
  457. break;
  458. case STI_GDP_1:
  459. clk_name = "pix_gdp2";
  460. break;
  461. case STI_GDP_2:
  462. clk_name = "pix_gdp3";
  463. break;
  464. case STI_GDP_3:
  465. clk_name = "pix_gdp4";
  466. break;
  467. default:
  468. DRM_ERROR("GDP id not recognized\n");
  469. return;
  470. }
  471. gdp->clk_pix = devm_clk_get(layer->dev, clk_name);
  472. if (IS_ERR(gdp->clk_pix))
  473. DRM_ERROR("Cannot get %s clock\n", clk_name);
  474. gdp->clk_main_parent = devm_clk_get(layer->dev, "main_parent");
  475. if (IS_ERR(gdp->clk_main_parent))
  476. DRM_ERROR("Cannot get main_parent clock\n");
  477. gdp->clk_aux_parent = devm_clk_get(layer->dev, "aux_parent");
  478. if (IS_ERR(gdp->clk_aux_parent))
  479. DRM_ERROR("Cannot get aux_parent clock\n");
  480. }
  481. }
  482. static const struct sti_layer_funcs gdp_ops = {
  483. .get_formats = sti_gdp_get_formats,
  484. .get_nb_formats = sti_gdp_get_nb_formats,
  485. .init = sti_gdp_init,
  486. .prepare = sti_gdp_prepare_layer,
  487. .commit = sti_gdp_commit_layer,
  488. .disable = sti_gdp_disable_layer,
  489. };
  490. struct sti_layer *sti_gdp_create(struct device *dev, int id)
  491. {
  492. struct sti_gdp *gdp;
  493. gdp = devm_kzalloc(dev, sizeof(*gdp), GFP_KERNEL);
  494. if (!gdp) {
  495. DRM_ERROR("Failed to allocate memory for GDP\n");
  496. return NULL;
  497. }
  498. gdp->layer.ops = &gdp_ops;
  499. gdp->vtg_field_nb.notifier_call = sti_gdp_field_cb;
  500. return (struct sti_layer *)gdp;
  501. }