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