Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Copyright © 2014 Intel Corporation
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 21 * DEALINGS IN THE SOFTWARE.
 22 *
 23 * Author: Shobhit Kumar <shobhit.kumar@intel.com>
 24 *
 25 */
 26
 27#include <drm/drmP.h>
 28#include <drm/drm_crtc.h>
 29#include <drm/drm_edid.h>
 30#include <drm/i915_drm.h>
 31#include <drm/drm_panel.h>
 32#include <linux/slab.h>
 33#include <video/mipi_display.h>
 34#include <asm/intel-mid.h>
 35#include <video/mipi_display.h>
 36#include "i915_drv.h"
 37#include "intel_drv.h"
 38#include "intel_dsi.h"
 39
 40struct vbt_panel {
 41	struct drm_panel panel;
 42	struct intel_dsi *intel_dsi;
 43};
 44
 45static inline struct vbt_panel *to_vbt_panel(struct drm_panel *panel)
 46{
 47	return container_of(panel, struct vbt_panel, panel);
 48}
 49
 50#define MIPI_TRANSFER_MODE_SHIFT	0
 51#define MIPI_VIRTUAL_CHANNEL_SHIFT	1
 52#define MIPI_PORT_SHIFT			3
 53
 54#define PREPARE_CNT_MAX		0x3F
 55#define EXIT_ZERO_CNT_MAX	0x3F
 56#define CLK_ZERO_CNT_MAX	0xFF
 57#define TRAIL_CNT_MAX		0x1F
 58
 59#define NS_KHZ_RATIO 1000000
 60
 61#define GPI0_NC_0_HV_DDI0_HPD           0x4130
 62#define GPIO_NC_0_HV_DDI0_PAD           0x4138
 63#define GPIO_NC_1_HV_DDI0_DDC_SDA       0x4120
 64#define GPIO_NC_1_HV_DDI0_DDC_SDA_PAD   0x4128
 65#define GPIO_NC_2_HV_DDI0_DDC_SCL       0x4110
 66#define GPIO_NC_2_HV_DDI0_DDC_SCL_PAD   0x4118
 67#define GPIO_NC_3_PANEL0_VDDEN          0x4140
 68#define GPIO_NC_3_PANEL0_VDDEN_PAD      0x4148
 69#define GPIO_NC_4_PANEL0_BLKEN          0x4150
 70#define GPIO_NC_4_PANEL0_BLKEN_PAD      0x4158
 71#define GPIO_NC_5_PANEL0_BLKCTL         0x4160
 72#define GPIO_NC_5_PANEL0_BLKCTL_PAD     0x4168
 73#define GPIO_NC_6_PCONF0                0x4180
 74#define GPIO_NC_6_PAD                   0x4188
 75#define GPIO_NC_7_PCONF0                0x4190
 76#define GPIO_NC_7_PAD                   0x4198
 77#define GPIO_NC_8_PCONF0                0x4170
 78#define GPIO_NC_8_PAD                   0x4178
 79#define GPIO_NC_9_PCONF0                0x4100
 80#define GPIO_NC_9_PAD                   0x4108
 81#define GPIO_NC_10_PCONF0               0x40E0
 82#define GPIO_NC_10_PAD                  0x40E8
 83#define GPIO_NC_11_PCONF0               0x40F0
 84#define GPIO_NC_11_PAD                  0x40F8
 85
 86struct gpio_table {
 87	u16 function_reg;
 88	u16 pad_reg;
 89	u8 init;
 90};
 91
 92static struct gpio_table gtable[] = {
 93	{ GPI0_NC_0_HV_DDI0_HPD, GPIO_NC_0_HV_DDI0_PAD, 0 },
 94	{ GPIO_NC_1_HV_DDI0_DDC_SDA, GPIO_NC_1_HV_DDI0_DDC_SDA_PAD, 0 },
 95	{ GPIO_NC_2_HV_DDI0_DDC_SCL, GPIO_NC_2_HV_DDI0_DDC_SCL_PAD, 0 },
 96	{ GPIO_NC_3_PANEL0_VDDEN, GPIO_NC_3_PANEL0_VDDEN_PAD, 0 },
 97	{ GPIO_NC_4_PANEL0_BLKEN, GPIO_NC_4_PANEL0_BLKEN_PAD, 0 },
 98	{ GPIO_NC_5_PANEL0_BLKCTL, GPIO_NC_5_PANEL0_BLKCTL_PAD, 0 },
 99	{ GPIO_NC_6_PCONF0, GPIO_NC_6_PAD, 0 },
100	{ GPIO_NC_7_PCONF0, GPIO_NC_7_PAD, 0 },
101	{ GPIO_NC_8_PCONF0, GPIO_NC_8_PAD, 0 },
102	{ GPIO_NC_9_PCONF0, GPIO_NC_9_PAD, 0 },
103	{ GPIO_NC_10_PCONF0, GPIO_NC_10_PAD, 0},
104	{ GPIO_NC_11_PCONF0, GPIO_NC_11_PAD, 0}
105};
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107static inline enum port intel_dsi_seq_port_to_port(u8 port)
108{
109	return port ? PORT_C : PORT_A;
110}
111
112static const u8 *mipi_exec_send_packet(struct intel_dsi *intel_dsi,
113				       const u8 *data)
114{
115	struct mipi_dsi_device *dsi_device;
116	u8 type, flags, seq_port;
117	u16 len;
118	enum port port;
119
 
 
120	flags = *data++;
121	type = *data++;
122
123	len = *((u16 *) data);
124	data += 2;
125
126	seq_port = (flags >> MIPI_PORT_SHIFT) & 3;
127
128	/* For DSI single link on Port A & C, the seq_port value which is
129	 * parsed from Sequence Block#53 of VBT has been set to 0
130	 * Now, read/write of packets for the DSI single link on Port A and
131	 * Port C will based on the DVO port from VBT block 2.
132	 */
133	if (intel_dsi->ports == (1 << PORT_C))
134		port = PORT_C;
135	else
136		port = intel_dsi_seq_port_to_port(seq_port);
137
138	dsi_device = intel_dsi->dsi_hosts[port]->device;
139	if (!dsi_device) {
140		DRM_DEBUG_KMS("no dsi device for port %c\n", port_name(port));
141		goto out;
142	}
143
144	if ((flags >> MIPI_TRANSFER_MODE_SHIFT) & 1)
145		dsi_device->mode_flags &= ~MIPI_DSI_MODE_LPM;
146	else
147		dsi_device->mode_flags |= MIPI_DSI_MODE_LPM;
148
149	dsi_device->channel = (flags >> MIPI_VIRTUAL_CHANNEL_SHIFT) & 3;
150
151	switch (type) {
152	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
153		mipi_dsi_generic_write(dsi_device, NULL, 0);
154		break;
155	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
156		mipi_dsi_generic_write(dsi_device, data, 1);
157		break;
158	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
159		mipi_dsi_generic_write(dsi_device, data, 2);
160		break;
161	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
162	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
163	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
164		DRM_DEBUG_DRIVER("Generic Read not yet implemented or used\n");
165		break;
166	case MIPI_DSI_GENERIC_LONG_WRITE:
167		mipi_dsi_generic_write(dsi_device, data, len);
168		break;
169	case MIPI_DSI_DCS_SHORT_WRITE:
170		mipi_dsi_dcs_write_buffer(dsi_device, data, 1);
171		break;
172	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
173		mipi_dsi_dcs_write_buffer(dsi_device, data, 2);
174		break;
175	case MIPI_DSI_DCS_READ:
176		DRM_DEBUG_DRIVER("DCS Read not yet implemented or used\n");
177		break;
178	case MIPI_DSI_DCS_LONG_WRITE:
179		mipi_dsi_dcs_write_buffer(dsi_device, data, len);
180		break;
181	}
182
183out:
184	data += len;
185
186	return data;
187}
188
189static const u8 *mipi_exec_delay(struct intel_dsi *intel_dsi, const u8 *data)
190{
191	u32 delay = *((const u32 *) data);
192
 
 
193	usleep_range(delay, delay + 10);
194	data += 4;
195
196	return data;
197}
198
199static const u8 *mipi_exec_gpio(struct intel_dsi *intel_dsi, const u8 *data)
 
