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