]> Pileus Git - ~andy/linux/blob - drivers/video/omap2/dss/hdmi.c
OMAP: DSS2: Use PM runtime & HWMOD support
[~andy/linux] / 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 "dss.h"
43 #include "hdmi.h"
44 #include "dss_features.h"
45
46 static struct {
47         struct mutex lock;
48         struct omap_display_platform_data *pdata;
49         struct platform_device *pdev;
50         void __iomem *base_wp;  /* HDMI wrapper */
51         int code;
52         int mode;
53         u8 edid[HDMI_EDID_MAX_LENGTH];
54         u8 edid_set;
55         bool custom_set;
56         struct hdmi_config cfg;
57
58         struct clk *sys_clk;
59         struct clk *hdmi_clk;
60 } hdmi;
61
62 /*
63  * Logic for the below structure :
64  * user enters the CEA or VESA timings by specifying the HDMI/DVI code.
65  * There is a correspondence between CEA/VESA timing and code, please
66  * refer to section 6.3 in HDMI 1.3 specification for timing code.
67  *
68  * In the below structure, cea_vesa_timings corresponds to all OMAP4
69  * supported CEA and VESA timing values.code_cea corresponds to the CEA
70  * code, It is used to get the timing from cea_vesa_timing array.Similarly
71  * with code_vesa. Code_index is used for back mapping, that is once EDID
72  * is read from the TV, EDID is parsed to find the timing values and then
73  * map it to corresponding CEA or VESA index.
74  */
75
76 static const struct hdmi_timings cea_vesa_timings[OMAP_HDMI_TIMINGS_NB] = {
77         { {640, 480, 25200, 96, 16, 48, 2, 10, 33} , 0 , 0},
78         { {1280, 720, 74250, 40, 440, 220, 5, 5, 20}, 1, 1},
79         { {1280, 720, 74250, 40, 110, 220, 5, 5, 20}, 1, 1},
80         { {720, 480, 27027, 62, 16, 60, 6, 9, 30}, 0, 0},
81         { {2880, 576, 108000, 256, 48, 272, 5, 5, 39}, 0, 0},
82         { {1440, 240, 27027, 124, 38, 114, 3, 4, 15}, 0, 0},
83         { {1440, 288, 27000, 126, 24, 138, 3, 2, 19}, 0, 0},
84         { {1920, 540, 74250, 44, 528, 148, 5, 2, 15}, 1, 1},
85         { {1920, 540, 74250, 44, 88, 148, 5, 2, 15}, 1, 1},
86         { {1920, 1080, 148500, 44, 88, 148, 5, 4, 36}, 1, 1},
87         { {720, 576, 27000, 64, 12, 68, 5, 5, 39}, 0, 0},
88         { {1440, 576, 54000, 128, 24, 136, 5, 5, 39}, 0, 0},
89         { {1920, 1080, 148500, 44, 528, 148, 5, 4, 36}, 1, 1},
90         { {2880, 480, 108108, 248, 64, 240, 6, 9, 30}, 0, 0},
91         { {1920, 1080, 74250, 44, 638, 148, 5, 4, 36}, 1, 1},
92         /* VESA From Here */
93         { {640, 480, 25175, 96, 16, 48, 2 , 11, 31}, 0, 0},
94         { {800, 600, 40000, 128, 40, 88, 4 , 1, 23}, 1, 1},
95         { {848, 480, 33750, 112, 16, 112, 8 , 6, 23}, 1, 1},
96         { {1280, 768, 79500, 128, 64, 192, 7 , 3, 20}, 1, 0},
97         { {1280, 800, 83500, 128, 72, 200, 6 , 3, 22}, 1, 0},
98         { {1360, 768, 85500, 112, 64, 256, 6 , 3, 18}, 1, 1},
99         { {1280, 960, 108000, 112, 96, 312, 3 , 1, 36}, 1, 1},
100         { {1280, 1024, 108000, 112, 48, 248, 3 , 1, 38}, 1, 1},
101         { {1024, 768, 65000, 136, 24, 160, 6, 3, 29}, 0, 0},
102         { {1400, 1050, 121750, 144, 88, 232, 4, 3, 32}, 1, 0},
103         { {1440, 900, 106500, 152, 80, 232, 6, 3, 25}, 1, 0},
104         { {1680, 1050, 146250, 176 , 104, 280, 6, 3, 30}, 1, 0},
105         { {1366, 768, 85500, 143, 70, 213, 3, 3, 24}, 1, 1},
106         { {1920, 1080, 148500, 44, 148, 80, 5, 4, 36}, 1, 1},
107         { {1280, 768, 68250, 32, 48, 80, 7, 3, 12}, 0, 1},
108         { {1400, 1050, 101000, 32, 48, 80, 4, 3, 23}, 0, 1},
109         { {1680, 1050, 119000, 32, 48, 80, 6, 3, 21}, 0, 1},
110         { {1280, 800, 79500, 32, 48, 80, 6, 3, 14}, 0, 1},
111         { {1280, 720, 74250, 40, 110, 220, 5, 5, 20}, 1, 1}
112 };
113
114 /*
115  * This is a static mapping array which maps the timing values
116  * with corresponding CEA / VESA code
117  */
118 static const int code_index[OMAP_HDMI_TIMINGS_NB] = {
119         1, 19, 4, 2, 37, 6, 21, 20, 5, 16, 17, 29, 31, 35, 32,
120         /* <--15 CEA 17--> vesa*/
121         4, 9, 0xE, 0x17, 0x1C, 0x27, 0x20, 0x23, 0x10, 0x2A,
122         0X2F, 0x3A, 0X51, 0X52, 0x16, 0x29, 0x39, 0x1B
123 };
124
125 /*
126  * This is reverse static mapping which maps the CEA / VESA code
127  * to the corresponding timing values
128  */
129 static const int code_cea[39] = {
130         -1,  0,  3,  3,  2,  8,  5,  5, -1, -1,
131         -1, -1, -1, -1, -1, -1,  9, 10, 10,  1,
132         7,   6,  6, -1, -1, -1, -1, -1, -1, 11,
133         11, 12, 14, -1, -1, 13, 13,  4,  4
134 };
135
136 static const int code_vesa[85] = {
137         -1, -1, -1, -1, 15, -1, -1, -1, -1, 16,
138         -1, -1, -1, -1, 17, -1, 23, -1, -1, -1,
139         -1, -1, 29, 18, -1, -1, -1, 32, 19, -1,
140         -1, -1, 21, -1, -1, 22, -1, -1, -1, 20,
141         -1, 30, 24, -1, -1, -1, -1, 25, -1, -1,
142         -1, -1, -1, -1, -1, -1, -1, 31, 26, -1,
143         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
144         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
145         -1, 27, 28, -1, 33};
146
147 static const u8 edid_header[8] = {0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0};
148
149 static inline void hdmi_write_reg(const struct hdmi_reg idx, u32 val)
150 {
151         __raw_writel(val, hdmi.base_wp + idx.idx);
152 }
153
154 static inline u32 hdmi_read_reg(const struct hdmi_reg idx)
155 {
156         return __raw_readl(hdmi.base_wp + idx.idx);
157 }
158
159 static inline int hdmi_wait_for_bit_change(const struct hdmi_reg idx,
160                                 int b2, int b1, u32 val)
161 {
162         u32 t = 0;
163         while (val != REG_GET(idx, b2, b1)) {
164                 udelay(1);
165                 if (t++ > 10000)
166                         return !val;
167         }
168         return val;
169 }
170
171 static int hdmi_runtime_get(void)
172 {
173         int r;
174
175         DSSDBG("hdmi_runtime_get\n");
176
177         r = pm_runtime_get_sync(&hdmi.pdev->dev);
178         WARN_ON(r < 0);
179         return r < 0 ? r : 0;
180 }
181
182 static void hdmi_runtime_put(void)
183 {
184         int r;
185
186         DSSDBG("hdmi_runtime_put\n");
187
188         r = pm_runtime_put(&hdmi.pdev->dev);
189         WARN_ON(r < 0);
190 }
191
192 int hdmi_init_display(struct omap_dss_device *dssdev)
193 {
194         DSSDBG("init_display\n");
195
196         return 0;
197 }
198
199 static int hdmi_pll_init(enum hdmi_clk_refsel refsel, int dcofreq,
200                 struct hdmi_pll_info *fmt, u16 sd)
201 {
202         u32 r;
203
204         /* PLL start always use manual mode */
205         REG_FLD_MOD(PLLCTRL_PLL_CONTROL, 0x0, 0, 0);
206
207         r = hdmi_read_reg(PLLCTRL_CFG1);
208         r = FLD_MOD(r, fmt->regm, 20, 9); /* CFG1_PLL_REGM */
209         r = FLD_MOD(r, fmt->regn, 8, 1);  /* CFG1_PLL_REGN */
210
211         hdmi_write_reg(PLLCTRL_CFG1, r);
212
213         r = hdmi_read_reg(PLLCTRL_CFG2);
214
215         r = FLD_MOD(r, 0x0, 12, 12); /* PLL_HIGHFREQ divide by 2 */
216         r = FLD_MOD(r, 0x1, 13, 13); /* PLL_REFEN */
217         r = FLD_MOD(r, 0x0, 14, 14); /* PHY_CLKINEN de-assert during locking */
218
219         if (dcofreq) {
220                 /* divider programming for frequency beyond 1000Mhz */
221                 REG_FLD_MOD(PLLCTRL_CFG3, sd, 17, 10);
222                 r = FLD_MOD(r, 0x4, 3, 1); /* 1000MHz and 2000MHz */
223         } else {
224                 r = FLD_MOD(r, 0x2, 3, 1); /* 500MHz and 1000MHz */
225         }
226
227         hdmi_write_reg(PLLCTRL_CFG2, r);
228
229         r = hdmi_read_reg(PLLCTRL_CFG4);
230         r = FLD_MOD(r, fmt->regm2, 24, 18);
231         r = FLD_MOD(r, fmt->regmf, 17, 0);
232
233         hdmi_write_reg(PLLCTRL_CFG4, r);
234
235         /* go now */
236         REG_FLD_MOD(PLLCTRL_PLL_GO, 0x1, 0, 0);
237
238         /* wait for bit change */
239         if (hdmi_wait_for_bit_change(PLLCTRL_PLL_GO, 0, 0, 1) != 1) {
240                 DSSERR("PLL GO bit not set\n");
241                 return -ETIMEDOUT;
242         }
243
244         /* Wait till the lock bit is set in PLL status */
245         if (hdmi_wait_for_bit_change(PLLCTRL_PLL_STATUS, 1, 1, 1) != 1) {
246                 DSSWARN("cannot lock PLL\n");
247                 DSSWARN("CFG1 0x%x\n",
248                         hdmi_read_reg(PLLCTRL_CFG1));
249                 DSSWARN("CFG2 0x%x\n",
250                         hdmi_read_reg(PLLCTRL_CFG2));
251                 DSSWARN("CFG4 0x%x\n",
252                         hdmi_read_reg(PLLCTRL_CFG4));
253                 return -ETIMEDOUT;
254         }
255
256         DSSDBG("PLL locked!\n");
257
258         return 0;
259 }
260
261 /* PHY_PWR_CMD */
262 static int hdmi_set_phy_pwr(enum hdmi_phy_pwr val)
263 {
264         /* Command for power control of HDMI PHY */
265         REG_FLD_MOD(HDMI_WP_PWR_CTRL, val, 7, 6);
266
267         /* Status of the power control of HDMI PHY */
268         if (hdmi_wait_for_bit_change(HDMI_WP_PWR_CTRL, 5, 4, val) != val) {
269                 DSSERR("Failed to set PHY power mode to %d\n", val);
270                 return -ETIMEDOUT;
271         }
272
273         return 0;
274 }
275
276 /* PLL_PWR_CMD */
277 static int hdmi_set_pll_pwr(enum hdmi_pll_pwr val)
278 {
279         /* Command for power control of HDMI PLL */
280         REG_FLD_MOD(HDMI_WP_PWR_CTRL, val, 3, 2);
281
282         /* wait till PHY_PWR_STATUS is set */
283         if (hdmi_wait_for_bit_change(HDMI_WP_PWR_CTRL, 1, 0, val) != val) {
284                 DSSERR("Failed to set PHY_PWR_STATUS\n");
285                 return -ETIMEDOUT;
286         }
287
288         return 0;
289 }
290
291 static int hdmi_pll_reset(void)
292 {
293         /* SYSRESET  controlled by power FSM */
294         REG_FLD_MOD(PLLCTRL_PLL_CONTROL, 0x0, 3, 3);
295
296         /* READ 0x0 reset is in progress */
297         if (hdmi_wait_for_bit_change(PLLCTRL_PLL_STATUS, 0, 0, 1) != 1) {
298                 DSSERR("Failed to sysreset PLL\n");
299                 return -ETIMEDOUT;
300         }
301
302         return 0;
303 }
304
305 static int hdmi_phy_init(void)
306 {
307         u16 r = 0;
308
309         r = hdmi_set_phy_pwr(HDMI_PHYPWRCMD_LDOON);
310         if (r)
311                 return r;
312
313         r = hdmi_set_phy_pwr(HDMI_PHYPWRCMD_TXON);
314         if (r)
315                 return r;
316
317         /*
318          * Read address 0 in order to get the SCP reset done completed
319          * Dummy access performed to make sure reset is done
320          */
321         hdmi_read_reg(HDMI_TXPHY_TX_CTRL);
322
323         /*
324          * Write to phy address 0 to configure the clock
325          * use HFBITCLK write HDMI_TXPHY_TX_CONTROL_FREQOUT field
326          */
327         REG_FLD_MOD(HDMI_TXPHY_TX_CTRL, 0x1, 31, 30);
328
329         /* Write to phy address 1 to start HDMI line (TXVALID and TMDSCLKEN) */
330         hdmi_write_reg(HDMI_TXPHY_DIGITAL_CTRL, 0xF0000000);
331
332         /* Setup max LDO voltage */
333         REG_FLD_MOD(HDMI_TXPHY_POWER_CTRL, 0xB, 3, 0);
334
335         /* Write to phy address 3 to change the polarity control */
336         REG_FLD_MOD(HDMI_TXPHY_PAD_CFG_CTRL, 0x1, 27, 27);
337
338         return 0;
339 }
340
341 static int hdmi_pll_program(struct hdmi_pll_info *fmt)
342 {
343         u16 r = 0;
344         enum hdmi_clk_refsel refsel;
345
346         r = hdmi_set_pll_pwr(HDMI_PLLPWRCMD_ALLOFF);
347         if (r)
348                 return r;
349
350         r = hdmi_set_pll_pwr(HDMI_PLLPWRCMD_BOTHON_ALLCLKS);
351         if (r)
352                 return r;
353
354         r = hdmi_pll_reset();
355         if (r)
356                 return r;
357
358         refsel = HDMI_REFSEL_SYSCLK;
359
360         r = hdmi_pll_init(refsel, fmt->dcofreq, fmt, fmt->regsd);
361         if (r)
362                 return r;
363
364         return 0;
365 }
366
367 static void hdmi_phy_off(void)
368 {
369         hdmi_set_phy_pwr(HDMI_PHYPWRCMD_OFF);
370 }
371
372 static int hdmi_core_ddc_edid(u8 *pedid, int ext)
373 {
374         u32 i, j;
375         char checksum = 0;
376         u32 offset = 0;
377
378         /* Turn on CLK for DDC */
379         REG_FLD_MOD(HDMI_CORE_AV_DPD, 0x7, 2, 0);
380
381         /*
382          * SW HACK : Without the Delay DDC(i2c bus) reads 0 values /
383          * right shifted values( The behavior is not consistent and seen only
384          * with some TV's)
385          */
386         usleep_range(800, 1000);
387
388         if (!ext) {
389                 /* Clk SCL Devices */
390                 REG_FLD_MOD(HDMI_CORE_DDC_CMD, 0xA, 3, 0);
391
392                 /* HDMI_CORE_DDC_STATUS_IN_PROG */
393                 if (hdmi_wait_for_bit_change(HDMI_CORE_DDC_STATUS,
394                                                 4, 4, 0) != 0) {
395                         DSSERR("Failed to program DDC\n");
396                         return -ETIMEDOUT;
397                 }
398
399                 /* Clear FIFO */
400                 REG_FLD_MOD(HDMI_CORE_DDC_CMD, 0x9, 3, 0);
401
402                 /* HDMI_CORE_DDC_STATUS_IN_PROG */
403                 if (hdmi_wait_for_bit_change(HDMI_CORE_DDC_STATUS,
404                                                 4, 4, 0) != 0) {
405                         DSSERR("Failed to program DDC\n");
406                         return -ETIMEDOUT;
407                 }
408
409         } else {
410                 if (ext % 2 != 0)
411                         offset = 0x80;
412         }
413
414         /* Load Segment Address Register */
415         REG_FLD_MOD(HDMI_CORE_DDC_SEGM, ext/2, 7, 0);
416
417         /* Load Slave Address Register */
418         REG_FLD_MOD(HDMI_CORE_DDC_ADDR, 0xA0 >> 1, 7, 1);
419
420         /* Load Offset Address Register */
421         REG_FLD_MOD(HDMI_CORE_DDC_OFFSET, offset, 7, 0);
422
423         /* Load Byte Count */
424         REG_FLD_MOD(HDMI_CORE_DDC_COUNT1, 0x80, 7, 0);
425         REG_FLD_MOD(HDMI_CORE_DDC_COUNT2, 0x0, 1, 0);
426
427         /* Set DDC_CMD */
428         if (ext)
429                 REG_FLD_MOD(HDMI_CORE_DDC_CMD, 0x4, 3, 0);
430         else
431                 REG_FLD_MOD(HDMI_CORE_DDC_CMD, 0x2, 3, 0);
432
433         /* HDMI_CORE_DDC_STATUS_BUS_LOW */
434         if (REG_GET(HDMI_CORE_DDC_STATUS, 6, 6) == 1) {
435                 DSSWARN("I2C Bus Low?\n");
436                 return -EIO;
437         }
438         /* HDMI_CORE_DDC_STATUS_NO_ACK */
439         if (REG_GET(HDMI_CORE_DDC_STATUS, 5, 5) == 1) {
440                 DSSWARN("I2C No Ack\n");
441                 return -EIO;
442         }
443
444         i = ext * 128;
445         j = 0;
446         while (((REG_GET(HDMI_CORE_DDC_STATUS, 4, 4) == 1) ||
447                         (REG_GET(HDMI_CORE_DDC_STATUS, 2, 2) == 0)) &&
448                         j < 128) {
449
450                 if (REG_GET(HDMI_CORE_DDC_STATUS, 2, 2) == 0) {
451                         /* FIFO not empty */
452                         pedid[i++] = REG_GET(HDMI_CORE_DDC_DATA, 7, 0);
453                         j++;
454                 }
455         }
456
457         for (j = 0; j < 128; j++)
458                 checksum += pedid[j];
459
460         if (checksum != 0) {
461                 DSSERR("E-EDID checksum failed!!\n");
462                 return -EIO;
463         }
464
465         return 0;
466 }
467
468 static int read_edid(u8 *pedid, u16 max_length)
469 {
470         int r = 0, n = 0, i = 0;
471         int max_ext_blocks = (max_length / 128) - 1;
472
473         r = hdmi_core_ddc_edid(pedid, 0);
474         if (r) {
475                 return r;
476         } else {
477                 n = pedid[0x7e];
478
479                 /*
480                  * README: need to comply with max_length set by the caller.
481                  * Better implementation should be to allocate necessary
482                  * memory to store EDID according to nb_block field found
483                  * in first block
484                  */
485                 if (n > max_ext_blocks)
486                         n = max_ext_blocks;
487
488                 for (i = 1; i <= n; i++) {
489                         r = hdmi_core_ddc_edid(pedid, i);
490                         if (r)
491                                 return r;
492                 }
493         }
494         return 0;
495 }
496
497 static int get_timings_index(void)
498 {
499         int code;
500
501         if (hdmi.mode == 0)
502                 code = code_vesa[hdmi.code];
503         else
504                 code = code_cea[hdmi.code];
505
506         if (code == -1) {
507                 /* HDMI code 4 corresponds to 640 * 480 VGA */
508                 hdmi.code = 4;
509                 /* DVI mode 1 corresponds to HDMI 0 to DVI */
510                 hdmi.mode = HDMI_DVI;
511
512                 code = code_vesa[hdmi.code];
513         }
514         return code;
515 }
516
517 static struct hdmi_cm hdmi_get_code(struct omap_video_timings *timing)
518 {
519         int i = 0, code = -1, temp_vsync = 0, temp_hsync = 0;
520         int timing_vsync = 0, timing_hsync = 0;
521         struct omap_video_timings temp;
522         struct hdmi_cm cm = {-1};
523         DSSDBG("hdmi_get_code\n");
524
525         for (i = 0; i < OMAP_HDMI_TIMINGS_NB; i++) {
526                 temp = cea_vesa_timings[i].timings;
527                 if ((temp.pixel_clock == timing->pixel_clock) &&
528                         (temp.x_res == timing->x_res) &&
529                         (temp.y_res == timing->y_res)) {
530
531                         temp_hsync = temp.hfp + temp.hsw + temp.hbp;
532                         timing_hsync = timing->hfp + timing->hsw + timing->hbp;
533                         temp_vsync = temp.vfp + temp.vsw + temp.vbp;
534                         timing_vsync = timing->vfp + timing->vsw + timing->vbp;
535
536                         DSSDBG("temp_hsync = %d , temp_vsync = %d"
537                                 "timing_hsync = %d, timing_vsync = %d\n",
538                                 temp_hsync, temp_hsync,
539                                 timing_hsync, timing_vsync);
540
541                         if ((temp_hsync == timing_hsync) &&
542                                         (temp_vsync == timing_vsync)) {
543                                 code = i;
544                                 cm.code = code_index[i];
545                                 if (code < 14)
546                                         cm.mode = HDMI_HDMI;
547                                 else
548                                         cm.mode = HDMI_DVI;
549                                 DSSDBG("Hdmi_code = %d mode = %d\n",
550                                          cm.code, cm.mode);
551                                 break;
552                          }
553                 }
554         }
555
556         return cm;
557 }
558
559 static void get_horz_vert_timing_info(int current_descriptor_addrs, u8 *edid ,
560                 struct omap_video_timings *timings)
561 {
562         /* X and Y resolution */
563         timings->x_res = (((edid[current_descriptor_addrs + 4] & 0xF0) << 4) |
564                          edid[current_descriptor_addrs + 2]);
565         timings->y_res = (((edid[current_descriptor_addrs + 7] & 0xF0) << 4) |
566                          edid[current_descriptor_addrs + 5]);
567
568         timings->pixel_clock = ((edid[current_descriptor_addrs + 1] << 8) |
569                                 edid[current_descriptor_addrs]);
570
571         timings->pixel_clock = 10 * timings->pixel_clock;
572
573         /* HORIZONTAL FRONT PORCH */
574         timings->hfp = edid[current_descriptor_addrs + 8] |
575                         ((edid[current_descriptor_addrs + 11] & 0xc0) << 2);
576         /* HORIZONTAL SYNC WIDTH */
577         timings->hsw = edid[current_descriptor_addrs + 9] |
578                         ((edid[current_descriptor_addrs + 11] & 0x30) << 4);
579         /* HORIZONTAL BACK PORCH */
580         timings->hbp = (((edid[current_descriptor_addrs + 4] & 0x0F) << 8) |
581                         edid[current_descriptor_addrs + 3]) -
582                         (timings->hfp + timings->hsw);
583         /* VERTICAL FRONT PORCH */
584         timings->vfp = ((edid[current_descriptor_addrs + 10] & 0xF0) >> 4) |
585                         ((edid[current_descriptor_addrs + 11] & 0x0f) << 2);
586         /* VERTICAL SYNC WIDTH */
587         timings->vsw = (edid[current_descriptor_addrs + 10] & 0x0F) |
588                         ((edid[current_descriptor_addrs + 11] & 0x03) << 4);
589         /* VERTICAL BACK PORCH */
590         timings->vbp = (((edid[current_descriptor_addrs + 7] & 0x0F) << 8) |
591                         edid[current_descriptor_addrs + 6]) -
592                         (timings->vfp + timings->vsw);
593
594 }
595
596 /* Description : This function gets the resolution information from EDID */
597 static void get_edid_timing_data(u8 *edid)
598 {
599         u8 count;
600         u16 current_descriptor_addrs;
601         struct hdmi_cm cm;
602         struct omap_video_timings edid_timings;
603
604         /* search block 0, there are 4 DTDs arranged in priority order */
605         for (count = 0; count < EDID_SIZE_BLOCK0_TIMING_DESCRIPTOR; count++) {
606                 current_descriptor_addrs =
607                         EDID_DESCRIPTOR_BLOCK0_ADDRESS +
608                         count * EDID_TIMING_DESCRIPTOR_SIZE;
609                 get_horz_vert_timing_info(current_descriptor_addrs,
610                                 edid, &edid_timings);
611                 cm = hdmi_get_code(&edid_timings);
612                 DSSDBG("Block0[%d] value matches code = %d , mode = %d\n",
613                         count, cm.code, cm.mode);
614                 if (cm.code == -1) {
615                         continue;
616                 } else {
617                         hdmi.code = cm.code;
618                         hdmi.mode = cm.mode;
619                         DSSDBG("code = %d , mode = %d\n",
620                                 hdmi.code, hdmi.mode);
621                         return;
622                 }
623         }
624         if (edid[0x7e] != 0x00) {
625                 for (count = 0; count < EDID_SIZE_BLOCK1_TIMING_DESCRIPTOR;
626                         count++) {
627                         current_descriptor_addrs =
628                         EDID_DESCRIPTOR_BLOCK1_ADDRESS +
629                         count * EDID_TIMING_DESCRIPTOR_SIZE;
630                         get_horz_vert_timing_info(current_descriptor_addrs,
631                                                 edid, &edid_timings);
632                         cm = hdmi_get_code(&edid_timings);
633                         DSSDBG("Block1[%d] value matches code = %d, mode = %d",
634                                 count, cm.code, cm.mode);
635                         if (cm.code == -1) {
636                                 continue;
637                         } else {
638                                 hdmi.code = cm.code;
639                                 hdmi.mode = cm.mode;
640                                 DSSDBG("code = %d , mode = %d\n",
641                                         hdmi.code, hdmi.mode);
642                                 return;
643                         }
644                 }
645         }
646
647         DSSINFO("no valid timing found , falling back to VGA\n");
648         hdmi.code = 4; /* setting default value of 640 480 VGA */
649         hdmi.mode = HDMI_DVI;
650 }
651
652 static void hdmi_read_edid(struct omap_video_timings *dp)
653 {
654         int ret = 0, code;
655
656         memset(hdmi.edid, 0, HDMI_EDID_MAX_LENGTH);
657
658         if (!hdmi.edid_set)
659                 ret = read_edid(hdmi.edid, HDMI_EDID_MAX_LENGTH);
660
661         if (!ret) {
662                 if (!memcmp(hdmi.edid, edid_header, sizeof(edid_header))) {
663                         /* search for timings of default resolution */
664                         get_edid_timing_data(hdmi.edid);
665                         hdmi.edid_set = true;
666                 }
667         } else {
668                 DSSWARN("failed to read E-EDID\n");
669         }
670
671         if (!hdmi.edid_set) {
672                 DSSINFO("fallback to VGA\n");
673                 hdmi.code = 4; /* setting default value of 640 480 VGA */
674                 hdmi.mode = HDMI_DVI;
675         }
676
677         code = get_timings_index();
678
679         *dp = cea_vesa_timings[code].timings;
680 }
681
682 static void hdmi_core_init(struct hdmi_core_video_config *video_cfg,
683                         struct hdmi_core_infoframe_avi *avi_cfg,
684                         struct hdmi_core_packet_enable_repeat *repeat_cfg)
685 {
686         DSSDBG("Enter hdmi_core_init\n");
687
688         /* video core */
689         video_cfg->ip_bus_width = HDMI_INPUT_8BIT;
690         video_cfg->op_dither_truc = HDMI_OUTPUTTRUNCATION_8BIT;
691         video_cfg->deep_color_pkt = HDMI_DEEPCOLORPACKECTDISABLE;
692         video_cfg->pkt_mode = HDMI_PACKETMODERESERVEDVALUE;
693         video_cfg->hdmi_dvi = HDMI_DVI;
694         video_cfg->tclk_sel_clkmult = HDMI_FPLL10IDCK;
695
696         /* info frame */
697         avi_cfg->db1_format = 0;
698         avi_cfg->db1_active_info = 0;
699         avi_cfg->db1_bar_info_dv = 0;
700         avi_cfg->db1_scan_info = 0;
701         avi_cfg->db2_colorimetry = 0;
702         avi_cfg->db2_aspect_ratio = 0;
703         avi_cfg->db2_active_fmt_ar = 0;
704         avi_cfg->db3_itc = 0;
705         avi_cfg->db3_ec = 0;
706         avi_cfg->db3_q_range = 0;
707         avi_cfg->db3_nup_scaling = 0;
708         avi_cfg->db4_videocode = 0;
709         avi_cfg->db5_pixel_repeat = 0;
710         avi_cfg->db6_7_line_eoftop = 0 ;
711         avi_cfg->db8_9_line_sofbottom = 0;
712         avi_cfg->db10_11_pixel_eofleft = 0;
713         avi_cfg->db12_13_pixel_sofright = 0;
714
715         /* packet enable and repeat */
716         repeat_cfg->audio_pkt = 0;
717         repeat_cfg->audio_pkt_repeat = 0;
718         repeat_cfg->avi_infoframe = 0;
719         repeat_cfg->avi_infoframe_repeat = 0;
720         repeat_cfg->gen_cntrl_pkt = 0;
721         repeat_cfg->gen_cntrl_pkt_repeat = 0;
722         repeat_cfg->generic_pkt = 0;
723         repeat_cfg->generic_pkt_repeat = 0;
724 }
725
726 static void hdmi_core_powerdown_disable(void)
727 {
728         DSSDBG("Enter hdmi_core_powerdown_disable\n");
729         REG_FLD_MOD(HDMI_CORE_CTRL1, 0x0, 0, 0);
730 }
731
732 static void hdmi_core_swreset_release(void)
733 {
734         DSSDBG("Enter hdmi_core_swreset_release\n");
735         REG_FLD_MOD(HDMI_CORE_SYS_SRST, 0x0, 0, 0);
736 }
737
738 static void hdmi_core_swreset_assert(void)
739 {
740         DSSDBG("Enter hdmi_core_swreset_assert\n");
741         REG_FLD_MOD(HDMI_CORE_SYS_SRST, 0x1, 0, 0);
742 }
743
744 /* DSS_HDMI_CORE_VIDEO_CONFIG */
745 static void hdmi_core_video_config(struct hdmi_core_video_config *cfg)
746 {
747         u32 r = 0;
748
749         /* sys_ctrl1 default configuration not tunable */
750         r = hdmi_read_reg(HDMI_CORE_CTRL1);
751         r = FLD_MOD(r, HDMI_CORE_CTRL1_VEN_FOLLOWVSYNC, 5, 5);
752         r = FLD_MOD(r, HDMI_CORE_CTRL1_HEN_FOLLOWHSYNC, 4, 4);
753         r = FLD_MOD(r, HDMI_CORE_CTRL1_BSEL_24BITBUS, 2, 2);
754         r = FLD_MOD(r, HDMI_CORE_CTRL1_EDGE_RISINGEDGE, 1, 1);
755         hdmi_write_reg(HDMI_CORE_CTRL1, r);
756
757         REG_FLD_MOD(HDMI_CORE_SYS_VID_ACEN, cfg->ip_bus_width, 7, 6);
758
759         /* Vid_Mode */
760         r = hdmi_read_reg(HDMI_CORE_SYS_VID_MODE);
761
762         /* dither truncation configuration */
763         if (cfg->op_dither_truc > HDMI_OUTPUTTRUNCATION_12BIT) {
764                 r = FLD_MOD(r, cfg->op_dither_truc - 3, 7, 6);
765                 r = FLD_MOD(r, 1, 5, 5);
766         } else {
767                 r = FLD_MOD(r, cfg->op_dither_truc, 7, 6);
768                 r = FLD_MOD(r, 0, 5, 5);
769         }
770         hdmi_write_reg(HDMI_CORE_SYS_VID_MODE, r);
771
772         /* HDMI_Ctrl */
773         r = hdmi_read_reg(HDMI_CORE_AV_HDMI_CTRL);
774         r = FLD_MOD(r, cfg->deep_color_pkt, 6, 6);
775         r = FLD_MOD(r, cfg->pkt_mode, 5, 3);
776         r = FLD_MOD(r, cfg->hdmi_dvi, 0, 0);
777         hdmi_write_reg(HDMI_CORE_AV_HDMI_CTRL, r);
778
779         /* TMDS_CTRL */
780         REG_FLD_MOD(HDMI_CORE_SYS_TMDS_CTRL,
781                 cfg->tclk_sel_clkmult, 6, 5);
782 }
783
784 static void hdmi_core_aux_infoframe_avi_config(
785                 struct hdmi_core_infoframe_avi info_avi)
786 {
787         u32 val;
788         char sum = 0, checksum = 0;
789
790         sum += 0x82 + 0x002 + 0x00D;
791         hdmi_write_reg(HDMI_CORE_AV_AVI_TYPE, 0x082);
792         hdmi_write_reg(HDMI_CORE_AV_AVI_VERS, 0x002);
793         hdmi_write_reg(HDMI_CORE_AV_AVI_LEN, 0x00D);
794
795         val = (info_avi.db1_format << 5) |
796                 (info_avi.db1_active_info << 4) |
797                 (info_avi.db1_bar_info_dv << 2) |
798                 (info_avi.db1_scan_info);
799         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(0), val);
800         sum += val;
801
802         val = (info_avi.db2_colorimetry << 6) |
803                 (info_avi.db2_aspect_ratio << 4) |
804                 (info_avi.db2_active_fmt_ar);
805         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(1), val);
806         sum += val;
807
808         val = (info_avi.db3_itc << 7) |
809                 (info_avi.db3_ec << 4) |
810                 (info_avi.db3_q_range << 2) |
811                 (info_avi.db3_nup_scaling);
812         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(2), val);
813         sum += val;
814
815         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(3), info_avi.db4_videocode);
816         sum += info_avi.db4_videocode;
817
818         val = info_avi.db5_pixel_repeat;
819         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(4), val);
820         sum += val;
821
822         val = info_avi.db6_7_line_eoftop & 0x00FF;
823         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(5), val);
824         sum += val;
825
826         val = ((info_avi.db6_7_line_eoftop >> 8) & 0x00FF);
827         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(6), val);
828         sum += val;
829
830         val = info_avi.db8_9_line_sofbottom & 0x00FF;
831         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(7), val);
832         sum += val;
833
834         val = ((info_avi.db8_9_line_sofbottom >> 8) & 0x00FF);
835         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(8), val);
836         sum += val;
837
838         val = info_avi.db10_11_pixel_eofleft & 0x00FF;
839         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(9), val);
840         sum += val;
841
842         val = ((info_avi.db10_11_pixel_eofleft >> 8) & 0x00FF);
843         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(10), val);
844         sum += val;
845
846         val = info_avi.db12_13_pixel_sofright & 0x00FF;
847         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(11), val);
848         sum += val;
849
850         val = ((info_avi.db12_13_pixel_sofright >> 8) & 0x00FF);
851         hdmi_write_reg(HDMI_CORE_AV_AVI_DBYTE(12), val);
852         sum += val;
853
854         checksum = 0x100 - sum;
855         hdmi_write_reg(HDMI_CORE_AV_AVI_CHSUM, checksum);
856 }
857
858 static void hdmi_core_av_packet_config(
859                 struct hdmi_core_packet_enable_repeat repeat_cfg)
860 {
861         /* enable/repeat the infoframe */
862         hdmi_write_reg(HDMI_CORE_AV_PB_CTRL1,
863                 (repeat_cfg.audio_pkt << 5) |
864                 (repeat_cfg.audio_pkt_repeat << 4) |
865                 (repeat_cfg.avi_infoframe << 1) |
866                 (repeat_cfg.avi_infoframe_repeat));
867
868         /* enable/repeat the packet */
869         hdmi_write_reg(HDMI_CORE_AV_PB_CTRL2,
870                 (repeat_cfg.gen_cntrl_pkt << 3) |
871                 (repeat_cfg.gen_cntrl_pkt_repeat << 2) |
872                 (repeat_cfg.generic_pkt << 1) |
873                 (repeat_cfg.generic_pkt_repeat));
874 }
875
876 static void hdmi_wp_init(struct omap_video_timings *timings,
877                         struct hdmi_video_format *video_fmt,
878                         struct hdmi_video_interface *video_int)
879 {
880         DSSDBG("Enter hdmi_wp_init\n");
881
882         timings->hbp = 0;
883         timings->hfp = 0;
884         timings->hsw = 0;
885         timings->vbp = 0;
886         timings->vfp = 0;
887         timings->vsw = 0;
888
889         video_fmt->packing_mode = HDMI_PACK_10b_RGB_YUV444;
890         video_fmt->y_res = 0;
891         video_fmt->x_res = 0;
892
893         video_int->vsp = 0;
894         video_int->hsp = 0;
895
896         video_int->interlacing = 0;
897         video_int->tm = 0; /* HDMI_TIMING_SLAVE */
898
899 }
900
901 static void hdmi_wp_video_start(bool start)
902 {
903         REG_FLD_MOD(HDMI_WP_VIDEO_CFG, start, 31, 31);
904 }
905
906 static void hdmi_wp_video_init_format(struct hdmi_video_format *video_fmt,
907         struct omap_video_timings *timings, struct hdmi_config *param)
908 {
909         DSSDBG("Enter hdmi_wp_video_init_format\n");
910
911         video_fmt->y_res = param->timings.timings.y_res;
912         video_fmt->x_res = param->timings.timings.x_res;
913
914         timings->hbp = param->timings.timings.hbp;
915         timings->hfp = param->timings.timings.hfp;
916         timings->hsw = param->timings.timings.hsw;
917         timings->vbp = param->timings.timings.vbp;
918         timings->vfp = param->timings.timings.vfp;
919         timings->vsw = param->timings.timings.vsw;
920 }
921
922 static void hdmi_wp_video_config_format(
923                 struct hdmi_video_format *video_fmt)
924 {
925         u32 l = 0;
926
927         REG_FLD_MOD(HDMI_WP_VIDEO_CFG, video_fmt->packing_mode, 10, 8);
928
929         l |= FLD_VAL(video_fmt->y_res, 31, 16);
930         l |= FLD_VAL(video_fmt->x_res, 15, 0);
931         hdmi_write_reg(HDMI_WP_VIDEO_SIZE, l);
932 }
933
934 static void hdmi_wp_video_config_interface(
935                 struct hdmi_video_interface *video_int)
936 {
937         u32 r;
938         DSSDBG("Enter hdmi_wp_video_config_interface\n");
939
940         r = hdmi_read_reg(HDMI_WP_VIDEO_CFG);
941         r = FLD_MOD(r, video_int->vsp, 7, 7);
942         r = FLD_MOD(r, video_int->hsp, 6, 6);
943         r = FLD_MOD(r, video_int->interlacing, 3, 3);
944         r = FLD_MOD(r, video_int->tm, 1, 0);
945         hdmi_write_reg(HDMI_WP_VIDEO_CFG, r);
946 }
947
948 static void hdmi_wp_video_config_timing(
949                 struct omap_video_timings *timings)
950 {
951         u32 timing_h = 0;
952         u32 timing_v = 0;
953
954         DSSDBG("Enter hdmi_wp_video_config_timing\n");
955
956         timing_h |= FLD_VAL(timings->hbp, 31, 20);
957         timing_h |= FLD_VAL(timings->hfp, 19, 8);
958         timing_h |= FLD_VAL(timings->hsw, 7, 0);
959         hdmi_write_reg(HDMI_WP_VIDEO_TIMING_H, timing_h);
960
961         timing_v |= FLD_VAL(timings->vbp, 31, 20);
962         timing_v |= FLD_VAL(timings->vfp, 19, 8);
963         timing_v |= FLD_VAL(timings->vsw, 7, 0);
964         hdmi_write_reg(HDMI_WP_VIDEO_TIMING_V, timing_v);
965 }
966
967 static void hdmi_basic_configure(struct hdmi_config *cfg)
968 {
969         /* HDMI */
970         struct omap_video_timings video_timing;
971         struct hdmi_video_format video_format;
972         struct hdmi_video_interface video_interface;
973         /* HDMI core */
974         struct hdmi_core_infoframe_avi avi_cfg;
975         struct hdmi_core_video_config v_core_cfg;
976         struct hdmi_core_packet_enable_repeat repeat_cfg;
977
978         hdmi_wp_init(&video_timing, &video_format,
979                 &video_interface);
980
981         hdmi_core_init(&v_core_cfg,
982                 &avi_cfg,
983                 &repeat_cfg);
984
985         hdmi_wp_video_init_format(&video_format,
986                         &video_timing, cfg);
987
988         hdmi_wp_video_config_timing(&video_timing);
989
990         /* video config */
991         video_format.packing_mode = HDMI_PACK_24b_RGB_YUV444_YUV422;
992
993         hdmi_wp_video_config_format(&video_format);
994
995         video_interface.vsp = cfg->timings.vsync_pol;
996         video_interface.hsp = cfg->timings.hsync_pol;
997         video_interface.interlacing = cfg->interlace;
998         video_interface.tm = 1 ; /* HDMI_TIMING_MASTER_24BIT */
999
1000         hdmi_wp_video_config_interface(&video_interface);
1001
1002         /*
1003          * configure core video part
1004          * set software reset in the core
1005          */
1006         hdmi_core_swreset_assert();
1007
1008         /* power down off */
1009         hdmi_core_powerdown_disable();
1010
1011         v_core_cfg.pkt_mode = HDMI_PACKETMODE24BITPERPIXEL;
1012         v_core_cfg.hdmi_dvi = cfg->cm.mode;
1013
1014         hdmi_core_video_config(&v_core_cfg);
1015
1016         /* release software reset in the core */
1017         hdmi_core_swreset_release();
1018
1019         /*
1020          * configure packet
1021          * info frame video see doc CEA861-D page 65
1022          */
1023         avi_cfg.db1_format = HDMI_INFOFRAME_AVI_DB1Y_RGB;
1024         avi_cfg.db1_active_info =
1025                 HDMI_INFOFRAME_AVI_DB1A_ACTIVE_FORMAT_OFF;
1026         avi_cfg.db1_bar_info_dv = HDMI_INFOFRAME_AVI_DB1B_NO;
1027         avi_cfg.db1_scan_info = HDMI_INFOFRAME_AVI_DB1S_0;
1028         avi_cfg.db2_colorimetry = HDMI_INFOFRAME_AVI_DB2C_NO;
1029         avi_cfg.db2_aspect_ratio = HDMI_INFOFRAME_AVI_DB2M_NO;
1030         avi_cfg.db2_active_fmt_ar = HDMI_INFOFRAME_AVI_DB2R_SAME;
1031         avi_cfg.db3_itc = HDMI_INFOFRAME_AVI_DB3ITC_NO;
1032         avi_cfg.db3_ec = HDMI_INFOFRAME_AVI_DB3EC_XVYUV601;
1033         avi_cfg.db3_q_range = HDMI_INFOFRAME_AVI_DB3Q_DEFAULT;
1034         avi_cfg.db3_nup_scaling = HDMI_INFOFRAME_AVI_DB3SC_NO;
1035         avi_cfg.db4_videocode = cfg->cm.code;
1036         avi_cfg.db5_pixel_repeat = HDMI_INFOFRAME_AVI_DB5PR_NO;
1037         avi_cfg.db6_7_line_eoftop = 0;
1038         avi_cfg.db8_9_line_sofbottom = 0;
1039         avi_cfg.db10_11_pixel_eofleft = 0;
1040         avi_cfg.db12_13_pixel_sofright = 0;
1041
1042         hdmi_core_aux_infoframe_avi_config(avi_cfg);
1043
1044         /* enable/repeat the infoframe */
1045         repeat_cfg.avi_infoframe = HDMI_PACKETENABLE;
1046         repeat_cfg.avi_infoframe_repeat = HDMI_PACKETREPEATON;
1047         /* wakeup */
1048         repeat_cfg.audio_pkt = HDMI_PACKETENABLE;
1049         repeat_cfg.audio_pkt_repeat = HDMI_PACKETREPEATON;
1050         hdmi_core_av_packet_config(repeat_cfg);
1051 }
1052
1053 static void update_hdmi_timings(struct hdmi_config *cfg,
1054                 struct omap_video_timings *timings, int code)
1055 {
1056         cfg->timings.timings.x_res = timings->x_res;
1057         cfg->timings.timings.y_res = timings->y_res;
1058         cfg->timings.timings.hbp = timings->hbp;
1059         cfg->timings.timings.hfp = timings->hfp;
1060         cfg->timings.timings.hsw = timings->hsw;
1061         cfg->timings.timings.vbp = timings->vbp;
1062         cfg->timings.timings.vfp = timings->vfp;
1063         cfg->timings.timings.vsw = timings->vsw;
1064         cfg->timings.timings.pixel_clock = timings->pixel_clock;
1065         cfg->timings.vsync_pol = cea_vesa_timings[code].vsync_pol;
1066         cfg->timings.hsync_pol = cea_vesa_timings[code].hsync_pol;
1067 }
1068
1069 static void hdmi_compute_pll(struct omap_dss_device *dssdev, int phy,
1070                 struct hdmi_pll_info *pi)
1071 {
1072         unsigned long clkin, refclk;
1073         u32 mf;
1074
1075         clkin = clk_get_rate(hdmi.sys_clk) / 10000;
1076         /*
1077          * Input clock is predivided by N + 1
1078          * out put of which is reference clk
1079          */
1080         pi->regn = dssdev->clocks.hdmi.regn;
1081         refclk = clkin / (pi->regn + 1);
1082
1083         /*
1084          * multiplier is pixel_clk/ref_clk
1085          * Multiplying by 100 to avoid fractional part removal
1086          */
1087         pi->regm = (phy * 100 / (refclk)) / 100;
1088         pi->regm2 = dssdev->clocks.hdmi.regm2;
1089
1090         /*
1091          * fractional multiplier is remainder of the difference between
1092          * multiplier and actual phy(required pixel clock thus should be
1093          * multiplied by 2^18(262144) divided by the reference clock
1094          */
1095         mf = (phy - pi->regm * refclk) * 262144;
1096         pi->regmf = mf / (refclk);
1097
1098         /*
1099          * Dcofreq should be set to 1 if required pixel clock
1100          * is greater than 1000MHz
1101          */
1102         pi->dcofreq = phy > 1000 * 100;
1103         pi->regsd = ((pi->regm * clkin / 10) / ((pi->regn + 1) * 250) + 5) / 10;
1104
1105         DSSDBG("M = %d Mf = %d\n", pi->regm, pi->regmf);
1106         DSSDBG("range = %d sd = %d\n", pi->dcofreq, pi->regsd);
1107 }
1108
1109 static int hdmi_power_on(struct omap_dss_device *dssdev)
1110 {
1111         int r, code = 0;
1112         struct hdmi_pll_info pll_data;
1113         struct omap_video_timings *p;
1114         unsigned long phy;
1115
1116         r = hdmi_runtime_get();
1117         if (r)
1118                 return r;
1119
1120         dispc_enable_channel(OMAP_DSS_CHANNEL_DIGIT, 0);
1121
1122         p = &dssdev->panel.timings;
1123
1124         DSSDBG("hdmi_power_on x_res= %d y_res = %d\n",
1125                 dssdev->panel.timings.x_res,
1126                 dssdev->panel.timings.y_res);
1127
1128         if (!hdmi.custom_set) {
1129                 DSSDBG("Read EDID as no EDID is not set on poweron\n");
1130                 hdmi_read_edid(p);
1131         }
1132         code = get_timings_index();
1133         dssdev->panel.timings = cea_vesa_timings[code].timings;
1134         update_hdmi_timings(&hdmi.cfg, p, code);
1135
1136         phy = p->pixel_clock;
1137
1138         hdmi_compute_pll(dssdev, phy, &pll_data);
1139
1140         hdmi_wp_video_start(0);
1141
1142         /* config the PLL and PHY first */
1143         r = hdmi_pll_program(&pll_data);
1144         if (r) {
1145                 DSSDBG("Failed to lock PLL\n");
1146                 goto err;
1147         }
1148
1149         r = hdmi_phy_init();
1150         if (r) {
1151                 DSSDBG("Failed to start PHY\n");
1152                 goto err;
1153         }
1154
1155         hdmi.cfg.cm.mode = hdmi.mode;
1156         hdmi.cfg.cm.code = hdmi.code;
1157         hdmi_basic_configure(&hdmi.cfg);
1158
1159         /* Make selection of HDMI in DSS */
1160         dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
1161
1162         /* Select the dispc clock source as PRCM clock, to ensure that it is not
1163          * DSI PLL source as the clock selected by DSI PLL might not be
1164          * sufficient for the resolution selected / that can be changed
1165          * dynamically by user. This can be moved to single location , say
1166          * Boardfile.
1167          */
1168         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
1169
1170         /* bypass TV gamma table */
1171         dispc_enable_gamma_table(0);
1172
1173         /* tv size */
1174         dispc_set_digit_size(dssdev->panel.timings.x_res,
1175                         dssdev->panel.timings.y_res);
1176
1177         dispc_enable_channel(OMAP_DSS_CHANNEL_DIGIT, 1);
1178
1179         hdmi_wp_video_start(1);
1180
1181         return 0;
1182 err:
1183         hdmi_runtime_put();
1184         return -EIO;
1185 }
1186
1187 static void hdmi_power_off(struct omap_dss_device *dssdev)
1188 {
1189         dispc_enable_channel(OMAP_DSS_CHANNEL_DIGIT, 0);
1190
1191         hdmi_wp_video_start(0);
1192         hdmi_phy_off();
1193         hdmi_set_pll_pwr(HDMI_PLLPWRCMD_ALLOFF);
1194         hdmi_runtime_put();
1195
1196         hdmi.edid_set = 0;
1197 }
1198
1199 int omapdss_hdmi_display_check_timing(struct omap_dss_device *dssdev,
1200                                         struct omap_video_timings *timings)
1201 {
1202         struct hdmi_cm cm;
1203
1204         cm = hdmi_get_code(timings);
1205         if (cm.code == -1) {
1206                 DSSERR("Invalid timing entered\n");
1207                 return -EINVAL;
1208         }
1209
1210         return 0;
1211
1212 }
1213
1214 void omapdss_hdmi_display_set_timing(struct omap_dss_device *dssdev)
1215 {
1216         struct hdmi_cm cm;
1217
1218         hdmi.custom_set = 1;
1219         cm = hdmi_get_code(&dssdev->panel.timings);
1220         hdmi.code = cm.code;
1221         hdmi.mode = cm.mode;
1222         omapdss_hdmi_display_enable(dssdev);
1223         hdmi.custom_set = 0;
1224 }
1225
1226 int omapdss_hdmi_display_enable(struct omap_dss_device *dssdev)
1227 {
1228         int r = 0;
1229
1230         DSSDBG("ENTER hdmi_display_enable\n");
1231
1232         mutex_lock(&hdmi.lock);
1233
1234         r = omap_dss_start_device(dssdev);
1235         if (r) {
1236                 DSSERR("failed to start device\n");
1237                 goto err0;
1238         }
1239
1240         if (dssdev->platform_enable) {
1241                 r = dssdev->platform_enable(dssdev);
1242                 if (r) {
1243                         DSSERR("failed to enable GPIO's\n");
1244                         goto err1;
1245                 }
1246         }
1247
1248         r = hdmi_power_on(dssdev);
1249         if (r) {
1250                 DSSERR("failed to power on device\n");
1251                 goto err2;
1252         }
1253
1254         mutex_unlock(&hdmi.lock);
1255         return 0;
1256
1257 err2:
1258         if (dssdev->platform_disable)
1259                 dssdev->platform_disable(dssdev);
1260 err1:
1261         omap_dss_stop_device(dssdev);
1262 err0:
1263         mutex_unlock(&hdmi.lock);
1264         return r;
1265 }
1266
1267 void omapdss_hdmi_display_disable(struct omap_dss_device *dssdev)
1268 {
1269         DSSDBG("Enter hdmi_display_disable\n");
1270
1271         mutex_lock(&hdmi.lock);
1272
1273         hdmi_power_off(dssdev);
1274
1275         if (dssdev->platform_disable)
1276                 dssdev->platform_disable(dssdev);
1277
1278         omap_dss_stop_device(dssdev);
1279
1280         mutex_unlock(&hdmi.lock);
1281 }
1282
1283 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
1284         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
1285 static void hdmi_wp_audio_config_format(
1286                 struct hdmi_audio_format *aud_fmt)
1287 {
1288         u32 r;
1289
1290         DSSDBG("Enter hdmi_wp_audio_config_format\n");
1291
1292         r = hdmi_read_reg(HDMI_WP_AUDIO_CFG);
1293         r = FLD_MOD(r, aud_fmt->stereo_channels, 26, 24);
1294         r = FLD_MOD(r, aud_fmt->active_chnnls_msk, 23, 16);
1295         r = FLD_MOD(r, aud_fmt->en_sig_blk_strt_end, 5, 5);
1296         r = FLD_MOD(r, aud_fmt->type, 4, 4);
1297         r = FLD_MOD(r, aud_fmt->justification, 3, 3);
1298         r = FLD_MOD(r, aud_fmt->sample_order, 2, 2);
1299         r = FLD_MOD(r, aud_fmt->samples_per_word, 1, 1);
1300         r = FLD_MOD(r, aud_fmt->sample_size, 0, 0);
1301         hdmi_write_reg(HDMI_WP_AUDIO_CFG, r);
1302 }
1303
1304 static void hdmi_wp_audio_config_dma(struct hdmi_audio_dma *aud_dma)
1305 {
1306         u32 r;
1307
1308         DSSDBG("Enter hdmi_wp_audio_config_dma\n");
1309
1310         r = hdmi_read_reg(HDMI_WP_AUDIO_CFG2);
1311         r = FLD_MOD(r, aud_dma->transfer_size, 15, 8);
1312         r = FLD_MOD(r, aud_dma->block_size, 7, 0);
1313         hdmi_write_reg(HDMI_WP_AUDIO_CFG2, r);
1314
1315         r = hdmi_read_reg(HDMI_WP_AUDIO_CTRL);
1316         r = FLD_MOD(r, aud_dma->mode, 9, 9);
1317         r = FLD_MOD(r, aud_dma->fifo_threshold, 8, 0);
1318         hdmi_write_reg(HDMI_WP_AUDIO_CTRL, r);
1319 }
1320
1321 static void hdmi_core_audio_config(struct hdmi_core_audio_config *cfg)
1322 {
1323         u32 r;
1324
1325         /* audio clock recovery parameters */
1326         r = hdmi_read_reg(HDMI_CORE_AV_ACR_CTRL);
1327         r = FLD_MOD(r, cfg->use_mclk, 2, 2);
1328         r = FLD_MOD(r, cfg->en_acr_pkt, 1, 1);
1329         r = FLD_MOD(r, cfg->cts_mode, 0, 0);
1330         hdmi_write_reg(HDMI_CORE_AV_ACR_CTRL, r);
1331
1332         REG_FLD_MOD(HDMI_CORE_AV_N_SVAL1, cfg->n, 7, 0);
1333         REG_FLD_MOD(HDMI_CORE_AV_N_SVAL2, cfg->n >> 8, 7, 0);
1334         REG_FLD_MOD(HDMI_CORE_AV_N_SVAL3, cfg->n >> 16, 7, 0);
1335
1336         if (cfg->cts_mode == HDMI_AUDIO_CTS_MODE_SW) {
1337                 REG_FLD_MOD(HDMI_CORE_AV_CTS_SVAL1, cfg->cts, 7, 0);
1338                 REG_FLD_MOD(HDMI_CORE_AV_CTS_SVAL2, cfg->cts >> 8, 7, 0);
1339                 REG_FLD_MOD(HDMI_CORE_AV_CTS_SVAL3, cfg->cts >> 16, 7, 0);
1340         } else {
1341                 /*
1342                  * HDMI IP uses this configuration to divide the MCLK to
1343                  * update CTS value.
1344                  */
1345                 REG_FLD_MOD(HDMI_CORE_AV_FREQ_SVAL, cfg->mclk_mode, 2, 0);
1346
1347                 /* Configure clock for audio packets */
1348                 REG_FLD_MOD(HDMI_CORE_AV_AUD_PAR_BUSCLK_1,
1349                         cfg->aud_par_busclk, 7, 0);
1350                 REG_FLD_MOD(HDMI_CORE_AV_AUD_PAR_BUSCLK_2,
1351                         (cfg->aud_par_busclk >> 8), 7, 0);
1352                 REG_FLD_MOD(HDMI_CORE_AV_AUD_PAR_BUSCLK_3,
1353                         (cfg->aud_par_busclk >> 16), 7, 0);
1354         }
1355
1356         /* Override of SPDIF sample frequency with value in I2S_CHST4 */
1357         REG_FLD_MOD(HDMI_CORE_AV_SPDIF_CTRL, cfg->fs_override, 1, 1);
1358
1359         /* I2S parameters */
1360         REG_FLD_MOD(HDMI_CORE_AV_I2S_CHST4, cfg->freq_sample, 3, 0);
1361
1362         r = hdmi_read_reg(HDMI_CORE_AV_I2S_IN_CTRL);
1363         r = FLD_MOD(r, cfg->i2s_cfg.en_high_bitrate_aud, 7, 7);
1364         r = FLD_MOD(r, cfg->i2s_cfg.sck_edge_mode, 6, 6);
1365         r = FLD_MOD(r, cfg->i2s_cfg.cbit_order, 5, 5);
1366         r = FLD_MOD(r, cfg->i2s_cfg.vbit, 4, 4);
1367         r = FLD_MOD(r, cfg->i2s_cfg.ws_polarity, 3, 3);
1368         r = FLD_MOD(r, cfg->i2s_cfg.justification, 2, 2);
1369         r = FLD_MOD(r, cfg->i2s_cfg.direction, 1, 1);
1370         r = FLD_MOD(r, cfg->i2s_cfg.shift, 0, 0);
1371         hdmi_write_reg(HDMI_CORE_AV_I2S_IN_CTRL, r);
1372
1373         r = hdmi_read_reg(HDMI_CORE_AV_I2S_CHST5);
1374         r = FLD_MOD(r, cfg->freq_sample, 7, 4);
1375         r = FLD_MOD(r, cfg->i2s_cfg.word_length, 3, 1);
1376         r = FLD_MOD(r, cfg->i2s_cfg.word_max_length, 0, 0);
1377         hdmi_write_reg(HDMI_CORE_AV_I2S_CHST5, r);
1378
1379         REG_FLD_MOD(HDMI_CORE_AV_I2S_IN_LEN, cfg->i2s_cfg.in_length_bits, 3, 0);
1380
1381         /* Audio channels and mode parameters */
1382         REG_FLD_MOD(HDMI_CORE_AV_HDMI_CTRL, cfg->layout, 2, 1);
1383         r = hdmi_read_reg(HDMI_CORE_AV_AUD_MODE);
1384         r = FLD_MOD(r, cfg->i2s_cfg.active_sds, 7, 4);
1385         r = FLD_MOD(r, cfg->en_dsd_audio, 3, 3);
1386         r = FLD_MOD(r, cfg->en_parallel_aud_input, 2, 2);
1387         r = FLD_MOD(r, cfg->en_spdif, 1, 1);
1388         hdmi_write_reg(HDMI_CORE_AV_AUD_MODE, r);
1389 }
1390
1391 static void hdmi_core_audio_infoframe_config(
1392                 struct hdmi_core_infoframe_audio *info_aud)
1393 {
1394         u8 val;
1395         u8 sum = 0, checksum = 0;
1396
1397         /*
1398          * Set audio info frame type, version and length as
1399          * described in HDMI 1.4a Section 8.2.2 specification.
1400          * Checksum calculation is defined in Section 5.3.5.
1401          */
1402         hdmi_write_reg(HDMI_CORE_AV_AUDIO_TYPE, 0x84);
1403         hdmi_write_reg(HDMI_CORE_AV_AUDIO_VERS, 0x01);
1404         hdmi_write_reg(HDMI_CORE_AV_AUDIO_LEN, 0x0a);
1405         sum += 0x84 + 0x001 + 0x00a;
1406
1407         val = (info_aud->db1_coding_type << 4)
1408                         | (info_aud->db1_channel_count - 1);
1409         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(0), val);
1410         sum += val;
1411
1412         val = (info_aud->db2_sample_freq << 2) | info_aud->db2_sample_size;
1413         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(1), val);
1414         sum += val;
1415
1416         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(2), 0x00);
1417
1418         val = info_aud->db4_channel_alloc;
1419         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(3), val);
1420         sum += val;
1421
1422         val = (info_aud->db5_downmix_inh << 7) | (info_aud->db5_lsv << 3);
1423         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(4), val);
1424         sum += val;
1425
1426         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(5), 0x00);
1427         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(6), 0x00);
1428         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(7), 0x00);
1429         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(8), 0x00);
1430         hdmi_write_reg(HDMI_CORE_AV_AUD_DBYTE(9), 0x00);
1431
1432         checksum = 0x100 - sum;
1433         hdmi_write_reg(HDMI_CORE_AV_AUDIO_CHSUM, checksum);
1434
1435         /*
1436          * TODO: Add MPEG and SPD enable and repeat cfg when EDID parsing
1437          * is available.
1438          */
1439 }
1440
1441 static int hdmi_config_audio_acr(u32 sample_freq, u32 *n, u32 *cts)
1442 {
1443         u32 r;
1444         u32 deep_color = 0;
1445         u32 pclk = hdmi.cfg.timings.timings.pixel_clock;
1446
1447         if (n == NULL || cts == NULL)
1448                 return -EINVAL;
1449         /*
1450          * Obtain current deep color configuration. This needed
1451          * to calculate the TMDS clock based on the pixel clock.
1452          */
1453         r = REG_GET(HDMI_WP_VIDEO_CFG, 1, 0);
1454         switch (r) {
1455         case 1: /* No deep color selected */
1456                 deep_color = 100;
1457                 break;
1458         case 2: /* 10-bit deep color selected */
1459                 deep_color = 125;
1460                 break;
1461         case 3: /* 12-bit deep color selected */
1462                 deep_color = 150;
1463                 break;
1464         default:
1465                 return -EINVAL;
1466         }
1467
1468         switch (sample_freq) {
1469         case 32000:
1470                 if ((deep_color == 125) && ((pclk == 54054)
1471                                 || (pclk == 74250)))
1472                         *n = 8192;
1473                 else
1474                         *n = 4096;
1475                 break;
1476         case 44100:
1477                 *n = 6272;
1478                 break;
1479         case 48000:
1480                 if ((deep_color == 125) && ((pclk == 54054)
1481                                 || (pclk == 74250)))
1482                         *n = 8192;
1483                 else
1484                         *n = 6144;
1485                 break;
1486         default:
1487                 *n = 0;
1488                 return -EINVAL;
1489         }
1490
1491         /* Calculate CTS. See HDMI 1.3a or 1.4a specifications */
1492         *cts = pclk * (*n / 128) * deep_color / (sample_freq / 10);
1493
1494         return 0;
1495 }
1496
1497 static int hdmi_audio_hw_params(struct snd_pcm_substream *substream,
1498                                     struct snd_pcm_hw_params *params,
1499                                     struct snd_soc_dai *dai)
1500 {
1501         struct hdmi_audio_format audio_format;
1502         struct hdmi_audio_dma audio_dma;
1503         struct hdmi_core_audio_config core_cfg;
1504         struct hdmi_core_infoframe_audio aud_if_cfg;
1505         int err, n, cts;
1506         enum hdmi_core_audio_sample_freq sample_freq;
1507
1508         switch (params_format(params)) {
1509         case SNDRV_PCM_FORMAT_S16_LE:
1510                 core_cfg.i2s_cfg.word_max_length =
1511                         HDMI_AUDIO_I2S_MAX_WORD_20BITS;
1512                 core_cfg.i2s_cfg.word_length = HDMI_AUDIO_I2S_CHST_WORD_16_BITS;
1513                 core_cfg.i2s_cfg.in_length_bits =
1514                         HDMI_AUDIO_I2S_INPUT_LENGTH_16;
1515                 core_cfg.i2s_cfg.justification = HDMI_AUDIO_JUSTIFY_LEFT;
1516                 audio_format.samples_per_word = HDMI_AUDIO_ONEWORD_TWOSAMPLES;
1517                 audio_format.sample_size = HDMI_AUDIO_SAMPLE_16BITS;
1518                 audio_format.justification = HDMI_AUDIO_JUSTIFY_LEFT;
1519                 audio_dma.transfer_size = 0x10;
1520                 break;
1521         case SNDRV_PCM_FORMAT_S24_LE:
1522                 core_cfg.i2s_cfg.word_max_length =
1523                         HDMI_AUDIO_I2S_MAX_WORD_24BITS;
1524                 core_cfg.i2s_cfg.word_length = HDMI_AUDIO_I2S_CHST_WORD_24_BITS;
1525                 core_cfg.i2s_cfg.in_length_bits =
1526                         HDMI_AUDIO_I2S_INPUT_LENGTH_24;
1527                 audio_format.samples_per_word = HDMI_AUDIO_ONEWORD_ONESAMPLE;
1528                 audio_format.sample_size = HDMI_AUDIO_SAMPLE_24BITS;
1529                 audio_format.justification = HDMI_AUDIO_JUSTIFY_RIGHT;
1530                 core_cfg.i2s_cfg.justification = HDMI_AUDIO_JUSTIFY_RIGHT;
1531                 audio_dma.transfer_size = 0x20;
1532                 break;
1533         default:
1534                 return -EINVAL;
1535         }
1536
1537         switch (params_rate(params)) {
1538         case 32000:
1539                 sample_freq = HDMI_AUDIO_FS_32000;
1540                 break;
1541         case 44100:
1542                 sample_freq = HDMI_AUDIO_FS_44100;
1543                 break;
1544         case 48000:
1545                 sample_freq = HDMI_AUDIO_FS_48000;
1546                 break;
1547         default:
1548                 return -EINVAL;
1549         }
1550
1551         err = hdmi_config_audio_acr(params_rate(params), &n, &cts);
1552         if (err < 0)
1553                 return err;
1554
1555         /* Audio wrapper config */
1556         audio_format.stereo_channels = HDMI_AUDIO_STEREO_ONECHANNEL;
1557         audio_format.active_chnnls_msk = 0x03;
1558         audio_format.type = HDMI_AUDIO_TYPE_LPCM;
1559         audio_format.sample_order = HDMI_AUDIO_SAMPLE_LEFT_FIRST;
1560         /* Disable start/stop signals of IEC 60958 blocks */
1561         audio_format.en_sig_blk_strt_end = HDMI_AUDIO_BLOCK_SIG_STARTEND_OFF;
1562
1563         audio_dma.block_size = 0xC0;
1564         audio_dma.mode = HDMI_AUDIO_TRANSF_DMA;
1565         audio_dma.fifo_threshold = 0x20; /* in number of samples */
1566
1567         hdmi_wp_audio_config_dma(&audio_dma);
1568         hdmi_wp_audio_config_format(&audio_format);
1569
1570         /*
1571          * I2S config
1572          */
1573         core_cfg.i2s_cfg.en_high_bitrate_aud = false;
1574         /* Only used with high bitrate audio */
1575         core_cfg.i2s_cfg.cbit_order = false;
1576         /* Serial data and word select should change on sck rising edge */
1577         core_cfg.i2s_cfg.sck_edge_mode = HDMI_AUDIO_I2S_SCK_EDGE_RISING;
1578         core_cfg.i2s_cfg.vbit = HDMI_AUDIO_I2S_VBIT_FOR_PCM;
1579         /* Set I2S word select polarity */
1580         core_cfg.i2s_cfg.ws_polarity = HDMI_AUDIO_I2S_WS_POLARITY_LOW_IS_LEFT;
1581         core_cfg.i2s_cfg.direction = HDMI_AUDIO_I2S_MSB_SHIFTED_FIRST;
1582         /* Set serial data to word select shift. See Phillips spec. */
1583         core_cfg.i2s_cfg.shift = HDMI_AUDIO_I2S_FIRST_BIT_SHIFT;
1584         /* Enable one of the four available serial data channels */
1585         core_cfg.i2s_cfg.active_sds = HDMI_AUDIO_I2S_SD0_EN;
1586
1587         /* Core audio config */
1588         core_cfg.freq_sample = sample_freq;
1589         core_cfg.n = n;
1590         core_cfg.cts = cts;
1591         if (dss_has_feature(FEAT_HDMI_CTS_SWMODE)) {
1592                 core_cfg.aud_par_busclk = 0;
1593                 core_cfg.cts_mode = HDMI_AUDIO_CTS_MODE_SW;
1594                 core_cfg.use_mclk = false;
1595         } else {
1596                 core_cfg.aud_par_busclk = (((128 * 31) - 1) << 8);
1597                 core_cfg.cts_mode = HDMI_AUDIO_CTS_MODE_HW;
1598                 core_cfg.use_mclk = true;
1599                 core_cfg.mclk_mode = HDMI_AUDIO_MCLK_128FS;
1600         }
1601         core_cfg.layout = HDMI_AUDIO_LAYOUT_2CH;
1602         core_cfg.en_spdif = false;
1603         /* Use sample frequency from channel status word */
1604         core_cfg.fs_override = true;
1605         /* Enable ACR packets */
1606         core_cfg.en_acr_pkt = true;
1607         /* Disable direct streaming digital audio */
1608         core_cfg.en_dsd_audio = false;
1609         /* Use parallel audio interface */
1610         core_cfg.en_parallel_aud_input = true;
1611
1612         hdmi_core_audio_config(&core_cfg);
1613
1614         /*
1615          * Configure packet
1616          * info frame audio see doc CEA861-D page 74
1617          */
1618         aud_if_cfg.db1_coding_type = HDMI_INFOFRAME_AUDIO_DB1CT_FROM_STREAM;
1619         aud_if_cfg.db1_channel_count = 2;
1620         aud_if_cfg.db2_sample_freq = HDMI_INFOFRAME_AUDIO_DB2SF_FROM_STREAM;
1621         aud_if_cfg.db2_sample_size = HDMI_INFOFRAME_AUDIO_DB2SS_FROM_STREAM;
1622         aud_if_cfg.db4_channel_alloc = 0x00;
1623         aud_if_cfg.db5_downmix_inh = false;
1624         aud_if_cfg.db5_lsv = 0;
1625
1626         hdmi_core_audio_infoframe_config(&aud_if_cfg);
1627         return 0;
1628 }
1629
1630 static int hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
1631                                   struct snd_soc_dai *dai)
1632 {
1633         int err = 0;
1634         switch (cmd) {
1635         case SNDRV_PCM_TRIGGER_START:
1636         case SNDRV_PCM_TRIGGER_RESUME:
1637         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1638                 REG_FLD_MOD(HDMI_CORE_AV_AUD_MODE, 1, 0, 0);
1639                 REG_FLD_MOD(HDMI_WP_AUDIO_CTRL, 1, 31, 31);
1640                 REG_FLD_MOD(HDMI_WP_AUDIO_CTRL, 1, 30, 30);
1641                 break;
1642
1643         case SNDRV_PCM_TRIGGER_STOP:
1644         case SNDRV_PCM_TRIGGER_SUSPEND:
1645         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1646                 REG_FLD_MOD(HDMI_CORE_AV_AUD_MODE, 0, 0, 0);
1647                 REG_FLD_MOD(HDMI_WP_AUDIO_CTRL, 0, 30, 30);
1648                 REG_FLD_MOD(HDMI_WP_AUDIO_CTRL, 0, 31, 31);
1649                 break;
1650         default:
1651                 err = -EINVAL;
1652         }
1653         return err;
1654 }
1655
1656 static int hdmi_audio_startup(struct snd_pcm_substream *substream,
1657                                   struct snd_soc_dai *dai)
1658 {
1659         if (!hdmi.mode) {
1660                 pr_err("Current video settings do not support audio.\n");
1661                 return -EIO;
1662         }
1663         return 0;
1664 }
1665
1666 static struct snd_soc_codec_driver hdmi_audio_codec_drv = {
1667 };
1668
1669 static struct snd_soc_dai_ops hdmi_audio_codec_ops = {
1670         .hw_params = hdmi_audio_hw_params,
1671         .trigger = hdmi_audio_trigger,
1672         .startup = hdmi_audio_startup,
1673 };
1674
1675 static struct snd_soc_dai_driver hdmi_codec_dai_drv = {
1676                 .name = "hdmi-audio-codec",
1677                 .playback = {
1678                         .channels_min = 2,
1679                         .channels_max = 2,
1680                         .rates = SNDRV_PCM_RATE_32000 |
1681                                 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
1682                         .formats = SNDRV_PCM_FMTBIT_S16_LE |
1683                                 SNDRV_PCM_FMTBIT_S24_LE,
1684                 },
1685                 .ops = &hdmi_audio_codec_ops,
1686 };
1687 #endif
1688
1689 static int hdmi_get_clocks(struct platform_device *pdev)
1690 {
1691         struct clk *clk;
1692
1693         clk = clk_get(&pdev->dev, "sys_clk");
1694         if (IS_ERR(clk)) {
1695                 DSSERR("can't get sys_clk\n");
1696                 return PTR_ERR(clk);
1697         }
1698
1699         hdmi.sys_clk = clk;
1700
1701         clk = clk_get(&pdev->dev, "fck");
1702         if (IS_ERR(clk)) {
1703                 DSSERR("can't get fck\n");
1704                 clk_put(hdmi.sys_clk);
1705                 return PTR_ERR(clk);
1706         }
1707
1708         hdmi.hdmi_clk = clk;
1709
1710         return 0;
1711 }
1712
1713 static void hdmi_put_clocks(void)
1714 {
1715         if (hdmi.sys_clk)
1716                 clk_put(hdmi.sys_clk);
1717         if (hdmi.hdmi_clk)
1718                 clk_put(hdmi.hdmi_clk);
1719 }
1720
1721 /* HDMI HW IP initialisation */
1722 static int omapdss_hdmihw_probe(struct platform_device *pdev)
1723 {
1724         struct resource *hdmi_mem;
1725         int r;
1726
1727         hdmi.pdata = pdev->dev.platform_data;
1728         hdmi.pdev = pdev;
1729
1730         mutex_init(&hdmi.lock);
1731
1732         hdmi_mem = platform_get_resource(hdmi.pdev, IORESOURCE_MEM, 0);
1733         if (!hdmi_mem) {
1734                 DSSERR("can't get IORESOURCE_MEM HDMI\n");
1735                 return -EINVAL;
1736         }
1737
1738         /* Base address taken from platform */
1739         hdmi.base_wp = ioremap(hdmi_mem->start, resource_size(hdmi_mem));
1740         if (!hdmi.base_wp) {
1741                 DSSERR("can't ioremap WP\n");
1742                 return -ENOMEM;
1743         }
1744
1745         r = hdmi_get_clocks(pdev);
1746         if (r) {
1747                 iounmap(hdmi.base_wp);
1748                 return r;
1749         }
1750
1751         pm_runtime_enable(&pdev->dev);
1752
1753         hdmi_panel_init();
1754
1755 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
1756         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
1757
1758         /* Register ASoC codec DAI */
1759         r = snd_soc_register_codec(&pdev->dev, &hdmi_audio_codec_drv,
1760                                         &hdmi_codec_dai_drv, 1);
1761         if (r) {
1762                 DSSERR("can't register ASoC HDMI audio codec\n");
1763                 return r;
1764         }
1765 #endif
1766         return 0;
1767 }
1768
1769 static int omapdss_hdmihw_remove(struct platform_device *pdev)
1770 {
1771         hdmi_panel_exit();
1772
1773 #if defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI) || \
1774         defined(CONFIG_SND_OMAP_SOC_OMAP4_HDMI_MODULE)
1775         snd_soc_unregister_codec(&pdev->dev);
1776 #endif
1777
1778         pm_runtime_disable(&pdev->dev);
1779
1780         hdmi_put_clocks();
1781
1782         iounmap(hdmi.base_wp);
1783
1784         return 0;
1785 }
1786
1787 static int hdmi_runtime_suspend(struct device *dev)
1788 {
1789         clk_disable(hdmi.hdmi_clk);
1790         clk_disable(hdmi.sys_clk);
1791
1792         dispc_runtime_put();
1793         dss_runtime_put();
1794
1795         return 0;
1796 }
1797
1798 static int hdmi_runtime_resume(struct device *dev)
1799 {
1800         int r;
1801
1802         r = dss_runtime_get();
1803         if (r < 0)
1804                 goto err_get_dss;
1805
1806         r = dispc_runtime_get();
1807         if (r < 0)
1808                 goto err_get_dispc;
1809
1810
1811         clk_enable(hdmi.sys_clk);
1812         clk_enable(hdmi.hdmi_clk);
1813
1814         return 0;
1815
1816 err_get_dispc:
1817         dss_runtime_put();
1818 err_get_dss:
1819         return r;
1820 }
1821
1822 static const struct dev_pm_ops hdmi_pm_ops = {
1823         .runtime_suspend = hdmi_runtime_suspend,
1824         .runtime_resume = hdmi_runtime_resume,
1825 };
1826
1827 static struct platform_driver omapdss_hdmihw_driver = {
1828         .probe          = omapdss_hdmihw_probe,
1829         .remove         = omapdss_hdmihw_remove,
1830         .driver         = {
1831                 .name   = "omapdss_hdmi",
1832                 .owner  = THIS_MODULE,
1833                 .pm     = &hdmi_pm_ops,
1834         },
1835 };
1836
1837 int hdmi_init_platform_driver(void)
1838 {
1839         return platform_driver_register(&omapdss_hdmihw_driver);
1840 }
1841
1842 void hdmi_uninit_platform_driver(void)
1843 {
1844         return platform_driver_unregister(&omapdss_hdmihw_driver);
1845 }