mtk_disp_color.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017 MediaTek Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/component.h>
  7. #include <linux/module.h>
  8. #include <linux/of_device.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/soc/mediatek/mtk-cmdq.h>
  12. #include "mtk_drm_crtc.h"
  13. #include "mtk_drm_ddp_comp.h"
  14. #define DISP_COLOR_CFG_MAIN 0x0400
  15. #define DISP_COLOR_START_MT2701 0x0f00
  16. #define DISP_COLOR_START_MT8173 0x0c00
  17. #define DISP_COLOR_START(comp) ((comp)->data->color_offset)
  18. #define DISP_COLOR_WIDTH(comp) (DISP_COLOR_START(comp) + 0x50)
  19. #define DISP_COLOR_HEIGHT(comp) (DISP_COLOR_START(comp) + 0x54)
  20. #define COLOR_BYPASS_ALL BIT(7)
  21. #define COLOR_SEQ_SEL BIT(13)
  22. struct mtk_disp_color_data {
  23. unsigned int color_offset;
  24. };
  25. /**
  26. * struct mtk_disp_color - DISP_COLOR driver structure
  27. * @ddp_comp - structure containing type enum and hardware resources
  28. * @crtc - associated crtc to report irq events to
  29. */
  30. struct mtk_disp_color {
  31. struct mtk_ddp_comp ddp_comp;
  32. struct drm_crtc *crtc;
  33. const struct mtk_disp_color_data *data;
  34. };
  35. static inline struct mtk_disp_color *comp_to_color(struct mtk_ddp_comp *comp)
  36. {
  37. return container_of(comp, struct mtk_disp_color, ddp_comp);
  38. }
  39. static void mtk_color_config(struct mtk_ddp_comp *comp, unsigned int w,
  40. unsigned int h, unsigned int vrefresh,
  41. unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
  42. {
  43. struct mtk_disp_color *color = comp_to_color(comp);
  44. mtk_ddp_write(cmdq_pkt, w, comp, DISP_COLOR_WIDTH(color));
  45. mtk_ddp_write(cmdq_pkt, h, comp, DISP_COLOR_HEIGHT(color));
  46. }
  47. static void mtk_color_start(struct mtk_ddp_comp *comp)
  48. {
  49. struct mtk_disp_color *color = comp_to_color(comp);
  50. writel(COLOR_BYPASS_ALL | COLOR_SEQ_SEL,
  51. comp->regs + DISP_COLOR_CFG_MAIN);
  52. writel(0x1, comp->regs + DISP_COLOR_START(color));
  53. }
  54. static const struct mtk_ddp_comp_funcs mtk_disp_color_funcs = {
  55. .config = mtk_color_config,
  56. .start = mtk_color_start,
  57. };
  58. static int mtk_disp_color_bind(struct device *dev, struct device *master,
  59. void *data)
  60. {
  61. struct mtk_disp_color *priv = dev_get_drvdata(dev);
  62. struct drm_device *drm_dev = data;
  63. int ret;
  64. ret = mtk_ddp_comp_register(drm_dev, &priv->ddp_comp);
  65. if (ret < 0) {
  66. dev_err(dev, "Failed to register component %pOF: %d\n",
  67. dev->of_node, ret);
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. static void mtk_disp_color_unbind(struct device *dev, struct device *master,
  73. void *data)
  74. {
  75. struct mtk_disp_color *priv = dev_get_drvdata(dev);
  76. struct drm_device *drm_dev = data;
  77. mtk_ddp_comp_unregister(drm_dev, &priv->ddp_comp);
  78. }
  79. static const struct component_ops mtk_disp_color_component_ops = {
  80. .bind = mtk_disp_color_bind,
  81. .unbind = mtk_disp_color_unbind,
  82. };
  83. static int mtk_disp_color_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct mtk_disp_color *priv;
  87. int comp_id;
  88. int ret;
  89. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  90. if (!priv)
  91. return -ENOMEM;
  92. comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DISP_COLOR);
  93. if (comp_id < 0) {
  94. dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
  95. return comp_id;
  96. }
  97. ret = mtk_ddp_comp_init(dev, dev->of_node, &priv->ddp_comp, comp_id,
  98. &mtk_disp_color_funcs);
  99. if (ret) {
  100. if (ret != -EPROBE_DEFER)
  101. dev_err(dev, "Failed to initialize component: %d\n",
  102. ret);
  103. return ret;
  104. }
  105. priv->data = of_device_get_match_data(dev);
  106. platform_set_drvdata(pdev, priv);
  107. ret = component_add(dev, &mtk_disp_color_component_ops);
  108. if (ret)
  109. dev_err(dev, "Failed to add component: %d\n", ret);
  110. return ret;
  111. }
  112. static int mtk_disp_color_remove(struct platform_device *pdev)
  113. {
  114. component_del(&pdev->dev, &mtk_disp_color_component_ops);
  115. return 0;
  116. }
  117. static const struct mtk_disp_color_data mt2701_color_driver_data = {
  118. .color_offset = DISP_COLOR_START_MT2701,
  119. };
  120. static const struct mtk_disp_color_data mt8173_color_driver_data = {
  121. .color_offset = DISP_COLOR_START_MT8173,
  122. };
  123. static const struct of_device_id mtk_disp_color_driver_dt_match[] = {
  124. { .compatible = "mediatek,mt2701-disp-color",
  125. .data = &mt2701_color_driver_data},
  126. { .compatible = "mediatek,mt8173-disp-color",
  127. .data = &mt8173_color_driver_data},
  128. {},
  129. };
  130. MODULE_DEVICE_TABLE(of, mtk_disp_color_driver_dt_match);
  131. struct platform_driver mtk_disp_color_driver = {
  132. .probe = mtk_disp_color_probe,
  133. .remove = mtk_disp_color_remove,
  134. .driver = {
  135. .name = "mediatek-disp-color",
  136. .owner = THIS_MODULE,
  137. .of_match_table = mtk_disp_color_driver_dt_match,
  138. },
  139. };