1/*
2 * hdmi_panel.c
3 *
4 * HDMI library support functions for TI OMAP4 processors.
5 *
6 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
7 * Authors:	Mythri P k <mythripk@ti.com>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/kernel.h>
23#include <linux/err.h>
24#include <linux/io.h>
25#include <linux/mutex.h>
26#include <linux/module.h>
27#include <video/omapdss.h>
28#include <linux/slab.h>
29
30#include "dss.h"
31
32static struct {
33	struct mutex hdmi_lock;
34} hdmi;
35
36
37static int hdmi_panel_probe(struct omap_dss_device *dssdev)
38{
39	DSSDBG("ENTER hdmi_panel_probe\n");
40
41	dssdev->panel.config = OMAP_DSS_LCD_TFT |
42			OMAP_DSS_LCD_IVS | OMAP_DSS_LCD_IHS;
43
44	dssdev->panel.timings = (struct omap_video_timings){640, 480, 25175, 96, 16, 48, 2 , 11, 31};
45
46	DSSDBG("hdmi_panel_probe x_res= %d y_res = %d\n",
47		dssdev->panel.timings.x_res,
48		dssdev->panel.timings.y_res);
49	return 0;
50}
51
52static void hdmi_panel_remove(struct omap_dss_device *dssdev)
53{
54
55}
56
57static int hdmi_panel_enable(struct omap_dss_device *dssdev)
58{
59	int r = 0;
60	DSSDBG("ENTER hdmi_panel_enable\n");
61
62	mutex_lock(&hdmi.hdmi_lock);
63
64	if (dssdev->state != OMAP_DSS_DISPLAY_DISABLED) {
65		r = -EINVAL;
66		goto err;
67	}
68
69	r = omapdss_hdmi_display_enable(dssdev);
70	if (r) {
71		DSSERR("failed to power on\n");
72		goto err;
73	}
74
75	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
76
77err:
78	mutex_unlock(&hdmi.hdmi_lock);
79
80	return r;
81}
82
83static void hdmi_panel_disable(struct omap_dss_device *dssdev)
84{
85	mutex_lock(&hdmi.hdmi_lock);
86
87	if (dssdev->state == OMAP_DSS_DISPLAY_ACTIVE)
88		omapdss_hdmi_display_disable(dssdev);
89
90	dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
91
92	mutex_unlock(&hdmi.hdmi_lock);
93}
94
95static int hdmi_panel_suspend(struct omap_dss_device *dssdev)
96{
97	int r = 0;
98
99	mutex_lock(&hdmi.hdmi_lock);
100
101	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
102		r = -EINVAL;
103		goto err;
104	}
105
106	dssdev->state = OMAP_DSS_DISPLAY_SUSPENDED;
107
108	omapdss_hdmi_display_disable(dssdev);
109
110err:
111	mutex_unlock(&hdmi.hdmi_lock);
112
113	return r;
114}
115
116static int hdmi_panel_resume(struct omap_dss_device *dssdev)
117{
118	int r = 0;
119
120	mutex_lock(&hdmi.hdmi_lock);
121
122	if (dssdev->state != OMAP_DSS_DISPLAY_SUSPENDED) {
123		r = -EINVAL;
124		goto err;
125	}
126
127	r = omapdss_hdmi_display_enable(dssdev);
128	if (r) {
129		DSSERR("failed to power on\n");
130		goto err;
131	}
132
133	dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
134
135err:
136	mutex_unlock(&hdmi.hdmi_lock);
137
138	return r;
139}
140
141static void hdmi_get_timings(struct omap_dss_device *dssdev,
142			struct omap_video_timings *timings)
143{
144	mutex_lock(&hdmi.hdmi_lock);
145
146	*timings = dssdev->panel.timings;
147
148	mutex_unlock(&hdmi.hdmi_lock);
149}
150
151static void hdmi_set_timings(struct omap_dss_device *dssdev,
152			struct omap_video_timings *timings)
153{
154	DSSDBG("hdmi_set_timings\n");
155
156	mutex_lock(&hdmi.hdmi_lock);
157
158	dssdev->panel.timings = *timings;
159	omapdss_hdmi_display_set_timing(dssdev);
160
161	mutex_unlock(&hdmi.hdmi_lock);
162}
163
164static int hdmi_check_timings(struct omap_dss_device *dssdev,
165			struct omap_video_timings *timings)
166{
167	int r = 0;
168
169	DSSDBG("hdmi_check_timings\n");
170
171	mutex_lock(&hdmi.hdmi_lock);
172
173	r = omapdss_hdmi_display_check_timing(dssdev, timings);
174
175	mutex_unlock(&hdmi.hdmi_lock);
176	return r;
177}
178
179static int hdmi_read_edid(struct omap_dss_device *dssdev, u8 *buf, int len)
180{
181	int r;
182
183	mutex_lock(&hdmi.hdmi_lock);
184
185	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
186		r = omapdss_hdmi_display_enable(dssdev);
187		if (r)
188			goto err;
189	}
190
191	r = omapdss_hdmi_read_edid(buf, len);
192
193	if (dssdev->state == OMAP_DSS_DISPLAY_DISABLED ||
194			dssdev->state == OMAP_DSS_DISPLAY_SUSPENDED)
195		omapdss_hdmi_display_disable(dssdev);
196err:
197	mutex_unlock(&hdmi.hdmi_lock);
198
199	return r;
200}
201
202static bool hdmi_detect(struct omap_dss_device *dssdev)
203{
204	int r;
205
206	mutex_lock(&hdmi.hdmi_lock);
207
208	if (dssdev->state != OMAP_DSS_DISPLAY_ACTIVE) {
209		r = omapdss_hdmi_display_enable(dssdev);
210		if (r)
211			goto err;
212	}
213
214	r = omapdss_hdmi_detect();
215
216	if (dssdev->state == OMAP_DSS_DISPLAY_DISABLED ||
217			dssdev->state == OMAP_DSS_DISPLAY_SUSPENDED)
218		omapdss_hdmi_display_disable(dssdev);
219err:
220	mutex_unlock(&hdmi.hdmi_lock);
221
222	return r;
223}
224
225static struct omap_dss_driver hdmi_driver = {
226	.probe		= hdmi_panel_probe,
227	.remove		= hdmi_panel_remove,
228	.enable		= hdmi_panel_enable,
229	.disable	= hdmi_panel_disable,
230	.suspend	= hdmi_panel_suspend,
231	.resume		= hdmi_panel_resume,
232	.get_timings	= hdmi_get_timings,
233	.set_timings	= hdmi_set_timings,
234	.check_timings	= hdmi_check_timings,
235	.read_edid	= hdmi_read_edid,
236	.detect		= hdmi_detect,
237	.driver			= {
238		.name   = "hdmi_panel",
239		.owner  = THIS_MODULE,
240	},
241};
242
243int hdmi_panel_init(void)
244{
245	mutex_init(&hdmi.hdmi_lock);
246
247	omap_dss_register_driver(&hdmi_driver);
248
249	return 0;
250}
251
252void hdmi_panel_exit(void)
253{
254	omap_dss_unregister_driver(&hdmi_driver);
255
256}
257