1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Samsung Electronics Co.Ltd
5 * Hyungwon Hwang <human.hwang@samsung.com>
9 #include <linux/component.h>
10 #include <linux/delay.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/of_address.h>
16 #include <linux/of_graph.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
21 #include <video/of_videomode.h>
22 #include <video/videomode.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_encoder.h>
26 #include <drm/drm_print.h>
28 #include "exynos_drm_drv.h"
29 #include "exynos_drm_crtc.h"
31 /* Sysreg registers for MIC */
32 #define DSD_CFG_MUX 0x1004
33 #define MIC0_RGB_MUX (1 << 0)
34 #define MIC0_I80_MUX (1 << 1)
35 #define MIC0_ON_MUX (1 << 5)
39 #define MIC_IP_VER 0x0004
40 #define MIC_V_TIMING_0 0x0008
41 #define MIC_V_TIMING_1 0x000C
42 #define MIC_IMG_SIZE 0x0010
43 #define MIC_INPUT_TIMING_0 0x0014
44 #define MIC_INPUT_TIMING_1 0x0018
45 #define MIC_2D_OUTPUT_TIMING_0 0x001C
46 #define MIC_2D_OUTPUT_TIMING_1 0x0020
47 #define MIC_2D_OUTPUT_TIMING_2 0x0024
48 #define MIC_3D_OUTPUT_TIMING_0 0x0028
49 #define MIC_3D_OUTPUT_TIMING_1 0x002C
50 #define MIC_3D_OUTPUT_TIMING_2 0x0030
51 #define MIC_CORE_PARA_0 0x0034
52 #define MIC_CORE_PARA_1 0x0038
53 #define MIC_CTC_CTRL 0x0040
54 #define MIC_RD_DATA 0x0044
56 #define MIC_UPD_REG (1 << 31)
57 #define MIC_ON_REG (1 << 30)
58 #define MIC_TD_ON_REG (1 << 29)
59 #define MIC_BS_CHG_OUT (1 << 16)
60 #define MIC_VIDEO_TYPE(x) (((x) & 0xf) << 12)
61 #define MIC_PSR_EN (1 << 5)
62 #define MIC_SW_RST (1 << 4)
63 #define MIC_ALL_RST (1 << 3)
64 #define MIC_CORE_VER_CONTROL (1 << 2)
65 #define MIC_MODE_SEL_COMMAND_MODE (1 << 1)
66 #define MIC_MODE_SEL_MASK (1 << 1)
67 #define MIC_CORE_EN (1 << 0)
69 #define MIC_V_PULSE_WIDTH(x) (((x) & 0x3fff) << 16)
70 #define MIC_V_PERIOD_LINE(x) ((x) & 0x3fff)
72 #define MIC_VBP_SIZE(x) (((x) & 0x3fff) << 16)
73 #define MIC_VFP_SIZE(x) ((x) & 0x3fff)
75 #define MIC_IMG_V_SIZE(x) (((x) & 0x3fff) << 16)
76 #define MIC_IMG_H_SIZE(x) ((x) & 0x3fff)
78 #define MIC_H_PULSE_WIDTH_IN(x) (((x) & 0x3fff) << 16)
79 #define MIC_H_PERIOD_PIXEL_IN(x) ((x) & 0x3fff)
81 #define MIC_HBP_SIZE_IN(x) (((x) & 0x3fff) << 16)
82 #define MIC_HFP_SIZE_IN(x) ((x) & 0x3fff)
84 #define MIC_H_PULSE_WIDTH_2D(x) (((x) & 0x3fff) << 16)
85 #define MIC_H_PERIOD_PIXEL_2D(x) ((x) & 0x3fff)
87 #define MIC_HBP_SIZE_2D(x) (((x) & 0x3fff) << 16)
88 #define MIC_HFP_SIZE_2D(x) ((x) & 0x3fff)
90 #define MIC_BS_SIZE_2D(x) ((x) & 0x3fff)
92 static const char *const clk_names[] = { "pclk_mic0", "sclk_rgb_vclk_to_mic0" };
93 #define NUM_CLKS ARRAY_SIZE(clk_names)
94 static DEFINE_MUTEX(mic_mutex);
99 struct regmap *sysreg;
100 struct clk *clks[NUM_CLKS];
104 struct drm_bridge bridge;
109 static void mic_set_path(struct exynos_mic *mic, bool enable)
114 ret = regmap_read(mic->sysreg, DSD_CFG_MUX, &val);
116 DRM_DEV_ERROR(mic->dev,
117 "mic: Failed to read system register\n");
129 val &= ~(MIC0_RGB_MUX | MIC0_I80_MUX | MIC0_ON_MUX);
131 ret = regmap_write(mic->sysreg, DSD_CFG_MUX, val);
133 DRM_DEV_ERROR(mic->dev,
134 "mic: Failed to read system register\n");
137 static int mic_sw_reset(struct exynos_mic *mic)
139 unsigned int retry = 100;
142 writel(MIC_SW_RST, mic->reg + MIC_OP);
144 while (retry-- > 0) {
145 ret = readl(mic->reg + MIC_OP);
146 if (!(ret & MIC_SW_RST))
155 static void mic_set_porch_timing(struct exynos_mic *mic)
157 struct videomode vm = mic->vm;
160 reg = MIC_V_PULSE_WIDTH(vm.vsync_len) +
161 MIC_V_PERIOD_LINE(vm.vsync_len + vm.vactive +
162 vm.vback_porch + vm.vfront_porch);
163 writel(reg, mic->reg + MIC_V_TIMING_0);
165 reg = MIC_VBP_SIZE(vm.vback_porch) +
166 MIC_VFP_SIZE(vm.vfront_porch);
167 writel(reg, mic->reg + MIC_V_TIMING_1);
169 reg = MIC_V_PULSE_WIDTH(vm.hsync_len) +
170 MIC_V_PERIOD_LINE(vm.hsync_len + vm.hactive +
171 vm.hback_porch + vm.hfront_porch);
172 writel(reg, mic->reg + MIC_INPUT_TIMING_0);
174 reg = MIC_VBP_SIZE(vm.hback_porch) +
175 MIC_VFP_SIZE(vm.hfront_porch);
176 writel(reg, mic->reg + MIC_INPUT_TIMING_1);
179 static void mic_set_img_size(struct exynos_mic *mic)
181 struct videomode *vm = &mic->vm;
184 reg = MIC_IMG_H_SIZE(vm->hactive) +
185 MIC_IMG_V_SIZE(vm->vactive);
187 writel(reg, mic->reg + MIC_IMG_SIZE);
190 static void mic_set_output_timing(struct exynos_mic *mic)
192 struct videomode vm = mic->vm;
195 DRM_DEV_DEBUG(mic->dev, "w: %u, h: %u\n", vm.hactive, vm.vactive);
196 bs_size_2d = ((vm.hactive >> 2) << 1) + (vm.vactive % 4);
197 reg = MIC_BS_SIZE_2D(bs_size_2d);
198 writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_2);
200 if (!mic->i80_mode) {
201 reg = MIC_H_PULSE_WIDTH_2D(vm.hsync_len) +
202 MIC_H_PERIOD_PIXEL_2D(vm.hsync_len + bs_size_2d +
203 vm.hback_porch + vm.hfront_porch);
204 writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_0);
206 reg = MIC_HBP_SIZE_2D(vm.hback_porch) +
207 MIC_H_PERIOD_PIXEL_2D(vm.hfront_porch);
208 writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_1);
212 static void mic_set_reg_on(struct exynos_mic *mic, bool enable)
214 u32 reg = readl(mic->reg + MIC_OP);
217 reg &= ~(MIC_MODE_SEL_MASK | MIC_CORE_VER_CONTROL | MIC_PSR_EN);
218 reg |= (MIC_CORE_EN | MIC_BS_CHG_OUT | MIC_ON_REG);
220 reg &= ~MIC_MODE_SEL_COMMAND_MODE;
222 reg |= MIC_MODE_SEL_COMMAND_MODE;
228 writel(reg, mic->reg + MIC_OP);
231 static void mic_post_disable(struct drm_bridge *bridge)
233 struct exynos_mic *mic = bridge->driver_private;
235 mutex_lock(&mic_mutex);
237 goto already_disabled;
239 mic_set_path(mic, 0);
241 pm_runtime_put(mic->dev);
245 mutex_unlock(&mic_mutex);
248 static void mic_mode_set(struct drm_bridge *bridge,
249 const struct drm_display_mode *mode,
250 const struct drm_display_mode *adjusted_mode)
252 struct exynos_mic *mic = bridge->driver_private;
254 mutex_lock(&mic_mutex);
255 drm_display_mode_to_videomode(mode, &mic->vm);
256 mic->i80_mode = to_exynos_crtc(bridge->encoder->crtc)->i80_mode;
257 mutex_unlock(&mic_mutex);
260 static void mic_pre_enable(struct drm_bridge *bridge)
262 struct exynos_mic *mic = bridge->driver_private;
265 mutex_lock(&mic_mutex);
269 ret = pm_runtime_resume_and_get(mic->dev);
273 mic_set_path(mic, 1);
275 ret = mic_sw_reset(mic);
277 DRM_DEV_ERROR(mic->dev, "Failed to reset\n");
282 mic_set_porch_timing(mic);
283 mic_set_img_size(mic);
284 mic_set_output_timing(mic);
285 mic_set_reg_on(mic, 1);
287 mutex_unlock(&mic_mutex);
292 pm_runtime_put(mic->dev);
294 mutex_unlock(&mic_mutex);
297 static const struct drm_bridge_funcs mic_bridge_funcs = {
298 .post_disable = mic_post_disable,
299 .mode_set = mic_mode_set,
300 .pre_enable = mic_pre_enable,
303 static int exynos_mic_bind(struct device *dev, struct device *master,
306 struct exynos_mic *mic = dev_get_drvdata(dev);
307 struct drm_device *drm_dev = data;
308 struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(drm_dev,
309 EXYNOS_DISPLAY_TYPE_LCD);
310 struct drm_encoder *e, *encoder = NULL;
312 drm_for_each_encoder(e, drm_dev)
313 if (e->possible_crtcs == drm_crtc_mask(&crtc->base))
318 mic->bridge.driver_private = mic;
320 return drm_bridge_attach(encoder, &mic->bridge, NULL, 0);
323 static void exynos_mic_unbind(struct device *dev, struct device *master,
326 struct exynos_mic *mic = dev_get_drvdata(dev);
328 mutex_lock(&mic_mutex);
330 goto already_disabled;
332 pm_runtime_put(mic->dev);
335 mutex_unlock(&mic_mutex);
338 static const struct component_ops exynos_mic_component_ops = {
339 .bind = exynos_mic_bind,
340 .unbind = exynos_mic_unbind,
343 static int exynos_mic_suspend(struct device *dev)
345 struct exynos_mic *mic = dev_get_drvdata(dev);
348 for (i = NUM_CLKS - 1; i > -1; i--)
349 clk_disable_unprepare(mic->clks[i]);
354 static int exynos_mic_resume(struct device *dev)
356 struct exynos_mic *mic = dev_get_drvdata(dev);
359 for (i = 0; i < NUM_CLKS; i++) {
360 ret = clk_prepare_enable(mic->clks[i]);
362 DRM_DEV_ERROR(dev, "Failed to enable clock (%s)\n",
365 clk_disable_unprepare(mic->clks[i]);
372 static DEFINE_RUNTIME_DEV_PM_OPS(exynos_mic_pm_ops, exynos_mic_suspend,
373 exynos_mic_resume, NULL);
375 static int exynos_mic_probe(struct platform_device *pdev)
377 struct device *dev = &pdev->dev;
378 struct exynos_mic *mic;
382 mic = devm_kzalloc(dev, sizeof(*mic), GFP_KERNEL);
385 "mic: Failed to allocate memory for MIC object\n");
392 ret = of_address_to_resource(dev->of_node, 0, &res);
394 DRM_DEV_ERROR(dev, "mic: Failed to get mem region for MIC\n");
397 mic->reg = devm_ioremap(dev, res.start, resource_size(&res));
399 DRM_DEV_ERROR(dev, "mic: Failed to remap for MIC\n");
404 mic->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
405 "samsung,disp-syscon");
406 if (IS_ERR(mic->sysreg)) {
407 DRM_DEV_ERROR(dev, "mic: Failed to get system register.\n");
408 ret = PTR_ERR(mic->sysreg);
412 for (i = 0; i < NUM_CLKS; i++) {
413 mic->clks[i] = devm_clk_get(dev, clk_names[i]);
414 if (IS_ERR(mic->clks[i])) {
415 DRM_DEV_ERROR(dev, "mic: Failed to get clock (%s)\n",
417 ret = PTR_ERR(mic->clks[i]);
422 platform_set_drvdata(pdev, mic);
424 mic->bridge.funcs = &mic_bridge_funcs;
425 mic->bridge.of_node = dev->of_node;
427 drm_bridge_add(&mic->bridge);
429 pm_runtime_enable(dev);
431 ret = component_add(dev, &exynos_mic_component_ops);
435 DRM_DEV_DEBUG_KMS(dev, "MIC has been probed\n");
440 pm_runtime_disable(dev);
445 static int exynos_mic_remove(struct platform_device *pdev)
447 struct exynos_mic *mic = platform_get_drvdata(pdev);
449 component_del(&pdev->dev, &exynos_mic_component_ops);
450 pm_runtime_disable(&pdev->dev);
452 drm_bridge_remove(&mic->bridge);
457 static const struct of_device_id exynos_mic_of_match[] = {
458 { .compatible = "samsung,exynos5433-mic" },
461 MODULE_DEVICE_TABLE(of, exynos_mic_of_match);
463 struct platform_driver mic_driver = {
464 .probe = exynos_mic_probe,
465 .remove = exynos_mic_remove,
467 .name = "exynos-mic",
468 .pm = pm_ptr(&exynos_mic_pm_ops),
469 .owner = THIS_MODULE,
470 .of_match_table = exynos_mic_of_match,