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