rcar_cmm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * R-Car Display Unit Color Management Module
  4. *
  5. * Copyright (C) 2019 Jacopo Mondi <[email protected]>
  6. */
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm_runtime.h>
  12. #include <drm/drm_color_mgmt.h>
  13. #include "rcar_cmm.h"
  14. #define CM2_LUT_CTRL 0x0000
  15. #define CM2_LUT_CTRL_LUT_EN BIT(0)
  16. #define CM2_LUT_TBL_BASE 0x0600
  17. #define CM2_LUT_TBL(__i) (CM2_LUT_TBL_BASE + (__i) * 4)
  18. struct rcar_cmm {
  19. void __iomem *base;
  20. /*
  21. * @lut: 1D-LUT state
  22. * @lut.enabled: 1D-LUT enabled flag
  23. */
  24. struct {
  25. bool enabled;
  26. } lut;
  27. };
  28. static inline int rcar_cmm_read(struct rcar_cmm *rcmm, u32 reg)
  29. {
  30. return ioread32(rcmm->base + reg);
  31. }
  32. static inline void rcar_cmm_write(struct rcar_cmm *rcmm, u32 reg, u32 data)
  33. {
  34. iowrite32(data, rcmm->base + reg);
  35. }
  36. /*
  37. * rcar_cmm_lut_write() - Scale the DRM LUT table entries to hardware precision
  38. * and write to the CMM registers
  39. * @rcmm: Pointer to the CMM device
  40. * @drm_lut: Pointer to the DRM LUT table
  41. */
  42. static void rcar_cmm_lut_write(struct rcar_cmm *rcmm,
  43. const struct drm_color_lut *drm_lut)
  44. {
  45. unsigned int i;
  46. for (i = 0; i < CM2_LUT_SIZE; ++i) {
  47. u32 entry = drm_color_lut_extract(drm_lut[i].red, 8) << 16
  48. | drm_color_lut_extract(drm_lut[i].green, 8) << 8
  49. | drm_color_lut_extract(drm_lut[i].blue, 8);
  50. rcar_cmm_write(rcmm, CM2_LUT_TBL(i), entry);
  51. }
  52. }
  53. /*
  54. * rcar_cmm_setup() - Configure the CMM unit
  55. * @pdev: The platform device associated with the CMM instance
  56. * @config: The CMM unit configuration
  57. *
  58. * Configure the CMM unit with the given configuration. Currently enabling,
  59. * disabling and programming of the 1-D LUT unit is supported.
  60. *
  61. * As rcar_cmm_setup() accesses the CMM registers the unit should be powered
  62. * and its functional clock enabled. To guarantee this, before any call to
  63. * this function is made, the CMM unit has to be enabled by calling
  64. * rcar_cmm_enable() first.
  65. *
  66. * TODO: Add support for LUT double buffer operations to avoid updating the
  67. * LUT table entries while a frame is being displayed.
  68. */
  69. int rcar_cmm_setup(struct platform_device *pdev,
  70. const struct rcar_cmm_config *config)
  71. {
  72. struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
  73. /* Disable LUT if no table is provided. */
  74. if (!config->lut.table) {
  75. if (rcmm->lut.enabled) {
  76. rcar_cmm_write(rcmm, CM2_LUT_CTRL, 0);
  77. rcmm->lut.enabled = false;
  78. }
  79. return 0;
  80. }
  81. /* Enable LUT and program the new gamma table values. */
  82. if (!rcmm->lut.enabled) {
  83. rcar_cmm_write(rcmm, CM2_LUT_CTRL, CM2_LUT_CTRL_LUT_EN);
  84. rcmm->lut.enabled = true;
  85. }
  86. rcar_cmm_lut_write(rcmm, config->lut.table);
  87. return 0;
  88. }
  89. EXPORT_SYMBOL_GPL(rcar_cmm_setup);
  90. /*
  91. * rcar_cmm_enable() - Enable the CMM unit
  92. * @pdev: The platform device associated with the CMM instance
  93. *
  94. * When the output of the corresponding DU channel is routed to the CMM unit,
  95. * the unit shall be enabled before the DU channel is started, and remain
  96. * enabled until the channel is stopped. The CMM unit shall be disabled with
  97. * rcar_cmm_disable().
  98. *
  99. * Calls to rcar_cmm_enable() and rcar_cmm_disable() are not reference-counted.
  100. * It is an error to attempt to enable an already enabled CMM unit, or to
  101. * attempt to disable a disabled unit.
  102. */
  103. int rcar_cmm_enable(struct platform_device *pdev)
  104. {
  105. int ret;
  106. ret = pm_runtime_resume_and_get(&pdev->dev);
  107. if (ret < 0)
  108. return ret;
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(rcar_cmm_enable);
  112. /*
  113. * rcar_cmm_disable() - Disable the CMM unit
  114. * @pdev: The platform device associated with the CMM instance
  115. *
  116. * See rcar_cmm_enable() for usage information.
  117. *
  118. * Disabling the CMM unit disable all the internal processing blocks. The CMM
  119. * state shall thus be restored with rcar_cmm_setup() when re-enabling the CMM
  120. * unit after the next rcar_cmm_enable() call.
  121. */
  122. void rcar_cmm_disable(struct platform_device *pdev)
  123. {
  124. struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
  125. rcar_cmm_write(rcmm, CM2_LUT_CTRL, 0);
  126. rcmm->lut.enabled = false;
  127. pm_runtime_put(&pdev->dev);
  128. }
  129. EXPORT_SYMBOL_GPL(rcar_cmm_disable);
  130. /*
  131. * rcar_cmm_init() - Initialize the CMM unit
  132. * @pdev: The platform device associated with the CMM instance
  133. *
  134. * Return: 0 on success, -EPROBE_DEFER if the CMM is not available yet,
  135. * -ENODEV if the DRM_RCAR_CMM config option is disabled
  136. */
  137. int rcar_cmm_init(struct platform_device *pdev)
  138. {
  139. struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
  140. if (!rcmm)
  141. return -EPROBE_DEFER;
  142. return 0;
  143. }
  144. EXPORT_SYMBOL_GPL(rcar_cmm_init);
  145. static int rcar_cmm_probe(struct platform_device *pdev)
  146. {
  147. struct rcar_cmm *rcmm;
  148. rcmm = devm_kzalloc(&pdev->dev, sizeof(*rcmm), GFP_KERNEL);
  149. if (!rcmm)
  150. return -ENOMEM;
  151. platform_set_drvdata(pdev, rcmm);
  152. rcmm->base = devm_platform_ioremap_resource(pdev, 0);
  153. if (IS_ERR(rcmm->base))
  154. return PTR_ERR(rcmm->base);
  155. pm_runtime_enable(&pdev->dev);
  156. return 0;
  157. }
  158. static int rcar_cmm_remove(struct platform_device *pdev)
  159. {
  160. pm_runtime_disable(&pdev->dev);
  161. return 0;
  162. }
  163. static const struct of_device_id rcar_cmm_of_table[] = {
  164. { .compatible = "renesas,rcar-gen3-cmm", },
  165. { .compatible = "renesas,rcar-gen2-cmm", },
  166. { },
  167. };
  168. MODULE_DEVICE_TABLE(of, rcar_cmm_of_table);
  169. static struct platform_driver rcar_cmm_platform_driver = {
  170. .probe = rcar_cmm_probe,
  171. .remove = rcar_cmm_remove,
  172. .driver = {
  173. .name = "rcar-cmm",
  174. .of_match_table = rcar_cmm_of_table,
  175. },
  176. };
  177. module_platform_driver(rcar_cmm_platform_driver);
  178. MODULE_AUTHOR("Jacopo Mondi <[email protected]>");
  179. MODULE_DESCRIPTION("Renesas R-Car CMM Driver");
  180. MODULE_LICENSE("GPL v2");