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