85ec3d63a897f0d7c1e3bcf9682494d8e2796544
[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         seq_printf(s,   "VP_CLK\t\t%lu\n"
1730                         "VP_PCLK\t\t%lu\n",
1731                         dispc_lclk_rate(OMAP_DSS_CHANNEL_LCD),
1732                         dispc_pclk_rate(OMAP_DSS_CHANNEL_LCD));
1733
1734         enable_clocks(0);
1735 }
1736
1737 void dsi_dump_clocks(struct seq_file *s)
1738 {
1739         struct platform_device *dsidev;
1740         int i;
1741
1742         for  (i = 0; i < MAX_NUM_DSI; i++) {
1743                 dsidev = dsi_get_dsidev_from_id(i);
1744                 if (dsidev)
1745                         dsi_dump_dsidev_clocks(dsidev, s);
1746         }
1747 }
1748
1749 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
1750 static void dsi_dump_dsidev_irqs(struct platform_device *dsidev,
1751                 struct seq_file *s)
1752 {
1753         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1754         unsigned long flags;
1755         struct dsi_irq_stats stats;
1756         int dsi_module = dsi_get_dsidev_id(dsidev);
1757
1758         spin_lock_irqsave(&dsi->irq_stats_lock, flags);
1759
1760         stats = dsi->irq_stats;
1761         memset(&dsi->irq_stats, 0, sizeof(dsi->irq_stats));
1762         dsi->irq_stats.last_reset = jiffies;
1763
1764         spin_unlock_irqrestore(&dsi->irq_stats_lock, flags);
1765
1766         seq_printf(s, "period %u ms\n",
1767                         jiffies_to_msecs(jiffies - stats.last_reset));
1768
1769         seq_printf(s, "irqs %d\n", stats.irq_count);
1770 #define PIS(x) \
1771         seq_printf(s, "%-20s %10d\n", #x, stats.dsi_irqs[ffs(DSI_IRQ_##x)-1]);
1772
1773         seq_printf(s, "-- DSI%d interrupts --\n", dsi_module + 1);
1774         PIS(VC0);
1775         PIS(VC1);
1776         PIS(VC2);
1777         PIS(VC3);
1778         PIS(WAKEUP);
1779         PIS(RESYNC);
1780         PIS(PLL_LOCK);
1781         PIS(PLL_UNLOCK);
1782         PIS(PLL_RECALL);
1783         PIS(COMPLEXIO_ERR);
1784         PIS(HS_TX_TIMEOUT);
1785         PIS(LP_RX_TIMEOUT);
1786         PIS(TE_TRIGGER);
1787         PIS(ACK_TRIGGER);
1788         PIS(SYNC_LOST);
1789         PIS(LDO_POWER_GOOD);
1790         PIS(TA_TIMEOUT);
1791 #undef PIS
1792
1793 #define PIS(x) \
1794         seq_printf(s, "%-20s %10d %10d %10d %10d\n", #x, \
1795                         stats.vc_irqs[0][ffs(DSI_VC_IRQ_##x)-1], \
1796                         stats.vc_irqs[1][ffs(DSI_VC_IRQ_##x)-1], \
1797                         stats.vc_irqs[2][ffs(DSI_VC_IRQ_##x)-1], \
1798                         stats.vc_irqs[3][ffs(DSI_VC_IRQ_##x)-1]);
1799
1800         seq_printf(s, "-- VC interrupts --\n");
1801         PIS(CS);
1802         PIS(ECC_CORR);
1803         PIS(PACKET_SENT);
1804         PIS(FIFO_TX_OVF);
1805         PIS(FIFO_RX_OVF);
1806         PIS(BTA);
1807         PIS(ECC_NO_CORR);
1808         PIS(FIFO_TX_UDF);
1809         PIS(PP_BUSY_CHANGE);
1810 #undef PIS
1811
1812 #define PIS(x) \
1813         seq_printf(s, "%-20s %10d\n", #x, \
1814                         stats.cio_irqs[ffs(DSI_CIO_IRQ_##x)-1]);
1815
1816         seq_printf(s, "-- CIO interrupts --\n");
1817         PIS(ERRSYNCESC1);
1818         PIS(ERRSYNCESC2);
1819         PIS(ERRSYNCESC3);
1820         PIS(ERRESC1);
1821         PIS(ERRESC2);
1822         PIS(ERRESC3);
1823         PIS(ERRCONTROL1);
1824         PIS(ERRCONTROL2);
1825         PIS(ERRCONTROL3);
1826         PIS(STATEULPS1);
1827         PIS(STATEULPS2);
1828         PIS(STATEULPS3);
1829         PIS(ERRCONTENTIONLP0_1);
1830         PIS(ERRCONTENTIONLP1_1);
1831         PIS(ERRCONTENTIONLP0_2);
1832         PIS(ERRCONTENTIONLP1_2);
1833         PIS(ERRCONTENTIONLP0_3);
1834         PIS(ERRCONTENTIONLP1_3);
1835         PIS(ULPSACTIVENOT_ALL0);
1836         PIS(ULPSACTIVENOT_ALL1);
1837 #undef PIS
1838 }
1839
1840 static void dsi1_dump_irqs(struct seq_file *s)
1841 {
1842         struct platform_device *dsidev = dsi_get_dsidev_from_id(0);
1843
1844         dsi_dump_dsidev_irqs(dsidev, s);
1845 }
1846
1847 static void dsi2_dump_irqs(struct seq_file *s)
1848 {
1849         struct platform_device *dsidev = dsi_get_dsidev_from_id(1);
1850
1851         dsi_dump_dsidev_irqs(dsidev, s);
1852 }
1853
1854 void dsi_create_debugfs_files_irq(struct dentry *debugfs_dir,
1855                 const struct file_operations *debug_fops)
1856 {
1857         struct platform_device *dsidev;
1858
1859         dsidev = dsi_get_dsidev_from_id(0);
1860         if (dsidev)
1861                 debugfs_create_file("dsi1_irqs", S_IRUGO, debugfs_dir,
1862                         &dsi1_dump_irqs, debug_fops);
1863
1864         dsidev = dsi_get_dsidev_from_id(1);
1865         if (dsidev)
1866                 debugfs_create_file("dsi2_irqs", S_IRUGO, debugfs_dir,
1867                         &dsi2_dump_irqs, debug_fops);
1868 }
1869 #endif
1870
1871 static void dsi_dump_dsidev_regs(struct platform_device *dsidev,
1872                 struct seq_file *s)
1873 {
1874 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dsi_read_reg(dsidev, r))
1875
1876         dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK);
1877         dsi_enable_scp_clk(dsidev);
1878
1879         DUMPREG(DSI_REVISION);
1880         DUMPREG(DSI_SYSCONFIG);
1881         DUMPREG(DSI_SYSSTATUS);
1882         DUMPREG(DSI_IRQSTATUS);
1883         DUMPREG(DSI_IRQENABLE);
1884         DUMPREG(DSI_CTRL);
1885         DUMPREG(DSI_COMPLEXIO_CFG1);
1886         DUMPREG(DSI_COMPLEXIO_IRQ_STATUS);
1887         DUMPREG(DSI_COMPLEXIO_IRQ_ENABLE);
1888         DUMPREG(DSI_CLK_CTRL);
1889         DUMPREG(DSI_TIMING1);
1890         DUMPREG(DSI_TIMING2);
1891         DUMPREG(DSI_VM_TIMING1);
1892         DUMPREG(DSI_VM_TIMING2);
1893         DUMPREG(DSI_VM_TIMING3);
1894         DUMPREG(DSI_CLK_TIMING);
1895         DUMPREG(DSI_TX_FIFO_VC_SIZE);
1896         DUMPREG(DSI_RX_FIFO_VC_SIZE);
1897         DUMPREG(DSI_COMPLEXIO_CFG2);
1898         DUMPREG(DSI_RX_FIFO_VC_FULLNESS);
1899         DUMPREG(DSI_VM_TIMING4);
1900         DUMPREG(DSI_TX_FIFO_VC_EMPTINESS);
1901         DUMPREG(DSI_VM_TIMING5);
1902         DUMPREG(DSI_VM_TIMING6);
1903         DUMPREG(DSI_VM_TIMING7);
1904         DUMPREG(DSI_STOPCLK_TIMING);
1905
1906         DUMPREG(DSI_VC_CTRL(0));
1907         DUMPREG(DSI_VC_TE(0));
1908         DUMPREG(DSI_VC_LONG_PACKET_HEADER(0));
1909         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(0));
1910         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(0));
1911         DUMPREG(DSI_VC_IRQSTATUS(0));
1912         DUMPREG(DSI_VC_IRQENABLE(0));
1913
1914         DUMPREG(DSI_VC_CTRL(1));
1915         DUMPREG(DSI_VC_TE(1));
1916         DUMPREG(DSI_VC_LONG_PACKET_HEADER(1));
1917         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(1));
1918         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(1));
1919         DUMPREG(DSI_VC_IRQSTATUS(1));
1920         DUMPREG(DSI_VC_IRQENABLE(1));
1921
1922         DUMPREG(DSI_VC_CTRL(2));
1923         DUMPREG(DSI_VC_TE(2));
1924         DUMPREG(DSI_VC_LONG_PACKET_HEADER(2));
1925         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(2));
1926         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(2));
1927         DUMPREG(DSI_VC_IRQSTATUS(2));
1928         DUMPREG(DSI_VC_IRQENABLE(2));
1929
1930         DUMPREG(DSI_VC_CTRL(3));
1931         DUMPREG(DSI_VC_TE(3));
1932         DUMPREG(DSI_VC_LONG_PACKET_HEADER(3));
1933         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(3));
1934         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(3));
1935         DUMPREG(DSI_VC_IRQSTATUS(3));
1936         DUMPREG(DSI_VC_IRQENABLE(3));
1937
1938         DUMPREG(DSI_DSIPHY_CFG0);
1939         DUMPREG(DSI_DSIPHY_CFG1);
1940         DUMPREG(DSI_DSIPHY_CFG2);
1941         DUMPREG(DSI_DSIPHY_CFG5);
1942
1943         DUMPREG(DSI_PLL_CONTROL);
1944         DUMPREG(DSI_PLL_STATUS);
1945         DUMPREG(DSI_PLL_GO);
1946         DUMPREG(DSI_PLL_CONFIGURATION1);
1947         DUMPREG(DSI_PLL_CONFIGURATION2);
1948
1949         dsi_disable_scp_clk(dsidev);
1950         dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK);
1951 #undef DUMPREG
1952 }
1953
1954 static void dsi1_dump_regs(struct seq_file *s)
1955 {
1956         struct platform_device *dsidev = dsi_get_dsidev_from_id(0);
1957
1958         dsi_dump_dsidev_regs(dsidev, s);
1959 }
1960
1961 static void dsi2_dump_regs(struct seq_file *s)
1962 {
1963         struct platform_device *dsidev = dsi_get_dsidev_from_id(1);
1964
1965         dsi_dump_dsidev_regs(dsidev, s);
1966 }
1967
1968 void dsi_create_debugfs_files_reg(struct dentry *debugfs_dir,
1969                 const struct file_operations *debug_fops)
1970 {
1971         struct platform_device *dsidev;
1972
1973         dsidev = dsi_get_dsidev_from_id(0);
1974         if (dsidev)
1975                 debugfs_create_file("dsi1_regs", S_IRUGO, debugfs_dir,
1976                         &dsi1_dump_regs, debug_fops);
1977
1978         dsidev = dsi_get_dsidev_from_id(1);
1979         if (dsidev)
1980                 debugfs_create_file("dsi2_regs", S_IRUGO, debugfs_dir,
1981                         &dsi2_dump_regs, debug_fops);
1982 }
1983 enum dsi_cio_power_state {
1984         DSI_COMPLEXIO_POWER_OFF         = 0x0,
1985         DSI_COMPLEXIO_POWER_ON          = 0x1,
1986         DSI_COMPLEXIO_POWER_ULPS        = 0x2,
1987 };
1988
1989 static int dsi_cio_power(struct platform_device *dsidev,
1990                 enum dsi_cio_power_state state)
1991 {
1992         int t = 0;
1993
1994         /* PWR_CMD */
1995         REG_FLD_MOD(dsidev, DSI_COMPLEXIO_CFG1, state, 28, 27);
1996
1997         /* PWR_STATUS */
1998         while (FLD_GET(dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG1),
1999                         26, 25) != state) {
2000                 if (++t > 1000) {
2001                         DSSERR("failed to set complexio power state to "
2002                                         "%d\n", state);
2003                         return -ENODEV;
2004                 }
2005                 udelay(1);
2006         }
2007
2008         return 0;
2009 }
2010
2011 static void dsi_set_lane_config(struct omap_dss_device *dssdev)
2012 {
2013         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2014         u32 r;
2015
2016         int clk_lane   = dssdev->phy.dsi.clk_lane;
2017         int data1_lane = dssdev->phy.dsi.data1_lane;
2018         int data2_lane = dssdev->phy.dsi.data2_lane;
2019         int clk_pol    = dssdev->phy.dsi.clk_pol;
2020         int data1_pol  = dssdev->phy.dsi.data1_pol;
2021         int data2_pol  = dssdev->phy.dsi.data2_pol;
2022
2023         r = dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG1);
2024         r = FLD_MOD(r, clk_lane, 2, 0);
2025         r = FLD_MOD(r, clk_pol, 3, 3);
2026         r = FLD_MOD(r, data1_lane, 6, 4);
2027         r = FLD_MOD(r, data1_pol, 7, 7);
2028         r = FLD_MOD(r, data2_lane, 10, 8);
2029         r = FLD_MOD(r, data2_pol, 11, 11);
2030         dsi_write_reg(dsidev, DSI_COMPLEXIO_CFG1, r);
2031
2032         /* The configuration of the DSI complex I/O (number of data lanes,
2033            position, differential order) should not be changed while
2034            DSS.DSI_CLK_CRTRL[20] LP_CLK_ENABLE bit is set to 1. In order for
2035            the hardware to take into account a new configuration of the complex
2036            I/O (done in DSS.DSI_COMPLEXIO_CFG1 register), it is recommended to
2037            follow this sequence: First set the DSS.DSI_CTRL[0] IF_EN bit to 1,
2038            then reset the DSS.DSI_CTRL[0] IF_EN to 0, then set
2039            DSS.DSI_CLK_CTRL[20] LP_CLK_ENABLE to 1 and finally set again the
2040            DSS.DSI_CTRL[0] IF_EN bit to 1. If the sequence is not followed, the
2041            DSI complex I/O configuration is unknown. */
2042
2043         /*
2044         REG_FLD_MOD(dsidev, DSI_CTRL, 1, 0, 0);
2045         REG_FLD_MOD(dsidev, DSI_CTRL, 0, 0, 0);
2046         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 1, 20, 20);
2047         REG_FLD_MOD(dsidev, DSI_CTRL, 1, 0, 0);
2048         */
2049 }
2050
2051 static inline unsigned ns2ddr(struct platform_device *dsidev, unsigned ns)
2052 {
2053         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2054
2055         /* convert time in ns to ddr ticks, rounding up */
2056         unsigned long ddr_clk = dsi->current_cinfo.clkin4ddr / 4;
2057         return (ns * (ddr_clk / 1000 / 1000) + 999) / 1000;
2058 }
2059
2060 static inline unsigned ddr2ns(struct platform_device *dsidev, unsigned ddr)
2061 {
2062         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2063
2064         unsigned long ddr_clk = dsi->current_cinfo.clkin4ddr / 4;
2065         return ddr * 1000 * 1000 / (ddr_clk / 1000);
2066 }
2067
2068 static void dsi_cio_timings(struct platform_device *dsidev)
2069 {
2070         u32 r;
2071         u32 ths_prepare, ths_prepare_ths_zero, ths_trail, ths_exit;
2072         u32 tlpx_half, tclk_trail, tclk_zero;
2073         u32 tclk_prepare;
2074
2075         /* calculate timings */
2076
2077         /* 1 * DDR_CLK = 2 * UI */
2078
2079         /* min 40ns + 4*UI      max 85ns + 6*UI */
2080         ths_prepare = ns2ddr(dsidev, 70) + 2;
2081
2082         /* min 145ns + 10*UI */
2083         ths_prepare_ths_zero = ns2ddr(dsidev, 175) + 2;
2084
2085         /* min max(8*UI, 60ns+4*UI) */
2086         ths_trail = ns2ddr(dsidev, 60) + 5;
2087
2088         /* min 100ns */
2089         ths_exit = ns2ddr(dsidev, 145);
2090
2091         /* tlpx min 50n */
2092         tlpx_half = ns2ddr(dsidev, 25);
2093
2094         /* min 60ns */
2095         tclk_trail = ns2ddr(dsidev, 60) + 2;
2096
2097         /* min 38ns, max 95ns */
2098         tclk_prepare = ns2ddr(dsidev, 65);
2099
2100         /* min tclk-prepare + tclk-zero = 300ns */
2101         tclk_zero = ns2ddr(dsidev, 260);
2102
2103         DSSDBG("ths_prepare %u (%uns), ths_prepare_ths_zero %u (%uns)\n",
2104                 ths_prepare, ddr2ns(dsidev, ths_prepare),
2105                 ths_prepare_ths_zero, ddr2ns(dsidev, ths_prepare_ths_zero));
2106         DSSDBG("ths_trail %u (%uns), ths_exit %u (%uns)\n",
2107                         ths_trail, ddr2ns(dsidev, ths_trail),
2108                         ths_exit, ddr2ns(dsidev, ths_exit));
2109
2110         DSSDBG("tlpx_half %u (%uns), tclk_trail %u (%uns), "
2111                         "tclk_zero %u (%uns)\n",
2112                         tlpx_half, ddr2ns(dsidev, tlpx_half),
2113                         tclk_trail, ddr2ns(dsidev, tclk_trail),
2114                         tclk_zero, ddr2ns(dsidev, tclk_zero));
2115         DSSDBG("tclk_prepare %u (%uns)\n",
2116                         tclk_prepare, ddr2ns(dsidev, tclk_prepare));
2117
2118         /* program timings */
2119
2120         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG0);
2121         r = FLD_MOD(r, ths_prepare, 31, 24);
2122         r = FLD_MOD(r, ths_prepare_ths_zero, 23, 16);
2123         r = FLD_MOD(r, ths_trail, 15, 8);
2124         r = FLD_MOD(r, ths_exit, 7, 0);
2125         dsi_write_reg(dsidev, DSI_DSIPHY_CFG0, r);
2126
2127         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG1);
2128         r = FLD_MOD(r, tlpx_half, 22, 16);
2129         r = FLD_MOD(r, tclk_trail, 15, 8);
2130         r = FLD_MOD(r, tclk_zero, 7, 0);
2131         dsi_write_reg(dsidev, DSI_DSIPHY_CFG1, r);
2132
2133         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG2);
2134         r = FLD_MOD(r, tclk_prepare, 7, 0);
2135         dsi_write_reg(dsidev, DSI_DSIPHY_CFG2, r);
2136 }
2137
2138 static void dsi_cio_enable_lane_override(struct omap_dss_device *dssdev,
2139                 enum dsi_lane lanes)
2140 {
2141         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2142         int clk_lane   = dssdev->phy.dsi.clk_lane;
2143         int data1_lane = dssdev->phy.dsi.data1_lane;
2144         int data2_lane = dssdev->phy.dsi.data2_lane;
2145         int clk_pol    = dssdev->phy.dsi.clk_pol;
2146         int data1_pol  = dssdev->phy.dsi.data1_pol;
2147         int data2_pol  = dssdev->phy.dsi.data2_pol;
2148
2149         u32 l = 0;
2150
2151         if (lanes & DSI_CLK_P)
2152                 l |= 1 << ((clk_lane - 1) * 2 + (clk_pol ? 0 : 1));
2153         if (lanes & DSI_CLK_N)
2154                 l |= 1 << ((clk_lane - 1) * 2 + (clk_pol ? 1 : 0));
2155
2156         if (lanes & DSI_DATA1_P)
2157                 l |= 1 << ((data1_lane - 1) * 2 + (data1_pol ? 0 : 1));
2158         if (lanes & DSI_DATA1_N)
2159                 l |= 1 << ((data1_lane - 1) * 2 + (data1_pol ? 1 : 0));
2160
2161         if (lanes & DSI_DATA2_P)
2162                 l |= 1 << ((data2_lane - 1) * 2 + (data2_pol ? 0 : 1));
2163         if (lanes & DSI_DATA2_N)
2164                 l |= 1 << ((data2_lane - 1) * 2 + (data2_pol ? 1 : 0));
2165
2166         /*
2167          * Bits in REGLPTXSCPDAT4TO0DXDY:
2168          * 17: DY0 18: DX0
2169          * 19: DY1 20: DX1
2170          * 21: DY2 22: DX2
2171          */
2172
2173         /* Set the lane override configuration */
2174
2175         /* REGLPTXSCPDAT4TO0DXDY */
2176         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, l, 22, 17);
2177
2178         /* Enable lane override */
2179
2180         /* ENLPTXSCPDAT */
2181         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 1, 27, 27);
2182 }
2183
2184 static void dsi_cio_disable_lane_override(struct platform_device *dsidev)
2185 {
2186         /* Disable lane override */
2187         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 0, 27, 27); /* ENLPTXSCPDAT */
2188         /* Reset the lane override configuration */
2189         /* REGLPTXSCPDAT4TO0DXDY */
2190         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 0, 22, 17);
2191 }
2192
2193 static int dsi_cio_wait_tx_clk_esc_reset(struct omap_dss_device *dssdev)
2194 {
2195         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2196         int t;
2197         int bits[3];
2198         bool in_use[3];
2199
2200         if (dss_has_feature(FEAT_DSI_REVERSE_TXCLKESC)) {
2201                 bits[0] = 28;
2202                 bits[1] = 27;
2203                 bits[2] = 26;
2204         } else {
2205                 bits[0] = 24;
2206                 bits[1] = 25;
2207                 bits[2] = 26;
2208         }
2209
2210         in_use[0] = false;
2211         in_use[1] = false;
2212         in_use[2] = false;
2213
2214         if (dssdev->phy.dsi.clk_lane != 0)
2215                 in_use[dssdev->phy.dsi.clk_lane - 1] = true;
2216         if (dssdev->phy.dsi.data1_lane != 0)
2217                 in_use[dssdev->phy.dsi.data1_lane - 1] = true;
2218         if (dssdev->phy.dsi.data2_lane != 0)
2219                 in_use[dssdev->phy.dsi.data2_lane - 1] = true;
2220
2221         t = 100000;
2222         while (true) {
2223                 u32 l;
2224                 int i;
2225                 int ok;
2226
2227                 l = dsi_read_reg(dsidev, DSI_DSIPHY_CFG5);
2228
2229                 ok = 0;
2230                 for (i = 0; i < 3; ++i) {
2231                         if (!in_use[i] || (l & (1 << bits[i])))
2232                                 ok++;
2233                 }
2234
2235                 if (ok == 3)
2236                         break;
2237
2238                 if (--t == 0) {
2239                         for (i = 0; i < 3; ++i) {
2240                                 if (!in_use[i] || (l & (1 << bits[i])))
2241                                         continue;
2242
2243                                 DSSERR("CIO TXCLKESC%d domain not coming " \
2244                                                 "out of reset\n", i);
2245                         }
2246                         return -EIO;
2247                 }
2248         }
2249
2250         return 0;
2251 }
2252
2253 static int dsi_cio_init(struct omap_dss_device *dssdev)
2254 {
2255         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2256         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2257         int r;
2258         u32 l;
2259
2260         DSSDBGF();
2261
2262         if (dsi->dsi_mux_pads)
2263                 dsi->dsi_mux_pads(true);
2264
2265         dsi_enable_scp_clk(dsidev);
2266
2267         /* A dummy read using the SCP interface to any DSIPHY register is
2268          * required after DSIPHY reset to complete the reset of the DSI complex
2269          * I/O. */
2270         dsi_read_reg(dsidev, DSI_DSIPHY_CFG5);
2271
2272         if (wait_for_bit_change(dsidev, DSI_DSIPHY_CFG5, 30, 1) != 1) {
2273                 DSSERR("CIO SCP Clock domain not coming out of reset.\n");
2274                 r = -EIO;
2275                 goto err_scp_clk_dom;
2276         }
2277
2278         dsi_set_lane_config(dssdev);
2279
2280         /* set TX STOP MODE timer to maximum for this operation */
2281         l = dsi_read_reg(dsidev, DSI_TIMING1);
2282         l = FLD_MOD(l, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2283         l = FLD_MOD(l, 1, 14, 14);      /* STOP_STATE_X16_IO */
2284         l = FLD_MOD(l, 1, 13, 13);      /* STOP_STATE_X4_IO */
2285         l = FLD_MOD(l, 0x1fff, 12, 0);  /* STOP_STATE_COUNTER_IO */
2286         dsi_write_reg(dsidev, DSI_TIMING1, l);
2287
2288         if (dsi->ulps_enabled) {
2289                 DSSDBG("manual ulps exit\n");
2290
2291                 /* ULPS is exited by Mark-1 state for 1ms, followed by
2292                  * stop state. DSS HW cannot do this via the normal
2293                  * ULPS exit sequence, as after reset the DSS HW thinks
2294                  * that we are not in ULPS mode, and refuses to send the
2295                  * sequence. So we need to send the ULPS exit sequence
2296                  * manually.
2297                  */
2298
2299                 dsi_cio_enable_lane_override(dssdev,
2300                                 DSI_CLK_P | DSI_DATA1_P | DSI_DATA2_P);
2301         }
2302
2303         r = dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_ON);
2304         if (r)
2305                 goto err_cio_pwr;
2306
2307         if (wait_for_bit_change(dsidev, DSI_COMPLEXIO_CFG1, 29, 1) != 1) {
2308                 DSSERR("CIO PWR clock domain not coming out of reset.\n");
2309                 r = -ENODEV;
2310                 goto err_cio_pwr_dom;
2311         }
2312
2313         dsi_if_enable(dsidev, true);
2314         dsi_if_enable(dsidev, false);
2315         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 1, 20, 20); /* LP_CLK_ENABLE */
2316
2317         r = dsi_cio_wait_tx_clk_esc_reset(dssdev);
2318         if (r)
2319                 goto err_tx_clk_esc_rst;
2320
2321         if (dsi->ulps_enabled) {
2322                 /* Keep Mark-1 state for 1ms (as per DSI spec) */
2323                 ktime_t wait = ns_to_ktime(1000 * 1000);
2324                 set_current_state(TASK_UNINTERRUPTIBLE);
2325                 schedule_hrtimeout(&wait, HRTIMER_MODE_REL);
2326
2327                 /* Disable the override. The lanes should be set to Mark-11
2328                  * state by the HW */
2329                 dsi_cio_disable_lane_override(dsidev);
2330         }
2331
2332         /* FORCE_TX_STOP_MODE_IO */
2333         REG_FLD_MOD(dsidev, DSI_TIMING1, 0, 15, 15);
2334
2335         dsi_cio_timings(dsidev);
2336
2337         dsi->ulps_enabled = false;
2338
2339         DSSDBG("CIO init done\n");
2340
2341         return 0;
2342
2343 err_tx_clk_esc_rst:
2344         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 0, 20, 20); /* LP_CLK_ENABLE */
2345 err_cio_pwr_dom:
2346         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_OFF);
2347 err_cio_pwr:
2348         if (dsi->ulps_enabled)
2349                 dsi_cio_disable_lane_override(dsidev);
2350 err_scp_clk_dom:
2351         dsi_disable_scp_clk(dsidev);
2352         if (dsi->dsi_mux_pads)
2353                 dsi->dsi_mux_pads(false);
2354         return r;
2355 }
2356
2357 static void dsi_cio_uninit(struct platform_device *dsidev)
2358 {
2359         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2360
2361         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_OFF);
2362         dsi_disable_scp_clk(dsidev);
2363         if (dsi->dsi_mux_pads)
2364                 dsi->dsi_mux_pads(false);
2365 }
2366
2367 static int _dsi_wait_reset(struct platform_device *dsidev)
2368 {
2369         int t = 0;
2370
2371         while (REG_GET(dsidev, DSI_SYSSTATUS, 0, 0) == 0) {
2372                 if (++t > 5) {
2373                         DSSERR("soft reset failed\n");
2374                         return -ENODEV;
2375                 }
2376                 udelay(1);
2377         }
2378
2379         return 0;
2380 }
2381
2382 static int _dsi_reset(struct platform_device *dsidev)
2383 {
2384         /* Soft reset */
2385         REG_FLD_MOD(dsidev, DSI_SYSCONFIG, 1, 1, 1);
2386         return _dsi_wait_reset(dsidev);
2387 }
2388
2389 static void dsi_config_tx_fifo(struct platform_device *dsidev,
2390                 enum fifo_size size1, enum fifo_size size2,
2391                 enum fifo_size size3, enum fifo_size size4)
2392 {
2393         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2394         u32 r = 0;
2395         int add = 0;
2396         int i;
2397
2398         dsi->vc[0].fifo_size = size1;
2399         dsi->vc[1].fifo_size = size2;
2400         dsi->vc[2].fifo_size = size3;
2401         dsi->vc[3].fifo_size = size4;
2402
2403         for (i = 0; i < 4; i++) {
2404                 u8 v;
2405                 int size = dsi->vc[i].fifo_size;
2406
2407                 if (add + size > 4) {
2408                         DSSERR("Illegal FIFO configuration\n");
2409                         BUG();
2410                 }
2411
2412                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
2413                 r |= v << (8 * i);
2414                 /*DSSDBG("TX FIFO vc %d: size %d, add %d\n", i, size, add); */
2415                 add += size;
2416         }
2417
2418         dsi_write_reg(dsidev, DSI_TX_FIFO_VC_SIZE, r);
2419 }
2420
2421 static void dsi_config_rx_fifo(struct platform_device *dsidev,
2422                 enum fifo_size size1, enum fifo_size size2,
2423                 enum fifo_size size3, enum fifo_size size4)
2424 {
2425         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2426         u32 r = 0;
2427         int add = 0;
2428         int i;
2429
2430         dsi->vc[0].fifo_size = size1;
2431         dsi->vc[1].fifo_size = size2;
2432         dsi->vc[2].fifo_size = size3;
2433         dsi->vc[3].fifo_size = size4;
2434
2435         for (i = 0; i < 4; i++) {
2436                 u8 v;
2437                 int size = dsi->vc[i].fifo_size;
2438
2439                 if (add + size > 4) {
2440                         DSSERR("Illegal FIFO configuration\n");
2441                         BUG();
2442                 }
2443
2444                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
2445                 r |= v << (8 * i);
2446                 /*DSSDBG("RX FIFO vc %d: size %d, add %d\n", i, size, add); */
2447                 add += size;
2448         }
2449
2450         dsi_write_reg(dsidev, DSI_RX_FIFO_VC_SIZE, r);
2451 }
2452
2453 static int dsi_force_tx_stop_mode_io(struct platform_device *dsidev)
2454 {
2455         u32 r;
2456
2457         r = dsi_read_reg(dsidev, DSI_TIMING1);
2458         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2459         dsi_write_reg(dsidev, DSI_TIMING1, r);
2460
2461         if (wait_for_bit_change(dsidev, DSI_TIMING1, 15, 0) != 0) {
2462                 DSSERR("TX_STOP bit not going down\n");
2463                 return -EIO;
2464         }
2465
2466         return 0;
2467 }
2468
2469 static bool dsi_vc_is_enabled(struct platform_device *dsidev, int channel)
2470 {
2471         return REG_GET(dsidev, DSI_VC_CTRL(channel), 0, 0);
2472 }
2473
2474 static void dsi_packet_sent_handler_vp(void *data, u32 mask)
2475 {
2476         struct dsi_packet_sent_handler_data *vp_data =
2477                 (struct dsi_packet_sent_handler_data *) data;
2478         struct dsi_data *dsi = dsi_get_dsidrv_data(vp_data->dsidev);
2479         const int channel = dsi->update_channel;
2480         u8 bit = dsi->te_enabled ? 30 : 31;
2481
2482         if (REG_GET(vp_data->dsidev, DSI_VC_TE(channel), bit, bit) == 0)
2483                 complete(vp_data->completion);
2484 }
2485
2486 static int dsi_sync_vc_vp(struct platform_device *dsidev, int channel)
2487 {
2488         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2489         DECLARE_COMPLETION_ONSTACK(completion);
2490         struct dsi_packet_sent_handler_data vp_data = { dsidev, &completion };
2491         int r = 0;
2492         u8 bit;
2493
2494         bit = dsi->te_enabled ? 30 : 31;
2495
2496         r = dsi_register_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2497                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2498         if (r)
2499                 goto err0;
2500
2501         /* Wait for completion only if TE_EN/TE_START is still set */
2502         if (REG_GET(dsidev, DSI_VC_TE(channel), bit, bit)) {
2503                 if (wait_for_completion_timeout(&completion,
2504                                 msecs_to_jiffies(10)) == 0) {
2505                         DSSERR("Failed to complete previous frame transfer\n");
2506                         r = -EIO;
2507                         goto err1;
2508                 }
2509         }
2510
2511         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2512                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2513
2514         return 0;
2515 err1:
2516         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2517                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2518 err0:
2519         return r;
2520 }
2521
2522 static void dsi_packet_sent_handler_l4(void *data, u32 mask)
2523 {
2524         struct dsi_packet_sent_handler_data *l4_data =
2525                 (struct dsi_packet_sent_handler_data *) data;
2526         struct dsi_data *dsi = dsi_get_dsidrv_data(l4_data->dsidev);
2527         const int channel = dsi->update_channel;
2528
2529         if (REG_GET(l4_data->dsidev, DSI_VC_CTRL(channel), 5, 5) == 0)
2530                 complete(l4_data->completion);
2531 }
2532
2533 static int dsi_sync_vc_l4(struct platform_device *dsidev, int channel)
2534 {
2535         DECLARE_COMPLETION_ONSTACK(completion);
2536         struct dsi_packet_sent_handler_data l4_data = { dsidev, &completion };
2537         int r = 0;
2538
2539         r = dsi_register_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2540                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2541         if (r)
2542                 goto err0;
2543
2544         /* Wait for completion only if TX_FIFO_NOT_EMPTY is still set */
2545         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 5, 5)) {
2546                 if (wait_for_completion_timeout(&completion,
2547                                 msecs_to_jiffies(10)) == 0) {
2548                         DSSERR("Failed to complete previous l4 transfer\n");
2549                         r = -EIO;
2550                         goto err1;
2551                 }
2552         }
2553
2554         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2555                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2556
2557         return 0;
2558 err1:
2559         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2560                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2561 err0:
2562         return r;
2563 }
2564
2565 static int dsi_sync_vc(struct platform_device *dsidev, int channel)
2566 {
2567         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2568
2569         WARN_ON(!dsi_bus_is_locked(dsidev));
2570
2571         WARN_ON(in_interrupt());
2572
2573         if (!dsi_vc_is_enabled(dsidev, channel))
2574                 return 0;
2575
2576         switch (dsi->vc[channel].mode) {
2577         case DSI_VC_MODE_VP:
2578                 return dsi_sync_vc_vp(dsidev, channel);
2579         case DSI_VC_MODE_L4:
2580                 return dsi_sync_vc_l4(dsidev, channel);
2581         default:
2582                 BUG();
2583         }
2584 }
2585
2586 static int dsi_vc_enable(struct platform_device *dsidev, int channel,
2587                 bool enable)
2588 {
2589         DSSDBG("dsi_vc_enable channel %d, enable %d\n",
2590                         channel, enable);
2591
2592         enable = enable ? 1 : 0;
2593
2594         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), enable, 0, 0);
2595
2596         if (wait_for_bit_change(dsidev, DSI_VC_CTRL(channel),
2597                 0, enable) != enable) {
2598                         DSSERR("Failed to set dsi_vc_enable to %d\n", enable);
2599                         return -EIO;
2600         }
2601
2602         return 0;
2603 }
2604
2605 static void dsi_vc_initial_config(struct platform_device *dsidev, int channel)
2606 {
2607         u32 r;
2608
2609         DSSDBGF("%d", channel);
2610
2611         r = dsi_read_reg(dsidev, DSI_VC_CTRL(channel));
2612
2613         if (FLD_GET(r, 15, 15)) /* VC_BUSY */
2614                 DSSERR("VC(%d) busy when trying to configure it!\n",
2615                                 channel);
2616
2617         r = FLD_MOD(r, 0, 1, 1); /* SOURCE, 0 = L4 */
2618         r = FLD_MOD(r, 0, 2, 2); /* BTA_SHORT_EN  */
2619         r = FLD_MOD(r, 0, 3, 3); /* BTA_LONG_EN */
2620         r = FLD_MOD(r, 0, 4, 4); /* MODE, 0 = command */
2621         r = FLD_MOD(r, 1, 7, 7); /* CS_TX_EN */
2622         r = FLD_MOD(r, 1, 8, 8); /* ECC_TX_EN */
2623         r = FLD_MOD(r, 0, 9, 9); /* MODE_SPEED, high speed on/off */
2624         if (dss_has_feature(FEAT_DSI_VC_OCP_WIDTH))
2625                 r = FLD_MOD(r, 3, 11, 10);      /* OCP_WIDTH = 32 bit */
2626
2627         r = FLD_MOD(r, 4, 29, 27); /* DMA_RX_REQ_NB = no dma */
2628         r = FLD_MOD(r, 4, 23, 21); /* DMA_TX_REQ_NB = no dma */
2629
2630         dsi_write_reg(dsidev, DSI_VC_CTRL(channel), r);
2631 }
2632
2633 static int dsi_vc_config_l4(struct platform_device *dsidev, int channel)
2634 {
2635         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2636
2637         if (dsi->vc[channel].mode == DSI_VC_MODE_L4)
2638                 return 0;
2639
2640         DSSDBGF("%d", channel);
2641
2642         dsi_sync_vc(dsidev, channel);
2643
2644         dsi_vc_enable(dsidev, channel, 0);
2645
2646         /* VC_BUSY */
2647         if (wait_for_bit_change(dsidev, DSI_VC_CTRL(channel), 15, 0) != 0) {
2648                 DSSERR("vc(%d) busy when trying to config for L4\n", channel);
2649                 return -EIO;
2650         }
2651
2652         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 0, 1, 1); /* SOURCE, 0 = L4 */
2653
2654         /* DCS_CMD_ENABLE */
2655         if (dss_has_feature(FEAT_DSI_DCS_CMD_CONFIG_VC))
2656                 REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 0, 30, 30);
2657
2658         dsi_vc_enable(dsidev, channel, 1);
2659
2660         dsi->vc[channel].mode = DSI_VC_MODE_L4;
2661
2662         return 0;
2663 }
2664
2665 static int dsi_vc_config_vp(struct platform_device *dsidev, int channel)
2666 {
2667         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2668
2669         if (dsi->vc[channel].mode == DSI_VC_MODE_VP)
2670                 return 0;
2671
2672         DSSDBGF("%d", channel);
2673
2674         dsi_sync_vc(dsidev, channel);
2675
2676         dsi_vc_enable(dsidev, channel, 0);
2677
2678         /* VC_BUSY */
2679         if (wait_for_bit_change(dsidev, DSI_VC_CTRL(channel), 15, 0) != 0) {
2680                 DSSERR("vc(%d) busy when trying to config for VP\n", channel);
2681                 return -EIO;
2682         }
2683
2684         /* SOURCE, 1 = video port */
2685         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 1, 1, 1);
2686
2687         /* DCS_CMD_ENABLE */
2688         if (dss_has_feature(FEAT_DSI_DCS_CMD_CONFIG_VC))
2689                 REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 1, 30, 30);
2690
2691         dsi_vc_enable(dsidev, channel, 1);
2692
2693         dsi->vc[channel].mode = DSI_VC_MODE_VP;
2694
2695         return 0;
2696 }
2697
2698
2699 void omapdss_dsi_vc_enable_hs(struct omap_dss_device *dssdev, int channel,
2700                 bool enable)
2701 {
2702         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2703
2704         DSSDBG("dsi_vc_enable_hs(%d, %d)\n", channel, enable);
2705
2706         WARN_ON(!dsi_bus_is_locked(dsidev));
2707
2708         dsi_vc_enable(dsidev, channel, 0);
2709         dsi_if_enable(dsidev, 0);
2710
2711         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), enable, 9, 9);
2712
2713         dsi_vc_enable(dsidev, channel, 1);
2714         dsi_if_enable(dsidev, 1);
2715
2716         dsi_force_tx_stop_mode_io(dsidev);
2717 }
2718 EXPORT_SYMBOL(omapdss_dsi_vc_enable_hs);
2719
2720 static void dsi_vc_flush_long_data(struct platform_device *dsidev, int channel)
2721 {
2722         while (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2723                 u32 val;
2724                 val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
2725                 DSSDBG("\t\tb1 %#02x b2 %#02x b3 %#02x b4 %#02x\n",
2726                                 (val >> 0) & 0xff,
2727                                 (val >> 8) & 0xff,
2728                                 (val >> 16) & 0xff,
2729                                 (val >> 24) & 0xff);
2730         }
2731 }
2732
2733 static void dsi_show_rx_ack_with_err(u16 err)
2734 {
2735         DSSERR("\tACK with ERROR (%#x):\n", err);
2736         if (err & (1 << 0))
2737                 DSSERR("\t\tSoT Error\n");
2738         if (err & (1 << 1))
2739                 DSSERR("\t\tSoT Sync Error\n");
2740         if (err & (1 << 2))
2741                 DSSERR("\t\tEoT Sync Error\n");
2742         if (err & (1 << 3))
2743                 DSSERR("\t\tEscape Mode Entry Command Error\n");
2744         if (err & (1 << 4))
2745                 DSSERR("\t\tLP Transmit Sync Error\n");
2746         if (err & (1 << 5))
2747                 DSSERR("\t\tHS Receive Timeout Error\n");
2748         if (err & (1 << 6))
2749                 DSSERR("\t\tFalse Control Error\n");
2750         if (err & (1 << 7))
2751                 DSSERR("\t\t(reserved7)\n");
2752         if (err & (1 << 8))
2753                 DSSERR("\t\tECC Error, single-bit (corrected)\n");
2754         if (err & (1 << 9))
2755                 DSSERR("\t\tECC Error, multi-bit (not corrected)\n");
2756         if (err & (1 << 10))
2757                 DSSERR("\t\tChecksum Error\n");
2758         if (err & (1 << 11))
2759                 DSSERR("\t\tData type not recognized\n");
2760         if (err & (1 << 12))
2761                 DSSERR("\t\tInvalid VC ID\n");
2762         if (err & (1 << 13))
2763                 DSSERR("\t\tInvalid Transmission Length\n");
2764         if (err & (1 << 14))
2765                 DSSERR("\t\t(reserved14)\n");
2766         if (err & (1 << 15))
2767                 DSSERR("\t\tDSI Protocol Violation\n");
2768 }
2769
2770 static u16 dsi_vc_flush_receive_data(struct platform_device *dsidev,
2771                 int channel)
2772 {
2773         /* RX_FIFO_NOT_EMPTY */
2774         while (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2775                 u32 val;
2776                 u8 dt;
2777                 val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
2778                 DSSERR("\trawval %#08x\n", val);
2779                 dt = FLD_GET(val, 5, 0);
2780                 if (dt == DSI_DT_RX_ACK_WITH_ERR) {
2781                         u16 err = FLD_GET(val, 23, 8);
2782                         dsi_show_rx_ack_with_err(err);
2783                 } else if (dt == DSI_DT_RX_SHORT_READ_1) {
2784                         DSSERR("\tDCS short response, 1 byte: %#x\n",
2785                                         FLD_GET(val, 23, 8));
2786                 } else if (dt == DSI_DT_RX_SHORT_READ_2) {
2787                         DSSERR("\tDCS short response, 2 byte: %#x\n",
2788                                         FLD_GET(val, 23, 8));
2789                 } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
2790                         DSSERR("\tDCS long response, len %d\n",
2791                                         FLD_GET(val, 23, 8));
2792                         dsi_vc_flush_long_data(dsidev, channel);
2793                 } else {
2794                         DSSERR("\tunknown datatype 0x%02x\n", dt);
2795                 }
2796         }
2797         return 0;
2798 }
2799
2800 static int dsi_vc_send_bta(struct platform_device *dsidev, int channel)
2801 {
2802         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2803
2804         if (dsi->debug_write || dsi->debug_read)
2805                 DSSDBG("dsi_vc_send_bta %d\n", channel);
2806
2807         WARN_ON(!dsi_bus_is_locked(dsidev));
2808
2809         /* RX_FIFO_NOT_EMPTY */
2810         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2811                 DSSERR("rx fifo not empty when sending BTA, dumping data:\n");
2812                 dsi_vc_flush_receive_data(dsidev, channel);
2813         }
2814
2815         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 1, 6, 6); /* BTA_EN */
2816
2817         return 0;
2818 }
2819
2820 int dsi_vc_send_bta_sync(struct omap_dss_device *dssdev, int channel)
2821 {
2822         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2823         DECLARE_COMPLETION_ONSTACK(completion);
2824         int r = 0;
2825         u32 err;
2826
2827         r = dsi_register_isr_vc(dsidev, channel, dsi_completion_handler,
2828                         &completion, DSI_VC_IRQ_BTA);
2829         if (r)
2830                 goto err0;
2831
2832         r = dsi_register_isr(dsidev, dsi_completion_handler, &completion,
2833                         DSI_IRQ_ERROR_MASK);
2834         if (r)
2835                 goto err1;
2836
2837         r = dsi_vc_send_bta(dsidev, channel);
2838         if (r)
2839                 goto err2;
2840
2841         if (wait_for_completion_timeout(&completion,
2842                                 msecs_to_jiffies(500)) == 0) {
2843                 DSSERR("Failed to receive BTA\n");
2844                 r = -EIO;
2845                 goto err2;
2846         }
2847
2848         err = dsi_get_errors(dsidev);
2849         if (err) {
2850                 DSSERR("Error while sending BTA: %x\n", err);
2851                 r = -EIO;
2852                 goto err2;
2853         }
2854 err2:
2855         dsi_unregister_isr(dsidev, dsi_completion_handler, &completion,
2856                         DSI_IRQ_ERROR_MASK);
2857 err1:
2858         dsi_unregister_isr_vc(dsidev, channel, dsi_completion_handler,
2859                         &completion, DSI_VC_IRQ_BTA);
2860 err0:
2861         return r;
2862 }
2863 EXPORT_SYMBOL(dsi_vc_send_bta_sync);
2864
2865 static inline void dsi_vc_write_long_header(struct platform_device *dsidev,
2866                 int channel, u8 data_type, u16 len, u8 ecc)
2867 {
2868         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2869         u32 val;
2870         u8 data_id;
2871
2872         WARN_ON(!dsi_bus_is_locked(dsidev));
2873
2874         data_id = data_type | dsi->vc[channel].vc_id << 6;
2875
2876         val = FLD_VAL(data_id, 7, 0) | FLD_VAL(len, 23, 8) |
2877                 FLD_VAL(ecc, 31, 24);
2878
2879         dsi_write_reg(dsidev, DSI_VC_LONG_PACKET_HEADER(channel), val);
2880 }
2881
2882 static inline void dsi_vc_write_long_payload(struct platform_device *dsidev,
2883                 int channel, u8 b1, u8 b2, u8 b3, u8 b4)
2884 {
2885         u32 val;
2886
2887         val = b4 << 24 | b3 << 16 | b2 << 8  | b1 << 0;
2888
2889 /*      DSSDBG("\twriting %02x, %02x, %02x, %02x (%#010x)\n",
2890                         b1, b2, b3, b4, val); */
2891
2892         dsi_write_reg(dsidev, DSI_VC_LONG_PACKET_PAYLOAD(channel), val);
2893 }
2894
2895 static int dsi_vc_send_long(struct platform_device *dsidev, int channel,
2896                 u8 data_type, u8 *data, u16 len, u8 ecc)
2897 {
2898         /*u32 val; */
2899         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2900         int i;
2901         u8 *p;
2902         int r = 0;
2903         u8 b1, b2, b3, b4;
2904
2905         if (dsi->debug_write)
2906                 DSSDBG("dsi_vc_send_long, %d bytes\n", len);
2907
2908         /* len + header */
2909         if (dsi->vc[channel].fifo_size * 32 * 4 < len + 4) {
2910                 DSSERR("unable to send long packet: packet too long.\n");
2911                 return -EINVAL;
2912         }
2913
2914         dsi_vc_config_l4(dsidev, channel);
2915
2916         dsi_vc_write_long_header(dsidev, channel, data_type, len, ecc);
2917
2918         p = data;
2919         for (i = 0; i < len >> 2; i++) {
2920                 if (dsi->debug_write)
2921                         DSSDBG("\tsending full packet %d\n", i);
2922
2923                 b1 = *p++;
2924                 b2 = *p++;
2925                 b3 = *p++;
2926                 b4 = *p++;
2927
2928                 dsi_vc_write_long_payload(dsidev, channel, b1, b2, b3, b4);
2929         }
2930
2931         i = len % 4;
2932         if (i) {
2933                 b1 = 0; b2 = 0; b3 = 0;
2934
2935                 if (dsi->debug_write)
2936                         DSSDBG("\tsending remainder bytes %d\n", i);
2937
2938                 switch (i) {
2939                 case 3:
2940                         b1 = *p++;
2941                         b2 = *p++;
2942                         b3 = *p++;
2943                         break;
2944                 case 2:
2945                         b1 = *p++;
2946                         b2 = *p++;
2947                         break;
2948                 case 1:
2949                         b1 = *p++;
2950                         break;
2951                 }
2952
2953                 dsi_vc_write_long_payload(dsidev, channel, b1, b2, b3, 0);
2954         }
2955
2956         return r;
2957 }
2958
2959 static int dsi_vc_send_short(struct platform_device *dsidev, int channel,
2960                 u8 data_type, u16 data, u8 ecc)
2961 {
2962         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2963         u32 r;
2964         u8 data_id;
2965
2966         WARN_ON(!dsi_bus_is_locked(dsidev));
2967
2968         if (dsi->debug_write)
2969                 DSSDBG("dsi_vc_send_short(ch%d, dt %#x, b1 %#x, b2 %#x)\n",
2970                                 channel,
2971                                 data_type, data & 0xff, (data >> 8) & 0xff);
2972
2973         dsi_vc_config_l4(dsidev, channel);
2974
2975         if (FLD_GET(dsi_read_reg(dsidev, DSI_VC_CTRL(channel)), 16, 16)) {
2976                 DSSERR("ERROR FIFO FULL, aborting transfer\n");
2977                 return -EINVAL;
2978         }
2979
2980         data_id = data_type | dsi->vc[channel].vc_id << 6;
2981
2982         r = (data_id << 0) | (data << 8) | (ecc << 24);
2983
2984         dsi_write_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel), r);
2985
2986         return 0;
2987 }
2988
2989 int dsi_vc_send_null(struct omap_dss_device *dssdev, int channel)
2990 {
2991         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2992         u8 nullpkg[] = {0, 0, 0, 0};
2993
2994         return dsi_vc_send_long(dsidev, channel, DSI_DT_NULL_PACKET, nullpkg,
2995                 4, 0);
2996 }
2997 EXPORT_SYMBOL(dsi_vc_send_null);
2998
2999 int dsi_vc_dcs_write_nosync(struct omap_dss_device *dssdev, int channel,
3000                 u8 *data, int len)
3001 {
3002         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3003         int r;
3004
3005         BUG_ON(len == 0);
3006
3007         if (len == 1) {
3008                 r = dsi_vc_send_short(dsidev, channel, DSI_DT_DCS_SHORT_WRITE_0,
3009                                 data[0], 0);
3010         } else if (len == 2) {
3011                 r = dsi_vc_send_short(dsidev, channel, DSI_DT_DCS_SHORT_WRITE_1,
3012                                 data[0] | (data[1] << 8), 0);
3013         } else {
3014                 /* 0x39 = DCS Long Write */
3015                 r = dsi_vc_send_long(dsidev, channel, DSI_DT_DCS_LONG_WRITE,
3016                                 data, len, 0);
3017         }
3018
3019         return r;
3020 }
3021 EXPORT_SYMBOL(dsi_vc_dcs_write_nosync);
3022
3023 int dsi_vc_dcs_write(struct omap_dss_device *dssdev, int channel, u8 *data,
3024                 int len)
3025 {
3026         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3027         int r;
3028
3029         r = dsi_vc_dcs_write_nosync(dssdev, channel, data, len);
3030         if (r)
3031                 goto err;
3032
3033         r = dsi_vc_send_bta_sync(dssdev, channel);
3034         if (r)
3035                 goto err;
3036
3037         /* RX_FIFO_NOT_EMPTY */
3038         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
3039                 DSSERR("rx fifo not empty after write, dumping data:\n");
3040                 dsi_vc_flush_receive_data(dsidev, channel);
3041                 r = -EIO;
3042                 goto err;
3043         }
3044
3045         return 0;
3046 err:
3047         DSSERR("dsi_vc_dcs_write(ch %d, cmd 0x%02x, len %d) failed\n",
3048                         channel, data[0], len);
3049         return r;
3050 }
3051 EXPORT_SYMBOL(dsi_vc_dcs_write);
3052
3053 int dsi_vc_dcs_write_0(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd)
3054 {
3055         return dsi_vc_dcs_write(dssdev, channel, &dcs_cmd, 1);
3056 }
3057 EXPORT_SYMBOL(dsi_vc_dcs_write_0);
3058
3059 int dsi_vc_dcs_write_1(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd,
3060                 u8 param)
3061 {
3062         u8 buf[2];
3063         buf[0] = dcs_cmd;
3064         buf[1] = param;
3065         return dsi_vc_dcs_write(dssdev, channel, buf, 2);
3066 }
3067 EXPORT_SYMBOL(dsi_vc_dcs_write_1);
3068
3069 int dsi_vc_dcs_read(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd,
3070                 u8 *buf, int buflen)
3071 {
3072         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3073         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3074         u32 val;
3075         u8 dt;
3076         int r;
3077
3078         if (dsi->debug_read)
3079                 DSSDBG("dsi_vc_dcs_read(ch%d, dcs_cmd %x)\n", channel, dcs_cmd);
3080
3081         r = dsi_vc_send_short(dsidev, channel, DSI_DT_DCS_READ, dcs_cmd, 0);
3082         if (r)
3083                 goto err;
3084
3085         r = dsi_vc_send_bta_sync(dssdev, channel);
3086         if (r)
3087                 goto err;
3088
3089         /* RX_FIFO_NOT_EMPTY */
3090         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20) == 0) {
3091                 DSSERR("RX fifo empty when trying to read.\n");
3092                 r = -EIO;
3093                 goto err;
3094         }
3095
3096         val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
3097         if (dsi->debug_read)
3098                 DSSDBG("\theader: %08x\n", val);
3099         dt = FLD_GET(val, 5, 0);
3100         if (dt == DSI_DT_RX_ACK_WITH_ERR) {
3101                 u16 err = FLD_GET(val, 23, 8);
3102                 dsi_show_rx_ack_with_err(err);
3103                 r = -EIO;
3104                 goto err;
3105
3106         } else if (dt == DSI_DT_RX_SHORT_READ_1) {
3107                 u8 data = FLD_GET(val, 15, 8);
3108                 if (dsi->debug_read)
3109                         DSSDBG("\tDCS short response, 1 byte: %02x\n", data);
3110
3111                 if (buflen < 1) {
3112                         r = -EIO;
3113                         goto err;
3114                 }
3115
3116                 buf[0] = data;
3117
3118                 return 1;
3119         } else if (dt == DSI_DT_RX_SHORT_READ_2) {
3120                 u16 data = FLD_GET(val, 23, 8);
3121                 if (dsi->debug_read)
3122                         DSSDBG("\tDCS short response, 2 byte: %04x\n", data);
3123
3124                 if (buflen < 2) {
3125                         r = -EIO;
3126                         goto err;
3127                 }
3128
3129                 buf[0] = data & 0xff;
3130                 buf[1] = (data >> 8) & 0xff;
3131
3132                 return 2;
3133         } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
3134                 int w;
3135                 int len = FLD_GET(val, 23, 8);
3136                 if (dsi->debug_read)
3137                         DSSDBG("\tDCS long response, len %d\n", len);
3138
3139                 if (len > buflen) {
3140                         r = -EIO;
3141                         goto err;
3142                 }
3143
3144                 /* two byte checksum ends the packet, not included in len */
3145                 for (w = 0; w < len + 2;) {
3146                         int b;
3147                         val = dsi_read_reg(dsidev,
3148                                 DSI_VC_SHORT_PACKET_HEADER(channel));
3149                         if (dsi->debug_read)
3150                                 DSSDBG("\t\t%02x %02x %02x %02x\n",
3151                                                 (val >> 0) & 0xff,
3152                                                 (val >> 8) & 0xff,
3153                                                 (val >> 16) & 0xff,
3154                                                 (val >> 24) & 0xff);
3155
3156                         for (b = 0; b < 4; ++b) {
3157                                 if (w < len)
3158                                         buf[w] = (val >> (b * 8)) & 0xff;
3159                                 /* we discard the 2 byte checksum */
3160                                 ++w;
3161                         }
3162                 }
3163
3164                 return len;
3165         } else {
3166                 DSSERR("\tunknown datatype 0x%02x\n", dt);
3167                 r = -EIO;
3168                 goto err;
3169         }
3170
3171         BUG();
3172 err:
3173         DSSERR("dsi_vc_dcs_read(ch %d, cmd 0x%02x) failed\n",
3174                         channel, dcs_cmd);
3175         return r;
3176
3177 }
3178 EXPORT_SYMBOL(dsi_vc_dcs_read);
3179
3180 int dsi_vc_dcs_read_1(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd,
3181                 u8 *data)
3182 {
3183         int r;
3184
3185         r = dsi_vc_dcs_read(dssdev, channel, dcs_cmd, data, 1);
3186
3187         if (r < 0)
3188                 return r;
3189
3190         if (r != 1)
3191                 return -EIO;
3192
3193         return 0;
3194 }
3195 EXPORT_SYMBOL(dsi_vc_dcs_read_1);
3196
3197 int dsi_vc_dcs_read_2(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd,
3198                 u8 *data1, u8 *data2)
3199 {
3200         u8 buf[2];
3201         int r;
3202
3203         r = dsi_vc_dcs_read(dssdev, channel, dcs_cmd, buf, 2);
3204
3205         if (r < 0)
3206                 return r;
3207
3208         if (r != 2)
3209                 return -EIO;
3210
3211         *data1 = buf[0];
3212         *data2 = buf[1];
3213
3214         return 0;
3215 }
3216 EXPORT_SYMBOL(dsi_vc_dcs_read_2);
3217
3218 int dsi_vc_set_max_rx_packet_size(struct omap_dss_device *dssdev, int channel,
3219                 u16 len)
3220 {
3221         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3222
3223         return dsi_vc_send_short(dsidev, channel, DSI_DT_SET_MAX_RET_PKG_SIZE,
3224                         len, 0);
3225 }
3226 EXPORT_SYMBOL(dsi_vc_set_max_rx_packet_size);
3227
3228 static int dsi_enter_ulps(struct platform_device *dsidev)
3229 {
3230         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3231         DECLARE_COMPLETION_ONSTACK(completion);
3232         int r;
3233
3234         DSSDBGF();
3235
3236         WARN_ON(!dsi_bus_is_locked(dsidev));
3237
3238         WARN_ON(dsi->ulps_enabled);
3239
3240         if (dsi->ulps_enabled)
3241                 return 0;
3242
3243         if (REG_GET(dsidev, DSI_CLK_CTRL, 13, 13)) {
3244                 DSSERR("DDR_CLK_ALWAYS_ON enabled when entering ULPS\n");
3245                 return -EIO;
3246         }
3247
3248         dsi_sync_vc(dsidev, 0);
3249         dsi_sync_vc(dsidev, 1);
3250         dsi_sync_vc(dsidev, 2);
3251         dsi_sync_vc(dsidev, 3);
3252
3253         dsi_force_tx_stop_mode_io(dsidev);
3254
3255         dsi_vc_enable(dsidev, 0, false);
3256         dsi_vc_enable(dsidev, 1, false);
3257         dsi_vc_enable(dsidev, 2, false);
3258         dsi_vc_enable(dsidev, 3, false);
3259
3260         if (REG_GET(dsidev, DSI_COMPLEXIO_CFG2, 16, 16)) {      /* HS_BUSY */
3261                 DSSERR("HS busy when enabling ULPS\n");
3262                 return -EIO;
3263         }
3264
3265         if (REG_GET(dsidev, DSI_COMPLEXIO_CFG2, 17, 17)) {      /* LP_BUSY */
3266                 DSSERR("LP busy when enabling ULPS\n");
3267                 return -EIO;
3268         }
3269
3270         r = dsi_register_isr_cio(dsidev, dsi_completion_handler, &completion,
3271                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3272         if (r)
3273                 return r;
3274
3275         /* Assert TxRequestEsc for data lanes and TxUlpsClk for clk lane */
3276         /* LANEx_ULPS_SIG2 */
3277         REG_FLD_MOD(dsidev, DSI_COMPLEXIO_CFG2, (1 << 0) | (1 << 1) | (1 << 2),
3278                 7, 5);
3279
3280         if (wait_for_completion_timeout(&completion,
3281                                 msecs_to_jiffies(1000)) == 0) {
3282                 DSSERR("ULPS enable timeout\n");
3283                 r = -EIO;
3284                 goto err;
3285         }
3286
3287         dsi_unregister_isr_cio(dsidev, dsi_completion_handler, &completion,
3288                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3289
3290         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_ULPS);
3291
3292         dsi_if_enable(dsidev, false);
3293
3294         dsi->ulps_enabled = true;
3295
3296         return 0;
3297
3298 err:
3299         dsi_unregister_isr_cio(dsidev, dsi_completion_handler, &completion,
3300                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3301         return r;
3302 }
3303
3304 static void dsi_set_lp_rx_timeout(struct platform_device *dsidev,
3305                 unsigned ticks, bool x4, bool x16)
3306 {
3307         unsigned long fck;
3308         unsigned long total_ticks;
3309         u32 r;
3310
3311         BUG_ON(ticks > 0x1fff);
3312
3313         /* ticks in DSI_FCK */
3314         fck = dsi_fclk_rate(dsidev);
3315
3316         r = dsi_read_reg(dsidev, DSI_TIMING2);
3317         r = FLD_MOD(r, 1, 15, 15);      /* LP_RX_TO */
3318         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* LP_RX_TO_X16 */
3319         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* LP_RX_TO_X4 */
3320         r = FLD_MOD(r, ticks, 12, 0);   /* LP_RX_COUNTER */
3321         dsi_write_reg(dsidev, DSI_TIMING2, r);
3322
3323         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3324
3325         DSSDBG("LP_RX_TO %lu ticks (%#x%s%s) = %lu ns\n",
3326                         total_ticks,
3327                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3328                         (total_ticks * 1000) / (fck / 1000 / 1000));
3329 }
3330
3331 static void dsi_set_ta_timeout(struct platform_device *dsidev, unsigned ticks,
3332                 bool x8, bool x16)
3333 {
3334         unsigned long fck;
3335         unsigned long total_ticks;
3336         u32 r;
3337
3338         BUG_ON(ticks > 0x1fff);
3339
3340         /* ticks in DSI_FCK */
3341         fck = dsi_fclk_rate(dsidev);
3342
3343         r = dsi_read_reg(dsidev, DSI_TIMING1);
3344         r = FLD_MOD(r, 1, 31, 31);      /* TA_TO */
3345         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* TA_TO_X16 */
3346         r = FLD_MOD(r, x8 ? 1 : 0, 29, 29);     /* TA_TO_X8 */
3347         r = FLD_MOD(r, ticks, 28, 16);  /* TA_TO_COUNTER */
3348         dsi_write_reg(dsidev, DSI_TIMING1, r);
3349
3350         total_ticks = ticks * (x16 ? 16 : 1) * (x8 ? 8 : 1);
3351
3352         DSSDBG("TA_TO %lu ticks (%#x%s%s) = %lu ns\n",
3353                         total_ticks,
3354                         ticks, x8 ? " x8" : "", x16 ? " x16" : "",
3355                         (total_ticks * 1000) / (fck / 1000 / 1000));
3356 }
3357
3358 static void dsi_set_stop_state_counter(struct platform_device *dsidev,
3359                 unsigned ticks, bool x4, bool x16)
3360 {
3361         unsigned long fck;
3362         unsigned long total_ticks;
3363         u32 r;
3364
3365         BUG_ON(ticks > 0x1fff);
3366
3367         /* ticks in DSI_FCK */
3368         fck = dsi_fclk_rate(dsidev);
3369
3370         r = dsi_read_reg(dsidev, DSI_TIMING1);
3371         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
3372         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* STOP_STATE_X16_IO */
3373         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* STOP_STATE_X4_IO */
3374         r = FLD_MOD(r, ticks, 12, 0);   /* STOP_STATE_COUNTER_IO */
3375         dsi_write_reg(dsidev, DSI_TIMING1, r);
3376
3377         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3378
3379         DSSDBG("STOP_STATE_COUNTER %lu ticks (%#x%s%s) = %lu ns\n",
3380                         total_ticks,
3381                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3382                         (total_ticks * 1000) / (fck / 1000 / 1000));
3383 }
3384
3385 static void dsi_set_hs_tx_timeout(struct platform_device *dsidev,
3386                 unsigned ticks, bool x4, bool x16)
3387 {
3388         unsigned long fck;
3389         unsigned long total_ticks;
3390         u32 r;
3391
3392         BUG_ON(ticks > 0x1fff);
3393
3394         /* ticks in TxByteClkHS */
3395         fck = dsi_get_txbyteclkhs(dsidev);
3396
3397         r = dsi_read_reg(dsidev, DSI_TIMING2);
3398         r = FLD_MOD(r, 1, 31, 31);      /* HS_TX_TO */
3399         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* HS_TX_TO_X16 */
3400         r = FLD_MOD(r, x4 ? 1 : 0, 29, 29);     /* HS_TX_TO_X8 (4 really) */
3401         r = FLD_MOD(r, ticks, 28, 16);  /* HS_TX_TO_COUNTER */
3402         dsi_write_reg(dsidev, DSI_TIMING2, r);
3403
3404         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3405
3406         DSSDBG("HS_TX_TO %lu ticks (%#x%s%s) = %lu ns\n",
3407                         total_ticks,
3408                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3409                         (total_ticks * 1000) / (fck / 1000 / 1000));
3410 }
3411 static int dsi_proto_config(struct omap_dss_device *dssdev)
3412 {
3413         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3414         u32 r;
3415         int buswidth = 0;
3416
3417         dsi_config_tx_fifo(dsidev, DSI_FIFO_SIZE_32,
3418                         DSI_FIFO_SIZE_32,
3419                         DSI_FIFO_SIZE_32,
3420                         DSI_FIFO_SIZE_32);
3421
3422         dsi_config_rx_fifo(dsidev, DSI_FIFO_SIZE_32,
3423                         DSI_FIFO_SIZE_32,
3424                         DSI_FIFO_SIZE_32,
3425                         DSI_FIFO_SIZE_32);
3426
3427         /* XXX what values for the timeouts? */
3428         dsi_set_stop_state_counter(dsidev, 0x1000, false, false);
3429         dsi_set_ta_timeout(dsidev, 0x1fff, true, true);
3430         dsi_set_lp_rx_timeout(dsidev, 0x1fff, true, true);
3431         dsi_set_hs_tx_timeout(dsidev, 0x1fff, true, true);
3432
3433         switch (dssdev->ctrl.pixel_size) {
3434         case 16:
3435                 buswidth = 0;
3436                 break;
3437         case 18:
3438                 buswidth = 1;
3439                 break;
3440         case 24:
3441                 buswidth = 2;
3442                 break;
3443         default:
3444                 BUG();
3445         }
3446
3447         r = dsi_read_reg(dsidev, DSI_CTRL);
3448         r = FLD_MOD(r, 1, 1, 1);        /* CS_RX_EN */
3449         r = FLD_MOD(r, 1, 2, 2);        /* ECC_RX_EN */
3450         r = FLD_MOD(r, 1, 3, 3);        /* TX_FIFO_ARBITRATION */
3451         r = FLD_MOD(r, 1, 4, 4);        /* VP_CLK_RATIO, always 1, see errata*/
3452         r = FLD_MOD(r, buswidth, 7, 6); /* VP_DATA_BUS_WIDTH */
3453         r = FLD_MOD(r, 0, 8, 8);        /* VP_CLK_POL */
3454         r = FLD_MOD(r, 2, 13, 12);      /* LINE_BUFFER, 2 lines */
3455         r = FLD_MOD(r, 1, 14, 14);      /* TRIGGER_RESET_MODE */
3456         r = FLD_MOD(r, 1, 19, 19);      /* EOT_ENABLE */
3457         if (!dss_has_feature(FEAT_DSI_DCS_CMD_CONFIG_VC)) {
3458                 r = FLD_MOD(r, 1, 24, 24);      /* DCS_CMD_ENABLE */
3459                 /* DCS_CMD_CODE, 1=start, 0=continue */
3460                 r = FLD_MOD(r, 0, 25, 25);
3461         }
3462
3463         dsi_write_reg(dsidev, DSI_CTRL, r);
3464
3465         dsi_vc_initial_config(dsidev, 0);
3466         dsi_vc_initial_config(dsidev, 1);
3467         dsi_vc_initial_config(dsidev, 2);
3468         dsi_vc_initial_config(dsidev, 3);
3469
3470         return 0;
3471 }
3472
3473 static void dsi_proto_timings(struct omap_dss_device *dssdev)
3474 {
3475         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3476         unsigned tlpx, tclk_zero, tclk_prepare, tclk_trail;
3477         unsigned tclk_pre, tclk_post;
3478         unsigned ths_prepare, ths_prepare_ths_zero, ths_zero;
3479         unsigned ths_trail, ths_exit;
3480         unsigned ddr_clk_pre, ddr_clk_post;
3481         unsigned enter_hs_mode_lat, exit_hs_mode_lat;
3482         unsigned ths_eot;
3483         u32 r;
3484
3485         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG0);
3486         ths_prepare = FLD_GET(r, 31, 24);
3487         ths_prepare_ths_zero = FLD_GET(r, 23, 16);
3488         ths_zero = ths_prepare_ths_zero - ths_prepare;
3489         ths_trail = FLD_GET(r, 15, 8);
3490         ths_exit = FLD_GET(r, 7, 0);
3491
3492         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG1);
3493         tlpx = FLD_GET(r, 22, 16) * 2;
3494         tclk_trail = FLD_GET(r, 15, 8);
3495         tclk_zero = FLD_GET(r, 7, 0);
3496
3497         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG2);
3498         tclk_prepare = FLD_GET(r, 7, 0);
3499
3500         /* min 8*UI */
3501         tclk_pre = 20;
3502         /* min 60ns + 52*UI */
3503         tclk_post = ns2ddr(dsidev, 60) + 26;
3504
3505         /* ths_eot is 2 for 2 datalanes and 4 for 1 datalane */
3506         if (dssdev->phy.dsi.data1_lane != 0 &&
3507                         dssdev->phy.dsi.data2_lane != 0)
3508                 ths_eot = 2;
3509         else
3510                 ths_eot = 4;
3511
3512         ddr_clk_pre = DIV_ROUND_UP(tclk_pre + tlpx + tclk_zero + tclk_prepare,
3513                         4);
3514         ddr_clk_post = DIV_ROUND_UP(tclk_post + ths_trail, 4) + ths_eot;
3515
3516         BUG_ON(ddr_clk_pre == 0 || ddr_clk_pre > 255);
3517         BUG_ON(ddr_clk_post == 0 || ddr_clk_post > 255);
3518
3519         r = dsi_read_reg(dsidev, DSI_CLK_TIMING);
3520         r = FLD_MOD(r, ddr_clk_pre, 15, 8);
3521         r = FLD_MOD(r, ddr_clk_post, 7, 0);
3522         dsi_write_reg(dsidev, DSI_CLK_TIMING, r);
3523
3524         DSSDBG("ddr_clk_pre %u, ddr_clk_post %u\n",
3525                         ddr_clk_pre,
3526                         ddr_clk_post);
3527
3528         enter_hs_mode_lat = 1 + DIV_ROUND_UP(tlpx, 4) +
3529                 DIV_ROUND_UP(ths_prepare, 4) +
3530                 DIV_ROUND_UP(ths_zero + 3, 4);
3531
3532         exit_hs_mode_lat = DIV_ROUND_UP(ths_trail + ths_exit, 4) + 1 + ths_eot;
3533
3534         r = FLD_VAL(enter_hs_mode_lat, 31, 16) |
3535                 FLD_VAL(exit_hs_mode_lat, 15, 0);
3536         dsi_write_reg(dsidev, DSI_VM_TIMING7, r);
3537
3538         DSSDBG("enter_hs_mode_lat %u, exit_hs_mode_lat %u\n",
3539                         enter_hs_mode_lat, exit_hs_mode_lat);
3540 }
3541
3542
3543 #define DSI_DECL_VARS \
3544         int __dsi_cb = 0; u32 __dsi_cv = 0;
3545
3546 #define DSI_FLUSH(dsidev, ch) \
3547         if (__dsi_cb > 0) { \
3548                 /*DSSDBG("sending long packet %#010x\n", __dsi_cv);*/ \
3549                 dsi_write_reg(dsidev, DSI_VC_LONG_PACKET_PAYLOAD(ch), __dsi_cv); \
3550                 __dsi_cb = __dsi_cv = 0; \
3551         }
3552
3553 #define DSI_PUSH(dsidev, ch, data) \
3554         do { \
3555                 __dsi_cv |= (data) << (__dsi_cb * 8); \
3556                 /*DSSDBG("cv = %#010x, cb = %d\n", __dsi_cv, __dsi_cb);*/ \
3557                 if (++__dsi_cb > 3) \
3558                         DSI_FLUSH(dsidev, ch); \
3559         } while (0)
3560
3561 static int dsi_update_screen_l4(struct omap_dss_device *dssdev,
3562                         int x, int y, int w, int h)
3563 {
3564         /* Note: supports only 24bit colors in 32bit container */
3565         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3566         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3567         int first = 1;
3568         int fifo_stalls = 0;
3569         int max_dsi_packet_size;
3570         int max_data_per_packet;
3571         int max_pixels_per_packet;
3572         int pixels_left;
3573         int bytespp = dssdev->ctrl.pixel_size / 8;
3574         int scr_width;
3575         u32 __iomem *data;
3576         int start_offset;
3577         int horiz_inc;
3578         int current_x;
3579         struct omap_overlay *ovl;
3580
3581         debug_irq = 0;
3582
3583         DSSDBG("dsi_update_screen_l4 (%d,%d %dx%d)\n",
3584                         x, y, w, h);
3585
3586         ovl = dssdev->manager->overlays[0];
3587
3588         if (ovl->info.color_mode != OMAP_DSS_COLOR_RGB24U)
3589                 return -EINVAL;
3590
3591         if (dssdev->ctrl.pixel_size != 24)
3592                 return -EINVAL;
3593
3594         scr_width = ovl->info.screen_width;
3595         data = ovl->info.vaddr;
3596
3597         start_offset = scr_width * y + x;
3598         horiz_inc = scr_width - w;
3599         current_x = x;
3600
3601         /* We need header(4) + DCSCMD(1) + pixels(numpix*bytespp) bytes
3602          * in fifo */
3603
3604         /* When using CPU, max long packet size is TX buffer size */
3605         max_dsi_packet_size = dsi->vc[0].fifo_size * 32 * 4;
3606
3607         /* we seem to get better perf if we divide the tx fifo to half,
3608            and while the other half is being sent, we fill the other half
3609            max_dsi_packet_size /= 2; */
3610
3611         max_data_per_packet = max_dsi_packet_size - 4 - 1;
3612
3613         max_pixels_per_packet = max_data_per_packet / bytespp;
3614
3615         DSSDBG("max_pixels_per_packet %d\n", max_pixels_per_packet);
3616
3617         pixels_left = w * h;
3618
3619         DSSDBG("total pixels %d\n", pixels_left);
3620
3621         data += start_offset;
3622
3623         while (pixels_left > 0) {
3624                 /* 0x2c = write_memory_start */
3625                 /* 0x3c = write_memory_continue */
3626                 u8 dcs_cmd = first ? 0x2c : 0x3c;
3627                 int pixels;
3628                 DSI_DECL_VARS;
3629                 first = 0;
3630
3631 #if 1
3632                 /* using fifo not empty */
3633                 /* TX_FIFO_NOT_EMPTY */
3634                 while (FLD_GET(dsi_read_reg(dsidev, DSI_VC_CTRL(0)), 5, 5)) {
3635                         fifo_stalls++;
3636                         if (fifo_stalls > 0xfffff) {
3637                                 DSSERR("fifo stalls overflow, pixels left %d\n",
3638                                                 pixels_left);
3639                                 dsi_if_enable(dsidev, 0);
3640                                 return -EIO;
3641                         }
3642                         udelay(1);
3643                 }
3644 #elif 1
3645                 /* using fifo emptiness */
3646                 while ((REG_GET(dsidev, DSI_TX_FIFO_VC_EMPTINESS, 7, 0)+1)*4 <
3647                                 max_dsi_packet_size) {
3648                         fifo_stalls++;
3649                         if (fifo_stalls > 0xfffff) {
3650                                 DSSERR("fifo stalls overflow, pixels left %d\n",
3651                                                pixels_left);
3652                                 dsi_if_enable(dsidev, 0);
3653                                 return -EIO;
3654                         }
3655                 }
3656 #else
3657                 while ((REG_GET(dsidev, DSI_TX_FIFO_VC_EMPTINESS,
3658                                 7, 0) + 1) * 4 == 0) {
3659                         fifo_stalls++;
3660                         if (fifo_stalls > 0xfffff) {
3661                                 DSSERR("fifo stalls overflow, pixels left %d\n",
3662                                                pixels_left);
3663                                 dsi_if_enable(dsidev, 0);
3664                                 return -EIO;
3665                         }
3666                 }
3667 #endif
3668                 pixels = min(max_pixels_per_packet, pixels_left);
3669
3670                 pixels_left -= pixels;
3671
3672                 dsi_vc_write_long_header(dsidev, 0, DSI_DT_DCS_LONG_WRITE,
3673                                 1 + pixels * bytespp, 0);
3674
3675                 DSI_PUSH(dsidev, 0, dcs_cmd);
3676
3677                 while (pixels-- > 0) {
3678                         u32 pix = __raw_readl(data++);
3679
3680                         DSI_PUSH(dsidev, 0, (pix >> 16) & 0xff);
3681                         DSI_PUSH(dsidev, 0, (pix >> 8) & 0xff);
3682                         DSI_PUSH(dsidev, 0, (pix >> 0) & 0xff);
3683
3684                         current_x++;
3685                         if (current_x == x+w) {
3686                                 current_x = x;
3687                                 data += horiz_inc;
3688                         }
3689                 }
3690
3691                 DSI_FLUSH(dsidev, 0);
3692         }
3693
3694         return 0;
3695 }
3696
3697 static void dsi_update_screen_dispc(struct omap_dss_device *dssdev,
3698                 u16 x, u16 y, u16 w, u16 h)
3699 {
3700         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3701         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3702         unsigned bytespp;
3703         unsigned bytespl;
3704         unsigned bytespf;
3705         unsigned total_len;
3706         unsigned packet_payload;
3707         unsigned packet_len;
3708         u32 l;
3709         int r;
3710         const unsigned channel = dsi->update_channel;
3711         /* line buffer is 1024 x 24bits */
3712         /* XXX: for some reason using full buffer size causes considerable TX
3713          * slowdown with update sizes that fill the whole buffer */
3714         const unsigned line_buf_size = 1023 * 3;
3715
3716         DSSDBG("dsi_update_screen_dispc(%d,%d %dx%d)\n",
3717                         x, y, w, h);
3718
3719         dsi_vc_config_vp(dsidev, channel);
3720
3721         bytespp = dssdev->ctrl.pixel_size / 8;
3722         bytespl = w * bytespp;
3723         bytespf = bytespl * h;
3724
3725         /* NOTE: packet_payload has to be equal to N * bytespl, where N is
3726          * number of lines in a packet.  See errata about VP_CLK_RATIO */
3727
3728         if (bytespf < line_buf_size)
3729                 packet_payload = bytespf;
3730         else
3731                 packet_payload = (line_buf_size) / bytespl * bytespl;
3732
3733         packet_len = packet_payload + 1;        /* 1 byte for DCS cmd */
3734         total_len = (bytespf / packet_payload) * packet_len;
3735
3736         if (bytespf % packet_payload)
3737                 total_len += (bytespf % packet_payload) + 1;
3738
3739         l = FLD_VAL(total_len, 23, 0); /* TE_SIZE */
3740         dsi_write_reg(dsidev, DSI_VC_TE(channel), l);
3741
3742         dsi_vc_write_long_header(dsidev, channel, DSI_DT_DCS_LONG_WRITE,
3743                 packet_len, 0);
3744
3745         if (dsi->te_enabled)
3746                 l = FLD_MOD(l, 1, 30, 30); /* TE_EN */
3747         else
3748                 l = FLD_MOD(l, 1, 31, 31); /* TE_START */
3749         dsi_write_reg(dsidev, DSI_VC_TE(channel), l);
3750
3751         /* We put SIDLEMODE to no-idle for the duration of the transfer,
3752          * because DSS interrupts are not capable of waking up the CPU and the
3753          * framedone interrupt could be delayed for quite a long time. I think
3754          * the same goes for any DSS interrupts, but for some reason I have not
3755          * seen the problem anywhere else than here.
3756          */
3757         dispc_disable_sidle();
3758
3759         dsi_perf_mark_start(dsidev);
3760
3761         r = queue_delayed_work(dsi->workqueue, &dsi->framedone_timeout_work,
3762                         msecs_to_jiffies(250));
3763         BUG_ON(r == 0);
3764
3765         dss_start_update(dssdev);
3766
3767         if (dsi->te_enabled) {
3768                 /* disable LP_RX_TO, so that we can receive TE.  Time to wait
3769                  * for TE is longer than the timer allows */
3770                 REG_FLD_MOD(dsidev, DSI_TIMING2, 0, 15, 15); /* LP_RX_TO */
3771
3772                 dsi_vc_send_bta(dsidev, channel);
3773
3774 #ifdef DSI_CATCH_MISSING_TE
3775                 mod_timer(&dsi->te_timer, jiffies + msecs_to_jiffies(250));
3776 #endif
3777         }
3778 }
3779
3780 #ifdef DSI_CATCH_MISSING_TE
3781 static void dsi_te_timeout(unsigned long arg)
3782 {
3783         DSSERR("TE not received for 250ms!\n");
3784 }
3785 #endif
3786
3787 static void dsi_handle_framedone(struct platform_device *dsidev, int error)
3788 {
3789         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3790
3791         /* SIDLEMODE back to smart-idle */
3792         dispc_enable_sidle();
3793
3794         if (dsi->te_enabled) {
3795                 /* enable LP_RX_TO again after the TE */
3796                 REG_FLD_MOD(dsidev, DSI_TIMING2, 1, 15, 15); /* LP_RX_TO */
3797         }
3798
3799         dsi->framedone_callback(error, dsi->framedone_data);
3800
3801         if (!error)
3802                 dsi_perf_show(dsidev, "DISPC");
3803 }
3804
3805 static void dsi_framedone_timeout_work_callback(struct work_struct *work)
3806 {
3807         struct dsi_data *dsi = container_of(work, struct dsi_data,
3808                         framedone_timeout_work.work);
3809         /* XXX While extremely unlikely, we could get FRAMEDONE interrupt after
3810          * 250ms which would conflict with this timeout work. What should be
3811          * done is first cancel the transfer on the HW, and then cancel the
3812          * possibly scheduled framedone work. However, cancelling the transfer
3813          * on the HW is buggy, and would probably require resetting the whole
3814          * DSI */
3815
3816         DSSERR("Framedone not received for 250ms!\n");
3817
3818         dsi_handle_framedone(dsi->pdev, -ETIMEDOUT);
3819 }
3820
3821 static void dsi_framedone_irq_callback(void *data, u32 mask)
3822 {
3823         struct omap_dss_device *dssdev = (struct omap_dss_device *) data;
3824         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3825         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3826
3827         /* Note: We get FRAMEDONE when DISPC has finished sending pixels and
3828          * turns itself off. However, DSI still has the pixels in its buffers,
3829          * and is sending the data.
3830          */
3831
3832         __cancel_delayed_work(&dsi->framedone_timeout_work);
3833
3834         dsi_handle_framedone(dsidev, 0);
3835
3836 #ifdef CONFIG_OMAP2_DSS_FAKE_VSYNC
3837         dispc_fake_vsync_irq();
3838 #endif
3839 }
3840
3841 int omap_dsi_prepare_update(struct omap_dss_device *dssdev,
3842                                     u16 *x, u16 *y, u16 *w, u16 *h,
3843                                     bool enlarge_update_area)
3844 {
3845         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3846         u16 dw, dh;
3847
3848         dssdev->driver->get_resolution(dssdev, &dw, &dh);
3849
3850         if  (*x > dw || *y > dh)
3851                 return -EINVAL;
3852
3853         if (*x + *w > dw)
3854                 return -EINVAL;
3855
3856         if (*y + *h > dh)
3857                 return -EINVAL;
3858
3859         if (*w == 1)
3860                 return -EINVAL;
3861
3862         if (*w == 0 || *h == 0)
3863                 return -EINVAL;
3864
3865         dsi_perf_mark_setup(dsidev);
3866
3867         if (dssdev->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
3868                 dss_setup_partial_planes(dssdev, x, y, w, h,
3869                                 enlarge_update_area);
3870                 dispc_set_lcd_size(dssdev->manager->id, *w, *h);
3871         }
3872
3873         return 0;
3874 }
3875 EXPORT_SYMBOL(omap_dsi_prepare_update);
3876
3877 int omap_dsi_update(struct omap_dss_device *dssdev,
3878                 int channel,
3879                 u16 x, u16 y, u16 w, u16 h,
3880                 void (*callback)(int, void *), void *data)
3881 {
3882         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3883         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3884
3885         dsi->update_channel = channel;
3886
3887         /* OMAP DSS cannot send updates of odd widths.
3888          * omap_dsi_prepare_update() makes the widths even, but add a BUG_ON
3889          * here to make sure we catch erroneous updates. Otherwise we'll only
3890          * see rather obscure HW error happening, as DSS halts. */
3891         BUG_ON(x % 2 == 1);
3892
3893         if (dssdev->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
3894                 dsi->framedone_callback = callback;
3895                 dsi->framedone_data = data;
3896
3897                 dsi->update_region.x = x;
3898                 dsi->update_region.y = y;
3899                 dsi->update_region.w = w;
3900                 dsi->update_region.h = h;
3901                 dsi->update_region.device = dssdev;
3902
3903                 dsi_update_screen_dispc(dssdev, x, y, w, h);
3904         } else {
3905                 int r;
3906
3907                 r = dsi_update_screen_l4(dssdev, x, y, w, h);
3908                 if (r)
3909                         return r;
3910
3911                 dsi_perf_show(dsidev, "L4");
3912                 callback(0, data);
3913         }
3914
3915         return 0;
3916 }
3917 EXPORT_SYMBOL(omap_dsi_update);
3918
3919 /* Display funcs */
3920
3921 static int dsi_display_init_dispc(struct omap_dss_device *dssdev)
3922 {
3923         int r;
3924         u32 irq;
3925
3926         irq = dssdev->manager->id == OMAP_DSS_CHANNEL_LCD ?
3927                 DISPC_IRQ_FRAMEDONE : DISPC_IRQ_FRAMEDONE2;
3928
3929         r = omap_dispc_register_isr(dsi_framedone_irq_callback, (void *) dssdev,
3930                         irq);
3931         if (r) {
3932                 DSSERR("can't get FRAMEDONE irq\n");
3933                 return r;
3934         }
3935
3936         dispc_set_lcd_display_type(dssdev->manager->id,
3937                         OMAP_DSS_LCD_DISPLAY_TFT);
3938
3939         dispc_set_parallel_interface_mode(dssdev->manager->id,
3940                         OMAP_DSS_PARALLELMODE_DSI);
3941         dispc_enable_fifohandcheck(dssdev->manager->id, 1);
3942
3943         dispc_set_tft_data_lines(dssdev->manager->id, dssdev->ctrl.pixel_size);
3944
3945         {
3946                 struct omap_video_timings timings = {
3947                         .hsw            = 1,
3948                         .hfp            = 1,
3949                         .hbp            = 1,
3950                         .vsw            = 1,
3951                         .vfp            = 0,
3952                         .vbp            = 0,
3953                 };
3954
3955                 dispc_set_lcd_timings(dssdev->manager->id, &timings);
3956         }
3957
3958         return 0;
3959 }
3960
3961 static void dsi_display_uninit_dispc(struct omap_dss_device *dssdev)
3962 {
3963         u32 irq;
3964
3965         irq = dssdev->manager->id == OMAP_DSS_CHANNEL_LCD ?
3966                 DISPC_IRQ_FRAMEDONE : DISPC_IRQ_FRAMEDONE2;
3967
3968         omap_dispc_unregister_isr(dsi_framedone_irq_callback, (void *) dssdev,
3969                         irq);
3970 }
3971
3972 static int dsi_configure_dsi_clocks(struct omap_dss_device *dssdev)
3973 {
3974         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3975         struct dsi_clock_info cinfo;
3976         int r;
3977
3978         /* we always use DSS_CLK_SYSCK as input clock */
3979         cinfo.use_sys_clk = true;
3980         cinfo.regn  = dssdev->clocks.dsi.regn;
3981         cinfo.regm  = dssdev->clocks.dsi.regm;
3982         cinfo.regm_dispc = dssdev->clocks.dsi.regm_dispc;
3983         cinfo.regm_dsi = dssdev->clocks.dsi.regm_dsi;
3984         r = dsi_calc_clock_rates(dssdev, &cinfo);
3985         if (r) {
3986                 DSSERR("Failed to calc dsi clocks\n");
3987                 return r;
3988         }
3989
3990         r = dsi_pll_set_clock_div(dsidev, &cinfo);
3991         if (r) {
3992                 DSSERR("Failed to set dsi clocks\n");
3993                 return r;
3994         }
3995
3996         return 0;
3997 }
3998
3999 static int dsi_configure_dispc_clocks(struct omap_dss_device *dssdev)
4000 {
4001         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4002         struct dispc_clock_info dispc_cinfo;
4003         int r;
4004         unsigned long long fck;
4005
4006         fck = dsi_get_pll_hsdiv_dispc_rate(dsidev);
4007
4008         dispc_cinfo.lck_div = dssdev->clocks.dispc.channel.lck_div;
4009         dispc_cinfo.pck_div = dssdev->clocks.dispc.channel.pck_div;
4010
4011         r = dispc_calc_clock_rates(fck, &dispc_cinfo);
4012         if (r) {
4013                 DSSERR("Failed to calc dispc clocks\n");
4014                 return r;
4015         }
4016
4017         r = dispc_set_clock_div(dssdev->manager->id, &dispc_cinfo);
4018         if (r) {
4019                 DSSERR("Failed to set dispc clocks\n");
4020                 return r;
4021         }
4022
4023         return 0;
4024 }
4025
4026 static int dsi_display_init_dsi(struct omap_dss_device *dssdev)
4027 {
4028         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4029         int dsi_module = dsi_get_dsidev_id(dsidev);
4030         int r;
4031
4032         r = dsi_pll_init(dsidev, true, true);
4033         if (r)
4034                 goto err0;
4035
4036         r = dsi_configure_dsi_clocks(dssdev);
4037         if (r)
4038                 goto err1;
4039
4040         dss_select_dispc_clk_source(dssdev->clocks.dispc.dispc_fclk_src);
4041         dss_select_dsi_clk_source(dsi_module, dssdev->clocks.dsi.dsi_fclk_src);
4042         dss_select_lcd_clk_source(dssdev->manager->id,
4043                         dssdev->clocks.dispc.channel.lcd_clk_src);
4044
4045         DSSDBG("PLL OK\n");
4046
4047         r = dsi_configure_dispc_clocks(dssdev);
4048         if (r)
4049                 goto err2;
4050
4051         r = dsi_cio_init(dssdev);
4052         if (r)
4053                 goto err2;
4054
4055         _dsi_print_reset_status(dsidev);
4056
4057         dsi_proto_timings(dssdev);
4058         dsi_set_lp_clk_divisor(dssdev);
4059
4060         if (1)
4061                 _dsi_print_reset_status(dsidev);
4062
4063         r = dsi_proto_config(dssdev);
4064         if (r)
4065                 goto err3;
4066
4067         /* enable interface */
4068         dsi_vc_enable(dsidev, 0, 1);
4069         dsi_vc_enable(dsidev, 1, 1);
4070         dsi_vc_enable(dsidev, 2, 1);
4071         dsi_vc_enable(dsidev, 3, 1);
4072         dsi_if_enable(dsidev, 1);
4073         dsi_force_tx_stop_mode_io(dsidev);
4074
4075         return 0;
4076 err3:
4077         dsi_cio_uninit(dsidev);
4078 err2:
4079         dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
4080         dss_select_dsi_clk_source(dsi_module, OMAP_DSS_CLK_SRC_FCK);
4081 err1:
4082         dsi_pll_uninit(dsidev, true);
4083 err0:
4084         return r;
4085 }
4086
4087 static void dsi_display_uninit_dsi(struct omap_dss_device *dssdev,
4088                 bool disconnect_lanes, bool enter_ulps)
4089 {
4090         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4091         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4092         int dsi_module = dsi_get_dsidev_id(dsidev);
4093
4094         if (enter_ulps && !dsi->ulps_enabled)
4095                 dsi_enter_ulps(dsidev);
4096
4097         /* disable interface */
4098         dsi_if_enable(dsidev, 0);
4099         dsi_vc_enable(dsidev, 0, 0);
4100         dsi_vc_enable(dsidev, 1, 0);
4101         dsi_vc_enable(dsidev, 2, 0);
4102         dsi_vc_enable(dsidev, 3, 0);
4103
4104         dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
4105         dss_select_dsi_clk_source(dsi_module, OMAP_DSS_CLK_SRC_FCK);
4106         dsi_cio_uninit(dsidev);
4107         dsi_pll_uninit(dsidev, disconnect_lanes);
4108 }
4109
4110 static int dsi_core_init(struct platform_device *dsidev)
4111 {
4112         /* Autoidle */
4113         REG_FLD_MOD(dsidev, DSI_SYSCONFIG, 1, 0, 0);
4114
4115         /* ENWAKEUP */
4116         REG_FLD_MOD(dsidev, DSI_SYSCONFIG, 1, 2, 2);
4117
4118         /* SIDLEMODE smart-idle */
4119         REG_FLD_MOD(dsidev, DSI_SYSCONFIG, 2, 4, 3);
4120
4121         _dsi_initialize_irq(dsidev);
4122
4123         return 0;
4124 }
4125
4126 int omapdss_dsi_display_enable(struct omap_dss_device *dssdev)
4127 {
4128         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4129         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4130         int r = 0;
4131
4132         DSSDBG("dsi_display_enable\n");
4133
4134         WARN_ON(!dsi_bus_is_locked(dsidev));
4135
4136         mutex_lock(&dsi->lock);
4137
4138         r = omap_dss_start_device(dssdev);
4139         if (r) {
4140                 DSSERR("failed to start device\n");
4141                 goto err0;
4142         }
4143
4144         enable_clocks(1);
4145         dsi_enable_pll_clock(dsidev, 1);
4146
4147         r = _dsi_reset(dsidev);
4148         if (r)
4149                 goto err1;
4150
4151         dsi_core_init(dsidev);
4152
4153         r = dsi_display_init_dispc(dssdev);
4154         if (r)
4155                 goto err1;
4156
4157         r = dsi_display_init_dsi(dssdev);
4158         if (r)
4159                 goto err2;
4160
4161         mutex_unlock(&dsi->lock);
4162
4163         return 0;
4164
4165 err2:
4166         dsi_display_uninit_dispc(dssdev);
4167 err1:
4168         enable_clocks(0);
4169         dsi_enable_pll_clock(dsidev, 0);
4170         omap_dss_stop_device(dssdev);
4171 err0:
4172         mutex_unlock(&dsi->lock);
4173         DSSDBG("dsi_display_enable FAILED\n");
4174         return r;
4175 }
4176 EXPORT_SYMBOL(omapdss_dsi_display_enable);
4177
4178 void omapdss_dsi_display_disable(struct omap_dss_device *dssdev,
4179                 bool disconnect_lanes, bool enter_ulps)
4180 {
4181         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4182         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4183
4184         DSSDBG("dsi_display_disable\n");
4185
4186         WARN_ON(!dsi_bus_is_locked(dsidev));
4187
4188         mutex_lock(&dsi->lock);
4189
4190         dsi_display_uninit_dispc(dssdev);
4191
4192         dsi_display_uninit_dsi(dssdev, disconnect_lanes, enter_ulps);
4193
4194         enable_clocks(0);
4195         dsi_enable_pll_clock(dsidev, 0);
4196
4197         omap_dss_stop_device(dssdev);
4198
4199         mutex_unlock(&dsi->lock);
4200 }
4201 EXPORT_SYMBOL(omapdss_dsi_display_disable);
4202
4203 int omapdss_dsi_enable_te(struct omap_dss_device *dssdev, bool enable)
4204 {
4205         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4206         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4207
4208         dsi->te_enabled = enable;
4209         return 0;
4210 }
4211 EXPORT_SYMBOL(omapdss_dsi_enable_te);
4212
4213 void dsi_get_overlay_fifo_thresholds(enum omap_plane plane,
4214                 u32 fifo_size, enum omap_burst_size *burst_size,
4215                 u32 *fifo_low, u32 *fifo_high)
4216 {
4217         unsigned burst_size_bytes;
4218
4219         *burst_size = OMAP_DSS_BURST_16x32;
4220         burst_size_bytes = 16 * 32 / 8;
4221
4222         *fifo_high = fifo_size - burst_size_bytes;
4223         *fifo_low = fifo_size - burst_size_bytes * 2;
4224 }
4225
4226 int dsi_init_display(struct omap_dss_device *dssdev)
4227 {
4228         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4229         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4230
4231         DSSDBG("DSI init\n");
4232
4233         /* XXX these should be figured out dynamically */
4234         dssdev->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE |
4235                 OMAP_DSS_DISPLAY_CAP_TEAR_ELIM;
4236
4237         if (dsi->vdds_dsi_reg == NULL) {
4238                 struct regulator *vdds_dsi;
4239
4240                 vdds_dsi = regulator_get(&dsi->pdev->dev, "vdds_dsi");
4241
4242                 if (IS_ERR(vdds_dsi)) {
4243                         DSSERR("can't get VDDS_DSI regulator\n");
4244                         return PTR_ERR(vdds_dsi);
4245                 }
4246
4247                 dsi->vdds_dsi_reg = vdds_dsi;
4248         }
4249
4250         return 0;
4251 }
4252
4253 int omap_dsi_request_vc(struct omap_dss_device *dssdev, int *channel)
4254 {
4255         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4256         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4257         int i;
4258
4259         for (i = 0; i < ARRAY_SIZE(dsi->vc); i++) {
4260                 if (!dsi->vc[i].dssdev) {
4261                         dsi->vc[i].dssdev = dssdev;
4262                         *channel = i;
4263                         return 0;
4264                 }
4265         }
4266
4267         DSSERR("cannot get VC for display %s", dssdev->name);
4268         return -ENOSPC;
4269 }
4270 EXPORT_SYMBOL(omap_dsi_request_vc);
4271
4272 int omap_dsi_set_vc_id(struct omap_dss_device *dssdev, int channel, int vc_id)
4273 {
4274         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4275         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4276
4277         if (vc_id < 0 || vc_id > 3) {
4278                 DSSERR("VC ID out of range\n");
4279                 return -EINVAL;
4280         }
4281
4282         if (channel < 0 || channel > 3) {
4283                 DSSERR("Virtual Channel out of range\n");
4284                 return -EINVAL;
4285         }
4286
4287         if (dsi->vc[channel].dssdev != dssdev) {
4288                 DSSERR("Virtual Channel not allocated to display %s\n",
4289                         dssdev->name);
4290                 return -EINVAL;
4291         }
4292
4293         dsi->vc[channel].vc_id = vc_id;
4294
4295         return 0;
4296 }
4297 EXPORT_SYMBOL(omap_dsi_set_vc_id);
4298
4299 void omap_dsi_release_vc(struct omap_dss_device *dssdev, int channel)
4300 {
4301         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4302         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4303
4304         if ((channel >= 0 && channel <= 3) &&
4305                 dsi->vc[channel].dssdev == dssdev) {
4306                 dsi->vc[channel].dssdev = NULL;
4307                 dsi->vc[channel].vc_id = 0;
4308         }
4309 }
4310 EXPORT_SYMBOL(omap_dsi_release_vc);
4311
4312 void dsi_wait_pll_hsdiv_dispc_active(struct platform_device *dsidev)
4313 {
4314         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 7, 1) != 1)
4315                 DSSERR("%s (%s) not active\n",
4316                         dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC),
4317                         dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC));
4318 }
4319
4320 void dsi_wait_pll_hsdiv_dsi_active(struct platform_device *dsidev)
4321 {
4322         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 8, 1) != 1)
4323                 DSSERR("%s (%s) not active\n",
4324                         dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI),
4325                         dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI));
4326 }
4327
4328 static void dsi_calc_clock_param_ranges(struct platform_device *dsidev)
4329 {
4330         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4331
4332         dsi->regn_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGN);
4333         dsi->regm_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM);
4334         dsi->regm_dispc_max =
4335                 dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM_DISPC);
4336         dsi->regm_dsi_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM_DSI);
4337         dsi->fint_min = dss_feat_get_param_min(FEAT_PARAM_DSIPLL_FINT);
4338         dsi->fint_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_FINT);
4339         dsi->lpdiv_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_LPDIV);
4340 }
4341
4342 static int dsi_init(struct platform_device *dsidev)
4343 {
4344         struct omap_display_platform_data *dss_plat_data;
4345         struct omap_dss_board_info *board_info;
4346         u32 rev;
4347         int r, i, dsi_module = dsi_get_dsidev_id(dsidev);
4348         struct resource *dsi_mem;
4349         struct dsi_data *dsi;
4350
4351         dsi = kzalloc(sizeof(*dsi), GFP_KERNEL);
4352         if (!dsi) {
4353                 r = -ENOMEM;
4354                 goto err0;
4355         }
4356
4357         dsi->pdev = dsidev;
4358         dsi_pdev_map[dsi_module] = dsidev;
4359         dev_set_drvdata(&dsidev->dev, dsi);
4360
4361         dss_plat_data = dsidev->dev.platform_data;
4362         board_info = dss_plat_data->board_data;
4363         dsi->dsi_mux_pads = board_info->dsi_mux_pads;
4364
4365         spin_lock_init(&dsi->irq_lock);
4366         spin_lock_init(&dsi->errors_lock);
4367         dsi->errors = 0;
4368
4369 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
4370         spin_lock_init(&dsi->irq_stats_lock);
4371         dsi->irq_stats.last_reset = jiffies;
4372 #endif
4373
4374         mutex_init(&dsi->lock);
4375         sema_init(&dsi->bus_lock, 1);
4376
4377         dsi->workqueue = create_singlethread_workqueue(dev_name(&dsidev->dev));
4378         if (dsi->workqueue == NULL) {
4379                 r = -ENOMEM;
4380                 goto err1;
4381         }
4382
4383         INIT_DELAYED_WORK_DEFERRABLE(&dsi->framedone_timeout_work,
4384                         dsi_framedone_timeout_work_callback);
4385
4386 #ifdef DSI_CATCH_MISSING_TE
4387         init_timer(&dsi->te_timer);
4388         dsi->te_timer.function = dsi_te_timeout;
4389         dsi->te_timer.data = 0;
4390 #endif
4391         dsi_mem = platform_get_resource(dsi->pdev, IORESOURCE_MEM, 0);
4392         if (!dsi_mem) {
4393                 DSSERR("can't get IORESOURCE_MEM DSI\n");
4394                 r = -EINVAL;
4395                 goto err2;
4396         }
4397         dsi->base = ioremap(dsi_mem->start, resource_size(dsi_mem));
4398         if (!dsi->base) {
4399                 DSSERR("can't ioremap DSI\n");
4400                 r = -ENOMEM;
4401                 goto err2;
4402         }
4403         dsi->irq = platform_get_irq(dsi->pdev, 0);
4404         if (dsi->irq < 0) {
4405                 DSSERR("platform_get_irq failed\n");
4406                 r = -ENODEV;
4407                 goto err3;
4408         }
4409
4410         r = request_irq(dsi->irq, omap_dsi_irq_handler, IRQF_SHARED,
4411                 dev_name(&dsidev->dev), dsi->pdev);
4412         if (r < 0) {
4413                 DSSERR("request_irq failed\n");
4414                 goto err3;
4415         }
4416
4417         /* DSI VCs initialization */
4418         for (i = 0; i < ARRAY_SIZE(dsi->vc); i++) {
4419                 dsi->vc[i].mode = DSI_VC_MODE_L4;
4420                 dsi->vc[i].dssdev = NULL;
4421                 dsi->vc[i].vc_id = 0;
4422         }
4423
4424         dsi_calc_clock_param_ranges(dsidev);
4425
4426         enable_clocks(1);
4427
4428         rev = dsi_read_reg(dsidev, DSI_REVISION);
4429         dev_dbg(&dsidev->dev, "OMAP DSI rev %d.%d\n",
4430                FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
4431
4432         enable_clocks(0);
4433
4434         return 0;
4435 err3:
4436         iounmap(dsi->base);
4437 err2:
4438         destroy_workqueue(dsi->workqueue);
4439 err1:
4440         kfree(dsi);
4441 err0:
4442         return r;
4443 }
4444
4445 static void dsi_exit(struct platform_device *dsidev)
4446 {
4447         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4448
4449         if (dsi->vdds_dsi_reg != NULL) {
4450                 if (dsi->vdds_dsi_enabled) {
4451                         regulator_disable(dsi->vdds_dsi_reg);
4452                         dsi->vdds_dsi_enabled = false;
4453                 }
4454
4455                 regulator_put(dsi->vdds_dsi_reg);
4456                 dsi->vdds_dsi_reg = NULL;
4457         }
4458
4459         free_irq(dsi->irq, dsi->pdev);
4460         iounmap(dsi->base);
4461
4462         destroy_workqueue(dsi->workqueue);
4463         kfree(dsi);
4464
4465         DSSDBG("omap_dsi_exit\n");
4466 }
4467
4468 /* DSI1 HW IP initialisation */
4469 static int omap_dsi1hw_probe(struct platform_device *dsidev)
4470 {
4471         int r;
4472
4473         r = dsi_init(dsidev);
4474         if (r) {
4475                 DSSERR("Failed to initialize DSI\n");
4476                 goto err_dsi;
4477         }
4478 err_dsi:
4479         return r;
4480 }
4481
4482 static int omap_dsi1hw_remove(struct platform_device *dsidev)
4483 {
4484         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4485
4486         dsi_exit(dsidev);
4487         WARN_ON(dsi->scp_clk_refcount > 0);
4488         return 0;
4489 }
4490
4491 static struct platform_driver omap_dsi1hw_driver = {
4492         .probe          = omap_dsi1hw_probe,
4493         .remove         = omap_dsi1hw_remove,
4494         .driver         = {
4495                 .name   = "omapdss_dsi1",
4496                 .owner  = THIS_MODULE,
4497         },
4498 };
4499
4500 int dsi_init_platform_driver(void)
4501 {
4502         return platform_driver_register(&omap_dsi1hw_driver);
4503 }
4504
4505 void dsi_uninit_platform_driver(void)
4506 {
4507         return platform_driver_unregister(&omap_dsi1hw_driver);
4508 }