]> Git Repo - J-linux.git/blob - drivers/gpu/drm/i915/intel_dsi_vbt.c
Merge tag 'topic/drmp-cleanup-2019-01-02' of git://anongit.freedesktop.org/drm/drm...
[J-linux.git] / drivers / gpu / drm / i915 / intel_dsi_vbt.c
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 <[email protected]>
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 <linux/gpio/consumer.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
40 #define MIPI_TRANSFER_MODE_SHIFT        0
41 #define MIPI_VIRTUAL_CHANNEL_SHIFT      1
42 #define MIPI_PORT_SHIFT                 3
43
44 #define PREPARE_CNT_MAX         0x3F
45 #define EXIT_ZERO_CNT_MAX       0x3F
46 #define CLK_ZERO_CNT_MAX        0xFF
47 #define TRAIL_CNT_MAX           0x1F
48
49 #define NS_KHZ_RATIO 1000000
50
51 /* base offsets for gpio pads */
52 #define VLV_GPIO_NC_0_HV_DDI0_HPD       0x4130
53 #define VLV_GPIO_NC_1_HV_DDI0_DDC_SDA   0x4120
54 #define VLV_GPIO_NC_2_HV_DDI0_DDC_SCL   0x4110
55 #define VLV_GPIO_NC_3_PANEL0_VDDEN      0x4140
56 #define VLV_GPIO_NC_4_PANEL0_BKLTEN     0x4150
57 #define VLV_GPIO_NC_5_PANEL0_BKLTCTL    0x4160
58 #define VLV_GPIO_NC_6_HV_DDI1_HPD       0x4180
59 #define VLV_GPIO_NC_7_HV_DDI1_DDC_SDA   0x4190
60 #define VLV_GPIO_NC_8_HV_DDI1_DDC_SCL   0x4170
61 #define VLV_GPIO_NC_9_PANEL1_VDDEN      0x4100
62 #define VLV_GPIO_NC_10_PANEL1_BKLTEN    0x40E0
63 #define VLV_GPIO_NC_11_PANEL1_BKLTCTL   0x40F0
64
65 #define VLV_GPIO_PCONF0(base_offset)    (base_offset)
66 #define VLV_GPIO_PAD_VAL(base_offset)   ((base_offset) + 8)
67
68 struct gpio_map {
69         u16 base_offset;
70         bool init;
71 };
72
73 static struct gpio_map vlv_gpio_table[] = {
74         { VLV_GPIO_NC_0_HV_DDI0_HPD },
75         { VLV_GPIO_NC_1_HV_DDI0_DDC_SDA },
76         { VLV_GPIO_NC_2_HV_DDI0_DDC_SCL },
77         { VLV_GPIO_NC_3_PANEL0_VDDEN },
78         { VLV_GPIO_NC_4_PANEL0_BKLTEN },
79         { VLV_GPIO_NC_5_PANEL0_BKLTCTL },
80         { VLV_GPIO_NC_6_HV_DDI1_HPD },
81         { VLV_GPIO_NC_7_HV_DDI1_DDC_SDA },
82         { VLV_GPIO_NC_8_HV_DDI1_DDC_SCL },
83         { VLV_GPIO_NC_9_PANEL1_VDDEN },
84         { VLV_GPIO_NC_10_PANEL1_BKLTEN },
85         { VLV_GPIO_NC_11_PANEL1_BKLTCTL },
86 };
87
88 #define CHV_GPIO_IDX_START_N            0
89 #define CHV_GPIO_IDX_START_E            73
90 #define CHV_GPIO_IDX_START_SW           100
91 #define CHV_GPIO_IDX_START_SE           198
92
93 #define CHV_VBT_MAX_PINS_PER_FMLY       15
94
95 #define CHV_GPIO_PAD_CFG0(f, i)         (0x4400 + (f) * 0x400 + (i) * 8)
96 #define  CHV_GPIO_GPIOEN                (1 << 15)
97 #define  CHV_GPIO_GPIOCFG_GPIO          (0 << 8)
98 #define  CHV_GPIO_GPIOCFG_GPO           (1 << 8)
99 #define  CHV_GPIO_GPIOCFG_GPI           (2 << 8)
100 #define  CHV_GPIO_GPIOCFG_HIZ           (3 << 8)
101 #define  CHV_GPIO_GPIOTXSTATE(state)    ((!!(state)) << 1)
102
103 #define CHV_GPIO_PAD_CFG1(f, i)         (0x4400 + (f) * 0x400 + (i) * 8 + 4)
104 #define  CHV_GPIO_CFGLOCK               (1 << 31)
105
106 static inline enum port intel_dsi_seq_port_to_port(u8 port)
107 {
108         return port ? PORT_C : PORT_A;
109 }
110
111 static const u8 *mipi_exec_send_packet(struct intel_dsi *intel_dsi,
112                                        const u8 *data)
113 {
114         struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
115         struct mipi_dsi_device *dsi_device;
116         u8 type, flags, seq_port;
117         u16 len;
118         enum port port;
119
120         DRM_DEBUG_KMS("\n");
121
122         flags = *data++;
123         type = *data++;
124
125         len = *((u16 *) data);
126         data += 2;
127
128         seq_port = (flags >> MIPI_PORT_SHIFT) & 3;
129
130         /* For DSI single link on Port A & C, the seq_port value which is
131          * parsed from Sequence Block#53 of VBT has been set to 0
132          * Now, read/write of packets for the DSI single link on Port A and
133          * Port C will based on the DVO port from VBT block 2.
134          */
135         if (intel_dsi->ports == (1 << PORT_C))
136                 port = PORT_C;
137         else
138                 port = intel_dsi_seq_port_to_port(seq_port);
139
140         dsi_device = intel_dsi->dsi_hosts[port]->device;
141         if (!dsi_device) {
142                 DRM_DEBUG_KMS("no dsi device for port %c\n", port_name(port));
143                 goto out;
144         }
145
146         if ((flags >> MIPI_TRANSFER_MODE_SHIFT) & 1)
147                 dsi_device->mode_flags &= ~MIPI_DSI_MODE_LPM;
148         else
149                 dsi_device->mode_flags |= MIPI_DSI_MODE_LPM;
150
151         dsi_device->channel = (flags >> MIPI_VIRTUAL_CHANNEL_SHIFT) & 3;
152
153         switch (type) {
154         case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
155                 mipi_dsi_generic_write(dsi_device, NULL, 0);
156                 break;
157         case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
158                 mipi_dsi_generic_write(dsi_device, data, 1);
159                 break;
160         case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
161                 mipi_dsi_generic_write(dsi_device, data, 2);
162                 break;
163         case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
164         case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
165         case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
166                 DRM_DEBUG_DRIVER("Generic Read not yet implemented or used\n");
167                 break;
168         case MIPI_DSI_GENERIC_LONG_WRITE:
169                 mipi_dsi_generic_write(dsi_device, data, len);
170                 break;
171         case MIPI_DSI_DCS_SHORT_WRITE:
172                 mipi_dsi_dcs_write_buffer(dsi_device, data, 1);
173                 break;
174         case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
175                 mipi_dsi_dcs_write_buffer(dsi_device, data, 2);
176                 break;
177         case MIPI_DSI_DCS_READ:
178                 DRM_DEBUG_DRIVER("DCS Read not yet implemented or used\n");
179                 break;
180         case MIPI_DSI_DCS_LONG_WRITE:
181                 mipi_dsi_dcs_write_buffer(dsi_device, data, len);
182                 break;
183         }
184
185         if (!IS_ICELAKE(dev_priv))
186                 vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
187
188 out:
189         data += len;
190
191         return data;
192 }
193
194 static const u8 *mipi_exec_delay(struct intel_dsi *intel_dsi, const u8 *data)
195 {
196         u32 delay = *((const u32 *) data);
197
198         DRM_DEBUG_KMS("\n");
199
200         usleep_range(delay, delay + 10);
201         data += 4;
202
203         return data;
204 }
205
206 static void vlv_exec_gpio(struct drm_i915_private *dev_priv,
207                           u8 gpio_source, u8 gpio_index, bool value)
208 {
209         struct gpio_map *map;
210         u16 pconf0, padval;
211         u32 tmp;
212         u8 port;
213
214         if (gpio_index >= ARRAY_SIZE(vlv_gpio_table)) {
215                 DRM_DEBUG_KMS("unknown gpio index %u\n", gpio_index);
216                 return;
217         }
218
219         map = &vlv_gpio_table[gpio_index];
220
221         if (dev_priv->vbt.dsi.seq_version >= 3) {
222                 /* XXX: this assumes vlv_gpio_table only has NC GPIOs. */
223                 port = IOSF_PORT_GPIO_NC;
224         } else {
225                 if (gpio_source == 0) {
226                         port = IOSF_PORT_GPIO_NC;
227                 } else if (gpio_source == 1) {
228                         DRM_DEBUG_KMS("SC gpio not supported\n");
229                         return;
230                 } else {
231                         DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
232                         return;
233                 }
234         }
235
236         pconf0 = VLV_GPIO_PCONF0(map->base_offset);
237         padval = VLV_GPIO_PAD_VAL(map->base_offset);
238
239         mutex_lock(&dev_priv->sb_lock);
240         if (!map->init) {
241                 /* FIXME: remove constant below */
242                 vlv_iosf_sb_write(dev_priv, port, pconf0, 0x2000CC00);
243                 map->init = true;
244         }
245
246         tmp = 0x4 | value;
247         vlv_iosf_sb_write(dev_priv, port, padval, tmp);
248         mutex_unlock(&dev_priv->sb_lock);
249 }
250
251 static void chv_exec_gpio(struct drm_i915_private *dev_priv,
252                           u8 gpio_source, u8 gpio_index, bool value)
253 {
254         u16 cfg0, cfg1;
255         u16 family_num;
256         u8 port;
257
258         if (dev_priv->vbt.dsi.seq_version >= 3) {
259                 if (gpio_index >= CHV_GPIO_IDX_START_SE) {
260                         /* XXX: it's unclear whether 255->57 is part of SE. */
261                         gpio_index -= CHV_GPIO_IDX_START_SE;
262                         port = CHV_IOSF_PORT_GPIO_SE;
263                 } else if (gpio_index >= CHV_GPIO_IDX_START_SW) {
264                         gpio_index -= CHV_GPIO_IDX_START_SW;
265                         port = CHV_IOSF_PORT_GPIO_SW;
266                 } else if (gpio_index >= CHV_GPIO_IDX_START_E) {
267                         gpio_index -= CHV_GPIO_IDX_START_E;
268                         port = CHV_IOSF_PORT_GPIO_E;
269                 } else {
270                         port = CHV_IOSF_PORT_GPIO_N;
271                 }
272         } else {
273                 /* XXX: The spec is unclear about CHV GPIO on seq v2 */
274                 if (gpio_source != 0) {
275                         DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
276                         return;
277                 }
278
279                 if (gpio_index >= CHV_GPIO_IDX_START_E) {
280                         DRM_DEBUG_KMS("invalid gpio index %u for GPIO N\n",
281                                       gpio_index);
282                         return;
283                 }
284
285                 port = CHV_IOSF_PORT_GPIO_N;
286         }
287
288         family_num = gpio_index / CHV_VBT_MAX_PINS_PER_FMLY;
289         gpio_index = gpio_index % CHV_VBT_MAX_PINS_PER_FMLY;
290
291         cfg0 = CHV_GPIO_PAD_CFG0(family_num, gpio_index);
292         cfg1 = CHV_GPIO_PAD_CFG1(family_num, gpio_index);
293
294         mutex_lock(&dev_priv->sb_lock);
295         vlv_iosf_sb_write(dev_priv, port, cfg1, 0);
296         vlv_iosf_sb_write(dev_priv, port, cfg0,
297                           CHV_GPIO_GPIOEN | CHV_GPIO_GPIOCFG_GPO |
298                           CHV_GPIO_GPIOTXSTATE(value));
299         mutex_unlock(&dev_priv->sb_lock);
300 }
301
302 static void bxt_exec_gpio(struct drm_i915_private *dev_priv,
303                           u8 gpio_source, u8 gpio_index, bool value)
304 {
305         /* XXX: this table is a quick ugly hack. */
306         static struct gpio_desc *bxt_gpio_table[U8_MAX + 1];
307         struct gpio_desc *gpio_desc = bxt_gpio_table[gpio_index];
308
309         if (!gpio_desc) {
310                 gpio_desc = devm_gpiod_get_index(dev_priv->drm.dev,
311                                                  NULL, gpio_index,
312                                                  value ? GPIOD_OUT_LOW :
313                                                  GPIOD_OUT_HIGH);
314
315                 if (IS_ERR_OR_NULL(gpio_desc)) {
316                         DRM_ERROR("GPIO index %u request failed (%ld)\n",
317                                   gpio_index, PTR_ERR(gpio_desc));
318                         return;
319                 }
320
321                 bxt_gpio_table[gpio_index] = gpio_desc;
322         }
323
324         gpiod_set_value(gpio_desc, value);
325 }
326
327 static const u8 *mipi_exec_gpio(struct intel_dsi *intel_dsi, const u8 *data)
328 {
329         struct drm_device *dev = intel_dsi->base.base.dev;
330         struct drm_i915_private *dev_priv = to_i915(dev);
331         u8 gpio_source, gpio_index = 0, gpio_number;
332         bool value;
333
334         DRM_DEBUG_KMS("\n");
335
336         if (dev_priv->vbt.dsi.seq_version >= 3)
337                 gpio_index = *data++;
338
339         gpio_number = *data++;
340
341         /* gpio source in sequence v2 only */
342         if (dev_priv->vbt.dsi.seq_version == 2)
343                 gpio_source = (*data >> 1) & 3;
344         else
345                 gpio_source = 0;
346
347         /* pull up/down */
348         value = *data++ & 1;
349
350         if (IS_VALLEYVIEW(dev_priv))
351                 vlv_exec_gpio(dev_priv, gpio_source, gpio_number, value);
352         else if (IS_CHERRYVIEW(dev_priv))
353                 chv_exec_gpio(dev_priv, gpio_source, gpio_number, value);
354         else
355                 bxt_exec_gpio(dev_priv, gpio_source, gpio_index, value);
356
357         return data;
358 }
359
360 static const u8 *mipi_exec_i2c(struct intel_dsi *intel_dsi, const u8 *data)
361 {
362         DRM_DEBUG_KMS("Skipping I2C element execution\n");
363
364         return data + *(data + 6) + 7;
365 }
366
367 static const u8 *mipi_exec_spi(struct intel_dsi *intel_dsi, const u8 *data)
368 {
369         DRM_DEBUG_KMS("Skipping SPI element execution\n");
370
371         return data + *(data + 5) + 6;
372 }
373
374 static const u8 *mipi_exec_pmic(struct intel_dsi *intel_dsi, const u8 *data)
375 {
376         DRM_DEBUG_KMS("Skipping PMIC element execution\n");
377
378         return data + 15;
379 }
380
381 typedef const u8 * (*fn_mipi_elem_exec)(struct intel_dsi *intel_dsi,
382                                         const u8 *data);
383 static const fn_mipi_elem_exec exec_elem[] = {
384         [MIPI_SEQ_ELEM_SEND_PKT] = mipi_exec_send_packet,
385         [MIPI_SEQ_ELEM_DELAY] = mipi_exec_delay,
386         [MIPI_SEQ_ELEM_GPIO] = mipi_exec_gpio,
387         [MIPI_SEQ_ELEM_I2C] = mipi_exec_i2c,
388         [MIPI_SEQ_ELEM_SPI] = mipi_exec_spi,
389         [MIPI_SEQ_ELEM_PMIC] = mipi_exec_pmic,
390 };
391
392 /*
393  * MIPI Sequence from VBT #53 parsing logic
394  * We have already separated each seqence during bios parsing
395  * Following is generic execution function for any sequence
396  */
397
398 static const char * const seq_name[] = {
399         [MIPI_SEQ_DEASSERT_RESET] = "MIPI_SEQ_DEASSERT_RESET",
400         [MIPI_SEQ_INIT_OTP] = "MIPI_SEQ_INIT_OTP",
401         [MIPI_SEQ_DISPLAY_ON] = "MIPI_SEQ_DISPLAY_ON",
402         [MIPI_SEQ_DISPLAY_OFF]  = "MIPI_SEQ_DISPLAY_OFF",
403         [MIPI_SEQ_ASSERT_RESET] = "MIPI_SEQ_ASSERT_RESET",
404         [MIPI_SEQ_BACKLIGHT_ON] = "MIPI_SEQ_BACKLIGHT_ON",
405         [MIPI_SEQ_BACKLIGHT_OFF] = "MIPI_SEQ_BACKLIGHT_OFF",
406         [MIPI_SEQ_TEAR_ON] = "MIPI_SEQ_TEAR_ON",
407         [MIPI_SEQ_TEAR_OFF] = "MIPI_SEQ_TEAR_OFF",
408         [MIPI_SEQ_POWER_ON] = "MIPI_SEQ_POWER_ON",
409         [MIPI_SEQ_POWER_OFF] = "MIPI_SEQ_POWER_OFF",
410 };
411
412 static const char *sequence_name(enum mipi_seq seq_id)
413 {
414         if (seq_id < ARRAY_SIZE(seq_name) && seq_name[seq_id])
415                 return seq_name[seq_id];
416         else
417                 return "(unknown)";
418 }
419
420 void intel_dsi_vbt_exec_sequence(struct intel_dsi *intel_dsi,
421                                  enum mipi_seq seq_id)
422 {
423         struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
424         const u8 *data;
425         fn_mipi_elem_exec mipi_elem_exec;
426
427         if (WARN_ON(seq_id >= ARRAY_SIZE(dev_priv->vbt.dsi.sequence)))
428                 return;
429
430         data = dev_priv->vbt.dsi.sequence[seq_id];
431         if (!data)
432                 return;
433
434         WARN_ON(*data != seq_id);
435
436         DRM_DEBUG_KMS("Starting MIPI sequence %d - %s\n",
437                       seq_id, sequence_name(seq_id));
438
439         /* Skip Sequence Byte. */
440         data++;
441
442         /* Skip Size of Sequence. */
443         if (dev_priv->vbt.dsi.seq_version >= 3)
444                 data += 4;
445
446         while (1) {
447                 u8 operation_byte = *data++;
448                 u8 operation_size = 0;
449
450                 if (operation_byte == MIPI_SEQ_ELEM_END)
451                         break;
452
453                 if (operation_byte < ARRAY_SIZE(exec_elem))
454                         mipi_elem_exec = exec_elem[operation_byte];
455                 else
456                         mipi_elem_exec = NULL;
457
458                 /* Size of Operation. */
459                 if (dev_priv->vbt.dsi.seq_version >= 3)
460                         operation_size = *data++;
461
462                 if (mipi_elem_exec) {
463                         const u8 *next = data + operation_size;
464
465                         data = mipi_elem_exec(intel_dsi, data);
466
467                         /* Consistency check if we have size. */
468                         if (operation_size && data != next) {
469                                 DRM_ERROR("Inconsistent operation size\n");
470                                 return;
471                         }
472                 } else if (operation_size) {
473                         /* We have size, skip. */
474                         DRM_DEBUG_KMS("Unsupported MIPI operation byte %u\n",
475                                       operation_byte);
476                         data += operation_size;
477                 } else {
478                         /* No size, can't skip without parsing. */
479                         DRM_ERROR("Unsupported MIPI operation byte %u\n",
480                                   operation_byte);
481                         return;
482                 }
483         }
484 }
485
486 void intel_dsi_msleep(struct intel_dsi *intel_dsi, int msec)
487 {
488         struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
489
490         /* For v3 VBTs in vid-mode the delays are part of the VBT sequences */
491         if (is_vid_mode(intel_dsi) && dev_priv->vbt.dsi.seq_version >= 3)
492                 return;
493
494         msleep(msec);
495 }
496
497 int intel_dsi_vbt_get_modes(struct intel_dsi *intel_dsi)
498 {
499         struct intel_connector *connector = intel_dsi->attached_connector;
500         struct drm_device *dev = intel_dsi->base.base.dev;
501         struct drm_i915_private *dev_priv = to_i915(dev);
502         struct drm_display_mode *mode;
503
504         mode = drm_mode_duplicate(dev, dev_priv->vbt.lfp_lvds_vbt_mode);
505         if (!mode)
506                 return 0;
507
508         mode->type |= DRM_MODE_TYPE_PREFERRED;
509
510         drm_mode_probed_add(&connector->base, mode);
511
512         return 1;
513 }
514
515 #define ICL_PREPARE_CNT_MAX     0x7
516 #define ICL_CLK_ZERO_CNT_MAX    0xf
517 #define ICL_TRAIL_CNT_MAX       0x7
518 #define ICL_TCLK_PRE_CNT_MAX    0x3
519 #define ICL_TCLK_POST_CNT_MAX   0x7
520 #define ICL_HS_ZERO_CNT_MAX     0xf
521 #define ICL_EXIT_ZERO_CNT_MAX   0x7
522
523 static void icl_dphy_param_init(struct intel_dsi *intel_dsi)
524 {
525         struct drm_device *dev = intel_dsi->base.base.dev;
526         struct drm_i915_private *dev_priv = to_i915(dev);
527         struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
528         u32 tlpx_ns;
529         u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
530         u32 ths_prepare_ns, tclk_trail_ns;
531         u32 hs_zero_cnt;
532         u32 tclk_pre_cnt, tclk_post_cnt;
533
534         tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
535
536         tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
537         ths_prepare_ns = max(mipi_config->ths_prepare,
538                              mipi_config->tclk_prepare);
539
540         /*
541          * prepare cnt in escape clocks
542          * this field represents a hexadecimal value with a precision
543          * of 1.2 – i.e. the most significant bit is the integer
544          * and the least significant 2 bits are fraction bits.
545          * so, the field can represent a range of 0.25 to 1.75
546          */
547         prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * 4, tlpx_ns);
548         if (prepare_cnt > ICL_PREPARE_CNT_MAX) {
549                 DRM_DEBUG_KMS("prepare_cnt out of range (%d)\n", prepare_cnt);
550                 prepare_cnt = ICL_PREPARE_CNT_MAX;
551         }
552
553         /* clk zero count in escape clocks */
554         clk_zero_cnt = DIV_ROUND_UP(mipi_config->tclk_prepare_clkzero -
555                                     ths_prepare_ns, tlpx_ns);
556         if (clk_zero_cnt > ICL_CLK_ZERO_CNT_MAX) {
557                 DRM_DEBUG_KMS("clk_zero_cnt out of range (%d)\n", clk_zero_cnt);
558                 clk_zero_cnt = ICL_CLK_ZERO_CNT_MAX;
559         }
560
561         /* trail cnt in escape clocks*/
562         trail_cnt = DIV_ROUND_UP(tclk_trail_ns, tlpx_ns);
563         if (trail_cnt > ICL_TRAIL_CNT_MAX) {
564                 DRM_DEBUG_KMS("trail_cnt out of range (%d)\n", trail_cnt);
565                 trail_cnt = ICL_TRAIL_CNT_MAX;
566         }
567
568         /* tclk pre count in escape clocks */
569         tclk_pre_cnt = DIV_ROUND_UP(mipi_config->tclk_pre, tlpx_ns);
570         if (tclk_pre_cnt > ICL_TCLK_PRE_CNT_MAX) {
571                 DRM_DEBUG_KMS("tclk_pre_cnt out of range (%d)\n", tclk_pre_cnt);
572                 tclk_pre_cnt = ICL_TCLK_PRE_CNT_MAX;
573         }
574
575         /* tclk post count in escape clocks */
576         tclk_post_cnt = DIV_ROUND_UP(mipi_config->tclk_post, tlpx_ns);
577         if (tclk_post_cnt > ICL_TCLK_POST_CNT_MAX) {
578                 DRM_DEBUG_KMS("tclk_post_cnt out of range (%d)\n", tclk_post_cnt);
579                 tclk_post_cnt = ICL_TCLK_POST_CNT_MAX;
580         }
581
582         /* hs zero cnt in escape clocks */
583         hs_zero_cnt = DIV_ROUND_UP(mipi_config->ths_prepare_hszero -
584                                    ths_prepare_ns, tlpx_ns);
585         if (hs_zero_cnt > ICL_HS_ZERO_CNT_MAX) {
586                 DRM_DEBUG_KMS("hs_zero_cnt out of range (%d)\n", hs_zero_cnt);
587                 hs_zero_cnt = ICL_HS_ZERO_CNT_MAX;
588         }
589
590         /* hs exit zero cnt in escape clocks */
591         exit_zero_cnt = DIV_ROUND_UP(mipi_config->ths_exit, tlpx_ns);
592         if (exit_zero_cnt > ICL_EXIT_ZERO_CNT_MAX) {
593                 DRM_DEBUG_KMS("exit_zero_cnt out of range (%d)\n", exit_zero_cnt);
594                 exit_zero_cnt = ICL_EXIT_ZERO_CNT_MAX;
595         }
596
597         /* clock lane dphy timings */
598         intel_dsi->dphy_reg = (CLK_PREPARE_OVERRIDE |
599                                CLK_PREPARE(prepare_cnt) |
600                                CLK_ZERO_OVERRIDE |
601                                CLK_ZERO(clk_zero_cnt) |
602                                CLK_PRE_OVERRIDE |
603                                CLK_PRE(tclk_pre_cnt) |
604                                CLK_POST_OVERRIDE |
605                                CLK_POST(tclk_post_cnt) |
606                                CLK_TRAIL_OVERRIDE |
607                                CLK_TRAIL(trail_cnt));
608
609         /* data lanes dphy timings */
610         intel_dsi->dphy_data_lane_reg = (HS_PREPARE_OVERRIDE |
611                                          HS_PREPARE(prepare_cnt) |
612                                          HS_ZERO_OVERRIDE |
613                                          HS_ZERO(hs_zero_cnt) |
614                                          HS_TRAIL_OVERRIDE |
615                                          HS_TRAIL(trail_cnt) |
616                                          HS_EXIT_OVERRIDE |
617                                          HS_EXIT(exit_zero_cnt));
618 }
619
620 static void vlv_dphy_param_init(struct intel_dsi *intel_dsi)
621 {
622         struct drm_device *dev = intel_dsi->base.base.dev;
623         struct drm_i915_private *dev_priv = to_i915(dev);
624         struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
625         u32 tlpx_ns, extra_byte_count, tlpx_ui;
626         u32 ui_num, ui_den;
627         u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
628         u32 ths_prepare_ns, tclk_trail_ns;
629         u32 tclk_prepare_clkzero, ths_prepare_hszero;
630         u32 lp_to_hs_switch, hs_to_lp_switch;
631         u32 mul;
632
633         tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
634
635         switch (intel_dsi->lane_count) {
636         case 1:
637         case 2:
638                 extra_byte_count = 2;
639                 break;
640         case 3:
641                 extra_byte_count = 4;
642                 break;
643         case 4:
644         default:
645                 extra_byte_count = 3;
646                 break;
647         }
648
649         /* in Kbps */
650         ui_num = NS_KHZ_RATIO;
651         ui_den = intel_dsi_bitrate(intel_dsi);
652
653         tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
654         ths_prepare_hszero = mipi_config->ths_prepare_hszero;
655
656         /*
657          * B060
658          * LP byte clock = TLPX/ (8UI)
659          */
660         intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
661
662         /* DDR clock period = 2 * UI
663          * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
664          * UI(nsec) = 10^6 / bitrate
665          * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
666          * DDR clock count  = ns_value / DDR clock period
667          *
668          * For GEMINILAKE dphy_param_reg will be programmed in terms of
669          * HS byte clock count for other platform in HS ddr clock count
670          */
671         mul = IS_GEMINILAKE(dev_priv) ? 8 : 2;
672         ths_prepare_ns = max(mipi_config->ths_prepare,
673                              mipi_config->tclk_prepare);
674
675         /* prepare count */
676         prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
677
678         if (prepare_cnt > PREPARE_CNT_MAX) {
679                 DRM_DEBUG_KMS("prepare count too high %u\n", prepare_cnt);
680                 prepare_cnt = PREPARE_CNT_MAX;
681         }
682
683         /* exit zero count */
684         exit_zero_cnt = DIV_ROUND_UP(
685                                 (ths_prepare_hszero - ths_prepare_ns) * ui_den,
686                                 ui_num * mul
687                                 );
688
689         /*
690          * Exit zero is unified val ths_zero and ths_exit
691          * minimum value for ths_exit = 110ns
692          * min (exit_zero_cnt * 2) = 110/UI
693          * exit_zero_cnt = 55/UI
694          */
695         if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
696                 exit_zero_cnt += 1;
697
698         if (exit_zero_cnt > EXIT_ZERO_CNT_MAX) {
699                 DRM_DEBUG_KMS("exit zero count too high %u\n", exit_zero_cnt);
700                 exit_zero_cnt = EXIT_ZERO_CNT_MAX;
701         }
702
703         /* clk zero count */
704         clk_zero_cnt = DIV_ROUND_UP(
705                                 (tclk_prepare_clkzero - ths_prepare_ns)
706                                 * ui_den, ui_num * mul);
707
708         if (clk_zero_cnt > CLK_ZERO_CNT_MAX) {
709                 DRM_DEBUG_KMS("clock zero count too high %u\n", clk_zero_cnt);
710                 clk_zero_cnt = CLK_ZERO_CNT_MAX;
711         }
712
713         /* trail count */
714         tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
715         trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
716
717         if (trail_cnt > TRAIL_CNT_MAX) {
718                 DRM_DEBUG_KMS("trail count too high %u\n", trail_cnt);
719                 trail_cnt = TRAIL_CNT_MAX;
720         }
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 * mul + EXIT_ZERO_COUNT *
728          *                                      mul + 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 * mul +
742                                                 exit_zero_cnt * mul + 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
779 bool intel_dsi_vbt_init(struct intel_dsi *intel_dsi, u16 panel_id)
780 {
781         struct drm_device *dev = intel_dsi->base.base.dev;
782         struct drm_i915_private *dev_priv = to_i915(dev);
783         struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
784         struct mipi_pps_data *pps = dev_priv->vbt.dsi.pps;
785         struct drm_display_mode *mode = dev_priv->vbt.lfp_lvds_vbt_mode;
786         u16 burst_mode_ratio;
787         enum port port;
788
789         DRM_DEBUG_KMS("\n");
790
791         intel_dsi->eotp_pkt = mipi_config->eot_pkt_disabled ? 0 : 1;
792         intel_dsi->clock_stop = mipi_config->enable_clk_stop ? 1 : 0;
793         intel_dsi->lane_count = mipi_config->lane_cnt + 1;
794         intel_dsi->pixel_format =
795                         pixel_format_from_register_bits(
796                                 mipi_config->videomode_color_format << 7);
797
798         intel_dsi->dual_link = mipi_config->dual_link;
799         intel_dsi->pixel_overlap = mipi_config->pixel_overlap;
800         intel_dsi->operation_mode = mipi_config->is_cmd_mode;
801         intel_dsi->video_mode_format = mipi_config->video_transfer_mode;
802         intel_dsi->escape_clk_div = mipi_config->byte_clk_sel;
803         intel_dsi->lp_rx_timeout = mipi_config->lp_rx_timeout;
804         intel_dsi->hs_tx_timeout = mipi_config->hs_tx_timeout;
805         intel_dsi->turn_arnd_val = mipi_config->turn_around_timeout;
806         intel_dsi->rst_timer_val = mipi_config->device_reset_timer;
807         intel_dsi->init_count = mipi_config->master_init_timer;
808         intel_dsi->bw_timer = mipi_config->dbi_bw_timer;
809         intel_dsi->video_frmt_cfg_bits =
810                 mipi_config->bta_enabled ? DISABLE_VIDEO_BTA : 0;
811         intel_dsi->bgr_enabled = mipi_config->rgb_flip;
812
813         /* Starting point, adjusted depending on dual link and burst mode */
814         intel_dsi->pclk = mode->clock;
815
816         /* In dual link mode each port needs half of pixel clock */
817         if (intel_dsi->dual_link) {
818                 intel_dsi->pclk /= 2;
819
820                 /* we can enable pixel_overlap if needed by panel. In this
821                  * case we need to increase the pixelclock for extra pixels
822                  */
823                 if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
824                         intel_dsi->pclk += DIV_ROUND_UP(mode->vtotal * intel_dsi->pixel_overlap * 60, 1000);
825                 }
826         }
827
828         /* Burst Mode Ratio
829          * Target ddr frequency from VBT / non burst ddr freq
830          * multiply by 100 to preserve remainder
831          */
832         if (intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
833                 if (mipi_config->target_burst_mode_freq) {
834                         u32 bitrate = intel_dsi_bitrate(intel_dsi);
835
836                         if (mipi_config->target_burst_mode_freq < bitrate) {
837                                 DRM_ERROR("Burst mode freq is less than computed\n");
838                                 return false;
839                         }
840
841                         burst_mode_ratio = DIV_ROUND_UP(
842                                 mipi_config->target_burst_mode_freq * 100,
843                                 bitrate);
844
845                         intel_dsi->pclk = DIV_ROUND_UP(intel_dsi->pclk * burst_mode_ratio, 100);
846                 } else {
847                         DRM_ERROR("Burst mode target is not set\n");
848                         return false;
849                 }
850         } else
851                 burst_mode_ratio = 100;
852
853         intel_dsi->burst_mode_ratio = burst_mode_ratio;
854
855         if (IS_ICELAKE(dev_priv))
856                 icl_dphy_param_init(intel_dsi);
857         else
858                 vlv_dphy_param_init(intel_dsi);
859
860         DRM_DEBUG_KMS("Pclk %d\n", intel_dsi->pclk);
861         DRM_DEBUG_KMS("Pixel overlap %d\n", intel_dsi->pixel_overlap);
862         DRM_DEBUG_KMS("Lane count %d\n", intel_dsi->lane_count);
863         DRM_DEBUG_KMS("DPHY param reg 0x%x\n", intel_dsi->dphy_reg);
864         DRM_DEBUG_KMS("Video mode format %s\n",
865                       intel_dsi->video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ?
866                       "non-burst with sync pulse" :
867                       intel_dsi->video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS ?
868                       "non-burst with sync events" :
869                       intel_dsi->video_mode_format == VIDEO_MODE_BURST ?
870                       "burst" : "<unknown>");
871         DRM_DEBUG_KMS("Burst mode ratio %d\n", intel_dsi->burst_mode_ratio);
872         DRM_DEBUG_KMS("Reset timer %d\n", intel_dsi->rst_timer_val);
873         DRM_DEBUG_KMS("Eot %s\n", enableddisabled(intel_dsi->eotp_pkt));
874         DRM_DEBUG_KMS("Clockstop %s\n", enableddisabled(!intel_dsi->clock_stop));
875         DRM_DEBUG_KMS("Mode %s\n", intel_dsi->operation_mode ? "command" : "video");
876         if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
877                 DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_FRONT_BACK\n");
878         else if (intel_dsi->dual_link == DSI_DUAL_LINK_PIXEL_ALT)
879                 DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_PIXEL_ALT\n");
880         else
881                 DRM_DEBUG_KMS("Dual link: NONE\n");
882         DRM_DEBUG_KMS("Pixel Format %d\n", intel_dsi->pixel_format);
883         DRM_DEBUG_KMS("TLPX %d\n", intel_dsi->escape_clk_div);
884         DRM_DEBUG_KMS("LP RX Timeout 0x%x\n", intel_dsi->lp_rx_timeout);
885         DRM_DEBUG_KMS("Turnaround Timeout 0x%x\n", intel_dsi->turn_arnd_val);
886         DRM_DEBUG_KMS("Init Count 0x%x\n", intel_dsi->init_count);
887         DRM_DEBUG_KMS("HS to LP Count 0x%x\n", intel_dsi->hs_to_lp_count);
888         DRM_DEBUG_KMS("LP Byte Clock %d\n", intel_dsi->lp_byte_clk);
889         DRM_DEBUG_KMS("DBI BW Timer 0x%x\n", intel_dsi->bw_timer);
890         DRM_DEBUG_KMS("LP to HS Clock Count 0x%x\n", intel_dsi->clk_lp_to_hs_count);
891         DRM_DEBUG_KMS("HS to LP Clock Count 0x%x\n", intel_dsi->clk_hs_to_lp_count);
892         DRM_DEBUG_KMS("BTA %s\n",
893                         enableddisabled(!(intel_dsi->video_frmt_cfg_bits & DISABLE_VIDEO_BTA)));
894
895         /* delays in VBT are in unit of 100us, so need to convert
896          * here in ms
897          * Delay (100us) * 100 /1000 = Delay / 10 (ms) */
898         intel_dsi->backlight_off_delay = pps->bl_disable_delay / 10;
899         intel_dsi->backlight_on_delay = pps->bl_enable_delay / 10;
900         intel_dsi->panel_on_delay = pps->panel_on_delay / 10;
901         intel_dsi->panel_off_delay = pps->panel_off_delay / 10;
902         intel_dsi->panel_pwr_cycle_delay = pps->panel_power_cycle_delay / 10;
903
904         /* a regular driver would get the device in probe */
905         for_each_dsi_port(port, intel_dsi->ports) {
906                 mipi_dsi_attach(intel_dsi->dsi_hosts[port]->device);
907         }
908
909         return true;
910 }
This page took 0.088946 seconds and 4 git commands to generate.