19f06080fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
277bbd891SHyungwon Hwang /*
377bbd891SHyungwon Hwang * Copyright (C) 2015 Samsung Electronics Co.Ltd
477bbd891SHyungwon Hwang * Authors:
577bbd891SHyungwon Hwang * Hyungwon Hwang <human.hwang@samsung.com>
677bbd891SHyungwon Hwang */
777bbd891SHyungwon Hwang
877bbd891SHyungwon Hwang #include <linux/clk.h>
9622688f3SMarek Szyprowski #include <linux/component.h>
102bda34d7SSam Ravnborg #include <linux/delay.h>
1177bbd891SHyungwon Hwang #include <linux/mfd/syscon.h>
122bda34d7SSam Ravnborg #include <linux/module.h>
132bda34d7SSam Ravnborg #include <linux/mutex.h>
142bda34d7SSam Ravnborg #include <linux/of.h>
152bda34d7SSam Ravnborg #include <linux/of_address.h>
162bda34d7SSam Ravnborg #include <linux/of_graph.h>
172bda34d7SSam Ravnborg #include <linux/platform_device.h>
182bda34d7SSam Ravnborg #include <linux/pm_runtime.h>
1977bbd891SHyungwon Hwang #include <linux/regmap.h>
2077bbd891SHyungwon Hwang
212bda34d7SSam Ravnborg #include <video/of_videomode.h>
222bda34d7SSam Ravnborg #include <video/videomode.h>
232bda34d7SSam Ravnborg
24ee68c743SBoris Brezillon #include <drm/drm_bridge.h>
252bda34d7SSam Ravnborg #include <drm/drm_encoder.h>
262bda34d7SSam Ravnborg #include <drm/drm_print.h>
272bda34d7SSam Ravnborg
2829c5079dSAndrzej Hajda #include "exynos_drm_drv.h"
297d787184SMarek Szyprowski #include "exynos_drm_crtc.h"
3029c5079dSAndrzej Hajda
3177bbd891SHyungwon Hwang /* Sysreg registers for MIC */
3277bbd891SHyungwon Hwang #define DSD_CFG_MUX 0x1004
3377bbd891SHyungwon Hwang #define MIC0_RGB_MUX (1 << 0)
3477bbd891SHyungwon Hwang #define MIC0_I80_MUX (1 << 1)
3577bbd891SHyungwon Hwang #define MIC0_ON_MUX (1 << 5)
3677bbd891SHyungwon Hwang
3777bbd891SHyungwon Hwang /* MIC registers */
3877bbd891SHyungwon Hwang #define MIC_OP 0x0
3977bbd891SHyungwon Hwang #define MIC_IP_VER 0x0004
4077bbd891SHyungwon Hwang #define MIC_V_TIMING_0 0x0008
4177bbd891SHyungwon Hwang #define MIC_V_TIMING_1 0x000C
4277bbd891SHyungwon Hwang #define MIC_IMG_SIZE 0x0010
4377bbd891SHyungwon Hwang #define MIC_INPUT_TIMING_0 0x0014
4477bbd891SHyungwon Hwang #define MIC_INPUT_TIMING_1 0x0018
4577bbd891SHyungwon Hwang #define MIC_2D_OUTPUT_TIMING_0 0x001C
4677bbd891SHyungwon Hwang #define MIC_2D_OUTPUT_TIMING_1 0x0020
4777bbd891SHyungwon Hwang #define MIC_2D_OUTPUT_TIMING_2 0x0024
4877bbd891SHyungwon Hwang #define MIC_3D_OUTPUT_TIMING_0 0x0028
4977bbd891SHyungwon Hwang #define MIC_3D_OUTPUT_TIMING_1 0x002C
5077bbd891SHyungwon Hwang #define MIC_3D_OUTPUT_TIMING_2 0x0030
5177bbd891SHyungwon Hwang #define MIC_CORE_PARA_0 0x0034
5277bbd891SHyungwon Hwang #define MIC_CORE_PARA_1 0x0038
5377bbd891SHyungwon Hwang #define MIC_CTC_CTRL 0x0040
5477bbd891SHyungwon Hwang #define MIC_RD_DATA 0x0044
5577bbd891SHyungwon Hwang
5677bbd891SHyungwon Hwang #define MIC_UPD_REG (1 << 31)
5777bbd891SHyungwon Hwang #define MIC_ON_REG (1 << 30)
5877bbd891SHyungwon Hwang #define MIC_TD_ON_REG (1 << 29)
5977bbd891SHyungwon Hwang #define MIC_BS_CHG_OUT (1 << 16)
6077bbd891SHyungwon Hwang #define MIC_VIDEO_TYPE(x) (((x) & 0xf) << 12)
6177bbd891SHyungwon Hwang #define MIC_PSR_EN (1 << 5)
6277bbd891SHyungwon Hwang #define MIC_SW_RST (1 << 4)
6377bbd891SHyungwon Hwang #define MIC_ALL_RST (1 << 3)
6477bbd891SHyungwon Hwang #define MIC_CORE_VER_CONTROL (1 << 2)
6577bbd891SHyungwon Hwang #define MIC_MODE_SEL_COMMAND_MODE (1 << 1)
6677bbd891SHyungwon Hwang #define MIC_MODE_SEL_MASK (1 << 1)
6777bbd891SHyungwon Hwang #define MIC_CORE_EN (1 << 0)
6877bbd891SHyungwon Hwang
6977bbd891SHyungwon Hwang #define MIC_V_PULSE_WIDTH(x) (((x) & 0x3fff) << 16)
7077bbd891SHyungwon Hwang #define MIC_V_PERIOD_LINE(x) ((x) & 0x3fff)
7177bbd891SHyungwon Hwang
7277bbd891SHyungwon Hwang #define MIC_VBP_SIZE(x) (((x) & 0x3fff) << 16)
7377bbd891SHyungwon Hwang #define MIC_VFP_SIZE(x) ((x) & 0x3fff)
7477bbd891SHyungwon Hwang
7577bbd891SHyungwon Hwang #define MIC_IMG_V_SIZE(x) (((x) & 0x3fff) << 16)
7677bbd891SHyungwon Hwang #define MIC_IMG_H_SIZE(x) ((x) & 0x3fff)
7777bbd891SHyungwon Hwang
7877bbd891SHyungwon Hwang #define MIC_H_PULSE_WIDTH_IN(x) (((x) & 0x3fff) << 16)
7977bbd891SHyungwon Hwang #define MIC_H_PERIOD_PIXEL_IN(x) ((x) & 0x3fff)
8077bbd891SHyungwon Hwang
8177bbd891SHyungwon Hwang #define MIC_HBP_SIZE_IN(x) (((x) & 0x3fff) << 16)
8277bbd891SHyungwon Hwang #define MIC_HFP_SIZE_IN(x) ((x) & 0x3fff)
8377bbd891SHyungwon Hwang
8477bbd891SHyungwon Hwang #define MIC_H_PULSE_WIDTH_2D(x) (((x) & 0x3fff) << 16)
8577bbd891SHyungwon Hwang #define MIC_H_PERIOD_PIXEL_2D(x) ((x) & 0x3fff)
8677bbd891SHyungwon Hwang
8777bbd891SHyungwon Hwang #define MIC_HBP_SIZE_2D(x) (((x) & 0x3fff) << 16)
8877bbd891SHyungwon Hwang #define MIC_HFP_SIZE_2D(x) ((x) & 0x3fff)
8977bbd891SHyungwon Hwang
9077bbd891SHyungwon Hwang #define MIC_BS_SIZE_2D(x) ((x) & 0x3fff)
9177bbd891SHyungwon Hwang
92a046e7bfSBernard Zhao static const char *const clk_names[] = { "pclk_mic0", "sclk_rgb_vclk_to_mic0" };
9377bbd891SHyungwon Hwang #define NUM_CLKS ARRAY_SIZE(clk_names)
9477bbd891SHyungwon Hwang static DEFINE_MUTEX(mic_mutex);
9577bbd891SHyungwon Hwang
9677bbd891SHyungwon Hwang struct exynos_mic {
9777bbd891SHyungwon Hwang struct device *dev;
9877bbd891SHyungwon Hwang void __iomem *reg;
9977bbd891SHyungwon Hwang struct regmap *sysreg;
10077bbd891SHyungwon Hwang struct clk *clks[NUM_CLKS];
10177bbd891SHyungwon Hwang
10277bbd891SHyungwon Hwang bool i80_mode;
10377bbd891SHyungwon Hwang struct videomode vm;
10477bbd891SHyungwon Hwang struct drm_bridge bridge;
10577bbd891SHyungwon Hwang
10677bbd891SHyungwon Hwang bool enabled;
10777bbd891SHyungwon Hwang };
10877bbd891SHyungwon Hwang
mic_set_path(struct exynos_mic * mic,bool enable)10977bbd891SHyungwon Hwang static void mic_set_path(struct exynos_mic *mic, bool enable)
11077bbd891SHyungwon Hwang {
11177bbd891SHyungwon Hwang int ret;
11277bbd891SHyungwon Hwang unsigned int val;
11377bbd891SHyungwon Hwang
11477bbd891SHyungwon Hwang ret = regmap_read(mic->sysreg, DSD_CFG_MUX, &val);
11577bbd891SHyungwon Hwang if (ret) {
1166f83d208SInki Dae DRM_DEV_ERROR(mic->dev,
1176f83d208SInki Dae "mic: Failed to read system register\n");
11877bbd891SHyungwon Hwang return;
11977bbd891SHyungwon Hwang }
12077bbd891SHyungwon Hwang
12177bbd891SHyungwon Hwang if (enable) {
12277bbd891SHyungwon Hwang if (mic->i80_mode)
12377bbd891SHyungwon Hwang val |= MIC0_I80_MUX;
12477bbd891SHyungwon Hwang else
12577bbd891SHyungwon Hwang val |= MIC0_RGB_MUX;
12677bbd891SHyungwon Hwang
12777bbd891SHyungwon Hwang val |= MIC0_ON_MUX;
12877bbd891SHyungwon Hwang } else
12977bbd891SHyungwon Hwang val &= ~(MIC0_RGB_MUX | MIC0_I80_MUX | MIC0_ON_MUX);
13077bbd891SHyungwon Hwang
13136ffc2bdSDan Carpenter ret = regmap_write(mic->sysreg, DSD_CFG_MUX, val);
13277bbd891SHyungwon Hwang if (ret)
1336f83d208SInki Dae DRM_DEV_ERROR(mic->dev,
1346f83d208SInki Dae "mic: Failed to read system register\n");
13577bbd891SHyungwon Hwang }
13677bbd891SHyungwon Hwang
mic_sw_reset(struct exynos_mic * mic)13777bbd891SHyungwon Hwang static int mic_sw_reset(struct exynos_mic *mic)
13877bbd891SHyungwon Hwang {
13977bbd891SHyungwon Hwang unsigned int retry = 100;
14077bbd891SHyungwon Hwang int ret;
14177bbd891SHyungwon Hwang
14277bbd891SHyungwon Hwang writel(MIC_SW_RST, mic->reg + MIC_OP);
14377bbd891SHyungwon Hwang
14477bbd891SHyungwon Hwang while (retry-- > 0) {
14577bbd891SHyungwon Hwang ret = readl(mic->reg + MIC_OP);
14677bbd891SHyungwon Hwang if (!(ret & MIC_SW_RST))
14777bbd891SHyungwon Hwang return 0;
14877bbd891SHyungwon Hwang
14977bbd891SHyungwon Hwang udelay(10);
15077bbd891SHyungwon Hwang }
15177bbd891SHyungwon Hwang
15277bbd891SHyungwon Hwang return -ETIMEDOUT;
15377bbd891SHyungwon Hwang }
15477bbd891SHyungwon Hwang
mic_set_porch_timing(struct exynos_mic * mic)15577bbd891SHyungwon Hwang static void mic_set_porch_timing(struct exynos_mic *mic)
15677bbd891SHyungwon Hwang {
15777bbd891SHyungwon Hwang struct videomode vm = mic->vm;
15877bbd891SHyungwon Hwang u32 reg;
15977bbd891SHyungwon Hwang
16077bbd891SHyungwon Hwang reg = MIC_V_PULSE_WIDTH(vm.vsync_len) +
16177bbd891SHyungwon Hwang MIC_V_PERIOD_LINE(vm.vsync_len + vm.vactive +
16277bbd891SHyungwon Hwang vm.vback_porch + vm.vfront_porch);
16377bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_V_TIMING_0);
16477bbd891SHyungwon Hwang
16577bbd891SHyungwon Hwang reg = MIC_VBP_SIZE(vm.vback_porch) +
16677bbd891SHyungwon Hwang MIC_VFP_SIZE(vm.vfront_porch);
16777bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_V_TIMING_1);
16877bbd891SHyungwon Hwang
16977bbd891SHyungwon Hwang reg = MIC_V_PULSE_WIDTH(vm.hsync_len) +
17077bbd891SHyungwon Hwang MIC_V_PERIOD_LINE(vm.hsync_len + vm.hactive +
17177bbd891SHyungwon Hwang vm.hback_porch + vm.hfront_porch);
17277bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_INPUT_TIMING_0);
17377bbd891SHyungwon Hwang
17477bbd891SHyungwon Hwang reg = MIC_VBP_SIZE(vm.hback_porch) +
17577bbd891SHyungwon Hwang MIC_VFP_SIZE(vm.hfront_porch);
17677bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_INPUT_TIMING_1);
17777bbd891SHyungwon Hwang }
17877bbd891SHyungwon Hwang
mic_set_img_size(struct exynos_mic * mic)17977bbd891SHyungwon Hwang static void mic_set_img_size(struct exynos_mic *mic)
18077bbd891SHyungwon Hwang {
18177bbd891SHyungwon Hwang struct videomode *vm = &mic->vm;
18277bbd891SHyungwon Hwang u32 reg;
18377bbd891SHyungwon Hwang
18477bbd891SHyungwon Hwang reg = MIC_IMG_H_SIZE(vm->hactive) +
18577bbd891SHyungwon Hwang MIC_IMG_V_SIZE(vm->vactive);
18677bbd891SHyungwon Hwang
18777bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_IMG_SIZE);
18877bbd891SHyungwon Hwang }
18977bbd891SHyungwon Hwang
mic_set_output_timing(struct exynos_mic * mic)19077bbd891SHyungwon Hwang static void mic_set_output_timing(struct exynos_mic *mic)
19177bbd891SHyungwon Hwang {
19277bbd891SHyungwon Hwang struct videomode vm = mic->vm;
19377bbd891SHyungwon Hwang u32 reg, bs_size_2d;
19477bbd891SHyungwon Hwang
1956be90056SInki Dae DRM_DEV_DEBUG(mic->dev, "w: %u, h: %u\n", vm.hactive, vm.vactive);
19677bbd891SHyungwon Hwang bs_size_2d = ((vm.hactive >> 2) << 1) + (vm.vactive % 4);
19777bbd891SHyungwon Hwang reg = MIC_BS_SIZE_2D(bs_size_2d);
19877bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_2);
19977bbd891SHyungwon Hwang
20077bbd891SHyungwon Hwang if (!mic->i80_mode) {
20177bbd891SHyungwon Hwang reg = MIC_H_PULSE_WIDTH_2D(vm.hsync_len) +
20277bbd891SHyungwon Hwang MIC_H_PERIOD_PIXEL_2D(vm.hsync_len + bs_size_2d +
20377bbd891SHyungwon Hwang vm.hback_porch + vm.hfront_porch);
20477bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_0);
20577bbd891SHyungwon Hwang
20677bbd891SHyungwon Hwang reg = MIC_HBP_SIZE_2D(vm.hback_porch) +
20777bbd891SHyungwon Hwang MIC_H_PERIOD_PIXEL_2D(vm.hfront_porch);
20877bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_2D_OUTPUT_TIMING_1);
20977bbd891SHyungwon Hwang }
21077bbd891SHyungwon Hwang }
21177bbd891SHyungwon Hwang
mic_set_reg_on(struct exynos_mic * mic,bool enable)21277bbd891SHyungwon Hwang static void mic_set_reg_on(struct exynos_mic *mic, bool enable)
21377bbd891SHyungwon Hwang {
21477bbd891SHyungwon Hwang u32 reg = readl(mic->reg + MIC_OP);
21577bbd891SHyungwon Hwang
21677bbd891SHyungwon Hwang if (enable) {
21777bbd891SHyungwon Hwang reg &= ~(MIC_MODE_SEL_MASK | MIC_CORE_VER_CONTROL | MIC_PSR_EN);
21877bbd891SHyungwon Hwang reg |= (MIC_CORE_EN | MIC_BS_CHG_OUT | MIC_ON_REG);
21977bbd891SHyungwon Hwang
22077bbd891SHyungwon Hwang reg &= ~MIC_MODE_SEL_COMMAND_MODE;
22177bbd891SHyungwon Hwang if (mic->i80_mode)
22277bbd891SHyungwon Hwang reg |= MIC_MODE_SEL_COMMAND_MODE;
22377bbd891SHyungwon Hwang } else {
22477bbd891SHyungwon Hwang reg &= ~MIC_CORE_EN;
22577bbd891SHyungwon Hwang }
22677bbd891SHyungwon Hwang
22777bbd891SHyungwon Hwang reg |= MIC_UPD_REG;
22877bbd891SHyungwon Hwang writel(reg, mic->reg + MIC_OP);
22977bbd891SHyungwon Hwang }
23077bbd891SHyungwon Hwang
mic_post_disable(struct drm_bridge * bridge)2318b0be572SMarek Szyprowski static void mic_post_disable(struct drm_bridge *bridge)
23277bbd891SHyungwon Hwang {
23377bbd891SHyungwon Hwang struct exynos_mic *mic = bridge->driver_private;
23477bbd891SHyungwon Hwang
23577bbd891SHyungwon Hwang mutex_lock(&mic_mutex);
23677bbd891SHyungwon Hwang if (!mic->enabled)
23777bbd891SHyungwon Hwang goto already_disabled;
23877bbd891SHyungwon Hwang
23977bbd891SHyungwon Hwang mic_set_path(mic, 0);
24077bbd891SHyungwon Hwang
2414e8ba5ccSMarek Szyprowski pm_runtime_put(mic->dev);
24277bbd891SHyungwon Hwang mic->enabled = 0;
24377bbd891SHyungwon Hwang
24477bbd891SHyungwon Hwang already_disabled:
24577bbd891SHyungwon Hwang mutex_unlock(&mic_mutex);
24677bbd891SHyungwon Hwang }
24777bbd891SHyungwon Hwang
mic_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)248e87eb57cSHoegeun Kwon static void mic_mode_set(struct drm_bridge *bridge,
24963f8f3baSLaurent Pinchart const struct drm_display_mode *mode,
25063f8f3baSLaurent Pinchart const struct drm_display_mode *adjusted_mode)
251e87eb57cSHoegeun Kwon {
252e87eb57cSHoegeun Kwon struct exynos_mic *mic = bridge->driver_private;
253e87eb57cSHoegeun Kwon
254e87eb57cSHoegeun Kwon mutex_lock(&mic_mutex);
255e87eb57cSHoegeun Kwon drm_display_mode_to_videomode(mode, &mic->vm);
25629c5079dSAndrzej Hajda mic->i80_mode = to_exynos_crtc(bridge->encoder->crtc)->i80_mode;
257e87eb57cSHoegeun Kwon mutex_unlock(&mic_mutex);
258e87eb57cSHoegeun Kwon }
259e87eb57cSHoegeun Kwon
mic_pre_enable(struct drm_bridge * bridge)2608b0be572SMarek Szyprowski static void mic_pre_enable(struct drm_bridge *bridge)
26177bbd891SHyungwon Hwang {
26277bbd891SHyungwon Hwang struct exynos_mic *mic = bridge->driver_private;
2634e8ba5ccSMarek Szyprowski int ret;
26477bbd891SHyungwon Hwang
26577bbd891SHyungwon Hwang mutex_lock(&mic_mutex);
26677bbd891SHyungwon Hwang if (mic->enabled)
2674e8ba5ccSMarek Szyprowski goto unlock;
26877bbd891SHyungwon Hwang
269a89b6c8fSTian Tao ret = pm_runtime_resume_and_get(mic->dev);
270a89b6c8fSTian Tao if (ret < 0)
2714e8ba5ccSMarek Szyprowski goto unlock;
27277bbd891SHyungwon Hwang
27377bbd891SHyungwon Hwang mic_set_path(mic, 1);
27477bbd891SHyungwon Hwang
27577bbd891SHyungwon Hwang ret = mic_sw_reset(mic);
27677bbd891SHyungwon Hwang if (ret) {
2776f83d208SInki Dae DRM_DEV_ERROR(mic->dev, "Failed to reset\n");
2784e8ba5ccSMarek Szyprowski goto turn_off;
27977bbd891SHyungwon Hwang }
28077bbd891SHyungwon Hwang
28177bbd891SHyungwon Hwang if (!mic->i80_mode)
28277bbd891SHyungwon Hwang mic_set_porch_timing(mic);
28377bbd891SHyungwon Hwang mic_set_img_size(mic);
28477bbd891SHyungwon Hwang mic_set_output_timing(mic);
28577bbd891SHyungwon Hwang mic_set_reg_on(mic, 1);
28677bbd891SHyungwon Hwang mic->enabled = 1;
28777bbd891SHyungwon Hwang mutex_unlock(&mic_mutex);
28877bbd891SHyungwon Hwang
28977bbd891SHyungwon Hwang return;
29077bbd891SHyungwon Hwang
2914e8ba5ccSMarek Szyprowski turn_off:
2924e8ba5ccSMarek Szyprowski pm_runtime_put(mic->dev);
2934e8ba5ccSMarek Szyprowski unlock:
29477bbd891SHyungwon Hwang mutex_unlock(&mic_mutex);
29577bbd891SHyungwon Hwang }
29677bbd891SHyungwon Hwang
297622688f3SMarek Szyprowski static const struct drm_bridge_funcs mic_bridge_funcs = {
298622688f3SMarek Szyprowski .post_disable = mic_post_disable,
299e87eb57cSHoegeun Kwon .mode_set = mic_mode_set,
300622688f3SMarek Szyprowski .pre_enable = mic_pre_enable,
301622688f3SMarek Szyprowski };
302622688f3SMarek Szyprowski
exynos_mic_bind(struct device * dev,struct device * master,void * data)303622688f3SMarek Szyprowski static int exynos_mic_bind(struct device *dev, struct device *master,
304622688f3SMarek Szyprowski void *data)
30577bbd891SHyungwon Hwang {
306622688f3SMarek Szyprowski struct exynos_mic *mic = dev_get_drvdata(dev);
3077d787184SMarek Szyprowski struct drm_device *drm_dev = data;
3087d787184SMarek Szyprowski struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(drm_dev,
3097d787184SMarek Szyprowski EXYNOS_DISPLAY_TYPE_LCD);
3107d787184SMarek Szyprowski struct drm_encoder *e, *encoder = NULL;
3117d787184SMarek Szyprowski
3127d787184SMarek Szyprowski drm_for_each_encoder(e, drm_dev)
3137d787184SMarek Szyprowski if (e->possible_crtcs == drm_crtc_mask(&crtc->base))
3147d787184SMarek Szyprowski encoder = e;
3157d787184SMarek Szyprowski if (!encoder)
3167d787184SMarek Szyprowski return -ENODEV;
317622688f3SMarek Szyprowski
318622688f3SMarek Szyprowski mic->bridge.driver_private = mic;
319622688f3SMarek Szyprowski
3207d787184SMarek Szyprowski return drm_bridge_attach(encoder, &mic->bridge, NULL, 0);
321622688f3SMarek Szyprowski }
322622688f3SMarek Szyprowski
exynos_mic_unbind(struct device * dev,struct device * master,void * data)323622688f3SMarek Szyprowski static void exynos_mic_unbind(struct device *dev, struct device *master,
324622688f3SMarek Szyprowski void *data)
325622688f3SMarek Szyprowski {
326622688f3SMarek Szyprowski struct exynos_mic *mic = dev_get_drvdata(dev);
32777bbd891SHyungwon Hwang
32877bbd891SHyungwon Hwang mutex_lock(&mic_mutex);
32977bbd891SHyungwon Hwang if (!mic->enabled)
33077bbd891SHyungwon Hwang goto already_disabled;
33177bbd891SHyungwon Hwang
3324e8ba5ccSMarek Szyprowski pm_runtime_put(mic->dev);
33377bbd891SHyungwon Hwang
33477bbd891SHyungwon Hwang already_disabled:
33577bbd891SHyungwon Hwang mutex_unlock(&mic_mutex);
33677bbd891SHyungwon Hwang }
33777bbd891SHyungwon Hwang
338622688f3SMarek Szyprowski static const struct component_ops exynos_mic_component_ops = {
339622688f3SMarek Szyprowski .bind = exynos_mic_bind,
340622688f3SMarek Szyprowski .unbind = exynos_mic_unbind,
34177bbd891SHyungwon Hwang };
34277bbd891SHyungwon Hwang
exynos_mic_suspend(struct device * dev)3434e8ba5ccSMarek Szyprowski static int exynos_mic_suspend(struct device *dev)
3444e8ba5ccSMarek Szyprowski {
3454e8ba5ccSMarek Szyprowski struct exynos_mic *mic = dev_get_drvdata(dev);
3464e8ba5ccSMarek Szyprowski int i;
3474e8ba5ccSMarek Szyprowski
3484e8ba5ccSMarek Szyprowski for (i = NUM_CLKS - 1; i > -1; i--)
3494e8ba5ccSMarek Szyprowski clk_disable_unprepare(mic->clks[i]);
3504e8ba5ccSMarek Szyprowski
3514e8ba5ccSMarek Szyprowski return 0;
3524e8ba5ccSMarek Szyprowski }
3534e8ba5ccSMarek Szyprowski
exynos_mic_resume(struct device * dev)3544e8ba5ccSMarek Szyprowski static int exynos_mic_resume(struct device *dev)
3554e8ba5ccSMarek Szyprowski {
3564e8ba5ccSMarek Szyprowski struct exynos_mic *mic = dev_get_drvdata(dev);
3574e8ba5ccSMarek Szyprowski int ret, i;
3584e8ba5ccSMarek Szyprowski
3594e8ba5ccSMarek Szyprowski for (i = 0; i < NUM_CLKS; i++) {
3604e8ba5ccSMarek Szyprowski ret = clk_prepare_enable(mic->clks[i]);
3614e8ba5ccSMarek Szyprowski if (ret < 0) {
3626f83d208SInki Dae DRM_DEV_ERROR(dev, "Failed to enable clock (%s)\n",
3634e8ba5ccSMarek Szyprowski clk_names[i]);
3644e8ba5ccSMarek Szyprowski while (--i > -1)
3654e8ba5ccSMarek Szyprowski clk_disable_unprepare(mic->clks[i]);
3664e8ba5ccSMarek Szyprowski return ret;
3674e8ba5ccSMarek Szyprowski }
3684e8ba5ccSMarek Szyprowski }
3694e8ba5ccSMarek Szyprowski return 0;
3704e8ba5ccSMarek Szyprowski }
3714e8ba5ccSMarek Szyprowski
372*1d9e6664SPaul Cercueil static DEFINE_RUNTIME_DEV_PM_OPS(exynos_mic_pm_ops, exynos_mic_suspend,
373*1d9e6664SPaul Cercueil exynos_mic_resume, NULL);
3744e8ba5ccSMarek Szyprowski
exynos_mic_probe(struct platform_device * pdev)3758b0be572SMarek Szyprowski static int exynos_mic_probe(struct platform_device *pdev)
37677bbd891SHyungwon Hwang {
37777bbd891SHyungwon Hwang struct device *dev = &pdev->dev;
37877bbd891SHyungwon Hwang struct exynos_mic *mic;
37977bbd891SHyungwon Hwang struct resource res;
38077bbd891SHyungwon Hwang int ret, i;
38177bbd891SHyungwon Hwang
38277bbd891SHyungwon Hwang mic = devm_kzalloc(dev, sizeof(*mic), GFP_KERNEL);
38377bbd891SHyungwon Hwang if (!mic) {
3846f83d208SInki Dae DRM_DEV_ERROR(dev,
3856f83d208SInki Dae "mic: Failed to allocate memory for MIC object\n");
38677bbd891SHyungwon Hwang ret = -ENOMEM;
38777bbd891SHyungwon Hwang goto err;
38877bbd891SHyungwon Hwang }
38977bbd891SHyungwon Hwang
39077bbd891SHyungwon Hwang mic->dev = dev;
39177bbd891SHyungwon Hwang
39277bbd891SHyungwon Hwang ret = of_address_to_resource(dev->of_node, 0, &res);
39377bbd891SHyungwon Hwang if (ret) {
3946f83d208SInki Dae DRM_DEV_ERROR(dev, "mic: Failed to get mem region for MIC\n");
39577bbd891SHyungwon Hwang goto err;
39677bbd891SHyungwon Hwang }
39777bbd891SHyungwon Hwang mic->reg = devm_ioremap(dev, res.start, resource_size(&res));
39877bbd891SHyungwon Hwang if (!mic->reg) {
3996f83d208SInki Dae DRM_DEV_ERROR(dev, "mic: Failed to remap for MIC\n");
40077bbd891SHyungwon Hwang ret = -ENOMEM;
40177bbd891SHyungwon Hwang goto err;
40277bbd891SHyungwon Hwang }
40377bbd891SHyungwon Hwang
40477bbd891SHyungwon Hwang mic->sysreg = syscon_regmap_lookup_by_phandle(dev->of_node,
40577bbd891SHyungwon Hwang "samsung,disp-syscon");
40677bbd891SHyungwon Hwang if (IS_ERR(mic->sysreg)) {
4076f83d208SInki Dae DRM_DEV_ERROR(dev, "mic: Failed to get system register.\n");
4086c9c1581SDan Carpenter ret = PTR_ERR(mic->sysreg);
40977bbd891SHyungwon Hwang goto err;
41077bbd891SHyungwon Hwang }
41177bbd891SHyungwon Hwang
41277bbd891SHyungwon Hwang for (i = 0; i < NUM_CLKS; i++) {
41338b5e5f4SMarek Szyprowski mic->clks[i] = devm_clk_get(dev, clk_names[i]);
41477bbd891SHyungwon Hwang if (IS_ERR(mic->clks[i])) {
4156f83d208SInki Dae DRM_DEV_ERROR(dev, "mic: Failed to get clock (%s)\n",
41677bbd891SHyungwon Hwang clk_names[i]);
41777bbd891SHyungwon Hwang ret = PTR_ERR(mic->clks[i]);
41877bbd891SHyungwon Hwang goto err;
41977bbd891SHyungwon Hwang }
42077bbd891SHyungwon Hwang }
42177bbd891SHyungwon Hwang
422622688f3SMarek Szyprowski platform_set_drvdata(pdev, mic);
423622688f3SMarek Szyprowski
424576d72fbSInki Dae mic->bridge.funcs = &mic_bridge_funcs;
425576d72fbSInki Dae mic->bridge.of_node = dev->of_node;
426576d72fbSInki Dae
427059e3c0bSInki Dae drm_bridge_add(&mic->bridge);
428576d72fbSInki Dae
4294e8ba5ccSMarek Szyprowski pm_runtime_enable(dev);
43077bbd891SHyungwon Hwang
4314e8ba5ccSMarek Szyprowski ret = component_add(dev, &exynos_mic_component_ops);
4324e8ba5ccSMarek Szyprowski if (ret)
4334e8ba5ccSMarek Szyprowski goto err_pm;
4344e8ba5ccSMarek Szyprowski
4356be90056SInki Dae DRM_DEV_DEBUG_KMS(dev, "MIC has been probed\n");
4364e8ba5ccSMarek Szyprowski
4374e8ba5ccSMarek Szyprowski return 0;
4384e8ba5ccSMarek Szyprowski
4394e8ba5ccSMarek Szyprowski err_pm:
4404e8ba5ccSMarek Szyprowski pm_runtime_disable(dev);
44177bbd891SHyungwon Hwang err:
44277bbd891SHyungwon Hwang return ret;
44377bbd891SHyungwon Hwang }
44477bbd891SHyungwon Hwang
exynos_mic_remove(struct platform_device * pdev)44577bbd891SHyungwon Hwang static int exynos_mic_remove(struct platform_device *pdev)
44677bbd891SHyungwon Hwang {
447576d72fbSInki Dae struct exynos_mic *mic = platform_get_drvdata(pdev);
448576d72fbSInki Dae
449622688f3SMarek Szyprowski component_del(&pdev->dev, &exynos_mic_component_ops);
4504e8ba5ccSMarek Szyprowski pm_runtime_disable(&pdev->dev);
451576d72fbSInki Dae
452576d72fbSInki Dae drm_bridge_remove(&mic->bridge);
453576d72fbSInki Dae
45477bbd891SHyungwon Hwang return 0;
45577bbd891SHyungwon Hwang }
45677bbd891SHyungwon Hwang
45777bbd891SHyungwon Hwang static const struct of_device_id exynos_mic_of_match[] = {
45877bbd891SHyungwon Hwang { .compatible = "samsung,exynos5433-mic" },
45977bbd891SHyungwon Hwang { }
46077bbd891SHyungwon Hwang };
46177bbd891SHyungwon Hwang MODULE_DEVICE_TABLE(of, exynos_mic_of_match);
46277bbd891SHyungwon Hwang
46377bbd891SHyungwon Hwang struct platform_driver mic_driver = {
46477bbd891SHyungwon Hwang .probe = exynos_mic_probe,
46577bbd891SHyungwon Hwang .remove = exynos_mic_remove,
46677bbd891SHyungwon Hwang .driver = {
46777bbd891SHyungwon Hwang .name = "exynos-mic",
468*1d9e6664SPaul Cercueil .pm = pm_ptr(&exynos_mic_pm_ops),
46977bbd891SHyungwon Hwang .owner = THIS_MODULE,
47077bbd891SHyungwon Hwang .of_match_table = exynos_mic_of_match,
47177bbd891SHyungwon Hwang },
47277bbd891SHyungwon Hwang };
473