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