200{
201	u8 gpio, action;
202	u16 function, pad;
203	u32 val;
204	struct drm_device *dev = intel_dsi->base.base.dev;
205	struct drm_i915_private *dev_priv = dev->dev_private;
206
207	if (dev_priv->vbt.dsi.seq_version >= 3)
208		data++;
209
210	gpio = *data++;
 
 
 
211
212	/* pull up/down */
213	action = *data++ & 1;
214
215	if (gpio >= ARRAY_SIZE(gtable)) {
216		DRM_DEBUG_KMS("unknown gpio %u\n", gpio);
217		goto out;
 
 
 
 
 
 
 
 
 
 
218	}
219
220	if (!IS_VALLEYVIEW(dev_priv)) {
221		DRM_DEBUG_KMS("GPIO element not supported on this platform\n");
222		goto out;
 
 
 
 
 
223	}
224
 
 
 
 
 
 
 
 
 
 
 
 
225	if (dev_priv->vbt.dsi.seq_version >= 3) {
226		DRM_DEBUG_KMS("GPIO element v3 not supported\n");
227		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228	}
229
230	function = gtable[gpio].function_reg;
231	pad = gtable[gpio].pad_reg;
 
 
 
232
233	mutex_lock(&dev_priv->sb_lock);
234	if (!gtable[gpio].init) {
235		/* program the function */
236		/* FIXME: remove constant below */
237		vlv_iosf_sb_write(dev_priv, IOSF_PORT_GPIO_NC, function,
238				  0x2000CC00);
239		gtable[gpio].init = 1;
240	}
 
 
 
 
 
 
 
 
 
 
 
241
242	val = 0x4 | action;
 
 
 
 
 
 
243
244	/* pull up/down */
245	vlv_iosf_sb_write(dev_priv, IOSF_PORT_GPIO_NC, pad, val);
246	mutex_unlock(&dev_priv->sb_lock);
 
 
 
 
 
 
247
248out:
249	return data;
250}
251
252static const u8 *mipi_exec_i2c_skip(struct intel_dsi *intel_dsi, const u8 *data)
253{
 
 
254	return data + *(data + 6) + 7;
255}
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257typedef const u8 * (*fn_mipi_elem_exec)(struct intel_dsi *intel_dsi,
258					const u8 *data);
259static const fn_mipi_elem_exec exec_elem[] = {
260	[MIPI_SEQ_ELEM_SEND_PKT] = mipi_exec_send_packet,
261	[MIPI_SEQ_ELEM_DELAY] = mipi_exec_delay,
262	[MIPI_SEQ_ELEM_GPIO] = mipi_exec_gpio,
263	[MIPI_SEQ_ELEM_I2C] = mipi_exec_i2c_skip,
 
 
264};
265
266/*
267 * MIPI Sequence from VBT #53 parsing logic
268 * We have already separated each seqence during bios parsing
269 * Following is generic execution function for any sequence
270 */
271
272static const char * const seq_name[] = {
273	[MIPI_SEQ_ASSERT_RESET] = "MIPI_SEQ_ASSERT_RESET",
274	[MIPI_SEQ_INIT_OTP] = "MIPI_SEQ_INIT_OTP",
275	[MIPI_SEQ_DISPLAY_ON] = "MIPI_SEQ_DISPLAY_ON",
276	[MIPI_SEQ_DISPLAY_OFF]  = "MIPI_SEQ_DISPLAY_OFF",
277	[MIPI_SEQ_DEASSERT_RESET] = "MIPI_SEQ_DEASSERT_RESET",
278	[MIPI_SEQ_BACKLIGHT_ON] = "MIPI_SEQ_BACKLIGHT_ON",
279	[MIPI_SEQ_BACKLIGHT_OFF] = "MIPI_SEQ_BACKLIGHT_OFF",
280	[MIPI_SEQ_TEAR_ON] = "MIPI_SEQ_TEAR_ON",
281	[MIPI_SEQ_TEAR_OFF] = "MIPI_SEQ_TEAR_OFF",
282	[MIPI_SEQ_POWER_ON] = "MIPI_SEQ_POWER_ON",
283	[MIPI_SEQ_POWER_OFF] = "MIPI_SEQ_POWER_OFF",
284};
285
286static const char *sequence_name(enum mipi_seq seq_id)
287{
288	if (seq_id < ARRAY_SIZE(seq_name) && seq_name[seq_id])
289		return seq_name[seq_id];
290	else
291		return "(unknown)";
292}
293
294static void generic_exec_sequence(struct drm_panel *panel, enum mipi_seq seq_id)
295{
296	struct vbt_panel *vbt_panel = to_vbt_panel(panel);
297	struct intel_dsi *intel_dsi = vbt_panel->intel_dsi;
298	struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
299	const u8 *data;
300	fn_mipi_elem_exec mipi_elem_exec;
301
302	if (WARN_ON(seq_id >= ARRAY_SIZE(dev_priv->vbt.dsi.sequence)))
303		return;
304
305	data = dev_priv->vbt.dsi.sequence[seq_id];
306	if (!data) {
307		DRM_DEBUG_KMS("MIPI sequence %d - %s not available\n",
308			      seq_id, sequence_name(seq_id));
309		return;
310	}
311
312	WARN_ON(*data != seq_id);
313
314	DRM_DEBUG_KMS("Starting MIPI sequence %d - %s\n",
315		      seq_id, sequence_name(seq_id));
316
317	/* Skip Sequence Byte. */
318	data++;
319
320	/* Skip Size of Sequence. */
321	if (dev_priv->vbt.dsi.seq_version >= 3)
322		data += 4;
323
324	while (1) {
325		u8 operation_byte = *data++;
326		u8 operation_size = 0;
327
328		if (operation_byte == MIPI_SEQ_ELEM_END)
329			break;
330
331		if (operation_byte < ARRAY_SIZE(exec_elem))
332			mipi_elem_exec = exec_elem[operation_byte];
333		else
334			mipi_elem_exec = NULL;
335
336		/* Size of Operation. */
337		if (dev_priv->vbt.dsi.seq_version >= 3)
338			operation_size = *data++;
339
340		if (mipi_elem_exec) {
 
 
341			data = mipi_elem_exec(intel_dsi, data);
 
 
 
 
 
 
342		} else if (operation_size) {
343			/* We have size, skip. */
344			DRM_DEBUG_KMS("Unsupported MIPI operation byte %u\n",
345				      operation_byte);
346			data += operation_size;
347		} else {
348			/* No size, can't skip without parsing. */
349			DRM_ERROR("Unsupported MIPI operation byte %u\n",
350				  operation_byte);
351			return;
352		}
353	}
354}
355
356static int vbt_panel_prepare(struct drm_panel *panel)
357{
358	generic_exec_sequence(panel, MIPI_SEQ_ASSERT_RESET);
 
 
359	generic_exec_sequence(panel, MIPI_SEQ_INIT_OTP);
360
361	return 0;
362}
363
364static int vbt_panel_unprepare(struct drm_panel *panel)
365{
366	generic_exec_sequence(panel, MIPI_SEQ_DEASSERT_RESET);
 
367
368	return 0;
369}
370
371static int vbt_panel_enable(struct drm_panel *panel)
372{
373	generic_exec_sequence(panel, MIPI_SEQ_DISPLAY_ON);
 
374
375	return 0;
376}
377
378static int vbt_panel_disable(struct drm_panel *panel)
379{
 
380	generic_exec_sequence(panel, MIPI_SEQ_DISPLAY_OFF);
381
382	return 0;
383}
384
385static int vbt_panel_get_modes(struct drm_panel *panel)
386{
387	struct vbt_panel *vbt_panel = to_vbt_panel(panel);
388	struct intel_dsi *intel_dsi = vbt_panel->intel_dsi;
389	struct drm_device *dev = intel_dsi->base.base.dev;
390	struct drm_i915_private *dev_priv = dev->dev_private;
391	struct drm_display_mode *mode;
392
393	if (!panel->connector)
394		return 0;
395
396	mode = drm_mode_duplicate(dev, dev_priv->vbt.lfp_lvds_vbt_mode);
397	if (!mode)
398		return 0;
399
400	mode->type |= DRM_MODE_TYPE_PREFERRED;
401
402	drm_mode_probed_add(panel->connector, mode);
403
404	return 1;
405}
406
407static const struct drm_panel_funcs vbt_panel_funcs = {
408	.disable = vbt_panel_disable,
409	.unprepare = vbt_panel_unprepare,
410	.prepare = vbt_panel_prepare,
411	.enable = vbt_panel_enable,
412	.get_modes = vbt_panel_get_modes,
413};
414
415struct drm_panel *vbt_panel_init(struct intel_dsi *intel_dsi, u16 panel_id)
416{
417	struct drm_device *dev = intel_dsi->base.base.dev;
418	struct drm_i915_private *dev_priv = dev->dev_private;
419	struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
420	struct mipi_pps_data *pps = dev_priv->vbt.dsi.pps;
421	struct drm_display_mode *mode = dev_priv->vbt.lfp_lvds_vbt_mode;
422	struct vbt_panel *vbt_panel;
423	u32 bits_per_pixel = 24;
424	u32 tlpx_ns, extra_byte_count, bitrate, tlpx_ui;
425	u32 ui_num, ui_den;
426	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
427	u32 ths_prepare_ns, tclk_trail_ns;
428	u32 tclk_prepare_clkzero, ths_prepare_hszero;
429	u32 lp_to_hs_switch, hs_to_lp_switch;
430	u32 pclk, computed_ddr;
431	u16 burst_mode_ratio;
432	enum port port;
433
434	DRM_DEBUG_KMS("\n");
435
436	intel_dsi->eotp_pkt = mipi_config->eot_pkt_disabled ? 0 : 1;
437	intel_dsi->clock_stop = mipi_config->enable_clk_stop ? 1 : 0;
438	intel_dsi->lane_count = mipi_config->lane_cnt + 1;
439	intel_dsi->pixel_format = mipi_config->videomode_color_format << 7;
 
 
 
 
440	intel_dsi->dual_link = mipi_config->dual_link;
441	intel_dsi->pixel_overlap = mipi_config->pixel_overlap;
442
443	bits_per_pixel = dsi_pixel_format_bpp(intel_dsi->pixel_format);
444
445	intel_dsi->operation_mode = mipi_config->is_cmd_mode;
446	intel_dsi->video_mode_format = mipi_config->video_transfer_mode;
447	intel_dsi->escape_clk_div = mipi_config->byte_clk_sel;
448	intel_dsi->lp_rx_timeout = mipi_config->lp_rx_timeout;
449	intel_dsi->turn_arnd_val = mipi_config->turn_around_timeout;
450	intel_dsi->rst_timer_val = mipi_config->device_reset_timer;
451	intel_dsi->init_count = mipi_config->master_init_timer;
452	intel_dsi->bw_timer = mipi_config->dbi_bw_timer;
453	intel_dsi->video_frmt_cfg_bits =
454		mipi_config->bta_enabled ? DISABLE_VIDEO_BTA : 0;
455
456	pclk = mode->clock;
457
458	/* In dual link mode each port needs half of pixel clock */
459	if (intel_dsi->dual_link) {
460		pclk = pclk / 2;
461
462		/* we can enable pixel_overlap if needed by panel. In this
463		 * case we need to increase the pixelclock for extra pixels
464		 */
465		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
466			pclk += DIV_ROUND_UP(mode->vtotal *
467						intel_dsi->pixel_overlap *
468						60, 1000);
469		}
470	}
471
472	/* Burst Mode Ratio
473	 * Target ddr frequency from VBT / non burst ddr freq
474	 * multiply by 100 to preserve remainder
475	 */
476	if (intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
477		if (mipi_config->target_burst_mode_freq) {
478			computed_ddr =
479				(pclk * bits_per_pixel) / intel_dsi->lane_count;
480
481			if (mipi_config->target_burst_mode_freq <
482								computed_ddr) {
483				DRM_ERROR("Burst mode freq is less than computed\n");
484				return NULL;
485			}
486
487			burst_mode_ratio = DIV_ROUND_UP(
488				mipi_config->target_burst_mode_freq * 100,
489				computed_ddr);
490
491			pclk = DIV_ROUND_UP(pclk * burst_mode_ratio, 100);
492		} else {
493			DRM_ERROR("Burst mode target is not set\n");
494			return NULL;
495		}
496	} else
497		burst_mode_ratio = 100;
498
499	intel_dsi->burst_mode_ratio = burst_mode_ratio;
500	intel_dsi->pclk = pclk;
501
502	bitrate = (pclk * bits_per_pixel) / intel_dsi->lane_count;
503
504	switch (intel_dsi->escape_clk_div) {
505	case 0:
506		tlpx_ns = 50;
507		break;
508	case 1:
509		tlpx_ns = 100;
510		break;
511
512	case 2:
513		tlpx_ns = 200;
514		break;
515	default:
516		tlpx_ns = 50;
517		break;
518	}
519
520	switch (intel_dsi->lane_count) {
521	case 1:
522	case 2:
523		extra_byte_count = 2;
524		break;
525	case 3:
526		extra_byte_count = 4;
527		break;
528	case 4:
529	default:
530		extra_byte_count = 3;
531		break;
532	}
533
534	/*
535	 * ui(s) = 1/f [f in hz]
536	 * ui(ns) = 10^9 / (f*10^6) [f in Mhz] -> 10^3/f(Mhz)
537	 */
538
539	/* in Kbps */
540	ui_num = NS_KHZ_RATIO;
541	ui_den = bitrate;
542
543	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
544	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
545
546	/*
547	 * B060
548	 * LP byte clock = TLPX/ (8UI)
549	 */
550	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
551
552	/* count values in UI = (ns value) * (bitrate / (2 * 10^6))
553	 *
554	 * Since txddrclkhs_i is 2xUI, all the count values programmed in
555	 * DPHY param register are divided by 2
556	 *
557	 * prepare count
558	 */
559	ths_prepare_ns = max(mipi_config->ths_prepare,
560			     mipi_config->tclk_prepare);
561	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * 2);
562
563	/* exit zero count */
564	exit_zero_cnt = DIV_ROUND_UP(
565				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
566				ui_num * 2
567				);
568
569	/*
570	 * Exit zero  is unified val ths_zero and ths_exit
571	 * minimum value for ths_exit = 110ns
572	 * min (exit_zero_cnt * 2) = 110/UI
573	 * exit_zero_cnt = 55/UI
574	 */
575	 if (exit_zero_cnt < (55 * ui_den / ui_num))
576		if ((55 * ui_den) % ui_num)
577			exit_zero_cnt += 1;
578
579	/* clk zero count */
580	clk_zero_cnt = DIV_ROUND_UP(
581			(tclk_prepare_clkzero -	ths_prepare_ns)
582			* ui_den, 2 * ui_num);
583
584	/* trail count */
585	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
586	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, 2 * ui_num);
587
588	if (prepare_cnt > PREPARE_CNT_MAX ||
589		exit_zero_cnt > EXIT_ZERO_CNT_MAX ||
590		clk_zero_cnt > CLK_ZERO_CNT_MAX ||
591		trail_cnt > TRAIL_CNT_MAX)
592		DRM_DEBUG_DRIVER("Values crossing maximum limits, restricting to max values\n");
593
594	if (prepare_cnt > PREPARE_CNT_MAX)
595		prepare_cnt = PREPARE_CNT_MAX;
596
597	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX)
598		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
599
600	if (clk_zero_cnt > CLK_ZERO_CNT_MAX)
601		clk_zero_cnt = CLK_ZERO_CNT_MAX;
602
603	if (trail_cnt > TRAIL_CNT_MAX)
604		trail_cnt = TRAIL_CNT_MAX;
605
606	/* B080 */
607	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
608						clk_zero_cnt << 8 | prepare_cnt;
609
610	/*
611	 * LP to HS switch count = 4TLPX + PREP_COUNT * 2 + EXIT_ZERO_COUNT * 2
612	 *					+ 10UI + Extra Byte Count
613	 *
614	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
615	 * Extra Byte Count is calculated according to number of lanes.
616	 * High Low Switch Count is the Max of LP to HS and
617	 * HS to LP switch count
618	 *
619	 */
620	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
621
622	/* B044 */
623	/* FIXME:
624	 * The comment above does not match with the code */
625	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * 2 +
626						exit_zero_cnt * 2 + 10, 8);
627
628	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
629
630	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
631	intel_dsi->hs_to_lp_count += extra_byte_count;
632
633	/* B088 */
634	/* LP -> HS for clock lanes
635	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
636	 *						extra byte count
637	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
638	 *					2(in UI) + extra byte count
639	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
640	 *					8 + extra byte count
641	 */
642	intel_dsi->clk_lp_to_hs_count =
643		DIV_ROUND_UP(
644			4 * tlpx_ui + prepare_cnt * 2 +
645			clk_zero_cnt * 2,
646			8);
647
648	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
649
650	/* HS->LP for Clock Lanes
651	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
652	 *						Extra byte count
653	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
654	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
655	 *						Extra byte count
656	 */
657	intel_dsi->clk_hs_to_lp_count =
658		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
659			8);
660	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
661
662	DRM_DEBUG_KMS("Eot %s\n", intel_dsi->eotp_pkt ? "enabled" : "disabled");
663	DRM_DEBUG_KMS("Clockstop %s\n", intel_dsi->clock_stop ?
664						"disabled" : "enabled");
665	DRM_DEBUG_KMS("Mode %s\n", intel_dsi->operation_mode ? "command" : "video");
666	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
667		DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_FRONT_BACK\n");
668	else if (intel_dsi->dual_link == DSI_DUAL_LINK_PIXEL_ALT)
669		DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_PIXEL_ALT\n");
670	else
671		DRM_DEBUG_KMS("Dual link: NONE\n");
672	DRM_DEBUG_KMS("Pixel Format %d\n", intel_dsi->pixel_format);
673	DRM_DEBUG_KMS("TLPX %d\n", intel_dsi->escape_clk_div);
674	DRM_DEBUG_KMS("LP RX Timeout 0x%x\n", intel_dsi->lp_rx_timeout);
675	DRM_DEBUG_KMS("Turnaround Timeout 0x%x\n", intel_dsi->turn_arnd_val);
676	DRM_DEBUG_KMS("Init Count 0x%x\n", intel_dsi->init_count);
677	DRM_DEBUG_KMS("HS to LP Count 0x%x\n", intel_dsi->hs_to_lp_count);
678	DRM_DEBUG_KMS("LP Byte Clock %d\n", intel_dsi->lp_byte_clk);
679	DRM_DEBUG_KMS("DBI BW Timer 0x%x\n", intel_dsi->bw_timer);
680	DRM_DEBUG_KMS("LP to HS Clock Count 0x%x\n", intel_dsi->clk_lp_to_hs_count);
681	DRM_DEBUG_KMS("HS to LP Clock Count 0x%x\n", intel_dsi->clk_hs_to_lp_count);
682	DRM_DEBUG_KMS("BTA %s\n",
683			intel_dsi->video_frmt_cfg_bits & DISABLE_VIDEO_BTA ?
684			"disabled" : "enabled");
685
686	/* delays in VBT are in unit of 100us, so need to convert
687	 * here in ms
688	 * Delay (100us) * 100 /1000 = Delay / 10 (ms) */
689	intel_dsi->backlight_off_delay = pps->bl_disable_delay / 10;
690	intel_dsi->backlight_on_delay = pps->bl_enable_delay / 10;
691	intel_dsi->panel_on_delay = pps->panel_on_delay / 10;
692	intel_dsi->panel_off_delay = pps->panel_off_delay / 10;
693	intel_dsi->panel_pwr_cycle_delay = pps->panel_power_cycle_delay / 10;
694
695	/* This is cheating a bit with the cleanup. */
696	vbt_panel = devm_kzalloc(dev->dev, sizeof(*vbt_panel), GFP_KERNEL);
697	if (!vbt_panel)
698		return NULL;
699
700	vbt_panel->intel_dsi = intel_dsi;
701	drm_panel_init(&vbt_panel->panel);
702	vbt_panel->panel.funcs = &vbt_panel_funcs;
703	drm_panel_add(&vbt_panel->panel);
704
705	/* a regular driver would get the device in probe */
706	for_each_dsi_port(port, intel_dsi->ports) {
707		mipi_dsi_attach(intel_dsi->dsi_hosts[port]->device);
708	}
709
710	return &vbt_panel->panel;
711}
v4.10.11
  1/*
  2 * Copyright © 2014 Intel Corporation
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice (including the next
 12 * paragraph) shall be included in all copies or substantial portions of the
 13 * Software.
 14 *
 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 21 * DEALINGS IN THE SOFTWARE.
 22 *
 23 * Author: Shobhit Kumar <shobhit.kumar@intel.com>
 24 *
 25 */
 26
 27#include <drm/drmP.h>
 28#include <drm/drm_crtc.h>
 29#include <drm/drm_edid.h>
 30#include <drm/i915_drm.h>
 31#include <drm/drm_panel.h>
 32#include <linux/slab.h>
 33#include <video/mipi_display.h>
 34#include <asm/intel-mid.h>
 35#include <video/mipi_display.h>
 36#include "i915_drv.h"
 37#include "intel_drv.h"
 38#include "intel_dsi.h"
 39
 40struct vbt_panel {
 41	struct drm_panel panel;
 42	struct intel_dsi *intel_dsi;
 43};
 44
 45static inline struct vbt_panel *to_vbt_panel(struct drm_panel *panel)
 46{
 47	return container_of(panel, struct vbt_panel, panel);
 48}
 49
 50#define MIPI_TRANSFER_MODE_SHIFT	0
 51#define MIPI_VIRTUAL_CHANNEL_SHIFT	1
 52#define MIPI_PORT_SHIFT			3
 53
 54#define PREPARE_CNT_MAX		0x3F
 55#define EXIT_ZERO_CNT_MAX	0x3F
 56#define CLK_ZERO_CNT_MAX	0xFF
 57#define TRAIL_CNT_MAX		0x1F
 58
 59#define NS_KHZ_RATIO 1000000
 60
 61/* base offsets for gpio pads */
 62#define VLV_GPIO_NC_0_HV_DDI0_HPD	0x4130
 63#define VLV_GPIO_NC_1_HV_DDI0_DDC_SDA	0x4120
 64#define VLV_GPIO_NC_2_HV_DDI0_DDC_SCL	0x4110
 65#define VLV_GPIO_NC_3_PANEL0_VDDEN	0x4140
 66#define VLV_GPIO_NC_4_PANEL0_BKLTEN	0x4150
 67#define VLV_GPIO_NC_5_PANEL0_BKLTCTL	0x4160
 68#define VLV_GPIO_NC_6_HV_DDI1_HPD	0x4180
 69#define VLV_GPIO_NC_7_HV_DDI1_DDC_SDA	0x4190
 70#define VLV_GPIO_NC_8_HV_DDI1_DDC_SCL	0x4170
 71#define VLV_GPIO_NC_9_PANEL1_VDDEN	0x4100
 72#define VLV_GPIO_NC_10_PANEL1_BKLTEN	0x40E0
 73#define VLV_GPIO_NC_11_PANEL1_BKLTCTL	0x40F0
 74
 75#define VLV_GPIO_PCONF0(base_offset)	(base_offset)
 76#define VLV_GPIO_PAD_VAL(base_offset)	((base_offset) + 8)
 77
 78struct gpio_map {
 79	u16 base_offset;
 80	bool init;
 
 
 
 
 
 
 
 
 
 81};
 82
 83static struct gpio_map vlv_gpio_table[] = {
 84	{ VLV_GPIO_NC_0_HV_DDI0_HPD },
 85	{ VLV_GPIO_NC_1_HV_DDI0_DDC_SDA },
 86	{ VLV_GPIO_NC_2_HV_DDI0_DDC_SCL },
 87	{ VLV_GPIO_NC_3_PANEL0_VDDEN },
 88	{ VLV_GPIO_NC_4_PANEL0_BKLTEN },
 89	{ VLV_GPIO_NC_5_PANEL0_BKLTCTL },
 90	{ VLV_GPIO_NC_6_HV_DDI1_HPD },
 91	{ VLV_GPIO_NC_7_HV_DDI1_DDC_SDA },
 92	{ VLV_GPIO_NC_8_HV_DDI1_DDC_SCL },
 93	{ VLV_GPIO_NC_9_PANEL1_VDDEN },
 94	{ VLV_GPIO_NC_10_PANEL1_BKLTEN },
 95	{ VLV_GPIO_NC_11_PANEL1_BKLTCTL },
 96};
 97
 98#define CHV_GPIO_IDX_START_N		0
 99#define CHV_GPIO_IDX_START_E		73
