xref: /linux/drivers/gpu/drm/i915/display/intel_dsi_vbt.c (revision 25489a4f556414445d342951615178368ee45cde)
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 <linux/gpio/consumer.h>
28 #include <linux/gpio/machine.h>
29 #include <linux/mfd/intel_soc_pmic.h>
30 #include <linux/pinctrl/consumer.h>
31 #include <linux/pinctrl/machine.h>
32 #include <linux/slab.h>
33 #include <linux/string_helpers.h>
34 #include <linux/unaligned.h>
35 
36 #include <drm/drm_crtc.h>
37 #include <drm/drm_edid.h>
38 #include <drm/drm_print.h>
39 
40 #include <video/mipi_display.h>
41 
42 #include "i915_reg.h"
43 #include "i915_utils.h"
44 #include "intel_de.h"
45 #include "intel_display_types.h"
46 #include "intel_dsi.h"
47 #include "intel_dsi_vbt.h"
48 #include "intel_gmbus_regs.h"
49 #include "intel_pps_regs.h"
50 #include "vlv_dsi.h"
51 #include "vlv_dsi_regs.h"
52 #include "vlv_sideband.h"
53 
54 #define MIPI_TRANSFER_MODE_SHIFT	0
55 #define MIPI_VIRTUAL_CHANNEL_SHIFT	1
56 #define MIPI_PORT_SHIFT			3
57 
58 struct i2c_adapter_lookup {
59 	u16 target_addr;
60 	struct intel_dsi *intel_dsi;
61 	acpi_handle dev_handle;
62 };
63 
64 #define CHV_GPIO_IDX_START_N		0
65 #define CHV_GPIO_IDX_START_E		73
66 #define CHV_GPIO_IDX_START_SW		100
67 #define CHV_GPIO_IDX_START_SE		198
68 
69 /* ICL DSI Display GPIO Pins */
70 #define  ICL_GPIO_DDSP_HPD_A		0
71 #define  ICL_GPIO_L_VDDEN_1		1
72 #define  ICL_GPIO_L_BKLTEN_1		2
73 #define  ICL_GPIO_DDPA_CTRLCLK_1	3
74 #define  ICL_GPIO_DDPA_CTRLDATA_1	4
75 #define  ICL_GPIO_DDSP_HPD_B		5
76 #define  ICL_GPIO_L_VDDEN_2		6
77 #define  ICL_GPIO_L_BKLTEN_2		7
78 #define  ICL_GPIO_DDPA_CTRLCLK_2	8
79 #define  ICL_GPIO_DDPA_CTRLDATA_2	9
80 
81 static enum port intel_dsi_seq_port_to_port(struct intel_dsi *intel_dsi,
82 					    u8 seq_port)
83 {
84 	/*
85 	 * If single link DSI is being used on any port, the VBT sequence block
86 	 * send packet apparently always has 0 for the port. Just use the port
87 	 * we have configured, and ignore the sequence block port.
88 	 */
89 	if (hweight8(intel_dsi->ports) == 1)
90 		return ffs(intel_dsi->ports) - 1;
91 
92 	if (seq_port) {
93 		if (intel_dsi->ports & BIT(PORT_B))
94 			return PORT_B;
95 		if (intel_dsi->ports & BIT(PORT_C))
96 			return PORT_C;
97 	}
98 
99 	return PORT_A;
100 }
101 
102 static const u8 *mipi_exec_send_packet(struct intel_dsi *intel_dsi,
103 				       const u8 *data)
104 {
105 	struct intel_display *display = to_intel_display(&intel_dsi->base);
106 	struct mipi_dsi_device *dsi_device;
107 	u8 type, flags, seq_port;
108 	u16 len;
109 	enum port port;
110 
111 	drm_dbg_kms(display->drm, "\n");
112 
113 	flags = *data++;
114 	type = *data++;
115 
116 	len = *((u16 *) data);
117 	data += 2;
118 
119 	seq_port = (flags >> MIPI_PORT_SHIFT) & 3;
120 
121 	port = intel_dsi_seq_port_to_port(intel_dsi, seq_port);
122 
123 	if (drm_WARN_ON(display->drm, !intel_dsi->dsi_hosts[port]))
124 		goto out;
125 
126 	dsi_device = intel_dsi->dsi_hosts[port]->device;
127 	if (!dsi_device) {
128 		drm_dbg_kms(display->drm, "no dsi device for port %c\n",
129 			    port_name(port));
130 		goto out;
131 	}
132 
133 	if ((flags >> MIPI_TRANSFER_MODE_SHIFT) & 1)
134 		dsi_device->mode_flags &= ~MIPI_DSI_MODE_LPM;
135 	else
136 		dsi_device->mode_flags |= MIPI_DSI_MODE_LPM;
137 
138 	dsi_device->channel = (flags >> MIPI_VIRTUAL_CHANNEL_SHIFT) & 3;
139 
140 	switch (type) {
141 	case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
142 		mipi_dsi_generic_write(dsi_device, NULL, 0);
143 		break;
144 	case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
145 		mipi_dsi_generic_write(dsi_device, data, 1);
146 		break;
147 	case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
148 		mipi_dsi_generic_write(dsi_device, data, 2);
149 		break;
150 	case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
151 	case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
152 	case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
153 		drm_dbg_kms(display->drm, "Generic Read not yet implemented or used\n");
154 		break;
155 	case MIPI_DSI_GENERIC_LONG_WRITE:
156 		mipi_dsi_generic_write(dsi_device, data, len);
157 		break;
158 	case MIPI_DSI_DCS_SHORT_WRITE:
159 		mipi_dsi_dcs_write_buffer(dsi_device, data, 1);
160 		break;
161 	case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
162 		mipi_dsi_dcs_write_buffer(dsi_device, data, 2);
163 		break;
164 	case MIPI_DSI_DCS_READ:
165 		drm_dbg_kms(display->drm, "DCS Read not yet implemented or used\n");
166 		break;
167 	case MIPI_DSI_DCS_LONG_WRITE:
168 		mipi_dsi_dcs_write_buffer(dsi_device, data, len);
169 		break;
170 	}
171 
172 	if (DISPLAY_VER(display) < 11)
173 		vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
174 
175 out:
176 	data += len;
177 
178 	return data;
179 }
180 
181 static const u8 *mipi_exec_delay(struct intel_dsi *intel_dsi, const u8 *data)
182 {
183 	struct intel_display *display = to_intel_display(&intel_dsi->base);
184 	u32 delay = *((const u32 *) data);
185 
186 	drm_dbg_kms(display->drm, "%d usecs\n", delay);
187 
188 	usleep_range(delay, delay + 10);
189 	data += 4;
190 
191 	return data;
192 }
193 
194 static void soc_gpio_set_value(struct intel_connector *connector, u8 gpio_index,
195 			       const char *con_id, u8 idx, bool value)
196 {
197 	struct intel_display *display = to_intel_display(connector);
198 	/* XXX: this table is a quick ugly hack. */
199 	static struct gpio_desc *soc_gpio_table[U8_MAX + 1];
200 	struct gpio_desc *gpio_desc = soc_gpio_table[gpio_index];
201 
202 	if (gpio_desc) {
203 		gpiod_set_value(gpio_desc, value);
204 	} else {
205 		gpio_desc = devm_gpiod_get_index(display->drm->dev, con_id, idx,
206 						 value ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW);
207 		if (IS_ERR(gpio_desc)) {
208 			drm_err(display->drm,
209 				"GPIO index %u request failed (%pe)\n",
210 				gpio_index, gpio_desc);
211 			return;
212 		}
213 
214 		soc_gpio_table[gpio_index] = gpio_desc;
215 	}
216 }
217 
218 static void soc_opaque_gpio_set_value(struct intel_connector *connector,
219 				      u8 gpio_index, const char *chip,
220 				      const char *con_id, u8 idx, bool value)
221 {
222 	struct gpiod_lookup_table *lookup;
223 
224 	lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
225 	if (!lookup)
226 		return;
227 
228 	lookup->dev_id = "0000:00:02.0";
229 	lookup->table[0] =
230 		GPIO_LOOKUP_IDX(chip, idx, con_id, idx, GPIO_ACTIVE_HIGH);
231 
232 	gpiod_add_lookup_table(lookup);
233 
234 	soc_gpio_set_value(connector, gpio_index, con_id, idx, value);
235 
236 	gpiod_remove_lookup_table(lookup);
237 	kfree(lookup);
238 }
239 
240 static void vlv_gpio_set_value(struct intel_connector *connector,
241 			       u8 gpio_source, u8 gpio_index, bool value)
242 {
243 	struct intel_display *display = to_intel_display(connector);
244 
245 	/* XXX: this assumes vlv_gpio_table only has NC GPIOs. */
246 	if (connector->panel.vbt.dsi.seq_version < 3) {
247 		if (gpio_source == 1) {
248 			drm_dbg_kms(display->drm, "SC gpio not supported\n");
249 			return;
250 		}
251 		if (gpio_source > 1) {
252 			drm_dbg_kms(display->drm,
253 				    "unknown gpio source %u\n", gpio_source);
254 			return;
255 		}
256 	}
257 
258 	soc_opaque_gpio_set_value(connector, gpio_index,
259 				  "INT33FC:01", "Panel N", gpio_index, value);
260 }
261 
262 static void chv_gpio_set_value(struct intel_connector *connector,
263 			       u8 gpio_source, u8 gpio_index, bool value)
264 {
265 	struct intel_display *display = to_intel_display(connector);
266 
267 	if (connector->panel.vbt.dsi.seq_version >= 3) {
268 		if (gpio_index >= CHV_GPIO_IDX_START_SE) {
269 			/* XXX: it's unclear whether 255->57 is part of SE. */
270 			soc_opaque_gpio_set_value(connector, gpio_index, "INT33FF:03", "Panel SE",
271 						  gpio_index - CHV_GPIO_IDX_START_SE, value);
272 		} else if (gpio_index >= CHV_GPIO_IDX_START_SW) {
273 			soc_opaque_gpio_set_value(connector, gpio_index, "INT33FF:00", "Panel SW",
274 						  gpio_index - CHV_GPIO_IDX_START_SW, value);
275 		} else if (gpio_index >= CHV_GPIO_IDX_START_E) {
276 			soc_opaque_gpio_set_value(connector, gpio_index, "INT33FF:02", "Panel E",
277 						  gpio_index - CHV_GPIO_IDX_START_E, value);
278 		} else {
279 			soc_opaque_gpio_set_value(connector, gpio_index, "INT33FF:01", "Panel N",
280 						  gpio_index - CHV_GPIO_IDX_START_N, value);
281 		}
282 	} else {
283 		/* XXX: The spec is unclear about CHV GPIO on seq v2 */
284 		if (gpio_source != 0) {
285 			drm_dbg_kms(display->drm,
286 				    "unknown gpio source %u\n", gpio_source);
287 			return;
288 		}
289 
290 		if (gpio_index >= CHV_GPIO_IDX_START_E) {
291 			drm_dbg_kms(display->drm,
292 				    "invalid gpio index %u for GPIO N\n",
293 				    gpio_index);
294 			return;
295 		}
296 
297 		soc_opaque_gpio_set_value(connector, gpio_index, "INT33FF:01", "Panel N",
298 					  gpio_index - CHV_GPIO_IDX_START_N, value);
299 	}
300 }
301 
302 static void bxt_gpio_set_value(struct intel_connector *connector,
303 			       u8 gpio_index, bool value)
304 {
305 	soc_gpio_set_value(connector, gpio_index, NULL, gpio_index, value);
306 }
307 
308 enum {
309 	MIPI_RESET_1 = 0,
310 	MIPI_AVDD_EN_1,
311 	MIPI_BKLT_EN_1,
312 	MIPI_AVEE_EN_1,
313 	MIPI_VIO_EN_1,
314 	MIPI_RESET_2,
315 	MIPI_AVDD_EN_2,
316 	MIPI_BKLT_EN_2,
317 	MIPI_AVEE_EN_2,
318 	MIPI_VIO_EN_2,
319 };
320 
321 static void icl_native_gpio_set_value(struct intel_display *display,
322 				      int gpio, bool value)
323 {
324 	int index;
325 
326 	if (drm_WARN_ON(display->drm, DISPLAY_VER(display) == 11 && gpio >= MIPI_RESET_2))
327 		return;
328 
329 	switch (gpio) {
330 	case MIPI_RESET_1:
331 	case MIPI_RESET_2:
332 		index = gpio == MIPI_RESET_1 ? HPD_PORT_A : HPD_PORT_B;
333 
334 		/*
335 		 * Disable HPD to set the pin to output, and set output
336 		 * value. The HPD pin should not be enabled for DSI anyway,
337 		 * assuming the board design and VBT are sane, and the pin isn't
338 		 * used by a non-DSI encoder.
339 		 *
340 		 * The locking protects against concurrent SHOTPLUG_CTL_DDI
341 		 * modifications in irq setup and handling.
342 		 */
343 		spin_lock_irq(&display->irq.lock);
344 		intel_de_rmw(display, SHOTPLUG_CTL_DDI,
345 			     SHOTPLUG_CTL_DDI_HPD_ENABLE(index) |
346 			     SHOTPLUG_CTL_DDI_HPD_OUTPUT_DATA(index),
347 			     value ? SHOTPLUG_CTL_DDI_HPD_OUTPUT_DATA(index) : 0);
348 		spin_unlock_irq(&display->irq.lock);
349 		break;
350 	case MIPI_AVDD_EN_1:
351 	case MIPI_AVDD_EN_2:
352 		index = gpio == MIPI_AVDD_EN_1 ? 0 : 1;
353 
354 		intel_de_rmw(display, PP_CONTROL(display, index), PANEL_POWER_ON,
355 			     value ? PANEL_POWER_ON : 0);
356 		break;
357 	case MIPI_BKLT_EN_1:
358 	case MIPI_BKLT_EN_2:
359 		index = gpio == MIPI_BKLT_EN_1 ? 0 : 1;
360 
361 		intel_de_rmw(display, PP_CONTROL(display, index), EDP_BLC_ENABLE,
362 			     value ? EDP_BLC_ENABLE : 0);
363 		break;
364 	case MIPI_AVEE_EN_1:
365 	case MIPI_AVEE_EN_2:
366 		index = gpio == MIPI_AVEE_EN_1 ? 1 : 2;
367 
368 		intel_de_rmw(display, GPIO(display, index),
369 			     GPIO_CLOCK_VAL_OUT,
370 			     GPIO_CLOCK_DIR_MASK | GPIO_CLOCK_DIR_OUT |
371 			     GPIO_CLOCK_VAL_MASK | (value ? GPIO_CLOCK_VAL_OUT : 0));
372 		break;
373 	case MIPI_VIO_EN_1:
374 	case MIPI_VIO_EN_2:
375 		index = gpio == MIPI_VIO_EN_1 ? 1 : 2;
376 
377 		intel_de_rmw(display, GPIO(display, index),
378 			     GPIO_DATA_VAL_OUT,
379 			     GPIO_DATA_DIR_MASK | GPIO_DATA_DIR_OUT |
380 			     GPIO_DATA_VAL_MASK | (value ? GPIO_DATA_VAL_OUT : 0));
381 		break;
382 	default:
383 		MISSING_CASE(gpio);
384 	}
385 }
386 
387 static const u8 *mipi_exec_gpio(struct intel_dsi *intel_dsi, const u8 *data)
388 {
389 	struct intel_display *display = to_intel_display(&intel_dsi->base);
390 	struct intel_connector *connector = intel_dsi->attached_connector;
391 	u8 gpio_source = 0, gpio_index = 0, gpio_number;
392 	bool value;
393 	int size;
394 	bool native = DISPLAY_VER(display) >= 11;
395 
396 	if (connector->panel.vbt.dsi.seq_version >= 3) {
397 		size = 3;
398 
399 		gpio_index = data[0];
400 		gpio_number = data[1];
401 		value = data[2] & BIT(0);
402 
403 		if (connector->panel.vbt.dsi.seq_version >= 4 && data[2] & BIT(1))
404 			native = false;
405 	} else {
406 		size = 2;
407 
408 		gpio_number = data[0];
409 		value = data[1] & BIT(0);
410 
411 		if (connector->panel.vbt.dsi.seq_version == 2)
412 			gpio_source = (data[1] >> 1) & 3;
413 	}
414 
415 	drm_dbg_kms(display->drm, "GPIO index %u, number %u, source %u, native %s, set to %s\n",
416 		    gpio_index, gpio_number, gpio_source, str_yes_no(native), str_on_off(value));
417 
418 	if (native)
419 		icl_native_gpio_set_value(display, gpio_number, value);
420 	else if (DISPLAY_VER(display) >= 9)
421 		bxt_gpio_set_value(connector, gpio_index, value);
422 	else if (display->platform.valleyview)
423 		vlv_gpio_set_value(connector, gpio_source, gpio_number, value);
424 	else if (display->platform.cherryview)
425 		chv_gpio_set_value(connector, gpio_source, gpio_number, value);
426 
427 	return data + size;
428 }
429 
430 #ifdef CONFIG_ACPI
431 static int i2c_adapter_lookup(struct acpi_resource *ares, void *data)
432 {
433 	struct i2c_adapter_lookup *lookup = data;
434 	struct intel_dsi *intel_dsi = lookup->intel_dsi;
435 	struct acpi_resource_i2c_serialbus *sb;
436 	struct i2c_adapter *adapter;
437 	acpi_handle adapter_handle;
438 	acpi_status status;
439 
440 	if (!i2c_acpi_get_i2c_resource(ares, &sb))
441 		return 1;
442 
443 	if (lookup->target_addr != sb->slave_address)
444 		return 1;
445 
446 	status = acpi_get_handle(lookup->dev_handle,
447 				 sb->resource_source.string_ptr,
448 				 &adapter_handle);
449 	if (ACPI_FAILURE(status))
450 		return 1;
451 
452 	adapter = i2c_acpi_find_adapter_by_handle(adapter_handle);
453 	if (adapter)
454 		intel_dsi->i2c_bus_num = adapter->nr;
455 
456 	return 1;
457 }
458 
459 static void i2c_acpi_find_adapter(struct intel_dsi *intel_dsi,
460 				  const u16 target_addr)
461 {
462 	struct intel_display *display = to_intel_display(&intel_dsi->base);
463 	struct acpi_device *adev = ACPI_COMPANION(display->drm->dev);
464 	struct i2c_adapter_lookup lookup = {
465 		.target_addr = target_addr,
466 		.intel_dsi = intel_dsi,
467 		.dev_handle = acpi_device_handle(adev),
468 	};
469 	LIST_HEAD(resource_list);
470 
471 	acpi_dev_get_resources(adev, &resource_list, i2c_adapter_lookup, &lookup);
472 	acpi_dev_free_resource_list(&resource_list);
473 }
474 #else
475 static inline void i2c_acpi_find_adapter(struct intel_dsi *intel_dsi,
476 					 const u16 target_addr)
477 {
478 }
479 #endif
480 
481 static const u8 *mipi_exec_i2c(struct intel_dsi *intel_dsi, const u8 *data)
482 {
483 	struct intel_display *display = to_intel_display(&intel_dsi->base);
484 	struct i2c_adapter *adapter;
485 	struct i2c_msg msg;
486 	int ret;
487 	u8 vbt_i2c_bus_num = *(data + 2);
488 	u16 target_addr = *(u16 *)(data + 3);
489 	u8 reg_offset = *(data + 5);
490 	u8 payload_size = *(data + 6);
491 	u8 *payload_data;
492 
493 	drm_dbg_kms(display->drm, "bus %d target-addr 0x%02x reg 0x%02x data %*ph\n",
494 		    vbt_i2c_bus_num, target_addr, reg_offset, payload_size, data + 7);
495 
496 	if (intel_dsi->i2c_bus_num < 0) {
497 		intel_dsi->i2c_bus_num = vbt_i2c_bus_num;
498 		i2c_acpi_find_adapter(intel_dsi, target_addr);
499 	}
500 
501 	adapter = i2c_get_adapter(intel_dsi->i2c_bus_num);
502 	if (!adapter) {
503 		drm_err(display->drm, "Cannot find a valid i2c bus for xfer\n");
504 		goto err_bus;
505 	}
506 
507 	payload_data = kzalloc(payload_size + 1, GFP_KERNEL);
508 	if (!payload_data)
509 		goto err_alloc;
510 
511 	payload_data[0] = reg_offset;
512 	memcpy(&payload_data[1], (data + 7), payload_size);
513 
514 	msg.addr = target_addr;
515 	msg.flags = 0;
516 	msg.len = payload_size + 1;
517 	msg.buf = payload_data;
518 
519 	ret = i2c_transfer(adapter, &msg, 1);
520 	if (ret < 0)
521 		drm_err(display->drm,
522 			"Failed to xfer payload of size (%u) to reg (%u)\n",
523 			payload_size, reg_offset);
524 
525 	kfree(payload_data);
526 err_alloc:
527 	i2c_put_adapter(adapter);
528 err_bus:
529 	return data + payload_size + 7;
530 }
531 
532 static const u8 *mipi_exec_spi(struct intel_dsi *intel_dsi, const u8 *data)
533 {
534 	struct intel_display *display = to_intel_display(&intel_dsi->base);
535 
536 	drm_dbg_kms(display->drm, "Skipping SPI element execution\n");
537 
538 	return data + *(data + 5) + 6;
539 }
540 
541 static const u8 *mipi_exec_pmic(struct intel_dsi *intel_dsi, const u8 *data)
542 {
543 	struct intel_display *display = to_intel_display(&intel_dsi->base);
544 #ifdef CONFIG_PMIC_OPREGION
545 	u32 value, mask, reg_address;
546 	u16 i2c_address;
547 	int ret;
548 
549 	/* byte 0 aka PMIC Flag is reserved */
550 	i2c_address	= get_unaligned_le16(data + 1);
551 	reg_address	= get_unaligned_le32(data + 3);
552 	value		= get_unaligned_le32(data + 7);
553 	mask		= get_unaligned_le32(data + 11);
554 
555 	ret = intel_soc_pmic_exec_mipi_pmic_seq_element(i2c_address,
556 							reg_address,
557 							value, mask);
558 	if (ret)
559 		drm_err(display->drm, "%s failed, error: %d\n", __func__, ret);
560 #else
561 	drm_err(display->drm,
562 		"Your hardware requires CONFIG_PMIC_OPREGION and it is not set\n");
563 #endif
564 
565 	return data + 15;
566 }
567 
568 typedef const u8 * (*fn_mipi_elem_exec)(struct intel_dsi *intel_dsi,
569 					const u8 *data);
570 static const fn_mipi_elem_exec exec_elem[] = {
571 	[MIPI_SEQ_ELEM_SEND_PKT] = mipi_exec_send_packet,
572 	[MIPI_SEQ_ELEM_DELAY] = mipi_exec_delay,
573 	[MIPI_SEQ_ELEM_GPIO] = mipi_exec_gpio,
574 	[MIPI_SEQ_ELEM_I2C] = mipi_exec_i2c,
575 	[MIPI_SEQ_ELEM_SPI] = mipi_exec_spi,
576 	[MIPI_SEQ_ELEM_PMIC] = mipi_exec_pmic,
577 };
578 
579 /*
580  * MIPI Sequence from VBT #53 parsing logic
581  * We have already separated each sequence during bios parsing
582  * Following is generic execution function for any sequence
583  */
584 
585 static const char * const seq_name[] = {
586 	[MIPI_SEQ_END] = "MIPI_SEQ_END",
587 	[MIPI_SEQ_DEASSERT_RESET] = "MIPI_SEQ_DEASSERT_RESET",
588 	[MIPI_SEQ_INIT_OTP] = "MIPI_SEQ_INIT_OTP",
589 	[MIPI_SEQ_DISPLAY_ON] = "MIPI_SEQ_DISPLAY_ON",
590 	[MIPI_SEQ_DISPLAY_OFF]  = "MIPI_SEQ_DISPLAY_OFF",
591 	[MIPI_SEQ_ASSERT_RESET] = "MIPI_SEQ_ASSERT_RESET",
592 	[MIPI_SEQ_BACKLIGHT_ON] = "MIPI_SEQ_BACKLIGHT_ON",
593 	[MIPI_SEQ_BACKLIGHT_OFF] = "MIPI_SEQ_BACKLIGHT_OFF",
594 	[MIPI_SEQ_TEAR_ON] = "MIPI_SEQ_TEAR_ON",
595 	[MIPI_SEQ_TEAR_OFF] = "MIPI_SEQ_TEAR_OFF",
596 	[MIPI_SEQ_POWER_ON] = "MIPI_SEQ_POWER_ON",
597 	[MIPI_SEQ_POWER_OFF] = "MIPI_SEQ_POWER_OFF",
598 };
599 
600 static const char *sequence_name(enum mipi_seq seq_id)
601 {
602 	if (seq_id < ARRAY_SIZE(seq_name))
603 		return seq_name[seq_id];
604 
605 	return "(unknown)";
606 }
607 
608 static void intel_dsi_vbt_exec(struct intel_dsi *intel_dsi,
609 			       enum mipi_seq seq_id)
610 {
611 	struct intel_display *display = to_intel_display(&intel_dsi->base);
612 	struct intel_connector *connector = intel_dsi->attached_connector;
613 	const u8 *data;
614 	fn_mipi_elem_exec mipi_elem_exec;
615 
616 	if (drm_WARN_ON(display->drm,
617 			seq_id >= ARRAY_SIZE(connector->panel.vbt.dsi.sequence)))
618 		return;
619 
620 	data = connector->panel.vbt.dsi.sequence[seq_id];
621 	if (!data)
622 		return;
623 
624 	drm_WARN_ON(display->drm, *data != seq_id);
625 
626 	drm_dbg_kms(display->drm, "Starting MIPI sequence %d - %s\n",
627 		    seq_id, sequence_name(seq_id));
628 
629 	/* Skip Sequence Byte. */
630 	data++;
631 
632 	/* Skip Size of Sequence. */
633 	if (connector->panel.vbt.dsi.seq_version >= 3)
634 		data += 4;
635 
636 	while (*data != MIPI_SEQ_ELEM_END) {
637 		u8 operation_byte = *data++;
638 		u8 operation_size = 0;
639 
640 		if (operation_byte < ARRAY_SIZE(exec_elem))
641 			mipi_elem_exec = exec_elem[operation_byte];
642 		else
643 			mipi_elem_exec = NULL;
644 
645 		/* Size of Operation. */
646 		if (connector->panel.vbt.dsi.seq_version >= 3)
647 			operation_size = *data++;
648 
649 		if (mipi_elem_exec) {
650 			const u8 *next = data + operation_size;
651 
652 			data = mipi_elem_exec(intel_dsi, data);
653 
654 			/* Consistency check if we have size. */
655 			if (operation_size && data != next) {
656 				drm_err(display->drm,
657 					"Inconsistent operation size\n");
658 				return;
659 			}
660 		} else if (operation_size) {
661 			/* We have size, skip. */
662 			drm_dbg_kms(display->drm,
663 				    "Unsupported MIPI operation byte %u\n",
664 				    operation_byte);
665 			data += operation_size;
666 		} else {
667 			/* No size, can't skip without parsing. */
668 			drm_err(display->drm,
669 				"Unsupported MIPI operation byte %u\n",
670 				operation_byte);
671 			return;
672 		}
673 	}
674 }
675 
676 void intel_dsi_vbt_exec_sequence(struct intel_dsi *intel_dsi,
677 				 enum mipi_seq seq_id)
678 {
679 	if (seq_id == MIPI_SEQ_POWER_ON && intel_dsi->gpio_panel)
680 		gpiod_set_value_cansleep(intel_dsi->gpio_panel, 1);
681 	if (seq_id == MIPI_SEQ_BACKLIGHT_ON && intel_dsi->gpio_backlight)
682 		gpiod_set_value_cansleep(intel_dsi->gpio_backlight, 1);
683 
684 	intel_dsi_vbt_exec(intel_dsi, seq_id);
685 
686 	if (seq_id == MIPI_SEQ_POWER_OFF && intel_dsi->gpio_panel)
687 		gpiod_set_value_cansleep(intel_dsi->gpio_panel, 0);
688 	if (seq_id == MIPI_SEQ_BACKLIGHT_OFF && intel_dsi->gpio_backlight)
689 		gpiod_set_value_cansleep(intel_dsi->gpio_backlight, 0);
690 }
691 
692 void intel_dsi_log_params(struct intel_dsi *intel_dsi)
693 {
694 	struct intel_display *display = to_intel_display(&intel_dsi->base);
695 	struct drm_printer p = drm_dbg_printer(display->drm, DRM_UT_KMS,
696 					       "DSI parameters:");
697 
698 	drm_printf(&p, "Pclk %d\n", intel_dsi->pclk);
699 	drm_printf(&p, "Pixel overlap %d\n", intel_dsi->pixel_overlap);
700 	drm_printf(&p, "Lane count %d\n", intel_dsi->lane_count);
701 	drm_printf(&p, "DPHY param reg 0x%x\n", intel_dsi->dphy_reg);
702 	drm_printf(&p, "Video mode format %s\n",
703 		   intel_dsi->video_mode == NON_BURST_SYNC_PULSE ?
704 		   "non-burst with sync pulse" :
705 		   intel_dsi->video_mode == NON_BURST_SYNC_EVENTS ?
706 		   "non-burst with sync events" :
707 		   intel_dsi->video_mode == BURST_MODE ?
708 		   "burst" : "<unknown>");
709 	drm_printf(&p, "Burst mode ratio %d\n", intel_dsi->burst_mode_ratio);
710 	drm_printf(&p, "Reset timer %d\n", intel_dsi->rst_timer_val);
711 	drm_printf(&p, "Eot %s\n", str_enabled_disabled(intel_dsi->eotp_pkt));
712 	drm_printf(&p, "Clockstop %s\n", str_enabled_disabled(!intel_dsi->clock_stop));
713 	drm_printf(&p, "Mode %s\n", intel_dsi->operation_mode ? "command" : "video");
714 	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
715 		drm_printf(&p, "Dual link: DSI_DUAL_LINK_FRONT_BACK\n");
716 	else if (intel_dsi->dual_link == DSI_DUAL_LINK_PIXEL_ALT)
717 		drm_printf(&p, "Dual link: DSI_DUAL_LINK_PIXEL_ALT\n");
718 	else
719 		drm_printf(&p, "Dual link: NONE\n");
720 	drm_printf(&p, "Pixel Format %d\n", intel_dsi->pixel_format);
721 	drm_printf(&p, "TLPX %d\n", intel_dsi->escape_clk_div);
722 	drm_printf(&p, "LP RX Timeout 0x%x\n", intel_dsi->lp_rx_timeout);
723 	drm_printf(&p, "Turnaround Timeout 0x%x\n", intel_dsi->turn_arnd_val);
724 	drm_printf(&p, "Init Count 0x%x\n", intel_dsi->init_count);
725 	drm_printf(&p, "HS to LP Count 0x%x\n", intel_dsi->hs_to_lp_count);
726 	drm_printf(&p, "LP Byte Clock %d\n", intel_dsi->lp_byte_clk);
727 	drm_printf(&p, "DBI BW Timer 0x%x\n", intel_dsi->bw_timer);
728 	drm_printf(&p, "LP to HS Clock Count 0x%x\n", intel_dsi->clk_lp_to_hs_count);
729 	drm_printf(&p, "HS to LP Clock Count 0x%x\n", intel_dsi->clk_hs_to_lp_count);
730 	drm_printf(&p, "BTA %s\n",
731 		   str_enabled_disabled(!(intel_dsi->video_frmt_cfg_bits & DISABLE_VIDEO_BTA)));
732 }
733 
734 static enum mipi_dsi_pixel_format vbt_to_dsi_pixel_format(unsigned int format)
735 {
736 	switch (format) {
737 	case PIXEL_FORMAT_RGB888:
738 		return MIPI_DSI_FMT_RGB888;
739 	case PIXEL_FORMAT_RGB666_LOOSELY_PACKED:
740 		return MIPI_DSI_FMT_RGB666;
741 	case PIXEL_FORMAT_RGB666:
742 		return MIPI_DSI_FMT_RGB666_PACKED;
743 	case PIXEL_FORMAT_RGB565:
744 		return MIPI_DSI_FMT_RGB565;
745 	default:
746 		MISSING_CASE(format);
747 		return MIPI_DSI_FMT_RGB666;
748 	}
749 }
750 
751 bool intel_dsi_vbt_init(struct intel_dsi *intel_dsi, u16 panel_id)
752 {
753 	struct intel_display *display = to_intel_display(&intel_dsi->base);
754 	struct intel_connector *connector = intel_dsi->attached_connector;
755 	struct mipi_config *mipi_config = connector->panel.vbt.dsi.config;
756 	struct mipi_pps_data *pps = connector->panel.vbt.dsi.pps;
757 	struct drm_display_mode *mode = connector->panel.vbt.lfp_vbt_mode;
758 	u16 burst_mode_ratio;
759 	enum port port;
760 
761 	drm_dbg_kms(display->drm, "\n");
762 
763 	intel_dsi->eotp_pkt = mipi_config->eot_pkt_disabled ? 0 : 1;
764 	intel_dsi->clock_stop = mipi_config->enable_clk_stop ? 1 : 0;
765 	intel_dsi->lane_count = mipi_config->lane_cnt + 1;
766 	intel_dsi->pixel_format =
767 		vbt_to_dsi_pixel_format(mipi_config->videomode_color_format);
768 
769 	intel_dsi->dual_link = mipi_config->dual_link;
770 	intel_dsi->pixel_overlap = mipi_config->pixel_overlap;
771 	intel_dsi->operation_mode = mipi_config->is_cmd_mode;
772 	intel_dsi->video_mode = mipi_config->video_transfer_mode;
773 	intel_dsi->escape_clk_div = mipi_config->byte_clk_sel;
774 	intel_dsi->lp_rx_timeout = mipi_config->lp_rx_timeout;
775 	intel_dsi->hs_tx_timeout = mipi_config->hs_tx_timeout;
776 	intel_dsi->turn_arnd_val = mipi_config->turn_around_timeout;
777 	intel_dsi->rst_timer_val = mipi_config->device_reset_timer;
778 	intel_dsi->init_count = mipi_config->master_init_timer;
779 	intel_dsi->bw_timer = mipi_config->dbi_bw_timer;
780 	intel_dsi->video_frmt_cfg_bits =
781 		mipi_config->bta_enabled ? DISABLE_VIDEO_BTA : 0;
782 	intel_dsi->bgr_enabled = mipi_config->rgb_flip;
783 
784 	/* Starting point, adjusted depending on dual link and burst mode */
785 	intel_dsi->pclk = mode->clock;
786 
787 	/* In dual link mode each port needs half of pixel clock */
788 	if (intel_dsi->dual_link) {
789 		intel_dsi->pclk /= 2;
790 
791 		/* we can enable pixel_overlap if needed by panel. In this
792 		 * case we need to increase the pixelclock for extra pixels
793 		 */
794 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
795 			intel_dsi->pclk += DIV_ROUND_UP(mode->vtotal * intel_dsi->pixel_overlap * 60, 1000);
796 		}
797 	}
798 
799 	/* Burst Mode Ratio
800 	 * Target ddr frequency from VBT / non burst ddr freq
801 	 * multiply by 100 to preserve remainder
802 	 */
803 	if (intel_dsi->video_mode == BURST_MODE) {
804 		u32 bitrate;
805 
806 		if (mipi_config->target_burst_mode_freq == 0) {
807 			drm_err(display->drm, "Burst mode target is not set\n");
808 			return false;
809 		}
810 
811 		bitrate = intel_dsi_bitrate(intel_dsi);
812 
813 		/*
814 		 * Sometimes the VBT contains a slightly lower clock, then
815 		 * the bitrate we have calculated, in this case just replace it
816 		 * with the calculated bitrate.
817 		 */
818 		if (mipi_config->target_burst_mode_freq < bitrate &&
819 		    intel_fuzzy_clock_check(mipi_config->target_burst_mode_freq,
820 					    bitrate))
821 			mipi_config->target_burst_mode_freq = bitrate;
822 
823 		if (mipi_config->target_burst_mode_freq < bitrate) {
824 			drm_err(display->drm, "Burst mode freq is less than computed\n");
825 			return false;
826 		}
827 
828 		burst_mode_ratio =
829 			DIV_ROUND_UP(mipi_config->target_burst_mode_freq * 100, bitrate);
830 
831 		intel_dsi->pclk = DIV_ROUND_UP(intel_dsi->pclk * burst_mode_ratio, 100);
832 	} else
833 		burst_mode_ratio = 100;
834 
835 	intel_dsi->burst_mode_ratio = burst_mode_ratio;
836 
837 	/* delays in VBT are in unit of 100us, so need to convert
838 	 * here in ms
839 	 * Delay (100us) * 100 /1000 = Delay / 10 (ms) */
840 	intel_dsi->backlight_off_delay = pps->bl_disable_delay / 10;
841 	intel_dsi->backlight_on_delay = pps->bl_enable_delay / 10;
842 	intel_dsi->panel_on_delay = pps->panel_on_delay / 10;
843 	intel_dsi->panel_off_delay = pps->panel_off_delay / 10;
844 	intel_dsi->panel_pwr_cycle_delay = pps->panel_power_cycle_delay / 10;
845 
846 	intel_dsi->i2c_bus_num = -1;
847 
848 	/* a regular driver would get the device in probe */
849 	for_each_dsi_port(port, intel_dsi->ports) {
850 		mipi_dsi_attach(intel_dsi->dsi_hosts[port]->device);
851 	}
852 
853 	return true;
854 }
855 
856 /*
857  * On some BYT/CHT devs some sequences are incomplete and we need to manually
858  * control some GPIOs. We need to add a GPIO lookup table before we get these.
859  * If the GOP did not initialize the panel (HDMI inserted) we may need to also
860  * change the pinmux for the SoC's PWM0 pin from GPIO to PWM.
861  */
862 static struct gpiod_lookup_table pmic_panel_gpio_table = {
863 	/* Intel GFX is consumer */
864 	.dev_id = "0000:00:02.0",
865 	.table = {
866 		/* Panel EN/DISABLE */
867 		GPIO_LOOKUP("gpio_crystalcove", 94, "panel", GPIO_ACTIVE_HIGH),
868 		{ }
869 	},
870 };
871 
872 static struct gpiod_lookup_table soc_panel_gpio_table = {
873 	.dev_id = "0000:00:02.0",
874 	.table = {
875 		GPIO_LOOKUP("INT33FC:01", 10, "backlight", GPIO_ACTIVE_HIGH),
876 		GPIO_LOOKUP("INT33FC:01", 11, "panel", GPIO_ACTIVE_HIGH),
877 		{ }
878 	},
879 };
880 
881 static const struct pinctrl_map soc_pwm_pinctrl_map[] = {
882 	PIN_MAP_MUX_GROUP("0000:00:02.0", "soc_pwm0", "INT33FC:00",
883 			  "pwm0_grp", "pwm"),
884 };
885 
886 void intel_dsi_vbt_gpio_init(struct intel_dsi *intel_dsi, bool panel_is_on)
887 {
888 	struct intel_display *display = to_intel_display(&intel_dsi->base);
889 	struct intel_connector *connector = intel_dsi->attached_connector;
890 	struct mipi_config *mipi_config = connector->panel.vbt.dsi.config;
891 	enum gpiod_flags flags = panel_is_on ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
892 	struct gpiod_lookup_table *gpiod_lookup_table = NULL;
893 	bool want_backlight_gpio = false;
894 	bool want_panel_gpio = false;
895 	struct pinctrl *pinctrl;
896 	int ret;
897 
898 	if ((display->platform.valleyview || display->platform.cherryview) &&
899 	    mipi_config->pwm_blc == PPS_BLC_PMIC) {
900 		gpiod_lookup_table = &pmic_panel_gpio_table;
901 		want_panel_gpio = true;
902 	}
903 
904 	if (display->platform.valleyview && mipi_config->pwm_blc == PPS_BLC_SOC) {
905 		gpiod_lookup_table = &soc_panel_gpio_table;
906 		want_panel_gpio = true;
907 		want_backlight_gpio = true;
908 
909 		/* Ensure PWM0 pin is muxed as PWM instead of GPIO */
910 		ret = pinctrl_register_mappings(soc_pwm_pinctrl_map,
911 					     ARRAY_SIZE(soc_pwm_pinctrl_map));
912 		if (ret)
913 			drm_err(display->drm,
914 				"Failed to register pwm0 pinmux mapping\n");
915 
916 		pinctrl = devm_pinctrl_get_select(display->drm->dev, "soc_pwm0");
917 		if (IS_ERR(pinctrl))
918 			drm_err(display->drm,
919 				"Failed to set pinmux to PWM\n");
920 	}
921 
922 	if (gpiod_lookup_table)
923 		gpiod_add_lookup_table(gpiod_lookup_table);
924 
925 	if (want_panel_gpio) {
926 		intel_dsi->gpio_panel = devm_gpiod_get(display->drm->dev, "panel", flags);
927 		if (IS_ERR(intel_dsi->gpio_panel)) {
928 			drm_err(display->drm,
929 				"Failed to own gpio for panel control\n");
930 			intel_dsi->gpio_panel = NULL;
931 		}
932 	}
933 
934 	if (want_backlight_gpio) {
935 		intel_dsi->gpio_backlight =
936 			devm_gpiod_get(display->drm->dev, "backlight", flags);
937 		if (IS_ERR(intel_dsi->gpio_backlight)) {
938 			drm_err(display->drm,
939 				"Failed to own gpio for backlight control\n");
940 			intel_dsi->gpio_backlight = NULL;
941 		}
942 	}
943 
944 	if (gpiod_lookup_table)
945 		gpiod_remove_lookup_table(gpiod_lookup_table);
946 }
947