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