2 * SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation
7 #include <linux/irqchip.h>
8 #include <linux/irqdesc.h>
9 #include <linux/irqchip/chained_irq.h>
11 #include <linux/interconnect.h>
13 #define to_dpu_mdss(x) container_of(x, struct dpu_mdss, base)
15 #define HW_INTR_STATUS 0x0010
17 /* Max BW defined in KBps */
18 #define MAX_BW 6800000
20 struct dpu_irq_controller
{
21 unsigned long enabled_mask
;
22 struct irq_domain
*domain
;
28 unsigned long mmio_len
;
29 struct dss_module_power mp
;
30 struct dpu_irq_controller irq_controller
;
31 struct icc_path
*path
[2];
35 static int dpu_mdss_parse_data_bus_icc_path(struct drm_device
*dev
,
36 struct dpu_mdss
*dpu_mdss
)
38 struct icc_path
*path0
= of_icc_get(dev
->dev
, "mdp0-mem");
39 struct icc_path
*path1
= of_icc_get(dev
->dev
, "mdp1-mem");
41 if (IS_ERR_OR_NULL(path0
))
42 return PTR_ERR_OR_ZERO(path0
);
44 dpu_mdss
->path
[0] = path0
;
45 dpu_mdss
->num_paths
= 1;
47 if (!IS_ERR_OR_NULL(path1
)) {
48 dpu_mdss
->path
[1] = path1
;
49 dpu_mdss
->num_paths
++;
55 static void dpu_mdss_icc_request_bw(struct msm_mdss
*mdss
)
57 struct dpu_mdss
*dpu_mdss
= to_dpu_mdss(mdss
);
59 u64 avg_bw
= dpu_mdss
->num_paths
? MAX_BW
/ dpu_mdss
->num_paths
: 0;
61 for (i
= 0; i
< dpu_mdss
->num_paths
; i
++)
62 icc_set_bw(dpu_mdss
->path
[i
], avg_bw
, kBps_to_icc(MAX_BW
));
65 static void dpu_mdss_irq(struct irq_desc
*desc
)
67 struct dpu_mdss
*dpu_mdss
= irq_desc_get_handler_data(desc
);
68 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
71 chained_irq_enter(chip
, desc
);
73 interrupts
= readl_relaxed(dpu_mdss
->mmio
+ HW_INTR_STATUS
);
76 irq_hw_number_t hwirq
= fls(interrupts
) - 1;
80 mapping
= irq_find_mapping(dpu_mdss
->irq_controller
.domain
,
83 DRM_ERROR("couldn't find irq mapping for %lu\n", hwirq
);
87 rc
= generic_handle_irq(mapping
);
89 DRM_ERROR("handle irq fail: irq=%lu mapping=%u rc=%d\n",
94 interrupts
&= ~(1 << hwirq
);
97 chained_irq_exit(chip
, desc
);
100 static void dpu_mdss_irq_mask(struct irq_data
*irqd
)
102 struct dpu_mdss
*dpu_mdss
= irq_data_get_irq_chip_data(irqd
);
105 smp_mb__before_atomic();
106 clear_bit(irqd
->hwirq
, &dpu_mdss
->irq_controller
.enabled_mask
);
108 smp_mb__after_atomic();
111 static void dpu_mdss_irq_unmask(struct irq_data
*irqd
)
113 struct dpu_mdss
*dpu_mdss
= irq_data_get_irq_chip_data(irqd
);
116 smp_mb__before_atomic();
117 set_bit(irqd
->hwirq
, &dpu_mdss
->irq_controller
.enabled_mask
);
119 smp_mb__after_atomic();
122 static struct irq_chip dpu_mdss_irq_chip
= {
124 .irq_mask
= dpu_mdss_irq_mask
,
125 .irq_unmask
= dpu_mdss_irq_unmask
,
128 static struct lock_class_key dpu_mdss_lock_key
, dpu_mdss_request_key
;
130 static int dpu_mdss_irqdomain_map(struct irq_domain
*domain
,
131 unsigned int irq
, irq_hw_number_t hwirq
)
133 struct dpu_mdss
*dpu_mdss
= domain
->host_data
;
135 irq_set_lockdep_class(irq
, &dpu_mdss_lock_key
, &dpu_mdss_request_key
);
136 irq_set_chip_and_handler(irq
, &dpu_mdss_irq_chip
, handle_level_irq
);
137 return irq_set_chip_data(irq
, dpu_mdss
);
140 static const struct irq_domain_ops dpu_mdss_irqdomain_ops
= {
141 .map
= dpu_mdss_irqdomain_map
,
142 .xlate
= irq_domain_xlate_onecell
,
145 static int _dpu_mdss_irq_domain_add(struct dpu_mdss
*dpu_mdss
)
148 struct irq_domain
*domain
;
150 dev
= dpu_mdss
->base
.dev
->dev
;
152 domain
= irq_domain_add_linear(dev
->of_node
, 32,
153 &dpu_mdss_irqdomain_ops
, dpu_mdss
);
155 DPU_ERROR("failed to add irq_domain\n");
159 dpu_mdss
->irq_controller
.enabled_mask
= 0;
160 dpu_mdss
->irq_controller
.domain
= domain
;
165 static void _dpu_mdss_irq_domain_fini(struct dpu_mdss
*dpu_mdss
)
167 if (dpu_mdss
->irq_controller
.domain
) {
168 irq_domain_remove(dpu_mdss
->irq_controller
.domain
);
169 dpu_mdss
->irq_controller
.domain
= NULL
;
172 static int dpu_mdss_enable(struct msm_mdss
*mdss
)
174 struct dpu_mdss
*dpu_mdss
= to_dpu_mdss(mdss
);
175 struct dss_module_power
*mp
= &dpu_mdss
->mp
;
178 dpu_mdss_icc_request_bw(mdss
);
180 ret
= msm_dss_enable_clk(mp
->clk_config
, mp
->num_clk
, true);
182 DPU_ERROR("clock enable failed, ret:%d\n", ret
);
187 static int dpu_mdss_disable(struct msm_mdss
*mdss
)
189 struct dpu_mdss
*dpu_mdss
= to_dpu_mdss(mdss
);
190 struct dss_module_power
*mp
= &dpu_mdss
->mp
;
193 ret
= msm_dss_enable_clk(mp
->clk_config
, mp
->num_clk
, false);
195 DPU_ERROR("clock disable failed, ret:%d\n", ret
);
197 for (i
= 0; i
< dpu_mdss
->num_paths
; i
++)
198 icc_set_bw(dpu_mdss
->path
[i
], 0, 0);
203 static void dpu_mdss_destroy(struct drm_device
*dev
)
205 struct platform_device
*pdev
= to_platform_device(dev
->dev
);
206 struct msm_drm_private
*priv
= dev
->dev_private
;
207 struct dpu_mdss
*dpu_mdss
= to_dpu_mdss(priv
->mdss
);
208 struct dss_module_power
*mp
= &dpu_mdss
->mp
;
212 pm_runtime_suspend(dev
->dev
);
213 pm_runtime_disable(dev
->dev
);
214 _dpu_mdss_irq_domain_fini(dpu_mdss
);
215 irq
= platform_get_irq(pdev
, 0);
216 irq_set_chained_handler_and_data(irq
, NULL
, NULL
);
217 msm_dss_put_clk(mp
->clk_config
, mp
->num_clk
);
218 devm_kfree(&pdev
->dev
, mp
->clk_config
);
220 for (i
= 0; i
< dpu_mdss
->num_paths
; i
++)
221 icc_put(dpu_mdss
->path
[i
]);
224 devm_iounmap(&pdev
->dev
, dpu_mdss
->mmio
);
225 dpu_mdss
->mmio
= NULL
;
229 static const struct msm_mdss_funcs mdss_funcs
= {
230 .enable
= dpu_mdss_enable
,
231 .disable
= dpu_mdss_disable
,
232 .destroy
= dpu_mdss_destroy
,
235 int dpu_mdss_init(struct drm_device
*dev
)
237 struct platform_device
*pdev
= to_platform_device(dev
->dev
);
238 struct msm_drm_private
*priv
= dev
->dev_private
;
239 struct resource
*res
;
240 struct dpu_mdss
*dpu_mdss
;
241 struct dss_module_power
*mp
;
245 dpu_mdss
= devm_kzalloc(dev
->dev
, sizeof(*dpu_mdss
), GFP_KERNEL
);
249 dpu_mdss
->mmio
= msm_ioremap(pdev
, "mdss", "mdss");
250 if (IS_ERR(dpu_mdss
->mmio
))
251 return PTR_ERR(dpu_mdss
->mmio
);
253 DRM_DEBUG("mapped mdss address space @%pK\n", dpu_mdss
->mmio
);
255 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mdss");
257 DRM_ERROR("failed to get memory resource for mdss\n");
260 dpu_mdss
->mmio_len
= resource_size(res
);
262 ret
= dpu_mdss_parse_data_bus_icc_path(dev
, dpu_mdss
);
267 ret
= msm_dss_parse_clock(pdev
, mp
);
269 DPU_ERROR("failed to parse clocks, ret=%d\n", ret
);
273 dpu_mdss
->base
.dev
= dev
;
274 dpu_mdss
->base
.funcs
= &mdss_funcs
;
276 ret
= _dpu_mdss_irq_domain_add(dpu_mdss
);
278 goto irq_domain_error
;
280 irq
= platform_get_irq(pdev
, 0);
284 irq_set_chained_handler_and_data(irq
, dpu_mdss_irq
,
287 priv
->mdss
= &dpu_mdss
->base
;
289 pm_runtime_enable(dev
->dev
);
291 dpu_mdss_icc_request_bw(priv
->mdss
);
296 _dpu_mdss_irq_domain_fini(dpu_mdss
);
298 msm_dss_put_clk(mp
->clk_config
, mp
->num_clk
);
300 devm_kfree(&pdev
->dev
, mp
->clk_config
);
302 devm_iounmap(&pdev
->dev
, dpu_mdss
->mmio
);
303 dpu_mdss
->mmio
= NULL
;