1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
4  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5  */
6 
7 #include <linux/platform_device.h>
8 
9 #include <drm/drm_drv.h>
10 #include <drm/drm_print.h>
11 
12 #include "tidss_crtc.h"
13 #include "tidss_dispc.h"
14 #include "tidss_drv.h"
15 #include "tidss_irq.h"
16 #include "tidss_plane.h"
17 
18 /* call with wait_lock and dispc runtime held */
tidss_irq_update(struct tidss_device * tidss)19 static void tidss_irq_update(struct tidss_device *tidss)
20 {
21 	assert_spin_locked(&tidss->wait_lock);
22 
23 	dispc_set_irqenable(tidss->dispc, tidss->irq_mask);
24 }
25 
tidss_irq_enable_vblank(struct drm_crtc * crtc)26 void tidss_irq_enable_vblank(struct drm_crtc *crtc)
27 {
28 	struct drm_device *ddev = crtc->dev;
29 	struct tidss_device *tidss = to_tidss(ddev);
30 	struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
31 	u32 hw_videoport = tcrtc->hw_videoport;
32 	unsigned long flags;
33 
34 	spin_lock_irqsave(&tidss->wait_lock, flags);
35 	tidss->irq_mask |= DSS_IRQ_VP_VSYNC_EVEN(hw_videoport) |
36 			   DSS_IRQ_VP_VSYNC_ODD(hw_videoport);
37 	tidss_irq_update(tidss);
38 	spin_unlock_irqrestore(&tidss->wait_lock, flags);
39 }
40 
tidss_irq_disable_vblank(struct drm_crtc * crtc)41 void tidss_irq_disable_vblank(struct drm_crtc *crtc)
42 {
43 	struct drm_device *ddev = crtc->dev;
44 	struct tidss_device *tidss = to_tidss(ddev);
45 	struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
46 	u32 hw_videoport = tcrtc->hw_videoport;
47 	unsigned long flags;
48 
49 	spin_lock_irqsave(&tidss->wait_lock, flags);
50 	tidss->irq_mask &= ~(DSS_IRQ_VP_VSYNC_EVEN(hw_videoport) |
51 			     DSS_IRQ_VP_VSYNC_ODD(hw_videoport));
52 	tidss_irq_update(tidss);
53 	spin_unlock_irqrestore(&tidss->wait_lock, flags);
54 }
55 
tidss_irq_handler(int irq,void * arg)56 static irqreturn_t tidss_irq_handler(int irq, void *arg)
57 {
58 	struct drm_device *ddev = (struct drm_device *)arg;
59 	struct tidss_device *tidss = to_tidss(ddev);
60 	unsigned int id;
61 	dispc_irq_t irqstatus;
62 
63 	irqstatus = dispc_read_and_clear_irqstatus(tidss->dispc);
64 
65 	for (id = 0; id < tidss->num_crtcs; id++) {
66 		struct drm_crtc *crtc = tidss->crtcs[id];
67 		struct tidss_crtc *tcrtc = to_tidss_crtc(crtc);
68 		u32 hw_videoport = tcrtc->hw_videoport;
69 
70 		if (irqstatus & (DSS_IRQ_VP_VSYNC_EVEN(hw_videoport) |
71 				 DSS_IRQ_VP_VSYNC_ODD(hw_videoport)))
72 			tidss_crtc_vblank_irq(crtc);
73 
74 		if (irqstatus & (DSS_IRQ_VP_FRAME_DONE(hw_videoport)))
75 			tidss_crtc_framedone_irq(crtc);
76 
77 		if (irqstatus & DSS_IRQ_VP_SYNC_LOST(hw_videoport))
78 			tidss_crtc_error_irq(crtc, irqstatus);
79 	}
80 
81 	if (irqstatus & DSS_IRQ_DEVICE_OCP_ERR)
82 		dev_err_ratelimited(tidss->dev, "OCP error\n");
83 
84 	return IRQ_HANDLED;
85 }
86 
tidss_irq_resume(struct tidss_device * tidss)87 void tidss_irq_resume(struct tidss_device *tidss)
88 {
89 	unsigned long flags;
90 
91 	spin_lock_irqsave(&tidss->wait_lock, flags);
92 	tidss_irq_update(tidss);
93 	spin_unlock_irqrestore(&tidss->wait_lock, flags);
94 }
95 
tidss_irq_preinstall(struct drm_device * ddev)96 static void tidss_irq_preinstall(struct drm_device *ddev)
97 {
98 	struct tidss_device *tidss = to_tidss(ddev);
99 
100 	spin_lock_init(&tidss->wait_lock);
101 
102 	tidss_runtime_get(tidss);
103 
104 	dispc_set_irqenable(tidss->dispc, 0);
105 	dispc_read_and_clear_irqstatus(tidss->dispc);
106 
107 	tidss_runtime_put(tidss);
108 }
109 
tidss_irq_postinstall(struct drm_device * ddev)110 static void tidss_irq_postinstall(struct drm_device *ddev)
111 {
112 	struct tidss_device *tidss = to_tidss(ddev);
113 	unsigned long flags;
114 	unsigned int i;
115 
116 	tidss_runtime_get(tidss);
117 
118 	spin_lock_irqsave(&tidss->wait_lock, flags);
119 
120 	tidss->irq_mask = DSS_IRQ_DEVICE_OCP_ERR;
121 
122 	for (i = 0; i < tidss->num_crtcs; ++i) {
123 		struct tidss_crtc *tcrtc = to_tidss_crtc(tidss->crtcs[i]);
124 
125 		tidss->irq_mask |= DSS_IRQ_VP_SYNC_LOST(tcrtc->hw_videoport);
126 
127 		tidss->irq_mask |= DSS_IRQ_VP_FRAME_DONE(tcrtc->hw_videoport);
128 	}
129 
130 	tidss_irq_update(tidss);
131 
132 	spin_unlock_irqrestore(&tidss->wait_lock, flags);
133 
134 	tidss_runtime_put(tidss);
135 }
136 
tidss_irq_install(struct drm_device * ddev,unsigned int irq)137 int tidss_irq_install(struct drm_device *ddev, unsigned int irq)
138 {
139 	int ret;
140 
141 	if (irq == IRQ_NOTCONNECTED)
142 		return -ENOTCONN;
143 
144 	tidss_irq_preinstall(ddev);
145 
146 	ret = request_irq(irq, tidss_irq_handler, 0, ddev->driver->name, ddev);
147 	if (ret)
148 		return ret;
149 
150 	tidss_irq_postinstall(ddev);
151 
152 	return 0;
153 }
154 
tidss_irq_uninstall(struct drm_device * ddev)155 void tidss_irq_uninstall(struct drm_device *ddev)
156 {
157 	struct tidss_device *tidss = to_tidss(ddev);
158 
159 	tidss_runtime_get(tidss);
160 	dispc_set_irqenable(tidss->dispc, 0);
161 	tidss_runtime_put(tidss);
162 
163 	free_irq(tidss->irq, ddev);
164 }
165