]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/tegra/dsi.c
Merge remote-tracking branch 'md/for-next'
[karo-tx-linux.git] / drivers / gpu / drm / tegra / dsi.c
1 /*
2  * Copyright (C) 2013 NVIDIA Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <linux/clk.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/debugfs.h>
26 #include <linux/host1x.h>
27 #include <linux/module.h>
28 #include <linux/of.h>
29 #include <linux/platform_device.h>
30
31 #include "drm.h"
32 #include "dc.h"
33
34 #define DSI_INCR_SYNCPT                 0x00
35 #define DSI_INCR_SYNCPT_CONTROL         0x01
36 #define DSI_INCR_SYNCPT_ERROR           0x02
37 #define DSI_CTXSW                       0x08
38 #define DSI_RD_DATA                     0x09
39 #define DSI_WR_DATA                     0x0a
40 #define DSI_POWER_CONTROL               0x0b
41 #define DSI_POWER_CONTROL_ENABLE        (1 << 0)
42 #define DSI_INT_ENABLE                  0x0c
43 #define DSI_INT_STATUS                  0x0d
44 #define DSI_INT_MASK                    0x0e
45 #define DSI_HOST_CONTROL                0x0f
46 #define DSI_HOST_CONTROL_RAW            (1 << 6)
47 #define DSI_HOST_CONTROL_HS             (1 << 5)
48 #define DSI_HOST_CONTROL_CS             (1 << 1)
49 #define DSI_HOST_CONTROL_ECC            (1 << 0)
50 #define DSI_CONTROL                     0x10
51 #define DSI_CONTROL_HS_CLK_CTRL         (1 << 20)
52 #define DSI_CONTROL_CHANNEL(c)          (((c) & 0x3) << 16)
53 #define DSI_CONTROL_FORMAT(f)           (((f) & 0x3) << 12)
54 #define DSI_CONTROL_TX_TRIG(x)          (((x) & 0x3) <<  8)
55 #define DSI_CONTROL_LANES(n)            (((n) & 0x3) <<  4)
56 #define DSI_CONTROL_DCS_ENABLE          (1 << 3)
57 #define DSI_CONTROL_SOURCE(s)           (((s) & 0x1) <<  2)
58 #define DSI_CONTROL_VIDEO_ENABLE        (1 << 1)
59 #define DSI_CONTROL_HOST_ENABLE         (1 << 0)
60 #define DSI_SOL_DELAY                   0x11
61 #define DSI_MAX_THRESHOLD               0x12
62 #define DSI_TRIGGER                     0x13
63 #define DSI_TX_CRC                      0x14
64 #define DSI_STATUS                      0x15
65 #define DSI_INIT_SEQ_CONTROL            0x1a
66 #define DSI_INIT_SEQ_DATA_0             0x1b
67 #define DSI_INIT_SEQ_DATA_1             0x1c
68 #define DSI_INIT_SEQ_DATA_2             0x1d
69 #define DSI_INIT_SEQ_DATA_3             0x1e
70 #define DSI_INIT_SEQ_DATA_4             0x1f
71 #define DSI_INIT_SEQ_DATA_5             0x20
72 #define DSI_INIT_SEQ_DATA_6             0x21
73 #define DSI_INIT_SEQ_DATA_7             0x22
74 #define DSI_PKT_SEQ_0_LO                0x23
75 #define DSI_PKT_SEQ_0_HI                0x24
76 #define DSI_PKT_SEQ_1_LO                0x25
77 #define DSI_PKT_SEQ_1_HI                0x26
78 #define DSI_PKT_SEQ_2_LO                0x27
79 #define DSI_PKT_SEQ_2_HI                0x28
80 #define DSI_PKT_SEQ_3_LO                0x29
81 #define DSI_PKT_SEQ_3_HI                0x2a
82 #define DSI_PKT_SEQ_4_LO                0x2b
83 #define DSI_PKT_SEQ_4_HI                0x2c
84 #define DSI_PKT_SEQ_5_LO                0x2d
85 #define DSI_PKT_SEQ_5_HI                0x2e
86 #define DSI_DCS_CMDS                    0x33
87 #define DSI_PKT_LEN_0_1                 0x34
88 #define DSI_PKT_LEN_2_3                 0x35
89 #define DSI_PKT_LEN_4_5                 0x36
90 #define DSI_PKT_LEN_6_7                 0x37
91 #define DSI_PHY_TIMING_0                0x3c
92 #define DSI_PHY_TIMING_1                0x3d
93 #define DSI_PHY_TIMING_2                0x3e
94 #define DSI_BTA_TIMING                  0x3f
95
96 #define DSI_TIMING_FIELD(value, period, hwinc) \
97         ((DIV_ROUND_CLOSEST(value, period) - (hwinc)) & 0xff)
98
99 #define DSI_TIMEOUT_0                   0x44
100 #define DSI_TIMEOUT_LRX(x)              (((x) & 0xffff) << 16)
101 #define DSI_TIMEOUT_HTX(x)              (((x) & 0xffff) <<  0)
102 #define DSI_TIMEOUT_1                   0x45
103 #define DSI_TIMEOUT_PR(x)               (((x) & 0xffff) << 16)
104 #define DSI_TIMEOUT_TA(x)               (((x) & 0xffff) <<  0)
105 #define DSI_TO_TALLY                    0x46
106 #define DSI_TALLY_TA(x)                 (((x) & 0xff) << 16)
107 #define DSI_TALLY_LRX(x)                (((x) & 0xff) <<  8)
108 #define DSI_TALLY_HTX(x)                (((x) & 0xff) <<  0)
109 #define DSI_PAD_CONTROL_0               0x4b
110 #define DSI_PAD_CONTROL_VS1_PDIO(x)     (((x) & 0xf) <<  0)
111 #define DSI_PAD_CONTROL_VS1_PDIO_CLK    (1 <<  8)
112 #define DSI_PAD_CONTROL_VS1_PULLDN(x)   (((x) & 0xf) << 16)
113 #define DSI_PAD_CONTROL_VS1_PULLDN_CLK  (1 << 24)
114 #define DSI_PAD_CONTROL_CD              0x4c
115 #define DSI_PAD_CD_STATUS               0x4d
116 #define DSI_VIDEO_MODE_CONTROL          0x4e
117 #define DSI_PAD_CONTROL_1               0x4f
118 #define DSI_PAD_CONTROL_2               0x50
119 #define DSI_PAD_OUT_CLK(x)              (((x) & 0x7) <<  0)
120 #define DSI_PAD_LP_DN(x)                (((x) & 0x7) <<  4)
121 #define DSI_PAD_LP_UP(x)                (((x) & 0x7) <<  8)
122 #define DSI_PAD_SLEW_DN(x)              (((x) & 0x7) << 12)
123 #define DSI_PAD_SLEW_UP(x)              (((x) & 0x7) << 16)
124 #define DSI_PAD_CONTROL_3               0x51
125 #define DSI_PAD_CONTROL_4               0x52
126 #define DSI_GANGED_MODE_CONTROL         0x53
127 #define DSI_GANGED_MODE_START           0x54
128 #define DSI_GANGED_MODE_SIZE            0x55
129 #define DSI_RAW_DATA_BYTE_COUNT         0x56
130 #define DSI_ULTRA_LOW_POWER_CONTROL     0x57
131 #define DSI_INIT_SEQ_DATA_8             0x58
132 #define DSI_INIT_SEQ_DATA_9             0x59
133 #define DSI_INIT_SEQ_DATA_10            0x5a
134 #define DSI_INIT_SEQ_DATA_11            0x5b
135 #define DSI_INIT_SEQ_DATA_12            0x5c
136 #define DSI_INIT_SEQ_DATA_13            0x5d
137 #define DSI_INIT_SEQ_DATA_14            0x5e
138 #define DSI_INIT_SEQ_DATA_15            0x5f
139
140 enum dsi_format {
141         DSI_FORMAT_16P,
142         DSI_FORMAT_18NP,
143         DSI_FORMAT_18P,
144         DSI_FORMAT_24P,
145 };
146
147 /*
148  * D-PHY timing parameters
149  *
150  * A detailed description of these parameters can be found in the  MIPI
151  * Alliance Specification for D-PHY, Section 5.9 "Global Operation Timing
152  * Parameters".
153  *
154  * All parameters are specified in nanoseconds.
155  */
156 struct dsi_phy_timing {
157         unsigned int clkmiss;
158         unsigned int clkpost;
159         unsigned int clkpre;
160         unsigned int clkprepare;
161         unsigned int clksettle;
162         unsigned int clktermen;
163         unsigned int clktrail;
164         unsigned int clkzero;
165         unsigned int dtermen;
166         unsigned int eot;
167         unsigned int hsexit;
168         unsigned int hsprepare;
169         unsigned int hszero;
170         unsigned int hssettle;
171         unsigned int hsskip;
172         unsigned int hstrail;
173         unsigned int init;
174         unsigned int lpx;
175         unsigned int taget;
176         unsigned int tago;
177         unsigned int tasure;
178         unsigned int wakeup;
179 };
180
181 /*
182  * Default D-PHY timings based on MIPI D-PHY specification. Derived from
183  * the valid ranges specified in Section 5.9 of the D-PHY specification
184  * with minor adjustments.
185  */
186 static int dsi_phy_timing_get_default(struct dsi_phy_timing *timing,
187                                       unsigned long period)
188 {
189         timing->clkmiss = 0;
190         timing->clkpost = 70 + 52 * period;
191         timing->clkpre = 8;
192         timing->clkprepare = 65;
193         timing->clksettle = 95;
194         timing->clktermen = 0;
195         timing->clktrail = 80;
196         timing->clkzero = 260;
197         timing->dtermen = 0;
198         timing->eot = 0;
199         timing->hsexit = 120;
200         timing->hsprepare = 65 + 5 * period;
201         timing->hszero = 145 + 5 * period;
202         timing->hssettle = 85 + 6 * period;
203         timing->hsskip = 40;
204         timing->hstrail = max(8 * period, 60 + 4 * period);
205         timing->init = 100000;
206         timing->lpx = 60;
207         timing->taget = 5 * timing->lpx;
208         timing->tago = 4 * timing->lpx;
209         timing->tasure = 2 * timing->lpx;
210         timing->wakeup = 1000000;
211
212         return 0;
213 }
214
215 /*
216  * Validate D-PHY timing according to MIPI Alliance Specification for D-PHY,
217  * Section 5.9 "Global Operation Timing Parameters".
218  */
219 static int dsi_phy_timing_validate(struct dsi_phy_timing *timing,
220                                    unsigned long period)
221 {
222         if (timing->clkmiss > 60)
223                 return -EINVAL;
224
225         if (timing->clkpost < (60 + 52 * period))
226                 return -EINVAL;
227
228         if (timing->clkpre < 8)
229                 return -EINVAL;
230
231         if (timing->clkprepare < 38 || timing->clkprepare > 95)
232                 return -EINVAL;
233
234         if (timing->clksettle < 95 || timing->clksettle > 300)
235                 return -EINVAL;
236
237         if (timing->clktermen > 38)
238                 return -EINVAL;
239
240         if (timing->clktrail < 60)
241                 return -EINVAL;
242
243         if (timing->clkprepare + timing->clkzero < 300)
244                 return -EINVAL;
245
246         if (timing->dtermen > 35 + 4 * period)
247                 return -EINVAL;
248
249         if (timing->eot > 105 + 12 * period)
250                 return -EINVAL;
251
252         if (timing->hsexit < 100)
253                 return -EINVAL;
254
255         if (timing->hsprepare < 40 + 4 * period ||
256             timing->hsprepare > 85 + 6 * period)
257                 return -EINVAL;
258
259         if (timing->hsprepare + timing->hszero < 145 + 10 * period)
260                 return -EINVAL;
261
262         if ((timing->hssettle < 85 + 6 * period) ||
263             (timing->hssettle > 145 + 10 * period))
264                 return -EINVAL;
265
266         if (timing->hsskip < 40 || timing->hsskip > 55 + 4 * period)
267                 return -EINVAL;
268
269         if (timing->hstrail < max(8 * period, 60 + 4 * period))
270                 return -EINVAL;
271
272         if (timing->init < 100000)
273                 return -EINVAL;
274
275         if (timing->lpx < 50)
276                 return -EINVAL;
277
278         if (timing->taget != 5 * timing->lpx)
279                 return -EINVAL;
280
281         if (timing->tago != 4 * timing->lpx)
282                 return -EINVAL;
283
284         if (timing->tasure < timing->lpx || timing->tasure > 2 * timing->lpx)
285                 return -EINVAL;
286
287         if (timing->wakeup < 1000000)
288                 return -EINVAL;
289
290         return 0;
291 }
292
293 struct tegra_dsi {
294         struct host1x_client client;
295         struct tegra_output output;
296         struct device *dev;
297
298         void __iomem *regs;
299
300         struct clk *clk_parent;
301         struct clk *clk;
302
303         struct drm_info_list *debugfs_files;
304         struct drm_minor *minor;
305         struct dentry *debugfs;
306
307         enum dsi_format format;
308         unsigned int lanes;
309 };
310
311 static inline struct tegra_dsi *
312 host1x_client_to_dsi(struct host1x_client *client)
313 {
314         return container_of(client, struct tegra_dsi, client);
315 }
316
317 static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
318 {
319         return container_of(output, struct tegra_dsi, output);
320 }
321
322 static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
323                                             unsigned long reg)
324 {
325         return readl(dsi->regs + (reg << 2));
326 }
327
328 static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
329                                     unsigned long reg)
330 {
331         writel(value, dsi->regs + (reg << 2));
332 }
333
334 static int tegra_dsi_show_regs(struct seq_file *s, void *data)
335 {
336         struct drm_info_node *node = s->private;
337         struct tegra_dsi *dsi = node->info_ent->data;
338
339 #define DUMP_REG(name)                                          \
340         seq_printf(s, "%-32s %#05x %08lx\n", #name, name,       \
341                    tegra_dsi_readl(dsi, name))
342
343         DUMP_REG(DSI_INCR_SYNCPT);
344         DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
345         DUMP_REG(DSI_INCR_SYNCPT_ERROR);
346         DUMP_REG(DSI_CTXSW);
347         DUMP_REG(DSI_RD_DATA);
348         DUMP_REG(DSI_WR_DATA);
349         DUMP_REG(DSI_POWER_CONTROL);
350         DUMP_REG(DSI_INT_ENABLE);
351         DUMP_REG(DSI_INT_STATUS);
352         DUMP_REG(DSI_INT_MASK);
353         DUMP_REG(DSI_HOST_CONTROL);
354         DUMP_REG(DSI_CONTROL);
355         DUMP_REG(DSI_SOL_DELAY);
356         DUMP_REG(DSI_MAX_THRESHOLD);
357         DUMP_REG(DSI_TRIGGER);
358         DUMP_REG(DSI_TX_CRC);
359         DUMP_REG(DSI_STATUS);
360
361         DUMP_REG(DSI_INIT_SEQ_CONTROL);
362         DUMP_REG(DSI_INIT_SEQ_DATA_0);
363         DUMP_REG(DSI_INIT_SEQ_DATA_1);
364         DUMP_REG(DSI_INIT_SEQ_DATA_2);
365         DUMP_REG(DSI_INIT_SEQ_DATA_3);
366         DUMP_REG(DSI_INIT_SEQ_DATA_4);
367         DUMP_REG(DSI_INIT_SEQ_DATA_5);
368         DUMP_REG(DSI_INIT_SEQ_DATA_6);
369         DUMP_REG(DSI_INIT_SEQ_DATA_7);
370
371         DUMP_REG(DSI_PKT_SEQ_0_LO);
372         DUMP_REG(DSI_PKT_SEQ_0_HI);
373         DUMP_REG(DSI_PKT_SEQ_1_LO);
374         DUMP_REG(DSI_PKT_SEQ_1_HI);
375         DUMP_REG(DSI_PKT_SEQ_2_LO);
376         DUMP_REG(DSI_PKT_SEQ_2_HI);
377         DUMP_REG(DSI_PKT_SEQ_3_LO);
378         DUMP_REG(DSI_PKT_SEQ_3_HI);
379         DUMP_REG(DSI_PKT_SEQ_4_LO);
380         DUMP_REG(DSI_PKT_SEQ_4_HI);
381         DUMP_REG(DSI_PKT_SEQ_5_LO);
382         DUMP_REG(DSI_PKT_SEQ_5_HI);
383
384         DUMP_REG(DSI_DCS_CMDS);
385
386         DUMP_REG(DSI_PKT_LEN_0_1);
387         DUMP_REG(DSI_PKT_LEN_2_3);
388         DUMP_REG(DSI_PKT_LEN_4_5);
389         DUMP_REG(DSI_PKT_LEN_6_7);
390
391         DUMP_REG(DSI_PHY_TIMING_0);
392         DUMP_REG(DSI_PHY_TIMING_1);
393         DUMP_REG(DSI_PHY_TIMING_2);
394         DUMP_REG(DSI_BTA_TIMING);
395
396         DUMP_REG(DSI_TIMEOUT_0);
397         DUMP_REG(DSI_TIMEOUT_1);
398         DUMP_REG(DSI_TO_TALLY);
399
400         DUMP_REG(DSI_PAD_CONTROL_0);
401         DUMP_REG(DSI_PAD_CONTROL_CD);
402         DUMP_REG(DSI_PAD_CD_STATUS);
403         DUMP_REG(DSI_VIDEO_MODE_CONTROL);
404         DUMP_REG(DSI_PAD_CONTROL_1);
405         DUMP_REG(DSI_PAD_CONTROL_2);
406         DUMP_REG(DSI_PAD_CONTROL_3);
407         DUMP_REG(DSI_PAD_CONTROL_4);
408
409         DUMP_REG(DSI_GANGED_MODE_CONTROL);
410         DUMP_REG(DSI_GANGED_MODE_START);
411         DUMP_REG(DSI_GANGED_MODE_SIZE);
412
413         DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
414         DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
415
416         DUMP_REG(DSI_INIT_SEQ_DATA_8);
417         DUMP_REG(DSI_INIT_SEQ_DATA_9);
418         DUMP_REG(DSI_INIT_SEQ_DATA_10);
419         DUMP_REG(DSI_INIT_SEQ_DATA_11);
420         DUMP_REG(DSI_INIT_SEQ_DATA_12);
421         DUMP_REG(DSI_INIT_SEQ_DATA_13);
422         DUMP_REG(DSI_INIT_SEQ_DATA_14);
423         DUMP_REG(DSI_INIT_SEQ_DATA_15);
424
425 #undef DUMP_REG
426
427         return 0;
428 }
429
430 static struct drm_info_list debugfs_files[] = {
431         { "regs", tegra_dsi_show_regs, 0, NULL },
432 };
433
434 static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
435                                   struct drm_minor *minor)
436 {
437         const char *name = dev_name(dsi->dev);
438         unsigned int i;
439         int err;
440
441         dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
442         if (!dsi->debugfs)
443                 return -ENOMEM;
444
445         dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
446                                      GFP_KERNEL);
447         if (!dsi->debugfs_files) {
448                 err = -ENOMEM;
449                 goto remove;
450         }
451
452         for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
453                 dsi->debugfs_files[i].data = dsi;
454
455         err = drm_debugfs_create_files(dsi->debugfs_files,
456                                        ARRAY_SIZE(debugfs_files),
457                                        dsi->debugfs, minor);
458         if (err < 0)
459                 goto free;
460
461         dsi->minor = minor;
462
463         return 0;
464
465 free:
466         kfree(dsi->debugfs_files);
467         dsi->debugfs_files = NULL;
468 remove:
469         debugfs_remove(dsi->debugfs);
470         dsi->debugfs = NULL;
471
472         return err;
473 }
474
475 static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
476 {
477         drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
478                                  dsi->minor);
479         dsi->minor = NULL;
480
481         kfree(dsi->debugfs_files);
482         dsi->debugfs_files = NULL;
483
484         debugfs_remove(dsi->debugfs);
485         dsi->debugfs = NULL;
486
487         return 0;
488 }
489
490 enum {
491         CMD_VS = 0x01,
492         CMD_VE = 0x11,
493
494         CMD_HS = 0x21,
495         CMD_HE = 0x31,
496
497         CMD_EOT = 0x08,
498         CMD_NULL = 0x09,
499         CMD_SHORTW = 0x15,
500         CMD_BLNK = 0x19,
501         CMD_LONGW = 0x39,
502
503         CMD_RGB = 0x00,
504         CMD_RGB_16BPP = 0x0E,
505         CMD_RGB_18BPP = 0x1E,
506         CMD_RGB_18BPPNP = 0x2E,
507         CMD_RGB_24BPP = 0x3E,
508 };
509
510 #define PKT_ID0(id)     ((((id) & 0x3f) << 3) | (1 << 9))
511 #define PKT_LEN0(len)   (((len) & 0x7) << 0)
512 #define PKT_ID1(id)     ((((id) & 0x3f) << 13) | (1 << 19))
513 #define PKT_LEN1(len)   (((len) & 0x7) << 10)
514 #define PKT_ID2(id)     ((((id) & 0x3f) << 23) | (1 << 29))
515 #define PKT_LEN2(len)   (((len) & 0x7) << 20)
516 #define PKT_ID3(id)     ((((id) & 0x3f) << 3) | (1 << 9))
517 #define PKT_LEN3(len)   (((len) & 0x7) << 0)
518 #define PKT_ID4(id)     ((((id) & 0x3f) << 13) | (1 << 19))
519 #define PKT_LEN4(len)   (((len) & 0x7) << 10)
520 #define PKT_ID5(id)     ((((id) & 0x3f) << 23) | (1 << 29))
521 #define PKT_LEN5(len)   (((len) & 0x7) << 20)
522 #define PKT_LP          (1 << 30)
523 #define NUM_PKT_SEQ     12
524
525 /* non-burst mode with sync-end */
526 static const u32 pkt_seq_vnb_syne[NUM_PKT_SEQ] = {
527         PKT_ID0(CMD_VS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
528         PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
529         0,
530         PKT_ID0(CMD_VE) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
531         PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
532         0,
533         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
534         PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
535         0,
536         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
537         PKT_ID2(CMD_HE) | PKT_LEN2(0),
538         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB_24BPP) | PKT_LEN4(3) |
539         PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
540         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
541         PKT_ID2(CMD_HE) | PKT_LEN2(0) | PKT_LP,
542         0,
543         PKT_ID0(CMD_HS) | PKT_LEN0(0) | PKT_ID1(CMD_BLNK) | PKT_LEN1(1) |
544         PKT_ID2(CMD_HE) | PKT_LEN2(0),
545         PKT_ID3(CMD_BLNK) | PKT_LEN3(2) | PKT_ID4(CMD_RGB_24BPP) | PKT_LEN4(3) |
546         PKT_ID5(CMD_BLNK) | PKT_LEN5(4),
547 };
548
549 static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
550 {
551         struct dsi_phy_timing timing;
552         unsigned long value, period;
553         long rate;
554         int err;
555
556         rate = clk_get_rate(dsi->clk);
557         if (rate < 0)
558                 return rate;
559
560         period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
561
562         err = dsi_phy_timing_get_default(&timing, period);
563         if (err < 0)
564                 return err;
565
566         err = dsi_phy_timing_validate(&timing, period);
567         if (err < 0) {
568                 dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
569                 return err;
570         }
571
572         /*
573          * The D-PHY timing fields below are expressed in byte-clock cycles,
574          * so multiply the period by 8.
575          */
576         period *= 8;
577
578         value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
579                 DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
580                 DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
581                 DSI_TIMING_FIELD(timing.hsprepare, period, 1);
582         tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
583
584         value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
585                 DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
586                 DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
587                 DSI_TIMING_FIELD(timing.lpx, period, 1);
588         tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
589
590         value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
591                 DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
592                 DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
593         tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
594
595         value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
596                 DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
597                 DSI_TIMING_FIELD(timing.tago, period, 1);
598         tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
599
600         return 0;
601 }
602
603 static int tegra_dsi_get_muldiv(enum dsi_format format, unsigned int *mulp,
604                                 unsigned int *divp)
605 {
606         switch (format) {
607         case DSI_FORMAT_16P:
608                 *mulp = 2;
609                 *divp = 1;
610                 break;
611
612         case DSI_FORMAT_18NP:
613                 *mulp = 9;
614                 *divp = 4;
615                 break;
616
617         case DSI_FORMAT_18P:
618         case DSI_FORMAT_24P:
619                 *mulp = 3;
620                 *divp = 1;
621                 break;
622
623         default:
624                 return -EINVAL;
625         }
626
627         return 0;
628 }
629
630 static int tegra_output_dsi_enable(struct tegra_output *output)
631 {
632         struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
633         struct drm_display_mode *mode = &dc->base.mode;
634         unsigned int hact, hsw, hbp, hfp, i, mul, div;
635         struct tegra_dsi *dsi = to_dsi(output);
636         /* FIXME: don't hardcode this */
637         const u32 *pkt_seq = pkt_seq_vnb_syne;
638         unsigned long value;
639         int err;
640
641         err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
642         if (err < 0)
643                 return err;
644
645         err = clk_enable(dsi->clk);
646         if (err < 0)
647                 return err;
648
649         tegra_periph_reset_deassert(dsi->clk);
650
651         value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(dsi->format) |
652                 DSI_CONTROL_LANES(dsi->lanes - 1) | DSI_CONTROL_SOURCE(0);
653         tegra_dsi_writel(dsi, value, DSI_CONTROL);
654
655         tegra_dc_writel(dc, DSI_ENABLE, DC_DISP_DISP_WIN_OPTIONS);
656
657         value = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
658                 PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
659         tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
660
661         tegra_dc_writel(dc, DISP_CTRL_MODE_C_DISPLAY, DC_CMD_DISPLAY_COMMAND);
662         tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
663         tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
664
665         tegra_dsi_writel(dsi, 0x000001e0, DSI_MAX_THRESHOLD);
666
667         value = DSI_HOST_CONTROL_HS | DSI_HOST_CONTROL_CS |
668                 DSI_HOST_CONTROL_ECC;
669         tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
670
671         value = tegra_dsi_readl(dsi, DSI_CONTROL);
672         value |= DSI_CONTROL_HS_CLK_CTRL;
673         value &= ~DSI_CONTROL_TX_TRIG(3);
674         value &= ~DSI_CONTROL_DCS_ENABLE;
675         value |= DSI_CONTROL_VIDEO_ENABLE;
676         value &= ~DSI_CONTROL_HOST_ENABLE;
677         tegra_dsi_writel(dsi, value, DSI_CONTROL);
678
679         err = tegra_dsi_set_phy_timing(dsi);
680         if (err < 0)
681                 return err;
682
683         for (i = 0; i < NUM_PKT_SEQ; i++)
684                 tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
685
686         /* horizontal active pixels */
687         hact = mode->hdisplay * mul / div;
688
689         /* horizontal sync width */
690         hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
691         hsw -= 10;
692
693         /* horizontal back porch */
694         hbp = (mode->htotal - mode->hsync_end) * mul / div;
695         hbp -= 14;
696
697         /* horizontal front porch */
698         hfp = (mode->hsync_start  - mode->hdisplay) * mul / div;
699         hfp -= 8;
700
701         tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
702         tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
703         tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
704         tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
705
706         /* set SOL delay */
707         tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
708
709         return 0;
710 }
711
712 static int tegra_output_dsi_disable(struct tegra_output *output)
713 {
714         struct tegra_dsi *dsi = to_dsi(output);
715
716         clk_disable(dsi->clk);
717
718         return 0;
719 }
720
721 static int tegra_output_dsi_setup_clock(struct tegra_output *output,
722                                         struct clk *clk, unsigned long pclk)
723 {
724         struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
725         struct drm_display_mode *mode = &dc->base.mode;
726         struct tegra_dsi *dsi = to_dsi(output);
727         unsigned long bclk, plld, value;
728         unsigned int timeout, mul, div;
729         struct clk *base;
730         int err;
731
732         err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
733         if (err < 0)
734                 return err;
735
736         pclk = mode->htotal * mode->vtotal * mode->vrefresh;
737         bclk = (pclk * mul) / (div * dsi->lanes);
738         plld = DIV_ROUND_UP(bclk * 8, 1000000);
739         pclk = (plld * 1000000) / 2;
740
741         err = clk_set_parent(clk, dsi->clk_parent);
742         if (err < 0) {
743                 dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
744                 return err;
745         }
746
747         base = clk_get_parent(dsi->clk_parent);
748
749         /*
750          * This assumes that the parent clock is pll_d_out0 or pll_d2_out
751          * respectively, each of which divides the base pll_d by 2.
752          */
753         err = clk_set_rate(base, pclk * 2);
754         if (err < 0) {
755                 dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
756                         pclk * 2);
757                 return err;
758         }
759
760         /* one frame high-speed transmission timeout */
761         timeout = (bclk / mode->vrefresh) / 512;
762         value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
763         tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
764
765         /* 2 ms peripheral timeout for panel */
766         timeout = 2 * bclk / 512 * 1000;
767         value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
768         tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
769
770         value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
771         tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
772
773         return 0;
774 }
775
776 static int tegra_output_dsi_check_mode(struct tegra_output *output,
777                                        struct drm_display_mode *mode,
778                                        enum drm_mode_status *status)
779 {
780         /*
781          * FIXME: For now, always assume that the mode is okay.
782          */
783
784         *status = MODE_OK;
785
786         return 0;
787 }
788
789 static const struct tegra_output_ops dsi_ops = {
790         .enable = tegra_output_dsi_enable,
791         .disable = tegra_output_dsi_disable,
792         .setup_clock = tegra_output_dsi_setup_clock,
793         .check_mode = tegra_output_dsi_check_mode,
794 };
795
796 static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
797 {
798         unsigned long value;
799
800         value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
801         tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
802
803         return 0;
804 }
805
806 static int tegra_dsi_init(struct host1x_client *client)
807 {
808         struct tegra_drm *tegra = dev_get_drvdata(client->parent);
809         struct tegra_dsi *dsi = host1x_client_to_dsi(client);
810         unsigned long value, i;
811         int err;
812
813         dsi->output.type = TEGRA_OUTPUT_DSI;
814         dsi->output.dev = client->dev;
815         dsi->output.ops = &dsi_ops;
816
817         err = tegra_output_init(tegra->drm, &dsi->output);
818         if (err < 0) {
819                 dev_err(client->dev, "output setup failed: %d\n", err);
820                 return err;
821         }
822
823         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
824                 err = tegra_dsi_debugfs_init(dsi, tegra->drm->primary);
825                 if (err < 0)
826                         dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
827         }
828
829         /*
830          * enable high-speed mode, checksum generation, ECC generation and
831          * disable raw mode
832          */
833         value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
834         value |= DSI_HOST_CONTROL_ECC | DSI_HOST_CONTROL_CS |
835                  DSI_HOST_CONTROL_HS;
836         value &= ~DSI_HOST_CONTROL_RAW;
837         tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
838
839         tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
840         tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
841
842         tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
843
844         for (i = 0; i < 8; i++) {
845                 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
846                 tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
847         }
848
849         for (i = 0; i < 12; i++)
850                 tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
851
852         tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
853
854         tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
855         tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
856         tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
857         tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
858         tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
859
860         /* start calibration */
861         tegra_dsi_pad_enable(dsi);
862
863         value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
864                 DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
865                 DSI_PAD_OUT_CLK(0x0);
866         tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
867
868         err = tegra_mipi_calibrate(dsi->dev);
869         if (err < 0) {
870                 dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
871                 return err;
872         }
873
874         tegra_dsi_writel(dsi, DSI_POWER_CONTROL_ENABLE, DSI_POWER_CONTROL);
875         usleep_range(300, 1000);
876
877         return 0;
878 }
879
880 static int tegra_dsi_exit(struct host1x_client *client)
881 {
882         struct tegra_dsi *dsi = host1x_client_to_dsi(client);
883         int err;
884
885         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
886                 err = tegra_dsi_debugfs_exit(dsi);
887                 if (err < 0)
888                         dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
889         }
890
891         err = tegra_output_disable(&dsi->output);
892         if (err < 0) {
893                 dev_err(client->dev, "output failed to disable: %d\n", err);
894                 return err;
895         }
896
897         err = tegra_output_exit(&dsi->output);
898         if (err < 0) {
899                 dev_err(client->dev, "output cleanup failed: %d\n", err);
900                 return err;
901         }
902
903         return 0;
904 }
905
906 static const struct host1x_client_ops dsi_client_ops = {
907         .init = tegra_dsi_init,
908         .exit = tegra_dsi_exit,
909 };
910
911 static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
912 {
913         struct clk *parent;
914         int err;
915
916         parent = clk_get_parent(dsi->clk);
917         if (!parent)
918                 return -EINVAL;
919
920         err = clk_set_parent(parent, dsi->clk_parent);
921         if (err < 0)
922                 return err;
923
924         return 0;
925 }
926
927 static int tegra_dsi_probe(struct platform_device *pdev)
928 {
929         struct tegra_dsi *dsi;
930         struct resource *regs;
931         int err;
932
933         dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
934         if (!dsi)
935                 return -ENOMEM;
936
937         dsi->output.dev = dsi->dev = &pdev->dev;
938
939         err = tegra_output_probe(&dsi->output);
940         if (err < 0)
941                 return err;
942
943         /*
944          * FIXME: Don't hardcode these. Perhaps they should be queried from
945          *        the panel or from the DSI interface's DT node.
946          */
947         dsi->format = DSI_FORMAT_24P;
948         dsi->lanes = 4;
949
950         dsi->clk = devm_clk_get(&pdev->dev, NULL);
951         if (IS_ERR(dsi->clk))
952                 return PTR_ERR(dsi->clk);;
953
954         err = clk_prepare_enable(dsi->clk);
955         if (err < 0)
956                 return err;
957
958         dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
959         if (IS_ERR(dsi->clk_parent))
960                 return PTR_ERR(dsi->clk_parent);
961
962         err = clk_prepare_enable(dsi->clk_parent);
963         if (err < 0)
964                 return err;
965
966         err = tegra_dsi_setup_clocks(dsi);
967         if (err < 0)
968                 return err;
969
970         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
971         dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
972         if (!dsi->regs)
973                 return -EADDRNOTAVAIL;
974
975         INIT_LIST_HEAD(&dsi->client.list);
976         dsi->client.ops = &dsi_client_ops;
977         dsi->client.dev = &pdev->dev;
978
979         err = host1x_client_register(&dsi->client);
980         if (err < 0) {
981                 dev_err(&pdev->dev, "failed to register host1x client: %d\n",
982                         err);
983                 return err;
984         }
985
986         platform_set_drvdata(pdev, dsi);
987
988         return 0;
989 }
990
991 static int tegra_dsi_remove(struct platform_device *pdev)
992 {
993         struct tegra_dsi *dsi = platform_get_drvdata(pdev);
994         int err;
995
996         err = host1x_client_unregister(&dsi->client);
997         if (err < 0) {
998                 dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
999                         err);
1000                 return err;
1001         }
1002
1003         clk_disable_unprepare(dsi->clk_parent);
1004         clk_disable_unprepare(dsi->clk);
1005
1006         err = tegra_output_remove(&dsi->output);
1007         if (err < 0) {
1008                 dev_err(&pdev->dev, "failed to remove output: %d\n", err);
1009                 return err;
1010         }
1011
1012         return 0;
1013 }
1014
1015 static const struct of_device_id tegra_dsi_of_match[] = {
1016         { .compatible = "nvidia,tegra114-dsi", },
1017         { },
1018 };
1019
1020 struct platform_driver tegra_dsi_driver = {
1021         .driver = {
1022                 .name = "tegra-dsi",
1023                 .of_match_table = tegra_dsi_of_match,
1024         },
1025         .probe = tegra_dsi_probe,
1026         .remove = tegra_dsi_remove,
1027 };