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