OMAP2PLUS: DSS2: Generalize naming of PRCM related clock enums in DSS driver
[pandora-kernel.git] / drivers / video / omap2 / dss / dsi.c
1 /*
2  * linux/drivers/video/omap2/dss/dsi.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #define DSS_SUBSYS_NAME "DSI"
21
22 #include <linux/kernel.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
25 #include <linux/device.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/mutex.h>
30 #include <linux/semaphore.h>
31 #include <linux/seq_file.h>
32 #include <linux/platform_device.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/wait.h>
35 #include <linux/workqueue.h>
36
37 #include <plat/display.h>
38 #include <plat/clock.h>
39
40 #include "dss.h"
41
42 /*#define VERBOSE_IRQ*/
43 #define DSI_CATCH_MISSING_TE
44
45 struct dsi_reg { u16 idx; };
46
47 #define DSI_REG(idx)            ((const struct dsi_reg) { idx })
48
49 #define DSI_SZ_REGS             SZ_1K
50 /* DSI Protocol Engine */
51
52 #define DSI_REVISION                    DSI_REG(0x0000)
53 #define DSI_SYSCONFIG                   DSI_REG(0x0010)
54 #define DSI_SYSSTATUS                   DSI_REG(0x0014)
55 #define DSI_IRQSTATUS                   DSI_REG(0x0018)
56 #define DSI_IRQENABLE                   DSI_REG(0x001C)
57 #define DSI_CTRL                        DSI_REG(0x0040)
58 #define DSI_COMPLEXIO_CFG1              DSI_REG(0x0048)
59 #define DSI_COMPLEXIO_IRQ_STATUS        DSI_REG(0x004C)
60 #define DSI_COMPLEXIO_IRQ_ENABLE        DSI_REG(0x0050)
61 #define DSI_CLK_CTRL                    DSI_REG(0x0054)
62 #define DSI_TIMING1                     DSI_REG(0x0058)
63 #define DSI_TIMING2                     DSI_REG(0x005C)
64 #define DSI_VM_TIMING1                  DSI_REG(0x0060)
65 #define DSI_VM_TIMING2                  DSI_REG(0x0064)
66 #define DSI_VM_TIMING3                  DSI_REG(0x0068)
67 #define DSI_CLK_TIMING                  DSI_REG(0x006C)
68 #define DSI_TX_FIFO_VC_SIZE             DSI_REG(0x0070)
69 #define DSI_RX_FIFO_VC_SIZE             DSI_REG(0x0074)
70 #define DSI_COMPLEXIO_CFG2              DSI_REG(0x0078)
71 #define DSI_RX_FIFO_VC_FULLNESS         DSI_REG(0x007C)
72 #define DSI_VM_TIMING4                  DSI_REG(0x0080)
73 #define DSI_TX_FIFO_VC_EMPTINESS        DSI_REG(0x0084)
74 #define DSI_VM_TIMING5                  DSI_REG(0x0088)
75 #define DSI_VM_TIMING6                  DSI_REG(0x008C)
76 #define DSI_VM_TIMING7                  DSI_REG(0x0090)
77 #define DSI_STOPCLK_TIMING              DSI_REG(0x0094)
78 #define DSI_VC_CTRL(n)                  DSI_REG(0x0100 + (n * 0x20))
79 #define DSI_VC_TE(n)                    DSI_REG(0x0104 + (n * 0x20))
80 #define DSI_VC_LONG_PACKET_HEADER(n)    DSI_REG(0x0108 + (n * 0x20))
81 #define DSI_VC_LONG_PACKET_PAYLOAD(n)   DSI_REG(0x010C + (n * 0x20))
82 #define DSI_VC_SHORT_PACKET_HEADER(n)   DSI_REG(0x0110 + (n * 0x20))
83 #define DSI_VC_IRQSTATUS(n)             DSI_REG(0x0118 + (n * 0x20))
84 #define DSI_VC_IRQENABLE(n)             DSI_REG(0x011C + (n * 0x20))
85
86 /* DSIPHY_SCP */
87
88 #define DSI_DSIPHY_CFG0                 DSI_REG(0x200 + 0x0000)
89 #define DSI_DSIPHY_CFG1                 DSI_REG(0x200 + 0x0004)
90 #define DSI_DSIPHY_CFG2                 DSI_REG(0x200 + 0x0008)
91 #define DSI_DSIPHY_CFG5                 DSI_REG(0x200 + 0x0014)
92
93 /* DSI_PLL_CTRL_SCP */
94
95 #define DSI_PLL_CONTROL                 DSI_REG(0x300 + 0x0000)
96 #define DSI_PLL_STATUS                  DSI_REG(0x300 + 0x0004)
97 #define DSI_PLL_GO                      DSI_REG(0x300 + 0x0008)
98 #define DSI_PLL_CONFIGURATION1          DSI_REG(0x300 + 0x000C)
99 #define DSI_PLL_CONFIGURATION2          DSI_REG(0x300 + 0x0010)
100
101 #define REG_GET(idx, start, end) \
102         FLD_GET(dsi_read_reg(idx), start, end)
103
104 #define REG_FLD_MOD(idx, val, start, end) \
105         dsi_write_reg(idx, FLD_MOD(dsi_read_reg(idx), val, start, end))
106
107 /* Global interrupts */
108 #define DSI_IRQ_VC0             (1 << 0)
109 #define DSI_IRQ_VC1             (1 << 1)
110 #define DSI_IRQ_VC2             (1 << 2)
111 #define DSI_IRQ_VC3             (1 << 3)
112 #define DSI_IRQ_WAKEUP          (1 << 4)
113 #define DSI_IRQ_RESYNC          (1 << 5)
114 #define DSI_IRQ_PLL_LOCK        (1 << 7)
115 #define DSI_IRQ_PLL_UNLOCK      (1 << 8)
116 #define DSI_IRQ_PLL_RECALL      (1 << 9)
117 #define DSI_IRQ_COMPLEXIO_ERR   (1 << 10)
118 #define DSI_IRQ_HS_TX_TIMEOUT   (1 << 14)
119 #define DSI_IRQ_LP_RX_TIMEOUT   (1 << 15)
120 #define DSI_IRQ_TE_TRIGGER      (1 << 16)
121 #define DSI_IRQ_ACK_TRIGGER     (1 << 17)
122 #define DSI_IRQ_SYNC_LOST       (1 << 18)
123 #define DSI_IRQ_LDO_POWER_GOOD  (1 << 19)
124 #define DSI_IRQ_TA_TIMEOUT      (1 << 20)
125 #define DSI_IRQ_ERROR_MASK \
126         (DSI_IRQ_HS_TX_TIMEOUT | DSI_IRQ_LP_RX_TIMEOUT | DSI_IRQ_SYNC_LOST | \
127         DSI_IRQ_TA_TIMEOUT)
128 #define DSI_IRQ_CHANNEL_MASK    0xf
129
130 /* Virtual channel interrupts */
131 #define DSI_VC_IRQ_CS           (1 << 0)
132 #define DSI_VC_IRQ_ECC_CORR     (1 << 1)
133 #define DSI_VC_IRQ_PACKET_SENT  (1 << 2)
134 #define DSI_VC_IRQ_FIFO_TX_OVF  (1 << 3)
135 #define DSI_VC_IRQ_FIFO_RX_OVF  (1 << 4)
136 #define DSI_VC_IRQ_BTA          (1 << 5)
137 #define DSI_VC_IRQ_ECC_NO_CORR  (1 << 6)
138 #define DSI_VC_IRQ_FIFO_TX_UDF  (1 << 7)
139 #define DSI_VC_IRQ_PP_BUSY_CHANGE (1 << 8)
140 #define DSI_VC_IRQ_ERROR_MASK \
141         (DSI_VC_IRQ_CS | DSI_VC_IRQ_ECC_CORR | DSI_VC_IRQ_FIFO_TX_OVF | \
142         DSI_VC_IRQ_FIFO_RX_OVF | DSI_VC_IRQ_ECC_NO_CORR | \
143         DSI_VC_IRQ_FIFO_TX_UDF)
144
145 /* ComplexIO interrupts */
146 #define DSI_CIO_IRQ_ERRSYNCESC1         (1 << 0)
147 #define DSI_CIO_IRQ_ERRSYNCESC2         (1 << 1)
148 #define DSI_CIO_IRQ_ERRSYNCESC3         (1 << 2)
149 #define DSI_CIO_IRQ_ERRESC1             (1 << 5)
150 #define DSI_CIO_IRQ_ERRESC2             (1 << 6)
151 #define DSI_CIO_IRQ_ERRESC3             (1 << 7)
152 #define DSI_CIO_IRQ_ERRCONTROL1         (1 << 10)
153 #define DSI_CIO_IRQ_ERRCONTROL2         (1 << 11)
154 #define DSI_CIO_IRQ_ERRCONTROL3         (1 << 12)
155 #define DSI_CIO_IRQ_STATEULPS1          (1 << 15)
156 #define DSI_CIO_IRQ_STATEULPS2          (1 << 16)
157 #define DSI_CIO_IRQ_STATEULPS3          (1 << 17)
158 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_1  (1 << 20)
159 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_1  (1 << 21)
160 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_2  (1 << 22)
161 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_2  (1 << 23)
162 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_3  (1 << 24)
163 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_3  (1 << 25)
164 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL0  (1 << 30)
165 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL1  (1 << 31)
166 #define DSI_CIO_IRQ_ERROR_MASK \
167         (DSI_CIO_IRQ_ERRSYNCESC1 | DSI_CIO_IRQ_ERRSYNCESC2 | \
168          DSI_CIO_IRQ_ERRSYNCESC3 | DSI_CIO_IRQ_ERRESC1 | DSI_CIO_IRQ_ERRESC2 | \
169          DSI_CIO_IRQ_ERRESC3 | DSI_CIO_IRQ_ERRCONTROL1 | \
170          DSI_CIO_IRQ_ERRCONTROL2 | DSI_CIO_IRQ_ERRCONTROL3 | \
171          DSI_CIO_IRQ_ERRCONTENTIONLP0_1 | DSI_CIO_IRQ_ERRCONTENTIONLP1_1 | \
172          DSI_CIO_IRQ_ERRCONTENTIONLP0_2 | DSI_CIO_IRQ_ERRCONTENTIONLP1_2 | \
173          DSI_CIO_IRQ_ERRCONTENTIONLP0_3 | DSI_CIO_IRQ_ERRCONTENTIONLP1_3)
174
175 #define DSI_DT_DCS_SHORT_WRITE_0        0x05
176 #define DSI_DT_DCS_SHORT_WRITE_1        0x15
177 #define DSI_DT_DCS_READ                 0x06
178 #define DSI_DT_SET_MAX_RET_PKG_SIZE     0x37
179 #define DSI_DT_NULL_PACKET              0x09
180 #define DSI_DT_DCS_LONG_WRITE           0x39
181
182 #define DSI_DT_RX_ACK_WITH_ERR          0x02
183 #define DSI_DT_RX_DCS_LONG_READ         0x1c
184 #define DSI_DT_RX_SHORT_READ_1          0x21
185 #define DSI_DT_RX_SHORT_READ_2          0x22
186
187 #define FINT_MAX 2100000
188 #define FINT_MIN 750000
189 #define REGN_MAX (1 << 7)
190 #define REGM_MAX ((1 << 11) - 1)
191 #define REGM3_MAX (1 << 4)
192 #define REGM4_MAX (1 << 4)
193 #define LP_DIV_MAX ((1 << 13) - 1)
194
195 enum fifo_size {
196         DSI_FIFO_SIZE_0         = 0,
197         DSI_FIFO_SIZE_32        = 1,
198         DSI_FIFO_SIZE_64        = 2,
199         DSI_FIFO_SIZE_96        = 3,
200         DSI_FIFO_SIZE_128       = 4,
201 };
202
203 enum dsi_vc_mode {
204         DSI_VC_MODE_L4 = 0,
205         DSI_VC_MODE_VP,
206 };
207
208 struct dsi_update_region {
209         u16 x, y, w, h;
210         struct omap_dss_device *device;
211 };
212
213 struct dsi_irq_stats {
214         unsigned long last_reset;
215         unsigned irq_count;
216         unsigned dsi_irqs[32];
217         unsigned vc_irqs[4][32];
218         unsigned cio_irqs[32];
219 };
220
221 static struct
222 {
223         struct platform_device *pdev;
224         void __iomem    *base;
225
226         struct dsi_clock_info current_cinfo;
227
228         struct regulator *vdds_dsi_reg;
229
230         struct {
231                 enum dsi_vc_mode mode;
232                 struct omap_dss_device *dssdev;
233                 enum fifo_size fifo_size;
234         } vc[4];
235
236         struct mutex lock;
237         struct semaphore bus_lock;
238
239         unsigned pll_locked;
240
241         struct completion bta_completion;
242         void (*bta_callback)(void);
243
244         int update_channel;
245         struct dsi_update_region update_region;
246
247         bool te_enabled;
248
249         struct workqueue_struct *workqueue;
250
251         void (*framedone_callback)(int, void *);
252         void *framedone_data;
253
254         struct delayed_work framedone_timeout_work;
255
256 #ifdef DSI_CATCH_MISSING_TE
257         struct timer_list te_timer;
258 #endif
259
260         unsigned long cache_req_pck;
261         unsigned long cache_clk_freq;
262         struct dsi_clock_info cache_cinfo;
263
264         u32             errors;
265         spinlock_t      errors_lock;
266 #ifdef DEBUG
267         ktime_t perf_setup_time;
268         ktime_t perf_start_time;
269 #endif
270         int debug_read;
271         int debug_write;
272
273 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
274         spinlock_t irq_stats_lock;
275         struct dsi_irq_stats irq_stats;
276 #endif
277 } dsi;
278
279 #ifdef DEBUG
280 static unsigned int dsi_perf;
281 module_param_named(dsi_perf, dsi_perf, bool, 0644);
282 #endif
283
284 static inline void dsi_write_reg(const struct dsi_reg idx, u32 val)
285 {
286         __raw_writel(val, dsi.base + idx.idx);
287 }
288
289 static inline u32 dsi_read_reg(const struct dsi_reg idx)
290 {
291         return __raw_readl(dsi.base + idx.idx);
292 }
293
294 static struct regulator *dsi_get_vdds_dsi(void)
295 {
296         struct regulator *reg;
297
298         if (dsi.vdds_dsi_reg != NULL)
299                 return dsi.vdds_dsi_reg;
300
301         reg = regulator_get(&dsi.pdev->dev, "vdds_dsi");
302         if (!IS_ERR(reg))
303                 dsi.vdds_dsi_reg = reg;
304
305         return reg;
306 }
307
308
309 void dsi_save_context(void)
310 {
311 }
312
313 void dsi_restore_context(void)
314 {
315 }
316
317 void dsi_bus_lock(void)
318 {
319         down(&dsi.bus_lock);
320 }
321 EXPORT_SYMBOL(dsi_bus_lock);
322
323 void dsi_bus_unlock(void)
324 {
325         up(&dsi.bus_lock);
326 }
327 EXPORT_SYMBOL(dsi_bus_unlock);
328
329 static bool dsi_bus_is_locked(void)
330 {
331         return dsi.bus_lock.count == 0;
332 }
333
334 static inline int wait_for_bit_change(const struct dsi_reg idx, int bitnum,
335                 int value)
336 {
337         int t = 100000;
338
339         while (REG_GET(idx, bitnum, bitnum) != value) {
340                 if (--t == 0)
341                         return !value;
342         }
343
344         return value;
345 }
346
347 #ifdef DEBUG
348 static void dsi_perf_mark_setup(void)
349 {
350         dsi.perf_setup_time = ktime_get();
351 }
352
353 static void dsi_perf_mark_start(void)
354 {
355         dsi.perf_start_time = ktime_get();
356 }
357
358 static void dsi_perf_show(const char *name)
359 {
360         ktime_t t, setup_time, trans_time;
361         u32 total_bytes;
362         u32 setup_us, trans_us, total_us;
363
364         if (!dsi_perf)
365                 return;
366
367         t = ktime_get();
368
369         setup_time = ktime_sub(dsi.perf_start_time, dsi.perf_setup_time);
370         setup_us = (u32)ktime_to_us(setup_time);
371         if (setup_us == 0)
372                 setup_us = 1;
373
374         trans_time = ktime_sub(t, dsi.perf_start_time);
375         trans_us = (u32)ktime_to_us(trans_time);
376         if (trans_us == 0)
377                 trans_us = 1;
378
379         total_us = setup_us + trans_us;
380
381         total_bytes = dsi.update_region.w *
382                 dsi.update_region.h *
383                 dsi.update_region.device->ctrl.pixel_size / 8;
384
385         printk(KERN_INFO "DSI(%s): %u us + %u us = %u us (%uHz), "
386                         "%u bytes, %u kbytes/sec\n",
387                         name,
388                         setup_us,
389                         trans_us,
390                         total_us,
391                         1000*1000 / total_us,
392                         total_bytes,
393                         total_bytes * 1000 / total_us);
394 }
395 #else
396 #define dsi_perf_mark_setup()
397 #define dsi_perf_mark_start()
398 #define dsi_perf_show(x)
399 #endif
400
401 static void print_irq_status(u32 status)
402 {
403 #ifndef VERBOSE_IRQ
404         if ((status & ~DSI_IRQ_CHANNEL_MASK) == 0)
405                 return;
406 #endif
407         printk(KERN_DEBUG "DSI IRQ: 0x%x: ", status);
408
409 #define PIS(x) \
410         if (status & DSI_IRQ_##x) \
411                 printk(#x " ");
412 #ifdef VERBOSE_IRQ
413         PIS(VC0);
414         PIS(VC1);
415         PIS(VC2);
416         PIS(VC3);
417 #endif
418         PIS(WAKEUP);
419         PIS(RESYNC);
420         PIS(PLL_LOCK);
421         PIS(PLL_UNLOCK);
422         PIS(PLL_RECALL);
423         PIS(COMPLEXIO_ERR);
424         PIS(HS_TX_TIMEOUT);
425         PIS(LP_RX_TIMEOUT);
426         PIS(TE_TRIGGER);
427         PIS(ACK_TRIGGER);
428         PIS(SYNC_LOST);
429         PIS(LDO_POWER_GOOD);
430         PIS(TA_TIMEOUT);
431 #undef PIS
432
433         printk("\n");
434 }
435
436 static void print_irq_status_vc(int channel, u32 status)
437 {
438 #ifndef VERBOSE_IRQ
439         if ((status & ~DSI_VC_IRQ_PACKET_SENT) == 0)
440                 return;
441 #endif
442         printk(KERN_DEBUG "DSI VC(%d) IRQ 0x%x: ", channel, status);
443
444 #define PIS(x) \
445         if (status & DSI_VC_IRQ_##x) \
446                 printk(#x " ");
447         PIS(CS);
448         PIS(ECC_CORR);
449 #ifdef VERBOSE_IRQ
450         PIS(PACKET_SENT);
451 #endif
452         PIS(FIFO_TX_OVF);
453         PIS(FIFO_RX_OVF);
454         PIS(BTA);
455         PIS(ECC_NO_CORR);
456         PIS(FIFO_TX_UDF);
457         PIS(PP_BUSY_CHANGE);
458 #undef PIS
459         printk("\n");
460 }
461
462 static void print_irq_status_cio(u32 status)
463 {
464         printk(KERN_DEBUG "DSI CIO IRQ 0x%x: ", status);
465
466 #define PIS(x) \
467         if (status & DSI_CIO_IRQ_##x) \
468                 printk(#x " ");
469         PIS(ERRSYNCESC1);
470         PIS(ERRSYNCESC2);
471         PIS(ERRSYNCESC3);
472         PIS(ERRESC1);
473         PIS(ERRESC2);
474         PIS(ERRESC3);
475         PIS(ERRCONTROL1);
476         PIS(ERRCONTROL2);
477         PIS(ERRCONTROL3);
478         PIS(STATEULPS1);
479         PIS(STATEULPS2);
480         PIS(STATEULPS3);
481         PIS(ERRCONTENTIONLP0_1);
482         PIS(ERRCONTENTIONLP1_1);
483         PIS(ERRCONTENTIONLP0_2);
484         PIS(ERRCONTENTIONLP1_2);
485         PIS(ERRCONTENTIONLP0_3);
486         PIS(ERRCONTENTIONLP1_3);
487         PIS(ULPSACTIVENOT_ALL0);
488         PIS(ULPSACTIVENOT_ALL1);
489 #undef PIS
490
491         printk("\n");
492 }
493
494 static int debug_irq;
495
496 /* called from dss */
497 void dsi_irq_handler(void)
498 {
499         u32 irqstatus, vcstatus, ciostatus;
500         int i;
501
502         irqstatus = dsi_read_reg(DSI_IRQSTATUS);
503
504 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
505         spin_lock(&dsi.irq_stats_lock);
506         dsi.irq_stats.irq_count++;
507         dss_collect_irq_stats(irqstatus, dsi.irq_stats.dsi_irqs);
508 #endif
509
510         if (irqstatus & DSI_IRQ_ERROR_MASK) {
511                 DSSERR("DSI error, irqstatus %x\n", irqstatus);
512                 print_irq_status(irqstatus);
513                 spin_lock(&dsi.errors_lock);
514                 dsi.errors |= irqstatus & DSI_IRQ_ERROR_MASK;
515                 spin_unlock(&dsi.errors_lock);
516         } else if (debug_irq) {
517                 print_irq_status(irqstatus);
518         }
519
520 #ifdef DSI_CATCH_MISSING_TE
521         if (irqstatus & DSI_IRQ_TE_TRIGGER)
522                 del_timer(&dsi.te_timer);
523 #endif
524
525         for (i = 0; i < 4; ++i) {
526                 if ((irqstatus & (1<<i)) == 0)
527                         continue;
528
529                 vcstatus = dsi_read_reg(DSI_VC_IRQSTATUS(i));
530
531 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
532                 dss_collect_irq_stats(vcstatus, dsi.irq_stats.vc_irqs[i]);
533 #endif
534
535                 if (vcstatus & DSI_VC_IRQ_BTA) {
536                         complete(&dsi.bta_completion);
537
538                         if (dsi.bta_callback)
539                                 dsi.bta_callback();
540                 }
541
542                 if (vcstatus & DSI_VC_IRQ_ERROR_MASK) {
543                         DSSERR("DSI VC(%d) error, vc irqstatus %x\n",
544                                        i, vcstatus);
545                         print_irq_status_vc(i, vcstatus);
546                 } else if (debug_irq) {
547                         print_irq_status_vc(i, vcstatus);
548                 }
549
550                 dsi_write_reg(DSI_VC_IRQSTATUS(i), vcstatus);
551                 /* flush posted write */
552                 dsi_read_reg(DSI_VC_IRQSTATUS(i));
553         }
554
555         if (irqstatus & DSI_IRQ_COMPLEXIO_ERR) {
556                 ciostatus = dsi_read_reg(DSI_COMPLEXIO_IRQ_STATUS);
557
558 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
559                 dss_collect_irq_stats(ciostatus, dsi.irq_stats.cio_irqs);
560 #endif
561
562                 dsi_write_reg(DSI_COMPLEXIO_IRQ_STATUS, ciostatus);
563                 /* flush posted write */
564                 dsi_read_reg(DSI_COMPLEXIO_IRQ_STATUS);
565
566                 if (ciostatus & DSI_CIO_IRQ_ERROR_MASK) {
567                         DSSERR("DSI CIO error, cio irqstatus %x\n", ciostatus);
568                         print_irq_status_cio(ciostatus);
569                 } else if (debug_irq) {
570                         print_irq_status_cio(ciostatus);
571                 }
572         }
573
574         dsi_write_reg(DSI_IRQSTATUS, irqstatus & ~DSI_IRQ_CHANNEL_MASK);
575         /* flush posted write */
576         dsi_read_reg(DSI_IRQSTATUS);
577
578 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
579         spin_unlock(&dsi.irq_stats_lock);
580 #endif
581 }
582
583
584 static void _dsi_initialize_irq(void)
585 {
586         u32 l;
587         int i;
588
589         /* disable all interrupts */
590         dsi_write_reg(DSI_IRQENABLE, 0);
591         for (i = 0; i < 4; ++i)
592                 dsi_write_reg(DSI_VC_IRQENABLE(i), 0);
593         dsi_write_reg(DSI_COMPLEXIO_IRQ_ENABLE, 0);
594
595         /* clear interrupt status */
596         l = dsi_read_reg(DSI_IRQSTATUS);
597         dsi_write_reg(DSI_IRQSTATUS, l & ~DSI_IRQ_CHANNEL_MASK);
598
599         for (i = 0; i < 4; ++i) {
600                 l = dsi_read_reg(DSI_VC_IRQSTATUS(i));
601                 dsi_write_reg(DSI_VC_IRQSTATUS(i), l);
602         }
603
604         l = dsi_read_reg(DSI_COMPLEXIO_IRQ_STATUS);
605         dsi_write_reg(DSI_COMPLEXIO_IRQ_STATUS, l);
606
607         /* enable error irqs */
608         l = DSI_IRQ_ERROR_MASK;
609 #ifdef DSI_CATCH_MISSING_TE
610         l |= DSI_IRQ_TE_TRIGGER;
611 #endif
612         dsi_write_reg(DSI_IRQENABLE, l);
613
614         l = DSI_VC_IRQ_ERROR_MASK;
615         for (i = 0; i < 4; ++i)
616                 dsi_write_reg(DSI_VC_IRQENABLE(i), l);
617
618         l = DSI_CIO_IRQ_ERROR_MASK;
619         dsi_write_reg(DSI_COMPLEXIO_IRQ_ENABLE, l);
620 }
621
622 static u32 dsi_get_errors(void)
623 {
624         unsigned long flags;
625         u32 e;
626         spin_lock_irqsave(&dsi.errors_lock, flags);
627         e = dsi.errors;
628         dsi.errors = 0;
629         spin_unlock_irqrestore(&dsi.errors_lock, flags);
630         return e;
631 }
632
633 static void dsi_vc_enable_bta_irq(int channel)
634 {
635         u32 l;
636
637         dsi_write_reg(DSI_VC_IRQSTATUS(channel), DSI_VC_IRQ_BTA);
638
639         l = dsi_read_reg(DSI_VC_IRQENABLE(channel));
640         l |= DSI_VC_IRQ_BTA;
641         dsi_write_reg(DSI_VC_IRQENABLE(channel), l);
642 }
643
644 static void dsi_vc_disable_bta_irq(int channel)
645 {
646         u32 l;
647
648         l = dsi_read_reg(DSI_VC_IRQENABLE(channel));
649         l &= ~DSI_VC_IRQ_BTA;
650         dsi_write_reg(DSI_VC_IRQENABLE(channel), l);
651 }
652
653 /* DSI func clock. this could also be DSI2_PLL_FCLK */
654 static inline void enable_clocks(bool enable)
655 {
656         if (enable)
657                 dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK);
658         else
659                 dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK);
660 }
661
662 /* source clock for DSI PLL. this could also be PCLKFREE */
663 static inline void dsi_enable_pll_clock(bool enable)
664 {
665         if (enable)
666                 dss_clk_enable(DSS_CLK_SYSCK);
667         else
668                 dss_clk_disable(DSS_CLK_SYSCK);
669
670         if (enable && dsi.pll_locked) {
671                 if (wait_for_bit_change(DSI_PLL_STATUS, 1, 1) != 1)
672                         DSSERR("cannot lock PLL when enabling clocks\n");
673         }
674 }
675
676 #ifdef DEBUG
677 static void _dsi_print_reset_status(void)
678 {
679         u32 l;
680
681         if (!dss_debug)
682                 return;
683
684         /* A dummy read using the SCP interface to any DSIPHY register is
685          * required after DSIPHY reset to complete the reset of the DSI complex
686          * I/O. */
687         l = dsi_read_reg(DSI_DSIPHY_CFG5);
688
689         printk(KERN_DEBUG "DSI resets: ");
690
691         l = dsi_read_reg(DSI_PLL_STATUS);
692         printk("PLL (%d) ", FLD_GET(l, 0, 0));
693
694         l = dsi_read_reg(DSI_COMPLEXIO_CFG1);
695         printk("CIO (%d) ", FLD_GET(l, 29, 29));
696
697         l = dsi_read_reg(DSI_DSIPHY_CFG5);
698         printk("PHY (%x, %d, %d, %d)\n",
699                         FLD_GET(l, 28, 26),
700                         FLD_GET(l, 29, 29),
701                         FLD_GET(l, 30, 30),
702                         FLD_GET(l, 31, 31));
703 }
704 #else
705 #define _dsi_print_reset_status()
706 #endif
707
708 static inline int dsi_if_enable(bool enable)
709 {
710         DSSDBG("dsi_if_enable(%d)\n", enable);
711
712         enable = enable ? 1 : 0;
713         REG_FLD_MOD(DSI_CTRL, enable, 0, 0); /* IF_EN */
714
715         if (wait_for_bit_change(DSI_CTRL, 0, enable) != enable) {
716                         DSSERR("Failed to set dsi_if_enable to %d\n", enable);
717                         return -EIO;
718         }
719
720         return 0;
721 }
722
723 unsigned long dsi_get_dsi1_pll_rate(void)
724 {
725         return dsi.current_cinfo.dsi1_pll_fclk;
726 }
727
728 static unsigned long dsi_get_dsi2_pll_rate(void)
729 {
730         return dsi.current_cinfo.dsi2_pll_fclk;
731 }
732
733 static unsigned long dsi_get_txbyteclkhs(void)
734 {
735         return dsi.current_cinfo.clkin4ddr / 16;
736 }
737
738 static unsigned long dsi_fclk_rate(void)
739 {
740         unsigned long r;
741
742         if (dss_get_dsi_clk_source() == DSS_SRC_DSS1_ALWON_FCLK) {
743                 /* DSI FCLK source is DSS1_ALWON_FCK, which is dss1_fck */
744                 r = dss_clk_get_rate(DSS_CLK_FCK);
745         } else {
746                 /* DSI FCLK source is DSI2_PLL_FCLK */
747                 r = dsi_get_dsi2_pll_rate();
748         }
749
750         return r;
751 }
752
753 static int dsi_set_lp_clk_divisor(struct omap_dss_device *dssdev)
754 {
755         unsigned long dsi_fclk;
756         unsigned lp_clk_div;
757         unsigned long lp_clk;
758
759         lp_clk_div = dssdev->phy.dsi.div.lp_clk_div;
760
761         if (lp_clk_div == 0 || lp_clk_div > LP_DIV_MAX)
762                 return -EINVAL;
763
764         dsi_fclk = dsi_fclk_rate();
765
766         lp_clk = dsi_fclk / 2 / lp_clk_div;
767
768         DSSDBG("LP_CLK_DIV %u, LP_CLK %lu\n", lp_clk_div, lp_clk);
769         dsi.current_cinfo.lp_clk = lp_clk;
770         dsi.current_cinfo.lp_clk_div = lp_clk_div;
771
772         REG_FLD_MOD(DSI_CLK_CTRL, lp_clk_div, 12, 0);   /* LP_CLK_DIVISOR */
773
774         REG_FLD_MOD(DSI_CLK_CTRL, dsi_fclk > 30000000 ? 1 : 0,
775                         21, 21);                /* LP_RX_SYNCHRO_ENABLE */
776
777         return 0;
778 }
779
780
781 enum dsi_pll_power_state {
782         DSI_PLL_POWER_OFF       = 0x0,
783         DSI_PLL_POWER_ON_HSCLK  = 0x1,
784         DSI_PLL_POWER_ON_ALL    = 0x2,
785         DSI_PLL_POWER_ON_DIV    = 0x3,
786 };
787
788 static int dsi_pll_power(enum dsi_pll_power_state state)
789 {
790         int t = 0;
791
792         REG_FLD_MOD(DSI_CLK_CTRL, state, 31, 30);       /* PLL_PWR_CMD */
793
794         /* PLL_PWR_STATUS */
795         while (FLD_GET(dsi_read_reg(DSI_CLK_CTRL), 29, 28) != state) {
796                 if (++t > 1000) {
797                         DSSERR("Failed to set DSI PLL power mode to %d\n",
798                                         state);
799                         return -ENODEV;
800                 }
801                 udelay(1);
802         }
803
804         return 0;
805 }
806
807 /* calculate clock rates using dividers in cinfo */
808 static int dsi_calc_clock_rates(struct omap_dss_device *dssdev,
809                 struct dsi_clock_info *cinfo)
810 {
811         if (cinfo->regn == 0 || cinfo->regn > REGN_MAX)
812                 return -EINVAL;
813
814         if (cinfo->regm == 0 || cinfo->regm > REGM_MAX)
815                 return -EINVAL;
816
817         if (cinfo->regm3 > REGM3_MAX)
818                 return -EINVAL;
819
820         if (cinfo->regm4 > REGM4_MAX)
821                 return -EINVAL;
822
823         if (cinfo->use_dss2_fck) {
824                 cinfo->clkin = dss_clk_get_rate(DSS_CLK_SYSCK);
825                 /* XXX it is unclear if highfreq should be used
826                  * with DSS2_FCK source also */
827                 cinfo->highfreq = 0;
828         } else {
829                 cinfo->clkin = dispc_pclk_rate(dssdev->manager->id);
830
831                 if (cinfo->clkin < 32000000)
832                         cinfo->highfreq = 0;
833                 else
834                         cinfo->highfreq = 1;
835         }
836
837         cinfo->fint = cinfo->clkin / (cinfo->regn * (cinfo->highfreq ? 2 : 1));
838
839         if (cinfo->fint > FINT_MAX || cinfo->fint < FINT_MIN)
840                 return -EINVAL;
841
842         cinfo->clkin4ddr = 2 * cinfo->regm * cinfo->fint;
843
844         if (cinfo->clkin4ddr > 1800 * 1000 * 1000)
845                 return -EINVAL;
846
847         if (cinfo->regm3 > 0)
848                 cinfo->dsi1_pll_fclk = cinfo->clkin4ddr / cinfo->regm3;
849         else
850                 cinfo->dsi1_pll_fclk = 0;
851
852         if (cinfo->regm4 > 0)
853                 cinfo->dsi2_pll_fclk = cinfo->clkin4ddr / cinfo->regm4;
854         else
855                 cinfo->dsi2_pll_fclk = 0;
856
857         return 0;
858 }
859
860 int dsi_pll_calc_clock_div_pck(bool is_tft, unsigned long req_pck,
861                 struct dsi_clock_info *dsi_cinfo,
862                 struct dispc_clock_info *dispc_cinfo)
863 {
864         struct dsi_clock_info cur, best;
865         struct dispc_clock_info best_dispc;
866         int min_fck_per_pck;
867         int match = 0;
868         unsigned long dss_clk_fck2;
869
870         dss_clk_fck2 = dss_clk_get_rate(DSS_CLK_SYSCK);
871
872         if (req_pck == dsi.cache_req_pck &&
873                         dsi.cache_cinfo.clkin == dss_clk_fck2) {
874                 DSSDBG("DSI clock info found from cache\n");
875                 *dsi_cinfo = dsi.cache_cinfo;
876                 dispc_find_clk_divs(is_tft, req_pck, dsi_cinfo->dsi1_pll_fclk,
877                                 dispc_cinfo);
878                 return 0;
879         }
880
881         min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
882
883         if (min_fck_per_pck &&
884                 req_pck * min_fck_per_pck > DISPC_MAX_FCK) {
885                 DSSERR("Requested pixel clock not possible with the current "
886                                 "OMAP2_DSS_MIN_FCK_PER_PCK setting. Turning "
887                                 "the constraint off.\n");
888                 min_fck_per_pck = 0;
889         }
890
891         DSSDBG("dsi_pll_calc\n");
892
893 retry:
894         memset(&best, 0, sizeof(best));
895         memset(&best_dispc, 0, sizeof(best_dispc));
896
897         memset(&cur, 0, sizeof(cur));
898         cur.clkin = dss_clk_fck2;
899         cur.use_dss2_fck = 1;
900         cur.highfreq = 0;
901
902         /* no highfreq: 0.75MHz < Fint = clkin / regn < 2.1MHz */
903         /* highfreq: 0.75MHz < Fint = clkin / (2*regn) < 2.1MHz */
904         /* To reduce PLL lock time, keep Fint high (around 2 MHz) */
905         for (cur.regn = 1; cur.regn < REGN_MAX; ++cur.regn) {
906                 if (cur.highfreq == 0)
907                         cur.fint = cur.clkin / cur.regn;
908                 else
909                         cur.fint = cur.clkin / (2 * cur.regn);
910
911                 if (cur.fint > FINT_MAX || cur.fint < FINT_MIN)
912                         continue;
913
914                 /* DSIPHY(MHz) = (2 * regm / regn) * (clkin / (highfreq + 1)) */
915                 for (cur.regm = 1; cur.regm < REGM_MAX; ++cur.regm) {
916                         unsigned long a, b;
917
918                         a = 2 * cur.regm * (cur.clkin/1000);
919                         b = cur.regn * (cur.highfreq + 1);
920                         cur.clkin4ddr = a / b * 1000;
921
922                         if (cur.clkin4ddr > 1800 * 1000 * 1000)
923                                 break;
924
925                         /* DSI1_PLL_FCLK(MHz) = DSIPHY(MHz) / regm3  < 173MHz */
926                         for (cur.regm3 = 1; cur.regm3 < REGM3_MAX;
927                                         ++cur.regm3) {
928                                 struct dispc_clock_info cur_dispc;
929                                 cur.dsi1_pll_fclk = cur.clkin4ddr / cur.regm3;
930
931                                 /* this will narrow down the search a bit,
932                                  * but still give pixclocks below what was
933                                  * requested */
934                                 if (cur.dsi1_pll_fclk  < req_pck)
935                                         break;
936
937                                 if (cur.dsi1_pll_fclk > DISPC_MAX_FCK)
938                                         continue;
939
940                                 if (min_fck_per_pck &&
941                                         cur.dsi1_pll_fclk <
942                                                 req_pck * min_fck_per_pck)
943                                         continue;
944
945                                 match = 1;
946
947                                 dispc_find_clk_divs(is_tft, req_pck,
948                                                 cur.dsi1_pll_fclk,
949                                                 &cur_dispc);
950
951                                 if (abs(cur_dispc.pck - req_pck) <
952                                                 abs(best_dispc.pck - req_pck)) {
953                                         best = cur;
954                                         best_dispc = cur_dispc;
955
956                                         if (cur_dispc.pck == req_pck)
957                                                 goto found;
958                                 }
959                         }
960                 }
961         }
962 found:
963         if (!match) {
964                 if (min_fck_per_pck) {
965                         DSSERR("Could not find suitable clock settings.\n"
966                                         "Turning FCK/PCK constraint off and"
967                                         "trying again.\n");
968                         min_fck_per_pck = 0;
969                         goto retry;
970                 }
971
972                 DSSERR("Could not find suitable clock settings.\n");
973
974                 return -EINVAL;
975         }
976
977         /* DSI2_PLL_FCLK (regm4) is not used */
978         best.regm4 = 0;
979         best.dsi2_pll_fclk = 0;
980
981         if (dsi_cinfo)
982                 *dsi_cinfo = best;
983         if (dispc_cinfo)
984                 *dispc_cinfo = best_dispc;
985
986         dsi.cache_req_pck = req_pck;
987         dsi.cache_clk_freq = 0;
988         dsi.cache_cinfo = best;
989
990         return 0;
991 }
992
993 int dsi_pll_set_clock_div(struct dsi_clock_info *cinfo)
994 {
995         int r = 0;
996         u32 l;
997         int f;
998
999         DSSDBGF();
1000
1001         dsi.current_cinfo.fint = cinfo->fint;
1002         dsi.current_cinfo.clkin4ddr = cinfo->clkin4ddr;
1003         dsi.current_cinfo.dsi1_pll_fclk = cinfo->dsi1_pll_fclk;
1004         dsi.current_cinfo.dsi2_pll_fclk = cinfo->dsi2_pll_fclk;
1005
1006         dsi.current_cinfo.regn = cinfo->regn;
1007         dsi.current_cinfo.regm = cinfo->regm;
1008         dsi.current_cinfo.regm3 = cinfo->regm3;
1009         dsi.current_cinfo.regm4 = cinfo->regm4;
1010
1011         DSSDBG("DSI Fint %ld\n", cinfo->fint);
1012
1013         DSSDBG("clkin (%s) rate %ld, highfreq %d\n",
1014                         cinfo->use_dss2_fck ? "dss2_fck" : "pclkfree",
1015                         cinfo->clkin,
1016                         cinfo->highfreq);
1017
1018         /* DSIPHY == CLKIN4DDR */
1019         DSSDBG("CLKIN4DDR = 2 * %d / %d * %lu / %d = %lu\n",
1020                         cinfo->regm,
1021                         cinfo->regn,
1022                         cinfo->clkin,
1023                         cinfo->highfreq + 1,
1024                         cinfo->clkin4ddr);
1025
1026         DSSDBG("Data rate on 1 DSI lane %ld Mbps\n",
1027                         cinfo->clkin4ddr / 1000 / 1000 / 2);
1028
1029         DSSDBG("Clock lane freq %ld Hz\n", cinfo->clkin4ddr / 4);
1030
1031         DSSDBG("regm3 = %d, dsi1_pll_fclk = %lu\n",
1032                         cinfo->regm3, cinfo->dsi1_pll_fclk);
1033         DSSDBG("regm4 = %d, dsi2_pll_fclk = %lu\n",
1034                         cinfo->regm4, cinfo->dsi2_pll_fclk);
1035
1036         REG_FLD_MOD(DSI_PLL_CONTROL, 0, 0, 0); /* DSI_PLL_AUTOMODE = manual */
1037
1038         l = dsi_read_reg(DSI_PLL_CONFIGURATION1);
1039         l = FLD_MOD(l, 1, 0, 0);                /* DSI_PLL_STOPMODE */
1040         l = FLD_MOD(l, cinfo->regn - 1, 7, 1);  /* DSI_PLL_REGN */
1041         l = FLD_MOD(l, cinfo->regm, 18, 8);     /* DSI_PLL_REGM */
1042         l = FLD_MOD(l, cinfo->regm3 > 0 ? cinfo->regm3 - 1 : 0,
1043                         22, 19);                /* DSI_CLOCK_DIV */
1044         l = FLD_MOD(l, cinfo->regm4 > 0 ? cinfo->regm4 - 1 : 0,
1045                         26, 23);                /* DSIPROTO_CLOCK_DIV */
1046         dsi_write_reg(DSI_PLL_CONFIGURATION1, l);
1047
1048         BUG_ON(cinfo->fint < 750000 || cinfo->fint > 2100000);
1049         if (cinfo->fint < 1000000)
1050                 f = 0x3;
1051         else if (cinfo->fint < 1250000)
1052                 f = 0x4;
1053         else if (cinfo->fint < 1500000)
1054                 f = 0x5;
1055         else if (cinfo->fint < 1750000)
1056                 f = 0x6;
1057         else
1058                 f = 0x7;
1059
1060         l = dsi_read_reg(DSI_PLL_CONFIGURATION2);
1061         l = FLD_MOD(l, f, 4, 1);                /* DSI_PLL_FREQSEL */
1062         l = FLD_MOD(l, cinfo->use_dss2_fck ? 0 : 1,
1063                         11, 11);                /* DSI_PLL_CLKSEL */
1064         l = FLD_MOD(l, cinfo->highfreq,
1065                         12, 12);                /* DSI_PLL_HIGHFREQ */
1066         l = FLD_MOD(l, 1, 13, 13);              /* DSI_PLL_REFEN */
1067         l = FLD_MOD(l, 0, 14, 14);              /* DSIPHY_CLKINEN */
1068         l = FLD_MOD(l, 1, 20, 20);              /* DSI_HSDIVBYPASS */
1069         dsi_write_reg(DSI_PLL_CONFIGURATION2, l);
1070
1071         REG_FLD_MOD(DSI_PLL_GO, 1, 0, 0);       /* DSI_PLL_GO */
1072
1073         if (wait_for_bit_change(DSI_PLL_GO, 0, 0) != 0) {
1074                 DSSERR("dsi pll go bit not going down.\n");
1075                 r = -EIO;
1076                 goto err;
1077         }
1078
1079         if (wait_for_bit_change(DSI_PLL_STATUS, 1, 1) != 1) {
1080                 DSSERR("cannot lock PLL\n");
1081                 r = -EIO;
1082                 goto err;
1083         }
1084
1085         dsi.pll_locked = 1;
1086
1087         l = dsi_read_reg(DSI_PLL_CONFIGURATION2);
1088         l = FLD_MOD(l, 0, 0, 0);        /* DSI_PLL_IDLE */
1089         l = FLD_MOD(l, 0, 5, 5);        /* DSI_PLL_PLLLPMODE */
1090         l = FLD_MOD(l, 0, 6, 6);        /* DSI_PLL_LOWCURRSTBY */
1091         l = FLD_MOD(l, 0, 7, 7);        /* DSI_PLL_TIGHTPHASELOCK */
1092         l = FLD_MOD(l, 0, 8, 8);        /* DSI_PLL_DRIFTGUARDEN */
1093         l = FLD_MOD(l, 0, 10, 9);       /* DSI_PLL_LOCKSEL */
1094         l = FLD_MOD(l, 1, 13, 13);      /* DSI_PLL_REFEN */
1095         l = FLD_MOD(l, 1, 14, 14);      /* DSIPHY_CLKINEN */
1096         l = FLD_MOD(l, 0, 15, 15);      /* DSI_BYPASSEN */
1097         l = FLD_MOD(l, 1, 16, 16);      /* DSS_CLOCK_EN */
1098         l = FLD_MOD(l, 0, 17, 17);      /* DSS_CLOCK_PWDN */
1099         l = FLD_MOD(l, 1, 18, 18);      /* DSI_PROTO_CLOCK_EN */
1100         l = FLD_MOD(l, 0, 19, 19);      /* DSI_PROTO_CLOCK_PWDN */
1101         l = FLD_MOD(l, 0, 20, 20);      /* DSI_HSDIVBYPASS */
1102         dsi_write_reg(DSI_PLL_CONFIGURATION2, l);
1103
1104         DSSDBG("PLL config done\n");
1105 err:
1106         return r;
1107 }
1108
1109 int dsi_pll_init(struct omap_dss_device *dssdev, bool enable_hsclk,
1110                 bool enable_hsdiv)
1111 {
1112         int r = 0;
1113         enum dsi_pll_power_state pwstate;
1114
1115         DSSDBG("PLL init\n");
1116
1117         enable_clocks(1);
1118         dsi_enable_pll_clock(1);
1119
1120         r = regulator_enable(dsi.vdds_dsi_reg);
1121         if (r)
1122                 goto err0;
1123
1124         /* XXX PLL does not come out of reset without this... */
1125         dispc_pck_free_enable(1);
1126
1127         if (wait_for_bit_change(DSI_PLL_STATUS, 0, 1) != 1) {
1128                 DSSERR("PLL not coming out of reset.\n");
1129                 r = -ENODEV;
1130                 dispc_pck_free_enable(0);
1131                 goto err1;
1132         }
1133
1134         /* XXX ... but if left on, we get problems when planes do not
1135          * fill the whole display. No idea about this */
1136         dispc_pck_free_enable(0);
1137
1138         if (enable_hsclk && enable_hsdiv)
1139                 pwstate = DSI_PLL_POWER_ON_ALL;
1140         else if (enable_hsclk)
1141                 pwstate = DSI_PLL_POWER_ON_HSCLK;
1142         else if (enable_hsdiv)
1143                 pwstate = DSI_PLL_POWER_ON_DIV;
1144         else
1145                 pwstate = DSI_PLL_POWER_OFF;
1146
1147         r = dsi_pll_power(pwstate);
1148
1149         if (r)
1150                 goto err1;
1151
1152         DSSDBG("PLL init done\n");
1153
1154         return 0;
1155 err1:
1156         regulator_disable(dsi.vdds_dsi_reg);
1157 err0:
1158         enable_clocks(0);
1159         dsi_enable_pll_clock(0);
1160         return r;
1161 }
1162
1163 void dsi_pll_uninit(void)
1164 {
1165         enable_clocks(0);
1166         dsi_enable_pll_clock(0);
1167
1168         dsi.pll_locked = 0;
1169         dsi_pll_power(DSI_PLL_POWER_OFF);
1170         regulator_disable(dsi.vdds_dsi_reg);
1171         DSSDBG("PLL uninit done\n");
1172 }
1173
1174 void dsi_dump_clocks(struct seq_file *s)
1175 {
1176         int clksel;
1177         struct dsi_clock_info *cinfo = &dsi.current_cinfo;
1178
1179         enable_clocks(1);
1180
1181         clksel = REG_GET(DSI_PLL_CONFIGURATION2, 11, 11);
1182
1183         seq_printf(s,   "- DSI PLL -\n");
1184
1185         seq_printf(s,   "dsi pll source = %s\n",
1186                         clksel == 0 ?
1187                         "dss2_alwon_fclk" : "pclkfree");
1188
1189         seq_printf(s,   "Fint\t\t%-16luregn %u\n", cinfo->fint, cinfo->regn);
1190
1191         seq_printf(s,   "CLKIN4DDR\t%-16luregm %u\n",
1192                         cinfo->clkin4ddr, cinfo->regm);
1193
1194         seq_printf(s,   "dsi1_pll_fck\t%-16luregm3 %u\t(%s)\n",
1195                         cinfo->dsi1_pll_fclk,
1196                         cinfo->regm3,
1197                         dss_get_dispc_clk_source() == DSS_SRC_DSS1_ALWON_FCLK ?
1198                         "off" : "on");
1199
1200         seq_printf(s,   "dsi2_pll_fck\t%-16luregm4 %u\t(%s)\n",
1201                         cinfo->dsi2_pll_fclk,
1202                         cinfo->regm4,
1203                         dss_get_dsi_clk_source() == DSS_SRC_DSS1_ALWON_FCLK ?
1204                         "off" : "on");
1205
1206         seq_printf(s,   "- DSI -\n");
1207
1208         seq_printf(s,   "dsi fclk source = %s\n",
1209                         dss_get_dsi_clk_source() == DSS_SRC_DSS1_ALWON_FCLK ?
1210                         "dss1_alwon_fclk" : "dsi2_pll_fclk");
1211
1212         seq_printf(s,   "DSI_FCLK\t%lu\n", dsi_fclk_rate());
1213
1214         seq_printf(s,   "DDR_CLK\t\t%lu\n",
1215                         cinfo->clkin4ddr / 4);
1216
1217         seq_printf(s,   "TxByteClkHS\t%lu\n", dsi_get_txbyteclkhs());
1218
1219         seq_printf(s,   "LP_CLK\t\t%lu\n", cinfo->lp_clk);
1220
1221         seq_printf(s,   "VP_CLK\t\t%lu\n"
1222                         "VP_PCLK\t\t%lu\n",
1223                         dispc_lclk_rate(OMAP_DSS_CHANNEL_LCD),
1224                         dispc_pclk_rate(OMAP_DSS_CHANNEL_LCD));
1225
1226         enable_clocks(0);
1227 }
1228
1229 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
1230 void dsi_dump_irqs(struct seq_file *s)
1231 {
1232         unsigned long flags;
1233         struct dsi_irq_stats stats;
1234
1235         spin_lock_irqsave(&dsi.irq_stats_lock, flags);
1236
1237         stats = dsi.irq_stats;
1238         memset(&dsi.irq_stats, 0, sizeof(dsi.irq_stats));
1239         dsi.irq_stats.last_reset = jiffies;
1240
1241         spin_unlock_irqrestore(&dsi.irq_stats_lock, flags);
1242
1243         seq_printf(s, "period %u ms\n",
1244                         jiffies_to_msecs(jiffies - stats.last_reset));
1245
1246         seq_printf(s, "irqs %d\n", stats.irq_count);
1247 #define PIS(x) \
1248         seq_printf(s, "%-20s %10d\n", #x, stats.dsi_irqs[ffs(DSI_IRQ_##x)-1]);
1249
1250         seq_printf(s, "-- DSI interrupts --\n");
1251         PIS(VC0);
1252         PIS(VC1);
1253         PIS(VC2);
1254         PIS(VC3);
1255         PIS(WAKEUP);
1256         PIS(RESYNC);
1257         PIS(PLL_LOCK);
1258         PIS(PLL_UNLOCK);
1259         PIS(PLL_RECALL);
1260         PIS(COMPLEXIO_ERR);
1261         PIS(HS_TX_TIMEOUT);
1262         PIS(LP_RX_TIMEOUT);
1263         PIS(TE_TRIGGER);
1264         PIS(ACK_TRIGGER);
1265         PIS(SYNC_LOST);
1266         PIS(LDO_POWER_GOOD);
1267         PIS(TA_TIMEOUT);
1268 #undef PIS
1269
1270 #define PIS(x) \
1271         seq_printf(s, "%-20s %10d %10d %10d %10d\n", #x, \
1272                         stats.vc_irqs[0][ffs(DSI_VC_IRQ_##x)-1], \
1273                         stats.vc_irqs[1][ffs(DSI_VC_IRQ_##x)-1], \
1274                         stats.vc_irqs[2][ffs(DSI_VC_IRQ_##x)-1], \
1275                         stats.vc_irqs[3][ffs(DSI_VC_IRQ_##x)-1]);
1276
1277         seq_printf(s, "-- VC interrupts --\n");
1278         PIS(CS);
1279         PIS(ECC_CORR);
1280         PIS(PACKET_SENT);
1281         PIS(FIFO_TX_OVF);
1282         PIS(FIFO_RX_OVF);
1283         PIS(BTA);
1284         PIS(ECC_NO_CORR);
1285         PIS(FIFO_TX_UDF);
1286         PIS(PP_BUSY_CHANGE);
1287 #undef PIS
1288
1289 #define PIS(x) \
1290         seq_printf(s, "%-20s %10d\n", #x, \
1291                         stats.cio_irqs[ffs(DSI_CIO_IRQ_##x)-1]);
1292
1293         seq_printf(s, "-- CIO interrupts --\n");
1294         PIS(ERRSYNCESC1);
1295         PIS(ERRSYNCESC2);
1296         PIS(ERRSYNCESC3);
1297         PIS(ERRESC1);
1298         PIS(ERRESC2);
1299         PIS(ERRESC3);
1300         PIS(ERRCONTROL1);
1301         PIS(ERRCONTROL2);
1302         PIS(ERRCONTROL3);
1303         PIS(STATEULPS1);
1304         PIS(STATEULPS2);
1305         PIS(STATEULPS3);
1306         PIS(ERRCONTENTIONLP0_1);
1307         PIS(ERRCONTENTIONLP1_1);
1308         PIS(ERRCONTENTIONLP0_2);
1309         PIS(ERRCONTENTIONLP1_2);
1310         PIS(ERRCONTENTIONLP0_3);
1311         PIS(ERRCONTENTIONLP1_3);
1312         PIS(ULPSACTIVENOT_ALL0);
1313         PIS(ULPSACTIVENOT_ALL1);
1314 #undef PIS
1315 }
1316 #endif
1317
1318 void dsi_dump_regs(struct seq_file *s)
1319 {
1320 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dsi_read_reg(r))
1321
1322         dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK);
1323
1324         DUMPREG(DSI_REVISION);
1325         DUMPREG(DSI_SYSCONFIG);
1326         DUMPREG(DSI_SYSSTATUS);
1327         DUMPREG(DSI_IRQSTATUS);
1328         DUMPREG(DSI_IRQENABLE);
1329         DUMPREG(DSI_CTRL);
1330         DUMPREG(DSI_COMPLEXIO_CFG1);
1331         DUMPREG(DSI_COMPLEXIO_IRQ_STATUS);
1332         DUMPREG(DSI_COMPLEXIO_IRQ_ENABLE);
1333         DUMPREG(DSI_CLK_CTRL);
1334         DUMPREG(DSI_TIMING1);
1335         DUMPREG(DSI_TIMING2);
1336         DUMPREG(DSI_VM_TIMING1);
1337         DUMPREG(DSI_VM_TIMING2);
1338         DUMPREG(DSI_VM_TIMING3);
1339         DUMPREG(DSI_CLK_TIMING);
1340         DUMPREG(DSI_TX_FIFO_VC_SIZE);
1341         DUMPREG(DSI_RX_FIFO_VC_SIZE);
1342         DUMPREG(DSI_COMPLEXIO_CFG2);
1343         DUMPREG(DSI_RX_FIFO_VC_FULLNESS);
1344         DUMPREG(DSI_VM_TIMING4);
1345         DUMPREG(DSI_TX_FIFO_VC_EMPTINESS);
1346         DUMPREG(DSI_VM_TIMING5);
1347         DUMPREG(DSI_VM_TIMING6);
1348         DUMPREG(DSI_VM_TIMING7);
1349         DUMPREG(DSI_STOPCLK_TIMING);
1350
1351         DUMPREG(DSI_VC_CTRL(0));
1352         DUMPREG(DSI_VC_TE(0));
1353         DUMPREG(DSI_VC_LONG_PACKET_HEADER(0));
1354         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(0));
1355         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(0));
1356         DUMPREG(DSI_VC_IRQSTATUS(0));
1357         DUMPREG(DSI_VC_IRQENABLE(0));
1358
1359         DUMPREG(DSI_VC_CTRL(1));
1360         DUMPREG(DSI_VC_TE(1));
1361         DUMPREG(DSI_VC_LONG_PACKET_HEADER(1));
1362         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(1));
1363         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(1));
1364         DUMPREG(DSI_VC_IRQSTATUS(1));
1365         DUMPREG(DSI_VC_IRQENABLE(1));
1366
1367         DUMPREG(DSI_VC_CTRL(2));
1368         DUMPREG(DSI_VC_TE(2));
1369         DUMPREG(DSI_VC_LONG_PACKET_HEADER(2));
1370         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(2));
1371         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(2));
1372         DUMPREG(DSI_VC_IRQSTATUS(2));
1373         DUMPREG(DSI_VC_IRQENABLE(2));
1374
1375         DUMPREG(DSI_VC_CTRL(3));
1376         DUMPREG(DSI_VC_TE(3));
1377         DUMPREG(DSI_VC_LONG_PACKET_HEADER(3));
1378         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(3));
1379         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(3));
1380         DUMPREG(DSI_VC_IRQSTATUS(3));
1381         DUMPREG(DSI_VC_IRQENABLE(3));
1382
1383         DUMPREG(DSI_DSIPHY_CFG0);
1384         DUMPREG(DSI_DSIPHY_CFG1);
1385         DUMPREG(DSI_DSIPHY_CFG2);
1386         DUMPREG(DSI_DSIPHY_CFG5);
1387
1388         DUMPREG(DSI_PLL_CONTROL);
1389         DUMPREG(DSI_PLL_STATUS);
1390         DUMPREG(DSI_PLL_GO);
1391         DUMPREG(DSI_PLL_CONFIGURATION1);
1392         DUMPREG(DSI_PLL_CONFIGURATION2);
1393
1394         dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK);
1395 #undef DUMPREG
1396 }
1397
1398 enum dsi_complexio_power_state {
1399         DSI_COMPLEXIO_POWER_OFF         = 0x0,
1400         DSI_COMPLEXIO_POWER_ON          = 0x1,
1401         DSI_COMPLEXIO_POWER_ULPS        = 0x2,
1402 };
1403
1404 static int dsi_complexio_power(enum dsi_complexio_power_state state)
1405 {
1406         int t = 0;
1407
1408         /* PWR_CMD */
1409         REG_FLD_MOD(DSI_COMPLEXIO_CFG1, state, 28, 27);
1410
1411         /* PWR_STATUS */
1412         while (FLD_GET(dsi_read_reg(DSI_COMPLEXIO_CFG1), 26, 25) != state) {
1413                 if (++t > 1000) {
1414                         DSSERR("failed to set complexio power state to "
1415                                         "%d\n", state);
1416                         return -ENODEV;
1417                 }
1418                 udelay(1);
1419         }
1420
1421         return 0;
1422 }
1423
1424 static void dsi_complexio_config(struct omap_dss_device *dssdev)
1425 {
1426         u32 r;
1427
1428         int clk_lane   = dssdev->phy.dsi.clk_lane;
1429         int data1_lane = dssdev->phy.dsi.data1_lane;
1430         int data2_lane = dssdev->phy.dsi.data2_lane;
1431         int clk_pol    = dssdev->phy.dsi.clk_pol;
1432         int data1_pol  = dssdev->phy.dsi.data1_pol;
1433         int data2_pol  = dssdev->phy.dsi.data2_pol;
1434
1435         r = dsi_read_reg(DSI_COMPLEXIO_CFG1);
1436         r = FLD_MOD(r, clk_lane, 2, 0);
1437         r = FLD_MOD(r, clk_pol, 3, 3);
1438         r = FLD_MOD(r, data1_lane, 6, 4);
1439         r = FLD_MOD(r, data1_pol, 7, 7);
1440         r = FLD_MOD(r, data2_lane, 10, 8);
1441         r = FLD_MOD(r, data2_pol, 11, 11);
1442         dsi_write_reg(DSI_COMPLEXIO_CFG1, r);
1443
1444         /* The configuration of the DSI complex I/O (number of data lanes,
1445            position, differential order) should not be changed while
1446            DSS.DSI_CLK_CRTRL[20] LP_CLK_ENABLE bit is set to 1. In order for
1447            the hardware to take into account a new configuration of the complex
1448            I/O (done in DSS.DSI_COMPLEXIO_CFG1 register), it is recommended to
1449            follow this sequence: First set the DSS.DSI_CTRL[0] IF_EN bit to 1,
1450            then reset the DSS.DSI_CTRL[0] IF_EN to 0, then set
1451            DSS.DSI_CLK_CTRL[20] LP_CLK_ENABLE to 1 and finally set again the
1452            DSS.DSI_CTRL[0] IF_EN bit to 1. If the sequence is not followed, the
1453            DSI complex I/O configuration is unknown. */
1454
1455         /*
1456         REG_FLD_MOD(DSI_CTRL, 1, 0, 0);
1457         REG_FLD_MOD(DSI_CTRL, 0, 0, 0);
1458         REG_FLD_MOD(DSI_CLK_CTRL, 1, 20, 20);
1459         REG_FLD_MOD(DSI_CTRL, 1, 0, 0);
1460         */
1461 }
1462
1463 static inline unsigned ns2ddr(unsigned ns)
1464 {
1465         /* convert time in ns to ddr ticks, rounding up */
1466         unsigned long ddr_clk = dsi.current_cinfo.clkin4ddr / 4;
1467         return (ns * (ddr_clk / 1000 / 1000) + 999) / 1000;
1468 }
1469
1470 static inline unsigned ddr2ns(unsigned ddr)
1471 {
1472         unsigned long ddr_clk = dsi.current_cinfo.clkin4ddr / 4;
1473         return ddr * 1000 * 1000 / (ddr_clk / 1000);
1474 }
1475
1476 static void dsi_complexio_timings(void)
1477 {
1478         u32 r;
1479         u32 ths_prepare, ths_prepare_ths_zero, ths_trail, ths_exit;
1480         u32 tlpx_half, tclk_trail, tclk_zero;
1481         u32 tclk_prepare;
1482
1483         /* calculate timings */
1484
1485         /* 1 * DDR_CLK = 2 * UI */
1486
1487         /* min 40ns + 4*UI      max 85ns + 6*UI */
1488         ths_prepare = ns2ddr(70) + 2;
1489
1490         /* min 145ns + 10*UI */
1491         ths_prepare_ths_zero = ns2ddr(175) + 2;
1492
1493         /* min max(8*UI, 60ns+4*UI) */
1494         ths_trail = ns2ddr(60) + 5;
1495
1496         /* min 100ns */
1497         ths_exit = ns2ddr(145);
1498
1499         /* tlpx min 50n */
1500         tlpx_half = ns2ddr(25);
1501
1502         /* min 60ns */
1503         tclk_trail = ns2ddr(60) + 2;
1504
1505         /* min 38ns, max 95ns */
1506         tclk_prepare = ns2ddr(65);
1507
1508         /* min tclk-prepare + tclk-zero = 300ns */
1509         tclk_zero = ns2ddr(260);
1510
1511         DSSDBG("ths_prepare %u (%uns), ths_prepare_ths_zero %u (%uns)\n",
1512                 ths_prepare, ddr2ns(ths_prepare),
1513                 ths_prepare_ths_zero, ddr2ns(ths_prepare_ths_zero));
1514         DSSDBG("ths_trail %u (%uns), ths_exit %u (%uns)\n",
1515                         ths_trail, ddr2ns(ths_trail),
1516                         ths_exit, ddr2ns(ths_exit));
1517
1518         DSSDBG("tlpx_half %u (%uns), tclk_trail %u (%uns), "
1519                         "tclk_zero %u (%uns)\n",
1520                         tlpx_half, ddr2ns(tlpx_half),
1521                         tclk_trail, ddr2ns(tclk_trail),
1522                         tclk_zero, ddr2ns(tclk_zero));
1523         DSSDBG("tclk_prepare %u (%uns)\n",
1524                         tclk_prepare, ddr2ns(tclk_prepare));
1525
1526         /* program timings */
1527
1528         r = dsi_read_reg(DSI_DSIPHY_CFG0);
1529         r = FLD_MOD(r, ths_prepare, 31, 24);
1530         r = FLD_MOD(r, ths_prepare_ths_zero, 23, 16);
1531         r = FLD_MOD(r, ths_trail, 15, 8);
1532         r = FLD_MOD(r, ths_exit, 7, 0);
1533         dsi_write_reg(DSI_DSIPHY_CFG0, r);
1534
1535         r = dsi_read_reg(DSI_DSIPHY_CFG1);
1536         r = FLD_MOD(r, tlpx_half, 22, 16);
1537         r = FLD_MOD(r, tclk_trail, 15, 8);
1538         r = FLD_MOD(r, tclk_zero, 7, 0);
1539         dsi_write_reg(DSI_DSIPHY_CFG1, r);
1540
1541         r = dsi_read_reg(DSI_DSIPHY_CFG2);
1542         r = FLD_MOD(r, tclk_prepare, 7, 0);
1543         dsi_write_reg(DSI_DSIPHY_CFG2, r);
1544 }
1545
1546
1547 static int dsi_complexio_init(struct omap_dss_device *dssdev)
1548 {
1549         int r = 0;
1550
1551         DSSDBG("dsi_complexio_init\n");
1552
1553         /* CIO_CLK_ICG, enable L3 clk to CIO */
1554         REG_FLD_MOD(DSI_CLK_CTRL, 1, 14, 14);
1555
1556         /* A dummy read using the SCP interface to any DSIPHY register is
1557          * required after DSIPHY reset to complete the reset of the DSI complex
1558          * I/O. */
1559         dsi_read_reg(DSI_DSIPHY_CFG5);
1560
1561         if (wait_for_bit_change(DSI_DSIPHY_CFG5, 30, 1) != 1) {
1562                 DSSERR("ComplexIO PHY not coming out of reset.\n");
1563                 r = -ENODEV;
1564                 goto err;
1565         }
1566
1567         dsi_complexio_config(dssdev);
1568
1569         r = dsi_complexio_power(DSI_COMPLEXIO_POWER_ON);
1570
1571         if (r)
1572                 goto err;
1573
1574         if (wait_for_bit_change(DSI_COMPLEXIO_CFG1, 29, 1) != 1) {
1575                 DSSERR("ComplexIO not coming out of reset.\n");
1576                 r = -ENODEV;
1577                 goto err;
1578         }
1579
1580         if (wait_for_bit_change(DSI_COMPLEXIO_CFG1, 21, 1) != 1) {
1581                 DSSERR("ComplexIO LDO power down.\n");
1582                 r = -ENODEV;
1583                 goto err;
1584         }
1585
1586         dsi_complexio_timings();
1587
1588         /*
1589            The configuration of the DSI complex I/O (number of data lanes,
1590            position, differential order) should not be changed while
1591            DSS.DSI_CLK_CRTRL[20] LP_CLK_ENABLE bit is set to 1. For the
1592            hardware to recognize a new configuration of the complex I/O (done
1593            in DSS.DSI_COMPLEXIO_CFG1 register), it is recommended to follow
1594            this sequence: First set the DSS.DSI_CTRL[0] IF_EN bit to 1, next
1595            reset the DSS.DSI_CTRL[0] IF_EN to 0, then set DSS.DSI_CLK_CTRL[20]
1596            LP_CLK_ENABLE to 1, and finally, set again the DSS.DSI_CTRL[0] IF_EN
1597            bit to 1. If the sequence is not followed, the DSi complex I/O
1598            configuration is undetermined.
1599            */
1600         dsi_if_enable(1);
1601         dsi_if_enable(0);
1602         REG_FLD_MOD(DSI_CLK_CTRL, 1, 20, 20); /* LP_CLK_ENABLE */
1603         dsi_if_enable(1);
1604         dsi_if_enable(0);
1605
1606         DSSDBG("CIO init done\n");
1607 err:
1608         return r;
1609 }
1610
1611 static void dsi_complexio_uninit(void)
1612 {
1613         dsi_complexio_power(DSI_COMPLEXIO_POWER_OFF);
1614 }
1615
1616 static int _dsi_wait_reset(void)
1617 {
1618         int t = 0;
1619
1620         while (REG_GET(DSI_SYSSTATUS, 0, 0) == 0) {
1621                 if (++t > 5) {
1622                         DSSERR("soft reset failed\n");
1623                         return -ENODEV;
1624                 }
1625                 udelay(1);
1626         }
1627
1628         return 0;
1629 }
1630
1631 static int _dsi_reset(void)
1632 {
1633         /* Soft reset */
1634         REG_FLD_MOD(DSI_SYSCONFIG, 1, 1, 1);
1635         return _dsi_wait_reset();
1636 }
1637
1638 static void dsi_reset_tx_fifo(int channel)
1639 {
1640         u32 mask;
1641         u32 l;
1642
1643         /* set fifosize of the channel to 0, then return the old size */
1644         l = dsi_read_reg(DSI_TX_FIFO_VC_SIZE);
1645
1646         mask = FLD_MASK((8 * channel) + 7, (8 * channel) + 4);
1647         dsi_write_reg(DSI_TX_FIFO_VC_SIZE, l & ~mask);
1648
1649         dsi_write_reg(DSI_TX_FIFO_VC_SIZE, l);
1650 }
1651
1652 static void dsi_config_tx_fifo(enum fifo_size size1, enum fifo_size size2,
1653                 enum fifo_size size3, enum fifo_size size4)
1654 {
1655         u32 r = 0;
1656         int add = 0;
1657         int i;
1658
1659         dsi.vc[0].fifo_size = size1;
1660         dsi.vc[1].fifo_size = size2;
1661         dsi.vc[2].fifo_size = size3;
1662         dsi.vc[3].fifo_size = size4;
1663
1664         for (i = 0; i < 4; i++) {
1665                 u8 v;
1666                 int size = dsi.vc[i].fifo_size;
1667
1668                 if (add + size > 4) {
1669                         DSSERR("Illegal FIFO configuration\n");
1670                         BUG();
1671                 }
1672
1673                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
1674                 r |= v << (8 * i);
1675                 /*DSSDBG("TX FIFO vc %d: size %d, add %d\n", i, size, add); */
1676                 add += size;
1677         }
1678
1679         dsi_write_reg(DSI_TX_FIFO_VC_SIZE, r);
1680 }
1681
1682 static void dsi_config_rx_fifo(enum fifo_size size1, enum fifo_size size2,
1683                 enum fifo_size size3, enum fifo_size size4)
1684 {
1685         u32 r = 0;
1686         int add = 0;
1687         int i;
1688
1689         dsi.vc[0].fifo_size = size1;
1690         dsi.vc[1].fifo_size = size2;
1691         dsi.vc[2].fifo_size = size3;
1692         dsi.vc[3].fifo_size = size4;
1693
1694         for (i = 0; i < 4; i++) {
1695                 u8 v;
1696                 int size = dsi.vc[i].fifo_size;
1697
1698                 if (add + size > 4) {
1699                         DSSERR("Illegal FIFO configuration\n");
1700                         BUG();
1701                 }
1702
1703                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
1704                 r |= v << (8 * i);
1705                 /*DSSDBG("RX FIFO vc %d: size %d, add %d\n", i, size, add); */
1706                 add += size;
1707         }
1708
1709         dsi_write_reg(DSI_RX_FIFO_VC_SIZE, r);
1710 }
1711
1712 static int dsi_force_tx_stop_mode_io(void)
1713 {
1714         u32 r;
1715
1716         r = dsi_read_reg(DSI_TIMING1);
1717         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
1718         dsi_write_reg(DSI_TIMING1, r);
1719
1720         if (wait_for_bit_change(DSI_TIMING1, 15, 0) != 0) {
1721                 DSSERR("TX_STOP bit not going down\n");
1722                 return -EIO;
1723         }
1724
1725         return 0;
1726 }
1727
1728 static int dsi_vc_enable(int channel, bool enable)
1729 {
1730         DSSDBG("dsi_vc_enable channel %d, enable %d\n",
1731                         channel, enable);
1732
1733         enable = enable ? 1 : 0;
1734
1735         REG_FLD_MOD(DSI_VC_CTRL(channel), enable, 0, 0);
1736
1737         if (wait_for_bit_change(DSI_VC_CTRL(channel), 0, enable) != enable) {
1738                         DSSERR("Failed to set dsi_vc_enable to %d\n", enable);
1739                         return -EIO;
1740         }
1741
1742         return 0;
1743 }
1744
1745 static void dsi_vc_initial_config(int channel)
1746 {
1747         u32 r;
1748
1749         DSSDBGF("%d", channel);
1750
1751         r = dsi_read_reg(DSI_VC_CTRL(channel));
1752
1753         if (FLD_GET(r, 15, 15)) /* VC_BUSY */
1754                 DSSERR("VC(%d) busy when trying to configure it!\n",
1755                                 channel);
1756
1757         r = FLD_MOD(r, 0, 1, 1); /* SOURCE, 0 = L4 */
1758         r = FLD_MOD(r, 0, 2, 2); /* BTA_SHORT_EN  */
1759         r = FLD_MOD(r, 0, 3, 3); /* BTA_LONG_EN */
1760         r = FLD_MOD(r, 0, 4, 4); /* MODE, 0 = command */
1761         r = FLD_MOD(r, 1, 7, 7); /* CS_TX_EN */
1762         r = FLD_MOD(r, 1, 8, 8); /* ECC_TX_EN */
1763         r = FLD_MOD(r, 0, 9, 9); /* MODE_SPEED, high speed on/off */
1764
1765         r = FLD_MOD(r, 4, 29, 27); /* DMA_RX_REQ_NB = no dma */
1766         r = FLD_MOD(r, 4, 23, 21); /* DMA_TX_REQ_NB = no dma */
1767
1768         dsi_write_reg(DSI_VC_CTRL(channel), r);
1769
1770         dsi.vc[channel].mode = DSI_VC_MODE_L4;
1771 }
1772
1773 static int dsi_vc_config_l4(int channel)
1774 {
1775         if (dsi.vc[channel].mode == DSI_VC_MODE_L4)
1776                 return 0;
1777
1778         DSSDBGF("%d", channel);
1779
1780         dsi_vc_enable(channel, 0);
1781
1782         /* VC_BUSY */
1783         if (wait_for_bit_change(DSI_VC_CTRL(channel), 15, 0) != 0) {
1784                 DSSERR("vc(%d) busy when trying to config for L4\n", channel);
1785                 return -EIO;
1786         }
1787
1788         REG_FLD_MOD(DSI_VC_CTRL(channel), 0, 1, 1); /* SOURCE, 0 = L4 */
1789
1790         dsi_vc_enable(channel, 1);
1791
1792         dsi.vc[channel].mode = DSI_VC_MODE_L4;
1793
1794         return 0;
1795 }
1796
1797 static int dsi_vc_config_vp(int channel)
1798 {
1799         if (dsi.vc[channel].mode == DSI_VC_MODE_VP)
1800                 return 0;
1801
1802         DSSDBGF("%d", channel);
1803
1804         dsi_vc_enable(channel, 0);
1805
1806         /* VC_BUSY */
1807         if (wait_for_bit_change(DSI_VC_CTRL(channel), 15, 0) != 0) {
1808                 DSSERR("vc(%d) busy when trying to config for VP\n", channel);
1809                 return -EIO;
1810         }
1811
1812         REG_FLD_MOD(DSI_VC_CTRL(channel), 1, 1, 1); /* SOURCE, 1 = video port */
1813
1814         dsi_vc_enable(channel, 1);
1815
1816         dsi.vc[channel].mode = DSI_VC_MODE_VP;
1817
1818         return 0;
1819 }
1820
1821
1822 void omapdss_dsi_vc_enable_hs(int channel, bool enable)
1823 {
1824         DSSDBG("dsi_vc_enable_hs(%d, %d)\n", channel, enable);
1825
1826         WARN_ON(!dsi_bus_is_locked());
1827
1828         dsi_vc_enable(channel, 0);
1829         dsi_if_enable(0);
1830
1831         REG_FLD_MOD(DSI_VC_CTRL(channel), enable, 9, 9);
1832
1833         dsi_vc_enable(channel, 1);
1834         dsi_if_enable(1);
1835
1836         dsi_force_tx_stop_mode_io();
1837 }
1838 EXPORT_SYMBOL(omapdss_dsi_vc_enable_hs);
1839
1840 static void dsi_vc_flush_long_data(int channel)
1841 {
1842         while (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {
1843                 u32 val;
1844                 val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
1845                 DSSDBG("\t\tb1 %#02x b2 %#02x b3 %#02x b4 %#02x\n",
1846                                 (val >> 0) & 0xff,
1847                                 (val >> 8) & 0xff,
1848                                 (val >> 16) & 0xff,
1849                                 (val >> 24) & 0xff);
1850         }
1851 }
1852
1853 static void dsi_show_rx_ack_with_err(u16 err)
1854 {
1855         DSSERR("\tACK with ERROR (%#x):\n", err);
1856         if (err & (1 << 0))
1857                 DSSERR("\t\tSoT Error\n");
1858         if (err & (1 << 1))
1859                 DSSERR("\t\tSoT Sync Error\n");
1860         if (err & (1 << 2))
1861                 DSSERR("\t\tEoT Sync Error\n");
1862         if (err & (1 << 3))
1863                 DSSERR("\t\tEscape Mode Entry Command Error\n");
1864         if (err & (1 << 4))
1865                 DSSERR("\t\tLP Transmit Sync Error\n");
1866         if (err & (1 << 5))
1867                 DSSERR("\t\tHS Receive Timeout Error\n");
1868         if (err & (1 << 6))
1869                 DSSERR("\t\tFalse Control Error\n");
1870         if (err & (1 << 7))
1871                 DSSERR("\t\t(reserved7)\n");
1872         if (err & (1 << 8))
1873                 DSSERR("\t\tECC Error, single-bit (corrected)\n");
1874         if (err & (1 << 9))
1875                 DSSERR("\t\tECC Error, multi-bit (not corrected)\n");
1876         if (err & (1 << 10))
1877                 DSSERR("\t\tChecksum Error\n");
1878         if (err & (1 << 11))
1879                 DSSERR("\t\tData type not recognized\n");
1880         if (err & (1 << 12))
1881                 DSSERR("\t\tInvalid VC ID\n");
1882         if (err & (1 << 13))
1883                 DSSERR("\t\tInvalid Transmission Length\n");
1884         if (err & (1 << 14))
1885                 DSSERR("\t\t(reserved14)\n");
1886         if (err & (1 << 15))
1887                 DSSERR("\t\tDSI Protocol Violation\n");
1888 }
1889
1890 static u16 dsi_vc_flush_receive_data(int channel)
1891 {
1892         /* RX_FIFO_NOT_EMPTY */
1893         while (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {
1894                 u32 val;
1895                 u8 dt;
1896                 val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
1897                 DSSERR("\trawval %#08x\n", val);
1898                 dt = FLD_GET(val, 5, 0);
1899                 if (dt == DSI_DT_RX_ACK_WITH_ERR) {
1900                         u16 err = FLD_GET(val, 23, 8);
1901                         dsi_show_rx_ack_with_err(err);
1902                 } else if (dt == DSI_DT_RX_SHORT_READ_1) {
1903                         DSSERR("\tDCS short response, 1 byte: %#x\n",
1904                                         FLD_GET(val, 23, 8));
1905                 } else if (dt == DSI_DT_RX_SHORT_READ_2) {
1906                         DSSERR("\tDCS short response, 2 byte: %#x\n",
1907                                         FLD_GET(val, 23, 8));
1908                 } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
1909                         DSSERR("\tDCS long response, len %d\n",
1910                                         FLD_GET(val, 23, 8));
1911                         dsi_vc_flush_long_data(channel);
1912                 } else {
1913                         DSSERR("\tunknown datatype 0x%02x\n", dt);
1914                 }
1915         }
1916         return 0;
1917 }
1918
1919 static int dsi_vc_send_bta(int channel)
1920 {
1921         if (dsi.debug_write || dsi.debug_read)
1922                 DSSDBG("dsi_vc_send_bta %d\n", channel);
1923
1924         WARN_ON(!dsi_bus_is_locked());
1925
1926         if (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {    /* RX_FIFO_NOT_EMPTY */
1927                 DSSERR("rx fifo not empty when sending BTA, dumping data:\n");
1928                 dsi_vc_flush_receive_data(channel);
1929         }
1930
1931         REG_FLD_MOD(DSI_VC_CTRL(channel), 1, 6, 6); /* BTA_EN */
1932
1933         return 0;
1934 }
1935
1936 int dsi_vc_send_bta_sync(int channel)
1937 {
1938         int r = 0;
1939         u32 err;
1940
1941         INIT_COMPLETION(dsi.bta_completion);
1942
1943         dsi_vc_enable_bta_irq(channel);
1944
1945         r = dsi_vc_send_bta(channel);
1946         if (r)
1947                 goto err;
1948
1949         if (wait_for_completion_timeout(&dsi.bta_completion,
1950                                 msecs_to_jiffies(500)) == 0) {
1951                 DSSERR("Failed to receive BTA\n");
1952                 r = -EIO;
1953                 goto err;
1954         }
1955
1956         err = dsi_get_errors();
1957         if (err) {
1958                 DSSERR("Error while sending BTA: %x\n", err);
1959                 r = -EIO;
1960                 goto err;
1961         }
1962 err:
1963         dsi_vc_disable_bta_irq(channel);
1964
1965         return r;
1966 }
1967 EXPORT_SYMBOL(dsi_vc_send_bta_sync);
1968
1969 static inline void dsi_vc_write_long_header(int channel, u8 data_type,
1970                 u16 len, u8 ecc)
1971 {
1972         u32 val;
1973         u8 data_id;
1974
1975         WARN_ON(!dsi_bus_is_locked());
1976
1977         data_id = data_type | channel << 6;
1978
1979         val = FLD_VAL(data_id, 7, 0) | FLD_VAL(len, 23, 8) |
1980                 FLD_VAL(ecc, 31, 24);
1981
1982         dsi_write_reg(DSI_VC_LONG_PACKET_HEADER(channel), val);
1983 }
1984
1985 static inline void dsi_vc_write_long_payload(int channel,
1986                 u8 b1, u8 b2, u8 b3, u8 b4)
1987 {
1988         u32 val;
1989
1990         val = b4 << 24 | b3 << 16 | b2 << 8  | b1 << 0;
1991
1992 /*      DSSDBG("\twriting %02x, %02x, %02x, %02x (%#010x)\n",
1993                         b1, b2, b3, b4, val); */
1994
1995         dsi_write_reg(DSI_VC_LONG_PACKET_PAYLOAD(channel), val);
1996 }
1997
1998 static int dsi_vc_send_long(int channel, u8 data_type, u8 *data, u16 len,
1999                 u8 ecc)
2000 {
2001         /*u32 val; */
2002         int i;
2003         u8 *p;
2004         int r = 0;
2005         u8 b1, b2, b3, b4;
2006
2007         if (dsi.debug_write)
2008                 DSSDBG("dsi_vc_send_long, %d bytes\n", len);
2009
2010         /* len + header */
2011         if (dsi.vc[channel].fifo_size * 32 * 4 < len + 4) {
2012                 DSSERR("unable to send long packet: packet too long.\n");
2013                 return -EINVAL;
2014         }
2015
2016         dsi_vc_config_l4(channel);
2017
2018         dsi_vc_write_long_header(channel, data_type, len, ecc);
2019
2020         p = data;
2021         for (i = 0; i < len >> 2; i++) {
2022                 if (dsi.debug_write)
2023                         DSSDBG("\tsending full packet %d\n", i);
2024
2025                 b1 = *p++;
2026                 b2 = *p++;
2027                 b3 = *p++;
2028                 b4 = *p++;
2029
2030                 dsi_vc_write_long_payload(channel, b1, b2, b3, b4);
2031         }
2032
2033         i = len % 4;
2034         if (i) {
2035                 b1 = 0; b2 = 0; b3 = 0;
2036
2037                 if (dsi.debug_write)
2038                         DSSDBG("\tsending remainder bytes %d\n", i);
2039
2040                 switch (i) {
2041                 case 3:
2042                         b1 = *p++;
2043                         b2 = *p++;
2044                         b3 = *p++;
2045                         break;
2046                 case 2:
2047                         b1 = *p++;
2048                         b2 = *p++;
2049                         break;
2050                 case 1:
2051                         b1 = *p++;
2052                         break;
2053                 }
2054
2055                 dsi_vc_write_long_payload(channel, b1, b2, b3, 0);
2056         }
2057
2058         return r;
2059 }
2060
2061 static int dsi_vc_send_short(int channel, u8 data_type, u16 data, u8 ecc)
2062 {
2063         u32 r;
2064         u8 data_id;
2065
2066         WARN_ON(!dsi_bus_is_locked());
2067
2068         if (dsi.debug_write)
2069                 DSSDBG("dsi_vc_send_short(ch%d, dt %#x, b1 %#x, b2 %#x)\n",
2070                                 channel,
2071                                 data_type, data & 0xff, (data >> 8) & 0xff);
2072
2073         dsi_vc_config_l4(channel);
2074
2075         if (FLD_GET(dsi_read_reg(DSI_VC_CTRL(channel)), 16, 16)) {
2076                 DSSERR("ERROR FIFO FULL, aborting transfer\n");
2077                 return -EINVAL;
2078         }
2079
2080         data_id = data_type | channel << 6;
2081
2082         r = (data_id << 0) | (data << 8) | (ecc << 24);
2083
2084         dsi_write_reg(DSI_VC_SHORT_PACKET_HEADER(channel), r);
2085
2086         return 0;
2087 }
2088
2089 int dsi_vc_send_null(int channel)
2090 {
2091         u8 nullpkg[] = {0, 0, 0, 0};
2092         return dsi_vc_send_long(channel, DSI_DT_NULL_PACKET, nullpkg, 4, 0);
2093 }
2094 EXPORT_SYMBOL(dsi_vc_send_null);
2095
2096 int dsi_vc_dcs_write_nosync(int channel, u8 *data, int len)
2097 {
2098         int r;
2099
2100         BUG_ON(len == 0);
2101
2102         if (len == 1) {
2103                 r = dsi_vc_send_short(channel, DSI_DT_DCS_SHORT_WRITE_0,
2104                                 data[0], 0);
2105         } else if (len == 2) {
2106                 r = dsi_vc_send_short(channel, DSI_DT_DCS_SHORT_WRITE_1,
2107                                 data[0] | (data[1] << 8), 0);
2108         } else {
2109                 /* 0x39 = DCS Long Write */
2110                 r = dsi_vc_send_long(channel, DSI_DT_DCS_LONG_WRITE,
2111                                 data, len, 0);
2112         }
2113
2114         return r;
2115 }
2116 EXPORT_SYMBOL(dsi_vc_dcs_write_nosync);
2117
2118 int dsi_vc_dcs_write(int channel, u8 *data, int len)
2119 {
2120         int r;
2121
2122         r = dsi_vc_dcs_write_nosync(channel, data, len);
2123         if (r)
2124                 goto err;
2125
2126         r = dsi_vc_send_bta_sync(channel);
2127         if (r)
2128                 goto err;
2129
2130         if (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {    /* RX_FIFO_NOT_EMPTY */
2131                 DSSERR("rx fifo not empty after write, dumping data:\n");
2132                 dsi_vc_flush_receive_data(channel);
2133                 r = -EIO;
2134                 goto err;
2135         }
2136
2137         return 0;
2138 err:
2139         DSSERR("dsi_vc_dcs_write(ch %d, cmd 0x%02x, len %d) failed\n",
2140                         channel, data[0], len);
2141         return r;
2142 }
2143 EXPORT_SYMBOL(dsi_vc_dcs_write);
2144
2145 int dsi_vc_dcs_write_0(int channel, u8 dcs_cmd)
2146 {
2147         return dsi_vc_dcs_write(channel, &dcs_cmd, 1);
2148 }
2149 EXPORT_SYMBOL(dsi_vc_dcs_write_0);
2150
2151 int dsi_vc_dcs_write_1(int channel, u8 dcs_cmd, u8 param)
2152 {
2153         u8 buf[2];
2154         buf[0] = dcs_cmd;
2155         buf[1] = param;
2156         return dsi_vc_dcs_write(channel, buf, 2);
2157 }
2158 EXPORT_SYMBOL(dsi_vc_dcs_write_1);
2159
2160 int dsi_vc_dcs_read(int channel, u8 dcs_cmd, u8 *buf, int buflen)
2161 {
2162         u32 val;
2163         u8 dt;
2164         int r;
2165
2166         if (dsi.debug_read)
2167                 DSSDBG("dsi_vc_dcs_read(ch%d, dcs_cmd %x)\n", channel, dcs_cmd);
2168
2169         r = dsi_vc_send_short(channel, DSI_DT_DCS_READ, dcs_cmd, 0);
2170         if (r)
2171                 goto err;
2172
2173         r = dsi_vc_send_bta_sync(channel);
2174         if (r)
2175                 goto err;
2176
2177         /* RX_FIFO_NOT_EMPTY */
2178         if (REG_GET(DSI_VC_CTRL(channel), 20, 20) == 0) {
2179                 DSSERR("RX fifo empty when trying to read.\n");
2180                 r = -EIO;
2181                 goto err;
2182         }
2183
2184         val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
2185         if (dsi.debug_read)
2186                 DSSDBG("\theader: %08x\n", val);
2187         dt = FLD_GET(val, 5, 0);
2188         if (dt == DSI_DT_RX_ACK_WITH_ERR) {
2189                 u16 err = FLD_GET(val, 23, 8);
2190                 dsi_show_rx_ack_with_err(err);
2191                 r = -EIO;
2192                 goto err;
2193
2194         } else if (dt == DSI_DT_RX_SHORT_READ_1) {
2195                 u8 data = FLD_GET(val, 15, 8);
2196                 if (dsi.debug_read)
2197                         DSSDBG("\tDCS short response, 1 byte: %02x\n", data);
2198
2199                 if (buflen < 1) {
2200                         r = -EIO;
2201                         goto err;
2202                 }
2203
2204                 buf[0] = data;
2205
2206                 return 1;
2207         } else if (dt == DSI_DT_RX_SHORT_READ_2) {
2208                 u16 data = FLD_GET(val, 23, 8);
2209                 if (dsi.debug_read)
2210                         DSSDBG("\tDCS short response, 2 byte: %04x\n", data);
2211
2212                 if (buflen < 2) {
2213                         r = -EIO;
2214                         goto err;
2215                 }
2216
2217                 buf[0] = data & 0xff;
2218                 buf[1] = (data >> 8) & 0xff;
2219
2220                 return 2;
2221         } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
2222                 int w;
2223                 int len = FLD_GET(val, 23, 8);
2224                 if (dsi.debug_read)
2225                         DSSDBG("\tDCS long response, len %d\n", len);
2226
2227                 if (len > buflen) {
2228                         r = -EIO;
2229                         goto err;
2230                 }
2231
2232                 /* two byte checksum ends the packet, not included in len */
2233                 for (w = 0; w < len + 2;) {
2234                         int b;
2235                         val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
2236                         if (dsi.debug_read)
2237                                 DSSDBG("\t\t%02x %02x %02x %02x\n",
2238                                                 (val >> 0) & 0xff,
2239                                                 (val >> 8) & 0xff,
2240                                                 (val >> 16) & 0xff,
2241                                                 (val >> 24) & 0xff);
2242
2243                         for (b = 0; b < 4; ++b) {
2244                                 if (w < len)
2245                                         buf[w] = (val >> (b * 8)) & 0xff;
2246                                 /* we discard the 2 byte checksum */
2247                                 ++w;
2248                         }
2249                 }
2250
2251                 return len;
2252         } else {
2253                 DSSERR("\tunknown datatype 0x%02x\n", dt);
2254                 r = -EIO;
2255                 goto err;
2256         }
2257
2258         BUG();
2259 err:
2260         DSSERR("dsi_vc_dcs_read(ch %d, cmd 0x%02x) failed\n",
2261                         channel, dcs_cmd);
2262         return r;
2263
2264 }
2265 EXPORT_SYMBOL(dsi_vc_dcs_read);
2266
2267 int dsi_vc_dcs_read_1(int channel, u8 dcs_cmd, u8 *data)
2268 {
2269         int r;
2270
2271         r = dsi_vc_dcs_read(channel, dcs_cmd, data, 1);
2272
2273         if (r < 0)
2274                 return r;
2275
2276         if (r != 1)
2277                 return -EIO;
2278
2279         return 0;
2280 }
2281 EXPORT_SYMBOL(dsi_vc_dcs_read_1);
2282
2283 int dsi_vc_dcs_read_2(int channel, u8 dcs_cmd, u8 *data1, u8 *data2)
2284 {
2285         u8 buf[2];
2286         int r;
2287
2288         r = dsi_vc_dcs_read(channel, dcs_cmd, buf, 2);
2289
2290         if (r < 0)
2291                 return r;
2292
2293         if (r != 2)
2294                 return -EIO;
2295
2296         *data1 = buf[0];
2297         *data2 = buf[1];
2298
2299         return 0;
2300 }
2301 EXPORT_SYMBOL(dsi_vc_dcs_read_2);
2302
2303 int dsi_vc_set_max_rx_packet_size(int channel, u16 len)
2304 {
2305         return dsi_vc_send_short(channel, DSI_DT_SET_MAX_RET_PKG_SIZE,
2306                         len, 0);
2307 }
2308 EXPORT_SYMBOL(dsi_vc_set_max_rx_packet_size);
2309
2310 static void dsi_set_lp_rx_timeout(unsigned ticks, bool x4, bool x16)
2311 {
2312         unsigned long fck;
2313         unsigned long total_ticks;
2314         u32 r;
2315
2316         BUG_ON(ticks > 0x1fff);
2317
2318         /* ticks in DSI_FCK */
2319         fck = dsi_fclk_rate();
2320
2321         r = dsi_read_reg(DSI_TIMING2);
2322         r = FLD_MOD(r, 1, 15, 15);      /* LP_RX_TO */
2323         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* LP_RX_TO_X16 */
2324         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* LP_RX_TO_X4 */
2325         r = FLD_MOD(r, ticks, 12, 0);   /* LP_RX_COUNTER */
2326         dsi_write_reg(DSI_TIMING2, r);
2327
2328         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
2329
2330         DSSDBG("LP_RX_TO %lu ticks (%#x%s%s) = %lu ns\n",
2331                         total_ticks,
2332                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
2333                         (total_ticks * 1000) / (fck / 1000 / 1000));
2334 }
2335
2336 static void dsi_set_ta_timeout(unsigned ticks, bool x8, bool x16)
2337 {
2338         unsigned long fck;
2339         unsigned long total_ticks;
2340         u32 r;
2341
2342         BUG_ON(ticks > 0x1fff);
2343
2344         /* ticks in DSI_FCK */
2345         fck = dsi_fclk_rate();
2346
2347         r = dsi_read_reg(DSI_TIMING1);
2348         r = FLD_MOD(r, 1, 31, 31);      /* TA_TO */
2349         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* TA_TO_X16 */
2350         r = FLD_MOD(r, x8 ? 1 : 0, 29, 29);     /* TA_TO_X8 */
2351         r = FLD_MOD(r, ticks, 28, 16);  /* TA_TO_COUNTER */
2352         dsi_write_reg(DSI_TIMING1, r);
2353
2354         total_ticks = ticks * (x16 ? 16 : 1) * (x8 ? 8 : 1);
2355
2356         DSSDBG("TA_TO %lu ticks (%#x%s%s) = %lu ns\n",
2357                         total_ticks,
2358                         ticks, x8 ? " x8" : "", x16 ? " x16" : "",
2359                         (total_ticks * 1000) / (fck / 1000 / 1000));
2360 }
2361
2362 static void dsi_set_stop_state_counter(unsigned ticks, bool x4, bool x16)
2363 {
2364         unsigned long fck;
2365         unsigned long total_ticks;
2366         u32 r;
2367
2368         BUG_ON(ticks > 0x1fff);
2369
2370         /* ticks in DSI_FCK */
2371         fck = dsi_fclk_rate();
2372
2373         r = dsi_read_reg(DSI_TIMING1);
2374         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2375         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* STOP_STATE_X16_IO */
2376         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* STOP_STATE_X4_IO */
2377         r = FLD_MOD(r, ticks, 12, 0);   /* STOP_STATE_COUNTER_IO */
2378         dsi_write_reg(DSI_TIMING1, r);
2379
2380         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
2381
2382         DSSDBG("STOP_STATE_COUNTER %lu ticks (%#x%s%s) = %lu ns\n",
2383                         total_ticks,
2384                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
2385                         (total_ticks * 1000) / (fck / 1000 / 1000));
2386 }
2387
2388 static void dsi_set_hs_tx_timeout(unsigned ticks, bool x4, bool x16)
2389 {
2390         unsigned long fck;
2391         unsigned long total_ticks;
2392         u32 r;
2393
2394         BUG_ON(ticks > 0x1fff);
2395
2396         /* ticks in TxByteClkHS */
2397         fck = dsi_get_txbyteclkhs();
2398
2399         r = dsi_read_reg(DSI_TIMING2);
2400         r = FLD_MOD(r, 1, 31, 31);      /* HS_TX_TO */
2401         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* HS_TX_TO_X16 */
2402         r = FLD_MOD(r, x4 ? 1 : 0, 29, 29);     /* HS_TX_TO_X8 (4 really) */
2403         r = FLD_MOD(r, ticks, 28, 16);  /* HS_TX_TO_COUNTER */
2404         dsi_write_reg(DSI_TIMING2, r);
2405
2406         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
2407
2408         DSSDBG("HS_TX_TO %lu ticks (%#x%s%s) = %lu ns\n",
2409                         total_ticks,
2410                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
2411                         (total_ticks * 1000) / (fck / 1000 / 1000));
2412 }
2413 static int dsi_proto_config(struct omap_dss_device *dssdev)
2414 {
2415         u32 r;
2416         int buswidth = 0;
2417
2418         dsi_config_tx_fifo(DSI_FIFO_SIZE_32,
2419                         DSI_FIFO_SIZE_32,
2420                         DSI_FIFO_SIZE_32,
2421                         DSI_FIFO_SIZE_32);
2422
2423         dsi_config_rx_fifo(DSI_FIFO_SIZE_32,
2424                         DSI_FIFO_SIZE_32,
2425                         DSI_FIFO_SIZE_32,
2426                         DSI_FIFO_SIZE_32);
2427
2428         /* XXX what values for the timeouts? */
2429         dsi_set_stop_state_counter(0x1000, false, false);
2430         dsi_set_ta_timeout(0x1fff, true, true);
2431         dsi_set_lp_rx_timeout(0x1fff, true, true);
2432         dsi_set_hs_tx_timeout(0x1fff, true, true);
2433
2434         switch (dssdev->ctrl.pixel_size) {
2435         case 16:
2436                 buswidth = 0;
2437                 break;
2438         case 18:
2439                 buswidth = 1;
2440                 break;
2441         case 24:
2442                 buswidth = 2;
2443                 break;
2444         default:
2445                 BUG();
2446         }
2447
2448         r = dsi_read_reg(DSI_CTRL);
2449         r = FLD_MOD(r, 1, 1, 1);        /* CS_RX_EN */
2450         r = FLD_MOD(r, 1, 2, 2);        /* ECC_RX_EN */
2451         r = FLD_MOD(r, 1, 3, 3);        /* TX_FIFO_ARBITRATION */
2452         r = FLD_MOD(r, 1, 4, 4);        /* VP_CLK_RATIO, always 1, see errata*/
2453         r = FLD_MOD(r, buswidth, 7, 6); /* VP_DATA_BUS_WIDTH */
2454         r = FLD_MOD(r, 0, 8, 8);        /* VP_CLK_POL */
2455         r = FLD_MOD(r, 2, 13, 12);      /* LINE_BUFFER, 2 lines */
2456         r = FLD_MOD(r, 1, 14, 14);      /* TRIGGER_RESET_MODE */
2457         r = FLD_MOD(r, 1, 19, 19);      /* EOT_ENABLE */
2458         r = FLD_MOD(r, 1, 24, 24);      /* DCS_CMD_ENABLE */
2459         r = FLD_MOD(r, 0, 25, 25);      /* DCS_CMD_CODE, 1=start, 0=continue */
2460
2461         dsi_write_reg(DSI_CTRL, r);
2462
2463         dsi_vc_initial_config(0);
2464         dsi_vc_initial_config(1);
2465         dsi_vc_initial_config(2);
2466         dsi_vc_initial_config(3);
2467
2468         return 0;
2469 }
2470
2471 static void dsi_proto_timings(struct omap_dss_device *dssdev)
2472 {
2473         unsigned tlpx, tclk_zero, tclk_prepare, tclk_trail;
2474         unsigned tclk_pre, tclk_post;
2475         unsigned ths_prepare, ths_prepare_ths_zero, ths_zero;
2476         unsigned ths_trail, ths_exit;
2477         unsigned ddr_clk_pre, ddr_clk_post;
2478         unsigned enter_hs_mode_lat, exit_hs_mode_lat;
2479         unsigned ths_eot;
2480         u32 r;
2481
2482         r = dsi_read_reg(DSI_DSIPHY_CFG0);
2483         ths_prepare = FLD_GET(r, 31, 24);
2484         ths_prepare_ths_zero = FLD_GET(r, 23, 16);
2485         ths_zero = ths_prepare_ths_zero - ths_prepare;
2486         ths_trail = FLD_GET(r, 15, 8);
2487         ths_exit = FLD_GET(r, 7, 0);
2488
2489         r = dsi_read_reg(DSI_DSIPHY_CFG1);
2490         tlpx = FLD_GET(r, 22, 16) * 2;
2491         tclk_trail = FLD_GET(r, 15, 8);
2492         tclk_zero = FLD_GET(r, 7, 0);
2493
2494         r = dsi_read_reg(DSI_DSIPHY_CFG2);
2495         tclk_prepare = FLD_GET(r, 7, 0);
2496
2497         /* min 8*UI */
2498         tclk_pre = 20;
2499         /* min 60ns + 52*UI */
2500         tclk_post = ns2ddr(60) + 26;
2501
2502         /* ths_eot is 2 for 2 datalanes and 4 for 1 datalane */
2503         if (dssdev->phy.dsi.data1_lane != 0 &&
2504                         dssdev->phy.dsi.data2_lane != 0)
2505                 ths_eot = 2;
2506         else
2507                 ths_eot = 4;
2508
2509         ddr_clk_pre = DIV_ROUND_UP(tclk_pre + tlpx + tclk_zero + tclk_prepare,
2510                         4);
2511         ddr_clk_post = DIV_ROUND_UP(tclk_post + ths_trail, 4) + ths_eot;
2512
2513         BUG_ON(ddr_clk_pre == 0 || ddr_clk_pre > 255);
2514         BUG_ON(ddr_clk_post == 0 || ddr_clk_post > 255);
2515
2516         r = dsi_read_reg(DSI_CLK_TIMING);
2517         r = FLD_MOD(r, ddr_clk_pre, 15, 8);
2518         r = FLD_MOD(r, ddr_clk_post, 7, 0);
2519         dsi_write_reg(DSI_CLK_TIMING, r);
2520
2521         DSSDBG("ddr_clk_pre %u, ddr_clk_post %u\n",
2522                         ddr_clk_pre,
2523                         ddr_clk_post);
2524
2525         enter_hs_mode_lat = 1 + DIV_ROUND_UP(tlpx, 4) +
2526                 DIV_ROUND_UP(ths_prepare, 4) +
2527                 DIV_ROUND_UP(ths_zero + 3, 4);
2528
2529         exit_hs_mode_lat = DIV_ROUND_UP(ths_trail + ths_exit, 4) + 1 + ths_eot;
2530
2531         r = FLD_VAL(enter_hs_mode_lat, 31, 16) |
2532                 FLD_VAL(exit_hs_mode_lat, 15, 0);
2533         dsi_write_reg(DSI_VM_TIMING7, r);
2534
2535         DSSDBG("enter_hs_mode_lat %u, exit_hs_mode_lat %u\n",
2536                         enter_hs_mode_lat, exit_hs_mode_lat);
2537 }
2538
2539
2540 #define DSI_DECL_VARS \
2541         int __dsi_cb = 0; u32 __dsi_cv = 0;
2542
2543 #define DSI_FLUSH(ch) \
2544         if (__dsi_cb > 0) { \
2545                 /*DSSDBG("sending long packet %#010x\n", __dsi_cv);*/ \
2546                 dsi_write_reg(DSI_VC_LONG_PACKET_PAYLOAD(ch), __dsi_cv); \
2547                 __dsi_cb = __dsi_cv = 0; \
2548         }
2549
2550 #define DSI_PUSH(ch, data) \
2551         do { \
2552                 __dsi_cv |= (data) << (__dsi_cb * 8); \
2553                 /*DSSDBG("cv = %#010x, cb = %d\n", __dsi_cv, __dsi_cb);*/ \
2554                 if (++__dsi_cb > 3) \
2555                         DSI_FLUSH(ch); \
2556         } while (0)
2557
2558 static int dsi_update_screen_l4(struct omap_dss_device *dssdev,
2559                         int x, int y, int w, int h)
2560 {
2561         /* Note: supports only 24bit colors in 32bit container */
2562         int first = 1;
2563         int fifo_stalls = 0;
2564         int max_dsi_packet_size;
2565         int max_data_per_packet;
2566         int max_pixels_per_packet;
2567         int pixels_left;
2568         int bytespp = dssdev->ctrl.pixel_size / 8;
2569         int scr_width;
2570         u32 __iomem *data;
2571         int start_offset;
2572         int horiz_inc;
2573         int current_x;
2574         struct omap_overlay *ovl;
2575
2576         debug_irq = 0;
2577
2578         DSSDBG("dsi_update_screen_l4 (%d,%d %dx%d)\n",
2579                         x, y, w, h);
2580
2581         ovl = dssdev->manager->overlays[0];
2582
2583         if (ovl->info.color_mode != OMAP_DSS_COLOR_RGB24U)
2584                 return -EINVAL;
2585
2586         if (dssdev->ctrl.pixel_size != 24)
2587                 return -EINVAL;
2588
2589         scr_width = ovl->info.screen_width;
2590         data = ovl->info.vaddr;
2591
2592         start_offset = scr_width * y + x;
2593         horiz_inc = scr_width - w;
2594         current_x = x;
2595
2596         /* We need header(4) + DCSCMD(1) + pixels(numpix*bytespp) bytes
2597          * in fifo */
2598
2599         /* When using CPU, max long packet size is TX buffer size */
2600         max_dsi_packet_size = dsi.vc[0].fifo_size * 32 * 4;
2601
2602         /* we seem to get better perf if we divide the tx fifo to half,
2603            and while the other half is being sent, we fill the other half
2604            max_dsi_packet_size /= 2; */
2605
2606         max_data_per_packet = max_dsi_packet_size - 4 - 1;
2607
2608         max_pixels_per_packet = max_data_per_packet / bytespp;
2609
2610         DSSDBG("max_pixels_per_packet %d\n", max_pixels_per_packet);
2611
2612         pixels_left = w * h;
2613
2614         DSSDBG("total pixels %d\n", pixels_left);
2615
2616         data += start_offset;
2617
2618         while (pixels_left > 0) {
2619                 /* 0x2c = write_memory_start */
2620                 /* 0x3c = write_memory_continue */
2621                 u8 dcs_cmd = first ? 0x2c : 0x3c;
2622                 int pixels;
2623                 DSI_DECL_VARS;
2624                 first = 0;
2625
2626 #if 1
2627                 /* using fifo not empty */
2628                 /* TX_FIFO_NOT_EMPTY */
2629                 while (FLD_GET(dsi_read_reg(DSI_VC_CTRL(0)), 5, 5)) {
2630                         fifo_stalls++;
2631                         if (fifo_stalls > 0xfffff) {
2632                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2633                                                 pixels_left);
2634                                 dsi_if_enable(0);
2635                                 return -EIO;
2636                         }
2637                         udelay(1);
2638                 }
2639 #elif 1
2640                 /* using fifo emptiness */
2641                 while ((REG_GET(DSI_TX_FIFO_VC_EMPTINESS, 7, 0)+1)*4 <
2642                                 max_dsi_packet_size) {
2643                         fifo_stalls++;
2644                         if (fifo_stalls > 0xfffff) {
2645                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2646                                                pixels_left);
2647                                 dsi_if_enable(0);
2648                                 return -EIO;
2649                         }
2650                 }
2651 #else
2652                 while ((REG_GET(DSI_TX_FIFO_VC_EMPTINESS, 7, 0)+1)*4 == 0) {
2653                         fifo_stalls++;
2654                         if (fifo_stalls > 0xfffff) {
2655                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2656                                                pixels_left);
2657                                 dsi_if_enable(0);
2658                                 return -EIO;
2659                         }
2660                 }
2661 #endif
2662                 pixels = min(max_pixels_per_packet, pixels_left);
2663
2664                 pixels_left -= pixels;
2665
2666                 dsi_vc_write_long_header(0, DSI_DT_DCS_LONG_WRITE,
2667                                 1 + pixels * bytespp, 0);
2668
2669                 DSI_PUSH(0, dcs_cmd);
2670
2671                 while (pixels-- > 0) {
2672                         u32 pix = __raw_readl(data++);
2673
2674                         DSI_PUSH(0, (pix >> 16) & 0xff);
2675                         DSI_PUSH(0, (pix >> 8) & 0xff);
2676                         DSI_PUSH(0, (pix >> 0) & 0xff);
2677
2678                         current_x++;
2679                         if (current_x == x+w) {
2680                                 current_x = x;
2681                                 data += horiz_inc;
2682                         }
2683                 }
2684
2685                 DSI_FLUSH(0);
2686         }
2687
2688         return 0;
2689 }
2690
2691 static void dsi_update_screen_dispc(struct omap_dss_device *dssdev,
2692                 u16 x, u16 y, u16 w, u16 h)
2693 {
2694         unsigned bytespp;
2695         unsigned bytespl;
2696         unsigned bytespf;
2697         unsigned total_len;
2698         unsigned packet_payload;
2699         unsigned packet_len;
2700         u32 l;
2701         int r;
2702         const unsigned channel = dsi.update_channel;
2703         /* line buffer is 1024 x 24bits */
2704         /* XXX: for some reason using full buffer size causes considerable TX
2705          * slowdown with update sizes that fill the whole buffer */
2706         const unsigned line_buf_size = 1023 * 3;
2707
2708         DSSDBG("dsi_update_screen_dispc(%d,%d %dx%d)\n",
2709                         x, y, w, h);
2710
2711         dsi_vc_config_vp(channel);
2712
2713         bytespp = dssdev->ctrl.pixel_size / 8;
2714         bytespl = w * bytespp;
2715         bytespf = bytespl * h;
2716
2717         /* NOTE: packet_payload has to be equal to N * bytespl, where N is
2718          * number of lines in a packet.  See errata about VP_CLK_RATIO */
2719
2720         if (bytespf < line_buf_size)
2721                 packet_payload = bytespf;
2722         else
2723                 packet_payload = (line_buf_size) / bytespl * bytespl;
2724
2725         packet_len = packet_payload + 1;        /* 1 byte for DCS cmd */
2726         total_len = (bytespf / packet_payload) * packet_len;
2727
2728         if (bytespf % packet_payload)
2729                 total_len += (bytespf % packet_payload) + 1;
2730
2731         l = FLD_VAL(total_len, 23, 0); /* TE_SIZE */
2732         dsi_write_reg(DSI_VC_TE(channel), l);
2733
2734         dsi_vc_write_long_header(channel, DSI_DT_DCS_LONG_WRITE, packet_len, 0);
2735
2736         if (dsi.te_enabled)
2737                 l = FLD_MOD(l, 1, 30, 30); /* TE_EN */
2738         else
2739                 l = FLD_MOD(l, 1, 31, 31); /* TE_START */
2740         dsi_write_reg(DSI_VC_TE(channel), l);
2741
2742         /* We put SIDLEMODE to no-idle for the duration of the transfer,
2743          * because DSS interrupts are not capable of waking up the CPU and the
2744          * framedone interrupt could be delayed for quite a long time. I think
2745          * the same goes for any DSS interrupts, but for some reason I have not
2746          * seen the problem anywhere else than here.
2747          */
2748         dispc_disable_sidle();
2749
2750         dsi_perf_mark_start();
2751
2752         r = queue_delayed_work(dsi.workqueue, &dsi.framedone_timeout_work,
2753                         msecs_to_jiffies(250));
2754         BUG_ON(r == 0);
2755
2756         dss_start_update(dssdev);
2757
2758         if (dsi.te_enabled) {
2759                 /* disable LP_RX_TO, so that we can receive TE.  Time to wait
2760                  * for TE is longer than the timer allows */
2761                 REG_FLD_MOD(DSI_TIMING2, 0, 15, 15); /* LP_RX_TO */
2762
2763                 dsi_vc_send_bta(channel);
2764
2765 #ifdef DSI_CATCH_MISSING_TE
2766                 mod_timer(&dsi.te_timer, jiffies + msecs_to_jiffies(250));
2767 #endif
2768         }
2769 }
2770
2771 #ifdef DSI_CATCH_MISSING_TE
2772 static void dsi_te_timeout(unsigned long arg)
2773 {
2774         DSSERR("TE not received for 250ms!\n");
2775 }
2776 #endif
2777
2778 static void dsi_handle_framedone(int error)
2779 {
2780         const int channel = dsi.update_channel;
2781
2782         cancel_delayed_work(&dsi.framedone_timeout_work);
2783
2784         dsi_vc_disable_bta_irq(channel);
2785
2786         /* SIDLEMODE back to smart-idle */
2787         dispc_enable_sidle();
2788
2789         dsi.bta_callback = NULL;
2790
2791         if (dsi.te_enabled) {
2792                 /* enable LP_RX_TO again after the TE */
2793                 REG_FLD_MOD(DSI_TIMING2, 1, 15, 15); /* LP_RX_TO */
2794         }
2795
2796         /* RX_FIFO_NOT_EMPTY */
2797         if (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {
2798                 DSSERR("Received error during frame transfer:\n");
2799                 dsi_vc_flush_receive_data(channel);
2800                 if (!error)
2801                         error = -EIO;
2802         }
2803
2804         dsi.framedone_callback(error, dsi.framedone_data);
2805
2806         if (!error)
2807                 dsi_perf_show("DISPC");
2808 }
2809
2810 static void dsi_framedone_timeout_work_callback(struct work_struct *work)
2811 {
2812         /* XXX While extremely unlikely, we could get FRAMEDONE interrupt after
2813          * 250ms which would conflict with this timeout work. What should be
2814          * done is first cancel the transfer on the HW, and then cancel the
2815          * possibly scheduled framedone work. However, cancelling the transfer
2816          * on the HW is buggy, and would probably require resetting the whole
2817          * DSI */
2818
2819         DSSERR("Framedone not received for 250ms!\n");
2820
2821         dsi_handle_framedone(-ETIMEDOUT);
2822 }
2823
2824 static void dsi_framedone_bta_callback(void)
2825 {
2826         dsi_handle_framedone(0);
2827
2828 #ifdef CONFIG_OMAP2_DSS_FAKE_VSYNC
2829         dispc_fake_vsync_irq();
2830 #endif
2831 }
2832
2833 static void dsi_framedone_irq_callback(void *data, u32 mask)
2834 {
2835         const int channel = dsi.update_channel;
2836         int r;
2837
2838         /* Note: We get FRAMEDONE when DISPC has finished sending pixels and
2839          * turns itself off. However, DSI still has the pixels in its buffers,
2840          * and is sending the data.
2841          */
2842
2843         if (dsi.te_enabled) {
2844                 /* enable LP_RX_TO again after the TE */
2845                 REG_FLD_MOD(DSI_TIMING2, 1, 15, 15); /* LP_RX_TO */
2846         }
2847
2848         /* Send BTA after the frame. We need this for the TE to work, as TE
2849          * trigger is only sent for BTAs without preceding packet. Thus we need
2850          * to BTA after the pixel packets so that next BTA will cause TE
2851          * trigger.
2852          *
2853          * This is not needed when TE is not in use, but we do it anyway to
2854          * make sure that the transfer has been completed. It would be more
2855          * optimal, but more complex, to wait only just before starting next
2856          * transfer.
2857          *
2858          * Also, as there's no interrupt telling when the transfer has been
2859          * done and the channel could be reconfigured, the only way is to
2860          * busyloop until TE_SIZE is zero. With BTA we can do this
2861          * asynchronously.
2862          * */
2863
2864         dsi.bta_callback = dsi_framedone_bta_callback;
2865
2866         barrier();
2867
2868         dsi_vc_enable_bta_irq(channel);
2869
2870         r = dsi_vc_send_bta(channel);
2871         if (r) {
2872                 DSSERR("BTA after framedone failed\n");
2873                 dsi_handle_framedone(-EIO);
2874         }
2875 }
2876
2877 int omap_dsi_prepare_update(struct omap_dss_device *dssdev,
2878                                     u16 *x, u16 *y, u16 *w, u16 *h,
2879                                     bool enlarge_update_area)
2880 {
2881         u16 dw, dh;
2882
2883         dssdev->driver->get_resolution(dssdev, &dw, &dh);
2884
2885         if  (*x > dw || *y > dh)
2886                 return -EINVAL;
2887
2888         if (*x + *w > dw)
2889                 return -EINVAL;
2890
2891         if (*y + *h > dh)
2892                 return -EINVAL;
2893
2894         if (*w == 1)
2895                 return -EINVAL;
2896
2897         if (*w == 0 || *h == 0)
2898                 return -EINVAL;
2899
2900         dsi_perf_mark_setup();
2901
2902         if (dssdev->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
2903                 dss_setup_partial_planes(dssdev, x, y, w, h,
2904                                 enlarge_update_area);
2905                 dispc_set_lcd_size(dssdev->manager->id, *w, *h);
2906         }
2907
2908         return 0;
2909 }
2910 EXPORT_SYMBOL(omap_dsi_prepare_update);
2911
2912 int omap_dsi_update(struct omap_dss_device *dssdev,
2913                 int channel,
2914                 u16 x, u16 y, u16 w, u16 h,
2915                 void (*callback)(int, void *), void *data)
2916 {
2917         dsi.update_channel = channel;
2918
2919         /* OMAP DSS cannot send updates of odd widths.
2920          * omap_dsi_prepare_update() makes the widths even, but add a BUG_ON
2921          * here to make sure we catch erroneous updates. Otherwise we'll only
2922          * see rather obscure HW error happening, as DSS halts. */
2923         BUG_ON(x % 2 == 1);
2924
2925         if (dssdev->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
2926                 dsi.framedone_callback = callback;
2927                 dsi.framedone_data = data;
2928
2929                 dsi.update_region.x = x;
2930                 dsi.update_region.y = y;
2931                 dsi.update_region.w = w;
2932                 dsi.update_region.h = h;
2933                 dsi.update_region.device = dssdev;
2934
2935                 dsi_update_screen_dispc(dssdev, x, y, w, h);
2936         } else {
2937                 int r;
2938
2939                 r = dsi_update_screen_l4(dssdev, x, y, w, h);
2940                 if (r)
2941                         return r;
2942
2943                 dsi_perf_show("L4");
2944                 callback(0, data);
2945         }
2946
2947         return 0;
2948 }
2949 EXPORT_SYMBOL(omap_dsi_update);
2950
2951 /* Display funcs */
2952
2953 static int dsi_display_init_dispc(struct omap_dss_device *dssdev)
2954 {
2955         int r;
2956
2957         r = omap_dispc_register_isr(dsi_framedone_irq_callback, NULL,
2958                         DISPC_IRQ_FRAMEDONE);
2959         if (r) {
2960                 DSSERR("can't get FRAMEDONE irq\n");
2961                 return r;
2962         }
2963
2964         dispc_set_lcd_display_type(dssdev->manager->id,
2965                         OMAP_DSS_LCD_DISPLAY_TFT);
2966
2967         dispc_set_parallel_interface_mode(dssdev->manager->id,
2968                         OMAP_DSS_PARALLELMODE_DSI);
2969         dispc_enable_fifohandcheck(dssdev->manager->id, 1);
2970
2971         dispc_set_tft_data_lines(dssdev->manager->id, dssdev->ctrl.pixel_size);
2972
2973         {
2974                 struct omap_video_timings timings = {
2975                         .hsw            = 1,
2976                         .hfp            = 1,
2977                         .hbp            = 1,
2978                         .vsw            = 1,
2979                         .vfp            = 0,
2980                         .vbp            = 0,
2981                 };
2982
2983                 dispc_set_lcd_timings(dssdev->manager->id, &timings);
2984         }
2985
2986         return 0;
2987 }
2988
2989 static void dsi_display_uninit_dispc(struct omap_dss_device *dssdev)
2990 {
2991         omap_dispc_unregister_isr(dsi_framedone_irq_callback, NULL,
2992                         DISPC_IRQ_FRAMEDONE);
2993 }
2994
2995 static int dsi_configure_dsi_clocks(struct omap_dss_device *dssdev)
2996 {
2997         struct dsi_clock_info cinfo;
2998         int r;
2999
3000         /* we always use DSS2_FCK as input clock */
3001         cinfo.use_dss2_fck = true;
3002         cinfo.regn  = dssdev->phy.dsi.div.regn;
3003         cinfo.regm  = dssdev->phy.dsi.div.regm;
3004         cinfo.regm3 = dssdev->phy.dsi.div.regm3;
3005         cinfo.regm4 = dssdev->phy.dsi.div.regm4;
3006         r = dsi_calc_clock_rates(dssdev, &cinfo);
3007         if (r) {
3008                 DSSERR("Failed to calc dsi clocks\n");
3009                 return r;
3010         }
3011
3012         r = dsi_pll_set_clock_div(&cinfo);
3013         if (r) {
3014                 DSSERR("Failed to set dsi clocks\n");
3015                 return r;
3016         }
3017
3018         return 0;
3019 }
3020
3021 static int dsi_configure_dispc_clocks(struct omap_dss_device *dssdev)
3022 {
3023         struct dispc_clock_info dispc_cinfo;
3024         int r;
3025         unsigned long long fck;
3026
3027         fck = dsi_get_dsi1_pll_rate();
3028
3029         dispc_cinfo.lck_div = dssdev->phy.dsi.div.lck_div;
3030         dispc_cinfo.pck_div = dssdev->phy.dsi.div.pck_div;
3031
3032         r = dispc_calc_clock_rates(fck, &dispc_cinfo);
3033         if (r) {
3034                 DSSERR("Failed to calc dispc clocks\n");
3035                 return r;
3036         }
3037
3038         r = dispc_set_clock_div(dssdev->manager->id, &dispc_cinfo);
3039         if (r) {
3040                 DSSERR("Failed to set dispc clocks\n");
3041                 return r;
3042         }
3043
3044         return 0;
3045 }
3046
3047 static int dsi_display_init_dsi(struct omap_dss_device *dssdev)
3048 {
3049         int r;
3050
3051         _dsi_print_reset_status();
3052
3053         r = dsi_pll_init(dssdev, true, true);
3054         if (r)
3055                 goto err0;
3056
3057         r = dsi_configure_dsi_clocks(dssdev);
3058         if (r)
3059                 goto err1;
3060
3061         dss_select_dispc_clk_source(DSS_SRC_DSI1_PLL_FCLK);
3062         dss_select_dsi_clk_source(DSS_SRC_DSI2_PLL_FCLK);
3063
3064         DSSDBG("PLL OK\n");
3065
3066         r = dsi_configure_dispc_clocks(dssdev);
3067         if (r)
3068                 goto err2;
3069
3070         r = dsi_complexio_init(dssdev);
3071         if (r)
3072                 goto err2;
3073
3074         _dsi_print_reset_status();
3075
3076         dsi_proto_timings(dssdev);
3077         dsi_set_lp_clk_divisor(dssdev);
3078
3079         if (1)
3080                 _dsi_print_reset_status();
3081
3082         r = dsi_proto_config(dssdev);
3083         if (r)
3084                 goto err3;
3085
3086         /* enable interface */
3087         dsi_vc_enable(0, 1);
3088         dsi_vc_enable(1, 1);
3089         dsi_vc_enable(2, 1);
3090         dsi_vc_enable(3, 1);
3091         dsi_if_enable(1);
3092         dsi_force_tx_stop_mode_io();
3093
3094         return 0;
3095 err3:
3096         dsi_complexio_uninit();
3097 err2:
3098         dss_select_dispc_clk_source(DSS_SRC_DSS1_ALWON_FCLK);
3099         dss_select_dsi_clk_source(DSS_SRC_DSS1_ALWON_FCLK);
3100 err1:
3101         dsi_pll_uninit();
3102 err0:
3103         return r;
3104 }
3105
3106 static void dsi_display_uninit_dsi(struct omap_dss_device *dssdev)
3107 {
3108         /* disable interface */
3109         dsi_if_enable(0);
3110         dsi_vc_enable(0, 0);
3111         dsi_vc_enable(1, 0);
3112         dsi_vc_enable(2, 0);
3113         dsi_vc_enable(3, 0);
3114
3115         dss_select_dispc_clk_source(DSS_SRC_DSS1_ALWON_FCLK);
3116         dss_select_dsi_clk_source(DSS_SRC_DSS1_ALWON_FCLK);
3117         dsi_complexio_uninit();
3118         dsi_pll_uninit();
3119 }
3120
3121 static int dsi_core_init(void)
3122 {
3123         /* Autoidle */
3124         REG_FLD_MOD(DSI_SYSCONFIG, 1, 0, 0);
3125
3126         /* ENWAKEUP */
3127         REG_FLD_MOD(DSI_SYSCONFIG, 1, 2, 2);
3128
3129         /* SIDLEMODE smart-idle */
3130         REG_FLD_MOD(DSI_SYSCONFIG, 2, 4, 3);
3131
3132         _dsi_initialize_irq();
3133
3134         return 0;
3135 }
3136
3137 int omapdss_dsi_display_enable(struct omap_dss_device *dssdev)
3138 {
3139         int r = 0;
3140
3141         DSSDBG("dsi_display_enable\n");
3142
3143         WARN_ON(!dsi_bus_is_locked());
3144
3145         mutex_lock(&dsi.lock);
3146
3147         r = omap_dss_start_device(dssdev);
3148         if (r) {
3149                 DSSERR("failed to start device\n");
3150                 goto err0;
3151         }
3152
3153         enable_clocks(1);
3154         dsi_enable_pll_clock(1);
3155
3156         r = _dsi_reset();
3157         if (r)
3158                 goto err1;
3159
3160         dsi_core_init();
3161
3162         r = dsi_display_init_dispc(dssdev);
3163         if (r)
3164                 goto err1;
3165
3166         r = dsi_display_init_dsi(dssdev);
3167         if (r)
3168                 goto err2;
3169
3170         mutex_unlock(&dsi.lock);
3171
3172         return 0;
3173
3174 err2:
3175         dsi_display_uninit_dispc(dssdev);
3176 err1:
3177         enable_clocks(0);
3178         dsi_enable_pll_clock(0);
3179         omap_dss_stop_device(dssdev);
3180 err0:
3181         mutex_unlock(&dsi.lock);
3182         DSSDBG("dsi_display_enable FAILED\n");
3183         return r;
3184 }
3185 EXPORT_SYMBOL(omapdss_dsi_display_enable);
3186
3187 void omapdss_dsi_display_disable(struct omap_dss_device *dssdev)
3188 {
3189         DSSDBG("dsi_display_disable\n");
3190
3191         WARN_ON(!dsi_bus_is_locked());
3192
3193         mutex_lock(&dsi.lock);
3194
3195         dsi_display_uninit_dispc(dssdev);
3196
3197         dsi_display_uninit_dsi(dssdev);
3198
3199         enable_clocks(0);
3200         dsi_enable_pll_clock(0);
3201
3202         omap_dss_stop_device(dssdev);
3203
3204         mutex_unlock(&dsi.lock);
3205 }
3206 EXPORT_SYMBOL(omapdss_dsi_display_disable);
3207
3208 int omapdss_dsi_enable_te(struct omap_dss_device *dssdev, bool enable)
3209 {
3210         dsi.te_enabled = enable;
3211         return 0;
3212 }
3213 EXPORT_SYMBOL(omapdss_dsi_enable_te);
3214
3215 void dsi_get_overlay_fifo_thresholds(enum omap_plane plane,
3216                 u32 fifo_size, enum omap_burst_size *burst_size,
3217                 u32 *fifo_low, u32 *fifo_high)
3218 {
3219         unsigned burst_size_bytes;
3220
3221         *burst_size = OMAP_DSS_BURST_16x32;
3222         burst_size_bytes = 16 * 32 / 8;
3223
3224         *fifo_high = fifo_size - burst_size_bytes;
3225         *fifo_low = fifo_size - burst_size_bytes * 2;
3226 }
3227
3228 int dsi_init_display(struct omap_dss_device *dssdev)
3229 {
3230         DSSDBG("DSI init\n");
3231
3232         /* XXX these should be figured out dynamically */
3233         dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
3234                 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
3235
3236         dsi.vc[0].dssdev = dssdev;
3237         dsi.vc[1].dssdev = dssdev;
3238
3239         return 0;
3240 }
3241
3242 void dsi_wait_dsi1_pll_active(void)
3243 {
3244         if (wait_for_bit_change(DSI_PLL_STATUS, 7, 1) != 1)
3245                 DSSERR("DSI1 PLL clock not active\n");
3246 }
3247
3248 void dsi_wait_dsi2_pll_active(void)
3249 {
3250         if (wait_for_bit_change(DSI_PLL_STATUS, 8, 1) != 1)
3251                 DSSERR("DSI2 PLL clock not active\n");
3252 }
3253
3254 static int dsi_init(struct platform_device *pdev)
3255 {
3256         u32 rev;
3257         int r;
3258         struct resource *dsi_mem;
3259
3260         spin_lock_init(&dsi.errors_lock);
3261         dsi.errors = 0;
3262
3263 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
3264         spin_lock_init(&dsi.irq_stats_lock);
3265         dsi.irq_stats.last_reset = jiffies;
3266 #endif
3267
3268         init_completion(&dsi.bta_completion);
3269
3270         mutex_init(&dsi.lock);
3271         sema_init(&dsi.bus_lock, 1);
3272
3273         dsi.workqueue = create_singlethread_workqueue("dsi");
3274         if (dsi.workqueue == NULL)
3275                 return -ENOMEM;
3276
3277         INIT_DELAYED_WORK_DEFERRABLE(&dsi.framedone_timeout_work,
3278                         dsi_framedone_timeout_work_callback);
3279
3280 #ifdef DSI_CATCH_MISSING_TE
3281         init_timer(&dsi.te_timer);
3282         dsi.te_timer.function = dsi_te_timeout;
3283         dsi.te_timer.data = 0;
3284 #endif
3285         dsi_mem = platform_get_resource(dsi.pdev, IORESOURCE_MEM, 0);
3286         if (!dsi_mem) {
3287                 DSSERR("can't get IORESOURCE_MEM DSI\n");
3288                 r = -EINVAL;
3289                 goto err1;
3290         }
3291         dsi.base = ioremap(dsi_mem->start, resource_size(dsi_mem));
3292         if (!dsi.base) {
3293                 DSSERR("can't ioremap DSI\n");
3294                 r = -ENOMEM;
3295                 goto err1;
3296         }
3297
3298         dsi.vdds_dsi_reg = dsi_get_vdds_dsi();
3299         if (IS_ERR(dsi.vdds_dsi_reg)) {
3300                 DSSERR("can't get VDDS_DSI regulator\n");
3301                 r = PTR_ERR(dsi.vdds_dsi_reg);
3302                 goto err2;
3303         }
3304
3305         enable_clocks(1);
3306
3307         rev = dsi_read_reg(DSI_REVISION);
3308         dev_dbg(&pdev->dev, "OMAP DSI rev %d.%d\n",
3309                FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
3310
3311         enable_clocks(0);
3312
3313         return 0;
3314 err2:
3315         iounmap(dsi.base);
3316 err1:
3317         destroy_workqueue(dsi.workqueue);
3318         return r;
3319 }
3320
3321 static void dsi_exit(void)
3322 {
3323         if (dsi.vdds_dsi_reg != NULL) {
3324                 regulator_put(dsi.vdds_dsi_reg);
3325                 dsi.vdds_dsi_reg = NULL;
3326         }
3327
3328         iounmap(dsi.base);
3329
3330         destroy_workqueue(dsi.workqueue);
3331
3332         DSSDBG("omap_dsi_exit\n");
3333 }
3334
3335 /* DSI1 HW IP initialisation */
3336 static int omap_dsi1hw_probe(struct platform_device *pdev)
3337 {
3338         int r;
3339         dsi.pdev = pdev;
3340         r = dsi_init(pdev);
3341         if (r) {
3342                 DSSERR("Failed to initialize DSI\n");
3343                 goto err_dsi;
3344         }
3345 err_dsi:
3346         return r;
3347 }
3348
3349 static int omap_dsi1hw_remove(struct platform_device *pdev)
3350 {
3351         dsi_exit();
3352         return 0;
3353 }
3354
3355 static struct platform_driver omap_dsi1hw_driver = {
3356         .probe          = omap_dsi1hw_probe,
3357         .remove         = omap_dsi1hw_remove,
3358         .driver         = {
3359                 .name   = "omapdss_dsi1",
3360                 .owner  = THIS_MODULE,
3361         },
3362 };
3363
3364 int dsi_init_platform_driver(void)
3365 {
3366         return platform_driver_register(&omap_dsi1hw_driver);
3367 }
3368
3369 void dsi_uninit_platform_driver(void)
3370 {
3371         return platform_driver_unregister(&omap_dsi1hw_driver);
3372 }