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