OMAP4: DSS2: HDMI: Split the current HDMI driver to move
[pandora-kernel.git] / drivers / video / omap2 / dss / hdmi.c
1 /*
2  * hdmi.c
3  *
4  * HDMI interface DSS driver setting for TI's OMAP4 family of processor.
5  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
6  * Authors: Yong Zhi
7  *      Mythri pk <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 #define DSS_SUBSYS_NAME "HDMI"
23
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/interrupt.h>
29 #include <linux/mutex.h>
30 #include <linux/delay.h>
31 #include <linux/string.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/clk.h>
35 #include <video/omapdss.h>
36 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
37         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
38 #include <sound/soc.h>
39 #include <sound/pcm_params.h>
40 #endif
41
42 #include "ti_hdmi.h"
43 #include "dss.h"
44 #include "dss_features.h"
45
46 #define HDMI_WP                 0x0
47 #define HDMI_CORE_SYS           0x400
48 #define HDMI_CORE_AV            0x900
49 #define HDMI_PLLCTRL            0x200
50 #define HDMI_PHY                0x300
51
52 /* HDMI EDID Length move this */
53 #define HDMI_EDID_MAX_LENGTH                    256
54 #define EDID_TIMING_DESCRIPTOR_SIZE             0x12
55 #define EDID_DESCRIPTOR_BLOCK0_ADDRESS          0x36
56 #define EDID_DESCRIPTOR_BLOCK1_ADDRESS          0x80
57 #define EDID_SIZE_BLOCK0_TIMING_DESCRIPTOR      4
58 #define EDID_SIZE_BLOCK1_TIMING_DESCRIPTOR      4
59
60 #define OMAP_HDMI_TIMINGS_NB                    34
61
62 static struct {
63         struct mutex lock;
64         struct omap_display_platform_data *pdata;
65         struct platform_device *pdev;
66         struct hdmi_ip_data ip_data;
67         int code;
68         int mode;
69         u8 edid[HDMI_EDID_MAX_LENGTH];
70         u8 edid_set;
71         bool custom_set;
72
73         struct clk *sys_clk;
74 } hdmi;
75
76 /*
77  * Logic for the below structure :
78  * user enters the CEA or VESA timings by specifying the HDMI/DVI code.
79  * There is a correspondence between CEA/VESA timing and code, please
80  * refer to section 6.3 in HDMI 1.3 specification for timing code.
81  *
82  * In the below structure, cea_vesa_timings corresponds to all OMAP4
83  * supported CEA and VESA timing values.code_cea corresponds to the CEA
84  * code, It is used to get the timing from cea_vesa_timing array.Similarly
85  * with code_vesa. Code_index is used for back mapping, that is once EDID
86  * is read from the TV, EDID is parsed to find the timing values and then
87  * map it to corresponding CEA or VESA index.
88  */
89
90 static const struct hdmi_timings cea_vesa_timings[OMAP_HDMI_TIMINGS_NB] = {
91         { {640, 480, 25200, 96, 16, 48, 2, 10, 33} , 0 , 0},
92         { {1280, 720, 74250, 40, 440, 220, 5, 5, 20}, 1, 1},
93         { {1280, 720, 74250, 40, 110, 220, 5, 5, 20}, 1, 1},
94         { {720, 480, 27027, 62, 16, 60, 6, 9, 30}, 0, 0},
95         { {2880, 576, 108000, 256, 48, 272, 5, 5, 39}, 0, 0},
96         { {1440, 240, 27027, 124, 38, 114, 3, 4, 15}, 0, 0},
97         { {1440, 288, 27000, 126, 24, 138, 3, 2, 19}, 0, 0},
98         { {1920, 540, 74250, 44, 528, 148, 5, 2, 15}, 1, 1},
99         { {1920, 540, 74250, 44, 88, 148, 5, 2, 15}, 1, 1},
100         { {1920, 1080, 148500, 44, 88, 148, 5, 4, 36}, 1, 1},
101         { {720, 576, 27000, 64, 12, 68, 5, 5, 39}, 0, 0},
102         { {1440, 576, 54000, 128, 24, 136, 5, 5, 39}, 0, 0},
103         { {1920, 1080, 148500, 44, 528, 148, 5, 4, 36}, 1, 1},
104         { {2880, 480, 108108, 248, 64, 240, 6, 9, 30}, 0, 0},
105         { {1920, 1080, 74250, 44, 638, 148, 5, 4, 36}, 1, 1},
106         /* VESA From Here */
107         { {640, 480, 25175, 96, 16, 48, 2 , 11, 31}, 0, 0},
108         { {800, 600, 40000, 128, 40, 88, 4 , 1, 23}, 1, 1},
109         { {848, 480, 33750, 112, 16, 112, 8 , 6, 23}, 1, 1},
110         { {1280, 768, 79500, 128, 64, 192, 7 , 3, 20}, 1, 0},
111         { {1280, 800, 83500, 128, 72, 200, 6 , 3, 22}, 1, 0},
112         { {1360, 768, 85500, 112, 64, 256, 6 , 3, 18}, 1, 1},
113         { {1280, 960, 108000, 112, 96, 312, 3 , 1, 36}, 1, 1},
114         { {1280, 1024, 108000, 112, 48, 248, 3 , 1, 38}, 1, 1},
115         { {1024, 768, 65000, 136, 24, 160, 6, 3, 29}, 0, 0},
116         { {1400, 1050, 121750, 144, 88, 232, 4, 3, 32}, 1, 0},
117         { {1440, 900, 106500, 152, 80, 232, 6, 3, 25}, 1, 0},
118         { {1680, 1050, 146250, 176 , 104, 280, 6, 3, 30}, 1, 0},
119         { {1366, 768, 85500, 143, 70, 213, 3, 3, 24}, 1, 1},
120         { {1920, 1080, 148500, 44, 148, 80, 5, 4, 36}, 1, 1},
121         { {1280, 768, 68250, 32, 48, 80, 7, 3, 12}, 0, 1},
122         { {1400, 1050, 101000, 32, 48, 80, 4, 3, 23}, 0, 1},
123         { {1680, 1050, 119000, 32, 48, 80, 6, 3, 21}, 0, 1},
124         { {1280, 800, 79500, 32, 48, 80, 6, 3, 14}, 0, 1},
125         { {1280, 720, 74250, 40, 110, 220, 5, 5, 20}, 1, 1}
126 };
127
128 /*
129  * This is a static mapping array which maps the timing values
130  * with corresponding CEA / VESA code
131  */
132 static const int code_index[OMAP_HDMI_TIMINGS_NB] = {
133         1, 19, 4, 2, 37, 6, 21, 20, 5, 16, 17, 29, 31, 35, 32,
134         /* <--15 CEA 17--> vesa*/
135         4, 9, 0xE, 0x17, 0x1C, 0x27, 0x20, 0x23, 0x10, 0x2A,
136         0X2F, 0x3A, 0X51, 0X52, 0x16, 0x29, 0x39, 0x1B
137 };
138
139 /*
140  * This is reverse static mapping which maps the CEA / VESA code
141  * to the corresponding timing values
142  */
143 static const int code_cea[39] = {
144         -1,  0,  3,  3,  2,  8,  5,  5, -1, -1,
145         -1, -1, -1, -1, -1, -1,  9, 10, 10,  1,
146         7,   6,  6, -1, -1, -1, -1, -1, -1, 11,
147         11, 12, 14, -1, -1, 13, 13,  4,  4
148 };
149
150 static const int code_vesa[85] = {
151         -1, -1, -1, -1, 15, -1, -1, -1, -1, 16,
152         -1, -1, -1, -1, 17, -1, 23, -1, -1, -1,
153         -1, -1, 29, 18, -1, -1, -1, 32, 19, -1,
154         -1, -1, 21, -1, -1, 22, -1, -1, -1, 20,
155         -1, 30, 24, -1, -1, -1, -1, 25, -1, -1,
156         -1, -1, -1, -1, -1, -1, -1, 31, 26, -1,
157         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
158         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
159         -1, 27, 28, -1, 33};
160
161 static const u8 edid_header[8] = {0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0};
162
163 static int hdmi_runtime_get(void)
164 {
165         int r;
166
167         DSSDBG("hdmi_runtime_get\n");
168
169         r = pm_runtime_get_sync(&hdmi.pdev->dev);
170         WARN_ON(r < 0);
171         return r < 0 ? r : 0;
172 }
173
174 static void hdmi_runtime_put(void)
175 {
176         int r;
177
178         DSSDBG("hdmi_runtime_put\n");
179
180         r = pm_runtime_put(&hdmi.pdev->dev);
181         WARN_ON(r < 0);
182 }
183
184 int hdmi_init_display(struct omap_dss_device *dssdev)
185 {
186         DSSDBG("init_display\n");
187
188         return 0;
189 }
190
191 static void copy_hdmi_to_dss_timings(
192                 const struct hdmi_video_timings *hdmi_timings,
193                 struct omap_video_timings *timings)
194 {
195         timings->x_res = hdmi_timings->x_res;
196         timings->y_res = hdmi_timings->y_res;
197         timings->pixel_clock = hdmi_timings->pixel_clock;
198         timings->hbp = hdmi_timings->hbp;
199         timings->hfp = hdmi_timings->hfp;
200         timings->hsw = hdmi_timings->hsw;
201         timings->vbp = hdmi_timings->vbp;
202         timings->vfp = hdmi_timings->vfp;
203         timings->vsw = hdmi_timings->vsw;
204 }
205
206 static int get_timings_index(void)
207 {
208         int code;
209
210         if (hdmi.mode == 0)
211                 code = code_vesa[hdmi.code];
212         else
213                 code = code_cea[hdmi.code];
214
215         if (code == -1) {
216                 /* HDMI code 4 corresponds to 640 * 480 VGA */
217                 hdmi.code = 4;
218                 /* DVI mode 1 corresponds to HDMI 0 to DVI */
219                 hdmi.mode = HDMI_DVI;
220
221                 code = code_vesa[hdmi.code];
222         }
223         return code;
224 }
225
226 static struct hdmi_cm hdmi_get_code(struct omap_video_timings *timing)
227 {
228         int i = 0, code = -1, temp_vsync = 0, temp_hsync = 0;
229         int timing_vsync = 0, timing_hsync = 0;
230         struct hdmi_video_timings temp;
231         struct hdmi_cm cm = {-1};
232         DSSDBG("hdmi_get_code\n");
233
234         for (i = 0; i < OMAP_HDMI_TIMINGS_NB; i++) {
235                 temp = cea_vesa_timings[i].timings;
236                 if ((temp.pixel_clock == timing->pixel_clock) &&
237                         (temp.x_res == timing->x_res) &&
238                         (temp.y_res == timing->y_res)) {
239
240                         temp_hsync = temp.hfp + temp.hsw + temp.hbp;
241                         timing_hsync = timing->hfp + timing->hsw + timing->hbp;
242                         temp_vsync = temp.vfp + temp.vsw + temp.vbp;
243                         timing_vsync = timing->vfp + timing->vsw + timing->vbp;
244
245                         DSSDBG("temp_hsync = %d , temp_vsync = %d"
246                                 "timing_hsync = %d, timing_vsync = %d\n",
247                                 temp_hsync, temp_hsync,
248                                 timing_hsync, timing_vsync);
249
250                         if ((temp_hsync == timing_hsync) &&
251                                         (temp_vsync == timing_vsync)) {
252                                 code = i;
253                                 cm.code = code_index[i];
254                                 if (code < 14)
255                                         cm.mode = HDMI_HDMI;
256                                 else
257                                         cm.mode = HDMI_DVI;
258                                 DSSDBG("Hdmi_code = %d mode = %d\n",
259                                          cm.code, cm.mode);
260                                 break;
261                          }
262                 }
263         }
264
265         return cm;
266 }
267
268 static void get_horz_vert_timing_info(int current_descriptor_addrs, u8 *edid ,
269                 struct omap_video_timings *timings)
270 {
271         /* X and Y resolution */
272         timings->x_res = (((edid[current_descriptor_addrs + 4] & 0xF0) << 4) |
273                          edid[current_descriptor_addrs + 2]);
274         timings->y_res = (((edid[current_descriptor_addrs + 7] & 0xF0) << 4) |
275                          edid[current_descriptor_addrs + 5]);
276
277         timings->pixel_clock = ((edid[current_descriptor_addrs + 1] << 8) |
278                                 edid[current_descriptor_addrs]);
279
280         timings->pixel_clock = 10 * timings->pixel_clock;
281
282         /* HORIZONTAL FRONT PORCH */
283         timings->hfp = edid[current_descriptor_addrs + 8] |
284                         ((edid[current_descriptor_addrs + 11] & 0xc0) << 2);
285         /* HORIZONTAL SYNC WIDTH */
286         timings->hsw = edid[current_descriptor_addrs + 9] |
287                         ((edid[current_descriptor_addrs + 11] & 0x30) << 4);
288         /* HORIZONTAL BACK PORCH */
289         timings->hbp = (((edid[current_descriptor_addrs + 4] & 0x0F) << 8) |
290                         edid[current_descriptor_addrs + 3]) -
291                         (timings->hfp + timings->hsw);
292         /* VERTICAL FRONT PORCH */
293         timings->vfp = ((edid[current_descriptor_addrs + 10] & 0xF0) >> 4) |
294                         ((edid[current_descriptor_addrs + 11] & 0x0f) << 2);
295         /* VERTICAL SYNC WIDTH */
296         timings->vsw = (edid[current_descriptor_addrs + 10] & 0x0F) |
297                         ((edid[current_descriptor_addrs + 11] & 0x03) << 4);
298         /* VERTICAL BACK PORCH */
299         timings->vbp = (((edid[current_descriptor_addrs + 7] & 0x0F) << 8) |
300                         edid[current_descriptor_addrs + 6]) -
301                         (timings->vfp + timings->vsw);
302
303 }
304
305 /* Description : This function gets the resolution information from EDID */
306 static void get_edid_timing_data(u8 *edid)
307 {
308         u8 count;
309         u16 current_descriptor_addrs;
310         struct hdmi_cm cm;
311         struct omap_video_timings edid_timings;
312
313         /* search block 0, there are 4 DTDs arranged in priority order */
314         for (count = 0; count < EDID_SIZE_BLOCK0_TIMING_DESCRIPTOR; count++) {
315                 current_descriptor_addrs =
316                         EDID_DESCRIPTOR_BLOCK0_ADDRESS +
317                         count * EDID_TIMING_DESCRIPTOR_SIZE;
318                 get_horz_vert_timing_info(current_descriptor_addrs,
319                                 edid, &edid_timings);
320                 cm = hdmi_get_code(&edid_timings);
321                 DSSDBG("Block0[%d] value matches code = %d , mode = %d\n",
322                         count, cm.code, cm.mode);
323                 if (cm.code == -1) {
324                         continue;
325                 } else {
326                         hdmi.code = cm.code;
327                         hdmi.mode = cm.mode;
328                         DSSDBG("code = %d , mode = %d\n",
329                                 hdmi.code, hdmi.mode);
330                         return;
331                 }
332         }
333         if (edid[0x7e] != 0x00) {
334                 for (count = 0; count < EDID_SIZE_BLOCK1_TIMING_DESCRIPTOR;
335                         count++) {
336                         current_descriptor_addrs =
337                         EDID_DESCRIPTOR_BLOCK1_ADDRESS +
338                         count * EDID_TIMING_DESCRIPTOR_SIZE;
339                         get_horz_vert_timing_info(current_descriptor_addrs,
340                                                 edid, &edid_timings);
341                         cm = hdmi_get_code(&edid_timings);
342                         DSSDBG("Block1[%d] value matches code = %d, mode = %d",
343                                 count, cm.code, cm.mode);
344                         if (cm.code == -1) {
345                                 continue;
346                         } else {
347                                 hdmi.code = cm.code;
348                                 hdmi.mode = cm.mode;
349                                 DSSDBG("code = %d , mode = %d\n",
350                                         hdmi.code, hdmi.mode);
351                                 return;
352                         }
353                 }
354         }
355
356         DSSINFO("no valid timing found , falling back to VGA\n");
357         hdmi.code = 4; /* setting default value of 640 480 VGA */
358         hdmi.mode = HDMI_DVI;
359 }
360
361 static void hdmi_read_edid(struct omap_video_timings *dp)
362 {
363         int ret = 0, code;
364
365         memset(hdmi.edid, 0, HDMI_EDID_MAX_LENGTH);
366
367         if (!hdmi.edid_set)
368                 ret = read_edid(&hdmi.ip_data, hdmi.edid,
369                                                 HDMI_EDID_MAX_LENGTH);
370         if (!ret) {
371                 if (!memcmp(hdmi.edid, edid_header, sizeof(edid_header))) {
372                         /* search for timings of default resolution */
373                         get_edid_timing_data(hdmi.edid);
374                         hdmi.edid_set = true;
375                 }
376         } else {
377                 DSSWARN("failed to read E-EDID\n");
378         }
379
380         if (!hdmi.edid_set) {
381                 DSSINFO("fallback to VGA\n");
382                 hdmi.code = 4; /* setting default value of 640 480 VGA */
383                 hdmi.mode = HDMI_DVI;
384         }
385
386         code = get_timings_index();
387
388         copy_hdmi_to_dss_timings(&cea_vesa_timings[code].timings, dp);
389
390 }
391
392 static void update_hdmi_timings(struct hdmi_config *cfg,
393                 struct omap_video_timings *timings, int code)
394 {
395         cfg->timings.timings.x_res = timings->x_res;
396         cfg->timings.timings.y_res = timings->y_res;
397         cfg->timings.timings.hbp = timings->hbp;
398         cfg->timings.timings.hfp = timings->hfp;
399         cfg->timings.timings.hsw = timings->hsw;
400         cfg->timings.timings.vbp = timings->vbp;
401         cfg->timings.timings.vfp = timings->vfp;
402         cfg->timings.timings.vsw = timings->vsw;
403         cfg->timings.timings.pixel_clock = timings->pixel_clock;
404         cfg->timings.vsync_pol = cea_vesa_timings[code].vsync_pol;
405         cfg->timings.hsync_pol = cea_vesa_timings[code].hsync_pol;
406 }
407
408 static void hdmi_compute_pll(struct omap_dss_device *dssdev, int phy,
409                 struct hdmi_pll_info *pi)
410 {
411         unsigned long clkin, refclk;
412         u32 mf;
413
414         clkin = clk_get_rate(hdmi.sys_clk) / 10000;
415         /*
416          * Input clock is predivided by N + 1
417          * out put of which is reference clk
418          */
419         pi->regn = dssdev->clocks.hdmi.regn;
420         refclk = clkin / (pi->regn + 1);
421
422         /*
423          * multiplier is pixel_clk/ref_clk
424          * Multiplying by 100 to avoid fractional part removal
425          */
426         pi->regm = (phy * 100 / (refclk)) / 100;
427         pi->regm2 = dssdev->clocks.hdmi.regm2;
428
429         /*
430          * fractional multiplier is remainder of the difference between
431          * multiplier and actual phy(required pixel clock thus should be
432          * multiplied by 2^18(262144) divided by the reference clock
433          */
434         mf = (phy - pi->regm * refclk) * 262144;
435         pi->regmf = mf / (refclk);
436
437         /*
438          * Dcofreq should be set to 1 if required pixel clock
439          * is greater than 1000MHz
440          */
441         pi->dcofreq = phy > 1000 * 100;
442         pi->regsd = ((pi->regm * clkin / 10) / ((pi->regn + 1) * 250) + 5) / 10;
443
444         /* Set the reference clock to sysclk reference */
445         pi->refsel = HDMI_REFSEL_SYSCLK;
446
447         DSSDBG("M = %d Mf = %d\n", pi->regm, pi->regmf);
448         DSSDBG("range = %d sd = %d\n", pi->dcofreq, pi->regsd);
449 }
450
451 static int hdmi_power_on(struct omap_dss_device *dssdev)
452 {
453         int r, code = 0;
454         struct omap_video_timings *p;
455         unsigned long phy;
456
457         r = hdmi_runtime_get();
458         if (r)
459                 return r;
460
461         dispc_mgr_enable(OMAP_DSS_CHANNEL_DIGIT, 0);
462
463         p = &dssdev->panel.timings;
464
465         DSSDBG("hdmi_power_on x_res= %d y_res = %d\n",
466                 dssdev->panel.timings.x_res,
467                 dssdev->panel.timings.y_res);
468
469         if (!hdmi.custom_set) {
470                 DSSDBG("Read EDID as no EDID is not set on poweron\n");
471                 hdmi_read_edid(p);
472         }
473         code = get_timings_index();
474         copy_hdmi_to_dss_timings(&cea_vesa_timings[code].timings,
475                         &dssdev->panel.timings);
476         update_hdmi_timings(&hdmi.ip_data.cfg, p, code);
477
478         phy = p->pixel_clock;
479
480         hdmi_compute_pll(dssdev, phy, &hdmi.ip_data.pll_data);
481
482         hdmi_wp_video_start(&hdmi.ip_data, 0);
483
484         /* config the PLL and PHY hdmi_set_pll_pwrfirst */
485         r = hdmi_pll_program(&hdmi.ip_data);
486         if (r) {
487                 DSSDBG("Failed to lock PLL\n");
488                 goto err;
489         }
490
491         r = hdmi_phy_init(&hdmi.ip_data);
492         if (r) {
493                 DSSDBG("Failed to start PHY\n");
494                 goto err;
495         }
496
497         hdmi.ip_data.cfg.cm.mode = hdmi.mode;
498         hdmi.ip_data.cfg.cm.code = hdmi.code;
499         hdmi_basic_configure(&hdmi.ip_data);
500
501         /* Make selection of HDMI in DSS */
502         dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
503
504         /* Select the dispc clock source as PRCM clock, to ensure that it is not
505          * DSI PLL source as the clock selected by DSI PLL might not be
506          * sufficient for the resolution selected / that can be changed
507          * dynamically by user. This can be moved to single location , say
508          * Boardfile.
509          */
510         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
511
512         /* bypass TV gamma table */
513         dispc_enable_gamma_table(0);
514
515         /* tv size */
516         dispc_set_digit_size(dssdev->panel.timings.x_res,
517                         dssdev->panel.timings.y_res);
518
519         dispc_mgr_enable(OMAP_DSS_CHANNEL_DIGIT, 1);
520
521         hdmi_wp_video_start(&hdmi.ip_data, 1);
522
523         return 0;
524 err:
525         hdmi_runtime_put();
526         return -EIO;
527 }
528
529 static void hdmi_power_off(struct omap_dss_device *dssdev)
530 {
531         dispc_mgr_enable(OMAP_DSS_CHANNEL_DIGIT, 0);
532
533         hdmi_wp_video_start(&hdmi.ip_data, 0);
534         hdmi_phy_off(&hdmi.ip_data);
535         hdmi_set_pll_pwr(&hdmi.ip_data, HDMI_PLLPWRCMD_ALLOFF);
536         hdmi_runtime_put();
537
538         hdmi.edid_set = 0;
539 }
540
541 int omapdss_hdmi_display_check_timing(struct omap_dss_device *dssdev,
542                                         struct omap_video_timings *timings)
543 {
544         struct hdmi_cm cm;
545
546         cm = hdmi_get_code(timings);
547         if (cm.code == -1) {
548                 DSSERR("Invalid timing entered\n");
549                 return -EINVAL;
550         }
551
552         return 0;
553
554 }
555
556 void omapdss_hdmi_display_set_timing(struct omap_dss_device *dssdev)
557 {
558         struct hdmi_cm cm;
559
560         hdmi.custom_set = 1;
561         cm = hdmi_get_code(&dssdev->panel.timings);
562         hdmi.code = cm.code;
563         hdmi.mode = cm.mode;
564         omapdss_hdmi_display_enable(dssdev);
565         hdmi.custom_set = 0;
566 }
567
568 int omapdss_hdmi_display_enable(struct omap_dss_device *dssdev)
569 {
570         int r = 0;
571
572         DSSDBG("ENTER hdmi_display_enable\n");
573
574         mutex_lock(&hdmi.lock);
575
576         if (dssdev->manager == NULL) {
577                 DSSERR("failed to enable display: no manager\n");
578                 r = -ENODEV;
579                 goto err0;
580         }
581
582         r = omap_dss_start_device(dssdev);
583         if (r) {
584                 DSSERR("failed to start device\n");
585                 goto err0;
586         }
587
588         if (dssdev->platform_enable) {
589                 r = dssdev->platform_enable(dssdev);
590                 if (r) {
591                         DSSERR("failed to enable GPIO's\n");
592                         goto err1;
593                 }
594         }
595
596         r = hdmi_power_on(dssdev);
597         if (r) {
598                 DSSERR("failed to power on device\n");
599                 goto err2;
600         }
601
602         mutex_unlock(&hdmi.lock);
603         return 0;
604
605 err2:
606         if (dssdev->platform_disable)
607                 dssdev->platform_disable(dssdev);
608 err1:
609         omap_dss_stop_device(dssdev);
610 err0:
611         mutex_unlock(&hdmi.lock);
612         return r;
613 }
614
615 void omapdss_hdmi_display_disable(struct omap_dss_device *dssdev)
616 {
617         DSSDBG("Enter hdmi_display_disable\n");
618
619         mutex_lock(&hdmi.lock);
620
621         hdmi_power_off(dssdev);
622
623         if (dssdev->platform_disable)
624                 dssdev->platform_disable(dssdev);
625
626         omap_dss_stop_device(dssdev);
627
628         mutex_unlock(&hdmi.lock);
629 }
630
631 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
632         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
633 static void hdmi_wp_audio_config_format(struct hdmi_ip_data *ip_data,
634                                         struct hdmi_audio_format *aud_fmt)
635 {
636         u32 r;
637
638         DSSDBG("Enter hdmi_wp_audio_config_format\n");
639
640         r = hdmi_read_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CFG);
641         r = FLD_MOD(r, aud_fmt->stereo_channels, 26, 24);
642         r = FLD_MOD(r, aud_fmt->active_chnnls_msk, 23, 16);
643         r = FLD_MOD(r, aud_fmt->en_sig_blk_strt_end, 5, 5);
644         r = FLD_MOD(r, aud_fmt->type, 4, 4);
645         r = FLD_MOD(r, aud_fmt->justification, 3, 3);
646         r = FLD_MOD(r, aud_fmt->sample_order, 2, 2);
647         r = FLD_MOD(r, aud_fmt->samples_per_word, 1, 1);
648         r = FLD_MOD(r, aud_fmt->sample_size, 0, 0);
649         hdmi_write_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CFG, r);
650 }
651
652 static void hdmi_wp_audio_config_dma(struct hdmi_ip_data *ip_data,
653                                         struct hdmi_audio_dma *aud_dma)
654 {
655         u32 r;
656
657         DSSDBG("Enter hdmi_wp_audio_config_dma\n");
658
659         r = hdmi_read_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CFG2);
660         r = FLD_MOD(r, aud_dma->transfer_size, 15, 8);
661         r = FLD_MOD(r, aud_dma->block_size, 7, 0);
662         hdmi_write_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CFG2, r);
663
664         r = hdmi_read_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CTRL);
665         r = FLD_MOD(r, aud_dma->mode, 9, 9);
666         r = FLD_MOD(r, aud_dma->fifo_threshold, 8, 0);
667         hdmi_write_reg(hdmi_wp_base(ip_data), HDMI_WP_AUDIO_CTRL, r);
668 }
669
670 static void hdmi_core_audio_config(struct hdmi_ip_data *ip_data,
671                                         struct hdmi_core_audio_config *cfg)
672 {
673         u32 r;
674         void __iomem *av_base = hdmi_av_base(ip_data);
675
676         /* audio clock recovery parameters */
677         r = hdmi_read_reg(av_base, HDMI_CORE_AV_ACR_CTRL);
678         r = FLD_MOD(r, cfg->use_mclk, 2, 2);
679         r = FLD_MOD(r, cfg->en_acr_pkt, 1, 1);
680         r = FLD_MOD(r, cfg->cts_mode, 0, 0);
681         hdmi_write_reg(av_base, HDMI_CORE_AV_ACR_CTRL, r);
682
683         REG_FLD_MOD(av_base, HDMI_CORE_AV_N_SVAL1, cfg->n, 7, 0);
684         REG_FLD_MOD(av_base, HDMI_CORE_AV_N_SVAL2, cfg->n >> 8, 7, 0);
685         REG_FLD_MOD(av_base, HDMI_CORE_AV_N_SVAL3, cfg->n >> 16, 7, 0);
686
687         if (cfg->cts_mode == HDMI_AUDIO_CTS_MODE_SW) {
688                 REG_FLD_MOD(av_base, HDMI_CORE_AV_CTS_SVAL1, cfg->cts, 7, 0);
689                 REG_FLD_MOD(av_base,
690                                 HDMI_CORE_AV_CTS_SVAL2, cfg->cts >> 8, 7, 0);
691                 REG_FLD_MOD(av_base,
692                                 HDMI_CORE_AV_CTS_SVAL3, cfg->cts >> 16, 7, 0);
693         } else {
694                 /*
695                  * HDMI IP uses this configuration to divide the MCLK to
696                  * update CTS value.
697                  */
698                 REG_FLD_MOD(av_base,
699                                 HDMI_CORE_AV_FREQ_SVAL, cfg->mclk_mode, 2, 0);
700
701                 /* Configure clock for audio packets */
702                 REG_FLD_MOD(av_base, HDMI_CORE_AV_AUD_PAR_BUSCLK_1,
703                                 cfg->aud_par_busclk, 7, 0);
704                 REG_FLD_MOD(av_base, HDMI_CORE_AV_AUD_PAR_BUSCLK_2,
705                                 (cfg->aud_par_busclk >> 8), 7, 0);
706                 REG_FLD_MOD(av_base, HDMI_CORE_AV_AUD_PAR_BUSCLK_3,
707                                 (cfg->aud_par_busclk >> 16), 7, 0);
708         }
709
710         /* Override of SPDIF sample frequency with value in I2S_CHST4 */
711         REG_FLD_MOD(av_base, HDMI_CORE_AV_SPDIF_CTRL,
712                                                 cfg->fs_override, 1, 1);
713
714         /* I2S parameters */
715         REG_FLD_MOD(av_base, HDMI_CORE_AV_I2S_CHST4,
716                                                 cfg->freq_sample, 3, 0);
717
718         r = hdmi_read_reg(av_base, HDMI_CORE_AV_I2S_IN_CTRL);
719         r = FLD_MOD(r, cfg->i2s_cfg.en_high_bitrate_aud, 7, 7);
720         r = FLD_MOD(r, cfg->i2s_cfg.sck_edge_mode, 6, 6);
721         r = FLD_MOD(r, cfg->i2s_cfg.cbit_order, 5, 5);
722         r = FLD_MOD(r, cfg->i2s_cfg.vbit, 4, 4);
723         r = FLD_MOD(r, cfg->i2s_cfg.ws_polarity, 3, 3);
724         r = FLD_MOD(r, cfg->i2s_cfg.justification, 2, 2);
725         r = FLD_MOD(r, cfg->i2s_cfg.direction, 1, 1);
726         r = FLD_MOD(r, cfg->i2s_cfg.shift, 0, 0);
727         hdmi_write_reg(av_base, HDMI_CORE_AV_I2S_IN_CTRL, r);
728
729         r = hdmi_read_reg(av_base, HDMI_CORE_AV_I2S_CHST5);
730         r = FLD_MOD(r, cfg->freq_sample, 7, 4);
731         r = FLD_MOD(r, cfg->i2s_cfg.word_length, 3, 1);
732         r = FLD_MOD(r, cfg->i2s_cfg.word_max_length, 0, 0);
733         hdmi_write_reg(av_base, HDMI_CORE_AV_I2S_CHST5, r);
734
735         REG_FLD_MOD(av_base, HDMI_CORE_AV_I2S_IN_LEN,
736                         cfg->i2s_cfg.in_length_bits, 3, 0);
737
738         /* Audio channels and mode parameters */
739         REG_FLD_MOD(av_base, HDMI_CORE_AV_HDMI_CTRL, cfg->layout, 2, 1);
740         r = hdmi_read_reg(av_base, HDMI_CORE_AV_AUD_MODE);
741         r = FLD_MOD(r, cfg->i2s_cfg.active_sds, 7, 4);
742         r = FLD_MOD(r, cfg->en_dsd_audio, 3, 3);
743         r = FLD_MOD(r, cfg->en_parallel_aud_input, 2, 2);
744         r = FLD_MOD(r, cfg->en_spdif, 1, 1);
745         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_MODE, r);
746 }
747
748 static void hdmi_core_audio_infoframe_config(struct hdmi_ip_data *ip_data,
749                 struct hdmi_core_infoframe_audio *info_aud)
750 {
751         u8 val;
752         u8 sum = 0, checksum = 0;
753         void __iomem *av_base = hdmi_av_base(ip_data);
754
755         /*
756          * Set audio info frame type, version and length as
757          * described in HDMI 1.4a Section 8.2.2 specification.
758          * Checksum calculation is defined in Section 5.3.5.
759          */
760         hdmi_write_reg(av_base, HDMI_CORE_AV_AUDIO_TYPE, 0x84);
761         hdmi_write_reg(av_base, HDMI_CORE_AV_AUDIO_VERS, 0x01);
762         hdmi_write_reg(av_base, HDMI_CORE_AV_AUDIO_LEN, 0x0a);
763         sum += 0x84 + 0x001 + 0x00a;
764
765         val = (info_aud->db1_coding_type << 4)
766                         | (info_aud->db1_channel_count - 1);
767         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(0), val);
768         sum += val;
769
770         val = (info_aud->db2_sample_freq << 2) | info_aud->db2_sample_size;
771         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(1), val);
772         sum += val;
773
774         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(2), 0x00);
775
776         val = info_aud->db4_channel_alloc;
777         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(3), val);
778         sum += val;
779
780         val = (info_aud->db5_downmix_inh << 7) | (info_aud->db5_lsv << 3);
781         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(4), val);
782         sum += val;
783
784         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(5), 0x00);
785         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(6), 0x00);
786         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(7), 0x00);
787         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(8), 0x00);
788         hdmi_write_reg(av_base, HDMI_CORE_AV_AUD_DBYTE(9), 0x00);
789
790         checksum = 0x100 - sum;
791         hdmi_write_reg(av_base,
792                                         HDMI_CORE_AV_AUDIO_CHSUM, checksum);
793
794         /*
795          * TODO: Add MPEG and SPD enable and repeat cfg when EDID parsing
796          * is available.
797          */
798 }
799
800 static int hdmi_config_audio_acr(struct hdmi_ip_data *ip_data,
801                                 u32 sample_freq, u32 *n, u32 *cts)
802 {
803         u32 r;
804         u32 deep_color = 0;
805         u32 pclk = hdmi.cfg.timings.timings.pixel_clock;
806
807         if (n == NULL || cts == NULL)
808                 return -EINVAL;
809         /*
810          * Obtain current deep color configuration. This needed
811          * to calculate the TMDS clock based on the pixel clock.
812          */
813         r = REG_GET(hdmi_wp_base(ip_data), HDMI_WP_VIDEO_CFG, 1, 0);
814         switch (r) {
815         case 1: /* No deep color selected */
816                 deep_color = 100;
817                 break;
818         case 2: /* 10-bit deep color selected */
819                 deep_color = 125;
820                 break;
821         case 3: /* 12-bit deep color selected */
822                 deep_color = 150;
823                 break;
824         default:
825                 return -EINVAL;
826         }
827
828         switch (sample_freq) {
829         case 32000:
830                 if ((deep_color == 125) && ((pclk == 54054)
831                                 || (pclk == 74250)))
832                         *n = 8192;
833                 else
834                         *n = 4096;
835                 break;
836         case 44100:
837                 *n = 6272;
838                 break;
839         case 48000:
840                 if ((deep_color == 125) && ((pclk == 54054)
841                                 || (pclk == 74250)))
842                         *n = 8192;
843                 else
844                         *n = 6144;
845                 break;
846         default:
847                 *n = 0;
848                 return -EINVAL;
849         }
850
851         /* Calculate CTS. See HDMI 1.3a or 1.4a specifications */
852         *cts = pclk * (*n / 128) * deep_color / (sample_freq / 10);
853
854         return 0;
855 }
856
857 static int hdmi_audio_hw_params(struct hdmi_ip_data *ip_data,
858                                         struct snd_pcm_substream *substream,
859                                     struct snd_pcm_hw_params *params,
860                                     struct snd_soc_dai *dai)
861 {
862         struct hdmi_audio_format audio_format;
863         struct hdmi_audio_dma audio_dma;
864         struct hdmi_core_audio_config core_cfg;
865         struct hdmi_core_infoframe_audio aud_if_cfg;
866         int err, n, cts;
867         enum hdmi_core_audio_sample_freq sample_freq;
868
869         switch (params_format(params)) {
870         case SNDRV_PCM_FORMAT_S16_LE:
871                 core_cfg.i2s_cfg.word_max_length =
872                         HDMI_AUDIO_I2S_MAX_WORD_20BITS;
873                 core_cfg.i2s_cfg.word_length = HDMI_AUDIO_I2S_CHST_WORD_16_BITS;
874                 core_cfg.i2s_cfg.in_length_bits =
875                         HDMI_AUDIO_I2S_INPUT_LENGTH_16;
876                 core_cfg.i2s_cfg.justification = HDMI_AUDIO_JUSTIFY_LEFT;
877                 audio_format.samples_per_word = HDMI_AUDIO_ONEWORD_TWOSAMPLES;
878                 audio_format.sample_size = HDMI_AUDIO_SAMPLE_16BITS;
879                 audio_format.justification = HDMI_AUDIO_JUSTIFY_LEFT;
880                 audio_dma.transfer_size = 0x10;
881                 break;
882         case SNDRV_PCM_FORMAT_S24_LE:
883                 core_cfg.i2s_cfg.word_max_length =
884                         HDMI_AUDIO_I2S_MAX_WORD_24BITS;
885                 core_cfg.i2s_cfg.word_length = HDMI_AUDIO_I2S_CHST_WORD_24_BITS;
886                 core_cfg.i2s_cfg.in_length_bits =
887                         HDMI_AUDIO_I2S_INPUT_LENGTH_24;
888                 audio_format.samples_per_word = HDMI_AUDIO_ONEWORD_ONESAMPLE;
889                 audio_format.sample_size = HDMI_AUDIO_SAMPLE_24BITS;
890                 audio_format.justification = HDMI_AUDIO_JUSTIFY_RIGHT;
891                 core_cfg.i2s_cfg.justification = HDMI_AUDIO_JUSTIFY_RIGHT;
892                 audio_dma.transfer_size = 0x20;
893                 break;
894         default:
895                 return -EINVAL;
896         }
897
898         switch (params_rate(params)) {
899         case 32000:
900                 sample_freq = HDMI_AUDIO_FS_32000;
901                 break;
902         case 44100:
903                 sample_freq = HDMI_AUDIO_FS_44100;
904                 break;
905         case 48000:
906                 sample_freq = HDMI_AUDIO_FS_48000;
907                 break;
908         default:
909                 return -EINVAL;
910         }
911
912         err = hdmi_config_audio_acr(ip_data, params_rate(params), &n, &cts);
913         if (err < 0)
914                 return err;
915
916         /* Audio wrapper config */
917         audio_format.stereo_channels = HDMI_AUDIO_STEREO_ONECHANNEL;
918         audio_format.active_chnnls_msk = 0x03;
919         audio_format.type = HDMI_AUDIO_TYPE_LPCM;
920         audio_format.sample_order = HDMI_AUDIO_SAMPLE_LEFT_FIRST;
921         /* Disable start/stop signals of IEC 60958 blocks */
922         audio_format.en_sig_blk_strt_end = HDMI_AUDIO_BLOCK_SIG_STARTEND_OFF;
923
924         audio_dma.block_size = 0xC0;
925         audio_dma.mode = HDMI_AUDIO_TRANSF_DMA;
926         audio_dma.fifo_threshold = 0x20; /* in number of samples */
927
928         hdmi_wp_audio_config_dma(ip_data, &audio_dma);
929         hdmi_wp_audio_config_format(ip_data, &audio_format);
930
931         /*
932          * I2S config
933          */
934         core_cfg.i2s_cfg.en_high_bitrate_aud = false;
935         /* Only used with high bitrate audio */
936         core_cfg.i2s_cfg.cbit_order = false;
937         /* Serial data and word select should change on sck rising edge */
938         core_cfg.i2s_cfg.sck_edge_mode = HDMI_AUDIO_I2S_SCK_EDGE_RISING;
939         core_cfg.i2s_cfg.vbit = HDMI_AUDIO_I2S_VBIT_FOR_PCM;
940         /* Set I2S word select polarity */
941         core_cfg.i2s_cfg.ws_polarity = HDMI_AUDIO_I2S_WS_POLARITY_LOW_IS_LEFT;
942         core_cfg.i2s_cfg.direction = HDMI_AUDIO_I2S_MSB_SHIFTED_FIRST;
943         /* Set serial data to word select shift. See Phillips spec. */
944         core_cfg.i2s_cfg.shift = HDMI_AUDIO_I2S_FIRST_BIT_SHIFT;
945         /* Enable one of the four available serial data channels */
946         core_cfg.i2s_cfg.active_sds = HDMI_AUDIO_I2S_SD0_EN;
947
948         /* Core audio config */
949         core_cfg.freq_sample = sample_freq;
950         core_cfg.n = n;
951         core_cfg.cts = cts;
952         if (dss_has_feature(FEAT_HDMI_CTS_SWMODE)) {
953                 core_cfg.aud_par_busclk = 0;
954                 core_cfg.cts_mode = HDMI_AUDIO_CTS_MODE_SW;
955                 core_cfg.use_mclk = false;
956         } else {
957                 core_cfg.aud_par_busclk = (((128 * 31) - 1) << 8);
958                 core_cfg.cts_mode = HDMI_AUDIO_CTS_MODE_HW;
959                 core_cfg.use_mclk = true;
960                 core_cfg.mclk_mode = HDMI_AUDIO_MCLK_128FS;
961         }
962         core_cfg.layout = HDMI_AUDIO_LAYOUT_2CH;
963         core_cfg.en_spdif = false;
964         /* Use sample frequency from channel status word */
965         core_cfg.fs_override = true;
966         /* Enable ACR packets */
967         core_cfg.en_acr_pkt = true;
968         /* Disable direct streaming digital audio */
969         core_cfg.en_dsd_audio = false;
970         /* Use parallel audio interface */
971         core_cfg.en_parallel_aud_input = true;
972
973         hdmi_core_audio_config(ip_data, &core_cfg);
974
975         /*
976          * Configure packet
977          * info frame audio see doc CEA861-D page 74
978          */
979         aud_if_cfg.db1_coding_type = HDMI_INFOFRAME_AUDIO_DB1CT_FROM_STREAM;
980         aud_if_cfg.db1_channel_count = 2;
981         aud_if_cfg.db2_sample_freq = HDMI_INFOFRAME_AUDIO_DB2SF_FROM_STREAM;
982         aud_if_cfg.db2_sample_size = HDMI_INFOFRAME_AUDIO_DB2SS_FROM_STREAM;
983         aud_if_cfg.db4_channel_alloc = 0x00;
984         aud_if_cfg.db5_downmix_inh = false;
985         aud_if_cfg.db5_lsv = 0;
986
987         hdmi_core_audio_infoframe_config(ip_data, &aud_if_cfg);
988         return 0;
989 }
990
991 static int hdmi_audio_trigger(struct hdmi_ip_data *ip_data,
992                                 struct snd_pcm_substream *substream, int cmd,
993                                 struct snd_soc_dai *dai)
994 {
995         int err = 0;
996         switch (cmd) {
997         case SNDRV_PCM_TRIGGER_START:
998         case SNDRV_PCM_TRIGGER_RESUME:
999         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1000                 REG_FLD_MOD(hdmi_av_base(ip_data),
1001                                         HDMI_CORE_AV_AUD_MODE, 1, 0, 0);
1002                 REG_FLD_MOD(hdmi_wp_base(ip_data),
1003                                         HDMI_WP_AUDIO_CTRL, 1, 31, 31);
1004                 REG_FLD_MOD(hdmi_wp_base(ip_data),
1005                                         HDMI_WP_AUDIO_CTRL, 1, 30, 30);
1006                 break;
1007
1008         case SNDRV_PCM_TRIGGER_STOP:
1009         case SNDRV_PCM_TRIGGER_SUSPEND:
1010         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1011                 REG_FLD_MOD(hdmi_av_base(ip_data),
1012                                         HDMI_CORE_AV_AUD_MODE, 0, 0, 0);
1013                 REG_FLD_MOD(hdmi_wp_base(ip_data),
1014                                         HDMI_WP_AUDIO_CTRL, 0, 30, 30);
1015                 REG_FLD_MOD(hdmi_wp_base(ip_data),
1016                                         HDMI_WP_AUDIO_CTRL, 0, 31, 31);
1017                 break;
1018         default:
1019                 err = -EINVAL;
1020         }
1021         return err;
1022 }
1023
1024 static int hdmi_audio_startup(struct snd_pcm_substream *substream,
1025                                   struct snd_soc_dai *dai)
1026 {
1027         if (!hdmi.mode) {
1028                 pr_err("Current video settings do not support audio.\n");
1029                 return -EIO;
1030         }
1031         return 0;
1032 }
1033
1034 static struct snd_soc_codec_driver hdmi_audio_codec_drv = {
1035 };
1036
1037 static struct snd_soc_dai_ops hdmi_audio_codec_ops = {
1038         .hw_params = hdmi_audio_hw_params,
1039         .trigger = hdmi_audio_trigger,
1040         .startup = hdmi_audio_startup,
1041 };
1042
1043 static struct snd_soc_dai_driver hdmi_codec_dai_drv = {
1044                 .name = "hdmi-audio-codec",
1045                 .playback = {
1046                         .channels_min = 2,
1047                         .channels_max = 2,
1048                         .rates = SNDRV_PCM_RATE_32000 |
1049                                 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1050                         .formats = SNDRV_PCM_FMTBIT_S16_LE |
1051                                 SNDRV_PCM_FMTBIT_S24_LE,
1052                 },
1053                 .ops = &hdmi_audio_codec_ops,
1054 };
1055 #endif
1056
1057 static int hdmi_get_clocks(struct platform_device *pdev)
1058 {
1059         struct clk *clk;
1060
1061         clk = clk_get(&pdev->dev, "sys_clk");
1062         if (IS_ERR(clk)) {
1063                 DSSERR("can't get sys_clk\n");
1064                 return PTR_ERR(clk);
1065         }
1066
1067         hdmi.sys_clk = clk;
1068
1069         return 0;
1070 }
1071
1072 static void hdmi_put_clocks(void)
1073 {
1074         if (hdmi.sys_clk)
1075                 clk_put(hdmi.sys_clk);
1076 }
1077
1078 /* HDMI HW IP initialisation */
1079 static int omapdss_hdmihw_probe(struct platform_device *pdev)
1080 {
1081         struct resource *hdmi_mem;
1082         int r;
1083
1084         hdmi.pdata = pdev->dev.platform_data;
1085         hdmi.pdev = pdev;
1086
1087         mutex_init(&hdmi.lock);
1088
1089         hdmi_mem = platform_get_resource(hdmi.pdev, IORESOURCE_MEM, 0);
1090         if (!hdmi_mem) {
1091                 DSSERR("can't get IORESOURCE_MEM HDMI\n");
1092                 return -EINVAL;
1093         }
1094
1095         /* Base address taken from platform */
1096         hdmi.ip_data.base_wp = ioremap(hdmi_mem->start,
1097                                                 resource_size(hdmi_mem));
1098         if (!hdmi.ip_data.base_wp) {
1099                 DSSERR("can't ioremap WP\n");
1100                 return -ENOMEM;
1101         }
1102
1103         r = hdmi_get_clocks(pdev);
1104         if (r) {
1105                 iounmap(hdmi.ip_data.base_wp);
1106                 return r;
1107         }
1108
1109         pm_runtime_enable(&pdev->dev);
1110
1111         hdmi.ip_data.core_sys_offset = HDMI_CORE_SYS;
1112         hdmi.ip_data.core_av_offset = HDMI_CORE_AV;
1113         hdmi.ip_data.pll_offset = HDMI_PLLCTRL;
1114         hdmi.ip_data.phy_offset = HDMI_PHY;
1115
1116         hdmi_panel_init();
1117
1118 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
1119         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
1120
1121         /* Register ASoC codec DAI */
1122         r = snd_soc_register_codec(&pdev->dev, &hdmi_audio_codec_drv,
1123                                         &hdmi_codec_dai_drv, 1);
1124         if (r) {
1125                 DSSERR("can't register ASoC HDMI audio codec\n");
1126                 return r;
1127         }
1128 #endif
1129         return 0;
1130 }
1131
1132 static int omapdss_hdmihw_remove(struct platform_device *pdev)
1133 {
1134         hdmi_panel_exit();
1135
1136 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
1137         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
1138         snd_soc_unregister_codec(&pdev->dev);
1139 #endif
1140
1141         pm_runtime_disable(&pdev->dev);
1142
1143         hdmi_put_clocks();
1144
1145         iounmap(hdmi.ip_data.base_wp);
1146
1147         return 0;
1148 }
1149
1150 static int hdmi_runtime_suspend(struct device *dev)
1151 {
1152         clk_disable(hdmi.sys_clk);
1153
1154         dispc_runtime_put();
1155         dss_runtime_put();
1156
1157         return 0;
1158 }
1159
1160 static int hdmi_runtime_resume(struct device *dev)
1161 {
1162         int r;
1163
1164         r = dss_runtime_get();
1165         if (r < 0)
1166                 goto err_get_dss;
1167
1168         r = dispc_runtime_get();
1169         if (r < 0)
1170                 goto err_get_dispc;
1171
1172
1173         clk_enable(hdmi.sys_clk);
1174
1175         return 0;
1176
1177 err_get_dispc:
1178         dss_runtime_put();
1179 err_get_dss:
1180         return r;
1181 }
1182
1183 static const struct dev_pm_ops hdmi_pm_ops = {
1184         .runtime_suspend = hdmi_runtime_suspend,
1185         .runtime_resume = hdmi_runtime_resume,
1186 };
1187
1188 static struct platform_driver omapdss_hdmihw_driver = {
1189         .probe          = omapdss_hdmihw_probe,
1190         .remove         = omapdss_hdmihw_remove,
1191         .driver         = {
1192                 .name   = "omapdss_hdmi",
1193                 .owner  = THIS_MODULE,
1194                 .pm     = &hdmi_pm_ops,
1195         },
1196 };
1197
1198 int hdmi_init_platform_driver(void)
1199 {
1200         return platform_driver_register(&omapdss_hdmihw_driver);
1201 }
1202
1203 void hdmi_uninit_platform_driver(void)
1204 {
1205         return platform_driver_unregister(&omapdss_hdmihw_driver);
1206 }