100#define CHV_GPIO_IDX_START_SW		100
101#define CHV_GPIO_IDX_START_SE		198
102
103#define CHV_VBT_MAX_PINS_PER_FMLY	15
104
105#define CHV_GPIO_PAD_CFG0(f, i)		(0x4400 + (f) * 0x400 + (i) * 8)
106#define  CHV_GPIO_GPIOEN		(1 << 15)
107#define  CHV_GPIO_GPIOCFG_GPIO		(0 << 8)
108#define  CHV_GPIO_GPIOCFG_GPO		(1 << 8)
109#define  CHV_GPIO_GPIOCFG_GPI		(2 << 8)
110#define  CHV_GPIO_GPIOCFG_HIZ		(3 << 8)
111#define  CHV_GPIO_GPIOTXSTATE(state)	((!!(state)) << 1)
112
113#define CHV_GPIO_PAD_CFG1(f, i)		(0x4400 + (f) * 0x400 + (i) * 8 + 4)
114#define  CHV_GPIO_CFGLOCK		(1 << 31)
115
116static inline enum port intel_dsi_seq_port_to_port(u8 port)
117{
118	return port ? PORT_C : PORT_A;
119}
120
121static const u8 *mipi_exec_send_packet(struct intel_dsi *intel_dsi,
122				       const u8 *data)
123{
124	struct mipi_dsi_device *dsi_device;
125	u8 type, flags, seq_port;
126	u16 len;
127	enum port port;
128
129	DRM_DEBUG_KMS("\n");
130
131	flags = *data++;
132	type = *data++;
133
134	len = *((u16 *) data);
135	data += 2;
136
137	seq_port = (flags >> MIPI_PORT_SHIFT) & 3;
138
139	/* For DSI single link on Port A & C, the seq_port value which is
140	 * parsed from Sequence Block#53 of VBT has been set to 0
141	 * Now, read/write of packets for the DSI single link on Port A and
142	 * Port C will based on the DVO port from VBT block 2.
143	 */
144	if (intel_dsi->ports == (1 << PORT_C))
145		port = PORT_C;
146	else
147		port = intel_dsi_seq_port_to_port(seq_port);
148
149	dsi_device = intel_dsi->dsi_hosts[port]->device;
150	if (!dsi_device) {
151		DRM_DEBUG_KMS("no dsi device for port %c\n", port_name(port));
152		goto out;
153	}
154
155	if ((flags >> MIPI_TRANSFER_MODE_SHIFT) & 1)
156		dsi_device->mode_flags &= ~MIPI_DSI_MODE_LPM;
157	else
158		dsi_device->mode_flags |= MIPI_DSI_MODE_LPM;
159
160	dsi_device->channel = (flags >> MIPI_VIRTUAL_CHANNEL_SHIFT) & 3;
161
162	switch (type) {
163	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
164		mipi_dsi_generic_write(dsi_device, NULL, 0);
165		break;
166	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
167		mipi_dsi_generic_write(dsi_device, data, 1);
168		break;
169	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
170		mipi_dsi_generic_write(dsi_device, data, 2);
171		break;
172	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
173	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
174	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
175		DRM_DEBUG_DRIVER("Generic Read not yet implemented or used\n");
176		break;
177	case MIPI_DSI_GENERIC_LONG_WRITE:
178		mipi_dsi_generic_write(dsi_device, data, len);
179		break;
180	case MIPI_DSI_DCS_SHORT_WRITE:
181		mipi_dsi_dcs_write_buffer(dsi_device, data, 1);
182		break;
183	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
184		mipi_dsi_dcs_write_buffer(dsi_device, data, 2);
185		break;
186	case MIPI_DSI_DCS_READ:
187		DRM_DEBUG_DRIVER("DCS Read not yet implemented or used\n");
188		break;
189	case MIPI_DSI_DCS_LONG_WRITE:
190		mipi_dsi_dcs_write_buffer(dsi_device, data, len);
191		break;
192	}
193
194out:
195	data += len;
196
197	return data;
198}
199
200static const u8 *mipi_exec_delay(struct intel_dsi *intel_dsi, const u8 *data)
201{
202	u32 delay = *((const u32 *) data);
203
204	DRM_DEBUG_KMS("\n");
205
206	usleep_range(delay, delay + 10);
207	data += 4;
208
209	return data;
210}
211
212static void vlv_exec_gpio(struct drm_i915_private *dev_priv,
213			  u8 gpio_source, u8 gpio_index, bool value)
214{
215	struct gpio_map *map;
216	u16 pconf0, padval;
217	u32 tmp;
218	u8 port;
 
 
 
 
219
220	if (gpio_index >= ARRAY_SIZE(vlv_gpio_table)) {
221		DRM_DEBUG_KMS("unknown gpio index %u\n", gpio_index);
222		return;
223	}
224
225	map = &vlv_gpio_table[gpio_index];
 
226
227	if (dev_priv->vbt.dsi.seq_version >= 3) {
228		/* XXX: this assumes vlv_gpio_table only has NC GPIOs. */
229		port = IOSF_PORT_GPIO_NC;
230	} else {
231		if (gpio_source == 0) {
232			port = IOSF_PORT_GPIO_NC;
233		} else if (gpio_source == 1) {
234			DRM_DEBUG_KMS("SC gpio not supported\n");
235			return;
236		} else {
237			DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
238			return;
239		}
240	}
241
242	pconf0 = VLV_GPIO_PCONF0(map->base_offset);
243	padval = VLV_GPIO_PAD_VAL(map->base_offset);
244
245	mutex_lock(&dev_priv->sb_lock);
246	if (!map->init) {
247		/* FIXME: remove constant below */
248		vlv_iosf_sb_write(dev_priv, port, pconf0, 0x2000CC00);
249		map->init = true;
250	}
251
252	tmp = 0x4 | value;
253	vlv_iosf_sb_write(dev_priv, port, padval, tmp);
254	mutex_unlock(&dev_priv->sb_lock);
255}
256
257static void chv_exec_gpio(struct drm_i915_private *dev_priv,
258			  u8 gpio_source, u8 gpio_index, bool value)
259{
260	u16 cfg0, cfg1;
261	u16 family_num;
262	u8 port;
263
264	if (dev_priv->vbt.dsi.seq_version >= 3) {
265		if (gpio_index >= CHV_GPIO_IDX_START_SE) {
266			/* XXX: it's unclear whether 255->57 is part of SE. */
267			gpio_index -= CHV_GPIO_IDX_START_SE;
268			port = CHV_IOSF_PORT_GPIO_SE;
269		} else if (gpio_index >= CHV_GPIO_IDX_START_SW) {
270			gpio_index -= CHV_GPIO_IDX_START_SW;
271			port = CHV_IOSF_PORT_GPIO_SW;
272		} else if (gpio_index >= CHV_GPIO_IDX_START_E) {
273			gpio_index -= CHV_GPIO_IDX_START_E;
274			port = CHV_IOSF_PORT_GPIO_E;
275		} else {
276			port = CHV_IOSF_PORT_GPIO_N;
277		}
278	} else {
279		/* XXX: The spec is unclear about CHV GPIO on seq v2 */
280		if (gpio_source != 0) {
281			DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
282			return;
283		}
284
285		if (gpio_index >= CHV_GPIO_IDX_START_E) {
286			DRM_DEBUG_KMS("invalid gpio index %u for GPIO N\n",
287				      gpio_index);
288			return;
289		}
290
291		port = CHV_IOSF_PORT_GPIO_N;
292	}
293
294	family_num = gpio_index / CHV_VBT_MAX_PINS_PER_FMLY;
295	gpio_index = gpio_index % CHV_VBT_MAX_PINS_PER_FMLY;
296
297	cfg0 = CHV_GPIO_PAD_CFG0(family_num, gpio_index);
298	cfg1 = CHV_GPIO_PAD_CFG1(family_num, gpio_index);
299
300	mutex_lock(&dev_priv->sb_lock);
301	vlv_iosf_sb_write(dev_priv, port, cfg1, 0);
302	vlv_iosf_sb_write(dev_priv, port, cfg0,
303			  CHV_GPIO_GPIOEN | CHV_GPIO_GPIOCFG_GPO |
304			  CHV_GPIO_GPIOTXSTATE(value));
305	mutex_unlock(&dev_priv->sb_lock);
306}
307
308static const u8 *mipi_exec_gpio(struct intel_dsi *intel_dsi, const u8 *data)
309{
310	struct drm_device *dev = intel_dsi->base.base.dev;
311	struct drm_i915_private *dev_priv = to_i915(dev);
312	u8 gpio_source, gpio_index;
313	bool value;
314
315	DRM_DEBUG_KMS("\n");
316
317	if (dev_priv->vbt.dsi.seq_version >= 3)
318		data++;
319
320	gpio_index = *data++;
321
322	/* gpio source in sequence v2 only */
323	if (dev_priv->vbt.dsi.seq_version == 2)
324		gpio_source = (*data >> 1) & 3;
325	else
326		gpio_source = 0;
327
328	/* pull up/down */
329	value = *data++ & 1;
330
331	if (IS_VALLEYVIEW(dev_priv))
332		vlv_exec_gpio(dev_priv, gpio_source, gpio_index, value);
333	else if (IS_CHERRYVIEW(dev_priv))
334		chv_exec_gpio(dev_priv, gpio_source, gpio_index, value);
335	else
336		DRM_DEBUG_KMS("GPIO element not supported on this platform\n");
337
 
338	return data;
339}
340
341static const u8 *mipi_exec_i2c(struct intel_dsi *intel_dsi, const u8 *data)
342{
343	DRM_DEBUG_KMS("Skipping I2C element execution\n");
344
345	return data + *(data + 6) + 7;
346}
347
348static const u8 *mipi_exec_spi(struct intel_dsi *intel_dsi, const u8 *data)
349{
350	DRM_DEBUG_KMS("Skipping SPI element execution\n");
351
352	return data + *(data + 5) + 6;
353}
354
355static const u8 *mipi_exec_pmic(struct intel_dsi *intel_dsi, const u8 *data)
356{
357	DRM_DEBUG_KMS("Skipping PMIC element execution\n");
358
359	return data + 15;
360}
361
362typedef const u8 * (*fn_mipi_elem_exec)(struct intel_dsi *intel_dsi,
363					const u8 *data);
364static const fn_mipi_elem_exec exec_elem[] = {
365	[MIPI_SEQ_ELEM_SEND_PKT] = mipi_exec_send_packet,
366	[MIPI_SEQ_ELEM_DELAY] = mipi_exec_delay,
367	[MIPI_SEQ_ELEM_GPIO] = mipi_exec_gpio,
368	[MIPI_SEQ_ELEM_I2C] = mipi_exec_i2c,
369	[MIPI_SEQ_ELEM_SPI] = mipi_exec_spi,
370	[MIPI_SEQ_ELEM_PMIC] = mipi_exec_pmic,
371};
372
373/*
374 * MIPI Sequence from VBT #53 parsing logic
375 * We have already separated each seqence during bios parsing
376 * Following is generic execution function for any sequence
377 */
378
379static const char * const seq_name[] = {
380	[MIPI_SEQ_DEASSERT_RESET] = "MIPI_SEQ_DEASSERT_RESET",
381	[MIPI_SEQ_INIT_OTP] = "MIPI_SEQ_INIT_OTP",
382	[MIPI_SEQ_DISPLAY_ON] = "MIPI_SEQ_DISPLAY_ON",
383	[MIPI_SEQ_DISPLAY_OFF]  = "MIPI_SEQ_DISPLAY_OFF",
384	[MIPI_SEQ_ASSERT_RESET] = "MIPI_SEQ_ASSERT_RESET",
385	[MIPI_SEQ_BACKLIGHT_ON] = "MIPI_SEQ_BACKLIGHT_ON",
386	[MIPI_SEQ_BACKLIGHT_OFF] = "MIPI_SEQ_BACKLIGHT_OFF",
387	[MIPI_SEQ_TEAR_ON] = "MIPI_SEQ_TEAR_ON",
388	[MIPI_SEQ_TEAR_OFF] = "MIPI_SEQ_TEAR_OFF",
389	[MIPI_SEQ_POWER_ON] = "MIPI_SEQ_POWER_ON",
390	[MIPI_SEQ_POWER_OFF] = "MIPI_SEQ_POWER_OFF",
391};
392
393static const char *sequence_name(enum mipi_seq seq_id)
394{
395	if (seq_id < ARRAY_SIZE(seq_name) && seq_name[seq_id])
396		return seq_name[seq_id];
397	else
398		return "(unknown)";
399}
400
401static void generic_exec_sequence(struct drm_panel *panel, enum mipi_seq seq_id)
402{
403	struct vbt_panel *vbt_panel = to_vbt_panel(panel);
404	struct intel_dsi *intel_dsi = vbt_panel->intel_dsi;
405	struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
406	const u8 *data;
407	fn_mipi_elem_exec mipi_elem_exec;
408
409	if (WARN_ON(seq_id >= ARRAY_SIZE(dev_priv->vbt.dsi.sequence)))
410		return;
411
412	data = dev_priv->vbt.dsi.sequence[seq_id];
413	if (!data)
 
 
414		return;
 
415
416	WARN_ON(*data != seq_id);
417
418	DRM_DEBUG_KMS("Starting MIPI sequence %d - %s\n",
419		      seq_id, sequence_name(seq_id));
420
421	/* Skip Sequence Byte. */
422	data++;
423
424	/* Skip Size of Sequence. */
425	if (dev_priv->vbt.dsi.seq_version >= 3)
426		data += 4;
427
428	while (1) {
429		u8 operation_byte = *data++;
430		u8 operation_size = 0;
431
432		if (operation_byte == MIPI_SEQ_ELEM_END)
433			break;
434
435		if (operation_byte < ARRAY_SIZE(exec_elem))
436			mipi_elem_exec = exec_elem[operation_byte];
437		else
438			mipi_elem_exec = NULL;
439
440		/* Size of Operation. */
441		if (dev_priv->vbt.dsi.seq_version >= 3)
442			operation_size = *data++;
443
444		if (mipi_elem_exec) {
445			const u8 *next = data + operation_size;
446
447			data = mipi_elem_exec(intel_dsi, data);
448
449			/* Consistency check if we have size. */
450			if (operation_size && data != next) {
451				DRM_ERROR("Inconsistent operation size\n");
452				return;
453			}
454		} else if (operation_size) {
455			/* We have size, skip. */
456			DRM_DEBUG_KMS("Unsupported MIPI operation byte %u\n",
457				      operation_byte);
458			data += operation_size;
459		} else {
460			/* No size, can't skip without parsing. */
461			DRM_ERROR("Unsupported MIPI operation byte %u\n",
462				  operation_byte);
463			return;
464		}
465	}
466}
467
468static int vbt_panel_prepare(struct drm_panel *panel)
469{
470	generic_exec_sequence(panel, MIPI_SEQ_ASSERT_RESET);
471	generic_exec_sequence(panel, MIPI_SEQ_POWER_ON);
472	generic_exec_sequence(panel, MIPI_SEQ_DEASSERT_RESET);
473	generic_exec_sequence(panel, MIPI_SEQ_INIT_OTP);
474
475	return 0;
476}
477
478static int vbt_panel_unprepare(struct drm_panel *panel)
479{
480	generic_exec_sequence(panel, MIPI_SEQ_ASSERT_RESET);
481	generic_exec_sequence(panel, MIPI_SEQ_POWER_OFF);
482
483	return 0;
484}
485
486static int vbt_panel_enable(struct drm_panel *panel)
487{
488	generic_exec_sequence(panel, MIPI_SEQ_DISPLAY_ON);
489	generic_exec_sequence(panel, MIPI_SEQ_BACKLIGHT_ON);
490
491	return 0;
492}
493
494static int vbt_panel_disable(struct drm_panel *panel)
495{
496	generic_exec_sequence(panel, MIPI_SEQ_BACKLIGHT_OFF);
497	generic_exec_sequence(panel, MIPI_SEQ_DISPLAY_OFF);
498
499	return 0;
500}
501
502static int vbt_panel_get_modes(struct drm_panel *panel)
503{
504	struct vbt_panel *vbt_panel = to_vbt_panel(panel);
505	struct intel_dsi *intel_dsi = vbt_panel->intel_dsi;
506	struct drm_device *dev = intel_dsi->base.base.dev;
507	struct drm_i915_private *dev_priv = to_i915(dev);
508	struct drm_display_mode *mode;
509
510	if (!panel->connector)
511		return 0;
512
513	mode = drm_mode_duplicate(dev, dev_priv->vbt.lfp_lvds_vbt_mode);
514	if (!mode)
515		return 0;
516
517	mode->type |= DRM_MODE_TYPE_PREFERRED;
518
519	drm_mode_probed_add(panel->connector, mode);
520
521	return 1;
522}
523
524static const struct drm_panel_funcs vbt_panel_funcs = {
525	.disable = vbt_panel_disable,
526	.unprepare = vbt_panel_unprepare,
527	.prepare = vbt_panel_prepare,
528	.enable = vbt_panel_enable,
529	.get_modes = vbt_panel_get_modes,
530};
531
532struct drm_panel *vbt_panel_init(struct intel_dsi *intel_dsi, u16 panel_id)
533{
534	struct drm_device *dev = intel_dsi->base.base.dev;
535	struct drm_i915_private *dev_priv = to_i915(dev);
536	struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
537	struct mipi_pps_data *pps = dev_priv->vbt.dsi.pps;
538	struct drm_display_mode *mode = dev_priv->vbt.lfp_lvds_vbt_mode;
539	struct vbt_panel *vbt_panel;
540	u32 bpp;
541	u32 tlpx_ns, extra_byte_count, bitrate, tlpx_ui;
542	u32 ui_num, ui_den;
543	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
544	u32 ths_prepare_ns, tclk_trail_ns;
545	u32 tclk_prepare_clkzero, ths_prepare_hszero;
546	u32 lp_to_hs_switch, hs_to_lp_switch;
547	u32 pclk, computed_ddr;
548	u16 burst_mode_ratio;
549	enum port port;
550
551	DRM_DEBUG_KMS("\n");
552
553	intel_dsi->eotp_pkt = mipi_config->eot_pkt_disabled ? 0 : 1;
554	intel_dsi->clock_stop = mipi_config->enable_clk_stop ? 1 : 0;
555	intel_dsi->lane_count = mipi_config->lane_cnt + 1;
556	intel_dsi->pixel_format =
557			pixel_format_from_register_bits(
558				mipi_config->videomode_color_format << 7);
559	bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
560
561	intel_dsi->dual_link = mipi_config->dual_link;
562	intel_dsi->pixel_overlap = mipi_config->pixel_overlap;
 
 
 
563	intel_dsi->operation_mode = mipi_config->is_cmd_mode;
564	intel_dsi->video_mode_format = mipi_config->video_transfer_mode;
565	intel_dsi->escape_clk_div = mipi_config->byte_clk_sel;
566	intel_dsi->lp_rx_timeout = mipi_config->lp_rx_timeout;
567	intel_dsi->turn_arnd_val = mipi_config->turn_around_timeout;
568	intel_dsi->rst_timer_val = mipi_config->device_reset_timer;
569	intel_dsi->init_count = mipi_config->master_init_timer;
570	intel_dsi->bw_timer = mipi_config->dbi_bw_timer;
571	intel_dsi->video_frmt_cfg_bits =
572		mipi_config->bta_enabled ? DISABLE_VIDEO_BTA : 0;
573
574	pclk = mode->clock;
575
576	/* In dual link mode each port needs half of pixel clock */
577	if (intel_dsi->dual_link) {
578		pclk = pclk / 2;
579
580		/* we can enable pixel_overlap if needed by panel. In this
581		 * case we need to increase the pixelclock for extra pixels
582		 */
583		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
584			pclk += DIV_ROUND_UP(mode->vtotal *
585						intel_dsi->pixel_overlap *
586						60, 1000);
587		}
588	}
589
590	/* Burst Mode Ratio
591	 * Target ddr frequency from VBT / non burst ddr freq
592	 * multiply by 100 to preserve remainder
593	 */
594	if (intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
595		if (mipi_config->target_burst_mode_freq) {
596			computed_ddr = (pclk * bpp) / intel_dsi->lane_count;
 
597
598			if (mipi_config->target_burst_mode_freq <
599								computed_ddr) {
600				DRM_ERROR("Burst mode freq is less than computed\n");
601				return NULL;
602			}
603
604			burst_mode_ratio = DIV_ROUND_UP(
605				mipi_config->target_burst_mode_freq * 100,
606				computed_ddr);
607
608			pclk = DIV_ROUND_UP(pclk * burst_mode_ratio, 100);
609		} else {
610			DRM_ERROR("Burst mode target is not set\n");
611			return NULL;
612		}
613	} else
614		burst_mode_ratio = 100;
615
616	intel_dsi->burst_mode_ratio = burst_mode_ratio;
617	intel_dsi->pclk = pclk;
618
619	bitrate = (pclk * bpp) / intel_dsi->lane_count;
620
621	switch (intel_dsi->escape_clk_div) {
622	case 0:
623		tlpx_ns = 50;
624		break;
625	case 1:
626		tlpx_ns = 100;
627		break;
628
629	case 2:
630		tlpx_ns = 200;
631		break;
632	default:
633		tlpx_ns = 50;
634		break;
635	}
636
637	switch (intel_dsi->lane_count) {
638	case 1:
639	case 2:
640		extra_byte_count = 2;
641		break;
642	case 3:
643		extra_byte_count = 4;
644		break;
645	case 4:
646	default:
647		extra_byte_count = 3;
648		break;
649	}
650
651	/*
652	 * ui(s) = 1/f [f in hz]
653	 * ui(ns) = 10^9 / (f*10^6) [f in Mhz] -> 10^3/f(Mhz)
654	 */
655
656	/* in Kbps */
657	ui_num = NS_KHZ_RATIO;
658	ui_den = bitrate;
659
660	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
661	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
662
663	/*
664	 * B060
665	 * LP byte clock = TLPX/ (8UI)
666	 */
667	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
668
669	/* count values in UI = (ns value) * (bitrate / (2 * 10^6))
670	 *
671	 * Since txddrclkhs_i is 2xUI, all the count values programmed in
672	 * DPHY param register are divided by 2
673	 *
674	 * prepare count
675	 */
676	ths_prepare_ns = max(mipi_config->ths_prepare,
677			     mipi_config->tclk_prepare);
678	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * 2);
679
680	/* exit zero count */
681	exit_zero_cnt = DIV_ROUND_UP(
682				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
683				ui_num * 2
684				);
685
686	/*
687	 * Exit zero is unified val ths_zero and ths_exit
688	 * minimum value for ths_exit = 110ns
689	 * min (exit_zero_cnt * 2) = 110/UI
690	 * exit_zero_cnt = 55/UI
691	 */
692	if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
693		exit_zero_cnt += 1;
 
