]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/video/fbdev/omap2/dss/dsi.c
Merge tag 'kvm-arm-for-3.18-take-2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / video / fbdev / 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/module.h>
31 #include <linux/semaphore.h>
32 #include <linux/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/wait.h>
36 #include <linux/workqueue.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/debugfs.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/of.h>
42 #include <linux/of_platform.h>
43
44 #include <video/omapdss.h>
45 #include <video/mipi_display.h>
46
47 #include "dss.h"
48 #include "dss_features.h"
49
50 #define DSI_CATCH_MISSING_TE
51
52 struct dsi_reg { u16 module; u16 idx; };
53
54 #define DSI_REG(mod, idx)               ((const struct dsi_reg) { mod, idx })
55
56 /* DSI Protocol Engine */
57
58 #define DSI_PROTO                       0
59 #define DSI_PROTO_SZ                    0x200
60
61 #define DSI_REVISION                    DSI_REG(DSI_PROTO, 0x0000)
62 #define DSI_SYSCONFIG                   DSI_REG(DSI_PROTO, 0x0010)
63 #define DSI_SYSSTATUS                   DSI_REG(DSI_PROTO, 0x0014)
64 #define DSI_IRQSTATUS                   DSI_REG(DSI_PROTO, 0x0018)
65 #define DSI_IRQENABLE                   DSI_REG(DSI_PROTO, 0x001C)
66 #define DSI_CTRL                        DSI_REG(DSI_PROTO, 0x0040)
67 #define DSI_GNQ                         DSI_REG(DSI_PROTO, 0x0044)
68 #define DSI_COMPLEXIO_CFG1              DSI_REG(DSI_PROTO, 0x0048)
69 #define DSI_COMPLEXIO_IRQ_STATUS        DSI_REG(DSI_PROTO, 0x004C)
70 #define DSI_COMPLEXIO_IRQ_ENABLE        DSI_REG(DSI_PROTO, 0x0050)
71 #define DSI_CLK_CTRL                    DSI_REG(DSI_PROTO, 0x0054)
72 #define DSI_TIMING1                     DSI_REG(DSI_PROTO, 0x0058)
73 #define DSI_TIMING2                     DSI_REG(DSI_PROTO, 0x005C)
74 #define DSI_VM_TIMING1                  DSI_REG(DSI_PROTO, 0x0060)
75 #define DSI_VM_TIMING2                  DSI_REG(DSI_PROTO, 0x0064)
76 #define DSI_VM_TIMING3                  DSI_REG(DSI_PROTO, 0x0068)
77 #define DSI_CLK_TIMING                  DSI_REG(DSI_PROTO, 0x006C)
78 #define DSI_TX_FIFO_VC_SIZE             DSI_REG(DSI_PROTO, 0x0070)
79 #define DSI_RX_FIFO_VC_SIZE             DSI_REG(DSI_PROTO, 0x0074)
80 #define DSI_COMPLEXIO_CFG2              DSI_REG(DSI_PROTO, 0x0078)
81 #define DSI_RX_FIFO_VC_FULLNESS         DSI_REG(DSI_PROTO, 0x007C)
82 #define DSI_VM_TIMING4                  DSI_REG(DSI_PROTO, 0x0080)
83 #define DSI_TX_FIFO_VC_EMPTINESS        DSI_REG(DSI_PROTO, 0x0084)
84 #define DSI_VM_TIMING5                  DSI_REG(DSI_PROTO, 0x0088)
85 #define DSI_VM_TIMING6                  DSI_REG(DSI_PROTO, 0x008C)
86 #define DSI_VM_TIMING7                  DSI_REG(DSI_PROTO, 0x0090)
87 #define DSI_STOPCLK_TIMING              DSI_REG(DSI_PROTO, 0x0094)
88 #define DSI_VC_CTRL(n)                  DSI_REG(DSI_PROTO, 0x0100 + (n * 0x20))
89 #define DSI_VC_TE(n)                    DSI_REG(DSI_PROTO, 0x0104 + (n * 0x20))
90 #define DSI_VC_LONG_PACKET_HEADER(n)    DSI_REG(DSI_PROTO, 0x0108 + (n * 0x20))
91 #define DSI_VC_LONG_PACKET_PAYLOAD(n)   DSI_REG(DSI_PROTO, 0x010C + (n * 0x20))
92 #define DSI_VC_SHORT_PACKET_HEADER(n)   DSI_REG(DSI_PROTO, 0x0110 + (n * 0x20))
93 #define DSI_VC_IRQSTATUS(n)             DSI_REG(DSI_PROTO, 0x0118 + (n * 0x20))
94 #define DSI_VC_IRQENABLE(n)             DSI_REG(DSI_PROTO, 0x011C + (n * 0x20))
95
96 /* DSIPHY_SCP */
97
98 #define DSI_PHY                         1
99 #define DSI_PHY_OFFSET                  0x200
100 #define DSI_PHY_SZ                      0x40
101
102 #define DSI_DSIPHY_CFG0                 DSI_REG(DSI_PHY, 0x0000)
103 #define DSI_DSIPHY_CFG1                 DSI_REG(DSI_PHY, 0x0004)
104 #define DSI_DSIPHY_CFG2                 DSI_REG(DSI_PHY, 0x0008)
105 #define DSI_DSIPHY_CFG5                 DSI_REG(DSI_PHY, 0x0014)
106 #define DSI_DSIPHY_CFG10                DSI_REG(DSI_PHY, 0x0028)
107
108 /* DSI_PLL_CTRL_SCP */
109
110 #define DSI_PLL                         2
111 #define DSI_PLL_OFFSET                  0x300
112 #define DSI_PLL_SZ                      0x20
113
114 #define DSI_PLL_CONTROL                 DSI_REG(DSI_PLL, 0x0000)
115 #define DSI_PLL_STATUS                  DSI_REG(DSI_PLL, 0x0004)
116 #define DSI_PLL_GO                      DSI_REG(DSI_PLL, 0x0008)
117 #define DSI_PLL_CONFIGURATION1          DSI_REG(DSI_PLL, 0x000C)
118 #define DSI_PLL_CONFIGURATION2          DSI_REG(DSI_PLL, 0x0010)
119
120 #define REG_GET(dsidev, idx, start, end) \
121         FLD_GET(dsi_read_reg(dsidev, idx), start, end)
122
123 #define REG_FLD_MOD(dsidev, idx, val, start, end) \
124         dsi_write_reg(dsidev, idx, FLD_MOD(dsi_read_reg(dsidev, idx), val, start, end))
125
126 /* Global interrupts */
127 #define DSI_IRQ_VC0             (1 << 0)
128 #define DSI_IRQ_VC1             (1 << 1)
129 #define DSI_IRQ_VC2             (1 << 2)
130 #define DSI_IRQ_VC3             (1 << 3)
131 #define DSI_IRQ_WAKEUP          (1 << 4)
132 #define DSI_IRQ_RESYNC          (1 << 5)
133 #define DSI_IRQ_PLL_LOCK        (1 << 7)
134 #define DSI_IRQ_PLL_UNLOCK      (1 << 8)
135 #define DSI_IRQ_PLL_RECALL      (1 << 9)
136 #define DSI_IRQ_COMPLEXIO_ERR   (1 << 10)
137 #define DSI_IRQ_HS_TX_TIMEOUT   (1 << 14)
138 #define DSI_IRQ_LP_RX_TIMEOUT   (1 << 15)
139 #define DSI_IRQ_TE_TRIGGER      (1 << 16)
140 #define DSI_IRQ_ACK_TRIGGER     (1 << 17)
141 #define DSI_IRQ_SYNC_LOST       (1 << 18)
142 #define DSI_IRQ_LDO_POWER_GOOD  (1 << 19)
143 #define DSI_IRQ_TA_TIMEOUT      (1 << 20)
144 #define DSI_IRQ_ERROR_MASK \
145         (DSI_IRQ_HS_TX_TIMEOUT | DSI_IRQ_LP_RX_TIMEOUT | DSI_IRQ_SYNC_LOST | \
146         DSI_IRQ_TA_TIMEOUT | DSI_IRQ_SYNC_LOST)
147 #define DSI_IRQ_CHANNEL_MASK    0xf
148
149 /* Virtual channel interrupts */
150 #define DSI_VC_IRQ_CS           (1 << 0)
151 #define DSI_VC_IRQ_ECC_CORR     (1 << 1)
152 #define DSI_VC_IRQ_PACKET_SENT  (1 << 2)
153 #define DSI_VC_IRQ_FIFO_TX_OVF  (1 << 3)
154 #define DSI_VC_IRQ_FIFO_RX_OVF  (1 << 4)
155 #define DSI_VC_IRQ_BTA          (1 << 5)
156 #define DSI_VC_IRQ_ECC_NO_CORR  (1 << 6)
157 #define DSI_VC_IRQ_FIFO_TX_UDF  (1 << 7)
158 #define DSI_VC_IRQ_PP_BUSY_CHANGE (1 << 8)
159 #define DSI_VC_IRQ_ERROR_MASK \
160         (DSI_VC_IRQ_CS | DSI_VC_IRQ_ECC_CORR | DSI_VC_IRQ_FIFO_TX_OVF | \
161         DSI_VC_IRQ_FIFO_RX_OVF | DSI_VC_IRQ_ECC_NO_CORR | \
162         DSI_VC_IRQ_FIFO_TX_UDF)
163
164 /* ComplexIO interrupts */
165 #define DSI_CIO_IRQ_ERRSYNCESC1         (1 << 0)
166 #define DSI_CIO_IRQ_ERRSYNCESC2         (1 << 1)
167 #define DSI_CIO_IRQ_ERRSYNCESC3         (1 << 2)
168 #define DSI_CIO_IRQ_ERRSYNCESC4         (1 << 3)
169 #define DSI_CIO_IRQ_ERRSYNCESC5         (1 << 4)
170 #define DSI_CIO_IRQ_ERRESC1             (1 << 5)
171 #define DSI_CIO_IRQ_ERRESC2             (1 << 6)
172 #define DSI_CIO_IRQ_ERRESC3             (1 << 7)
173 #define DSI_CIO_IRQ_ERRESC4             (1 << 8)
174 #define DSI_CIO_IRQ_ERRESC5             (1 << 9)
175 #define DSI_CIO_IRQ_ERRCONTROL1         (1 << 10)
176 #define DSI_CIO_IRQ_ERRCONTROL2         (1 << 11)
177 #define DSI_CIO_IRQ_ERRCONTROL3         (1 << 12)
178 #define DSI_CIO_IRQ_ERRCONTROL4         (1 << 13)
179 #define DSI_CIO_IRQ_ERRCONTROL5         (1 << 14)
180 #define DSI_CIO_IRQ_STATEULPS1          (1 << 15)
181 #define DSI_CIO_IRQ_STATEULPS2          (1 << 16)
182 #define DSI_CIO_IRQ_STATEULPS3          (1 << 17)
183 #define DSI_CIO_IRQ_STATEULPS4          (1 << 18)
184 #define DSI_CIO_IRQ_STATEULPS5          (1 << 19)
185 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_1  (1 << 20)
186 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_1  (1 << 21)
187 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_2  (1 << 22)
188 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_2  (1 << 23)
189 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_3  (1 << 24)
190 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_3  (1 << 25)
191 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_4  (1 << 26)
192 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_4  (1 << 27)
193 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_5  (1 << 28)
194 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_5  (1 << 29)
195 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL0  (1 << 30)
196 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL1  (1 << 31)
197 #define DSI_CIO_IRQ_ERROR_MASK \
198         (DSI_CIO_IRQ_ERRSYNCESC1 | DSI_CIO_IRQ_ERRSYNCESC2 | \
199          DSI_CIO_IRQ_ERRSYNCESC3 | DSI_CIO_IRQ_ERRSYNCESC4 | \
200          DSI_CIO_IRQ_ERRSYNCESC5 | \
201          DSI_CIO_IRQ_ERRESC1 | DSI_CIO_IRQ_ERRESC2 | \
202          DSI_CIO_IRQ_ERRESC3 | DSI_CIO_IRQ_ERRESC4 | \
203          DSI_CIO_IRQ_ERRESC5 | \
204          DSI_CIO_IRQ_ERRCONTROL1 | DSI_CIO_IRQ_ERRCONTROL2 | \
205          DSI_CIO_IRQ_ERRCONTROL3 | DSI_CIO_IRQ_ERRCONTROL4 | \
206          DSI_CIO_IRQ_ERRCONTROL5 | \
207          DSI_CIO_IRQ_ERRCONTENTIONLP0_1 | DSI_CIO_IRQ_ERRCONTENTIONLP1_1 | \
208          DSI_CIO_IRQ_ERRCONTENTIONLP0_2 | DSI_CIO_IRQ_ERRCONTENTIONLP1_2 | \
209          DSI_CIO_IRQ_ERRCONTENTIONLP0_3 | DSI_CIO_IRQ_ERRCONTENTIONLP1_3 | \
210          DSI_CIO_IRQ_ERRCONTENTIONLP0_4 | DSI_CIO_IRQ_ERRCONTENTIONLP1_4 | \
211          DSI_CIO_IRQ_ERRCONTENTIONLP0_5 | DSI_CIO_IRQ_ERRCONTENTIONLP1_5)
212
213 typedef void (*omap_dsi_isr_t) (void *arg, u32 mask);
214
215 static int dsi_display_init_dispc(struct platform_device *dsidev,
216         struct omap_overlay_manager *mgr);
217 static void dsi_display_uninit_dispc(struct platform_device *dsidev,
218         struct omap_overlay_manager *mgr);
219
220 static int dsi_vc_send_null(struct omap_dss_device *dssdev, int channel);
221
222 #define DSI_MAX_NR_ISRS                2
223 #define DSI_MAX_NR_LANES        5
224
225 enum dsi_lane_function {
226         DSI_LANE_UNUSED = 0,
227         DSI_LANE_CLK,
228         DSI_LANE_DATA1,
229         DSI_LANE_DATA2,
230         DSI_LANE_DATA3,
231         DSI_LANE_DATA4,
232 };
233
234 struct dsi_lane_config {
235         enum dsi_lane_function function;
236         u8 polarity;
237 };
238
239 struct dsi_isr_data {
240         omap_dsi_isr_t  isr;
241         void            *arg;
242         u32             mask;
243 };
244
245 enum fifo_size {
246         DSI_FIFO_SIZE_0         = 0,
247         DSI_FIFO_SIZE_32        = 1,
248         DSI_FIFO_SIZE_64        = 2,
249         DSI_FIFO_SIZE_96        = 3,
250         DSI_FIFO_SIZE_128       = 4,
251 };
252
253 enum dsi_vc_source {
254         DSI_VC_SOURCE_L4 = 0,
255         DSI_VC_SOURCE_VP,
256 };
257
258 struct dsi_irq_stats {
259         unsigned long last_reset;
260         unsigned irq_count;
261         unsigned dsi_irqs[32];
262         unsigned vc_irqs[4][32];
263         unsigned cio_irqs[32];
264 };
265
266 struct dsi_isr_tables {
267         struct dsi_isr_data isr_table[DSI_MAX_NR_ISRS];
268         struct dsi_isr_data isr_table_vc[4][DSI_MAX_NR_ISRS];
269         struct dsi_isr_data isr_table_cio[DSI_MAX_NR_ISRS];
270 };
271
272 struct dsi_clk_calc_ctx {
273         struct platform_device *dsidev;
274
275         /* inputs */
276
277         const struct omap_dss_dsi_config *config;
278
279         unsigned long req_pck_min, req_pck_nom, req_pck_max;
280
281         /* outputs */
282
283         struct dsi_clock_info dsi_cinfo;
284         struct dispc_clock_info dispc_cinfo;
285
286         struct omap_video_timings dispc_vm;
287         struct omap_dss_dsi_videomode_timings dsi_vm;
288 };
289
290 struct dsi_data {
291         struct platform_device *pdev;
292         void __iomem *proto_base;
293         void __iomem *phy_base;
294         void __iomem *pll_base;
295
296         int module_id;
297
298         int irq;
299
300         bool is_enabled;
301
302         struct clk *dss_clk;
303         struct clk *sys_clk;
304
305         struct dispc_clock_info user_dispc_cinfo;
306         struct dsi_clock_info user_dsi_cinfo;
307
308         struct dsi_clock_info current_cinfo;
309
310         bool vdds_dsi_enabled;
311         struct regulator *vdds_dsi_reg;
312
313         struct {
314                 enum dsi_vc_source source;
315                 struct omap_dss_device *dssdev;
316                 enum fifo_size tx_fifo_size;
317                 enum fifo_size rx_fifo_size;
318                 int vc_id;
319         } vc[4];
320
321         struct mutex lock;
322         struct semaphore bus_lock;
323
324         unsigned pll_locked;
325
326         spinlock_t irq_lock;
327         struct dsi_isr_tables isr_tables;
328         /* space for a copy used by the interrupt handler */
329         struct dsi_isr_tables isr_tables_copy;
330
331         int update_channel;
332 #ifdef DSI_PERF_MEASURE
333         unsigned update_bytes;
334 #endif
335
336         bool te_enabled;
337         bool ulps_enabled;
338
339         void (*framedone_callback)(int, void *);
340         void *framedone_data;
341
342         struct delayed_work framedone_timeout_work;
343
344 #ifdef DSI_CATCH_MISSING_TE
345         struct timer_list te_timer;
346 #endif
347
348         unsigned long cache_req_pck;
349         unsigned long cache_clk_freq;
350         struct dsi_clock_info cache_cinfo;
351
352         u32             errors;
353         spinlock_t      errors_lock;
354 #ifdef DSI_PERF_MEASURE
355         ktime_t perf_setup_time;
356         ktime_t perf_start_time;
357 #endif
358         int debug_read;
359         int debug_write;
360
361 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
362         spinlock_t irq_stats_lock;
363         struct dsi_irq_stats irq_stats;
364 #endif
365         /* DSI PLL Parameter Ranges */
366         unsigned long regm_max, regn_max;
367         unsigned long  regm_dispc_max, regm_dsi_max;
368         unsigned long  fint_min, fint_max;
369         unsigned long lpdiv_max;
370
371         unsigned num_lanes_supported;
372         unsigned line_buffer_size;
373
374         struct dsi_lane_config lanes[DSI_MAX_NR_LANES];
375         unsigned num_lanes_used;
376
377         unsigned scp_clk_refcount;
378
379         struct dss_lcd_mgr_config mgr_config;
380         struct omap_video_timings timings;
381         enum omap_dss_dsi_pixel_format pix_fmt;
382         enum omap_dss_dsi_mode mode;
383         struct omap_dss_dsi_videomode_timings vm_timings;
384
385         struct omap_dss_device output;
386 };
387
388 struct dsi_packet_sent_handler_data {
389         struct platform_device *dsidev;
390         struct completion *completion;
391 };
392
393 struct dsi_module_id_data {
394         u32 address;
395         int id;
396 };
397
398 static const struct of_device_id dsi_of_match[];
399
400 #ifdef DSI_PERF_MEASURE
401 static bool dsi_perf;
402 module_param(dsi_perf, bool, 0644);
403 #endif
404
405 static inline struct dsi_data *dsi_get_dsidrv_data(struct platform_device *dsidev)
406 {
407         return dev_get_drvdata(&dsidev->dev);
408 }
409
410 static inline struct platform_device *dsi_get_dsidev_from_dssdev(struct omap_dss_device *dssdev)
411 {
412         return to_platform_device(dssdev->dev);
413 }
414
415 struct platform_device *dsi_get_dsidev_from_id(int module)
416 {
417         struct omap_dss_device *out;
418         enum omap_dss_output_id id;
419
420         switch (module) {
421         case 0:
422                 id = OMAP_DSS_OUTPUT_DSI1;
423                 break;
424         case 1:
425                 id = OMAP_DSS_OUTPUT_DSI2;
426                 break;
427         default:
428                 return NULL;
429         }
430
431         out = omap_dss_get_output(id);
432
433         return out ? to_platform_device(out->dev) : NULL;
434 }
435
436 static inline void dsi_write_reg(struct platform_device *dsidev,
437                 const struct dsi_reg idx, u32 val)
438 {
439         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
440         void __iomem *base;
441
442         switch(idx.module) {
443                 case DSI_PROTO: base = dsi->proto_base; break;
444                 case DSI_PHY: base = dsi->phy_base; break;
445                 case DSI_PLL: base = dsi->pll_base; break;
446                 default: return;
447         }
448
449         __raw_writel(val, base + idx.idx);
450 }
451
452 static inline u32 dsi_read_reg(struct platform_device *dsidev,
453                 const struct dsi_reg idx)
454 {
455         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
456         void __iomem *base;
457
458         switch(idx.module) {
459                 case DSI_PROTO: base = dsi->proto_base; break;
460                 case DSI_PHY: base = dsi->phy_base; break;
461                 case DSI_PLL: base = dsi->pll_base; break;
462                 default: return 0;
463         }
464
465         return __raw_readl(base + idx.idx);
466 }
467
468 static void dsi_bus_lock(struct omap_dss_device *dssdev)
469 {
470         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
471         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
472
473         down(&dsi->bus_lock);
474 }
475
476 static void dsi_bus_unlock(struct omap_dss_device *dssdev)
477 {
478         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
479         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
480
481         up(&dsi->bus_lock);
482 }
483
484 static bool dsi_bus_is_locked(struct platform_device *dsidev)
485 {
486         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
487
488         return dsi->bus_lock.count == 0;
489 }
490
491 static void dsi_completion_handler(void *data, u32 mask)
492 {
493         complete((struct completion *)data);
494 }
495
496 static inline int wait_for_bit_change(struct platform_device *dsidev,
497                 const struct dsi_reg idx, int bitnum, int value)
498 {
499         unsigned long timeout;
500         ktime_t wait;
501         int t;
502
503         /* first busyloop to see if the bit changes right away */
504         t = 100;
505         while (t-- > 0) {
506                 if (REG_GET(dsidev, idx, bitnum, bitnum) == value)
507                         return value;
508         }
509
510         /* then loop for 500ms, sleeping for 1ms in between */
511         timeout = jiffies + msecs_to_jiffies(500);
512         while (time_before(jiffies, timeout)) {
513                 if (REG_GET(dsidev, idx, bitnum, bitnum) == value)
514                         return value;
515
516                 wait = ns_to_ktime(1000 * 1000);
517                 set_current_state(TASK_UNINTERRUPTIBLE);
518                 schedule_hrtimeout(&wait, HRTIMER_MODE_REL);
519         }
520
521         return !value;
522 }
523
524 u8 dsi_get_pixel_size(enum omap_dss_dsi_pixel_format fmt)
525 {
526         switch (fmt) {
527         case OMAP_DSS_DSI_FMT_RGB888:
528         case OMAP_DSS_DSI_FMT_RGB666:
529                 return 24;
530         case OMAP_DSS_DSI_FMT_RGB666_PACKED:
531                 return 18;
532         case OMAP_DSS_DSI_FMT_RGB565:
533                 return 16;
534         default:
535                 BUG();
536                 return 0;
537         }
538 }
539
540 #ifdef DSI_PERF_MEASURE
541 static void dsi_perf_mark_setup(struct platform_device *dsidev)
542 {
543         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
544         dsi->perf_setup_time = ktime_get();
545 }
546
547 static void dsi_perf_mark_start(struct platform_device *dsidev)
548 {
549         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
550         dsi->perf_start_time = ktime_get();
551 }
552
553 static void dsi_perf_show(struct platform_device *dsidev, const char *name)
554 {
555         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
556         ktime_t t, setup_time, trans_time;
557         u32 total_bytes;
558         u32 setup_us, trans_us, total_us;
559
560         if (!dsi_perf)
561                 return;
562
563         t = ktime_get();
564
565         setup_time = ktime_sub(dsi->perf_start_time, dsi->perf_setup_time);
566         setup_us = (u32)ktime_to_us(setup_time);
567         if (setup_us == 0)
568                 setup_us = 1;
569
570         trans_time = ktime_sub(t, dsi->perf_start_time);
571         trans_us = (u32)ktime_to_us(trans_time);
572         if (trans_us == 0)
573                 trans_us = 1;
574
575         total_us = setup_us + trans_us;
576
577         total_bytes = dsi->update_bytes;
578
579         printk(KERN_INFO "DSI(%s): %u us + %u us = %u us (%uHz), "
580                         "%u bytes, %u kbytes/sec\n",
581                         name,
582                         setup_us,
583                         trans_us,
584                         total_us,
585                         1000*1000 / total_us,
586                         total_bytes,
587                         total_bytes * 1000 / total_us);
588 }
589 #else
590 static inline void dsi_perf_mark_setup(struct platform_device *dsidev)
591 {
592 }
593
594 static inline void dsi_perf_mark_start(struct platform_device *dsidev)
595 {
596 }
597
598 static inline void dsi_perf_show(struct platform_device *dsidev,
599                 const char *name)
600 {
601 }
602 #endif
603
604 static int verbose_irq;
605
606 static void print_irq_status(u32 status)
607 {
608         if (status == 0)
609                 return;
610
611         if (!verbose_irq && (status & ~DSI_IRQ_CHANNEL_MASK) == 0)
612                 return;
613
614 #define PIS(x) (status & DSI_IRQ_##x) ? (#x " ") : ""
615
616         pr_debug("DSI IRQ: 0x%x: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
617                 status,
618                 verbose_irq ? PIS(VC0) : "",
619                 verbose_irq ? PIS(VC1) : "",
620                 verbose_irq ? PIS(VC2) : "",
621                 verbose_irq ? PIS(VC3) : "",
622                 PIS(WAKEUP),
623                 PIS(RESYNC),
624                 PIS(PLL_LOCK),
625                 PIS(PLL_UNLOCK),
626                 PIS(PLL_RECALL),
627                 PIS(COMPLEXIO_ERR),
628                 PIS(HS_TX_TIMEOUT),
629                 PIS(LP_RX_TIMEOUT),
630                 PIS(TE_TRIGGER),
631                 PIS(ACK_TRIGGER),
632                 PIS(SYNC_LOST),
633                 PIS(LDO_POWER_GOOD),
634                 PIS(TA_TIMEOUT));
635 #undef PIS
636 }
637
638 static void print_irq_status_vc(int channel, u32 status)
639 {
640         if (status == 0)
641                 return;
642
643         if (!verbose_irq && (status & ~DSI_VC_IRQ_PACKET_SENT) == 0)
644                 return;
645
646 #define PIS(x) (status & DSI_VC_IRQ_##x) ? (#x " ") : ""
647
648         pr_debug("DSI VC(%d) IRQ 0x%x: %s%s%s%s%s%s%s%s%s\n",
649                 channel,
650                 status,
651                 PIS(CS),
652                 PIS(ECC_CORR),
653                 PIS(ECC_NO_CORR),
654                 verbose_irq ? PIS(PACKET_SENT) : "",
655                 PIS(BTA),
656                 PIS(FIFO_TX_OVF),
657                 PIS(FIFO_RX_OVF),
658                 PIS(FIFO_TX_UDF),
659                 PIS(PP_BUSY_CHANGE));
660 #undef PIS
661 }
662
663 static void print_irq_status_cio(u32 status)
664 {
665         if (status == 0)
666                 return;
667
668 #define PIS(x) (status & DSI_CIO_IRQ_##x) ? (#x " ") : ""
669
670         pr_debug("DSI CIO IRQ 0x%x: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
671                 status,
672                 PIS(ERRSYNCESC1),
673                 PIS(ERRSYNCESC2),
674                 PIS(ERRSYNCESC3),
675                 PIS(ERRESC1),
676                 PIS(ERRESC2),
677                 PIS(ERRESC3),
678                 PIS(ERRCONTROL1),
679                 PIS(ERRCONTROL2),
680                 PIS(ERRCONTROL3),
681                 PIS(STATEULPS1),
682                 PIS(STATEULPS2),
683                 PIS(STATEULPS3),
684                 PIS(ERRCONTENTIONLP0_1),
685                 PIS(ERRCONTENTIONLP1_1),
686                 PIS(ERRCONTENTIONLP0_2),
687                 PIS(ERRCONTENTIONLP1_2),
688                 PIS(ERRCONTENTIONLP0_3),
689                 PIS(ERRCONTENTIONLP1_3),
690                 PIS(ULPSACTIVENOT_ALL0),
691                 PIS(ULPSACTIVENOT_ALL1));
692 #undef PIS
693 }
694
695 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
696 static void dsi_collect_irq_stats(struct platform_device *dsidev, u32 irqstatus,
697                 u32 *vcstatus, u32 ciostatus)
698 {
699         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
700         int i;
701
702         spin_lock(&dsi->irq_stats_lock);
703
704         dsi->irq_stats.irq_count++;
705         dss_collect_irq_stats(irqstatus, dsi->irq_stats.dsi_irqs);
706
707         for (i = 0; i < 4; ++i)
708                 dss_collect_irq_stats(vcstatus[i], dsi->irq_stats.vc_irqs[i]);
709
710         dss_collect_irq_stats(ciostatus, dsi->irq_stats.cio_irqs);
711
712         spin_unlock(&dsi->irq_stats_lock);
713 }
714 #else
715 #define dsi_collect_irq_stats(dsidev, irqstatus, vcstatus, ciostatus)
716 #endif
717
718 static int debug_irq;
719
720 static void dsi_handle_irq_errors(struct platform_device *dsidev, u32 irqstatus,
721                 u32 *vcstatus, u32 ciostatus)
722 {
723         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
724         int i;
725
726         if (irqstatus & DSI_IRQ_ERROR_MASK) {
727                 DSSERR("DSI error, irqstatus %x\n", irqstatus);
728                 print_irq_status(irqstatus);
729                 spin_lock(&dsi->errors_lock);
730                 dsi->errors |= irqstatus & DSI_IRQ_ERROR_MASK;
731                 spin_unlock(&dsi->errors_lock);
732         } else if (debug_irq) {
733                 print_irq_status(irqstatus);
734         }
735
736         for (i = 0; i < 4; ++i) {
737                 if (vcstatus[i] & DSI_VC_IRQ_ERROR_MASK) {
738                         DSSERR("DSI VC(%d) error, vc irqstatus %x\n",
739                                        i, vcstatus[i]);
740                         print_irq_status_vc(i, vcstatus[i]);
741                 } else if (debug_irq) {
742                         print_irq_status_vc(i, vcstatus[i]);
743                 }
744         }
745
746         if (ciostatus & DSI_CIO_IRQ_ERROR_MASK) {
747                 DSSERR("DSI CIO error, cio irqstatus %x\n", ciostatus);
748                 print_irq_status_cio(ciostatus);
749         } else if (debug_irq) {
750                 print_irq_status_cio(ciostatus);
751         }
752 }
753
754 static void dsi_call_isrs(struct dsi_isr_data *isr_array,
755                 unsigned isr_array_size, u32 irqstatus)
756 {
757         struct dsi_isr_data *isr_data;
758         int i;
759
760         for (i = 0; i < isr_array_size; i++) {
761                 isr_data = &isr_array[i];
762                 if (isr_data->isr && isr_data->mask & irqstatus)
763                         isr_data->isr(isr_data->arg, irqstatus);
764         }
765 }
766
767 static void dsi_handle_isrs(struct dsi_isr_tables *isr_tables,
768                 u32 irqstatus, u32 *vcstatus, u32 ciostatus)
769 {
770         int i;
771
772         dsi_call_isrs(isr_tables->isr_table,
773                         ARRAY_SIZE(isr_tables->isr_table),
774                         irqstatus);
775
776         for (i = 0; i < 4; ++i) {
777                 if (vcstatus[i] == 0)
778                         continue;
779                 dsi_call_isrs(isr_tables->isr_table_vc[i],
780                                 ARRAY_SIZE(isr_tables->isr_table_vc[i]),
781                                 vcstatus[i]);
782         }
783
784         if (ciostatus != 0)
785                 dsi_call_isrs(isr_tables->isr_table_cio,
786                                 ARRAY_SIZE(isr_tables->isr_table_cio),
787                                 ciostatus);
788 }
789
790 static irqreturn_t omap_dsi_irq_handler(int irq, void *arg)
791 {
792         struct platform_device *dsidev;
793         struct dsi_data *dsi;
794         u32 irqstatus, vcstatus[4], ciostatus;
795         int i;
796
797         dsidev = (struct platform_device *) arg;
798         dsi = dsi_get_dsidrv_data(dsidev);
799
800         if (!dsi->is_enabled)
801                 return IRQ_NONE;
802
803         spin_lock(&dsi->irq_lock);
804
805         irqstatus = dsi_read_reg(dsidev, DSI_IRQSTATUS);
806
807         /* IRQ is not for us */
808         if (!irqstatus) {
809                 spin_unlock(&dsi->irq_lock);
810                 return IRQ_NONE;
811         }
812
813         dsi_write_reg(dsidev, DSI_IRQSTATUS, irqstatus & ~DSI_IRQ_CHANNEL_MASK);
814         /* flush posted write */
815         dsi_read_reg(dsidev, DSI_IRQSTATUS);
816
817         for (i = 0; i < 4; ++i) {
818                 if ((irqstatus & (1 << i)) == 0) {
819                         vcstatus[i] = 0;
820                         continue;
821                 }
822
823                 vcstatus[i] = dsi_read_reg(dsidev, DSI_VC_IRQSTATUS(i));
824
825                 dsi_write_reg(dsidev, DSI_VC_IRQSTATUS(i), vcstatus[i]);
826                 /* flush posted write */
827                 dsi_read_reg(dsidev, DSI_VC_IRQSTATUS(i));
828         }
829
830         if (irqstatus & DSI_IRQ_COMPLEXIO_ERR) {
831                 ciostatus = dsi_read_reg(dsidev, DSI_COMPLEXIO_IRQ_STATUS);
832
833                 dsi_write_reg(dsidev, DSI_COMPLEXIO_IRQ_STATUS, ciostatus);
834                 /* flush posted write */
835                 dsi_read_reg(dsidev, DSI_COMPLEXIO_IRQ_STATUS);
836         } else {
837                 ciostatus = 0;
838         }
839
840 #ifdef DSI_CATCH_MISSING_TE
841         if (irqstatus & DSI_IRQ_TE_TRIGGER)
842                 del_timer(&dsi->te_timer);
843 #endif
844
845         /* make a copy and unlock, so that isrs can unregister
846          * themselves */
847         memcpy(&dsi->isr_tables_copy, &dsi->isr_tables,
848                 sizeof(dsi->isr_tables));
849
850         spin_unlock(&dsi->irq_lock);
851
852         dsi_handle_isrs(&dsi->isr_tables_copy, irqstatus, vcstatus, ciostatus);
853
854         dsi_handle_irq_errors(dsidev, irqstatus, vcstatus, ciostatus);
855
856         dsi_collect_irq_stats(dsidev, irqstatus, vcstatus, ciostatus);
857
858         return IRQ_HANDLED;
859 }
860
861 /* dsi->irq_lock has to be locked by the caller */
862 static void _omap_dsi_configure_irqs(struct platform_device *dsidev,
863                 struct dsi_isr_data *isr_array,
864                 unsigned isr_array_size, u32 default_mask,
865                 const struct dsi_reg enable_reg,
866                 const struct dsi_reg status_reg)
867 {
868         struct dsi_isr_data *isr_data;
869         u32 mask;
870         u32 old_mask;
871         int i;
872
873         mask = default_mask;
874
875         for (i = 0; i < isr_array_size; i++) {
876                 isr_data = &isr_array[i];
877
878                 if (isr_data->isr == NULL)
879                         continue;
880
881                 mask |= isr_data->mask;
882         }
883
884         old_mask = dsi_read_reg(dsidev, enable_reg);
885         /* clear the irqstatus for newly enabled irqs */
886         dsi_write_reg(dsidev, status_reg, (mask ^ old_mask) & mask);
887         dsi_write_reg(dsidev, enable_reg, mask);
888
889         /* flush posted writes */
890         dsi_read_reg(dsidev, enable_reg);
891         dsi_read_reg(dsidev, status_reg);
892 }
893
894 /* dsi->irq_lock has to be locked by the caller */
895 static void _omap_dsi_set_irqs(struct platform_device *dsidev)
896 {
897         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
898         u32 mask = DSI_IRQ_ERROR_MASK;
899 #ifdef DSI_CATCH_MISSING_TE
900         mask |= DSI_IRQ_TE_TRIGGER;
901 #endif
902         _omap_dsi_configure_irqs(dsidev, dsi->isr_tables.isr_table,
903                         ARRAY_SIZE(dsi->isr_tables.isr_table), mask,
904                         DSI_IRQENABLE, DSI_IRQSTATUS);
905 }
906
907 /* dsi->irq_lock has to be locked by the caller */
908 static void _omap_dsi_set_irqs_vc(struct platform_device *dsidev, int vc)
909 {
910         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
911
912         _omap_dsi_configure_irqs(dsidev, dsi->isr_tables.isr_table_vc[vc],
913                         ARRAY_SIZE(dsi->isr_tables.isr_table_vc[vc]),
914                         DSI_VC_IRQ_ERROR_MASK,
915                         DSI_VC_IRQENABLE(vc), DSI_VC_IRQSTATUS(vc));
916 }
917
918 /* dsi->irq_lock has to be locked by the caller */
919 static void _omap_dsi_set_irqs_cio(struct platform_device *dsidev)
920 {
921         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
922
923         _omap_dsi_configure_irqs(dsidev, dsi->isr_tables.isr_table_cio,
924                         ARRAY_SIZE(dsi->isr_tables.isr_table_cio),
925                         DSI_CIO_IRQ_ERROR_MASK,
926                         DSI_COMPLEXIO_IRQ_ENABLE, DSI_COMPLEXIO_IRQ_STATUS);
927 }
928
929 static void _dsi_initialize_irq(struct platform_device *dsidev)
930 {
931         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
932         unsigned long flags;
933         int vc;
934
935         spin_lock_irqsave(&dsi->irq_lock, flags);
936
937         memset(&dsi->isr_tables, 0, sizeof(dsi->isr_tables));
938
939         _omap_dsi_set_irqs(dsidev);
940         for (vc = 0; vc < 4; ++vc)
941                 _omap_dsi_set_irqs_vc(dsidev, vc);
942         _omap_dsi_set_irqs_cio(dsidev);
943
944         spin_unlock_irqrestore(&dsi->irq_lock, flags);
945 }
946
947 static int _dsi_register_isr(omap_dsi_isr_t isr, void *arg, u32 mask,
948                 struct dsi_isr_data *isr_array, unsigned isr_array_size)
949 {
950         struct dsi_isr_data *isr_data;
951         int free_idx;
952         int i;
953
954         BUG_ON(isr == NULL);
955
956         /* check for duplicate entry and find a free slot */
957         free_idx = -1;
958         for (i = 0; i < isr_array_size; i++) {
959                 isr_data = &isr_array[i];
960
961                 if (isr_data->isr == isr && isr_data->arg == arg &&
962                                 isr_data->mask == mask) {
963                         return -EINVAL;
964                 }
965
966                 if (isr_data->isr == NULL && free_idx == -1)
967                         free_idx = i;
968         }
969
970         if (free_idx == -1)
971                 return -EBUSY;
972
973         isr_data = &isr_array[free_idx];
974         isr_data->isr = isr;
975         isr_data->arg = arg;
976         isr_data->mask = mask;
977
978         return 0;
979 }
980
981 static int _dsi_unregister_isr(omap_dsi_isr_t isr, void *arg, u32 mask,
982                 struct dsi_isr_data *isr_array, unsigned isr_array_size)
983 {
984         struct dsi_isr_data *isr_data;
985         int i;
986
987         for (i = 0; i < isr_array_size; i++) {
988                 isr_data = &isr_array[i];
989                 if (isr_data->isr != isr || isr_data->arg != arg ||
990                                 isr_data->mask != mask)
991                         continue;
992
993                 isr_data->isr = NULL;
994                 isr_data->arg = NULL;
995                 isr_data->mask = 0;
996
997                 return 0;
998         }
999
1000         return -EINVAL;
1001 }
1002
1003 static int dsi_register_isr(struct platform_device *dsidev, omap_dsi_isr_t isr,
1004                 void *arg, u32 mask)
1005 {
1006         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1007         unsigned long flags;
1008         int r;
1009
1010         spin_lock_irqsave(&dsi->irq_lock, flags);
1011
1012         r = _dsi_register_isr(isr, arg, mask, dsi->isr_tables.isr_table,
1013                         ARRAY_SIZE(dsi->isr_tables.isr_table));
1014
1015         if (r == 0)
1016                 _omap_dsi_set_irqs(dsidev);
1017
1018         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1019
1020         return r;
1021 }
1022
1023 static int dsi_unregister_isr(struct platform_device *dsidev,
1024                 omap_dsi_isr_t isr, void *arg, u32 mask)
1025 {
1026         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1027         unsigned long flags;
1028         int r;
1029
1030         spin_lock_irqsave(&dsi->irq_lock, flags);
1031
1032         r = _dsi_unregister_isr(isr, arg, mask, dsi->isr_tables.isr_table,
1033                         ARRAY_SIZE(dsi->isr_tables.isr_table));
1034
1035         if (r == 0)
1036                 _omap_dsi_set_irqs(dsidev);
1037
1038         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1039
1040         return r;
1041 }
1042
1043 static int dsi_register_isr_vc(struct platform_device *dsidev, int channel,
1044                 omap_dsi_isr_t isr, void *arg, u32 mask)
1045 {
1046         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1047         unsigned long flags;
1048         int r;
1049
1050         spin_lock_irqsave(&dsi->irq_lock, flags);
1051
1052         r = _dsi_register_isr(isr, arg, mask,
1053                         dsi->isr_tables.isr_table_vc[channel],
1054                         ARRAY_SIZE(dsi->isr_tables.isr_table_vc[channel]));
1055
1056         if (r == 0)
1057                 _omap_dsi_set_irqs_vc(dsidev, channel);
1058
1059         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1060
1061         return r;
1062 }
1063
1064 static int dsi_unregister_isr_vc(struct platform_device *dsidev, int channel,
1065                 omap_dsi_isr_t isr, void *arg, u32 mask)
1066 {
1067         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1068         unsigned long flags;
1069         int r;
1070
1071         spin_lock_irqsave(&dsi->irq_lock, flags);
1072
1073         r = _dsi_unregister_isr(isr, arg, mask,
1074                         dsi->isr_tables.isr_table_vc[channel],
1075                         ARRAY_SIZE(dsi->isr_tables.isr_table_vc[channel]));
1076
1077         if (r == 0)
1078                 _omap_dsi_set_irqs_vc(dsidev, channel);
1079
1080         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1081
1082         return r;
1083 }
1084
1085 static int dsi_register_isr_cio(struct platform_device *dsidev,
1086                 omap_dsi_isr_t isr, void *arg, u32 mask)
1087 {
1088         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1089         unsigned long flags;
1090         int r;
1091
1092         spin_lock_irqsave(&dsi->irq_lock, flags);
1093
1094         r = _dsi_register_isr(isr, arg, mask, dsi->isr_tables.isr_table_cio,
1095                         ARRAY_SIZE(dsi->isr_tables.isr_table_cio));
1096
1097         if (r == 0)
1098                 _omap_dsi_set_irqs_cio(dsidev);
1099
1100         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1101
1102         return r;
1103 }
1104
1105 static int dsi_unregister_isr_cio(struct platform_device *dsidev,
1106                 omap_dsi_isr_t isr, void *arg, u32 mask)
1107 {
1108         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1109         unsigned long flags;
1110         int r;
1111
1112         spin_lock_irqsave(&dsi->irq_lock, flags);
1113
1114         r = _dsi_unregister_isr(isr, arg, mask, dsi->isr_tables.isr_table_cio,
1115                         ARRAY_SIZE(dsi->isr_tables.isr_table_cio));
1116
1117         if (r == 0)
1118                 _omap_dsi_set_irqs_cio(dsidev);
1119
1120         spin_unlock_irqrestore(&dsi->irq_lock, flags);
1121
1122         return r;
1123 }
1124
1125 static u32 dsi_get_errors(struct platform_device *dsidev)
1126 {
1127         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1128         unsigned long flags;
1129         u32 e;
1130         spin_lock_irqsave(&dsi->errors_lock, flags);
1131         e = dsi->errors;
1132         dsi->errors = 0;
1133         spin_unlock_irqrestore(&dsi->errors_lock, flags);
1134         return e;
1135 }
1136
1137 int dsi_runtime_get(struct platform_device *dsidev)
1138 {
1139         int r;
1140         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1141
1142         DSSDBG("dsi_runtime_get\n");
1143
1144         r = pm_runtime_get_sync(&dsi->pdev->dev);
1145         WARN_ON(r < 0);
1146         return r < 0 ? r : 0;
1147 }
1148
1149 void dsi_runtime_put(struct platform_device *dsidev)
1150 {
1151         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1152         int r;
1153
1154         DSSDBG("dsi_runtime_put\n");
1155
1156         r = pm_runtime_put_sync(&dsi->pdev->dev);
1157         WARN_ON(r < 0 && r != -ENOSYS);
1158 }
1159
1160 static int dsi_regulator_init(struct platform_device *dsidev)
1161 {
1162         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1163         struct regulator *vdds_dsi;
1164         int r;
1165
1166         if (dsi->vdds_dsi_reg != NULL)
1167                 return 0;
1168
1169         vdds_dsi = devm_regulator_get(&dsi->pdev->dev, "vdd");
1170
1171         if (IS_ERR(vdds_dsi)) {
1172                 if (PTR_ERR(vdds_dsi) != -EPROBE_DEFER)
1173                         DSSERR("can't get DSI VDD regulator\n");
1174                 return PTR_ERR(vdds_dsi);
1175         }
1176
1177         if (regulator_can_change_voltage(vdds_dsi)) {
1178                 r = regulator_set_voltage(vdds_dsi, 1800000, 1800000);
1179                 if (r) {
1180                         devm_regulator_put(vdds_dsi);
1181                         DSSERR("can't set the DSI regulator voltage\n");
1182                         return r;
1183                 }
1184         }
1185
1186         dsi->vdds_dsi_reg = vdds_dsi;
1187
1188         return 0;
1189 }
1190
1191 /* source clock for DSI PLL. this could also be PCLKFREE */
1192 static inline void dsi_enable_pll_clock(struct platform_device *dsidev,
1193                 bool enable)
1194 {
1195         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1196
1197         if (enable)
1198                 clk_prepare_enable(dsi->sys_clk);
1199         else
1200                 clk_disable_unprepare(dsi->sys_clk);
1201
1202         if (enable && dsi->pll_locked) {
1203                 if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 1, 1) != 1)
1204                         DSSERR("cannot lock PLL when enabling clocks\n");
1205         }
1206 }
1207
1208 static void _dsi_print_reset_status(struct platform_device *dsidev)
1209 {
1210         u32 l;
1211         int b0, b1, b2;
1212
1213         /* A dummy read using the SCP interface to any DSIPHY register is
1214          * required after DSIPHY reset to complete the reset of the DSI complex
1215          * I/O. */
1216         l = dsi_read_reg(dsidev, DSI_DSIPHY_CFG5);
1217
1218         if (dss_has_feature(FEAT_DSI_REVERSE_TXCLKESC)) {
1219                 b0 = 28;
1220                 b1 = 27;
1221                 b2 = 26;
1222         } else {
1223                 b0 = 24;
1224                 b1 = 25;
1225                 b2 = 26;
1226         }
1227
1228 #define DSI_FLD_GET(fld, start, end)\
1229         FLD_GET(dsi_read_reg(dsidev, DSI_##fld), start, end)
1230
1231         pr_debug("DSI resets: PLL (%d) CIO (%d) PHY (%x%x%x, %d, %d, %d)\n",
1232                 DSI_FLD_GET(PLL_STATUS, 0, 0),
1233                 DSI_FLD_GET(COMPLEXIO_CFG1, 29, 29),
1234                 DSI_FLD_GET(DSIPHY_CFG5, b0, b0),
1235                 DSI_FLD_GET(DSIPHY_CFG5, b1, b1),
1236                 DSI_FLD_GET(DSIPHY_CFG5, b2, b2),
1237                 DSI_FLD_GET(DSIPHY_CFG5, 29, 29),
1238                 DSI_FLD_GET(DSIPHY_CFG5, 30, 30),
1239                 DSI_FLD_GET(DSIPHY_CFG5, 31, 31));
1240
1241 #undef DSI_FLD_GET
1242 }
1243
1244 static inline int dsi_if_enable(struct platform_device *dsidev, bool enable)
1245 {
1246         DSSDBG("dsi_if_enable(%d)\n", enable);
1247
1248         enable = enable ? 1 : 0;
1249         REG_FLD_MOD(dsidev, DSI_CTRL, enable, 0, 0); /* IF_EN */
1250
1251         if (wait_for_bit_change(dsidev, DSI_CTRL, 0, enable) != enable) {
1252                         DSSERR("Failed to set dsi_if_enable to %d\n", enable);
1253                         return -EIO;
1254         }
1255
1256         return 0;
1257 }
1258
1259 unsigned long dsi_get_pll_hsdiv_dispc_rate(struct platform_device *dsidev)
1260 {
1261         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1262
1263         return dsi->current_cinfo.dsi_pll_hsdiv_dispc_clk;
1264 }
1265
1266 static unsigned long dsi_get_pll_hsdiv_dsi_rate(struct platform_device *dsidev)
1267 {
1268         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1269
1270         return dsi->current_cinfo.dsi_pll_hsdiv_dsi_clk;
1271 }
1272
1273 static unsigned long dsi_get_txbyteclkhs(struct platform_device *dsidev)
1274 {
1275         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1276
1277         return dsi->current_cinfo.clkin4ddr / 16;
1278 }
1279
1280 static unsigned long dsi_fclk_rate(struct platform_device *dsidev)
1281 {
1282         unsigned long r;
1283         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1284
1285         if (dss_get_dsi_clk_source(dsi->module_id) == OMAP_DSS_CLK_SRC_FCK) {
1286                 /* DSI FCLK source is DSS_CLK_FCK */
1287                 r = clk_get_rate(dsi->dss_clk);
1288         } else {
1289                 /* DSI FCLK source is dsi_pll_hsdiv_dsi_clk */
1290                 r = dsi_get_pll_hsdiv_dsi_rate(dsidev);
1291         }
1292
1293         return r;
1294 }
1295
1296 static int dsi_lp_clock_calc(struct dsi_clock_info *cinfo,
1297                 unsigned long lp_clk_min, unsigned long lp_clk_max)
1298 {
1299         unsigned long dsi_fclk = cinfo->dsi_pll_hsdiv_dsi_clk;
1300         unsigned lp_clk_div;
1301         unsigned long lp_clk;
1302
1303         lp_clk_div = DIV_ROUND_UP(dsi_fclk, lp_clk_max * 2);
1304         lp_clk = dsi_fclk / 2 / lp_clk_div;
1305
1306         if (lp_clk < lp_clk_min || lp_clk > lp_clk_max)
1307                 return -EINVAL;
1308
1309         cinfo->lp_clk_div = lp_clk_div;
1310         cinfo->lp_clk = lp_clk;
1311
1312         return 0;
1313 }
1314
1315 static int dsi_set_lp_clk_divisor(struct platform_device *dsidev)
1316 {
1317         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1318         unsigned long dsi_fclk;
1319         unsigned lp_clk_div;
1320         unsigned long lp_clk;
1321
1322         lp_clk_div = dsi->user_dsi_cinfo.lp_clk_div;
1323
1324         if (lp_clk_div == 0 || lp_clk_div > dsi->lpdiv_max)
1325                 return -EINVAL;
1326
1327         dsi_fclk = dsi_fclk_rate(dsidev);
1328
1329         lp_clk = dsi_fclk / 2 / lp_clk_div;
1330
1331         DSSDBG("LP_CLK_DIV %u, LP_CLK %lu\n", lp_clk_div, lp_clk);
1332         dsi->current_cinfo.lp_clk = lp_clk;
1333         dsi->current_cinfo.lp_clk_div = lp_clk_div;
1334
1335         /* LP_CLK_DIVISOR */
1336         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, lp_clk_div, 12, 0);
1337
1338         /* LP_RX_SYNCHRO_ENABLE */
1339         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, dsi_fclk > 30000000 ? 1 : 0, 21, 21);
1340
1341         return 0;
1342 }
1343
1344 static void dsi_enable_scp_clk(struct platform_device *dsidev)
1345 {
1346         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1347
1348         if (dsi->scp_clk_refcount++ == 0)
1349                 REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 1, 14, 14); /* CIO_CLK_ICG */
1350 }
1351
1352 static void dsi_disable_scp_clk(struct platform_device *dsidev)
1353 {
1354         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1355
1356         WARN_ON(dsi->scp_clk_refcount == 0);
1357         if (--dsi->scp_clk_refcount == 0)
1358                 REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 0, 14, 14); /* CIO_CLK_ICG */
1359 }
1360
1361 enum dsi_pll_power_state {
1362         DSI_PLL_POWER_OFF       = 0x0,
1363         DSI_PLL_POWER_ON_HSCLK  = 0x1,
1364         DSI_PLL_POWER_ON_ALL    = 0x2,
1365         DSI_PLL_POWER_ON_DIV    = 0x3,
1366 };
1367
1368 static int dsi_pll_power(struct platform_device *dsidev,
1369                 enum dsi_pll_power_state state)
1370 {
1371         int t = 0;
1372
1373         /* DSI-PLL power command 0x3 is not working */
1374         if (dss_has_feature(FEAT_DSI_PLL_PWR_BUG) &&
1375                         state == DSI_PLL_POWER_ON_DIV)
1376                 state = DSI_PLL_POWER_ON_ALL;
1377
1378         /* PLL_PWR_CMD */
1379         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, state, 31, 30);
1380
1381         /* PLL_PWR_STATUS */
1382         while (FLD_GET(dsi_read_reg(dsidev, DSI_CLK_CTRL), 29, 28) != state) {
1383                 if (++t > 1000) {
1384                         DSSERR("Failed to set DSI PLL power mode to %d\n",
1385                                         state);
1386                         return -ENODEV;
1387                 }
1388                 udelay(1);
1389         }
1390
1391         return 0;
1392 }
1393
1394 unsigned long dsi_get_pll_clkin(struct platform_device *dsidev)
1395 {
1396         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1397         return clk_get_rate(dsi->sys_clk);
1398 }
1399
1400 bool dsi_hsdiv_calc(struct platform_device *dsidev, unsigned long pll,
1401                 unsigned long out_min, dsi_hsdiv_calc_func func, void *data)
1402 {
1403         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1404         int regm, regm_start, regm_stop;
1405         unsigned long out_max;
1406         unsigned long out;
1407
1408         out_min = out_min ? out_min : 1;
1409         out_max = dss_feat_get_param_max(FEAT_PARAM_DSS_FCK);
1410
1411         regm_start = max(DIV_ROUND_UP(pll, out_max), 1ul);
1412         regm_stop = min(pll / out_min, dsi->regm_dispc_max);
1413
1414         for (regm = regm_start; regm <= regm_stop; ++regm) {
1415                 out = pll / regm;
1416
1417                 if (func(regm, out, data))
1418                         return true;
1419         }
1420
1421         return false;
1422 }
1423
1424 bool dsi_pll_calc(struct platform_device *dsidev, unsigned long clkin,
1425                 unsigned long pll_min, unsigned long pll_max,
1426                 dsi_pll_calc_func func, void *data)
1427 {
1428         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1429         int regn, regn_start, regn_stop;
1430         int regm, regm_start, regm_stop;
1431         unsigned long fint, pll;
1432         const unsigned long pll_hw_max = 1800000000;
1433         unsigned long fint_hw_min, fint_hw_max;
1434
1435         fint_hw_min = dsi->fint_min;
1436         fint_hw_max = dsi->fint_max;
1437
1438         regn_start = max(DIV_ROUND_UP(clkin, fint_hw_max), 1ul);
1439         regn_stop = min(clkin / fint_hw_min, dsi->regn_max);
1440
1441         pll_max = pll_max ? pll_max : ULONG_MAX;
1442
1443         for (regn = regn_start; regn <= regn_stop; ++regn) {
1444                 fint = clkin / regn;
1445
1446                 regm_start = max(DIV_ROUND_UP(DIV_ROUND_UP(pll_min, fint), 2),
1447                                 1ul);
1448                 regm_stop = min3(pll_max / fint / 2,
1449                                 pll_hw_max / fint / 2,
1450                                 dsi->regm_max);
1451
1452                 for (regm = regm_start; regm <= regm_stop; ++regm) {
1453                         pll = 2 * regm * fint;
1454
1455                         if (func(regn, regm, fint, pll, data))
1456                                 return true;
1457                 }
1458         }
1459
1460         return false;
1461 }
1462
1463 /* calculate clock rates using dividers in cinfo */
1464 static int dsi_calc_clock_rates(struct platform_device *dsidev,
1465                 struct dsi_clock_info *cinfo)
1466 {
1467         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1468
1469         if (cinfo->regn == 0 || cinfo->regn > dsi->regn_max)
1470                 return -EINVAL;
1471
1472         if (cinfo->regm == 0 || cinfo->regm > dsi->regm_max)
1473                 return -EINVAL;
1474
1475         if (cinfo->regm_dispc > dsi->regm_dispc_max)
1476                 return -EINVAL;
1477
1478         if (cinfo->regm_dsi > dsi->regm_dsi_max)
1479                 return -EINVAL;
1480
1481         cinfo->clkin = clk_get_rate(dsi->sys_clk);
1482         cinfo->fint = cinfo->clkin / cinfo->regn;
1483
1484         if (cinfo->fint > dsi->fint_max || cinfo->fint < dsi->fint_min)
1485                 return -EINVAL;
1486
1487         cinfo->clkin4ddr = 2 * cinfo->regm * cinfo->fint;
1488
1489         if (cinfo->clkin4ddr > 1800 * 1000 * 1000)
1490                 return -EINVAL;
1491
1492         if (cinfo->regm_dispc > 0)
1493                 cinfo->dsi_pll_hsdiv_dispc_clk =
1494                         cinfo->clkin4ddr / cinfo->regm_dispc;
1495         else
1496                 cinfo->dsi_pll_hsdiv_dispc_clk = 0;
1497
1498         if (cinfo->regm_dsi > 0)
1499                 cinfo->dsi_pll_hsdiv_dsi_clk =
1500                         cinfo->clkin4ddr / cinfo->regm_dsi;
1501         else
1502                 cinfo->dsi_pll_hsdiv_dsi_clk = 0;
1503
1504         return 0;
1505 }
1506
1507 static void dsi_pll_calc_dsi_fck(struct dsi_clock_info *cinfo)
1508 {
1509         unsigned long max_dsi_fck;
1510
1511         max_dsi_fck = dss_feat_get_param_max(FEAT_PARAM_DSI_FCK);
1512
1513         cinfo->regm_dsi = DIV_ROUND_UP(cinfo->clkin4ddr, max_dsi_fck);
1514         cinfo->dsi_pll_hsdiv_dsi_clk = cinfo->clkin4ddr / cinfo->regm_dsi;
1515 }
1516
1517 int dsi_pll_set_clock_div(struct platform_device *dsidev,
1518                 struct dsi_clock_info *cinfo)
1519 {
1520         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1521         int r = 0;
1522         u32 l;
1523         int f = 0;
1524         u8 regn_start, regn_end, regm_start, regm_end;
1525         u8 regm_dispc_start, regm_dispc_end, regm_dsi_start, regm_dsi_end;
1526
1527         DSSDBG("DSI PLL clock config starts");
1528
1529         dsi->current_cinfo.clkin = cinfo->clkin;
1530         dsi->current_cinfo.fint = cinfo->fint;
1531         dsi->current_cinfo.clkin4ddr = cinfo->clkin4ddr;
1532         dsi->current_cinfo.dsi_pll_hsdiv_dispc_clk =
1533                         cinfo->dsi_pll_hsdiv_dispc_clk;
1534         dsi->current_cinfo.dsi_pll_hsdiv_dsi_clk =
1535                         cinfo->dsi_pll_hsdiv_dsi_clk;
1536
1537         dsi->current_cinfo.regn = cinfo->regn;
1538         dsi->current_cinfo.regm = cinfo->regm;
1539         dsi->current_cinfo.regm_dispc = cinfo->regm_dispc;
1540         dsi->current_cinfo.regm_dsi = cinfo->regm_dsi;
1541
1542         DSSDBG("DSI Fint %ld\n", cinfo->fint);
1543
1544         DSSDBG("clkin rate %ld\n", cinfo->clkin);
1545
1546         /* DSIPHY == CLKIN4DDR */
1547         DSSDBG("CLKIN4DDR = 2 * %d / %d * %lu = %lu\n",
1548                         cinfo->regm,
1549                         cinfo->regn,
1550                         cinfo->clkin,
1551                         cinfo->clkin4ddr);
1552
1553         DSSDBG("Data rate on 1 DSI lane %ld Mbps\n",
1554                         cinfo->clkin4ddr / 1000 / 1000 / 2);
1555
1556         DSSDBG("Clock lane freq %ld Hz\n", cinfo->clkin4ddr / 4);
1557
1558         DSSDBG("regm_dispc = %d, %s (%s) = %lu\n", cinfo->regm_dispc,
1559                 dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC),
1560                 dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC),
1561                 cinfo->dsi_pll_hsdiv_dispc_clk);
1562         DSSDBG("regm_dsi = %d, %s (%s) = %lu\n", cinfo->regm_dsi,
1563                 dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI),
1564                 dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI),
1565                 cinfo->dsi_pll_hsdiv_dsi_clk);
1566
1567         dss_feat_get_reg_field(FEAT_REG_DSIPLL_REGN, &regn_start, &regn_end);
1568         dss_feat_get_reg_field(FEAT_REG_DSIPLL_REGM, &regm_start, &regm_end);
1569         dss_feat_get_reg_field(FEAT_REG_DSIPLL_REGM_DISPC, &regm_dispc_start,
1570                         &regm_dispc_end);
1571         dss_feat_get_reg_field(FEAT_REG_DSIPLL_REGM_DSI, &regm_dsi_start,
1572                         &regm_dsi_end);
1573
1574         /* DSI_PLL_AUTOMODE = manual */
1575         REG_FLD_MOD(dsidev, DSI_PLL_CONTROL, 0, 0, 0);
1576
1577         l = dsi_read_reg(dsidev, DSI_PLL_CONFIGURATION1);
1578         l = FLD_MOD(l, 1, 0, 0);                /* DSI_PLL_STOPMODE */
1579         /* DSI_PLL_REGN */
1580         l = FLD_MOD(l, cinfo->regn - 1, regn_start, regn_end);
1581         /* DSI_PLL_REGM */
1582         l = FLD_MOD(l, cinfo->regm, regm_start, regm_end);
1583         /* DSI_CLOCK_DIV */
1584         l = FLD_MOD(l, cinfo->regm_dispc > 0 ? cinfo->regm_dispc - 1 : 0,
1585                         regm_dispc_start, regm_dispc_end);
1586         /* DSIPROTO_CLOCK_DIV */
1587         l = FLD_MOD(l, cinfo->regm_dsi > 0 ? cinfo->regm_dsi - 1 : 0,
1588                         regm_dsi_start, regm_dsi_end);
1589         dsi_write_reg(dsidev, DSI_PLL_CONFIGURATION1, l);
1590
1591         BUG_ON(cinfo->fint < dsi->fint_min || cinfo->fint > dsi->fint_max);
1592
1593         l = dsi_read_reg(dsidev, DSI_PLL_CONFIGURATION2);
1594
1595         if (dss_has_feature(FEAT_DSI_PLL_FREQSEL)) {
1596                 f = cinfo->fint < 1000000 ? 0x3 :
1597                         cinfo->fint < 1250000 ? 0x4 :
1598                         cinfo->fint < 1500000 ? 0x5 :
1599                         cinfo->fint < 1750000 ? 0x6 :
1600                         0x7;
1601
1602                 l = FLD_MOD(l, f, 4, 1);        /* DSI_PLL_FREQSEL */
1603         } else if (dss_has_feature(FEAT_DSI_PLL_SELFREQDCO)) {
1604                 f = cinfo->clkin4ddr < 1000000000 ? 0x2 : 0x4;
1605
1606                 l = FLD_MOD(l, f, 4, 1);        /* PLL_SELFREQDCO */
1607         }
1608
1609         l = FLD_MOD(l, 1, 13, 13);              /* DSI_PLL_REFEN */
1610         l = FLD_MOD(l, 0, 14, 14);              /* DSIPHY_CLKINEN */
1611         l = FLD_MOD(l, 1, 20, 20);              /* DSI_HSDIVBYPASS */
1612         if (dss_has_feature(FEAT_DSI_PLL_REFSEL))
1613                 l = FLD_MOD(l, 3, 22, 21);      /* REF_SYSCLK = sysclk */
1614         dsi_write_reg(dsidev, DSI_PLL_CONFIGURATION2, l);
1615
1616         REG_FLD_MOD(dsidev, DSI_PLL_GO, 1, 0, 0);       /* DSI_PLL_GO */
1617
1618         if (wait_for_bit_change(dsidev, DSI_PLL_GO, 0, 0) != 0) {
1619                 DSSERR("dsi pll go bit not going down.\n");
1620                 r = -EIO;
1621                 goto err;
1622         }
1623
1624         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 1, 1) != 1) {
1625                 DSSERR("cannot lock PLL\n");
1626                 r = -EIO;
1627                 goto err;
1628         }
1629
1630         dsi->pll_locked = 1;
1631
1632         l = dsi_read_reg(dsidev, DSI_PLL_CONFIGURATION2);
1633         l = FLD_MOD(l, 0, 0, 0);        /* DSI_PLL_IDLE */
1634         l = FLD_MOD(l, 0, 5, 5);        /* DSI_PLL_PLLLPMODE */
1635         l = FLD_MOD(l, 0, 6, 6);        /* DSI_PLL_LOWCURRSTBY */
1636         l = FLD_MOD(l, 0, 7, 7);        /* DSI_PLL_TIGHTPHASELOCK */
1637         l = FLD_MOD(l, 0, 8, 8);        /* DSI_PLL_DRIFTGUARDEN */
1638         l = FLD_MOD(l, 0, 10, 9);       /* DSI_PLL_LOCKSEL */
1639         l = FLD_MOD(l, 1, 13, 13);      /* DSI_PLL_REFEN */
1640         l = FLD_MOD(l, 1, 14, 14);      /* DSIPHY_CLKINEN */
1641         l = FLD_MOD(l, 0, 15, 15);      /* DSI_BYPASSEN */
1642         l = FLD_MOD(l, 1, 16, 16);      /* DSS_CLOCK_EN */
1643         l = FLD_MOD(l, 0, 17, 17);      /* DSS_CLOCK_PWDN */
1644         l = FLD_MOD(l, 1, 18, 18);      /* DSI_PROTO_CLOCK_EN */
1645         l = FLD_MOD(l, 0, 19, 19);      /* DSI_PROTO_CLOCK_PWDN */
1646         l = FLD_MOD(l, 0, 20, 20);      /* DSI_HSDIVBYPASS */
1647         dsi_write_reg(dsidev, DSI_PLL_CONFIGURATION2, l);
1648
1649         DSSDBG("PLL config done\n");
1650 err:
1651         return r;
1652 }
1653
1654 int dsi_pll_init(struct platform_device *dsidev, bool enable_hsclk,
1655                 bool enable_hsdiv)
1656 {
1657         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1658         int r = 0;
1659         enum dsi_pll_power_state pwstate;
1660
1661         DSSDBG("PLL init\n");
1662
1663         /*
1664          * It seems that on many OMAPs we need to enable both to have a
1665          * functional HSDivider.
1666          */
1667         enable_hsclk = enable_hsdiv = true;
1668
1669         r = dsi_regulator_init(dsidev);
1670         if (r)
1671                 return r;
1672
1673         dsi_enable_pll_clock(dsidev, 1);
1674         /*
1675          * Note: SCP CLK is not required on OMAP3, but it is required on OMAP4.
1676          */
1677         dsi_enable_scp_clk(dsidev);
1678
1679         if (!dsi->vdds_dsi_enabled) {
1680                 r = regulator_enable(dsi->vdds_dsi_reg);
1681                 if (r)
1682                         goto err0;
1683                 dsi->vdds_dsi_enabled = true;
1684         }
1685
1686         /* XXX PLL does not come out of reset without this... */
1687         dispc_pck_free_enable(1);
1688
1689         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 0, 1) != 1) {
1690                 DSSERR("PLL not coming out of reset.\n");
1691                 r = -ENODEV;
1692                 dispc_pck_free_enable(0);
1693                 goto err1;
1694         }
1695
1696         /* XXX ... but if left on, we get problems when planes do not
1697          * fill the whole display. No idea about this */
1698         dispc_pck_free_enable(0);
1699
1700         if (enable_hsclk && enable_hsdiv)
1701                 pwstate = DSI_PLL_POWER_ON_ALL;
1702         else if (enable_hsclk)
1703                 pwstate = DSI_PLL_POWER_ON_HSCLK;
1704         else if (enable_hsdiv)
1705                 pwstate = DSI_PLL_POWER_ON_DIV;
1706         else
1707                 pwstate = DSI_PLL_POWER_OFF;
1708
1709         r = dsi_pll_power(dsidev, pwstate);
1710
1711         if (r)
1712                 goto err1;
1713
1714         DSSDBG("PLL init done\n");
1715
1716         return 0;
1717 err1:
1718         if (dsi->vdds_dsi_enabled) {
1719                 regulator_disable(dsi->vdds_dsi_reg);
1720                 dsi->vdds_dsi_enabled = false;
1721         }
1722 err0:
1723         dsi_disable_scp_clk(dsidev);
1724         dsi_enable_pll_clock(dsidev, 0);
1725         return r;
1726 }
1727
1728 void dsi_pll_uninit(struct platform_device *dsidev, bool disconnect_lanes)
1729 {
1730         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1731
1732         dsi->pll_locked = 0;
1733         dsi_pll_power(dsidev, DSI_PLL_POWER_OFF);
1734         if (disconnect_lanes) {
1735                 WARN_ON(!dsi->vdds_dsi_enabled);
1736                 regulator_disable(dsi->vdds_dsi_reg);
1737                 dsi->vdds_dsi_enabled = false;
1738         }
1739
1740         dsi_disable_scp_clk(dsidev);
1741         dsi_enable_pll_clock(dsidev, 0);
1742
1743         DSSDBG("PLL uninit done\n");
1744 }
1745
1746 static void dsi_dump_dsidev_clocks(struct platform_device *dsidev,
1747                 struct seq_file *s)
1748 {
1749         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1750         struct dsi_clock_info *cinfo = &dsi->current_cinfo;
1751         enum omap_dss_clk_source dispc_clk_src, dsi_clk_src;
1752         int dsi_module = dsi->module_id;
1753
1754         dispc_clk_src = dss_get_dispc_clk_source();
1755         dsi_clk_src = dss_get_dsi_clk_source(dsi_module);
1756
1757         if (dsi_runtime_get(dsidev))
1758                 return;
1759
1760         seq_printf(s,   "- DSI%d PLL -\n", dsi_module + 1);
1761
1762         seq_printf(s,   "dsi pll clkin\t%lu\n", cinfo->clkin);
1763
1764         seq_printf(s,   "Fint\t\t%-16luregn %u\n", cinfo->fint, cinfo->regn);
1765
1766         seq_printf(s,   "CLKIN4DDR\t%-16luregm %u\n",
1767                         cinfo->clkin4ddr, cinfo->regm);
1768
1769         seq_printf(s,   "DSI_PLL_HSDIV_DISPC (%s)\t%-16luregm_dispc %u\t(%s)\n",
1770                         dss_feat_get_clk_source_name(dsi_module == 0 ?
1771                                 OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC :
1772                                 OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC),
1773                         cinfo->dsi_pll_hsdiv_dispc_clk,
1774                         cinfo->regm_dispc,
1775                         dispc_clk_src == OMAP_DSS_CLK_SRC_FCK ?
1776                         "off" : "on");
1777
1778         seq_printf(s,   "DSI_PLL_HSDIV_DSI (%s)\t%-16luregm_dsi %u\t(%s)\n",
1779                         dss_feat_get_clk_source_name(dsi_module == 0 ?
1780                                 OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI :
1781                                 OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DSI),
1782                         cinfo->dsi_pll_hsdiv_dsi_clk,
1783                         cinfo->regm_dsi,
1784                         dsi_clk_src == OMAP_DSS_CLK_SRC_FCK ?
1785                         "off" : "on");
1786
1787         seq_printf(s,   "- DSI%d -\n", dsi_module + 1);
1788
1789         seq_printf(s,   "dsi fclk source = %s (%s)\n",
1790                         dss_get_generic_clk_source_name(dsi_clk_src),
1791                         dss_feat_get_clk_source_name(dsi_clk_src));
1792
1793         seq_printf(s,   "DSI_FCLK\t%lu\n", dsi_fclk_rate(dsidev));
1794
1795         seq_printf(s,   "DDR_CLK\t\t%lu\n",
1796                         cinfo->clkin4ddr / 4);
1797
1798         seq_printf(s,   "TxByteClkHS\t%lu\n", dsi_get_txbyteclkhs(dsidev));
1799
1800         seq_printf(s,   "LP_CLK\t\t%lu\n", cinfo->lp_clk);
1801
1802         dsi_runtime_put(dsidev);
1803 }
1804
1805 void dsi_dump_clocks(struct seq_file *s)
1806 {
1807         struct platform_device *dsidev;
1808         int i;
1809
1810         for  (i = 0; i < MAX_NUM_DSI; i++) {
1811                 dsidev = dsi_get_dsidev_from_id(i);
1812                 if (dsidev)
1813                         dsi_dump_dsidev_clocks(dsidev, s);
1814         }
1815 }
1816
1817 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
1818 static void dsi_dump_dsidev_irqs(struct platform_device *dsidev,
1819                 struct seq_file *s)
1820 {
1821         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
1822         unsigned long flags;
1823         struct dsi_irq_stats stats;
1824
1825         spin_lock_irqsave(&dsi->irq_stats_lock, flags);
1826
1827         stats = dsi->irq_stats;
1828         memset(&dsi->irq_stats, 0, sizeof(dsi->irq_stats));
1829         dsi->irq_stats.last_reset = jiffies;
1830
1831         spin_unlock_irqrestore(&dsi->irq_stats_lock, flags);
1832
1833         seq_printf(s, "period %u ms\n",
1834                         jiffies_to_msecs(jiffies - stats.last_reset));
1835
1836         seq_printf(s, "irqs %d\n", stats.irq_count);
1837 #define PIS(x) \
1838         seq_printf(s, "%-20s %10d\n", #x, stats.dsi_irqs[ffs(DSI_IRQ_##x)-1]);
1839
1840         seq_printf(s, "-- DSI%d interrupts --\n", dsi->module_id + 1);
1841         PIS(VC0);
1842         PIS(VC1);
1843         PIS(VC2);
1844         PIS(VC3);
1845         PIS(WAKEUP);
1846         PIS(RESYNC);
1847         PIS(PLL_LOCK);
1848         PIS(PLL_UNLOCK);
1849         PIS(PLL_RECALL);
1850         PIS(COMPLEXIO_ERR);
1851         PIS(HS_TX_TIMEOUT);
1852         PIS(LP_RX_TIMEOUT);
1853         PIS(TE_TRIGGER);
1854         PIS(ACK_TRIGGER);
1855         PIS(SYNC_LOST);
1856         PIS(LDO_POWER_GOOD);
1857         PIS(TA_TIMEOUT);
1858 #undef PIS
1859
1860 #define PIS(x) \
1861         seq_printf(s, "%-20s %10d %10d %10d %10d\n", #x, \
1862                         stats.vc_irqs[0][ffs(DSI_VC_IRQ_##x)-1], \
1863                         stats.vc_irqs[1][ffs(DSI_VC_IRQ_##x)-1], \
1864                         stats.vc_irqs[2][ffs(DSI_VC_IRQ_##x)-1], \
1865                         stats.vc_irqs[3][ffs(DSI_VC_IRQ_##x)-1]);
1866
1867         seq_printf(s, "-- VC interrupts --\n");
1868         PIS(CS);
1869         PIS(ECC_CORR);
1870         PIS(PACKET_SENT);
1871         PIS(FIFO_TX_OVF);
1872         PIS(FIFO_RX_OVF);
1873         PIS(BTA);
1874         PIS(ECC_NO_CORR);
1875         PIS(FIFO_TX_UDF);
1876         PIS(PP_BUSY_CHANGE);
1877 #undef PIS
1878
1879 #define PIS(x) \
1880         seq_printf(s, "%-20s %10d\n", #x, \
1881                         stats.cio_irqs[ffs(DSI_CIO_IRQ_##x)-1]);
1882
1883         seq_printf(s, "-- CIO interrupts --\n");
1884         PIS(ERRSYNCESC1);
1885         PIS(ERRSYNCESC2);
1886         PIS(ERRSYNCESC3);
1887         PIS(ERRESC1);
1888         PIS(ERRESC2);
1889         PIS(ERRESC3);
1890         PIS(ERRCONTROL1);
1891         PIS(ERRCONTROL2);
1892         PIS(ERRCONTROL3);
1893         PIS(STATEULPS1);
1894         PIS(STATEULPS2);
1895         PIS(STATEULPS3);
1896         PIS(ERRCONTENTIONLP0_1);
1897         PIS(ERRCONTENTIONLP1_1);
1898         PIS(ERRCONTENTIONLP0_2);
1899         PIS(ERRCONTENTIONLP1_2);
1900         PIS(ERRCONTENTIONLP0_3);
1901         PIS(ERRCONTENTIONLP1_3);
1902         PIS(ULPSACTIVENOT_ALL0);
1903         PIS(ULPSACTIVENOT_ALL1);
1904 #undef PIS
1905 }
1906
1907 static void dsi1_dump_irqs(struct seq_file *s)
1908 {
1909         struct platform_device *dsidev = dsi_get_dsidev_from_id(0);
1910
1911         dsi_dump_dsidev_irqs(dsidev, s);
1912 }
1913
1914 static void dsi2_dump_irqs(struct seq_file *s)
1915 {
1916         struct platform_device *dsidev = dsi_get_dsidev_from_id(1);
1917
1918         dsi_dump_dsidev_irqs(dsidev, s);
1919 }
1920 #endif
1921
1922 static void dsi_dump_dsidev_regs(struct platform_device *dsidev,
1923                 struct seq_file *s)
1924 {
1925 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dsi_read_reg(dsidev, r))
1926
1927         if (dsi_runtime_get(dsidev))
1928                 return;
1929         dsi_enable_scp_clk(dsidev);
1930
1931         DUMPREG(DSI_REVISION);
1932         DUMPREG(DSI_SYSCONFIG);
1933         DUMPREG(DSI_SYSSTATUS);
1934         DUMPREG(DSI_IRQSTATUS);
1935         DUMPREG(DSI_IRQENABLE);
1936         DUMPREG(DSI_CTRL);
1937         DUMPREG(DSI_COMPLEXIO_CFG1);
1938         DUMPREG(DSI_COMPLEXIO_IRQ_STATUS);
1939         DUMPREG(DSI_COMPLEXIO_IRQ_ENABLE);
1940         DUMPREG(DSI_CLK_CTRL);
1941         DUMPREG(DSI_TIMING1);
1942         DUMPREG(DSI_TIMING2);
1943         DUMPREG(DSI_VM_TIMING1);
1944         DUMPREG(DSI_VM_TIMING2);
1945         DUMPREG(DSI_VM_TIMING3);
1946         DUMPREG(DSI_CLK_TIMING);
1947         DUMPREG(DSI_TX_FIFO_VC_SIZE);
1948         DUMPREG(DSI_RX_FIFO_VC_SIZE);
1949         DUMPREG(DSI_COMPLEXIO_CFG2);
1950         DUMPREG(DSI_RX_FIFO_VC_FULLNESS);
1951         DUMPREG(DSI_VM_TIMING4);
1952         DUMPREG(DSI_TX_FIFO_VC_EMPTINESS);
1953         DUMPREG(DSI_VM_TIMING5);
1954         DUMPREG(DSI_VM_TIMING6);
1955         DUMPREG(DSI_VM_TIMING7);
1956         DUMPREG(DSI_STOPCLK_TIMING);
1957
1958         DUMPREG(DSI_VC_CTRL(0));
1959         DUMPREG(DSI_VC_TE(0));
1960         DUMPREG(DSI_VC_LONG_PACKET_HEADER(0));
1961         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(0));
1962         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(0));
1963         DUMPREG(DSI_VC_IRQSTATUS(0));
1964         DUMPREG(DSI_VC_IRQENABLE(0));
1965
1966         DUMPREG(DSI_VC_CTRL(1));
1967         DUMPREG(DSI_VC_TE(1));
1968         DUMPREG(DSI_VC_LONG_PACKET_HEADER(1));
1969         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(1));
1970         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(1));
1971         DUMPREG(DSI_VC_IRQSTATUS(1));
1972         DUMPREG(DSI_VC_IRQENABLE(1));
1973
1974         DUMPREG(DSI_VC_CTRL(2));
1975         DUMPREG(DSI_VC_TE(2));
1976         DUMPREG(DSI_VC_LONG_PACKET_HEADER(2));
1977         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(2));
1978         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(2));
1979         DUMPREG(DSI_VC_IRQSTATUS(2));
1980         DUMPREG(DSI_VC_IRQENABLE(2));
1981
1982         DUMPREG(DSI_VC_CTRL(3));
1983         DUMPREG(DSI_VC_TE(3));
1984         DUMPREG(DSI_VC_LONG_PACKET_HEADER(3));
1985         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(3));
1986         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(3));
1987         DUMPREG(DSI_VC_IRQSTATUS(3));
1988         DUMPREG(DSI_VC_IRQENABLE(3));
1989
1990         DUMPREG(DSI_DSIPHY_CFG0);
1991         DUMPREG(DSI_DSIPHY_CFG1);
1992         DUMPREG(DSI_DSIPHY_CFG2);
1993         DUMPREG(DSI_DSIPHY_CFG5);
1994
1995         DUMPREG(DSI_PLL_CONTROL);
1996         DUMPREG(DSI_PLL_STATUS);
1997         DUMPREG(DSI_PLL_GO);
1998         DUMPREG(DSI_PLL_CONFIGURATION1);
1999         DUMPREG(DSI_PLL_CONFIGURATION2);
2000
2001         dsi_disable_scp_clk(dsidev);
2002         dsi_runtime_put(dsidev);
2003 #undef DUMPREG
2004 }
2005
2006 static void dsi1_dump_regs(struct seq_file *s)
2007 {
2008         struct platform_device *dsidev = dsi_get_dsidev_from_id(0);
2009
2010         dsi_dump_dsidev_regs(dsidev, s);
2011 }
2012
2013 static void dsi2_dump_regs(struct seq_file *s)
2014 {
2015         struct platform_device *dsidev = dsi_get_dsidev_from_id(1);
2016
2017         dsi_dump_dsidev_regs(dsidev, s);
2018 }
2019
2020 enum dsi_cio_power_state {
2021         DSI_COMPLEXIO_POWER_OFF         = 0x0,
2022         DSI_COMPLEXIO_POWER_ON          = 0x1,
2023         DSI_COMPLEXIO_POWER_ULPS        = 0x2,
2024 };
2025
2026 static int dsi_cio_power(struct platform_device *dsidev,
2027                 enum dsi_cio_power_state state)
2028 {
2029         int t = 0;
2030
2031         /* PWR_CMD */
2032         REG_FLD_MOD(dsidev, DSI_COMPLEXIO_CFG1, state, 28, 27);
2033
2034         /* PWR_STATUS */
2035         while (FLD_GET(dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG1),
2036                         26, 25) != state) {
2037                 if (++t > 1000) {
2038                         DSSERR("failed to set complexio power state to "
2039                                         "%d\n", state);
2040                         return -ENODEV;
2041                 }
2042                 udelay(1);
2043         }
2044
2045         return 0;
2046 }
2047
2048 static unsigned dsi_get_line_buf_size(struct platform_device *dsidev)
2049 {
2050         int val;
2051
2052         /* line buffer on OMAP3 is 1024 x 24bits */
2053         /* XXX: for some reason using full buffer size causes
2054          * considerable TX slowdown with update sizes that fill the
2055          * whole buffer */
2056         if (!dss_has_feature(FEAT_DSI_GNQ))
2057                 return 1023 * 3;
2058
2059         val = REG_GET(dsidev, DSI_GNQ, 14, 12); /* VP1_LINE_BUFFER_SIZE */
2060
2061         switch (val) {
2062         case 1:
2063                 return 512 * 3;         /* 512x24 bits */
2064         case 2:
2065                 return 682 * 3;         /* 682x24 bits */
2066         case 3:
2067                 return 853 * 3;         /* 853x24 bits */
2068         case 4:
2069                 return 1024 * 3;        /* 1024x24 bits */
2070         case 5:
2071                 return 1194 * 3;        /* 1194x24 bits */
2072         case 6:
2073                 return 1365 * 3;        /* 1365x24 bits */
2074         case 7:
2075                 return 1920 * 3;        /* 1920x24 bits */
2076         default:
2077                 BUG();
2078                 return 0;
2079         }
2080 }
2081
2082 static int dsi_set_lane_config(struct platform_device *dsidev)
2083 {
2084         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2085         static const u8 offsets[] = { 0, 4, 8, 12, 16 };
2086         static const enum dsi_lane_function functions[] = {
2087                 DSI_LANE_CLK,
2088                 DSI_LANE_DATA1,
2089                 DSI_LANE_DATA2,
2090                 DSI_LANE_DATA3,
2091                 DSI_LANE_DATA4,
2092         };
2093         u32 r;
2094         int i;
2095
2096         r = dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG1);
2097
2098         for (i = 0; i < dsi->num_lanes_used; ++i) {
2099                 unsigned offset = offsets[i];
2100                 unsigned polarity, lane_number;
2101                 unsigned t;
2102
2103                 for (t = 0; t < dsi->num_lanes_supported; ++t)
2104                         if (dsi->lanes[t].function == functions[i])
2105                                 break;
2106
2107                 if (t == dsi->num_lanes_supported)
2108                         return -EINVAL;
2109
2110                 lane_number = t;
2111                 polarity = dsi->lanes[t].polarity;
2112
2113                 r = FLD_MOD(r, lane_number + 1, offset + 2, offset);
2114                 r = FLD_MOD(r, polarity, offset + 3, offset + 3);
2115         }
2116
2117         /* clear the unused lanes */
2118         for (; i < dsi->num_lanes_supported; ++i) {
2119                 unsigned offset = offsets[i];
2120
2121                 r = FLD_MOD(r, 0, offset + 2, offset);
2122                 r = FLD_MOD(r, 0, offset + 3, offset + 3);
2123         }
2124
2125         dsi_write_reg(dsidev, DSI_COMPLEXIO_CFG1, r);
2126
2127         return 0;
2128 }
2129
2130 static inline unsigned ns2ddr(struct platform_device *dsidev, unsigned ns)
2131 {
2132         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2133
2134         /* convert time in ns to ddr ticks, rounding up */
2135         unsigned long ddr_clk = dsi->current_cinfo.clkin4ddr / 4;
2136         return (ns * (ddr_clk / 1000 / 1000) + 999) / 1000;
2137 }
2138
2139 static inline unsigned ddr2ns(struct platform_device *dsidev, unsigned ddr)
2140 {
2141         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2142
2143         unsigned long ddr_clk = dsi->current_cinfo.clkin4ddr / 4;
2144         return ddr * 1000 * 1000 / (ddr_clk / 1000);
2145 }
2146
2147 static void dsi_cio_timings(struct platform_device *dsidev)
2148 {
2149         u32 r;
2150         u32 ths_prepare, ths_prepare_ths_zero, ths_trail, ths_exit;
2151         u32 tlpx_half, tclk_trail, tclk_zero;
2152         u32 tclk_prepare;
2153
2154         /* calculate timings */
2155
2156         /* 1 * DDR_CLK = 2 * UI */
2157
2158         /* min 40ns + 4*UI      max 85ns + 6*UI */
2159         ths_prepare = ns2ddr(dsidev, 70) + 2;
2160
2161         /* min 145ns + 10*UI */
2162         ths_prepare_ths_zero = ns2ddr(dsidev, 175) + 2;
2163
2164         /* min max(8*UI, 60ns+4*UI) */
2165         ths_trail = ns2ddr(dsidev, 60) + 5;
2166
2167         /* min 100ns */
2168         ths_exit = ns2ddr(dsidev, 145);
2169
2170         /* tlpx min 50n */
2171         tlpx_half = ns2ddr(dsidev, 25);
2172
2173         /* min 60ns */
2174         tclk_trail = ns2ddr(dsidev, 60) + 2;
2175
2176         /* min 38ns, max 95ns */
2177         tclk_prepare = ns2ddr(dsidev, 65);
2178
2179         /* min tclk-prepare + tclk-zero = 300ns */
2180         tclk_zero = ns2ddr(dsidev, 260);
2181
2182         DSSDBG("ths_prepare %u (%uns), ths_prepare_ths_zero %u (%uns)\n",
2183                 ths_prepare, ddr2ns(dsidev, ths_prepare),
2184                 ths_prepare_ths_zero, ddr2ns(dsidev, ths_prepare_ths_zero));
2185         DSSDBG("ths_trail %u (%uns), ths_exit %u (%uns)\n",
2186                         ths_trail, ddr2ns(dsidev, ths_trail),
2187                         ths_exit, ddr2ns(dsidev, ths_exit));
2188
2189         DSSDBG("tlpx_half %u (%uns), tclk_trail %u (%uns), "
2190                         "tclk_zero %u (%uns)\n",
2191                         tlpx_half, ddr2ns(dsidev, tlpx_half),
2192                         tclk_trail, ddr2ns(dsidev, tclk_trail),
2193                         tclk_zero, ddr2ns(dsidev, tclk_zero));
2194         DSSDBG("tclk_prepare %u (%uns)\n",
2195                         tclk_prepare, ddr2ns(dsidev, tclk_prepare));
2196
2197         /* program timings */
2198
2199         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG0);
2200         r = FLD_MOD(r, ths_prepare, 31, 24);
2201         r = FLD_MOD(r, ths_prepare_ths_zero, 23, 16);
2202         r = FLD_MOD(r, ths_trail, 15, 8);
2203         r = FLD_MOD(r, ths_exit, 7, 0);
2204         dsi_write_reg(dsidev, DSI_DSIPHY_CFG0, r);
2205
2206         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG1);
2207         r = FLD_MOD(r, tlpx_half, 20, 16);
2208         r = FLD_MOD(r, tclk_trail, 15, 8);
2209         r = FLD_MOD(r, tclk_zero, 7, 0);
2210
2211         if (dss_has_feature(FEAT_DSI_PHY_DCC)) {
2212                 r = FLD_MOD(r, 0, 21, 21);      /* DCCEN = disable */
2213                 r = FLD_MOD(r, 1, 22, 22);      /* CLKINP_DIVBY2EN = enable */
2214                 r = FLD_MOD(r, 1, 23, 23);      /* CLKINP_SEL = enable */
2215         }
2216
2217         dsi_write_reg(dsidev, DSI_DSIPHY_CFG1, r);
2218
2219         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG2);
2220         r = FLD_MOD(r, tclk_prepare, 7, 0);
2221         dsi_write_reg(dsidev, DSI_DSIPHY_CFG2, r);
2222 }
2223
2224 /* lane masks have lane 0 at lsb. mask_p for positive lines, n for negative */
2225 static void dsi_cio_enable_lane_override(struct platform_device *dsidev,
2226                 unsigned mask_p, unsigned mask_n)
2227 {
2228         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2229         int i;
2230         u32 l;
2231         u8 lptxscp_start = dsi->num_lanes_supported == 3 ? 22 : 26;
2232
2233         l = 0;
2234
2235         for (i = 0; i < dsi->num_lanes_supported; ++i) {
2236                 unsigned p = dsi->lanes[i].polarity;
2237
2238                 if (mask_p & (1 << i))
2239                         l |= 1 << (i * 2 + (p ? 0 : 1));
2240
2241                 if (mask_n & (1 << i))
2242                         l |= 1 << (i * 2 + (p ? 1 : 0));
2243         }
2244
2245         /*
2246          * Bits in REGLPTXSCPDAT4TO0DXDY:
2247          * 17: DY0 18: DX0
2248          * 19: DY1 20: DX1
2249          * 21: DY2 22: DX2
2250          * 23: DY3 24: DX3
2251          * 25: DY4 26: DX4
2252          */
2253
2254         /* Set the lane override configuration */
2255
2256         /* REGLPTXSCPDAT4TO0DXDY */
2257         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, l, lptxscp_start, 17);
2258
2259         /* Enable lane override */
2260
2261         /* ENLPTXSCPDAT */
2262         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 1, 27, 27);
2263 }
2264
2265 static void dsi_cio_disable_lane_override(struct platform_device *dsidev)
2266 {
2267         /* Disable lane override */
2268         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 0, 27, 27); /* ENLPTXSCPDAT */
2269         /* Reset the lane override configuration */
2270         /* REGLPTXSCPDAT4TO0DXDY */
2271         REG_FLD_MOD(dsidev, DSI_DSIPHY_CFG10, 0, 22, 17);
2272 }
2273
2274 static int dsi_cio_wait_tx_clk_esc_reset(struct platform_device *dsidev)
2275 {
2276         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2277         int t, i;
2278         bool in_use[DSI_MAX_NR_LANES];
2279         static const u8 offsets_old[] = { 28, 27, 26 };
2280         static const u8 offsets_new[] = { 24, 25, 26, 27, 28 };
2281         const u8 *offsets;
2282
2283         if (dss_has_feature(FEAT_DSI_REVERSE_TXCLKESC))
2284                 offsets = offsets_old;
2285         else
2286                 offsets = offsets_new;
2287
2288         for (i = 0; i < dsi->num_lanes_supported; ++i)
2289                 in_use[i] = dsi->lanes[i].function != DSI_LANE_UNUSED;
2290
2291         t = 100000;
2292         while (true) {
2293                 u32 l;
2294                 int ok;
2295
2296                 l = dsi_read_reg(dsidev, DSI_DSIPHY_CFG5);
2297
2298                 ok = 0;
2299                 for (i = 0; i < dsi->num_lanes_supported; ++i) {
2300                         if (!in_use[i] || (l & (1 << offsets[i])))
2301                                 ok++;
2302                 }
2303
2304                 if (ok == dsi->num_lanes_supported)
2305                         break;
2306
2307                 if (--t == 0) {
2308                         for (i = 0; i < dsi->num_lanes_supported; ++i) {
2309                                 if (!in_use[i] || (l & (1 << offsets[i])))
2310                                         continue;
2311
2312                                 DSSERR("CIO TXCLKESC%d domain not coming " \
2313                                                 "out of reset\n", i);
2314                         }
2315                         return -EIO;
2316                 }
2317         }
2318
2319         return 0;
2320 }
2321
2322 /* return bitmask of enabled lanes, lane0 being the lsb */
2323 static unsigned dsi_get_lane_mask(struct platform_device *dsidev)
2324 {
2325         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2326         unsigned mask = 0;
2327         int i;
2328
2329         for (i = 0; i < dsi->num_lanes_supported; ++i) {
2330                 if (dsi->lanes[i].function != DSI_LANE_UNUSED)
2331                         mask |= 1 << i;
2332         }
2333
2334         return mask;
2335 }
2336
2337 static int dsi_cio_init(struct platform_device *dsidev)
2338 {
2339         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2340         int r;
2341         u32 l;
2342
2343         DSSDBG("DSI CIO init starts");
2344
2345         r = dss_dsi_enable_pads(dsi->module_id, dsi_get_lane_mask(dsidev));
2346         if (r)
2347                 return r;
2348
2349         dsi_enable_scp_clk(dsidev);
2350
2351         /* A dummy read using the SCP interface to any DSIPHY register is
2352          * required after DSIPHY reset to complete the reset of the DSI complex
2353          * I/O. */
2354         dsi_read_reg(dsidev, DSI_DSIPHY_CFG5);
2355
2356         if (wait_for_bit_change(dsidev, DSI_DSIPHY_CFG5, 30, 1) != 1) {
2357                 DSSERR("CIO SCP Clock domain not coming out of reset.\n");
2358                 r = -EIO;
2359                 goto err_scp_clk_dom;
2360         }
2361
2362         r = dsi_set_lane_config(dsidev);
2363         if (r)
2364                 goto err_scp_clk_dom;
2365
2366         /* set TX STOP MODE timer to maximum for this operation */
2367         l = dsi_read_reg(dsidev, DSI_TIMING1);
2368         l = FLD_MOD(l, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2369         l = FLD_MOD(l, 1, 14, 14);      /* STOP_STATE_X16_IO */
2370         l = FLD_MOD(l, 1, 13, 13);      /* STOP_STATE_X4_IO */
2371         l = FLD_MOD(l, 0x1fff, 12, 0);  /* STOP_STATE_COUNTER_IO */
2372         dsi_write_reg(dsidev, DSI_TIMING1, l);
2373
2374         if (dsi->ulps_enabled) {
2375                 unsigned mask_p;
2376                 int i;
2377
2378                 DSSDBG("manual ulps exit\n");
2379
2380                 /* ULPS is exited by Mark-1 state for 1ms, followed by
2381                  * stop state. DSS HW cannot do this via the normal
2382                  * ULPS exit sequence, as after reset the DSS HW thinks
2383                  * that we are not in ULPS mode, and refuses to send the
2384                  * sequence. So we need to send the ULPS exit sequence
2385                  * manually by setting positive lines high and negative lines
2386                  * low for 1ms.
2387                  */
2388
2389                 mask_p = 0;
2390
2391                 for (i = 0; i < dsi->num_lanes_supported; ++i) {
2392                         if (dsi->lanes[i].function == DSI_LANE_UNUSED)
2393                                 continue;
2394                         mask_p |= 1 << i;
2395                 }
2396
2397                 dsi_cio_enable_lane_override(dsidev, mask_p, 0);
2398         }
2399
2400         r = dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_ON);
2401         if (r)
2402                 goto err_cio_pwr;
2403
2404         if (wait_for_bit_change(dsidev, DSI_COMPLEXIO_CFG1, 29, 1) != 1) {
2405                 DSSERR("CIO PWR clock domain not coming out of reset.\n");
2406                 r = -ENODEV;
2407                 goto err_cio_pwr_dom;
2408         }
2409
2410         dsi_if_enable(dsidev, true);
2411         dsi_if_enable(dsidev, false);
2412         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 1, 20, 20); /* LP_CLK_ENABLE */
2413
2414         r = dsi_cio_wait_tx_clk_esc_reset(dsidev);
2415         if (r)
2416                 goto err_tx_clk_esc_rst;
2417
2418         if (dsi->ulps_enabled) {
2419                 /* Keep Mark-1 state for 1ms (as per DSI spec) */
2420                 ktime_t wait = ns_to_ktime(1000 * 1000);
2421                 set_current_state(TASK_UNINTERRUPTIBLE);
2422                 schedule_hrtimeout(&wait, HRTIMER_MODE_REL);
2423
2424                 /* Disable the override. The lanes should be set to Mark-11
2425                  * state by the HW */
2426                 dsi_cio_disable_lane_override(dsidev);
2427         }
2428
2429         /* FORCE_TX_STOP_MODE_IO */
2430         REG_FLD_MOD(dsidev, DSI_TIMING1, 0, 15, 15);
2431
2432         dsi_cio_timings(dsidev);
2433
2434         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
2435                 /* DDR_CLK_ALWAYS_ON */
2436                 REG_FLD_MOD(dsidev, DSI_CLK_CTRL,
2437                         dsi->vm_timings.ddr_clk_always_on, 13, 13);
2438         }
2439
2440         dsi->ulps_enabled = false;
2441
2442         DSSDBG("CIO init done\n");
2443
2444         return 0;
2445
2446 err_tx_clk_esc_rst:
2447         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 0, 20, 20); /* LP_CLK_ENABLE */
2448 err_cio_pwr_dom:
2449         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_OFF);
2450 err_cio_pwr:
2451         if (dsi->ulps_enabled)
2452                 dsi_cio_disable_lane_override(dsidev);
2453 err_scp_clk_dom:
2454         dsi_disable_scp_clk(dsidev);
2455         dss_dsi_disable_pads(dsi->module_id, dsi_get_lane_mask(dsidev));
2456         return r;
2457 }
2458
2459 static void dsi_cio_uninit(struct platform_device *dsidev)
2460 {
2461         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2462
2463         /* DDR_CLK_ALWAYS_ON */
2464         REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 0, 13, 13);
2465
2466         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_OFF);
2467         dsi_disable_scp_clk(dsidev);
2468         dss_dsi_disable_pads(dsi->module_id, dsi_get_lane_mask(dsidev));
2469 }
2470
2471 static void dsi_config_tx_fifo(struct platform_device *dsidev,
2472                 enum fifo_size size1, enum fifo_size size2,
2473                 enum fifo_size size3, enum fifo_size size4)
2474 {
2475         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2476         u32 r = 0;
2477         int add = 0;
2478         int i;
2479
2480         dsi->vc[0].tx_fifo_size = size1;
2481         dsi->vc[1].tx_fifo_size = size2;
2482         dsi->vc[2].tx_fifo_size = size3;
2483         dsi->vc[3].tx_fifo_size = size4;
2484
2485         for (i = 0; i < 4; i++) {
2486                 u8 v;
2487                 int size = dsi->vc[i].tx_fifo_size;
2488
2489                 if (add + size > 4) {
2490                         DSSERR("Illegal FIFO configuration\n");
2491                         BUG();
2492                         return;
2493                 }
2494
2495                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
2496                 r |= v << (8 * i);
2497                 /*DSSDBG("TX FIFO vc %d: size %d, add %d\n", i, size, add); */
2498                 add += size;
2499         }
2500
2501         dsi_write_reg(dsidev, DSI_TX_FIFO_VC_SIZE, r);
2502 }
2503
2504 static void dsi_config_rx_fifo(struct platform_device *dsidev,
2505                 enum fifo_size size1, enum fifo_size size2,
2506                 enum fifo_size size3, enum fifo_size size4)
2507 {
2508         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2509         u32 r = 0;
2510         int add = 0;
2511         int i;
2512
2513         dsi->vc[0].rx_fifo_size = size1;
2514         dsi->vc[1].rx_fifo_size = size2;
2515         dsi->vc[2].rx_fifo_size = size3;
2516         dsi->vc[3].rx_fifo_size = size4;
2517
2518         for (i = 0; i < 4; i++) {
2519                 u8 v;
2520                 int size = dsi->vc[i].rx_fifo_size;
2521
2522                 if (add + size > 4) {
2523                         DSSERR("Illegal FIFO configuration\n");
2524                         BUG();
2525                         return;
2526                 }
2527
2528                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
2529                 r |= v << (8 * i);
2530                 /*DSSDBG("RX FIFO vc %d: size %d, add %d\n", i, size, add); */
2531                 add += size;
2532         }
2533
2534         dsi_write_reg(dsidev, DSI_RX_FIFO_VC_SIZE, r);
2535 }
2536
2537 static int dsi_force_tx_stop_mode_io(struct platform_device *dsidev)
2538 {
2539         u32 r;
2540
2541         r = dsi_read_reg(dsidev, DSI_TIMING1);
2542         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2543         dsi_write_reg(dsidev, DSI_TIMING1, r);
2544
2545         if (wait_for_bit_change(dsidev, DSI_TIMING1, 15, 0) != 0) {
2546                 DSSERR("TX_STOP bit not going down\n");
2547                 return -EIO;
2548         }
2549
2550         return 0;
2551 }
2552
2553 static bool dsi_vc_is_enabled(struct platform_device *dsidev, int channel)
2554 {
2555         return REG_GET(dsidev, DSI_VC_CTRL(channel), 0, 0);
2556 }
2557
2558 static void dsi_packet_sent_handler_vp(void *data, u32 mask)
2559 {
2560         struct dsi_packet_sent_handler_data *vp_data =
2561                 (struct dsi_packet_sent_handler_data *) data;
2562         struct dsi_data *dsi = dsi_get_dsidrv_data(vp_data->dsidev);
2563         const int channel = dsi->update_channel;
2564         u8 bit = dsi->te_enabled ? 30 : 31;
2565
2566         if (REG_GET(vp_data->dsidev, DSI_VC_TE(channel), bit, bit) == 0)
2567                 complete(vp_data->completion);
2568 }
2569
2570 static int dsi_sync_vc_vp(struct platform_device *dsidev, int channel)
2571 {
2572         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2573         DECLARE_COMPLETION_ONSTACK(completion);
2574         struct dsi_packet_sent_handler_data vp_data = { dsidev, &completion };
2575         int r = 0;
2576         u8 bit;
2577
2578         bit = dsi->te_enabled ? 30 : 31;
2579
2580         r = dsi_register_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2581                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2582         if (r)
2583                 goto err0;
2584
2585         /* Wait for completion only if TE_EN/TE_START is still set */
2586         if (REG_GET(dsidev, DSI_VC_TE(channel), bit, bit)) {
2587                 if (wait_for_completion_timeout(&completion,
2588                                 msecs_to_jiffies(10)) == 0) {
2589                         DSSERR("Failed to complete previous frame transfer\n");
2590                         r = -EIO;
2591                         goto err1;
2592                 }
2593         }
2594
2595         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2596                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2597
2598         return 0;
2599 err1:
2600         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_vp,
2601                 &vp_data, DSI_VC_IRQ_PACKET_SENT);
2602 err0:
2603         return r;
2604 }
2605
2606 static void dsi_packet_sent_handler_l4(void *data, u32 mask)
2607 {
2608         struct dsi_packet_sent_handler_data *l4_data =
2609                 (struct dsi_packet_sent_handler_data *) data;
2610         struct dsi_data *dsi = dsi_get_dsidrv_data(l4_data->dsidev);
2611         const int channel = dsi->update_channel;
2612
2613         if (REG_GET(l4_data->dsidev, DSI_VC_CTRL(channel), 5, 5) == 0)
2614                 complete(l4_data->completion);
2615 }
2616
2617 static int dsi_sync_vc_l4(struct platform_device *dsidev, int channel)
2618 {
2619         DECLARE_COMPLETION_ONSTACK(completion);
2620         struct dsi_packet_sent_handler_data l4_data = { dsidev, &completion };
2621         int r = 0;
2622
2623         r = dsi_register_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2624                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2625         if (r)
2626                 goto err0;
2627
2628         /* Wait for completion only if TX_FIFO_NOT_EMPTY is still set */
2629         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 5, 5)) {
2630                 if (wait_for_completion_timeout(&completion,
2631                                 msecs_to_jiffies(10)) == 0) {
2632                         DSSERR("Failed to complete previous l4 transfer\n");
2633                         r = -EIO;
2634                         goto err1;
2635                 }
2636         }
2637
2638         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2639                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2640
2641         return 0;
2642 err1:
2643         dsi_unregister_isr_vc(dsidev, channel, dsi_packet_sent_handler_l4,
2644                 &l4_data, DSI_VC_IRQ_PACKET_SENT);
2645 err0:
2646         return r;
2647 }
2648
2649 static int dsi_sync_vc(struct platform_device *dsidev, int channel)
2650 {
2651         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2652
2653         WARN_ON(!dsi_bus_is_locked(dsidev));
2654
2655         WARN_ON(in_interrupt());
2656
2657         if (!dsi_vc_is_enabled(dsidev, channel))
2658                 return 0;
2659
2660         switch (dsi->vc[channel].source) {
2661         case DSI_VC_SOURCE_VP:
2662                 return dsi_sync_vc_vp(dsidev, channel);
2663         case DSI_VC_SOURCE_L4:
2664                 return dsi_sync_vc_l4(dsidev, channel);
2665         default:
2666                 BUG();
2667                 return -EINVAL;
2668         }
2669 }
2670
2671 static int dsi_vc_enable(struct platform_device *dsidev, int channel,
2672                 bool enable)
2673 {
2674         DSSDBG("dsi_vc_enable channel %d, enable %d\n",
2675                         channel, enable);
2676
2677         enable = enable ? 1 : 0;
2678
2679         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), enable, 0, 0);
2680
2681         if (wait_for_bit_change(dsidev, DSI_VC_CTRL(channel),
2682                 0, enable) != enable) {
2683                         DSSERR("Failed to set dsi_vc_enable to %d\n", enable);
2684                         return -EIO;
2685         }
2686
2687         return 0;
2688 }
2689
2690 static void dsi_vc_initial_config(struct platform_device *dsidev, int channel)
2691 {
2692         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2693         u32 r;
2694
2695         DSSDBG("Initial config of virtual channel %d", channel);
2696
2697         r = dsi_read_reg(dsidev, DSI_VC_CTRL(channel));
2698
2699         if (FLD_GET(r, 15, 15)) /* VC_BUSY */
2700                 DSSERR("VC(%d) busy when trying to configure it!\n",
2701                                 channel);
2702
2703         r = FLD_MOD(r, 0, 1, 1); /* SOURCE, 0 = L4 */
2704         r = FLD_MOD(r, 0, 2, 2); /* BTA_SHORT_EN  */
2705         r = FLD_MOD(r, 0, 3, 3); /* BTA_LONG_EN */
2706         r = FLD_MOD(r, 0, 4, 4); /* MODE, 0 = command */
2707         r = FLD_MOD(r, 1, 7, 7); /* CS_TX_EN */
2708         r = FLD_MOD(r, 1, 8, 8); /* ECC_TX_EN */
2709         r = FLD_MOD(r, 0, 9, 9); /* MODE_SPEED, high speed on/off */
2710         if (dss_has_feature(FEAT_DSI_VC_OCP_WIDTH))
2711                 r = FLD_MOD(r, 3, 11, 10);      /* OCP_WIDTH = 32 bit */
2712
2713         r = FLD_MOD(r, 4, 29, 27); /* DMA_RX_REQ_NB = no dma */
2714         r = FLD_MOD(r, 4, 23, 21); /* DMA_TX_REQ_NB = no dma */
2715
2716         dsi_write_reg(dsidev, DSI_VC_CTRL(channel), r);
2717
2718         dsi->vc[channel].source = DSI_VC_SOURCE_L4;
2719 }
2720
2721 static int dsi_vc_config_source(struct platform_device *dsidev, int channel,
2722                 enum dsi_vc_source source)
2723 {
2724         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2725
2726         if (dsi->vc[channel].source == source)
2727                 return 0;
2728
2729         DSSDBG("Source config of virtual channel %d", channel);
2730
2731         dsi_sync_vc(dsidev, channel);
2732
2733         dsi_vc_enable(dsidev, channel, 0);
2734
2735         /* VC_BUSY */
2736         if (wait_for_bit_change(dsidev, DSI_VC_CTRL(channel), 15, 0) != 0) {
2737                 DSSERR("vc(%d) busy when trying to config for VP\n", channel);
2738                 return -EIO;
2739         }
2740
2741         /* SOURCE, 0 = L4, 1 = video port */
2742         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), source, 1, 1);
2743
2744         /* DCS_CMD_ENABLE */
2745         if (dss_has_feature(FEAT_DSI_DCS_CMD_CONFIG_VC)) {
2746                 bool enable = source == DSI_VC_SOURCE_VP;
2747                 REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), enable, 30, 30);
2748         }
2749
2750         dsi_vc_enable(dsidev, channel, 1);
2751
2752         dsi->vc[channel].source = source;
2753
2754         return 0;
2755 }
2756
2757 static void dsi_vc_enable_hs(struct omap_dss_device *dssdev, int channel,
2758                 bool enable)
2759 {
2760         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2761         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2762
2763         DSSDBG("dsi_vc_enable_hs(%d, %d)\n", channel, enable);
2764
2765         WARN_ON(!dsi_bus_is_locked(dsidev));
2766
2767         dsi_vc_enable(dsidev, channel, 0);
2768         dsi_if_enable(dsidev, 0);
2769
2770         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), enable, 9, 9);
2771
2772         dsi_vc_enable(dsidev, channel, 1);
2773         dsi_if_enable(dsidev, 1);
2774
2775         dsi_force_tx_stop_mode_io(dsidev);
2776
2777         /* start the DDR clock by sending a NULL packet */
2778         if (dsi->vm_timings.ddr_clk_always_on && enable)
2779                 dsi_vc_send_null(dssdev, channel);
2780 }
2781
2782 static void dsi_vc_flush_long_data(struct platform_device *dsidev, int channel)
2783 {
2784         while (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2785                 u32 val;
2786                 val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
2787                 DSSDBG("\t\tb1 %#02x b2 %#02x b3 %#02x b4 %#02x\n",
2788                                 (val >> 0) & 0xff,
2789                                 (val >> 8) & 0xff,
2790                                 (val >> 16) & 0xff,
2791                                 (val >> 24) & 0xff);
2792         }
2793 }
2794
2795 static void dsi_show_rx_ack_with_err(u16 err)
2796 {
2797         DSSERR("\tACK with ERROR (%#x):\n", err);
2798         if (err & (1 << 0))
2799                 DSSERR("\t\tSoT Error\n");
2800         if (err & (1 << 1))
2801                 DSSERR("\t\tSoT Sync Error\n");
2802         if (err & (1 << 2))
2803                 DSSERR("\t\tEoT Sync Error\n");
2804         if (err & (1 << 3))
2805                 DSSERR("\t\tEscape Mode Entry Command Error\n");
2806         if (err & (1 << 4))
2807                 DSSERR("\t\tLP Transmit Sync Error\n");
2808         if (err & (1 << 5))
2809                 DSSERR("\t\tHS Receive Timeout Error\n");
2810         if (err & (1 << 6))
2811                 DSSERR("\t\tFalse Control Error\n");
2812         if (err & (1 << 7))
2813                 DSSERR("\t\t(reserved7)\n");
2814         if (err & (1 << 8))
2815                 DSSERR("\t\tECC Error, single-bit (corrected)\n");
2816         if (err & (1 << 9))
2817                 DSSERR("\t\tECC Error, multi-bit (not corrected)\n");
2818         if (err & (1 << 10))
2819                 DSSERR("\t\tChecksum Error\n");
2820         if (err & (1 << 11))
2821                 DSSERR("\t\tData type not recognized\n");
2822         if (err & (1 << 12))
2823                 DSSERR("\t\tInvalid VC ID\n");
2824         if (err & (1 << 13))
2825                 DSSERR("\t\tInvalid Transmission Length\n");
2826         if (err & (1 << 14))
2827                 DSSERR("\t\t(reserved14)\n");
2828         if (err & (1 << 15))
2829                 DSSERR("\t\tDSI Protocol Violation\n");
2830 }
2831
2832 static u16 dsi_vc_flush_receive_data(struct platform_device *dsidev,
2833                 int channel)
2834 {
2835         /* RX_FIFO_NOT_EMPTY */
2836         while (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2837                 u32 val;
2838                 u8 dt;
2839                 val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
2840                 DSSERR("\trawval %#08x\n", val);
2841                 dt = FLD_GET(val, 5, 0);
2842                 if (dt == MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT) {
2843                         u16 err = FLD_GET(val, 23, 8);
2844                         dsi_show_rx_ack_with_err(err);
2845                 } else if (dt == MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE) {
2846                         DSSERR("\tDCS short response, 1 byte: %#x\n",
2847                                         FLD_GET(val, 23, 8));
2848                 } else if (dt == MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE) {
2849                         DSSERR("\tDCS short response, 2 byte: %#x\n",
2850                                         FLD_GET(val, 23, 8));
2851                 } else if (dt == MIPI_DSI_RX_DCS_LONG_READ_RESPONSE) {
2852                         DSSERR("\tDCS long response, len %d\n",
2853                                         FLD_GET(val, 23, 8));
2854                         dsi_vc_flush_long_data(dsidev, channel);
2855                 } else {
2856                         DSSERR("\tunknown datatype 0x%02x\n", dt);
2857                 }
2858         }
2859         return 0;
2860 }
2861
2862 static int dsi_vc_send_bta(struct platform_device *dsidev, int channel)
2863 {
2864         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2865
2866         if (dsi->debug_write || dsi->debug_read)
2867                 DSSDBG("dsi_vc_send_bta %d\n", channel);
2868
2869         WARN_ON(!dsi_bus_is_locked(dsidev));
2870
2871         /* RX_FIFO_NOT_EMPTY */
2872         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
2873                 DSSERR("rx fifo not empty when sending BTA, dumping data:\n");
2874                 dsi_vc_flush_receive_data(dsidev, channel);
2875         }
2876
2877         REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 1, 6, 6); /* BTA_EN */
2878
2879         /* flush posted write */
2880         dsi_read_reg(dsidev, DSI_VC_CTRL(channel));
2881
2882         return 0;
2883 }
2884
2885 static int dsi_vc_send_bta_sync(struct omap_dss_device *dssdev, int channel)
2886 {
2887         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
2888         DECLARE_COMPLETION_ONSTACK(completion);
2889         int r = 0;
2890         u32 err;
2891
2892         r = dsi_register_isr_vc(dsidev, channel, dsi_completion_handler,
2893                         &completion, DSI_VC_IRQ_BTA);
2894         if (r)
2895                 goto err0;
2896
2897         r = dsi_register_isr(dsidev, dsi_completion_handler, &completion,
2898                         DSI_IRQ_ERROR_MASK);
2899         if (r)
2900                 goto err1;
2901
2902         r = dsi_vc_send_bta(dsidev, channel);
2903         if (r)
2904                 goto err2;
2905
2906         if (wait_for_completion_timeout(&completion,
2907                                 msecs_to_jiffies(500)) == 0) {
2908                 DSSERR("Failed to receive BTA\n");
2909                 r = -EIO;
2910                 goto err2;
2911         }
2912
2913         err = dsi_get_errors(dsidev);
2914         if (err) {
2915                 DSSERR("Error while sending BTA: %x\n", err);
2916                 r = -EIO;
2917                 goto err2;
2918         }
2919 err2:
2920         dsi_unregister_isr(dsidev, dsi_completion_handler, &completion,
2921                         DSI_IRQ_ERROR_MASK);
2922 err1:
2923         dsi_unregister_isr_vc(dsidev, channel, dsi_completion_handler,
2924                         &completion, DSI_VC_IRQ_BTA);
2925 err0:
2926         return r;
2927 }
2928
2929 static inline void dsi_vc_write_long_header(struct platform_device *dsidev,
2930                 int channel, u8 data_type, u16 len, u8 ecc)
2931 {
2932         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2933         u32 val;
2934         u8 data_id;
2935
2936         WARN_ON(!dsi_bus_is_locked(dsidev));
2937
2938         data_id = data_type | dsi->vc[channel].vc_id << 6;
2939
2940         val = FLD_VAL(data_id, 7, 0) | FLD_VAL(len, 23, 8) |
2941                 FLD_VAL(ecc, 31, 24);
2942
2943         dsi_write_reg(dsidev, DSI_VC_LONG_PACKET_HEADER(channel), val);
2944 }
2945
2946 static inline void dsi_vc_write_long_payload(struct platform_device *dsidev,
2947                 int channel, u8 b1, u8 b2, u8 b3, u8 b4)
2948 {
2949         u32 val;
2950
2951         val = b4 << 24 | b3 << 16 | b2 << 8  | b1 << 0;
2952
2953 /*      DSSDBG("\twriting %02x, %02x, %02x, %02x (%#010x)\n",
2954                         b1, b2, b3, b4, val); */
2955
2956         dsi_write_reg(dsidev, DSI_VC_LONG_PACKET_PAYLOAD(channel), val);
2957 }
2958
2959 static int dsi_vc_send_long(struct platform_device *dsidev, int channel,
2960                 u8 data_type, u8 *data, u16 len, u8 ecc)
2961 {
2962         /*u32 val; */
2963         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
2964         int i;
2965         u8 *p;
2966         int r = 0;
2967         u8 b1, b2, b3, b4;
2968
2969         if (dsi->debug_write)
2970                 DSSDBG("dsi_vc_send_long, %d bytes\n", len);
2971
2972         /* len + header */
2973         if (dsi->vc[channel].tx_fifo_size * 32 * 4 < len + 4) {
2974                 DSSERR("unable to send long packet: packet too long.\n");
2975                 return -EINVAL;
2976         }
2977
2978         dsi_vc_config_source(dsidev, channel, DSI_VC_SOURCE_L4);
2979
2980         dsi_vc_write_long_header(dsidev, channel, data_type, len, ecc);
2981
2982         p = data;
2983         for (i = 0; i < len >> 2; i++) {
2984                 if (dsi->debug_write)
2985                         DSSDBG("\tsending full packet %d\n", i);
2986
2987                 b1 = *p++;
2988                 b2 = *p++;
2989                 b3 = *p++;
2990                 b4 = *p++;
2991
2992                 dsi_vc_write_long_payload(dsidev, channel, b1, b2, b3, b4);
2993         }
2994
2995         i = len % 4;
2996         if (i) {
2997                 b1 = 0; b2 = 0; b3 = 0;
2998
2999                 if (dsi->debug_write)
3000                         DSSDBG("\tsending remainder bytes %d\n", i);
3001
3002                 switch (i) {
3003                 case 3:
3004                         b1 = *p++;
3005                         b2 = *p++;
3006                         b3 = *p++;
3007                         break;
3008                 case 2:
3009                         b1 = *p++;
3010                         b2 = *p++;
3011                         break;
3012                 case 1:
3013                         b1 = *p++;
3014                         break;
3015                 }
3016
3017                 dsi_vc_write_long_payload(dsidev, channel, b1, b2, b3, 0);
3018         }
3019
3020         return r;
3021 }
3022
3023 static int dsi_vc_send_short(struct platform_device *dsidev, int channel,
3024                 u8 data_type, u16 data, u8 ecc)
3025 {
3026         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3027         u32 r;
3028         u8 data_id;
3029
3030         WARN_ON(!dsi_bus_is_locked(dsidev));
3031
3032         if (dsi->debug_write)
3033                 DSSDBG("dsi_vc_send_short(ch%d, dt %#x, b1 %#x, b2 %#x)\n",
3034                                 channel,
3035                                 data_type, data & 0xff, (data >> 8) & 0xff);
3036
3037         dsi_vc_config_source(dsidev, channel, DSI_VC_SOURCE_L4);
3038
3039         if (FLD_GET(dsi_read_reg(dsidev, DSI_VC_CTRL(channel)), 16, 16)) {
3040                 DSSERR("ERROR FIFO FULL, aborting transfer\n");
3041                 return -EINVAL;
3042         }
3043
3044         data_id = data_type | dsi->vc[channel].vc_id << 6;
3045
3046         r = (data_id << 0) | (data << 8) | (ecc << 24);
3047
3048         dsi_write_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel), r);
3049
3050         return 0;
3051 }
3052
3053 static int dsi_vc_send_null(struct omap_dss_device *dssdev, int channel)
3054 {
3055         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3056
3057         return dsi_vc_send_long(dsidev, channel, MIPI_DSI_NULL_PACKET, NULL,
3058                 0, 0);
3059 }
3060
3061 static int dsi_vc_write_nosync_common(struct platform_device *dsidev,
3062                 int channel, u8 *data, int len, enum dss_dsi_content_type type)
3063 {
3064         int r;
3065
3066         if (len == 0) {
3067                 BUG_ON(type == DSS_DSI_CONTENT_DCS);
3068                 r = dsi_vc_send_short(dsidev, channel,
3069                                 MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM, 0, 0);
3070         } else if (len == 1) {
3071                 r = dsi_vc_send_short(dsidev, channel,
3072                                 type == DSS_DSI_CONTENT_GENERIC ?
3073                                 MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM :
3074                                 MIPI_DSI_DCS_SHORT_WRITE, data[0], 0);
3075         } else if (len == 2) {
3076                 r = dsi_vc_send_short(dsidev, channel,
3077                                 type == DSS_DSI_CONTENT_GENERIC ?
3078                                 MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM :
3079                                 MIPI_DSI_DCS_SHORT_WRITE_PARAM,
3080                                 data[0] | (data[1] << 8), 0);
3081         } else {
3082                 r = dsi_vc_send_long(dsidev, channel,
3083                                 type == DSS_DSI_CONTENT_GENERIC ?
3084                                 MIPI_DSI_GENERIC_LONG_WRITE :
3085                                 MIPI_DSI_DCS_LONG_WRITE, data, len, 0);
3086         }
3087
3088         return r;
3089 }
3090
3091 static int dsi_vc_dcs_write_nosync(struct omap_dss_device *dssdev, int channel,
3092                 u8 *data, int len)
3093 {
3094         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3095
3096         return dsi_vc_write_nosync_common(dsidev, channel, data, len,
3097                         DSS_DSI_CONTENT_DCS);
3098 }
3099
3100 static int dsi_vc_generic_write_nosync(struct omap_dss_device *dssdev, int channel,
3101                 u8 *data, int len)
3102 {
3103         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3104
3105         return dsi_vc_write_nosync_common(dsidev, channel, data, len,
3106                         DSS_DSI_CONTENT_GENERIC);
3107 }
3108
3109 static int dsi_vc_write_common(struct omap_dss_device *dssdev, int channel,
3110                 u8 *data, int len, enum dss_dsi_content_type type)
3111 {
3112         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3113         int r;
3114
3115         r = dsi_vc_write_nosync_common(dsidev, channel, data, len, type);
3116         if (r)
3117                 goto err;
3118
3119         r = dsi_vc_send_bta_sync(dssdev, channel);
3120         if (r)
3121                 goto err;
3122
3123         /* RX_FIFO_NOT_EMPTY */
3124         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20)) {
3125                 DSSERR("rx fifo not empty after write, dumping data:\n");
3126                 dsi_vc_flush_receive_data(dsidev, channel);
3127                 r = -EIO;
3128                 goto err;
3129         }
3130
3131         return 0;
3132 err:
3133         DSSERR("dsi_vc_write_common(ch %d, cmd 0x%02x, len %d) failed\n",
3134                         channel, data[0], len);
3135         return r;
3136 }
3137
3138 static int dsi_vc_dcs_write(struct omap_dss_device *dssdev, int channel, u8 *data,
3139                 int len)
3140 {
3141         return dsi_vc_write_common(dssdev, channel, data, len,
3142                         DSS_DSI_CONTENT_DCS);
3143 }
3144
3145 static int dsi_vc_generic_write(struct omap_dss_device *dssdev, int channel, u8 *data,
3146                 int len)
3147 {
3148         return dsi_vc_write_common(dssdev, channel, data, len,
3149                         DSS_DSI_CONTENT_GENERIC);
3150 }
3151
3152 static int dsi_vc_dcs_send_read_request(struct platform_device *dsidev,
3153                 int channel, u8 dcs_cmd)
3154 {
3155         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3156         int r;
3157
3158         if (dsi->debug_read)
3159                 DSSDBG("dsi_vc_dcs_send_read_request(ch%d, dcs_cmd %x)\n",
3160                         channel, dcs_cmd);
3161
3162         r = dsi_vc_send_short(dsidev, channel, MIPI_DSI_DCS_READ, dcs_cmd, 0);
3163         if (r) {
3164                 DSSERR("dsi_vc_dcs_send_read_request(ch %d, cmd 0x%02x)"
3165                         " failed\n", channel, dcs_cmd);
3166                 return r;
3167         }
3168
3169         return 0;
3170 }
3171
3172 static int dsi_vc_generic_send_read_request(struct platform_device *dsidev,
3173                 int channel, u8 *reqdata, int reqlen)
3174 {
3175         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3176         u16 data;
3177         u8 data_type;
3178         int r;
3179
3180         if (dsi->debug_read)
3181                 DSSDBG("dsi_vc_generic_send_read_request(ch %d, reqlen %d)\n",
3182                         channel, reqlen);
3183
3184         if (reqlen == 0) {
3185                 data_type = MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM;
3186                 data = 0;
3187         } else if (reqlen == 1) {
3188                 data_type = MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM;
3189                 data = reqdata[0];
3190         } else if (reqlen == 2) {
3191                 data_type = MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM;
3192                 data = reqdata[0] | (reqdata[1] << 8);
3193         } else {
3194                 BUG();
3195                 return -EINVAL;
3196         }
3197
3198         r = dsi_vc_send_short(dsidev, channel, data_type, data, 0);
3199         if (r) {
3200                 DSSERR("dsi_vc_generic_send_read_request(ch %d, reqlen %d)"
3201                         " failed\n", channel, reqlen);
3202                 return r;
3203         }
3204
3205         return 0;
3206 }
3207
3208 static int dsi_vc_read_rx_fifo(struct platform_device *dsidev, int channel,
3209                 u8 *buf, int buflen, enum dss_dsi_content_type type)
3210 {
3211         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3212         u32 val;
3213         u8 dt;
3214         int r;
3215
3216         /* RX_FIFO_NOT_EMPTY */
3217         if (REG_GET(dsidev, DSI_VC_CTRL(channel), 20, 20) == 0) {
3218                 DSSERR("RX fifo empty when trying to read.\n");
3219                 r = -EIO;
3220                 goto err;
3221         }
3222
3223         val = dsi_read_reg(dsidev, DSI_VC_SHORT_PACKET_HEADER(channel));
3224         if (dsi->debug_read)
3225                 DSSDBG("\theader: %08x\n", val);
3226         dt = FLD_GET(val, 5, 0);
3227         if (dt == MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT) {
3228                 u16 err = FLD_GET(val, 23, 8);
3229                 dsi_show_rx_ack_with_err(err);
3230                 r = -EIO;
3231                 goto err;
3232
3233         } else if (dt == (type == DSS_DSI_CONTENT_GENERIC ?
3234                         MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE :
3235                         MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE)) {
3236                 u8 data = FLD_GET(val, 15, 8);
3237                 if (dsi->debug_read)
3238                         DSSDBG("\t%s short response, 1 byte: %02x\n",
3239                                 type == DSS_DSI_CONTENT_GENERIC ? "GENERIC" :
3240                                 "DCS", data);
3241
3242                 if (buflen < 1) {
3243                         r = -EIO;
3244                         goto err;
3245                 }
3246
3247                 buf[0] = data;
3248
3249                 return 1;
3250         } else if (dt == (type == DSS_DSI_CONTENT_GENERIC ?
3251                         MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE :
3252                         MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE)) {
3253                 u16 data = FLD_GET(val, 23, 8);
3254                 if (dsi->debug_read)
3255                         DSSDBG("\t%s short response, 2 byte: %04x\n",
3256                                 type == DSS_DSI_CONTENT_GENERIC ? "GENERIC" :
3257                                 "DCS", data);
3258
3259                 if (buflen < 2) {
3260                         r = -EIO;
3261                         goto err;
3262                 }
3263
3264                 buf[0] = data & 0xff;
3265                 buf[1] = (data >> 8) & 0xff;
3266
3267                 return 2;
3268         } else if (dt == (type == DSS_DSI_CONTENT_GENERIC ?
3269                         MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE :
3270                         MIPI_DSI_RX_DCS_LONG_READ_RESPONSE)) {
3271                 int w;
3272                 int len = FLD_GET(val, 23, 8);
3273                 if (dsi->debug_read)
3274                         DSSDBG("\t%s long response, len %d\n",
3275                                 type == DSS_DSI_CONTENT_GENERIC ? "GENERIC" :
3276                                 "DCS", len);
3277
3278                 if (len > buflen) {
3279                         r = -EIO;
3280                         goto err;
3281                 }
3282
3283                 /* two byte checksum ends the packet, not included in len */
3284                 for (w = 0; w < len + 2;) {
3285                         int b;
3286                         val = dsi_read_reg(dsidev,
3287                                 DSI_VC_SHORT_PACKET_HEADER(channel));
3288                         if (dsi->debug_read)
3289                                 DSSDBG("\t\t%02x %02x %02x %02x\n",
3290                                                 (val >> 0) & 0xff,
3291                                                 (val >> 8) & 0xff,
3292                                                 (val >> 16) & 0xff,
3293                                                 (val >> 24) & 0xff);
3294
3295                         for (b = 0; b < 4; ++b) {
3296                                 if (w < len)
3297                                         buf[w] = (val >> (b * 8)) & 0xff;
3298                                 /* we discard the 2 byte checksum */
3299                                 ++w;
3300                         }
3301                 }
3302
3303                 return len;
3304         } else {
3305                 DSSERR("\tunknown datatype 0x%02x\n", dt);
3306                 r = -EIO;
3307                 goto err;
3308         }
3309
3310 err:
3311         DSSERR("dsi_vc_read_rx_fifo(ch %d type %s) failed\n", channel,
3312                 type == DSS_DSI_CONTENT_GENERIC ? "GENERIC" : "DCS");
3313
3314         return r;
3315 }
3316
3317 static int dsi_vc_dcs_read(struct omap_dss_device *dssdev, int channel, u8 dcs_cmd,
3318                 u8 *buf, int buflen)
3319 {
3320         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3321         int r;
3322
3323         r = dsi_vc_dcs_send_read_request(dsidev, channel, dcs_cmd);
3324         if (r)
3325                 goto err;
3326
3327         r = dsi_vc_send_bta_sync(dssdev, channel);
3328         if (r)
3329                 goto err;
3330
3331         r = dsi_vc_read_rx_fifo(dsidev, channel, buf, buflen,
3332                 DSS_DSI_CONTENT_DCS);
3333         if (r < 0)
3334                 goto err;
3335
3336         if (r != buflen) {
3337                 r = -EIO;
3338                 goto err;
3339         }
3340
3341         return 0;
3342 err:
3343         DSSERR("dsi_vc_dcs_read(ch %d, cmd 0x%02x) failed\n", channel, dcs_cmd);
3344         return r;
3345 }
3346
3347 static int dsi_vc_generic_read(struct omap_dss_device *dssdev, int channel,
3348                 u8 *reqdata, int reqlen, u8 *buf, int buflen)
3349 {
3350         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3351         int r;
3352
3353         r = dsi_vc_generic_send_read_request(dsidev, channel, reqdata, reqlen);
3354         if (r)
3355                 return r;
3356
3357         r = dsi_vc_send_bta_sync(dssdev, channel);
3358         if (r)
3359                 return r;
3360
3361         r = dsi_vc_read_rx_fifo(dsidev, channel, buf, buflen,
3362                 DSS_DSI_CONTENT_GENERIC);
3363         if (r < 0)
3364                 return r;
3365
3366         if (r != buflen) {
3367                 r = -EIO;
3368                 return r;
3369         }
3370
3371         return 0;
3372 }
3373
3374 static int dsi_vc_set_max_rx_packet_size(struct omap_dss_device *dssdev, int channel,
3375                 u16 len)
3376 {
3377         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
3378
3379         return dsi_vc_send_short(dsidev, channel,
3380                         MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE, len, 0);
3381 }
3382
3383 static int dsi_enter_ulps(struct platform_device *dsidev)
3384 {
3385         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3386         DECLARE_COMPLETION_ONSTACK(completion);
3387         int r, i;
3388         unsigned mask;
3389
3390         DSSDBG("Entering ULPS");
3391
3392         WARN_ON(!dsi_bus_is_locked(dsidev));
3393
3394         WARN_ON(dsi->ulps_enabled);
3395
3396         if (dsi->ulps_enabled)
3397                 return 0;
3398
3399         /* DDR_CLK_ALWAYS_ON */
3400         if (REG_GET(dsidev, DSI_CLK_CTRL, 13, 13)) {
3401                 dsi_if_enable(dsidev, 0);
3402                 REG_FLD_MOD(dsidev, DSI_CLK_CTRL, 0, 13, 13);
3403                 dsi_if_enable(dsidev, 1);
3404         }
3405
3406         dsi_sync_vc(dsidev, 0);
3407         dsi_sync_vc(dsidev, 1);
3408         dsi_sync_vc(dsidev, 2);
3409         dsi_sync_vc(dsidev, 3);
3410
3411         dsi_force_tx_stop_mode_io(dsidev);
3412
3413         dsi_vc_enable(dsidev, 0, false);
3414         dsi_vc_enable(dsidev, 1, false);
3415         dsi_vc_enable(dsidev, 2, false);
3416         dsi_vc_enable(dsidev, 3, false);
3417
3418         if (REG_GET(dsidev, DSI_COMPLEXIO_CFG2, 16, 16)) {      /* HS_BUSY */
3419                 DSSERR("HS busy when enabling ULPS\n");
3420                 return -EIO;
3421         }
3422
3423         if (REG_GET(dsidev, DSI_COMPLEXIO_CFG2, 17, 17)) {      /* LP_BUSY */
3424                 DSSERR("LP busy when enabling ULPS\n");
3425                 return -EIO;
3426         }
3427
3428         r = dsi_register_isr_cio(dsidev, dsi_completion_handler, &completion,
3429                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3430         if (r)
3431                 return r;
3432
3433         mask = 0;
3434
3435         for (i = 0; i < dsi->num_lanes_supported; ++i) {
3436                 if (dsi->lanes[i].function == DSI_LANE_UNUSED)
3437                         continue;
3438                 mask |= 1 << i;
3439         }
3440         /* Assert TxRequestEsc for data lanes and TxUlpsClk for clk lane */
3441         /* LANEx_ULPS_SIG2 */
3442         REG_FLD_MOD(dsidev, DSI_COMPLEXIO_CFG2, mask, 9, 5);
3443
3444         /* flush posted write and wait for SCP interface to finish the write */
3445         dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG2);
3446
3447         if (wait_for_completion_timeout(&completion,
3448                                 msecs_to_jiffies(1000)) == 0) {
3449                 DSSERR("ULPS enable timeout\n");
3450                 r = -EIO;
3451                 goto err;
3452         }
3453
3454         dsi_unregister_isr_cio(dsidev, dsi_completion_handler, &completion,
3455                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3456
3457         /* Reset LANEx_ULPS_SIG2 */
3458         REG_FLD_MOD(dsidev, DSI_COMPLEXIO_CFG2, 0, 9, 5);
3459
3460         /* flush posted write and wait for SCP interface to finish the write */
3461         dsi_read_reg(dsidev, DSI_COMPLEXIO_CFG2);
3462
3463         dsi_cio_power(dsidev, DSI_COMPLEXIO_POWER_ULPS);
3464
3465         dsi_if_enable(dsidev, false);
3466
3467         dsi->ulps_enabled = true;
3468
3469         return 0;
3470
3471 err:
3472         dsi_unregister_isr_cio(dsidev, dsi_completion_handler, &completion,
3473                         DSI_CIO_IRQ_ULPSACTIVENOT_ALL0);
3474         return r;
3475 }
3476
3477 static void dsi_set_lp_rx_timeout(struct platform_device *dsidev,
3478                 unsigned ticks, bool x4, bool x16)
3479 {
3480         unsigned long fck;
3481         unsigned long total_ticks;
3482         u32 r;
3483
3484         BUG_ON(ticks > 0x1fff);
3485
3486         /* ticks in DSI_FCK */
3487         fck = dsi_fclk_rate(dsidev);
3488
3489         r = dsi_read_reg(dsidev, DSI_TIMING2);
3490         r = FLD_MOD(r, 1, 15, 15);      /* LP_RX_TO */
3491         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* LP_RX_TO_X16 */
3492         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* LP_RX_TO_X4 */
3493         r = FLD_MOD(r, ticks, 12, 0);   /* LP_RX_COUNTER */
3494         dsi_write_reg(dsidev, DSI_TIMING2, r);
3495
3496         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3497
3498         DSSDBG("LP_RX_TO %lu ticks (%#x%s%s) = %lu ns\n",
3499                         total_ticks,
3500                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3501                         (total_ticks * 1000) / (fck / 1000 / 1000));
3502 }
3503
3504 static void dsi_set_ta_timeout(struct platform_device *dsidev, unsigned ticks,
3505                 bool x8, bool x16)
3506 {
3507         unsigned long fck;
3508         unsigned long total_ticks;
3509         u32 r;
3510
3511         BUG_ON(ticks > 0x1fff);
3512
3513         /* ticks in DSI_FCK */
3514         fck = dsi_fclk_rate(dsidev);
3515
3516         r = dsi_read_reg(dsidev, DSI_TIMING1);
3517         r = FLD_MOD(r, 1, 31, 31);      /* TA_TO */
3518         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* TA_TO_X16 */
3519         r = FLD_MOD(r, x8 ? 1 : 0, 29, 29);     /* TA_TO_X8 */
3520         r = FLD_MOD(r, ticks, 28, 16);  /* TA_TO_COUNTER */
3521         dsi_write_reg(dsidev, DSI_TIMING1, r);
3522
3523         total_ticks = ticks * (x16 ? 16 : 1) * (x8 ? 8 : 1);
3524
3525         DSSDBG("TA_TO %lu ticks (%#x%s%s) = %lu ns\n",
3526                         total_ticks,
3527                         ticks, x8 ? " x8" : "", x16 ? " x16" : "",
3528                         (total_ticks * 1000) / (fck / 1000 / 1000));
3529 }
3530
3531 static void dsi_set_stop_state_counter(struct platform_device *dsidev,
3532                 unsigned ticks, bool x4, bool x16)
3533 {
3534         unsigned long fck;
3535         unsigned long total_ticks;
3536         u32 r;
3537
3538         BUG_ON(ticks > 0x1fff);
3539
3540         /* ticks in DSI_FCK */
3541         fck = dsi_fclk_rate(dsidev);
3542
3543         r = dsi_read_reg(dsidev, DSI_TIMING1);
3544         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
3545         r = FLD_MOD(r, x16 ? 1 : 0, 14, 14);    /* STOP_STATE_X16_IO */
3546         r = FLD_MOD(r, x4 ? 1 : 0, 13, 13);     /* STOP_STATE_X4_IO */
3547         r = FLD_MOD(r, ticks, 12, 0);   /* STOP_STATE_COUNTER_IO */
3548         dsi_write_reg(dsidev, DSI_TIMING1, r);
3549
3550         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3551
3552         DSSDBG("STOP_STATE_COUNTER %lu ticks (%#x%s%s) = %lu ns\n",
3553                         total_ticks,
3554                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3555                         (total_ticks * 1000) / (fck / 1000 / 1000));
3556 }
3557
3558 static void dsi_set_hs_tx_timeout(struct platform_device *dsidev,
3559                 unsigned ticks, bool x4, bool x16)
3560 {
3561         unsigned long fck;
3562         unsigned long total_ticks;
3563         u32 r;
3564
3565         BUG_ON(ticks > 0x1fff);
3566
3567         /* ticks in TxByteClkHS */
3568         fck = dsi_get_txbyteclkhs(dsidev);
3569
3570         r = dsi_read_reg(dsidev, DSI_TIMING2);
3571         r = FLD_MOD(r, 1, 31, 31);      /* HS_TX_TO */
3572         r = FLD_MOD(r, x16 ? 1 : 0, 30, 30);    /* HS_TX_TO_X16 */
3573         r = FLD_MOD(r, x4 ? 1 : 0, 29, 29);     /* HS_TX_TO_X8 (4 really) */
3574         r = FLD_MOD(r, ticks, 28, 16);  /* HS_TX_TO_COUNTER */
3575         dsi_write_reg(dsidev, DSI_TIMING2, r);
3576
3577         total_ticks = ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1);
3578
3579         DSSDBG("HS_TX_TO %lu ticks (%#x%s%s) = %lu ns\n",
3580                         total_ticks,
3581                         ticks, x4 ? " x4" : "", x16 ? " x16" : "",
3582                         (total_ticks * 1000) / (fck / 1000 / 1000));
3583 }
3584
3585 static void dsi_config_vp_num_line_buffers(struct platform_device *dsidev)
3586 {
3587         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3588         int num_line_buffers;
3589
3590         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
3591                 int bpp = dsi_get_pixel_size(dsi->pix_fmt);
3592                 struct omap_video_timings *timings = &dsi->timings;
3593                 /*
3594                  * Don't use line buffers if width is greater than the video
3595                  * port's line buffer size
3596                  */
3597                 if (dsi->line_buffer_size <= timings->x_res * bpp / 8)
3598                         num_line_buffers = 0;
3599                 else
3600                         num_line_buffers = 2;
3601         } else {
3602                 /* Use maximum number of line buffers in command mode */
3603                 num_line_buffers = 2;
3604         }
3605
3606         /* LINE_BUFFER */
3607         REG_FLD_MOD(dsidev, DSI_CTRL, num_line_buffers, 13, 12);
3608 }
3609
3610 static void dsi_config_vp_sync_events(struct platform_device *dsidev)
3611 {
3612         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3613         bool sync_end;
3614         u32 r;
3615
3616         if (dsi->vm_timings.trans_mode == OMAP_DSS_DSI_PULSE_MODE)
3617                 sync_end = true;
3618         else
3619                 sync_end = false;
3620
3621         r = dsi_read_reg(dsidev, DSI_CTRL);
3622         r = FLD_MOD(r, 1, 9, 9);                /* VP_DE_POL */
3623         r = FLD_MOD(r, 1, 10, 10);              /* VP_HSYNC_POL */
3624         r = FLD_MOD(r, 1, 11, 11);              /* VP_VSYNC_POL */
3625         r = FLD_MOD(r, 1, 15, 15);              /* VP_VSYNC_START */
3626         r = FLD_MOD(r, sync_end, 16, 16);       /* VP_VSYNC_END */
3627         r = FLD_MOD(r, 1, 17, 17);              /* VP_HSYNC_START */
3628         r = FLD_MOD(r, sync_end, 18, 18);       /* VP_HSYNC_END */
3629         dsi_write_reg(dsidev, DSI_CTRL, r);
3630 }
3631
3632 static void dsi_config_blanking_modes(struct platform_device *dsidev)
3633 {
3634         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3635         int blanking_mode = dsi->vm_timings.blanking_mode;
3636         int hfp_blanking_mode = dsi->vm_timings.hfp_blanking_mode;
3637         int hbp_blanking_mode = dsi->vm_timings.hbp_blanking_mode;
3638         int hsa_blanking_mode = dsi->vm_timings.hsa_blanking_mode;
3639         u32 r;
3640
3641         /*
3642          * 0 = TX FIFO packets sent or LPS in corresponding blanking periods
3643          * 1 = Long blanking packets are sent in corresponding blanking periods
3644          */
3645         r = dsi_read_reg(dsidev, DSI_CTRL);
3646         r = FLD_MOD(r, blanking_mode, 20, 20);          /* BLANKING_MODE */
3647         r = FLD_MOD(r, hfp_blanking_mode, 21, 21);      /* HFP_BLANKING */
3648         r = FLD_MOD(r, hbp_blanking_mode, 22, 22);      /* HBP_BLANKING */
3649         r = FLD_MOD(r, hsa_blanking_mode, 23, 23);      /* HSA_BLANKING */
3650         dsi_write_reg(dsidev, DSI_CTRL, r);
3651 }
3652
3653 /*
3654  * According to section 'HS Command Mode Interleaving' in OMAP TRM, Scenario 3
3655  * results in maximum transition time for data and clock lanes to enter and
3656  * exit HS mode. Hence, this is the scenario where the least amount of command
3657  * mode data can be interleaved. We program the minimum amount of TXBYTECLKHS
3658  * clock cycles that can be used to interleave command mode data in HS so that
3659  * all scenarios are satisfied.
3660  */
3661 static int dsi_compute_interleave_hs(int blank, bool ddr_alwon, int enter_hs,
3662                 int exit_hs, int exiths_clk, int ddr_pre, int ddr_post)
3663 {
3664         int transition;
3665
3666         /*
3667          * If DDR_CLK_ALWAYS_ON is set, we need to consider HS mode transition
3668          * time of data lanes only, if it isn't set, we need to consider HS
3669          * transition time of both data and clock lanes. HS transition time
3670          * of Scenario 3 is considered.
3671          */
3672         if (ddr_alwon) {
3673                 transition = enter_hs + exit_hs + max(enter_hs, 2) + 1;
3674         } else {
3675                 int trans1, trans2;
3676                 trans1 = ddr_pre + enter_hs + exit_hs + max(enter_hs, 2) + 1;
3677                 trans2 = ddr_pre + enter_hs + exiths_clk + ddr_post + ddr_pre +
3678                                 enter_hs + 1;
3679                 transition = max(trans1, trans2);
3680         }
3681
3682         return blank > transition ? blank - transition : 0;
3683 }
3684
3685 /*
3686  * According to section 'LP Command Mode Interleaving' in OMAP TRM, Scenario 1
3687  * results in maximum transition time for data lanes to enter and exit LP mode.
3688  * Hence, this is the scenario where the least amount of command mode data can
3689  * be interleaved. We program the minimum amount of bytes that can be
3690  * interleaved in LP so that all scenarios are satisfied.
3691  */
3692 static int dsi_compute_interleave_lp(int blank, int enter_hs, int exit_hs,
3693                 int lp_clk_div, int tdsi_fclk)
3694 {
3695         int trans_lp;   /* time required for a LP transition, in TXBYTECLKHS */
3696         int tlp_avail;  /* time left for interleaving commands, in CLKIN4DDR */
3697         int ttxclkesc;  /* period of LP transmit escape clock, in CLKIN4DDR */
3698         int thsbyte_clk = 16;   /* Period of TXBYTECLKHS clock, in CLKIN4DDR */
3699         int lp_inter;   /* cmd mode data that can be interleaved, in bytes */
3700
3701         /* maximum LP transition time according to Scenario 1 */
3702         trans_lp = exit_hs + max(enter_hs, 2) + 1;
3703
3704         /* CLKIN4DDR = 16 * TXBYTECLKHS */
3705         tlp_avail = thsbyte_clk * (blank - trans_lp);
3706
3707         ttxclkesc = tdsi_fclk * lp_clk_div;
3708
3709         lp_inter = ((tlp_avail - 8 * thsbyte_clk - 5 * tdsi_fclk) / ttxclkesc -
3710                         26) / 16;
3711
3712         return max(lp_inter, 0);
3713 }
3714
3715 static void dsi_config_cmd_mode_interleaving(struct platform_device *dsidev)
3716 {
3717         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3718         int blanking_mode;
3719         int hfp_blanking_mode, hbp_blanking_mode, hsa_blanking_mode;
3720         int hsa, hfp, hbp, width_bytes, bllp, lp_clk_div;
3721         int ddr_clk_pre, ddr_clk_post, enter_hs_mode_lat, exit_hs_mode_lat;
3722         int tclk_trail, ths_exit, exiths_clk;
3723         bool ddr_alwon;
3724         struct omap_video_timings *timings = &dsi->timings;
3725         int bpp = dsi_get_pixel_size(dsi->pix_fmt);
3726         int ndl = dsi->num_lanes_used - 1;
3727         int dsi_fclk_hsdiv = dsi->user_dsi_cinfo.regm_dsi + 1;
3728         int hsa_interleave_hs = 0, hsa_interleave_lp = 0;
3729         int hfp_interleave_hs = 0, hfp_interleave_lp = 0;
3730         int hbp_interleave_hs = 0, hbp_interleave_lp = 0;
3731         int bl_interleave_hs = 0, bl_interleave_lp = 0;
3732         u32 r;
3733
3734         r = dsi_read_reg(dsidev, DSI_CTRL);
3735         blanking_mode = FLD_GET(r, 20, 20);
3736         hfp_blanking_mode = FLD_GET(r, 21, 21);
3737         hbp_blanking_mode = FLD_GET(r, 22, 22);
3738         hsa_blanking_mode = FLD_GET(r, 23, 23);
3739
3740         r = dsi_read_reg(dsidev, DSI_VM_TIMING1);
3741         hbp = FLD_GET(r, 11, 0);
3742         hfp = FLD_GET(r, 23, 12);
3743         hsa = FLD_GET(r, 31, 24);
3744
3745         r = dsi_read_reg(dsidev, DSI_CLK_TIMING);
3746         ddr_clk_post = FLD_GET(r, 7, 0);
3747         ddr_clk_pre = FLD_GET(r, 15, 8);
3748
3749         r = dsi_read_reg(dsidev, DSI_VM_TIMING7);
3750         exit_hs_mode_lat = FLD_GET(r, 15, 0);
3751         enter_hs_mode_lat = FLD_GET(r, 31, 16);
3752
3753         r = dsi_read_reg(dsidev, DSI_CLK_CTRL);
3754         lp_clk_div = FLD_GET(r, 12, 0);
3755         ddr_alwon = FLD_GET(r, 13, 13);
3756
3757         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG0);
3758         ths_exit = FLD_GET(r, 7, 0);
3759
3760         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG1);
3761         tclk_trail = FLD_GET(r, 15, 8);
3762
3763         exiths_clk = ths_exit + tclk_trail;
3764
3765         width_bytes = DIV_ROUND_UP(timings->x_res * bpp, 8);
3766         bllp = hbp + hfp + hsa + DIV_ROUND_UP(width_bytes + 6, ndl);
3767
3768         if (!hsa_blanking_mode) {
3769                 hsa_interleave_hs = dsi_compute_interleave_hs(hsa, ddr_alwon,
3770                                         enter_hs_mode_lat, exit_hs_mode_lat,
3771                                         exiths_clk, ddr_clk_pre, ddr_clk_post);
3772                 hsa_interleave_lp = dsi_compute_interleave_lp(hsa,
3773                                         enter_hs_mode_lat, exit_hs_mode_lat,
3774                                         lp_clk_div, dsi_fclk_hsdiv);
3775         }
3776
3777         if (!hfp_blanking_mode) {
3778                 hfp_interleave_hs = dsi_compute_interleave_hs(hfp, ddr_alwon,
3779                                         enter_hs_mode_lat, exit_hs_mode_lat,
3780                                         exiths_clk, ddr_clk_pre, ddr_clk_post);
3781                 hfp_interleave_lp = dsi_compute_interleave_lp(hfp,
3782                                         enter_hs_mode_lat, exit_hs_mode_lat,
3783                                         lp_clk_div, dsi_fclk_hsdiv);
3784         }
3785
3786         if (!hbp_blanking_mode) {
3787                 hbp_interleave_hs = dsi_compute_interleave_hs(hbp, ddr_alwon,
3788                                         enter_hs_mode_lat, exit_hs_mode_lat,
3789                                         exiths_clk, ddr_clk_pre, ddr_clk_post);
3790
3791                 hbp_interleave_lp = dsi_compute_interleave_lp(hbp,
3792                                         enter_hs_mode_lat, exit_hs_mode_lat,
3793                                         lp_clk_div, dsi_fclk_hsdiv);
3794         }
3795
3796         if (!blanking_mode) {
3797                 bl_interleave_hs = dsi_compute_interleave_hs(bllp, ddr_alwon,
3798                                         enter_hs_mode_lat, exit_hs_mode_lat,
3799                                         exiths_clk, ddr_clk_pre, ddr_clk_post);
3800
3801                 bl_interleave_lp = dsi_compute_interleave_lp(bllp,
3802                                         enter_hs_mode_lat, exit_hs_mode_lat,
3803                                         lp_clk_div, dsi_fclk_hsdiv);
3804         }
3805
3806         DSSDBG("DSI HS interleaving(TXBYTECLKHS) HSA %d, HFP %d, HBP %d, BLLP %d\n",
3807                 hsa_interleave_hs, hfp_interleave_hs, hbp_interleave_hs,
3808                 bl_interleave_hs);
3809
3810         DSSDBG("DSI LP interleaving(bytes) HSA %d, HFP %d, HBP %d, BLLP %d\n",
3811                 hsa_interleave_lp, hfp_interleave_lp, hbp_interleave_lp,
3812                 bl_interleave_lp);
3813
3814         r = dsi_read_reg(dsidev, DSI_VM_TIMING4);
3815         r = FLD_MOD(r, hsa_interleave_hs, 23, 16);
3816         r = FLD_MOD(r, hfp_interleave_hs, 15, 8);
3817         r = FLD_MOD(r, hbp_interleave_hs, 7, 0);
3818         dsi_write_reg(dsidev, DSI_VM_TIMING4, r);
3819
3820         r = dsi_read_reg(dsidev, DSI_VM_TIMING5);
3821         r = FLD_MOD(r, hsa_interleave_lp, 23, 16);
3822         r = FLD_MOD(r, hfp_interleave_lp, 15, 8);
3823         r = FLD_MOD(r, hbp_interleave_lp, 7, 0);
3824         dsi_write_reg(dsidev, DSI_VM_TIMING5, r);
3825
3826         r = dsi_read_reg(dsidev, DSI_VM_TIMING6);
3827         r = FLD_MOD(r, bl_interleave_hs, 31, 15);
3828         r = FLD_MOD(r, bl_interleave_lp, 16, 0);
3829         dsi_write_reg(dsidev, DSI_VM_TIMING6, r);
3830 }
3831
3832 static int dsi_proto_config(struct platform_device *dsidev)
3833 {
3834         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3835         u32 r;
3836         int buswidth = 0;
3837
3838         dsi_config_tx_fifo(dsidev, DSI_FIFO_SIZE_32,
3839                         DSI_FIFO_SIZE_32,
3840                         DSI_FIFO_SIZE_32,
3841                         DSI_FIFO_SIZE_32);
3842
3843         dsi_config_rx_fifo(dsidev, DSI_FIFO_SIZE_32,
3844                         DSI_FIFO_SIZE_32,
3845                         DSI_FIFO_SIZE_32,
3846                         DSI_FIFO_SIZE_32);
3847
3848         /* XXX what values for the timeouts? */
3849         dsi_set_stop_state_counter(dsidev, 0x1000, false, false);
3850         dsi_set_ta_timeout(dsidev, 0x1fff, true, true);
3851         dsi_set_lp_rx_timeout(dsidev, 0x1fff, true, true);
3852         dsi_set_hs_tx_timeout(dsidev, 0x1fff, true, true);
3853
3854         switch (dsi_get_pixel_size(dsi->pix_fmt)) {
3855         case 16:
3856                 buswidth = 0;
3857                 break;
3858         case 18:
3859                 buswidth = 1;
3860                 break;
3861         case 24:
3862                 buswidth = 2;
3863                 break;
3864         default:
3865                 BUG();
3866                 return -EINVAL;
3867         }
3868
3869         r = dsi_read_reg(dsidev, DSI_CTRL);
3870         r = FLD_MOD(r, 1, 1, 1);        /* CS_RX_EN */
3871         r = FLD_MOD(r, 1, 2, 2);        /* ECC_RX_EN */
3872         r = FLD_MOD(r, 1, 3, 3);        /* TX_FIFO_ARBITRATION */
3873         r = FLD_MOD(r, 1, 4, 4);        /* VP_CLK_RATIO, always 1, see errata*/
3874         r = FLD_MOD(r, buswidth, 7, 6); /* VP_DATA_BUS_WIDTH */
3875         r = FLD_MOD(r, 0, 8, 8);        /* VP_CLK_POL */
3876         r = FLD_MOD(r, 1, 14, 14);      /* TRIGGER_RESET_MODE */
3877         r = FLD_MOD(r, 1, 19, 19);      /* EOT_ENABLE */
3878         if (!dss_has_feature(FEAT_DSI_DCS_CMD_CONFIG_VC)) {
3879                 r = FLD_MOD(r, 1, 24, 24);      /* DCS_CMD_ENABLE */
3880                 /* DCS_CMD_CODE, 1=start, 0=continue */
3881                 r = FLD_MOD(r, 0, 25, 25);
3882         }
3883
3884         dsi_write_reg(dsidev, DSI_CTRL, r);
3885
3886         dsi_config_vp_num_line_buffers(dsidev);
3887
3888         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
3889                 dsi_config_vp_sync_events(dsidev);
3890                 dsi_config_blanking_modes(dsidev);
3891                 dsi_config_cmd_mode_interleaving(dsidev);
3892         }
3893
3894         dsi_vc_initial_config(dsidev, 0);
3895         dsi_vc_initial_config(dsidev, 1);
3896         dsi_vc_initial_config(dsidev, 2);
3897         dsi_vc_initial_config(dsidev, 3);
3898
3899         return 0;
3900 }
3901
3902 static void dsi_proto_timings(struct platform_device *dsidev)
3903 {
3904         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
3905         unsigned tlpx, tclk_zero, tclk_prepare, tclk_trail;
3906         unsigned tclk_pre, tclk_post;
3907         unsigned ths_prepare, ths_prepare_ths_zero, ths_zero;
3908         unsigned ths_trail, ths_exit;
3909         unsigned ddr_clk_pre, ddr_clk_post;
3910         unsigned enter_hs_mode_lat, exit_hs_mode_lat;
3911         unsigned ths_eot;
3912         int ndl = dsi->num_lanes_used - 1;
3913         u32 r;
3914
3915         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG0);
3916         ths_prepare = FLD_GET(r, 31, 24);
3917         ths_prepare_ths_zero = FLD_GET(r, 23, 16);
3918         ths_zero = ths_prepare_ths_zero - ths_prepare;
3919         ths_trail = FLD_GET(r, 15, 8);
3920         ths_exit = FLD_GET(r, 7, 0);
3921
3922         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG1);
3923         tlpx = FLD_GET(r, 20, 16) * 2;
3924         tclk_trail = FLD_GET(r, 15, 8);
3925         tclk_zero = FLD_GET(r, 7, 0);
3926
3927         r = dsi_read_reg(dsidev, DSI_DSIPHY_CFG2);
3928         tclk_prepare = FLD_GET(r, 7, 0);
3929
3930         /* min 8*UI */
3931         tclk_pre = 20;
3932         /* min 60ns + 52*UI */
3933         tclk_post = ns2ddr(dsidev, 60) + 26;
3934
3935         ths_eot = DIV_ROUND_UP(4, ndl);
3936
3937         ddr_clk_pre = DIV_ROUND_UP(tclk_pre + tlpx + tclk_zero + tclk_prepare,
3938                         4);
3939         ddr_clk_post = DIV_ROUND_UP(tclk_post + ths_trail, 4) + ths_eot;
3940
3941         BUG_ON(ddr_clk_pre == 0 || ddr_clk_pre > 255);
3942         BUG_ON(ddr_clk_post == 0 || ddr_clk_post > 255);
3943
3944         r = dsi_read_reg(dsidev, DSI_CLK_TIMING);
3945         r = FLD_MOD(r, ddr_clk_pre, 15, 8);
3946         r = FLD_MOD(r, ddr_clk_post, 7, 0);
3947         dsi_write_reg(dsidev, DSI_CLK_TIMING, r);
3948
3949         DSSDBG("ddr_clk_pre %u, ddr_clk_post %u\n",
3950                         ddr_clk_pre,
3951                         ddr_clk_post);
3952
3953         enter_hs_mode_lat = 1 + DIV_ROUND_UP(tlpx, 4) +
3954                 DIV_ROUND_UP(ths_prepare, 4) +
3955                 DIV_ROUND_UP(ths_zero + 3, 4);
3956
3957         exit_hs_mode_lat = DIV_ROUND_UP(ths_trail + ths_exit, 4) + 1 + ths_eot;
3958
3959         r = FLD_VAL(enter_hs_mode_lat, 31, 16) |
3960                 FLD_VAL(exit_hs_mode_lat, 15, 0);
3961         dsi_write_reg(dsidev, DSI_VM_TIMING7, r);
3962
3963         DSSDBG("enter_hs_mode_lat %u, exit_hs_mode_lat %u\n",
3964                         enter_hs_mode_lat, exit_hs_mode_lat);
3965
3966          if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
3967                 /* TODO: Implement a video mode check_timings function */
3968                 int hsa = dsi->vm_timings.hsa;
3969                 int hfp = dsi->vm_timings.hfp;
3970                 int hbp = dsi->vm_timings.hbp;
3971                 int vsa = dsi->vm_timings.vsa;
3972                 int vfp = dsi->vm_timings.vfp;
3973                 int vbp = dsi->vm_timings.vbp;
3974                 int window_sync = dsi->vm_timings.window_sync;
3975                 bool hsync_end;
3976                 struct omap_video_timings *timings = &dsi->timings;
3977                 int bpp = dsi_get_pixel_size(dsi->pix_fmt);
3978                 int tl, t_he, width_bytes;
3979
3980                 hsync_end = dsi->vm_timings.trans_mode == OMAP_DSS_DSI_PULSE_MODE;
3981                 t_he = hsync_end ?
3982                         ((hsa == 0 && ndl == 3) ? 1 : DIV_ROUND_UP(4, ndl)) : 0;
3983
3984                 width_bytes = DIV_ROUND_UP(timings->x_res * bpp, 8);
3985
3986                 /* TL = t_HS + HSA + t_HE + HFP + ceil((WC + 6) / NDL) + HBP */
3987                 tl = DIV_ROUND_UP(4, ndl) + (hsync_end ? hsa : 0) + t_he + hfp +
3988                         DIV_ROUND_UP(width_bytes + 6, ndl) + hbp;
3989
3990                 DSSDBG("HBP: %d, HFP: %d, HSA: %d, TL: %d TXBYTECLKHS\n", hbp,
3991                         hfp, hsync_end ? hsa : 0, tl);
3992                 DSSDBG("VBP: %d, VFP: %d, VSA: %d, VACT: %d lines\n", vbp, vfp,
3993                         vsa, timings->y_res);
3994
3995                 r = dsi_read_reg(dsidev, DSI_VM_TIMING1);
3996                 r = FLD_MOD(r, hbp, 11, 0);     /* HBP */
3997                 r = FLD_MOD(r, hfp, 23, 12);    /* HFP */
3998                 r = FLD_MOD(r, hsync_end ? hsa : 0, 31, 24);    /* HSA */
3999                 dsi_write_reg(dsidev, DSI_VM_TIMING1, r);
4000
4001                 r = dsi_read_reg(dsidev, DSI_VM_TIMING2);
4002                 r = FLD_MOD(r, vbp, 7, 0);      /* VBP */
4003                 r = FLD_MOD(r, vfp, 15, 8);     /* VFP */
4004                 r = FLD_MOD(r, vsa, 23, 16);    /* VSA */
4005                 r = FLD_MOD(r, window_sync, 27, 24);    /* WINDOW_SYNC */
4006                 dsi_write_reg(dsidev, DSI_VM_TIMING2, r);
4007
4008                 r = dsi_read_reg(dsidev, DSI_VM_TIMING3);
4009                 r = FLD_MOD(r, timings->y_res, 14, 0);  /* VACT */
4010                 r = FLD_MOD(r, tl, 31, 16);             /* TL */
4011                 dsi_write_reg(dsidev, DSI_VM_TIMING3, r);
4012         }
4013 }
4014
4015 static int dsi_configure_pins(struct omap_dss_device *dssdev,
4016                 const struct omap_dsi_pin_config *pin_cfg)
4017 {
4018         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4019         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4020         int num_pins;
4021         const int *pins;
4022         struct dsi_lane_config lanes[DSI_MAX_NR_LANES];
4023         int num_lanes;
4024         int i;
4025
4026         static const enum dsi_lane_function functions[] = {
4027                 DSI_LANE_CLK,
4028                 DSI_LANE_DATA1,
4029                 DSI_LANE_DATA2,
4030                 DSI_LANE_DATA3,
4031                 DSI_LANE_DATA4,
4032         };
4033
4034         num_pins = pin_cfg->num_pins;
4035         pins = pin_cfg->pins;
4036
4037         if (num_pins < 4 || num_pins > dsi->num_lanes_supported * 2
4038                         || num_pins % 2 != 0)
4039                 return -EINVAL;
4040
4041         for (i = 0; i < DSI_MAX_NR_LANES; ++i)
4042                 lanes[i].function = DSI_LANE_UNUSED;
4043
4044         num_lanes = 0;
4045
4046         for (i = 0; i < num_pins; i += 2) {
4047                 u8 lane, pol;
4048                 int dx, dy;
4049
4050                 dx = pins[i];
4051                 dy = pins[i + 1];
4052
4053                 if (dx < 0 || dx >= dsi->num_lanes_supported * 2)
4054                         return -EINVAL;
4055
4056                 if (dy < 0 || dy >= dsi->num_lanes_supported * 2)
4057                         return -EINVAL;
4058
4059                 if (dx & 1) {
4060                         if (dy != dx - 1)
4061                                 return -EINVAL;
4062                         pol = 1;
4063                 } else {
4064                         if (dy != dx + 1)
4065                                 return -EINVAL;
4066                         pol = 0;
4067                 }
4068
4069                 lane = dx / 2;
4070
4071                 lanes[lane].function = functions[i / 2];
4072                 lanes[lane].polarity = pol;
4073                 num_lanes++;
4074         }
4075
4076         memcpy(dsi->lanes, lanes, sizeof(dsi->lanes));
4077         dsi->num_lanes_used = num_lanes;
4078
4079         return 0;
4080 }
4081
4082 static int dsi_enable_video_output(struct omap_dss_device *dssdev, int channel)
4083 {
4084         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4085         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4086         struct omap_overlay_manager *mgr = dsi->output.manager;
4087         int bpp = dsi_get_pixel_size(dsi->pix_fmt);
4088         struct omap_dss_device *out = &dsi->output;
4089         u8 data_type;
4090         u16 word_count;
4091         int r;
4092
4093         if (out == NULL || out->manager == NULL) {
4094                 DSSERR("failed to enable display: no output/manager\n");
4095                 return -ENODEV;
4096         }
4097
4098         r = dsi_display_init_dispc(dsidev, mgr);
4099         if (r)
4100                 goto err_init_dispc;
4101
4102         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
4103                 switch (dsi->pix_fmt) {
4104                 case OMAP_DSS_DSI_FMT_RGB888:
4105                         data_type = MIPI_DSI_PACKED_PIXEL_STREAM_24;
4106                         break;
4107                 case OMAP_DSS_DSI_FMT_RGB666:
4108                         data_type = MIPI_DSI_PIXEL_STREAM_3BYTE_18;
4109                         break;
4110                 case OMAP_DSS_DSI_FMT_RGB666_PACKED:
4111                         data_type = MIPI_DSI_PACKED_PIXEL_STREAM_18;
4112                         break;
4113                 case OMAP_DSS_DSI_FMT_RGB565:
4114                         data_type = MIPI_DSI_PACKED_PIXEL_STREAM_16;
4115                         break;
4116                 default:
4117                         r = -EINVAL;
4118                         goto err_pix_fmt;
4119                 }
4120
4121                 dsi_if_enable(dsidev, false);
4122                 dsi_vc_enable(dsidev, channel, false);
4123
4124                 /* MODE, 1 = video mode */
4125                 REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 1, 4, 4);
4126
4127                 word_count = DIV_ROUND_UP(dsi->timings.x_res * bpp, 8);
4128
4129                 dsi_vc_write_long_header(dsidev, channel, data_type,
4130                                 word_count, 0);
4131
4132                 dsi_vc_enable(dsidev, channel, true);
4133                 dsi_if_enable(dsidev, true);
4134         }
4135
4136         r = dss_mgr_enable(mgr);
4137         if (r)
4138                 goto err_mgr_enable;
4139
4140         return 0;
4141
4142 err_mgr_enable:
4143         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
4144                 dsi_if_enable(dsidev, false);
4145                 dsi_vc_enable(dsidev, channel, false);
4146         }
4147 err_pix_fmt:
4148         dsi_display_uninit_dispc(dsidev, mgr);
4149 err_init_dispc:
4150         return r;
4151 }
4152
4153 static void dsi_disable_video_output(struct omap_dss_device *dssdev, int channel)
4154 {
4155         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4156         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4157         struct omap_overlay_manager *mgr = dsi->output.manager;
4158
4159         if (dsi->mode == OMAP_DSS_DSI_VIDEO_MODE) {
4160                 dsi_if_enable(dsidev, false);
4161                 dsi_vc_enable(dsidev, channel, false);
4162
4163                 /* MODE, 0 = command mode */
4164                 REG_FLD_MOD(dsidev, DSI_VC_CTRL(channel), 0, 4, 4);
4165
4166                 dsi_vc_enable(dsidev, channel, true);
4167                 dsi_if_enable(dsidev, true);
4168         }
4169
4170         dss_mgr_disable(mgr);
4171
4172         dsi_display_uninit_dispc(dsidev, mgr);
4173 }
4174
4175 static void dsi_update_screen_dispc(struct platform_device *dsidev)
4176 {
4177         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4178         struct omap_overlay_manager *mgr = dsi->output.manager;
4179         unsigned bytespp;
4180         unsigned bytespl;
4181         unsigned bytespf;
4182         unsigned total_len;
4183         unsigned packet_payload;
4184         unsigned packet_len;
4185         u32 l;
4186         int r;
4187         const unsigned channel = dsi->update_channel;
4188         const unsigned line_buf_size = dsi->line_buffer_size;
4189         u16 w = dsi->timings.x_res;
4190         u16 h = dsi->timings.y_res;
4191
4192         DSSDBG("dsi_update_screen_dispc(%dx%d)\n", w, h);
4193
4194         dsi_vc_config_source(dsidev, channel, DSI_VC_SOURCE_VP);
4195
4196         bytespp = dsi_get_pixel_size(dsi->pix_fmt) / 8;
4197         bytespl = w * bytespp;
4198         bytespf = bytespl * h;
4199
4200         /* NOTE: packet_payload has to be equal to N * bytespl, where N is
4201          * number of lines in a packet.  See errata about VP_CLK_RATIO */
4202
4203         if (bytespf < line_buf_size)
4204                 packet_payload = bytespf;
4205         else
4206                 packet_payload = (line_buf_size) / bytespl * bytespl;
4207
4208         packet_len = packet_payload + 1;        /* 1 byte for DCS cmd */
4209         total_len = (bytespf / packet_payload) * packet_len;
4210
4211         if (bytespf % packet_payload)
4212                 total_len += (bytespf % packet_payload) + 1;
4213
4214         l = FLD_VAL(total_len, 23, 0); /* TE_SIZE */
4215         dsi_write_reg(dsidev, DSI_VC_TE(channel), l);
4216
4217         dsi_vc_write_long_header(dsidev, channel, MIPI_DSI_DCS_LONG_WRITE,
4218                 packet_len, 0);
4219
4220         if (dsi->te_enabled)
4221                 l = FLD_MOD(l, 1, 30, 30); /* TE_EN */
4222         else
4223                 l = FLD_MOD(l, 1, 31, 31); /* TE_START */
4224         dsi_write_reg(dsidev, DSI_VC_TE(channel), l);
4225
4226         /* We put SIDLEMODE to no-idle for the duration of the transfer,
4227          * because DSS interrupts are not capable of waking up the CPU and the
4228          * framedone interrupt could be delayed for quite a long time. I think
4229          * the same goes for any DSS interrupts, but for some reason I have not
4230          * seen the problem anywhere else than here.
4231          */
4232         dispc_disable_sidle();
4233
4234         dsi_perf_mark_start(dsidev);
4235
4236         r = schedule_delayed_work(&dsi->framedone_timeout_work,
4237                 msecs_to_jiffies(250));
4238         BUG_ON(r == 0);
4239
4240         dss_mgr_set_timings(mgr, &dsi->timings);
4241
4242         dss_mgr_start_update(mgr);
4243
4244         if (dsi->te_enabled) {
4245                 /* disable LP_RX_TO, so that we can receive TE.  Time to wait
4246                  * for TE is longer than the timer allows */
4247                 REG_FLD_MOD(dsidev, DSI_TIMING2, 0, 15, 15); /* LP_RX_TO */
4248
4249                 dsi_vc_send_bta(dsidev, channel);
4250
4251 #ifdef DSI_CATCH_MISSING_TE
4252                 mod_timer(&dsi->te_timer, jiffies + msecs_to_jiffies(250));
4253 #endif
4254         }
4255 }
4256
4257 #ifdef DSI_CATCH_MISSING_TE
4258 static void dsi_te_timeout(unsigned long arg)
4259 {
4260         DSSERR("TE not received for 250ms!\n");
4261 }
4262 #endif
4263
4264 static void dsi_handle_framedone(struct platform_device *dsidev, int error)
4265 {
4266         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4267
4268         /* SIDLEMODE back to smart-idle */
4269         dispc_enable_sidle();
4270
4271         if (dsi->te_enabled) {
4272                 /* enable LP_RX_TO again after the TE */
4273                 REG_FLD_MOD(dsidev, DSI_TIMING2, 1, 15, 15); /* LP_RX_TO */
4274         }
4275
4276         dsi->framedone_callback(error, dsi->framedone_data);
4277
4278         if (!error)
4279                 dsi_perf_show(dsidev, "DISPC");
4280 }
4281
4282 static void dsi_framedone_timeout_work_callback(struct work_struct *work)
4283 {
4284         struct dsi_data *dsi = container_of(work, struct dsi_data,
4285                         framedone_timeout_work.work);
4286         /* XXX While extremely unlikely, we could get FRAMEDONE interrupt after
4287          * 250ms which would conflict with this timeout work. What should be
4288          * done is first cancel the transfer on the HW, and then cancel the
4289          * possibly scheduled framedone work. However, cancelling the transfer
4290          * on the HW is buggy, and would probably require resetting the whole
4291          * DSI */
4292
4293         DSSERR("Framedone not received for 250ms!\n");
4294
4295         dsi_handle_framedone(dsi->pdev, -ETIMEDOUT);
4296 }
4297
4298 static void dsi_framedone_irq_callback(void *data)
4299 {
4300         struct platform_device *dsidev = (struct platform_device *) data;
4301         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4302
4303         /* Note: We get FRAMEDONE when DISPC has finished sending pixels and
4304          * turns itself off. However, DSI still has the pixels in its buffers,
4305          * and is sending the data.
4306          */
4307
4308         cancel_delayed_work(&dsi->framedone_timeout_work);
4309
4310         dsi_handle_framedone(dsidev, 0);
4311 }
4312
4313 static int dsi_update(struct omap_dss_device *dssdev, int channel,
4314                 void (*callback)(int, void *), void *data)
4315 {
4316         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4317         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4318         u16 dw, dh;
4319
4320         dsi_perf_mark_setup(dsidev);
4321
4322         dsi->update_channel = channel;
4323
4324         dsi->framedone_callback = callback;
4325         dsi->framedone_data = data;
4326
4327         dw = dsi->timings.x_res;
4328         dh = dsi->timings.y_res;
4329
4330 #ifdef DSI_PERF_MEASURE
4331         dsi->update_bytes = dw * dh *
4332                 dsi_get_pixel_size(dsi->pix_fmt) / 8;
4333 #endif
4334         dsi_update_screen_dispc(dsidev);
4335
4336         return 0;
4337 }
4338
4339 /* Display funcs */
4340
4341 static int dsi_configure_dispc_clocks(struct platform_device *dsidev)
4342 {
4343         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4344         struct dispc_clock_info dispc_cinfo;
4345         int r;
4346         unsigned long fck;
4347
4348         fck = dsi_get_pll_hsdiv_dispc_rate(dsidev);
4349
4350         dispc_cinfo.lck_div = dsi->user_dispc_cinfo.lck_div;
4351         dispc_cinfo.pck_div = dsi->user_dispc_cinfo.pck_div;
4352
4353         r = dispc_calc_clock_rates(fck, &dispc_cinfo);
4354         if (r) {
4355                 DSSERR("Failed to calc dispc clocks\n");
4356                 return r;
4357         }
4358
4359         dsi->mgr_config.clock_info = dispc_cinfo;
4360
4361         return 0;
4362 }
4363
4364 static int dsi_display_init_dispc(struct platform_device *dsidev,
4365                 struct omap_overlay_manager *mgr)
4366 {
4367         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4368         int r;
4369
4370         dss_select_lcd_clk_source(mgr->id, dsi->module_id == 0 ?
4371                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC :
4372                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC);
4373
4374         if (dsi->mode == OMAP_DSS_DSI_CMD_MODE) {
4375                 r = dss_mgr_register_framedone_handler(mgr,
4376                                 dsi_framedone_irq_callback, dsidev);
4377                 if (r) {
4378                         DSSERR("can't register FRAMEDONE handler\n");
4379                         goto err;
4380                 }
4381
4382                 dsi->mgr_config.stallmode = true;
4383                 dsi->mgr_config.fifohandcheck = true;
4384         } else {
4385                 dsi->mgr_config.stallmode = false;
4386                 dsi->mgr_config.fifohandcheck = false;
4387         }
4388
4389         /*
4390          * override interlace, logic level and edge related parameters in
4391          * omap_video_timings with default values
4392          */
4393         dsi->timings.interlace = false;
4394         dsi->timings.hsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
4395         dsi->timings.vsync_level = OMAPDSS_SIG_ACTIVE_HIGH;
4396         dsi->timings.data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE;
4397         dsi->timings.de_level = OMAPDSS_SIG_ACTIVE_HIGH;
4398         dsi->timings.sync_pclk_edge = OMAPDSS_DRIVE_SIG_OPPOSITE_EDGES;
4399
4400         dss_mgr_set_timings(mgr, &dsi->timings);
4401
4402         r = dsi_configure_dispc_clocks(dsidev);
4403         if (r)
4404                 goto err1;
4405
4406         dsi->mgr_config.io_pad_mode = DSS_IO_PAD_MODE_BYPASS;
4407         dsi->mgr_config.video_port_width =
4408                         dsi_get_pixel_size(dsi->pix_fmt);
4409         dsi->mgr_config.lcden_sig_polarity = 0;
4410
4411         dss_mgr_set_lcd_config(mgr, &dsi->mgr_config);
4412
4413         return 0;
4414 err1:
4415         if (dsi->mode == OMAP_DSS_DSI_CMD_MODE)
4416                 dss_mgr_unregister_framedone_handler(mgr,
4417                                 dsi_framedone_irq_callback, dsidev);
4418 err:
4419         dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
4420         return r;
4421 }
4422
4423 static void dsi_display_uninit_dispc(struct platform_device *dsidev,
4424                 struct omap_overlay_manager *mgr)
4425 {
4426         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4427
4428         if (dsi->mode == OMAP_DSS_DSI_CMD_MODE)
4429                 dss_mgr_unregister_framedone_handler(mgr,
4430                                 dsi_framedone_irq_callback, dsidev);
4431
4432         dss_select_lcd_clk_source(mgr->id, OMAP_DSS_CLK_SRC_FCK);
4433 }
4434
4435 static int dsi_configure_dsi_clocks(struct platform_device *dsidev)
4436 {
4437         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4438         struct dsi_clock_info cinfo;
4439         int r;
4440
4441         cinfo = dsi->user_dsi_cinfo;
4442
4443         r = dsi_calc_clock_rates(dsidev, &cinfo);
4444         if (r) {
4445                 DSSERR("Failed to calc dsi clocks\n");
4446                 return r;
4447         }
4448
4449         r = dsi_pll_set_clock_div(dsidev, &cinfo);
4450         if (r) {
4451                 DSSERR("Failed to set dsi clocks\n");
4452                 return r;
4453         }
4454
4455         return 0;
4456 }
4457
4458 static int dsi_display_init_dsi(struct platform_device *dsidev)
4459 {
4460         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4461         int r;
4462
4463         r = dsi_pll_init(dsidev, true, true);
4464         if (r)
4465                 goto err0;
4466
4467         r = dsi_configure_dsi_clocks(dsidev);
4468         if (r)
4469                 goto err1;
4470
4471         dss_select_dsi_clk_source(dsi->module_id, dsi->module_id == 0 ?
4472                         OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI :
4473                         OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DSI);
4474
4475         DSSDBG("PLL OK\n");
4476
4477         r = dsi_cio_init(dsidev);
4478         if (r)
4479                 goto err2;
4480
4481         _dsi_print_reset_status(dsidev);
4482
4483         dsi_proto_timings(dsidev);
4484         dsi_set_lp_clk_divisor(dsidev);
4485
4486         if (1)
4487                 _dsi_print_reset_status(dsidev);
4488
4489         r = dsi_proto_config(dsidev);
4490         if (r)
4491                 goto err3;
4492
4493         /* enable interface */
4494         dsi_vc_enable(dsidev, 0, 1);
4495         dsi_vc_enable(dsidev, 1, 1);
4496         dsi_vc_enable(dsidev, 2, 1);
4497         dsi_vc_enable(dsidev, 3, 1);
4498         dsi_if_enable(dsidev, 1);
4499         dsi_force_tx_stop_mode_io(dsidev);
4500
4501         return 0;
4502 err3:
4503         dsi_cio_uninit(dsidev);
4504 err2:
4505         dss_select_dsi_clk_source(dsi->module_id, OMAP_DSS_CLK_SRC_FCK);
4506 err1:
4507         dsi_pll_uninit(dsidev, true);
4508 err0:
4509         return r;
4510 }
4511
4512 static void dsi_display_uninit_dsi(struct platform_device *dsidev,
4513                 bool disconnect_lanes, bool enter_ulps)
4514 {
4515         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4516
4517         if (enter_ulps && !dsi->ulps_enabled)
4518                 dsi_enter_ulps(dsidev);
4519
4520         /* disable interface */
4521         dsi_if_enable(dsidev, 0);
4522         dsi_vc_enable(dsidev, 0, 0);
4523         dsi_vc_enable(dsidev, 1, 0);
4524         dsi_vc_enable(dsidev, 2, 0);
4525         dsi_vc_enable(dsidev, 3, 0);
4526
4527         dss_select_dsi_clk_source(dsi->module_id, OMAP_DSS_CLK_SRC_FCK);
4528         dsi_cio_uninit(dsidev);
4529         dsi_pll_uninit(dsidev, disconnect_lanes);
4530 }
4531
4532 static int dsi_display_enable(struct omap_dss_device *dssdev)
4533 {
4534         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4535         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4536         int r = 0;
4537
4538         DSSDBG("dsi_display_enable\n");
4539
4540         WARN_ON(!dsi_bus_is_locked(dsidev));
4541
4542         mutex_lock(&dsi->lock);
4543
4544         r = dsi_runtime_get(dsidev);
4545         if (r)
4546                 goto err_get_dsi;
4547
4548         dsi_enable_pll_clock(dsidev, 1);
4549
4550         _dsi_initialize_irq(dsidev);
4551
4552         r = dsi_display_init_dsi(dsidev);
4553         if (r)
4554                 goto err_init_dsi;
4555
4556         mutex_unlock(&dsi->lock);
4557
4558         return 0;
4559
4560 err_init_dsi:
4561         dsi_enable_pll_clock(dsidev, 0);
4562         dsi_runtime_put(dsidev);
4563 err_get_dsi:
4564         mutex_unlock(&dsi->lock);
4565         DSSDBG("dsi_display_enable FAILED\n");
4566         return r;
4567 }
4568
4569 static void dsi_display_disable(struct omap_dss_device *dssdev,
4570                 bool disconnect_lanes, bool enter_ulps)
4571 {
4572         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4573         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4574
4575         DSSDBG("dsi_display_disable\n");
4576
4577         WARN_ON(!dsi_bus_is_locked(dsidev));
4578
4579         mutex_lock(&dsi->lock);
4580
4581         dsi_sync_vc(dsidev, 0);
4582         dsi_sync_vc(dsidev, 1);
4583         dsi_sync_vc(dsidev, 2);
4584         dsi_sync_vc(dsidev, 3);
4585
4586         dsi_display_uninit_dsi(dsidev, disconnect_lanes, enter_ulps);
4587
4588         dsi_runtime_put(dsidev);
4589         dsi_enable_pll_clock(dsidev, 0);
4590
4591         mutex_unlock(&dsi->lock);
4592 }
4593
4594 static int dsi_enable_te(struct omap_dss_device *dssdev, bool enable)
4595 {
4596         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
4597         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
4598
4599         dsi->te_enabled = enable;
4600         return 0;
4601 }
4602
4603 #ifdef PRINT_VERBOSE_VM_TIMINGS
4604 static void print_dsi_vm(const char *str,
4605                 const struct omap_dss_dsi_videomode_timings *t)
4606 {
4607         unsigned long byteclk = t->hsclk / 4;
4608         int bl, wc, pps, tot;
4609
4610         wc = DIV_ROUND_UP(t->hact * t->bitspp, 8);
4611         pps = DIV_ROUND_UP(wc + 6, t->ndl); /* pixel packet size */
4612         bl = t->hss + t->hsa + t->hse + t->hbp + t->hfp;
4613         tot = bl + pps;
4614
4615 #define TO_DSI_T(x) ((u32)div64_u64((u64)x * 1000000000llu, byteclk))
4616
4617         pr_debug("%s bck %lu, %u/%u/%u/%u/%u/%u = %u+%u = %u, "
4618                         "%u/%u/%u/%u/%u/%u = %u + %u = %u\n",
4619                         str,
4620                         byteclk,
4621                         t->hss, t->hsa, t->hse, t->hbp, pps, t->hfp,
4622                         bl, pps, tot,
4623                         TO_DSI_T(t->hss),
4624                         TO_DSI_T(t->hsa),
4625                         TO_DSI_T(t->hse),
4626                         TO_DSI_T(t->hbp),
4627                         TO_DSI_T(pps),
4628                         TO_DSI_T(t->hfp),
4629
4630                         TO_DSI_T(bl),
4631                         TO_DSI_T(pps),
4632
4633                         TO_DSI_T(tot));
4634 #undef TO_DSI_T
4635 }
4636
4637 static void print_dispc_vm(const char *str, const struct omap_video_timings *t)
4638 {
4639         unsigned long pck = t->pixelclock;
4640         int hact, bl, tot;
4641
4642         hact = t->x_res;
4643         bl = t->hsw + t->hbp + t->hfp;
4644         tot = hact + bl;
4645
4646 #define TO_DISPC_T(x) ((u32)div64_u64((u64)x * 1000000000llu, pck))
4647
4648         pr_debug("%s pck %lu, %u/%u/%u/%u = %u+%u = %u, "
4649                         "%u/%u/%u/%u = %u + %u = %u\n",
4650                         str,
4651                         pck,
4652                         t->hsw, t->hbp, hact, t->hfp,
4653                         bl, hact, tot,
4654                         TO_DISPC_T(t->hsw),
4655                         TO_DISPC_T(t->hbp),
4656                         TO_DISPC_T(hact),
4657                         TO_DISPC_T(t->hfp),
4658                         TO_DISPC_T(bl),
4659                         TO_DISPC_T(hact),
4660                         TO_DISPC_T(tot));
4661 #undef TO_DISPC_T
4662 }
4663
4664 /* note: this is not quite accurate */
4665 static void print_dsi_dispc_vm(const char *str,
4666                 const struct omap_dss_dsi_videomode_timings *t)
4667 {
4668         struct omap_video_timings vm = { 0 };
4669         unsigned long byteclk = t->hsclk / 4;
4670         unsigned long pck;
4671         u64 dsi_tput;
4672         int dsi_hact, dsi_htot;
4673
4674         dsi_tput = (u64)byteclk * t->ndl * 8;
4675         pck = (u32)div64_u64(dsi_tput, t->bitspp);
4676         dsi_hact = DIV_ROUND_UP(DIV_ROUND_UP(t->hact * t->bitspp, 8) + 6, t->ndl);
4677         dsi_htot = t->hss + t->hsa + t->hse + t->hbp + dsi_hact + t->hfp;
4678
4679         vm.pixelclock = pck;
4680         vm.hsw = div64_u64((u64)(t->hsa + t->hse) * pck, byteclk);
4681         vm.hbp = div64_u64((u64)t->hbp * pck, byteclk);
4682         vm.hfp = div64_u64((u64)t->hfp * pck, byteclk);
4683         vm.x_res = t->hact;
4684
4685         print_dispc_vm(str, &vm);
4686 }
4687 #endif /* PRINT_VERBOSE_VM_TIMINGS */
4688
4689 static bool dsi_cm_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
4690                 unsigned long pck, void *data)
4691 {
4692         struct dsi_clk_calc_ctx *ctx = data;
4693         struct omap_video_timings *t = &ctx->dispc_vm;
4694
4695         ctx->dispc_cinfo.lck_div = lckd;
4696         ctx->dispc_cinfo.pck_div = pckd;
4697         ctx->dispc_cinfo.lck = lck;
4698         ctx->dispc_cinfo.pck = pck;
4699
4700         *t = *ctx->config->timings;
4701         t->pixelclock = pck;
4702         t->x_res = ctx->config->timings->x_res;
4703         t->y_res = ctx->config->timings->y_res;
4704         t->hsw = t->hfp = t->hbp = t->vsw = 1;
4705         t->vfp = t->vbp = 0;
4706
4707         return true;
4708 }
4709
4710 static bool dsi_cm_calc_hsdiv_cb(int regm_dispc, unsigned long dispc,
4711                 void *data)
4712 {
4713         struct dsi_clk_calc_ctx *ctx = data;
4714
4715         ctx->dsi_cinfo.regm_dispc = regm_dispc;
4716         ctx->dsi_cinfo.dsi_pll_hsdiv_dispc_clk = dispc;
4717
4718         return dispc_div_calc(dispc, ctx->req_pck_min, ctx->req_pck_max,
4719                         dsi_cm_calc_dispc_cb, ctx);
4720 }
4721
4722 static bool dsi_cm_calc_pll_cb(int regn, int regm, unsigned long fint,
4723                 unsigned long pll, void *data)
4724 {
4725         struct dsi_clk_calc_ctx *ctx = data;
4726
4727         ctx->dsi_cinfo.regn = regn;
4728         ctx->dsi_cinfo.regm = regm;
4729         ctx->dsi_cinfo.fint = fint;
4730         ctx->dsi_cinfo.clkin4ddr = pll;
4731
4732         return dsi_hsdiv_calc(ctx->dsidev, pll, ctx->req_pck_min,
4733                         dsi_cm_calc_hsdiv_cb, ctx);
4734 }
4735
4736 static bool dsi_cm_calc(struct dsi_data *dsi,
4737                 const struct omap_dss_dsi_config *cfg,
4738                 struct dsi_clk_calc_ctx *ctx)
4739 {
4740         unsigned long clkin;
4741         int bitspp, ndl;
4742         unsigned long pll_min, pll_max;
4743         unsigned long pck, txbyteclk;
4744
4745         clkin = clk_get_rate(dsi->sys_clk);
4746         bitspp = dsi_get_pixel_size(cfg->pixel_format);
4747         ndl = dsi->num_lanes_used - 1;
4748
4749         /*
4750          * Here we should calculate minimum txbyteclk to be able to send the
4751          * frame in time, and also to handle TE. That's not very simple, though,
4752          * especially as we go to LP between each pixel packet due to HW
4753          * "feature". So let's just estimate very roughly and multiply by 1.5.
4754          */
4755         pck = cfg->timings->pixelclock;
4756         pck = pck * 3 / 2;
4757         txbyteclk = pck * bitspp / 8 / ndl;
4758
4759         memset(ctx, 0, sizeof(*ctx));
4760         ctx->dsidev = dsi->pdev;
4761         ctx->config = cfg;
4762         ctx->req_pck_min = pck;
4763         ctx->req_pck_nom = pck;
4764         ctx->req_pck_max = pck * 3 / 2;
4765         ctx->dsi_cinfo.clkin = clkin;
4766
4767         pll_min = max(cfg->hs_clk_min * 4, txbyteclk * 4 * 4);
4768         pll_max = cfg->hs_clk_max * 4;
4769
4770         return dsi_pll_calc(dsi->pdev, clkin,
4771                         pll_min, pll_max,
4772                         dsi_cm_calc_pll_cb, ctx);
4773 }
4774
4775 static bool dsi_vm_calc_blanking(struct dsi_clk_calc_ctx *ctx)
4776 {
4777         struct dsi_data *dsi = dsi_get_dsidrv_data(ctx->dsidev);
4778         const struct omap_dss_dsi_config *cfg = ctx->config;
4779         int bitspp = dsi_get_pixel_size(cfg->pixel_format);
4780         int ndl = dsi->num_lanes_used - 1;
4781         unsigned long hsclk = ctx->dsi_cinfo.clkin4ddr / 4;
4782         unsigned long byteclk = hsclk / 4;
4783
4784         unsigned long dispc_pck, req_pck_min, req_pck_nom, req_pck_max;
4785         int xres;
4786         int panel_htot, panel_hbl; /* pixels */
4787         int dispc_htot, dispc_hbl; /* pixels */
4788         int dsi_htot, dsi_hact, dsi_hbl, hss, hse; /* byteclks */
4789         int hfp, hsa, hbp;
4790         const struct omap_video_timings *req_vm;
4791         struct omap_video_timings *dispc_vm;
4792         struct omap_dss_dsi_videomode_timings *dsi_vm;
4793         u64 dsi_tput, dispc_tput;
4794
4795         dsi_tput = (u64)byteclk * ndl * 8;
4796
4797         req_vm = cfg->timings;
4798         req_pck_min = ctx->req_pck_min;
4799         req_pck_max = ctx->req_pck_max;
4800         req_pck_nom = ctx->req_pck_nom;
4801
4802         dispc_pck = ctx->dispc_cinfo.pck;
4803         dispc_tput = (u64)dispc_pck * bitspp;
4804
4805         xres = req_vm->x_res;
4806
4807         panel_hbl = req_vm->hfp + req_vm->hbp + req_vm->hsw;
4808         panel_htot = xres + panel_hbl;
4809
4810         dsi_hact = DIV_ROUND_UP(DIV_ROUND_UP(xres * bitspp, 8) + 6, ndl);
4811
4812         /*
4813          * When there are no line buffers, DISPC and DSI must have the
4814          * same tput. Otherwise DISPC tput needs to be higher than DSI's.
4815          */
4816         if (dsi->line_buffer_size < xres * bitspp / 8) {
4817                 if (dispc_tput != dsi_tput)
4818                         return false;
4819         } else {
4820                 if (dispc_tput < dsi_tput)
4821                         return false;
4822         }
4823
4824         /* DSI tput must be over the min requirement */
4825         if (dsi_tput < (u64)bitspp * req_pck_min)
4826                 return false;
4827
4828         /* When non-burst mode, DSI tput must be below max requirement. */
4829         if (cfg->trans_mode != OMAP_DSS_DSI_BURST_MODE) {
4830                 if (dsi_tput > (u64)bitspp * req_pck_max)
4831                         return false;
4832         }
4833
4834         hss = DIV_ROUND_UP(4, ndl);
4835
4836         if (cfg->trans_mode == OMAP_DSS_DSI_PULSE_MODE) {
4837                 if (ndl == 3 && req_vm->hsw == 0)
4838                         hse = 1;
4839                 else
4840                         hse = DIV_ROUND_UP(4, ndl);
4841         } else {
4842                 hse = 0;
4843         }
4844
4845         /* DSI htot to match the panel's nominal pck */
4846         dsi_htot = div64_u64((u64)panel_htot * byteclk, req_pck_nom);
4847
4848         /* fail if there would be no time for blanking */
4849         if (dsi_htot < hss + hse + dsi_hact)
4850                 return false;
4851
4852         /* total DSI blanking needed to achieve panel's TL */
4853         dsi_hbl = dsi_htot - dsi_hact;
4854
4855         /* DISPC htot to match the DSI TL */
4856         dispc_htot = div64_u64((u64)dsi_htot * dispc_pck, byteclk);
4857
4858         /* verify that the DSI and DISPC TLs are the same */
4859         if ((u64)dsi_htot * dispc_pck != (u64)dispc_htot * byteclk)
4860                 return false;
4861
4862         dispc_hbl = dispc_htot - xres;
4863
4864         /* setup DSI videomode */
4865
4866         dsi_vm = &ctx->dsi_vm;
4867         memset(dsi_vm, 0, sizeof(*dsi_vm));
4868
4869         dsi_vm->hsclk = hsclk;
4870
4871         dsi_vm->ndl = ndl;
4872         dsi_vm->bitspp = bitspp;
4873
4874         if (cfg->trans_mode != OMAP_DSS_DSI_PULSE_MODE) {
4875                 hsa = 0;
4876         } else if (ndl == 3 && req_vm->hsw == 0) {
4877                 hsa = 0;
4878         } else {
4879                 hsa = div64_u64((u64)req_vm->hsw * byteclk, req_pck_nom);
4880                 hsa = max(hsa - hse, 1);
4881         }
4882
4883         hbp = div64_u64((u64)req_vm->hbp * byteclk, req_pck_nom);
4884         hbp = max(hbp, 1);
4885
4886         hfp = dsi_hbl - (hss + hsa + hse + hbp);
4887         if (hfp < 1) {
4888                 int t;
4889                 /* we need to take cycles from hbp */
4890
4891                 t = 1 - hfp;
4892                 hbp = max(hbp - t, 1);
4893                 hfp = dsi_hbl - (hss + hsa + hse + hbp);
4894
4895                 if (hfp < 1 && hsa > 0) {
4896                         /* we need to take cycles from hsa */
4897                         t = 1 - hfp;
4898                         hsa = max(hsa - t, 1);
4899                         hfp = dsi_hbl - (hss + hsa + hse + hbp);
4900                 }
4901         }
4902
4903         if (hfp < 1)
4904                 return false;
4905
4906         dsi_vm->hss = hss;
4907         dsi_vm->hsa = hsa;
4908         dsi_vm->hse = hse;
4909         dsi_vm->hbp = hbp;
4910         dsi_vm->hact = xres;
4911         dsi_vm->hfp = hfp;
4912
4913         dsi_vm->vsa = req_vm->vsw;
4914         dsi_vm->vbp = req_vm->vbp;
4915         dsi_vm->vact = req_vm->y_res;
4916         dsi_vm->vfp = req_vm->vfp;
4917
4918         dsi_vm->trans_mode = cfg->trans_mode;
4919
4920         dsi_vm->blanking_mode = 0;
4921         dsi_vm->hsa_blanking_mode = 1;
4922         dsi_vm->hfp_blanking_mode = 1;
4923         dsi_vm->hbp_blanking_mode = 1;
4924
4925         dsi_vm->ddr_clk_always_on = cfg->ddr_clk_always_on;
4926         dsi_vm->window_sync = 4;
4927
4928         /* setup DISPC videomode */
4929
4930         dispc_vm = &ctx->dispc_vm;
4931         *dispc_vm = *req_vm;
4932         dispc_vm->pixelclock = dispc_pck;
4933
4934         if (cfg->trans_mode == OMAP_DSS_DSI_PULSE_MODE) {
4935                 hsa = div64_u64((u64)req_vm->hsw * dispc_pck,
4936                                 req_pck_nom);
4937                 hsa = max(hsa, 1);
4938         } else {
4939                 hsa = 1;
4940         }
4941
4942         hbp = div64_u64((u64)req_vm->hbp * dispc_pck, req_pck_nom);
4943         hbp = max(hbp, 1);
4944
4945         hfp = dispc_hbl - hsa - hbp;
4946         if (hfp < 1) {
4947                 int t;
4948                 /* we need to take cycles from hbp */
4949
4950                 t = 1 - hfp;
4951                 hbp = max(hbp - t, 1);
4952                 hfp = dispc_hbl - hsa - hbp;
4953
4954                 if (hfp < 1) {
4955                         /* we need to take cycles from hsa */
4956                         t = 1 - hfp;
4957                         hsa = max(hsa - t, 1);
4958                         hfp = dispc_hbl - hsa - hbp;
4959                 }
4960         }
4961
4962         if (hfp < 1)
4963                 return false;
4964
4965         dispc_vm->hfp = hfp;
4966         dispc_vm->hsw = hsa;
4967         dispc_vm->hbp = hbp;
4968
4969         return true;
4970 }
4971
4972
4973 static bool dsi_vm_calc_dispc_cb(int lckd, int pckd, unsigned long lck,
4974                 unsigned long pck, void *data)
4975 {
4976         struct dsi_clk_calc_ctx *ctx = data;
4977
4978         ctx->dispc_cinfo.lck_div = lckd;
4979         ctx->dispc_cinfo.pck_div = pckd;
4980         ctx->dispc_cinfo.lck = lck;
4981         ctx->dispc_cinfo.pck = pck;
4982
4983         if (dsi_vm_calc_blanking(ctx) == false)
4984                 return false;
4985
4986 #ifdef PRINT_VERBOSE_VM_TIMINGS
4987         print_dispc_vm("dispc", &ctx->dispc_vm);
4988         print_dsi_vm("dsi  ", &ctx->dsi_vm);
4989         print_dispc_vm("req  ", ctx->config->timings);
4990         print_dsi_dispc_vm("act  ", &ctx->dsi_vm);
4991 #endif
4992
4993         return true;
4994 }
4995
4996 static bool dsi_vm_calc_hsdiv_cb(int regm_dispc, unsigned long dispc,
4997                 void *data)
4998 {
4999         struct dsi_clk_calc_ctx *ctx = data;
5000         unsigned long pck_max;
5001
5002         ctx->dsi_cinfo.regm_dispc = regm_dispc;
5003         ctx->dsi_cinfo.dsi_pll_hsdiv_dispc_clk = dispc;
5004
5005         /*
5006          * In burst mode we can let the dispc pck be arbitrarily high, but it
5007          * limits our scaling abilities. So for now, don't aim too high.
5008          */
5009
5010         if (ctx->config->trans_mode == OMAP_DSS_DSI_BURST_MODE)
5011                 pck_max = ctx->req_pck_max + 10000000;
5012         else
5013                 pck_max = ctx->req_pck_max;
5014
5015         return dispc_div_calc(dispc, ctx->req_pck_min, pck_max,
5016                         dsi_vm_calc_dispc_cb, ctx);
5017 }
5018
5019 static bool dsi_vm_calc_pll_cb(int regn, int regm, unsigned long fint,
5020                 unsigned long pll, void *data)
5021 {
5022         struct dsi_clk_calc_ctx *ctx = data;
5023
5024         ctx->dsi_cinfo.regn = regn;
5025         ctx->dsi_cinfo.regm = regm;
5026         ctx->dsi_cinfo.fint = fint;
5027         ctx->dsi_cinfo.clkin4ddr = pll;
5028
5029         return dsi_hsdiv_calc(ctx->dsidev, pll, ctx->req_pck_min,
5030                         dsi_vm_calc_hsdiv_cb, ctx);
5031 }
5032
5033 static bool dsi_vm_calc(struct dsi_data *dsi,
5034                 const struct omap_dss_dsi_config *cfg,
5035                 struct dsi_clk_calc_ctx *ctx)
5036 {
5037         const struct omap_video_timings *t = cfg->timings;
5038         unsigned long clkin;
5039         unsigned long pll_min;
5040         unsigned long pll_max;
5041         int ndl = dsi->num_lanes_used - 1;
5042         int bitspp = dsi_get_pixel_size(cfg->pixel_format);
5043         unsigned long byteclk_min;
5044
5045         clkin = clk_get_rate(dsi->sys_clk);
5046
5047         memset(ctx, 0, sizeof(*ctx));
5048         ctx->dsidev = dsi->pdev;
5049         ctx->config = cfg;
5050
5051         ctx->dsi_cinfo.clkin = clkin;
5052
5053         /* these limits should come from the panel driver */
5054         ctx->req_pck_min = t->pixelclock - 1000;
5055         ctx->req_pck_nom = t->pixelclock;
5056         ctx->req_pck_max = t->pixelclock + 1000;
5057
5058         byteclk_min = div64_u64((u64)ctx->req_pck_min * bitspp, ndl * 8);
5059         pll_min = max(cfg->hs_clk_min * 4, byteclk_min * 4 * 4);
5060
5061         if (cfg->trans_mode == OMAP_DSS_DSI_BURST_MODE) {
5062                 pll_max = cfg->hs_clk_max * 4;
5063         } else {
5064                 unsigned long byteclk_max;
5065                 byteclk_max = div64_u64((u64)ctx->req_pck_max * bitspp,
5066                                 ndl * 8);
5067
5068                 pll_max = byteclk_max * 4 * 4;
5069         }
5070
5071         return dsi_pll_calc(dsi->pdev, clkin,
5072                         pll_min, pll_max,
5073                         dsi_vm_calc_pll_cb, ctx);
5074 }
5075
5076 static int dsi_set_config(struct omap_dss_device *dssdev,
5077                 const struct omap_dss_dsi_config *config)
5078 {
5079         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
5080         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5081         struct dsi_clk_calc_ctx ctx;
5082         bool ok;
5083         int r;
5084
5085         mutex_lock(&dsi->lock);
5086
5087         dsi->pix_fmt = config->pixel_format;
5088         dsi->mode = config->mode;
5089
5090         if (config->mode == OMAP_DSS_DSI_VIDEO_MODE)
5091                 ok = dsi_vm_calc(dsi, config, &ctx);
5092         else
5093                 ok = dsi_cm_calc(dsi, config, &ctx);
5094
5095         if (!ok) {
5096                 DSSERR("failed to find suitable DSI clock settings\n");
5097                 r = -EINVAL;
5098                 goto err;
5099         }
5100
5101         dsi_pll_calc_dsi_fck(&ctx.dsi_cinfo);
5102
5103         r = dsi_lp_clock_calc(&ctx.dsi_cinfo, config->lp_clk_min,
5104                         config->lp_clk_max);
5105         if (r) {
5106                 DSSERR("failed to find suitable DSI LP clock settings\n");
5107                 goto err;
5108         }
5109
5110         dsi->user_dsi_cinfo = ctx.dsi_cinfo;
5111         dsi->user_dispc_cinfo = ctx.dispc_cinfo;
5112
5113         dsi->timings = ctx.dispc_vm;
5114         dsi->vm_timings = ctx.dsi_vm;
5115
5116         mutex_unlock(&dsi->lock);
5117
5118         return 0;
5119 err:
5120         mutex_unlock(&dsi->lock);
5121
5122         return r;
5123 }
5124
5125 /*
5126  * Return a hardcoded channel for the DSI output. This should work for
5127  * current use cases, but this can be later expanded to either resolve
5128  * the channel in some more dynamic manner, or get the channel as a user
5129  * parameter.
5130  */
5131 static enum omap_channel dsi_get_channel(int module_id)
5132 {
5133         switch (omapdss_get_version()) {
5134         case OMAPDSS_VER_OMAP24xx:
5135         case OMAPDSS_VER_AM43xx:
5136                 DSSWARN("DSI not supported\n");
5137                 return OMAP_DSS_CHANNEL_LCD;
5138
5139         case OMAPDSS_VER_OMAP34xx_ES1:
5140         case OMAPDSS_VER_OMAP34xx_ES3:
5141         case OMAPDSS_VER_OMAP3630:
5142         case OMAPDSS_VER_AM35xx:
5143                 return OMAP_DSS_CHANNEL_LCD;
5144
5145         case OMAPDSS_VER_OMAP4430_ES1:
5146         case OMAPDSS_VER_OMAP4430_ES2:
5147         case OMAPDSS_VER_OMAP4:
5148                 switch (module_id) {
5149                 case 0:
5150                         return OMAP_DSS_CHANNEL_LCD;
5151                 case 1:
5152                         return OMAP_DSS_CHANNEL_LCD2;
5153                 default:
5154                         DSSWARN("unsupported module id\n");
5155                         return OMAP_DSS_CHANNEL_LCD;
5156                 }
5157
5158         case OMAPDSS_VER_OMAP5:
5159                 switch (module_id) {
5160                 case 0:
5161                         return OMAP_DSS_CHANNEL_LCD;
5162                 case 1:
5163                         return OMAP_DSS_CHANNEL_LCD3;
5164                 default:
5165                         DSSWARN("unsupported module id\n");
5166                         return OMAP_DSS_CHANNEL_LCD;
5167                 }
5168
5169         default:
5170                 DSSWARN("unsupported DSS version\n");
5171                 return OMAP_DSS_CHANNEL_LCD;
5172         }
5173 }
5174
5175 static int dsi_request_vc(struct omap_dss_device *dssdev, int *channel)
5176 {
5177         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
5178         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5179         int i;
5180
5181         for (i = 0; i < ARRAY_SIZE(dsi->vc); i++) {
5182                 if (!dsi->vc[i].dssdev) {
5183                         dsi->vc[i].dssdev = dssdev;
5184                         *channel = i;
5185                         return 0;
5186                 }
5187         }
5188
5189         DSSERR("cannot get VC for display %s", dssdev->name);
5190         return -ENOSPC;
5191 }
5192
5193 static int dsi_set_vc_id(struct omap_dss_device *dssdev, int channel, int vc_id)
5194 {
5195         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
5196         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5197
5198         if (vc_id < 0 || vc_id > 3) {
5199                 DSSERR("VC ID out of range\n");
5200                 return -EINVAL;
5201         }
5202
5203         if (channel < 0 || channel > 3) {
5204                 DSSERR("Virtual Channel out of range\n");
5205                 return -EINVAL;
5206         }
5207
5208         if (dsi->vc[channel].dssdev != dssdev) {
5209                 DSSERR("Virtual Channel not allocated to display %s\n",
5210                         dssdev->name);
5211                 return -EINVAL;
5212         }
5213
5214         dsi->vc[channel].vc_id = vc_id;
5215
5216         return 0;
5217 }
5218
5219 static void dsi_release_vc(struct omap_dss_device *dssdev, int channel)
5220 {
5221         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
5222         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5223
5224         if ((channel >= 0 && channel <= 3) &&
5225                 dsi->vc[channel].dssdev == dssdev) {
5226                 dsi->vc[channel].dssdev = NULL;
5227                 dsi->vc[channel].vc_id = 0;
5228         }
5229 }
5230
5231 void dsi_wait_pll_hsdiv_dispc_active(struct platform_device *dsidev)
5232 {
5233         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 7, 1) != 1)
5234                 DSSERR("%s (%s) not active\n",
5235                         dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC),
5236                         dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC));
5237 }
5238
5239 void dsi_wait_pll_hsdiv_dsi_active(struct platform_device *dsidev)
5240 {
5241         if (wait_for_bit_change(dsidev, DSI_PLL_STATUS, 8, 1) != 1)
5242                 DSSERR("%s (%s) not active\n",
5243                         dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI),
5244                         dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI));
5245 }
5246
5247 static void dsi_calc_clock_param_ranges(struct platform_device *dsidev)
5248 {
5249         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5250
5251         dsi->regn_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGN);
5252         dsi->regm_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM);
5253         dsi->regm_dispc_max =
5254                 dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM_DISPC);
5255         dsi->regm_dsi_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_REGM_DSI);
5256         dsi->fint_min = dss_feat_get_param_min(FEAT_PARAM_DSIPLL_FINT);
5257         dsi->fint_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_FINT);
5258         dsi->lpdiv_max = dss_feat_get_param_max(FEAT_PARAM_DSIPLL_LPDIV);
5259 }
5260
5261 static int dsi_get_clocks(struct platform_device *dsidev)
5262 {
5263         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5264         struct clk *clk;
5265
5266         clk = devm_clk_get(&dsidev->dev, "fck");
5267         if (IS_ERR(clk)) {
5268                 DSSERR("can't get fck\n");
5269                 return PTR_ERR(clk);
5270         }
5271
5272         dsi->dss_clk = clk;
5273
5274         clk = devm_clk_get(&dsidev->dev, "sys_clk");
5275         if (IS_ERR(clk)) {
5276                 DSSERR("can't get sys_clk\n");
5277                 return PTR_ERR(clk);
5278         }
5279
5280         dsi->sys_clk = clk;
5281
5282         return 0;
5283 }
5284
5285 static int dsi_connect(struct omap_dss_device *dssdev,
5286                 struct omap_dss_device *dst)
5287 {
5288         struct platform_device *dsidev = dsi_get_dsidev_from_dssdev(dssdev);
5289         struct omap_overlay_manager *mgr;
5290         int r;
5291
5292         r = dsi_regulator_init(dsidev);
5293         if (r)
5294                 return r;
5295
5296         mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
5297         if (!mgr)
5298                 return -ENODEV;
5299
5300         r = dss_mgr_connect(mgr, dssdev);
5301         if (r)
5302                 return r;
5303
5304         r = omapdss_output_set_device(dssdev, dst);
5305         if (r) {
5306                 DSSERR("failed to connect output to new device: %s\n",
5307                                 dssdev->name);
5308                 dss_mgr_disconnect(mgr, dssdev);
5309                 return r;
5310         }
5311
5312         return 0;
5313 }
5314
5315 static void dsi_disconnect(struct omap_dss_device *dssdev,
5316                 struct omap_dss_device *dst)
5317 {
5318         WARN_ON(dst != dssdev->dst);
5319
5320         if (dst != dssdev->dst)
5321                 return;
5322
5323         omapdss_output_unset_device(dssdev);
5324
5325         if (dssdev->manager)
5326                 dss_mgr_disconnect(dssdev->manager, dssdev);
5327 }
5328
5329 static const struct omapdss_dsi_ops dsi_ops = {
5330         .connect = dsi_connect,
5331         .disconnect = dsi_disconnect,
5332
5333         .bus_lock = dsi_bus_lock,
5334         .bus_unlock = dsi_bus_unlock,
5335
5336         .enable = dsi_display_enable,
5337         .disable = dsi_display_disable,
5338
5339         .enable_hs = dsi_vc_enable_hs,
5340
5341         .configure_pins = dsi_configure_pins,
5342         .set_config = dsi_set_config,
5343
5344         .enable_video_output = dsi_enable_video_output,
5345         .disable_video_output = dsi_disable_video_output,
5346
5347         .update = dsi_update,
5348
5349         .enable_te = dsi_enable_te,
5350
5351         .request_vc = dsi_request_vc,
5352         .set_vc_id = dsi_set_vc_id,
5353         .release_vc = dsi_release_vc,
5354
5355         .dcs_write = dsi_vc_dcs_write,
5356         .dcs_write_nosync = dsi_vc_dcs_write_nosync,
5357         .dcs_read = dsi_vc_dcs_read,
5358
5359         .gen_write = dsi_vc_generic_write,
5360         .gen_write_nosync = dsi_vc_generic_write_nosync,
5361         .gen_read = dsi_vc_generic_read,
5362
5363         .bta_sync = dsi_vc_send_bta_sync,
5364
5365         .set_max_rx_packet_size = dsi_vc_set_max_rx_packet_size,
5366 };
5367
5368 static void dsi_init_output(struct platform_device *dsidev)
5369 {
5370         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5371         struct omap_dss_device *out = &dsi->output;
5372
5373         out->dev = &dsidev->dev;
5374         out->id = dsi->module_id == 0 ?
5375                         OMAP_DSS_OUTPUT_DSI1 : OMAP_DSS_OUTPUT_DSI2;
5376
5377         out->output_type = OMAP_DISPLAY_TYPE_DSI;
5378         out->name = dsi->module_id == 0 ? "dsi.0" : "dsi.1";
5379         out->dispc_channel = dsi_get_channel(dsi->module_id);
5380         out->ops.dsi = &dsi_ops;
5381         out->owner = THIS_MODULE;
5382
5383         omapdss_register_output(out);
5384 }
5385
5386 static void dsi_uninit_output(struct platform_device *dsidev)
5387 {
5388         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5389         struct omap_dss_device *out = &dsi->output;
5390
5391         omapdss_unregister_output(out);
5392 }
5393
5394 static int dsi_probe_of(struct platform_device *pdev)
5395 {
5396         struct device_node *node = pdev->dev.of_node;
5397         struct dsi_data *dsi = dsi_get_dsidrv_data(pdev);
5398         struct property *prop;
5399         u32 lane_arr[10];
5400         int len, num_pins;
5401         int r, i;
5402         struct device_node *ep;
5403         struct omap_dsi_pin_config pin_cfg;
5404
5405         ep = omapdss_of_get_first_endpoint(node);
5406         if (!ep)
5407                 return 0;
5408
5409         prop = of_find_property(ep, "lanes", &len);
5410         if (prop == NULL) {
5411                 dev_err(&pdev->dev, "failed to find lane data\n");
5412                 r = -EINVAL;
5413                 goto err;
5414         }
5415
5416         num_pins = len / sizeof(u32);
5417
5418         if (num_pins < 4 || num_pins % 2 != 0 ||
5419                 num_pins > dsi->num_lanes_supported * 2) {
5420                 dev_err(&pdev->dev, "bad number of lanes\n");
5421                 r = -EINVAL;
5422                 goto err;
5423         }
5424
5425         r = of_property_read_u32_array(ep, "lanes", lane_arr, num_pins);
5426         if (r) {
5427                 dev_err(&pdev->dev, "failed to read lane data\n");
5428                 goto err;
5429         }
5430
5431         pin_cfg.num_pins = num_pins;
5432         for (i = 0; i < num_pins; ++i)
5433                 pin_cfg.pins[i] = (int)lane_arr[i];
5434
5435         r = dsi_configure_pins(&dsi->output, &pin_cfg);
5436         if (r) {
5437                 dev_err(&pdev->dev, "failed to configure pins");
5438                 goto err;
5439         }
5440
5441         of_node_put(ep);
5442
5443         return 0;
5444
5445 err:
5446         of_node_put(ep);
5447         return r;
5448 }
5449
5450 /* DSI1 HW IP initialisation */
5451 static int omap_dsihw_probe(struct platform_device *dsidev)
5452 {
5453         u32 rev;
5454         int r, i;
5455         struct dsi_data *dsi;
5456         struct resource *dsi_mem;
5457         struct resource *res;
5458         struct resource temp_res;
5459
5460         dsi = devm_kzalloc(&dsidev->dev, sizeof(*dsi), GFP_KERNEL);
5461         if (!dsi)
5462                 return -ENOMEM;
5463
5464         dsi->pdev = dsidev;
5465         dev_set_drvdata(&dsidev->dev, dsi);
5466
5467         spin_lock_init(&dsi->irq_lock);
5468         spin_lock_init(&dsi->errors_lock);
5469         dsi->errors = 0;
5470
5471 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
5472         spin_lock_init(&dsi->irq_stats_lock);
5473         dsi->irq_stats.last_reset = jiffies;
5474 #endif
5475
5476         mutex_init(&dsi->lock);
5477         sema_init(&dsi->bus_lock, 1);
5478
5479         INIT_DEFERRABLE_WORK(&dsi->framedone_timeout_work,
5480                              dsi_framedone_timeout_work_callback);
5481
5482 #ifdef DSI_CATCH_MISSING_TE
5483         init_timer(&dsi->te_timer);
5484         dsi->te_timer.function = dsi_te_timeout;
5485         dsi->te_timer.data = 0;
5486 #endif
5487
5488         res = platform_get_resource_byname(dsidev, IORESOURCE_MEM, "proto");
5489         if (!res) {
5490                 res = platform_get_resource(dsidev, IORESOURCE_MEM, 0);
5491                 if (!res) {
5492                         DSSERR("can't get IORESOURCE_MEM DSI\n");
5493                         return -EINVAL;
5494                 }
5495
5496                 temp_res.start = res->start;
5497                 temp_res.end = temp_res.start + DSI_PROTO_SZ - 1;
5498                 res = &temp_res;
5499         }
5500
5501         dsi_mem = res;
5502
5503         dsi->proto_base = devm_ioremap(&dsidev->dev, res->start,
5504                 resource_size(res));
5505         if (!dsi->proto_base) {
5506                 DSSERR("can't ioremap DSI protocol engine\n");
5507                 return -ENOMEM;
5508         }
5509
5510         res = platform_get_resource_byname(dsidev, IORESOURCE_MEM, "phy");
5511         if (!res) {
5512                 res = platform_get_resource(dsidev, IORESOURCE_MEM, 0);
5513                 if (!res) {
5514                         DSSERR("can't get IORESOURCE_MEM DSI\n");
5515                         return -EINVAL;
5516                 }
5517
5518                 temp_res.start = res->start + DSI_PHY_OFFSET;
5519                 temp_res.end = temp_res.start + DSI_PHY_SZ - 1;
5520                 res = &temp_res;
5521         }
5522
5523         dsi->phy_base = devm_ioremap(&dsidev->dev, res->start,
5524                 resource_size(res));
5525         if (!dsi->proto_base) {
5526                 DSSERR("can't ioremap DSI PHY\n");
5527                 return -ENOMEM;
5528         }
5529
5530         res = platform_get_resource_byname(dsidev, IORESOURCE_MEM, "pll");
5531         if (!res) {
5532                 res = platform_get_resource(dsidev, IORESOURCE_MEM, 0);
5533                 if (!res) {
5534                         DSSERR("can't get IORESOURCE_MEM DSI\n");
5535                         return -EINVAL;
5536                 }
5537
5538                 temp_res.start = res->start + DSI_PLL_OFFSET;
5539                 temp_res.end = temp_res.start + DSI_PLL_SZ - 1;
5540                 res = &temp_res;
5541         }
5542
5543         dsi->pll_base = devm_ioremap(&dsidev->dev, res->start,
5544                 resource_size(res));
5545         if (!dsi->proto_base) {
5546                 DSSERR("can't ioremap DSI PLL\n");
5547                 return -ENOMEM;
5548         }
5549
5550         dsi->irq = platform_get_irq(dsi->pdev, 0);
5551         if (dsi->irq < 0) {
5552                 DSSERR("platform_get_irq failed\n");
5553                 return -ENODEV;
5554         }
5555
5556         r = devm_request_irq(&dsidev->dev, dsi->irq, omap_dsi_irq_handler,
5557                              IRQF_SHARED, dev_name(&dsidev->dev), dsi->pdev);
5558         if (r < 0) {
5559                 DSSERR("request_irq failed\n");
5560                 return r;
5561         }
5562
5563         if (dsidev->dev.of_node) {
5564                 const struct of_device_id *match;
5565                 const struct dsi_module_id_data *d;
5566
5567                 match = of_match_node(dsi_of_match, dsidev->dev.of_node);
5568                 if (!match) {
5569                         DSSERR("unsupported DSI module\n");
5570                         return -ENODEV;
5571                 }
5572
5573                 d = match->data;
5574
5575                 while (d->address != 0 && d->address != dsi_mem->start)
5576                         d++;
5577
5578                 if (d->address == 0) {
5579                         DSSERR("unsupported DSI module\n");
5580                         return -ENODEV;
5581                 }
5582
5583                 dsi->module_id = d->id;
5584         } else {
5585                 dsi->module_id = dsidev->id;
5586         }
5587
5588         /* DSI VCs initialization */
5589         for (i = 0; i < ARRAY_SIZE(dsi->vc); i++) {
5590                 dsi->vc[i].source = DSI_VC_SOURCE_L4;
5591                 dsi->vc[i].dssdev = NULL;
5592                 dsi->vc[i].vc_id = 0;
5593         }
5594
5595         dsi_calc_clock_param_ranges(dsidev);
5596
5597         r = dsi_get_clocks(dsidev);
5598         if (r)
5599                 return r;
5600
5601         pm_runtime_enable(&dsidev->dev);
5602
5603         r = dsi_runtime_get(dsidev);
5604         if (r)
5605                 goto err_runtime_get;
5606
5607         rev = dsi_read_reg(dsidev, DSI_REVISION);
5608         dev_dbg(&dsidev->dev, "OMAP DSI rev %d.%d\n",
5609                FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
5610
5611         /* DSI on OMAP3 doesn't have register DSI_GNQ, set number
5612          * of data to 3 by default */
5613         if (dss_has_feature(FEAT_DSI_GNQ))
5614                 /* NB_DATA_LANES */
5615                 dsi->num_lanes_supported = 1 + REG_GET(dsidev, DSI_GNQ, 11, 9);
5616         else
5617                 dsi->num_lanes_supported = 3;
5618
5619         dsi->line_buffer_size = dsi_get_line_buf_size(dsidev);
5620
5621         dsi_init_output(dsidev);
5622
5623         if (dsidev->dev.of_node) {
5624                 r = dsi_probe_of(dsidev);
5625                 if (r) {
5626                         DSSERR("Invalid DSI DT data\n");
5627                         goto err_probe_of;
5628                 }
5629
5630                 r = of_platform_populate(dsidev->dev.of_node, NULL, NULL,
5631                         &dsidev->dev);
5632                 if (r)
5633                         DSSERR("Failed to populate DSI child devices: %d\n", r);
5634         }
5635
5636         dsi_runtime_put(dsidev);
5637
5638         if (dsi->module_id == 0)
5639                 dss_debugfs_create_file("dsi1_regs", dsi1_dump_regs);
5640         else if (dsi->module_id == 1)
5641                 dss_debugfs_create_file("dsi2_regs", dsi2_dump_regs);
5642
5643 #ifdef CONFIG_OMAP2_DSS_COLLECT_IRQ_STATS
5644         if (dsi->module_id == 0)
5645                 dss_debugfs_create_file("dsi1_irqs", dsi1_dump_irqs);
5646         else if (dsi->module_id == 1)
5647                 dss_debugfs_create_file("dsi2_irqs", dsi2_dump_irqs);
5648 #endif
5649
5650         return 0;
5651
5652 err_probe_of:
5653         dsi_uninit_output(dsidev);
5654         dsi_runtime_put(dsidev);
5655
5656 err_runtime_get:
5657         pm_runtime_disable(&dsidev->dev);
5658         return r;
5659 }
5660
5661 static int __exit omap_dsihw_remove(struct platform_device *dsidev)
5662 {
5663         struct dsi_data *dsi = dsi_get_dsidrv_data(dsidev);
5664
5665         of_platform_depopulate(&dsidev->dev);
5666
5667         WARN_ON(dsi->scp_clk_refcount > 0);
5668
5669         dsi_uninit_output(dsidev);
5670
5671         pm_runtime_disable(&dsidev->dev);
5672
5673         if (dsi->vdds_dsi_reg != NULL && dsi->vdds_dsi_enabled) {
5674                 regulator_disable(dsi->vdds_dsi_reg);
5675                 dsi->vdds_dsi_enabled = false;
5676         }
5677
5678         return 0;
5679 }
5680
5681 static int dsi_runtime_suspend(struct device *dev)
5682 {
5683         struct platform_device *pdev = to_platform_device(dev);
5684         struct dsi_data *dsi = dsi_get_dsidrv_data(pdev);
5685
5686         dsi->is_enabled = false;
5687         /* ensure the irq handler sees the is_enabled value */
5688         smp_wmb();
5689         /* wait for current handler to finish before turning the DSI off */
5690         synchronize_irq(dsi->irq);
5691
5692         dispc_runtime_put();
5693
5694         return 0;
5695 }
5696
5697 static int dsi_runtime_resume(struct device *dev)
5698 {
5699         struct platform_device *pdev = to_platform_device(dev);
5700         struct dsi_data *dsi = dsi_get_dsidrv_data(pdev);
5701         int r;
5702
5703         r = dispc_runtime_get();
5704         if (r)
5705                 return r;
5706
5707         dsi->is_enabled = true;
5708         /* ensure the irq handler sees the is_enabled value */
5709         smp_wmb();
5710
5711         return 0;
5712 }
5713
5714 static const struct dev_pm_ops dsi_pm_ops = {
5715         .runtime_suspend = dsi_runtime_suspend,
5716         .runtime_resume = dsi_runtime_resume,
5717 };
5718
5719 static const struct dsi_module_id_data dsi_of_data_omap3[] = {
5720         { .address = 0x4804fc00, .id = 0, },
5721         { },
5722 };
5723
5724 static const struct dsi_module_id_data dsi_of_data_omap4[] = {
5725         { .address = 0x58004000, .id = 0, },
5726         { .address = 0x58005000, .id = 1, },
5727         { },
5728 };
5729
5730 static const struct dsi_module_id_data dsi_of_data_omap5[] = {
5731         { .address = 0x58004000, .id = 0, },
5732         { .address = 0x58009000, .id = 1, },
5733         { },
5734 };
5735
5736 static const struct of_device_id dsi_of_match[] = {
5737         { .compatible = "ti,omap3-dsi", .data = dsi_of_data_omap3, },
5738         { .compatible = "ti,omap4-dsi", .data = dsi_of_data_omap4, },
5739         { .compatible = "ti,omap5-dsi", .data = dsi_of_data_omap5, },
5740         {},
5741 };
5742
5743 static struct platform_driver omap_dsihw_driver = {
5744         .probe          = omap_dsihw_probe,
5745         .remove         = __exit_p(omap_dsihw_remove),
5746         .driver         = {
5747                 .name   = "omapdss_dsi",
5748                 .owner  = THIS_MODULE,
5749                 .pm     = &dsi_pm_ops,
5750                 .of_match_table = dsi_of_match,
5751         },
5752 };
5753
5754 int __init dsi_init_platform_driver(void)
5755 {
5756         return platform_driver_register(&omap_dsihw_driver);
5757 }
5758
5759 void __exit dsi_uninit_platform_driver(void)
5760 {
5761         platform_driver_unregister(&omap_dsihw_driver);
5762 }