sti_gdp.c 14 KB

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