694
695	/* clk zero count */
696	clk_zero_cnt = DIV_ROUND_UP(
697			(tclk_prepare_clkzero -	ths_prepare_ns)
698			* ui_den, 2 * ui_num);
699
700	/* trail count */
701	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
702	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, 2 * ui_num);
703
704	if (prepare_cnt > PREPARE_CNT_MAX ||
705		exit_zero_cnt > EXIT_ZERO_CNT_MAX ||
706		clk_zero_cnt > CLK_ZERO_CNT_MAX ||
707		trail_cnt > TRAIL_CNT_MAX)
708		DRM_DEBUG_DRIVER("Values crossing maximum limits, restricting to max values\n");
709
710	if (prepare_cnt > PREPARE_CNT_MAX)
711		prepare_cnt = PREPARE_CNT_MAX;
712
713	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX)
714		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
715
716	if (clk_zero_cnt > CLK_ZERO_CNT_MAX)
717		clk_zero_cnt = CLK_ZERO_CNT_MAX;
718
719	if (trail_cnt > TRAIL_CNT_MAX)
720		trail_cnt = TRAIL_CNT_MAX;
721
722	/* B080 */
723	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
724						clk_zero_cnt << 8 | prepare_cnt;
725
726	/*
727	 * LP to HS switch count = 4TLPX + PREP_COUNT * 2 + EXIT_ZERO_COUNT * 2
728	 *					+ 10UI + Extra Byte Count
729	 *
730	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
731	 * Extra Byte Count is calculated according to number of lanes.
732	 * High Low Switch Count is the Max of LP to HS and
733	 * HS to LP switch count
734	 *
735	 */
736	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
737
738	/* B044 */
739	/* FIXME:
740	 * The comment above does not match with the code */
741	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * 2 +
742						exit_zero_cnt * 2 + 10, 8);
743
744	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
745
746	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
747	intel_dsi->hs_to_lp_count += extra_byte_count;
748
749	/* B088 */
750	/* LP -> HS for clock lanes
751	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
752	 *						extra byte count
753	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
754	 *					2(in UI) + extra byte count
755	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
756	 *					8 + extra byte count
757	 */
758	intel_dsi->clk_lp_to_hs_count =
759		DIV_ROUND_UP(
760			4 * tlpx_ui + prepare_cnt * 2 +
761			clk_zero_cnt * 2,
762			8);
763
764	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
765
766	/* HS->LP for Clock Lanes
767	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
768	 *						Extra byte count
769	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
770	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
771	 *						Extra byte count
772	 */
773	intel_dsi->clk_hs_to_lp_count =
774		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
775			8);
776	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
777
778	DRM_DEBUG_KMS("Eot %s\n", enableddisabled(intel_dsi->eotp_pkt));
779	DRM_DEBUG_KMS("Clockstop %s\n", enableddisabled(!intel_dsi->clock_stop));
 
