blob: 576262e405d60bc2fc98f6ca0fd927cb1eb6561c [file] [log] [blame]
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2013 NVIDIA Corporation
4 */
5
Svyatoslav Ryhelfeddf9f2023-03-27 11:11:48 +03006#include <linux/err.h>
7
8#include "mipi-phy.h"
9
10/*
11 * Default D-PHY timings based on MIPI D-PHY specification. Derived from the
12 * valid ranges specified in Section 6.9, Table 14, Page 40 of the D-PHY
13 * specification (v1.2) with minor adjustments.
14 */
15int mipi_dphy_timing_get_default(struct mipi_dphy_timing *timing,
16 unsigned long period)
17{
18 timing->clkmiss = 0;
19 timing->clkpost = 70 + 52 * period;
20 timing->clkpre = 8;
21 timing->clkprepare = 65;
22 timing->clksettle = 95;
23 timing->clktermen = 0;
24 timing->clktrail = 80;
25 timing->clkzero = 260;
26 timing->dtermen = 0;
27 timing->eot = 0;
28 timing->hsexit = 120;
29 timing->hsprepare = 65 + 5 * period;
30 timing->hszero = 145 + 5 * period;
31 timing->hssettle = 85 + 6 * period;
32 timing->hsskip = 40;
33
34 /*
35 * The MIPI D-PHY specification (Section 6.9, v1.2, Table 14, Page 40)
36 * contains this formula as:
37 *
38 * T_HS-TRAIL = max(n * 8 * period, 60 + n * 4 * period)
39 *
40 * where n = 1 for forward-direction HS mode and n = 4 for reverse-
41 * direction HS mode. There's only one setting and this function does
42 * not parameterize on anything other that period, so this code will
43 * assumes that reverse-direction HS mode is supported and uses n = 4.
44 */
45 timing->hstrail = max(4 * 8 * period, 60 + 4 * 4 * period);
46
47 timing->init = 100000;
48 timing->lpx = 60;
49 timing->taget = 5 * timing->lpx;
50 timing->tago = 4 * timing->lpx;
51 timing->tasure = 2 * timing->lpx;
52 timing->wakeup = 1000000;
53
54 return 0;
55}
56
57/*
58 * Validate D-PHY timing according to MIPI D-PHY specification
59 * (v1.2, Section 6.9 "Global Operation Timing Parameters").
60 */
61int mipi_dphy_timing_validate(struct mipi_dphy_timing *timing,
62 unsigned long period)
63{
64 if (timing->clkmiss > 60)
65 return -EINVAL;
66
67 if (timing->clkpost < (60 + 52 * period))
68 return -EINVAL;
69
70 if (timing->clkpre < 8)
71 return -EINVAL;
72
73 if (timing->clkprepare < 38 || timing->clkprepare > 95)
74 return -EINVAL;
75
76 if (timing->clksettle < 95 || timing->clksettle > 300)
77 return -EINVAL;
78
79 if (timing->clktermen > 38)
80 return -EINVAL;
81
82 if (timing->clktrail < 60)
83 return -EINVAL;
84
85 if (timing->clkprepare + timing->clkzero < 300)
86 return -EINVAL;
87
88 if (timing->dtermen > 35 + 4 * period)
89 return -EINVAL;
90
91 if (timing->eot > 105 + 12 * period)
92 return -EINVAL;
93
94 if (timing->hsexit < 100)
95 return -EINVAL;
96
97 if (timing->hsprepare < 40 + 4 * period ||
98 timing->hsprepare > 85 + 6 * period)
99 return -EINVAL;
100
101 if (timing->hsprepare + timing->hszero < 145 + 10 * period)
102 return -EINVAL;
103
104 if ((timing->hssettle < 85 + 6 * period) ||
105 (timing->hssettle > 145 + 10 * period))
106 return -EINVAL;
107
108 if (timing->hsskip < 40 || timing->hsskip > 55 + 4 * period)
109 return -EINVAL;
110
111 if (timing->hstrail < max(8 * period, 60 + 4 * period))
112 return -EINVAL;
113
114 if (timing->init < 100000)
115 return -EINVAL;
116
117 if (timing->lpx < 50)
118 return -EINVAL;
119
120 if (timing->taget != 5 * timing->lpx)
121 return -EINVAL;
122
123 if (timing->tago != 4 * timing->lpx)
124 return -EINVAL;
125
126 if (timing->tasure < timing->lpx || timing->tasure > 2 * timing->lpx)
127 return -EINVAL;
128
129 if (timing->wakeup < 1000000)
130 return -EINVAL;
131
132 return 0;
133}