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