780	DRM_DEBUG_KMS("Mode %s\n", intel_dsi->operation_mode ? "command" : "video");
781	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
782		DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_FRONT_BACK\n");
783	else if (intel_dsi->dual_link == DSI_DUAL_LINK_PIXEL_ALT)
784		DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_PIXEL_ALT\n");
785	else
786		DRM_DEBUG_KMS("Dual link: NONE\n");
787	DRM_DEBUG_KMS("Pixel Format %d\n", intel_dsi->pixel_format);
788	DRM_DEBUG_KMS("TLPX %d\n", intel_dsi->escape_clk_div);
789	DRM_DEBUG_KMS("LP RX Timeout 0x%x\n", intel_dsi->lp_rx_timeout);
790	DRM_DEBUG_KMS("Turnaround Timeout 0x%x\n", intel_dsi->turn_arnd_val);
791	DRM_DEBUG_KMS("Init Count 0x%x\n", intel_dsi->init_count);
792	DRM_DEBUG_KMS("HS to LP Count 0x%x\n", intel_dsi->hs_to_lp_count);
793	DRM_DEBUG_KMS("LP Byte Clock %d\n", intel_dsi->lp_byte_clk);
794	DRM_DEBUG_KMS("DBI BW Timer 0x%x\n", intel_dsi->bw_timer);
795	DRM_DEBUG_KMS("LP to HS Clock Count 0x%x\n", intel_dsi->clk_lp_to_hs_count);
796	DRM_DEBUG_KMS("HS to LP Clock Count 0x%x\n", intel_dsi->clk_hs_to_lp_count);
797	DRM_DEBUG_KMS("BTA %s\n",
798			enableddisabled(!(intel_dsi->video_frmt_cfg_bits & DISABLE_VIDEO_BTA)));
 
