1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Allwinnertech Co., Ltd.
4 * Copyright (C) 2017-2018 Bootlin
6 * Maxime Ripard <maxime.ripard@bootlin.com>
10 #include <linux/component.h>
11 #include <linux/crc-ccitt.h>
12 #include <linux/of_address.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/reset.h>
16 #include <linux/slab.h>
18 #include <linux/phy/phy.h>
21 #include <drm/drm_atomic_helper.h>
22 #include <drm/drm_crtc_helper.h>
23 #include <drm/drm_mipi_dsi.h>
24 #include <drm/drm_panel.h>
26 #include "sun4i_drv.h"
27 #include "sun6i_mipi_dsi.h"
29 #include <video/mipi_display.h>
31 #define SUN6I_DSI_CTL_REG 0x000
32 #define SUN6I_DSI_CTL_EN BIT(0)
34 #define SUN6I_DSI_BASIC_CTL_REG 0x00c
35 #define SUN6I_DSI_BASIC_CTL_HBP_DIS BIT(2)
36 #define SUN6I_DSI_BASIC_CTL_HSA_HSE_DIS BIT(1)
37 #define SUN6I_DSI_BASIC_CTL_VIDEO_BURST BIT(0)
39 #define SUN6I_DSI_BASIC_CTL0_REG 0x010
40 #define SUN6I_DSI_BASIC_CTL0_HS_EOTP_EN BIT(18)
41 #define SUN6I_DSI_BASIC_CTL0_CRC_EN BIT(17)
42 #define SUN6I_DSI_BASIC_CTL0_ECC_EN BIT(16)
43 #define SUN6I_DSI_BASIC_CTL0_INST_ST BIT(0)
45 #define SUN6I_DSI_BASIC_CTL1_REG 0x014
46 #define SUN6I_DSI_BASIC_CTL1_VIDEO_ST_DELAY(n) (((n) & 0x1fff) << 4)
47 #define SUN6I_DSI_BASIC_CTL1_VIDEO_FILL BIT(2)
48 #define SUN6I_DSI_BASIC_CTL1_VIDEO_PRECISION BIT(1)
49 #define SUN6I_DSI_BASIC_CTL1_VIDEO_MODE BIT(0)
51 #define SUN6I_DSI_BASIC_SIZE0_REG 0x018
52 #define SUN6I_DSI_BASIC_SIZE0_VBP(n) (((n) & 0xfff) << 16)
53 #define SUN6I_DSI_BASIC_SIZE0_VSA(n) ((n) & 0xfff)
55 #define SUN6I_DSI_BASIC_SIZE1_REG 0x01c
56 #define SUN6I_DSI_BASIC_SIZE1_VT(n) (((n) & 0xfff) << 16)
57 #define SUN6I_DSI_BASIC_SIZE1_VACT(n) ((n) & 0xfff)
59 #define SUN6I_DSI_INST_FUNC_REG(n) (0x020 + (n) * 0x04)
60 #define SUN6I_DSI_INST_FUNC_INST_MODE(n) (((n) & 0xf) << 28)
61 #define SUN6I_DSI_INST_FUNC_ESCAPE_ENTRY(n) (((n) & 0xf) << 24)
62 #define SUN6I_DSI_INST_FUNC_TRANS_PACKET(n) (((n) & 0xf) << 20)
63 #define SUN6I_DSI_INST_FUNC_LANE_CEN BIT(4)
64 #define SUN6I_DSI_INST_FUNC_LANE_DEN(n) ((n) & 0xf)
66 #define SUN6I_DSI_INST_LOOP_SEL_REG 0x040
68 #define SUN6I_DSI_INST_LOOP_NUM_REG(n) (0x044 + (n) * 0x10)
69 #define SUN6I_DSI_INST_LOOP_NUM_N1(n) (((n) & 0xfff) << 16)
70 #define SUN6I_DSI_INST_LOOP_NUM_N0(n) ((n) & 0xfff)
72 #define SUN6I_DSI_INST_JUMP_SEL_REG 0x048
74 #define SUN6I_DSI_INST_JUMP_CFG_REG(n) (0x04c + (n) * 0x04)
75 #define SUN6I_DSI_INST_JUMP_CFG_TO(n) (((n) & 0xf) << 20)
76 #define SUN6I_DSI_INST_JUMP_CFG_POINT(n) (((n) & 0xf) << 16)
77 #define SUN6I_DSI_INST_JUMP_CFG_NUM(n) ((n) & 0xffff)
79 #define SUN6I_DSI_TRANS_START_REG 0x060
81 #define SUN6I_DSI_TRANS_ZERO_REG 0x078
83 #define SUN6I_DSI_TCON_DRQ_REG 0x07c
84 #define SUN6I_DSI_TCON_DRQ_ENABLE_MODE BIT(28)
85 #define SUN6I_DSI_TCON_DRQ_SET(n) ((n) & 0x3ff)
87 #define SUN6I_DSI_PIXEL_CTL0_REG 0x080
88 #define SUN6I_DSI_PIXEL_CTL0_PD_PLUG_DISABLE BIT(16)
89 #define SUN6I_DSI_PIXEL_CTL0_FORMAT(n) ((n) & 0xf)
91 #define SUN6I_DSI_PIXEL_CTL1_REG 0x084
93 #define SUN6I_DSI_PIXEL_PH_REG 0x090
94 #define SUN6I_DSI_PIXEL_PH_ECC(n) (((n) & 0xff) << 24)
95 #define SUN6I_DSI_PIXEL_PH_WC(n) (((n) & 0xffff) << 8)
96 #define SUN6I_DSI_PIXEL_PH_VC(n) (((n) & 3) << 6)
97 #define SUN6I_DSI_PIXEL_PH_DT(n) ((n) & 0x3f)
99 #define SUN6I_DSI_PIXEL_PF0_REG 0x098
100 #define SUN6I_DSI_PIXEL_PF0_CRC_FORCE(n) ((n) & 0xffff)
102 #define SUN6I_DSI_PIXEL_PF1_REG 0x09c
103 #define SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINEN(n) (((n) & 0xffff) << 16)
104 #define SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINE0(n) ((n) & 0xffff)
106 #define SUN6I_DSI_SYNC_HSS_REG 0x0b0
108 #define SUN6I_DSI_SYNC_HSE_REG 0x0b4
110 #define SUN6I_DSI_SYNC_VSS_REG 0x0b8
112 #define SUN6I_DSI_SYNC_VSE_REG 0x0bc
114 #define SUN6I_DSI_BLK_HSA0_REG 0x0c0
116 #define SUN6I_DSI_BLK_HSA1_REG 0x0c4
117 #define SUN6I_DSI_BLK_PF(n) (((n) & 0xffff) << 16)
118 #define SUN6I_DSI_BLK_PD(n) ((n) & 0xff)
120 #define SUN6I_DSI_BLK_HBP0_REG 0x0c8
122 #define SUN6I_DSI_BLK_HBP1_REG 0x0cc
124 #define SUN6I_DSI_BLK_HFP0_REG 0x0d0
126 #define SUN6I_DSI_BLK_HFP1_REG 0x0d4
128 #define SUN6I_DSI_BLK_HBLK0_REG 0x0e0
130 #define SUN6I_DSI_BLK_HBLK1_REG 0x0e4
132 #define SUN6I_DSI_BLK_VBLK0_REG 0x0e8
134 #define SUN6I_DSI_BLK_VBLK1_REG 0x0ec
136 #define SUN6I_DSI_BURST_LINE_REG 0x0f0
137 #define SUN6I_DSI_BURST_LINE_SYNC_POINT(n) (((n) & 0xffff) << 16)
138 #define SUN6I_DSI_BURST_LINE_NUM(n) ((n) & 0xffff)
140 #define SUN6I_DSI_BURST_DRQ_REG 0x0f4
141 #define SUN6I_DSI_BURST_DRQ_EDGE1(n) (((n) & 0xffff) << 16)
142 #define SUN6I_DSI_BURST_DRQ_EDGE0(n) ((n) & 0xffff)
144 #define SUN6I_DSI_CMD_CTL_REG 0x200
145 #define SUN6I_DSI_CMD_CTL_RX_OVERFLOW BIT(26)
146 #define SUN6I_DSI_CMD_CTL_RX_FLAG BIT(25)
147 #define SUN6I_DSI_CMD_CTL_TX_FLAG BIT(9)
149 #define SUN6I_DSI_CMD_RX_REG(n) (0x240 + (n) * 0x04)
151 #define SUN6I_DSI_DEBUG_DATA_REG 0x2f8
153 #define SUN6I_DSI_CMD_TX_REG(n) (0x300 + (n) * 0x04)
155 enum sun6i_dsi_start_inst
{
162 enum sun6i_dsi_inst_id
{
163 DSI_INST_ID_LP11
= 0,
171 DSI_INST_ID_END
= 15,
174 enum sun6i_dsi_inst_mode
{
175 DSI_INST_MODE_STOP
= 0,
178 DSI_INST_MODE_ESCAPE
,
179 DSI_INST_MODE_HSCEXIT
,
183 enum sun6i_dsi_inst_escape
{
184 DSI_INST_ESCA_LPDT
= 0,
194 enum sun6i_dsi_inst_packet
{
195 DSI_INST_PACK_PIXEL
= 0,
196 DSI_INST_PACK_COMMAND
,
199 static const u32 sun6i_dsi_ecc_array
[] = {
200 [0] = (BIT(0) | BIT(1) | BIT(2) | BIT(4) | BIT(5) | BIT(7) | BIT(10) |
201 BIT(11) | BIT(13) | BIT(16) | BIT(20) | BIT(21) | BIT(22) |
203 [1] = (BIT(0) | BIT(1) | BIT(3) | BIT(4) | BIT(6) | BIT(8) | BIT(10) |
204 BIT(12) | BIT(14) | BIT(17) | BIT(20) | BIT(21) | BIT(22) |
206 [2] = (BIT(0) | BIT(2) | BIT(3) | BIT(5) | BIT(6) | BIT(9) | BIT(11) |
207 BIT(12) | BIT(15) | BIT(18) | BIT(20) | BIT(21) | BIT(22)),
208 [3] = (BIT(1) | BIT(2) | BIT(3) | BIT(7) | BIT(8) | BIT(9) | BIT(13) |
209 BIT(14) | BIT(15) | BIT(19) | BIT(20) | BIT(21) | BIT(23)),
210 [4] = (BIT(4) | BIT(5) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | BIT(16) |
211 BIT(17) | BIT(18) | BIT(19) | BIT(20) | BIT(22) | BIT(23)),
212 [5] = (BIT(10) | BIT(11) | BIT(12) | BIT(13) | BIT(14) | BIT(15) |
213 BIT(16) | BIT(17) | BIT(18) | BIT(19) | BIT(21) | BIT(22) |
217 static u32
sun6i_dsi_ecc_compute(unsigned int data
)
222 for (i
= 0; i
< ARRAY_SIZE(sun6i_dsi_ecc_array
); i
++) {
223 u32 field
= sun6i_dsi_ecc_array
[i
];
228 for (j
= 0; j
< 24; j
++) {
229 if (!(BIT(j
) & field
))
233 val
= (BIT(j
) & data
) ? 1 : 0;
236 val
^= (BIT(j
) & data
) ? 1 : 0;
246 static u16
sun6i_dsi_crc_compute(u8
const *buffer
, size_t len
)
248 return crc_ccitt(0xffff, buffer
, len
);
251 static u16
sun6i_dsi_crc_repeat(u8 pd
, u8
*buffer
, size_t len
)
253 memset(buffer
, pd
, len
);
255 return sun6i_dsi_crc_compute(buffer
, len
);
258 static u32
sun6i_dsi_build_sync_pkt(u8 dt
, u8 vc
, u8 d0
, u8 d1
)
262 val
|= (vc
& 3) << 6;
263 val
|= (d0
& 0xff) << 8;
264 val
|= (d1
& 0xff) << 16;
265 val
|= sun6i_dsi_ecc_compute(val
) << 24;
270 static u32
sun6i_dsi_build_blk0_pkt(u8 vc
, u16 wc
)
272 return sun6i_dsi_build_sync_pkt(MIPI_DSI_BLANKING_PACKET
, vc
,
276 static u32
sun6i_dsi_build_blk1_pkt(u16 pd
, u8
*buffer
, size_t len
)
278 u32 val
= SUN6I_DSI_BLK_PD(pd
);
280 return val
| SUN6I_DSI_BLK_PF(sun6i_dsi_crc_repeat(pd
, buffer
, len
));
283 static void sun6i_dsi_inst_abort(struct sun6i_dsi
*dsi
)
285 regmap_update_bits(dsi
->regs
, SUN6I_DSI_BASIC_CTL0_REG
,
286 SUN6I_DSI_BASIC_CTL0_INST_ST
, 0);
289 static void sun6i_dsi_inst_commit(struct sun6i_dsi
*dsi
)
291 regmap_update_bits(dsi
->regs
, SUN6I_DSI_BASIC_CTL0_REG
,
292 SUN6I_DSI_BASIC_CTL0_INST_ST
,
293 SUN6I_DSI_BASIC_CTL0_INST_ST
);
296 static int sun6i_dsi_inst_wait_for_completion(struct sun6i_dsi
*dsi
)
300 return regmap_read_poll_timeout(dsi
->regs
, SUN6I_DSI_BASIC_CTL0_REG
,
302 !(val
& SUN6I_DSI_BASIC_CTL0_INST_ST
),
306 static void sun6i_dsi_inst_setup(struct sun6i_dsi
*dsi
,
307 enum sun6i_dsi_inst_id id
,
308 enum sun6i_dsi_inst_mode mode
,
310 enum sun6i_dsi_inst_packet packet
,
311 enum sun6i_dsi_inst_escape escape
)
313 regmap_write(dsi
->regs
, SUN6I_DSI_INST_FUNC_REG(id
),
314 SUN6I_DSI_INST_FUNC_INST_MODE(mode
) |
315 SUN6I_DSI_INST_FUNC_ESCAPE_ENTRY(escape
) |
316 SUN6I_DSI_INST_FUNC_TRANS_PACKET(packet
) |
317 (clock
? SUN6I_DSI_INST_FUNC_LANE_CEN
: 0) |
318 SUN6I_DSI_INST_FUNC_LANE_DEN(data
));
321 static void sun6i_dsi_inst_init(struct sun6i_dsi
*dsi
,
322 struct mipi_dsi_device
*device
)
324 u8 lanes_mask
= GENMASK(device
->lanes
- 1, 0);
326 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_LP11
, DSI_INST_MODE_STOP
,
327 true, lanes_mask
, 0, 0);
329 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_TBA
, DSI_INST_MODE_TBA
,
332 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_HSC
, DSI_INST_MODE_HS
,
333 true, 0, DSI_INST_PACK_PIXEL
, 0);
335 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_HSD
, DSI_INST_MODE_HS
,
336 false, lanes_mask
, DSI_INST_PACK_PIXEL
, 0);
338 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_LPDT
, DSI_INST_MODE_ESCAPE
,
339 false, 1, DSI_INST_PACK_COMMAND
,
342 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_HSCEXIT
, DSI_INST_MODE_HSCEXIT
,
345 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_NOP
, DSI_INST_MODE_STOP
,
346 false, lanes_mask
, 0, 0);
348 sun6i_dsi_inst_setup(dsi
, DSI_INST_ID_DLY
, DSI_INST_MODE_NOP
,
349 true, lanes_mask
, 0, 0);
351 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_CFG_REG(0),
352 SUN6I_DSI_INST_JUMP_CFG_POINT(DSI_INST_ID_NOP
) |
353 SUN6I_DSI_INST_JUMP_CFG_TO(DSI_INST_ID_HSCEXIT
) |
354 SUN6I_DSI_INST_JUMP_CFG_NUM(1));
357 static u16
sun6i_dsi_get_video_start_delay(struct sun6i_dsi
*dsi
,
358 struct drm_display_mode
*mode
)
360 return mode
->vtotal
- (mode
->vsync_end
- mode
->vdisplay
) + 1;
363 static void sun6i_dsi_setup_burst(struct sun6i_dsi
*dsi
,
364 struct drm_display_mode
*mode
)
366 struct mipi_dsi_device
*device
= dsi
->device
;
369 if ((mode
->hsync_end
- mode
->hdisplay
) > 20) {
371 u16 drq
= (mode
->hsync_end
- mode
->hdisplay
) - 20;
373 drq
*= mipi_dsi_pixel_format_to_bpp(device
->format
);
376 val
= (SUN6I_DSI_TCON_DRQ_ENABLE_MODE
|
377 SUN6I_DSI_TCON_DRQ_SET(drq
));
380 regmap_write(dsi
->regs
, SUN6I_DSI_TCON_DRQ_REG
, val
);
383 static void sun6i_dsi_setup_inst_loop(struct sun6i_dsi
*dsi
,
384 struct drm_display_mode
*mode
)
388 regmap_write(dsi
->regs
, SUN6I_DSI_INST_LOOP_NUM_REG(0),
389 SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
390 SUN6I_DSI_INST_LOOP_NUM_N1(delay
));
391 regmap_write(dsi
->regs
, SUN6I_DSI_INST_LOOP_NUM_REG(1),
392 SUN6I_DSI_INST_LOOP_NUM_N0(50 - 1) |
393 SUN6I_DSI_INST_LOOP_NUM_N1(delay
));
396 static void sun6i_dsi_setup_format(struct sun6i_dsi
*dsi
,
397 struct drm_display_mode
*mode
)
399 struct mipi_dsi_device
*device
= dsi
->device
;
400 u32 val
= SUN6I_DSI_PIXEL_PH_VC(device
->channel
);
405 * TODO: The format defines are only valid in video mode and
406 * change in command mode.
408 switch (device
->format
) {
409 case MIPI_DSI_FMT_RGB888
:
410 dt
= MIPI_DSI_PACKED_PIXEL_STREAM_24
;
413 case MIPI_DSI_FMT_RGB666
:
414 dt
= MIPI_DSI_PIXEL_STREAM_3BYTE_18
;
417 case MIPI_DSI_FMT_RGB666_PACKED
:
418 dt
= MIPI_DSI_PACKED_PIXEL_STREAM_18
;
421 case MIPI_DSI_FMT_RGB565
:
422 dt
= MIPI_DSI_PACKED_PIXEL_STREAM_16
;
428 val
|= SUN6I_DSI_PIXEL_PH_DT(dt
);
430 wc
= mode
->hdisplay
* mipi_dsi_pixel_format_to_bpp(device
->format
) / 8;
431 val
|= SUN6I_DSI_PIXEL_PH_WC(wc
);
432 val
|= SUN6I_DSI_PIXEL_PH_ECC(sun6i_dsi_ecc_compute(val
));
434 regmap_write(dsi
->regs
, SUN6I_DSI_PIXEL_PH_REG
, val
);
436 regmap_write(dsi
->regs
, SUN6I_DSI_PIXEL_PF0_REG
,
437 SUN6I_DSI_PIXEL_PF0_CRC_FORCE(0xffff));
439 regmap_write(dsi
->regs
, SUN6I_DSI_PIXEL_PF1_REG
,
440 SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINE0(0xffff) |
441 SUN6I_DSI_PIXEL_PF1_CRC_INIT_LINEN(0xffff));
443 regmap_write(dsi
->regs
, SUN6I_DSI_PIXEL_CTL0_REG
,
444 SUN6I_DSI_PIXEL_CTL0_PD_PLUG_DISABLE
|
445 SUN6I_DSI_PIXEL_CTL0_FORMAT(fmt
));
448 static void sun6i_dsi_setup_timings(struct sun6i_dsi
*dsi
,
449 struct drm_display_mode
*mode
)
451 struct mipi_dsi_device
*device
= dsi
->device
;
452 unsigned int Bpp
= mipi_dsi_pixel_format_to_bpp(device
->format
) / 8;
453 u16 hbp
, hfp
, hsa
, hblk
, vblk
;
457 /* Do all timing calculations up front to allocate buffer space */
460 * A sync period is composed of a blanking packet (4 bytes +
461 * payload + 2 bytes) and a sync event packet (4 bytes). Its
462 * minimal size is therefore 10 bytes
464 #define HSA_PACKET_OVERHEAD 10
465 hsa
= max((unsigned int)HSA_PACKET_OVERHEAD
,
466 (mode
->hsync_end
- mode
->hsync_start
) * Bpp
- HSA_PACKET_OVERHEAD
);
469 * The backporch is set using a blanking packet (4 bytes +
470 * payload + 2 bytes). Its minimal size is therefore 6 bytes
472 #define HBP_PACKET_OVERHEAD 6
473 hbp
= max((unsigned int)HBP_PACKET_OVERHEAD
,
474 (mode
->hsync_start
- mode
->hdisplay
) * Bpp
- HBP_PACKET_OVERHEAD
);
477 * The frontporch is set using a blanking packet (4 bytes +
478 * payload + 2 bytes). Its minimal size is therefore 6 bytes
480 #define HFP_PACKET_OVERHEAD 6
481 hfp
= max((unsigned int)HFP_PACKET_OVERHEAD
,
482 (mode
->htotal
- mode
->hsync_end
) * Bpp
- HFP_PACKET_OVERHEAD
);
485 * hblk seems to be the line + porches length.
487 hblk
= mode
->htotal
* Bpp
- hsa
;
490 * And I'm not entirely sure what vblk is about. The driver in
491 * Allwinner BSP is using a rather convoluted calculation
492 * there only for 4 lanes. However, using 0 (the !4 lanes
493 * case) even with a 4 lanes screen seems to work...
497 /* How many bytes do we need to send all payloads? */
498 bytes
= max_t(size_t, max(max(hfp
, hblk
), max(hsa
, hbp
)), vblk
);
499 buffer
= kmalloc(bytes
, GFP_KERNEL
);
500 if (WARN_ON(!buffer
))
503 regmap_write(dsi
->regs
, SUN6I_DSI_BASIC_CTL_REG
, 0);
505 regmap_write(dsi
->regs
, SUN6I_DSI_SYNC_HSS_REG
,
506 sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_START
,
510 regmap_write(dsi
->regs
, SUN6I_DSI_SYNC_HSE_REG
,
511 sun6i_dsi_build_sync_pkt(MIPI_DSI_H_SYNC_END
,
515 regmap_write(dsi
->regs
, SUN6I_DSI_SYNC_VSS_REG
,
516 sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_START
,
520 regmap_write(dsi
->regs
, SUN6I_DSI_SYNC_VSE_REG
,
521 sun6i_dsi_build_sync_pkt(MIPI_DSI_V_SYNC_END
,
525 regmap_write(dsi
->regs
, SUN6I_DSI_BASIC_SIZE0_REG
,
526 SUN6I_DSI_BASIC_SIZE0_VSA(mode
->vsync_end
-
528 SUN6I_DSI_BASIC_SIZE0_VBP(mode
->vsync_start
-
531 regmap_write(dsi
->regs
, SUN6I_DSI_BASIC_SIZE1_REG
,
532 SUN6I_DSI_BASIC_SIZE1_VACT(mode
->vdisplay
) |
533 SUN6I_DSI_BASIC_SIZE1_VT(mode
->vtotal
));
536 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HSA0_REG
,
537 sun6i_dsi_build_blk0_pkt(device
->channel
, hsa
));
538 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HSA1_REG
,
539 sun6i_dsi_build_blk1_pkt(0, buffer
, hsa
));
542 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HBP0_REG
,
543 sun6i_dsi_build_blk0_pkt(device
->channel
, hbp
));
544 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HBP1_REG
,
545 sun6i_dsi_build_blk1_pkt(0, buffer
, hbp
));
548 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HFP0_REG
,
549 sun6i_dsi_build_blk0_pkt(device
->channel
, hfp
));
550 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HFP1_REG
,
551 sun6i_dsi_build_blk1_pkt(0, buffer
, hfp
));
554 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HBLK0_REG
,
555 sun6i_dsi_build_blk0_pkt(device
->channel
, hblk
));
556 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_HBLK1_REG
,
557 sun6i_dsi_build_blk1_pkt(0, buffer
, hblk
));
560 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_VBLK0_REG
,
561 sun6i_dsi_build_blk0_pkt(device
->channel
, vblk
));
562 regmap_write(dsi
->regs
, SUN6I_DSI_BLK_VBLK1_REG
,
563 sun6i_dsi_build_blk1_pkt(0, buffer
, vblk
));
568 static int sun6i_dsi_start(struct sun6i_dsi
*dsi
,
569 enum sun6i_dsi_start_inst func
)
573 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_SEL_REG
,
574 DSI_INST_ID_LPDT
<< (4 * DSI_INST_ID_LP11
) |
575 DSI_INST_ID_END
<< (4 * DSI_INST_ID_LPDT
));
578 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_SEL_REG
,
579 DSI_INST_ID_LPDT
<< (4 * DSI_INST_ID_LP11
) |
580 DSI_INST_ID_DLY
<< (4 * DSI_INST_ID_LPDT
) |
581 DSI_INST_ID_TBA
<< (4 * DSI_INST_ID_DLY
) |
582 DSI_INST_ID_END
<< (4 * DSI_INST_ID_TBA
));
585 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_SEL_REG
,
586 DSI_INST_ID_HSC
<< (4 * DSI_INST_ID_LP11
) |
587 DSI_INST_ID_END
<< (4 * DSI_INST_ID_HSC
));
590 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_SEL_REG
,
591 DSI_INST_ID_NOP
<< (4 * DSI_INST_ID_LP11
) |
592 DSI_INST_ID_HSD
<< (4 * DSI_INST_ID_NOP
) |
593 DSI_INST_ID_DLY
<< (4 * DSI_INST_ID_HSD
) |
594 DSI_INST_ID_NOP
<< (4 * DSI_INST_ID_DLY
) |
595 DSI_INST_ID_END
<< (4 * DSI_INST_ID_HSCEXIT
));
598 regmap_write(dsi
->regs
, SUN6I_DSI_INST_JUMP_SEL_REG
,
599 DSI_INST_ID_END
<< (4 * DSI_INST_ID_LP11
));
603 sun6i_dsi_inst_abort(dsi
);
604 sun6i_dsi_inst_commit(dsi
);
606 if (func
== DSI_START_HSC
)
607 regmap_write_bits(dsi
->regs
,
608 SUN6I_DSI_INST_FUNC_REG(DSI_INST_ID_LP11
),
609 SUN6I_DSI_INST_FUNC_LANE_CEN
, 0);
614 static void sun6i_dsi_encoder_enable(struct drm_encoder
*encoder
)
616 struct drm_display_mode
*mode
= &encoder
->crtc
->state
->adjusted_mode
;
617 struct sun6i_dsi
*dsi
= encoder_to_sun6i_dsi(encoder
);
618 struct mipi_dsi_device
*device
= dsi
->device
;
621 DRM_DEBUG_DRIVER("Enabling DSI output\n");
623 pm_runtime_get_sync(dsi
->dev
);
625 delay
= sun6i_dsi_get_video_start_delay(dsi
, mode
);
626 regmap_write(dsi
->regs
, SUN6I_DSI_BASIC_CTL1_REG
,
627 SUN6I_DSI_BASIC_CTL1_VIDEO_ST_DELAY(delay
) |
628 SUN6I_DSI_BASIC_CTL1_VIDEO_FILL
|
629 SUN6I_DSI_BASIC_CTL1_VIDEO_PRECISION
|
630 SUN6I_DSI_BASIC_CTL1_VIDEO_MODE
);
632 sun6i_dsi_setup_burst(dsi
, mode
);
633 sun6i_dsi_setup_inst_loop(dsi
, mode
);
634 sun6i_dsi_setup_format(dsi
, mode
);
635 sun6i_dsi_setup_timings(dsi
, mode
);
637 sun6i_dphy_init(dsi
->dphy
, device
->lanes
);
638 sun6i_dphy_power_on(dsi
->dphy
, device
->lanes
);
640 if (!IS_ERR(dsi
->panel
))
641 drm_panel_prepare(dsi
->panel
);
644 * FIXME: This should be moved after the switch to HS mode.
646 * Unfortunately, once in HS mode, it seems like we're not
647 * able to send DCS commands anymore, which would prevent any
648 * panel to send any DCS command as part as their enable
649 * method, which is quite common.
651 * I haven't seen any artifact due to that sub-optimal
652 * ordering on the panels I've tested it with, so I guess this
653 * will do for now, until that IP is better understood.
655 if (!IS_ERR(dsi
->panel
))
656 drm_panel_enable(dsi
->panel
);
658 sun6i_dsi_start(dsi
, DSI_START_HSC
);
662 sun6i_dsi_start(dsi
, DSI_START_HSD
);
665 static void sun6i_dsi_encoder_disable(struct drm_encoder
*encoder
)
667 struct sun6i_dsi
*dsi
= encoder_to_sun6i_dsi(encoder
);
669 DRM_DEBUG_DRIVER("Disabling DSI output\n");
671 if (!IS_ERR(dsi
->panel
)) {
672 drm_panel_disable(dsi
->panel
);
673 drm_panel_unprepare(dsi
->panel
);
676 sun6i_dphy_power_off(dsi
->dphy
);
677 sun6i_dphy_exit(dsi
->dphy
);
679 pm_runtime_put(dsi
->dev
);
682 static int sun6i_dsi_get_modes(struct drm_connector
*connector
)
684 struct sun6i_dsi
*dsi
= connector_to_sun6i_dsi(connector
);
686 return drm_panel_get_modes(dsi
->panel
);
689 static struct drm_connector_helper_funcs sun6i_dsi_connector_helper_funcs
= {
690 .get_modes
= sun6i_dsi_get_modes
,
693 static enum drm_connector_status
694 sun6i_dsi_connector_detect(struct drm_connector
*connector
, bool force
)
696 return connector_status_connected
;
699 static const struct drm_connector_funcs sun6i_dsi_connector_funcs
= {
700 .detect
= sun6i_dsi_connector_detect
,
701 .fill_modes
= drm_helper_probe_single_connector_modes
,
702 .destroy
= drm_connector_cleanup
,
703 .reset
= drm_atomic_helper_connector_reset
,
704 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
705 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
708 static const struct drm_encoder_helper_funcs sun6i_dsi_enc_helper_funcs
= {
709 .disable
= sun6i_dsi_encoder_disable
,
710 .enable
= sun6i_dsi_encoder_enable
,
713 static const struct drm_encoder_funcs sun6i_dsi_enc_funcs
= {
714 .destroy
= drm_encoder_cleanup
,
717 static u32
sun6i_dsi_dcs_build_pkt_hdr(struct sun6i_dsi
*dsi
,
718 const struct mipi_dsi_msg
*msg
)
722 if (msg
->type
== MIPI_DSI_DCS_LONG_WRITE
) {
723 pkt
|= ((msg
->tx_len
+ 1) & 0xffff) << 8;
724 pkt
|= (((msg
->tx_len
+ 1) >> 8) & 0xffff) << 16;
726 pkt
|= (((u8
*)msg
->tx_buf
)[0] << 8);
728 pkt
|= (((u8
*)msg
->tx_buf
)[1] << 16);
731 pkt
|= sun6i_dsi_ecc_compute(pkt
) << 24;
736 static int sun6i_dsi_dcs_write_short(struct sun6i_dsi
*dsi
,
737 const struct mipi_dsi_msg
*msg
)
739 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_TX_REG(0),
740 sun6i_dsi_dcs_build_pkt_hdr(dsi
, msg
));
741 regmap_write_bits(dsi
->regs
, SUN6I_DSI_CMD_CTL_REG
,
744 sun6i_dsi_start(dsi
, DSI_START_LPTX
);
749 static int sun6i_dsi_dcs_write_long(struct sun6i_dsi
*dsi
,
750 const struct mipi_dsi_msg
*msg
)
756 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_TX_REG(0),
757 sun6i_dsi_dcs_build_pkt_hdr(dsi
, msg
));
759 bounce
= kzalloc(msg
->tx_len
+ sizeof(crc
), GFP_KERNEL
);
763 memcpy(bounce
, msg
->tx_buf
, msg
->tx_len
);
766 crc
= sun6i_dsi_crc_compute(bounce
, msg
->tx_len
);
767 memcpy((u8
*)bounce
+ msg
->tx_len
, &crc
, sizeof(crc
));
770 regmap_bulk_write(dsi
->regs
, SUN6I_DSI_CMD_TX_REG(1), bounce
, len
);
771 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_CTL_REG
, len
+ 4 - 1);
774 sun6i_dsi_start(dsi
, DSI_START_LPTX
);
776 ret
= sun6i_dsi_inst_wait_for_completion(dsi
);
778 sun6i_dsi_inst_abort(dsi
);
783 * TODO: There's some bits (reg 0x200, bits 8/9) that
784 * apparently can be used to check whether the data have been
785 * sent, but I couldn't get it to work reliably.
790 static int sun6i_dsi_dcs_read(struct sun6i_dsi
*dsi
,
791 const struct mipi_dsi_msg
*msg
)
797 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_TX_REG(0),
798 sun6i_dsi_dcs_build_pkt_hdr(dsi
, msg
));
799 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_CTL_REG
,
802 sun6i_dsi_start(dsi
, DSI_START_LPRX
);
804 ret
= sun6i_dsi_inst_wait_for_completion(dsi
);
806 sun6i_dsi_inst_abort(dsi
);
811 * TODO: There's some bits (reg 0x200, bits 24/25) that
812 * apparently can be used to check whether the data have been
813 * received, but I couldn't get it to work reliably.
815 regmap_read(dsi
->regs
, SUN6I_DSI_CMD_CTL_REG
, &val
);
816 if (val
& SUN6I_DSI_CMD_CTL_RX_OVERFLOW
)
819 regmap_read(dsi
->regs
, SUN6I_DSI_CMD_RX_REG(0), &val
);
821 if (byte0
== MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT
)
824 ((u8
*)msg
->rx_buf
)[0] = (val
>> 8);
829 static int sun6i_dsi_attach(struct mipi_dsi_host
*host
,
830 struct mipi_dsi_device
*device
)
832 struct sun6i_dsi
*dsi
= host_to_sun6i_dsi(host
);
834 dsi
->device
= device
;
835 dsi
->panel
= of_drm_find_panel(device
->dev
.of_node
);
836 if (IS_ERR(dsi
->panel
))
837 return PTR_ERR(dsi
->panel
);
839 dev_info(host
->dev
, "Attached device %s\n", device
->name
);
844 static int sun6i_dsi_detach(struct mipi_dsi_host
*host
,
845 struct mipi_dsi_device
*device
)
847 struct sun6i_dsi
*dsi
= host_to_sun6i_dsi(host
);
855 static ssize_t
sun6i_dsi_transfer(struct mipi_dsi_host
*host
,
856 const struct mipi_dsi_msg
*msg
)
858 struct sun6i_dsi
*dsi
= host_to_sun6i_dsi(host
);
861 ret
= sun6i_dsi_inst_wait_for_completion(dsi
);
863 sun6i_dsi_inst_abort(dsi
);
865 regmap_write(dsi
->regs
, SUN6I_DSI_CMD_CTL_REG
,
866 SUN6I_DSI_CMD_CTL_RX_OVERFLOW
|
867 SUN6I_DSI_CMD_CTL_RX_FLAG
|
868 SUN6I_DSI_CMD_CTL_TX_FLAG
);
871 case MIPI_DSI_DCS_SHORT_WRITE
:
872 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
873 ret
= sun6i_dsi_dcs_write_short(dsi
, msg
);
876 case MIPI_DSI_DCS_LONG_WRITE
:
877 ret
= sun6i_dsi_dcs_write_long(dsi
, msg
);
880 case MIPI_DSI_DCS_READ
:
881 if (msg
->rx_len
== 1) {
882 ret
= sun6i_dsi_dcs_read(dsi
, msg
);
893 static const struct mipi_dsi_host_ops sun6i_dsi_host_ops
= {
894 .attach
= sun6i_dsi_attach
,
895 .detach
= sun6i_dsi_detach
,
896 .transfer
= sun6i_dsi_transfer
,
899 static const struct regmap_config sun6i_dsi_regmap_config
= {
903 .max_register
= SUN6I_DSI_CMD_TX_REG(255),
907 static int sun6i_dsi_bind(struct device
*dev
, struct device
*master
,
910 struct drm_device
*drm
= data
;
911 struct sun4i_drv
*drv
= drm
->dev_private
;
912 struct sun6i_dsi
*dsi
= dev_get_drvdata(dev
);
916 return -EPROBE_DEFER
;
920 drm_encoder_helper_add(&dsi
->encoder
,
921 &sun6i_dsi_enc_helper_funcs
);
922 ret
= drm_encoder_init(drm
,
924 &sun6i_dsi_enc_funcs
,
925 DRM_MODE_ENCODER_DSI
,
928 dev_err(dsi
->dev
, "Couldn't initialise the DSI encoder\n");
931 dsi
->encoder
.possible_crtcs
= BIT(0);
933 drm_connector_helper_add(&dsi
->connector
,
934 &sun6i_dsi_connector_helper_funcs
);
935 ret
= drm_connector_init(drm
, &dsi
->connector
,
936 &sun6i_dsi_connector_funcs
,
937 DRM_MODE_CONNECTOR_DSI
);
940 "Couldn't initialise the DSI connector\n");
941 goto err_cleanup_connector
;
944 drm_connector_attach_encoder(&dsi
->connector
, &dsi
->encoder
);
945 drm_panel_attach(dsi
->panel
, &dsi
->connector
);
949 err_cleanup_connector
:
950 drm_encoder_cleanup(&dsi
->encoder
);
954 static void sun6i_dsi_unbind(struct device
*dev
, struct device
*master
,
957 struct sun6i_dsi
*dsi
= dev_get_drvdata(dev
);
959 drm_panel_detach(dsi
->panel
);
962 static const struct component_ops sun6i_dsi_ops
= {
963 .bind
= sun6i_dsi_bind
,
964 .unbind
= sun6i_dsi_unbind
,
967 static int sun6i_dsi_probe(struct platform_device
*pdev
)
969 struct device
*dev
= &pdev
->dev
;
970 struct device_node
*dphy_node
;
971 struct sun6i_dsi
*dsi
;
972 struct resource
*res
;
976 dsi
= devm_kzalloc(dev
, sizeof(*dsi
), GFP_KERNEL
);
979 dev_set_drvdata(dev
, dsi
);
981 dsi
->host
.ops
= &sun6i_dsi_host_ops
;
984 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
985 base
= devm_ioremap_resource(dev
, res
);
987 dev_err(dev
, "Couldn't map the DSI encoder registers\n");
988 return PTR_ERR(base
);
991 dsi
->regs
= devm_regmap_init_mmio_clk(dev
, "bus", base
,
992 &sun6i_dsi_regmap_config
);
993 if (IS_ERR(dsi
->regs
)) {
994 dev_err(dev
, "Couldn't create the DSI encoder regmap\n");
995 return PTR_ERR(dsi
->regs
);
998 dsi
->reset
= devm_reset_control_get_shared(dev
, NULL
);
999 if (IS_ERR(dsi
->reset
)) {
1000 dev_err(dev
, "Couldn't get our reset line\n");
1001 return PTR_ERR(dsi
->reset
);
1004 dsi
->mod_clk
= devm_clk_get(dev
, "mod");
1005 if (IS_ERR(dsi
->mod_clk
)) {
1006 dev_err(dev
, "Couldn't get the DSI mod clock\n");
1007 return PTR_ERR(dsi
->mod_clk
);
1011 * In order to operate properly, that clock seems to be always
1014 clk_set_rate_exclusive(dsi
->mod_clk
, 297000000);
1016 dphy_node
= of_parse_phandle(dev
->of_node
, "phys", 0);
1017 ret
= sun6i_dphy_probe(dsi
, dphy_node
);
1018 of_node_put(dphy_node
);
1020 dev_err(dev
, "Couldn't get the MIPI D-PHY\n");
1021 goto err_unprotect_clk
;
1024 pm_runtime_enable(dev
);
1026 ret
= mipi_dsi_host_register(&dsi
->host
);
1028 dev_err(dev
, "Couldn't register MIPI-DSI host\n");
1029 goto err_remove_phy
;
1032 ret
= component_add(&pdev
->dev
, &sun6i_dsi_ops
);
1034 dev_err(dev
, "Couldn't register our component\n");
1035 goto err_remove_dsi_host
;
1040 err_remove_dsi_host
:
1041 mipi_dsi_host_unregister(&dsi
->host
);
1043 pm_runtime_disable(dev
);
1044 sun6i_dphy_remove(dsi
);
1046 clk_rate_exclusive_put(dsi
->mod_clk
);
1050 static int sun6i_dsi_remove(struct platform_device
*pdev
)
1052 struct device
*dev
= &pdev
->dev
;
1053 struct sun6i_dsi
*dsi
= dev_get_drvdata(dev
);
1055 component_del(&pdev
->dev
, &sun6i_dsi_ops
);
1056 mipi_dsi_host_unregister(&dsi
->host
);
1057 pm_runtime_disable(dev
);
1058 sun6i_dphy_remove(dsi
);
1059 clk_rate_exclusive_put(dsi
->mod_clk
);
1064 static int __maybe_unused
sun6i_dsi_runtime_resume(struct device
*dev
)
1066 struct sun6i_dsi
*dsi
= dev_get_drvdata(dev
);
1068 reset_control_deassert(dsi
->reset
);
1069 clk_prepare_enable(dsi
->mod_clk
);
1072 * Enable the DSI block.
1074 * Some part of it can only be done once we get a number of
1075 * lanes, see sun6i_dsi_inst_init
1077 regmap_write(dsi
->regs
, SUN6I_DSI_CTL_REG
, SUN6I_DSI_CTL_EN
);
1079 regmap_write(dsi
->regs
, SUN6I_DSI_BASIC_CTL0_REG
,
1080 SUN6I_DSI_BASIC_CTL0_ECC_EN
| SUN6I_DSI_BASIC_CTL0_CRC_EN
);
1082 regmap_write(dsi
->regs
, SUN6I_DSI_TRANS_START_REG
, 10);
1083 regmap_write(dsi
->regs
, SUN6I_DSI_TRANS_ZERO_REG
, 0);
1086 sun6i_dsi_inst_init(dsi
, dsi
->device
);
1088 regmap_write(dsi
->regs
, SUN6I_DSI_DEBUG_DATA_REG
, 0xff);
1093 static int __maybe_unused
sun6i_dsi_runtime_suspend(struct device
*dev
)
1095 struct sun6i_dsi
*dsi
= dev_get_drvdata(dev
);
1097 clk_disable_unprepare(dsi
->mod_clk
);
1098 reset_control_assert(dsi
->reset
);
1103 static const struct dev_pm_ops sun6i_dsi_pm_ops
= {
1104 SET_RUNTIME_PM_OPS(sun6i_dsi_runtime_suspend
,
1105 sun6i_dsi_runtime_resume
,
1109 static const struct of_device_id sun6i_dsi_of_table
[] = {
1110 { .compatible
= "allwinner,sun6i-a31-mipi-dsi" },
1113 MODULE_DEVICE_TABLE(of
, sun6i_dsi_of_table
);
1115 static struct platform_driver sun6i_dsi_platform_driver
= {
1116 .probe
= sun6i_dsi_probe
,
1117 .remove
= sun6i_dsi_remove
,
1119 .name
= "sun6i-mipi-dsi",
1120 .of_match_table
= sun6i_dsi_of_table
,
1121 .pm
= &sun6i_dsi_pm_ops
,
1124 module_platform_driver(sun6i_dsi_platform_driver
);
1126 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1127 MODULE_DESCRIPTION("Allwinner A31 DSI Driver");
1128 MODULE_LICENSE("GPL");