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