799
800	/* delays in VBT are in unit of 100us, so need to convert
801	 * here in ms
802	 * Delay (100us) * 100 /1000 = Delay / 10 (ms) */
803	intel_dsi->backlight_off_delay = pps->bl_disable_delay / 10;
804	intel_dsi->backlight_on_delay = pps->bl_enable_delay / 10;
805	intel_dsi->panel_on_delay = pps->panel_on_delay / 10;
806	intel_dsi->panel_off_delay = pps->panel_off_delay / 10;
807	intel_dsi->panel_pwr_cycle_delay = pps->panel_power_cycle_delay / 10;
808
809	/* This is cheating a bit with the cleanup. */
810	vbt_panel = devm_kzalloc(dev->dev, sizeof(*vbt_panel), GFP_KERNEL);
811	if (!vbt_panel)
812		return NULL;
813
814	vbt_panel->intel_dsi = intel_dsi;
815	drm_panel_init(&vbt_panel->panel);
816	vbt_panel->panel.funcs = &vbt_panel_funcs;
817	drm_panel_add(&vbt_panel->panel);
818
819	/* a regular driver would get the device in probe */
820	for_each_dsi_port(port, intel_dsi->ports) {
821		mipi_dsi_attach(intel_dsi->dsi_hosts[port]->device);
822	}
823
824	return &vbt_panel->panel;
825}