WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / rcar-du / rcar_cmm.c
blobc578095b09a53e89cc5777da7bcd800eac25ac03
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * rcar_cmm.c -- R-Car Display Unit Color Management Module
5 * Copyright (C) 2019 Jacopo Mondi <jacopo+renesas@jmondi.org>
6 */
8 #include <linux/io.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
14 #include <drm/drm_color_mgmt.h>
16 #include "rcar_cmm.h"
18 #define CM2_LUT_CTRL 0x0000
19 #define CM2_LUT_CTRL_LUT_EN BIT(0)
20 #define CM2_LUT_TBL_BASE 0x0600
21 #define CM2_LUT_TBL(__i) (CM2_LUT_TBL_BASE + (__i) * 4)
23 struct rcar_cmm {
24 void __iomem *base;
27 * @lut: 1D-LUT state
28 * @lut.enabled: 1D-LUT enabled flag
30 struct {
31 bool enabled;
32 } lut;
35 static inline int rcar_cmm_read(struct rcar_cmm *rcmm, u32 reg)
37 return ioread32(rcmm->base + reg);
40 static inline void rcar_cmm_write(struct rcar_cmm *rcmm, u32 reg, u32 data)
42 iowrite32(data, rcmm->base + reg);
46 * rcar_cmm_lut_write() - Scale the DRM LUT table entries to hardware precision
47 * and write to the CMM registers
48 * @rcmm: Pointer to the CMM device
49 * @drm_lut: Pointer to the DRM LUT table
51 static void rcar_cmm_lut_write(struct rcar_cmm *rcmm,
52 const struct drm_color_lut *drm_lut)
54 unsigned int i;
56 for (i = 0; i < CM2_LUT_SIZE; ++i) {
57 u32 entry = drm_color_lut_extract(drm_lut[i].red, 8) << 16
58 | drm_color_lut_extract(drm_lut[i].green, 8) << 8
59 | drm_color_lut_extract(drm_lut[i].blue, 8);
61 rcar_cmm_write(rcmm, CM2_LUT_TBL(i), entry);
66 * rcar_cmm_setup() - Configure the CMM unit
67 * @pdev: The platform device associated with the CMM instance
68 * @config: The CMM unit configuration
70 * Configure the CMM unit with the given configuration. Currently enabling,
71 * disabling and programming of the 1-D LUT unit is supported.
73 * As rcar_cmm_setup() accesses the CMM registers the unit should be powered
74 * and its functional clock enabled. To guarantee this, before any call to
75 * this function is made, the CMM unit has to be enabled by calling
76 * rcar_cmm_enable() first.
78 * TODO: Add support for LUT double buffer operations to avoid updating the
79 * LUT table entries while a frame is being displayed.
81 int rcar_cmm_setup(struct platform_device *pdev,
82 const struct rcar_cmm_config *config)
84 struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
86 /* Disable LUT if no table is provided. */
87 if (!config->lut.table) {
88 if (rcmm->lut.enabled) {
89 rcar_cmm_write(rcmm, CM2_LUT_CTRL, 0);
90 rcmm->lut.enabled = false;
93 return 0;
96 /* Enable LUT and program the new gamma table values. */
97 if (!rcmm->lut.enabled) {
98 rcar_cmm_write(rcmm, CM2_LUT_CTRL, CM2_LUT_CTRL_LUT_EN);
99 rcmm->lut.enabled = true;
102 rcar_cmm_lut_write(rcmm, config->lut.table);
104 return 0;
106 EXPORT_SYMBOL_GPL(rcar_cmm_setup);
109 * rcar_cmm_enable() - Enable the CMM unit
110 * @pdev: The platform device associated with the CMM instance
112 * When the output of the corresponding DU channel is routed to the CMM unit,
113 * the unit shall be enabled before the DU channel is started, and remain
114 * enabled until the channel is stopped. The CMM unit shall be disabled with
115 * rcar_cmm_disable().
117 * Calls to rcar_cmm_enable() and rcar_cmm_disable() are not reference-counted.
118 * It is an error to attempt to enable an already enabled CMM unit, or to
119 * attempt to disable a disabled unit.
121 int rcar_cmm_enable(struct platform_device *pdev)
123 int ret;
125 ret = pm_runtime_get_sync(&pdev->dev);
126 if (ret < 0)
127 return ret;
129 return 0;
131 EXPORT_SYMBOL_GPL(rcar_cmm_enable);
134 * rcar_cmm_disable() - Disable the CMM unit
135 * @pdev: The platform device associated with the CMM instance
137 * See rcar_cmm_enable() for usage information.
139 * Disabling the CMM unit disable all the internal processing blocks. The CMM
140 * state shall thus be restored with rcar_cmm_setup() when re-enabling the CMM
141 * unit after the next rcar_cmm_enable() call.
143 void rcar_cmm_disable(struct platform_device *pdev)
145 struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
147 rcar_cmm_write(rcmm, CM2_LUT_CTRL, 0);
148 rcmm->lut.enabled = false;
150 pm_runtime_put(&pdev->dev);
152 EXPORT_SYMBOL_GPL(rcar_cmm_disable);
155 * rcar_cmm_init() - Initialize the CMM unit
156 * @pdev: The platform device associated with the CMM instance
158 * Return: 0 on success, -EPROBE_DEFER if the CMM is not available yet,
159 * -ENODEV if the DRM_RCAR_CMM config option is disabled
161 int rcar_cmm_init(struct platform_device *pdev)
163 struct rcar_cmm *rcmm = platform_get_drvdata(pdev);
165 if (!rcmm)
166 return -EPROBE_DEFER;
168 return 0;
170 EXPORT_SYMBOL_GPL(rcar_cmm_init);
172 static int rcar_cmm_probe(struct platform_device *pdev)
174 struct rcar_cmm *rcmm;
176 rcmm = devm_kzalloc(&pdev->dev, sizeof(*rcmm), GFP_KERNEL);
177 if (!rcmm)
178 return -ENOMEM;
179 platform_set_drvdata(pdev, rcmm);
181 rcmm->base = devm_platform_ioremap_resource(pdev, 0);
182 if (IS_ERR(rcmm->base))
183 return PTR_ERR(rcmm->base);
185 pm_runtime_enable(&pdev->dev);
187 return 0;
190 static int rcar_cmm_remove(struct platform_device *pdev)
192 pm_runtime_disable(&pdev->dev);
194 return 0;
197 static const struct of_device_id rcar_cmm_of_table[] = {
198 { .compatible = "renesas,rcar-gen3-cmm", },
199 { .compatible = "renesas,rcar-gen2-cmm", },
200 { },
202 MODULE_DEVICE_TABLE(of, rcar_cmm_of_table);
204 static struct platform_driver rcar_cmm_platform_driver = {
205 .probe = rcar_cmm_probe,
206 .remove = rcar_cmm_remove,
207 .driver = {
208 .name = "rcar-cmm",
209 .of_match_table = rcar_cmm_of_table,
213 module_platform_driver(rcar_cmm_platform_driver);
215 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org>");
216 MODULE_DESCRIPTION("Renesas R-Car CMM Driver");
217 MODULE_LICENSE("GPL v2");