-
Notifications
You must be signed in to change notification settings - Fork 34
/
mipi.py
312 lines (250 loc) · 9.56 KB
/
mipi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# SPDX-License-Identifier: GPL-2.0-only
#
# Enums originally taken from:
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/video/mipi_display.h
#
# Defines for Mobile Industry Processor Interface (MIPI(R))
# Display Working Group standards: DSI, DCS, DBI, DPI
#
# Copyright (C) 2010 Guennadi Liakhovetski <[email protected]>
# Copyright (C) 2006 Nokia Corporation
# Author: Imre Deak <[email protected]>
#
# Some code adapted from:
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/gpu/drm/drm_mipi_dsi.c
#
# Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
# Andrzej Hajda <[email protected]>
#
from __future__ import annotations
from enum import IntEnum, unique, Enum
from typing import List, Optional, Union, Tuple
from generator import Options
import wrap
def _hex_fill(i: int, size: int = 1) -> str:
return f'{i:#0{size * 2 + 2}x}'
def _get_params_hex(b: bytes) -> List[str]:
return [_hex_fill(i) for i in b]
def _get_params_int(size: int, byteorder: str):
def _get_params(b: bytes) -> List[str]:
if len(b) < size:
return [_hex_fill(int.from_bytes(b, byteorder=byteorder), size)]
itr = iter(b)
return [_hex_fill(int.from_bytes(t, byteorder=byteorder), size) for t in zip(*[itr] * size)]
return _get_params
@unique
class TearMode(IntEnum):
VBLANK = 0
VHBLANK = 1
@property
def identifier(self):
return 'MIPI_DSI_DCS_TEAR_MODE_' + self.name
@staticmethod
def get_params(b: bytes) -> List[str]:
return [TearMode(b[0]).identifier]
# MIPI DCS commands
@unique
class DCSCommand(Enum):
NOP = 0x00, 0, 'mipi_dsi_dcs_nop_multi'
SOFT_RESET = 0x01, 0, 'mipi_dsi_dcs_soft_reset_multi'
# GET_COMPRESSION_MODE = 0x03,
# GET_DISPLAY_ID = 0x04,
# GET_ERROR_COUNT_ON_DSI = 0x05,
# GET_RED_CHANNEL = 0x06,
# GET_GREEN_CHANNEL = 0x07,
# GET_BLUE_CHANNEL = 0x08,
# GET_DISPLAY_STATUS = 0x09,
# GET_POWER_MODE = 0x0A,
# GET_ADDRESS_MODE = 0x0B,
# GET_PIXEL_FORMAT = 0x0C,
# GET_DISPLAY_MODE = 0x0D,
# GET_SIGNAL_MODE = 0x0E,
# GET_DIAGNOSTIC_RESULT = 0x0F,
ENTER_SLEEP_MODE = 0x10, 0, 'mipi_dsi_dcs_enter_sleep_mode_multi'
EXIT_SLEEP_MODE = 0x11, 0, 'mipi_dsi_dcs_exit_sleep_mode_multi'
ENTER_PARTIAL_MODE = 0x12, 0,
ENTER_NORMAL_MODE = 0x13, 0,
# GET_IMAGE_CHECKSUM_RGB = 0x14,
# GET_IMAGE_CHECKSUM_CT = 0x15,
EXIT_INVERT_MODE = 0x20, 0,
ENTER_INVERT_MODE = 0x21, 0,
SET_GAMMA_CURVE = 0x26, 1,
SET_DISPLAY_OFF = 0x28, 0, 'mipi_dsi_dcs_set_display_off_multi'
SET_DISPLAY_ON = 0x29, 0, 'mipi_dsi_dcs_set_display_on_multi'
SET_COLUMN_ADDRESS = 0x2A, 4, 'mipi_dsi_dcs_set_column_address_multi', _get_params_int(2, 'big')
SET_PAGE_ADDRESS = 0x2B, 4, 'mipi_dsi_dcs_set_page_address_multi', _get_params_int(2, 'big')
WRITE_MEMORY_START = 0x2C,
WRITE_LUT = 0x2D,
READ_MEMORY_START = 0x2E,
SET_PARTIAL_ROWS = 0x30,
SET_PARTIAL_COLUMNS = 0x31,
SET_SCROLL_AREA = 0x33, 6,
SET_TEAR_OFF = 0x34, 0, 'mipi_dsi_dcs_set_tear_off_multi'
SET_TEAR_ON = 0x35, 1, 'mipi_dsi_dcs_set_tear_on_multi', TearMode.get_params
SET_ADDRESS_MODE = 0x36, 1,
SET_SCROLL_START = 0x37, 2,
EXIT_IDLE_MODE = 0x38, 0,
ENTER_IDLE_MODE = 0x39, 0,
SET_PIXEL_FORMAT = 0x3A, 1, 'mipi_dsi_dcs_set_pixel_format_multi'
WRITE_MEMORY_CONTINUE = 0x3C,
SET_3D_CONTROL = 0x3D,
READ_MEMORY_CONTINUE = 0x3E,
# GET_3D_CONTROL = 0x3F,
SET_VSYNC_TIMING = 0x40
SET_TEAR_SCANLINE = 0x44, 2, 'mipi_dsi_dcs_set_tear_scanline_multi', _get_params_int(2, 'big')
GET_SCANLINE = 0x45,
SET_DISPLAY_BRIGHTNESS = 0x51, (1, 2), 'mipi_dsi_dcs_set_display_brightness_multi', _get_params_int(2, 'little')
# GET_DISPLAY_BRIGHTNESS = 0x52,
WRITE_CONTROL_DISPLAY = 0x53, 1,
# GET_CONTROL_DISPLAY = 0x54,
WRITE_POWER_SAVE = 0x55, 1,
# GET_POWER_SAVE = 0x56,
SET_CABC_MIN_BRIGHTNESS = 0x5E,
# GET_CABC_MIN_BRIGHTNESS = 0x5F,
READ_DDB_START = 0xA1,
READ_PPS_START = 0xA2,
READ_DDB_CONTINUE = 0xA8,
READ_PPS_CONTINUE = 0xA9,
def __new__(cls, value: int, nargs: Union[int, Tuple[int]] = (), method: str = None,
_get_params=_get_params_hex) -> DCSCommand:
obj = object.__new__(cls)
obj._value_ = value
obj.nargs = nargs if isinstance(nargs, tuple) else (nargs,)
obj.method = method
obj._get_params = _get_params
return obj
@property
def identifier(self):
return 'MIPI_DCS_' + self.name
def get_params(self, b: bytes):
params = self._get_params(b)
params.insert(0, '&dsi_ctx')
return params
@staticmethod
def find(payload: bytes, dumb: bool) -> Optional[DCSCommand]:
try:
dcs = DCSCommand(payload[0])
except ValueError:
# Not a specified DCS command
return None
if dumb and dcs not in DCSCommand._DUMB_ALLOWED:
return None
if dcs.nargs and len(payload) - 1 not in dcs.nargs:
# Argument count does not match. Weird.
expected_args = " or ".join(str(i) for i in dcs.nargs)
print(f"WARNING: DCS command {dcs.name} with incorrect argument count "
f"(expected: {expected_args}, is: {len(payload) - 1}). Consider using --dumb-dcs")
return None
try:
# Try to parse the argument(s)
dcs.get_params(payload[1:])
except ValueError as e:
# Not a valid argument. Weird.
print(f"WARNING: DCS command {dcs.name} with invalid arguments {payload[1:]}: {e} Consider using --dumb-dcs")
return None
return dcs
DCSCommand._DUMB_ALLOWED = [DCSCommand.ENTER_SLEEP_MODE, DCSCommand.EXIT_SLEEP_MODE,
DCSCommand.SET_DISPLAY_ON, DCSCommand.SET_DISPLAY_OFF]
def _generate_call(method: str, args: List[str]) -> str:
return wrap.join(f'\t{method}(', ',', ');', args)
def _generate_generic_write(t: Transaction, payload: bytes, options: Options) -> str:
# TODO: Warn when downstream uses LONG_WRITE but mainline would use SHORT
params = _get_params_hex(payload)
params.insert(0, '&dsi_ctx')
return wrap.join('\tmipi_dsi_generic_write_seq_multi(', ',', ');', params, force=2)
def _generate_dcs_write(t: Transaction, payload: bytes, options: Options) -> str:
# TODO: Warn when downstream uses LONG_WRITE but mainline would use SHORT
dcs = DCSCommand.find(payload, options.dumb_dcs)
if dcs and dcs.method:
return _generate_call(dcs.method, dcs.get_params(payload[1:]))
params = _get_params_hex(payload)
if dcs:
params[0] = dcs.identifier
params.insert(0, '&dsi_ctx')
return wrap.join('\tmipi_dsi_dcs_write_seq_multi(', ',', ');', params, force=2)
def _generate_peripheral(t: Transaction, payload: bytes, options: Options) -> str:
if t == Transaction.TURN_ON_PERIPHERAL:
return _generate_call('mipi_dsi_turn_on_peripheral_multi', ['&dsi_ctx'])
elif t == Transaction.SHUTDOWN_PERIPHERAL:
return _generate_call('mipi_dsi_shutdown_peripheral_multi', ['&dsi_ctx'])
else:
raise ValueError(t)
def _generate_compression_mode(t: Transaction, payload: bytes, options: Options) -> str:
return _generate_call('mipi_dsi_compression_mode_multi', ['&dsi_ctx', str(bool(payload[0])).lower()])
def _generate_ignore(t: Transaction, payload: bytes, options: Options) -> str:
print(f"WARNING: Ignoring weird {t.name}")
return f"\t// WARNING: Ignoring weird {t.name}"
def _generate_fallback(t: Transaction, payload: bytes, options: Options) -> str:
raise ValueError(t.name + ' is not supported')
# MIPI DSI Processor-to-Peripheral transaction types
@unique
class Transaction(Enum):
V_SYNC_START = 0x01,
V_SYNC_END = 0x11,
H_SYNC_START = 0x21,
H_SYNC_END = 0x31,
COMPRESSION_MODE = 0x07, 1, _generate_compression_mode
END_OF_TRANSMISSION = 0x08,
COLOR_MODE_OFF = 0x02,
COLOR_MODE_ON = 0x12,
SHUTDOWN_PERIPHERAL = 0x22, 0, _generate_peripheral
TURN_ON_PERIPHERAL = 0x32, 0, _generate_peripheral
GENERIC_SHORT_WRITE_0_PARAM = 0x03, 0, _generate_generic_write
GENERIC_SHORT_WRITE_1_PARAM = 0x13, 1, _generate_generic_write
GENERIC_SHORT_WRITE_2_PARAM = 0x23, 2, _generate_generic_write
GENERIC_READ_REQUEST_0_PARAM = 0x04,
GENERIC_READ_REQUEST_1_PARAM = 0x14,
GENERIC_READ_REQUEST_2_PARAM = 0x24,
DCS_SHORT_WRITE = 0x05, 0, _generate_dcs_write
DCS_SHORT_WRITE_PARAM = 0x15, 1, _generate_dcs_write
DCS_READ = 0x06,
EXECUTE_QUEUE = 0x16,
SET_MAXIMUM_RETURN_PACKET_SIZE = 0x37, -1, _generate_ignore
NULL_PACKET = 0x09, -1, _generate_ignore
BLANKING_PACKET = 0x19,
GENERIC_LONG_WRITE = 0x29, -1, _generate_generic_write
DCS_LONG_WRITE = 0x39, -1, _generate_dcs_write
PICTURE_PARAMETER_SET = 0x0a,
COMPRESSED_PIXEL_STREAM = 0x0b,
LOOSELY_PACKED_PIXEL_STREAM_YCBCR20 = 0x0c,
PACKED_PIXEL_STREAM_YCBCR24 = 0x1c,
PACKED_PIXEL_STREAM_YCBCR16 = 0x2c,
PACKED_PIXEL_STREAM_30 = 0x0d,
PACKED_PIXEL_STREAM_36 = 0x1d,
PACKED_PIXEL_STREAM_YCBCR12 = 0x3d,
PACKED_PIXEL_STREAM_16 = 0x0e,
PACKED_PIXEL_STREAM_18 = 0x1e,
PIXEL_STREAM_3BYTE_18 = 0x2e,
PACKED_PIXEL_STREAM_24 = 0x3e,
def __new__(cls, value: int, max_args: int = -1, generate=_generate_fallback) -> Transaction:
obj = object.__new__(cls)
obj._value_ = value
obj.max_args = max_args
obj._generate = generate
return obj
@property
def identifier(self):
return 'MIPI_DSI_' + self.name
@property
def is_long(self):
# From mipi_dsi_packet_format_is_long()
return self in [
Transaction.NULL_PACKET,
Transaction.BLANKING_PACKET,
Transaction.GENERIC_LONG_WRITE,
Transaction.DCS_LONG_WRITE,
Transaction.PICTURE_PARAMETER_SET,
Transaction.COMPRESSED_PIXEL_STREAM,
Transaction.LOOSELY_PACKED_PIXEL_STREAM_YCBCR20,
Transaction.PACKED_PIXEL_STREAM_YCBCR24,
Transaction.PACKED_PIXEL_STREAM_YCBCR16,
Transaction.PACKED_PIXEL_STREAM_30,
Transaction.PACKED_PIXEL_STREAM_36,
Transaction.PACKED_PIXEL_STREAM_YCBCR12,
Transaction.PACKED_PIXEL_STREAM_16,
Transaction.PACKED_PIXEL_STREAM_18,
Transaction.PIXEL_STREAM_3BYTE_18,
Transaction.PACKED_PIXEL_STREAM_24,
]
def generate(self, payload: bytes, options: Options) -> str:
return self._generate(self, payload, options)