1 // SPDX-License-Identifier: GPL-2.0+
3 * i.MX8 NWL MIPI DSI host driver
5 * Copyright (C) 2017 NXP
6 * Copyright (C) 2020 Purism SPC
9 #include <linux/bitfield.h>
10 #include <linux/clk.h>
11 #include <linux/irq.h>
12 #include <linux/math64.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/mux/consumer.h>
17 #include <linux/of_platform.h>
18 #include <linux/phy/phy.h>
19 #include <linux/regmap.h>
20 #include <linux/reset.h>
21 #include <linux/sys_soc.h>
22 #include <linux/time64.h>
24 #include <drm/drm_bridge.h>
25 #include <drm/drm_mipi_dsi.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_panel.h>
28 #include <drm/drm_print.h>
30 #include <video/mipi_display.h>
34 #define DRV_NAME "nwl-dsi"
36 /* i.MX8 NWL quirks */
37 /* i.MX8MQ errata E11418 */
38 #define E11418_HS_MODE_QUIRK BIT(0)
40 #define NWL_DSI_MIPI_FIFO_TIMEOUT msecs_to_jiffies(500)
42 enum transfer_direction
{
47 #define NWL_DSI_ENDPOINT_LCDIF 0
48 #define NWL_DSI_ENDPOINT_DCSS 1
50 struct nwl_dsi_plat_clk_config
{
56 struct nwl_dsi_transfer
{
57 const struct mipi_dsi_msg
*msg
;
58 struct mipi_dsi_packet packet
;
59 struct completion completed
;
61 int status
; /* status of transmission */
62 enum transfer_direction direction
;
66 size_t tx_len
; /* in bytes */
67 size_t rx_len
; /* in bytes */
71 struct drm_bridge bridge
;
72 struct mipi_dsi_host dsi_host
;
73 struct drm_bridge
*panel_bridge
;
76 union phy_configure_opts phy_cfg
;
79 struct regmap
*regmap
;
82 * The DSI host controller needs this reset sequence according to NWL:
83 * 1. Deassert pclk reset to get access to DSI regs
84 * 2. Configure DSI Host and DPHY and enable DPHY
85 * 3. Deassert ESC and BYTE resets to allow host TX operations)
86 * 4. Send DSI cmds to configure peripheral (handled by panel drv)
87 * 5. Deassert DPI reset so DPI receives pixels and starts sending
90 * TODO: Since panel_bridges do their DSI setup in enable we
91 * currently have 4. and 5. swapped.
93 struct reset_control
*rst_byte
;
94 struct reset_control
*rst_esc
;
95 struct reset_control
*rst_dpi
;
96 struct reset_control
*rst_pclk
;
97 struct mux_control
*mux
;
100 struct clk
*phy_ref_clk
;
101 struct clk
*rx_esc_clk
;
102 struct clk
*tx_esc_clk
;
103 struct clk
*core_clk
;
105 * hardware bug: the i.MX8MQ needs this clock on during reset
106 * even when not using LCDIF.
108 struct clk
*lcdif_clk
;
112 enum mipi_dsi_pixel_format format
;
113 struct drm_display_mode mode
;
114 unsigned long dsi_mode_flags
;
117 struct nwl_dsi_transfer
*xfer
;
120 static const struct regmap_config nwl_dsi_regmap_config
= {
124 .max_register
= NWL_DSI_IRQ_MASK2
,
128 static inline struct nwl_dsi
*bridge_to_dsi(struct drm_bridge
*bridge
)
130 return container_of(bridge
, struct nwl_dsi
, bridge
);
133 static int nwl_dsi_clear_error(struct nwl_dsi
*dsi
)
135 int ret
= dsi
->error
;
141 static void nwl_dsi_write(struct nwl_dsi
*dsi
, unsigned int reg
, u32 val
)
148 ret
= regmap_write(dsi
->regmap
, reg
, val
);
150 DRM_DEV_ERROR(dsi
->dev
,
151 "Failed to write NWL DSI reg 0x%x: %d\n", reg
,
157 static u32
nwl_dsi_read(struct nwl_dsi
*dsi
, u32 reg
)
165 ret
= regmap_read(dsi
->regmap
, reg
, &val
);
167 DRM_DEV_ERROR(dsi
->dev
, "Failed to read NWL DSI reg 0x%x: %d\n",
174 static int nwl_dsi_get_dpi_pixel_format(enum mipi_dsi_pixel_format format
)
177 case MIPI_DSI_FMT_RGB565
:
178 return NWL_DSI_PIXEL_FORMAT_16
;
179 case MIPI_DSI_FMT_RGB666
:
180 return NWL_DSI_PIXEL_FORMAT_18L
;
181 case MIPI_DSI_FMT_RGB666_PACKED
:
182 return NWL_DSI_PIXEL_FORMAT_18
;
183 case MIPI_DSI_FMT_RGB888
:
184 return NWL_DSI_PIXEL_FORMAT_24
;
191 * ps2bc - Picoseconds to byte clock cycles
193 static u32
ps2bc(struct nwl_dsi
*dsi
, unsigned long long ps
)
195 u32 bpp
= mipi_dsi_pixel_format_to_bpp(dsi
->format
);
197 return DIV64_U64_ROUND_UP(ps
* dsi
->mode
.clock
* bpp
,
198 dsi
->lanes
* 8 * NSEC_PER_SEC
);
202 * ui2bc - UI time periods to byte clock cycles
204 static u32
ui2bc(struct nwl_dsi
*dsi
, unsigned long long ui
)
206 u32 bpp
= mipi_dsi_pixel_format_to_bpp(dsi
->format
);
208 return DIV64_U64_ROUND_UP(ui
* dsi
->lanes
,
209 dsi
->mode
.clock
* 1000 * bpp
);
213 * us2bc - micro seconds to lp clock cycles
215 static u32
us2lp(u32 lp_clk_rate
, unsigned long us
)
217 return DIV_ROUND_UP(us
* lp_clk_rate
, USEC_PER_SEC
);
220 static int nwl_dsi_config_host(struct nwl_dsi
*dsi
)
223 struct phy_configure_opts_mipi_dphy
*cfg
= &dsi
->phy_cfg
.mipi_dphy
;
225 if (dsi
->lanes
< 1 || dsi
->lanes
> 4)
228 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "DSI Lanes %d\n", dsi
->lanes
);
229 nwl_dsi_write(dsi
, NWL_DSI_CFG_NUM_LANES
, dsi
->lanes
- 1);
231 if (dsi
->dsi_mode_flags
& MIPI_DSI_CLOCK_NON_CONTINUOUS
) {
232 nwl_dsi_write(dsi
, NWL_DSI_CFG_NONCONTINUOUS_CLK
, 0x01);
233 nwl_dsi_write(dsi
, NWL_DSI_CFG_AUTOINSERT_EOTP
, 0x01);
235 nwl_dsi_write(dsi
, NWL_DSI_CFG_NONCONTINUOUS_CLK
, 0x00);
236 nwl_dsi_write(dsi
, NWL_DSI_CFG_AUTOINSERT_EOTP
, 0x00);
239 /* values in byte clock cycles */
240 cycles
= ui2bc(dsi
, cfg
->clk_pre
);
241 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "cfg_t_pre: 0x%x\n", cycles
);
242 nwl_dsi_write(dsi
, NWL_DSI_CFG_T_PRE
, cycles
);
243 cycles
= ps2bc(dsi
, cfg
->lpx
+ cfg
->clk_prepare
+ cfg
->clk_zero
);
244 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "cfg_tx_gap (pre): 0x%x\n", cycles
);
245 cycles
+= ui2bc(dsi
, cfg
->clk_pre
);
246 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "cfg_t_post: 0x%x\n", cycles
);
247 nwl_dsi_write(dsi
, NWL_DSI_CFG_T_POST
, cycles
);
248 cycles
= ps2bc(dsi
, cfg
->hs_exit
);
249 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "cfg_tx_gap: 0x%x\n", cycles
);
250 nwl_dsi_write(dsi
, NWL_DSI_CFG_TX_GAP
, cycles
);
252 nwl_dsi_write(dsi
, NWL_DSI_CFG_EXTRA_CMDS_AFTER_EOTP
, 0x01);
253 nwl_dsi_write(dsi
, NWL_DSI_CFG_HTX_TO_COUNT
, 0x00);
254 nwl_dsi_write(dsi
, NWL_DSI_CFG_LRX_H_TO_COUNT
, 0x00);
255 nwl_dsi_write(dsi
, NWL_DSI_CFG_BTA_H_TO_COUNT
, 0x00);
256 /* In LP clock cycles */
257 cycles
= us2lp(cfg
->lp_clk_rate
, cfg
->wakeup
);
258 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "cfg_twakeup: 0x%x\n", cycles
);
259 nwl_dsi_write(dsi
, NWL_DSI_CFG_TWAKEUP
, cycles
);
261 return nwl_dsi_clear_error(dsi
);
264 static int nwl_dsi_config_dpi(struct nwl_dsi
*dsi
)
269 int hfront_porch
, hback_porch
, vfront_porch
, vback_porch
;
270 int hsync_len
, vsync_len
;
272 hfront_porch
= dsi
->mode
.hsync_start
- dsi
->mode
.hdisplay
;
273 hsync_len
= dsi
->mode
.hsync_end
- dsi
->mode
.hsync_start
;
274 hback_porch
= dsi
->mode
.htotal
- dsi
->mode
.hsync_end
;
276 vfront_porch
= dsi
->mode
.vsync_start
- dsi
->mode
.vdisplay
;
277 vsync_len
= dsi
->mode
.vsync_end
- dsi
->mode
.vsync_start
;
278 vback_porch
= dsi
->mode
.vtotal
- dsi
->mode
.vsync_end
;
280 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "hfront_porch = %d\n", hfront_porch
);
281 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "hback_porch = %d\n", hback_porch
);
282 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "hsync_len = %d\n", hsync_len
);
283 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "hdisplay = %d\n", dsi
->mode
.hdisplay
);
284 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "vfront_porch = %d\n", vfront_porch
);
285 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "vback_porch = %d\n", vback_porch
);
286 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "vsync_len = %d\n", vsync_len
);
287 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "vactive = %d\n", dsi
->mode
.vdisplay
);
288 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "clock = %d kHz\n", dsi
->mode
.clock
);
290 color_format
= nwl_dsi_get_dpi_pixel_format(dsi
->format
);
291 if (color_format
< 0) {
292 DRM_DEV_ERROR(dsi
->dev
, "Invalid color format 0x%x\n",
296 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "pixel fmt = %d\n", dsi
->format
);
298 nwl_dsi_write(dsi
, NWL_DSI_INTERFACE_COLOR_CODING
, NWL_DSI_DPI_24_BIT
);
299 nwl_dsi_write(dsi
, NWL_DSI_PIXEL_FORMAT
, color_format
);
301 * Adjusting input polarity based on the video mode results in
302 * a black screen so always pick active low:
304 nwl_dsi_write(dsi
, NWL_DSI_VSYNC_POLARITY
,
305 NWL_DSI_VSYNC_POLARITY_ACTIVE_LOW
);
306 nwl_dsi_write(dsi
, NWL_DSI_HSYNC_POLARITY
,
307 NWL_DSI_HSYNC_POLARITY_ACTIVE_LOW
);
309 burst_mode
= (dsi
->dsi_mode_flags
& MIPI_DSI_MODE_VIDEO_BURST
) &&
310 !(dsi
->dsi_mode_flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
);
313 nwl_dsi_write(dsi
, NWL_DSI_VIDEO_MODE
, NWL_DSI_VM_BURST_MODE
);
314 nwl_dsi_write(dsi
, NWL_DSI_PIXEL_FIFO_SEND_LEVEL
, 256);
316 mode
= ((dsi
->dsi_mode_flags
& MIPI_DSI_MODE_VIDEO_SYNC_PULSE
) ?
317 NWL_DSI_VM_BURST_MODE_WITH_SYNC_PULSES
:
318 NWL_DSI_VM_NON_BURST_MODE_WITH_SYNC_EVENTS
);
319 nwl_dsi_write(dsi
, NWL_DSI_VIDEO_MODE
, mode
);
320 nwl_dsi_write(dsi
, NWL_DSI_PIXEL_FIFO_SEND_LEVEL
,
324 nwl_dsi_write(dsi
, NWL_DSI_HFP
, hfront_porch
);
325 nwl_dsi_write(dsi
, NWL_DSI_HBP
, hback_porch
);
326 nwl_dsi_write(dsi
, NWL_DSI_HSA
, hsync_len
);
328 nwl_dsi_write(dsi
, NWL_DSI_ENABLE_MULT_PKTS
, 0x0);
329 nwl_dsi_write(dsi
, NWL_DSI_BLLP_MODE
, 0x1);
330 nwl_dsi_write(dsi
, NWL_DSI_USE_NULL_PKT_BLLP
, 0x0);
331 nwl_dsi_write(dsi
, NWL_DSI_VC
, 0x0);
333 nwl_dsi_write(dsi
, NWL_DSI_PIXEL_PAYLOAD_SIZE
, dsi
->mode
.hdisplay
);
334 nwl_dsi_write(dsi
, NWL_DSI_VACTIVE
, dsi
->mode
.vdisplay
- 1);
335 nwl_dsi_write(dsi
, NWL_DSI_VBP
, vback_porch
);
336 nwl_dsi_write(dsi
, NWL_DSI_VFP
, vfront_porch
);
338 return nwl_dsi_clear_error(dsi
);
341 static int nwl_dsi_init_interrupts(struct nwl_dsi
*dsi
)
345 nwl_dsi_write(dsi
, NWL_DSI_IRQ_MASK
, 0xffffffff);
346 nwl_dsi_write(dsi
, NWL_DSI_IRQ_MASK2
, 0x7);
348 irq_enable
= ~(u32
)(NWL_DSI_TX_PKT_DONE_MASK
|
349 NWL_DSI_RX_PKT_HDR_RCVD_MASK
|
350 NWL_DSI_TX_FIFO_OVFLW_MASK
|
351 NWL_DSI_HS_TX_TIMEOUT_MASK
);
353 nwl_dsi_write(dsi
, NWL_DSI_IRQ_MASK
, irq_enable
);
355 return nwl_dsi_clear_error(dsi
);
358 static int nwl_dsi_host_attach(struct mipi_dsi_host
*dsi_host
,
359 struct mipi_dsi_device
*device
)
361 struct nwl_dsi
*dsi
= container_of(dsi_host
, struct nwl_dsi
, dsi_host
);
362 struct device
*dev
= dsi
->dev
;
364 DRM_DEV_INFO(dev
, "lanes=%u, format=0x%x flags=0x%lx\n", device
->lanes
,
365 device
->format
, device
->mode_flags
);
367 if (device
->lanes
< 1 || device
->lanes
> 4)
370 dsi
->lanes
= device
->lanes
;
371 dsi
->format
= device
->format
;
372 dsi
->dsi_mode_flags
= device
->mode_flags
;
377 static bool nwl_dsi_read_packet(struct nwl_dsi
*dsi
, u32 status
)
379 struct device
*dev
= dsi
->dev
;
380 struct nwl_dsi_transfer
*xfer
= dsi
->xfer
;
382 u8
*payload
= xfer
->msg
->rx_buf
;
390 if (xfer
->rx_word_count
== 0) {
391 if (!(status
& NWL_DSI_RX_PKT_HDR_RCVD
))
393 /* Get the RX header and parse it */
394 val
= nwl_dsi_read(dsi
, NWL_DSI_RX_PKT_HEADER
);
395 err
= nwl_dsi_clear_error(dsi
);
398 word_count
= NWL_DSI_WC(val
);
399 channel
= NWL_DSI_RX_VC(val
);
400 data_type
= NWL_DSI_RX_DT(val
);
402 if (channel
!= xfer
->msg
->channel
) {
404 "[%02X] Channel mismatch (%u != %u)\n",
405 xfer
->cmd
, channel
, xfer
->msg
->channel
);
406 xfer
->status
= -EINVAL
;
411 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE
:
412 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE
:
413 if (xfer
->msg
->rx_len
> 1) {
414 /* read second byte */
415 payload
[1] = word_count
>> 8;
419 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE
:
420 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE
:
421 if (xfer
->msg
->rx_len
> 0) {
422 /* read first byte */
423 payload
[0] = word_count
& 0xff;
426 xfer
->status
= xfer
->rx_len
;
428 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT
:
430 DRM_DEV_ERROR(dev
, "[%02X] DSI error report: 0x%02x\n",
431 xfer
->cmd
, word_count
);
432 xfer
->status
= -EPROTO
;
436 if (word_count
> xfer
->msg
->rx_len
) {
438 "[%02X] Receive buffer too small: %zu (< %u)\n",
439 xfer
->cmd
, xfer
->msg
->rx_len
, word_count
);
440 xfer
->status
= -EINVAL
;
444 xfer
->rx_word_count
= word_count
;
446 /* Set word_count from previous header read */
447 word_count
= xfer
->rx_word_count
;
450 /* If RX payload is not yet received, wait for it */
451 if (!(status
& NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD
))
454 /* Read the RX payload */
455 while (word_count
>= 4) {
456 val
= nwl_dsi_read(dsi
, NWL_DSI_RX_PAYLOAD
);
457 payload
[0] = (val
>> 0) & 0xff;
458 payload
[1] = (val
>> 8) & 0xff;
459 payload
[2] = (val
>> 16) & 0xff;
460 payload
[3] = (val
>> 24) & 0xff;
466 if (word_count
> 0) {
467 val
= nwl_dsi_read(dsi
, NWL_DSI_RX_PAYLOAD
);
468 switch (word_count
) {
470 payload
[2] = (val
>> 16) & 0xff;
474 payload
[1] = (val
>> 8) & 0xff;
478 payload
[0] = (val
>> 0) & 0xff;
484 xfer
->status
= xfer
->rx_len
;
485 err
= nwl_dsi_clear_error(dsi
);
492 static void nwl_dsi_finish_transmission(struct nwl_dsi
*dsi
, u32 status
)
494 struct nwl_dsi_transfer
*xfer
= dsi
->xfer
;
495 bool end_packet
= false;
500 if (xfer
->direction
== DSI_PACKET_SEND
&&
501 status
& NWL_DSI_TX_PKT_DONE
) {
502 xfer
->status
= xfer
->tx_len
;
504 } else if (status
& NWL_DSI_DPHY_DIRECTION
&&
505 ((status
& (NWL_DSI_RX_PKT_HDR_RCVD
|
506 NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD
)))) {
507 end_packet
= nwl_dsi_read_packet(dsi
, status
);
511 complete(&xfer
->completed
);
514 static void nwl_dsi_begin_transmission(struct nwl_dsi
*dsi
)
516 struct nwl_dsi_transfer
*xfer
= dsi
->xfer
;
517 struct mipi_dsi_packet
*pkt
= &xfer
->packet
;
523 u32 hs_workaround
= 0;
525 /* Send the payload, if any */
526 length
= pkt
->payload_length
;
527 payload
= pkt
->payload
;
529 while (length
>= 4) {
530 val
= *(u32
*)payload
;
531 hs_workaround
|= !(val
& 0xFFFF00);
532 nwl_dsi_write(dsi
, NWL_DSI_TX_PAYLOAD
, val
);
536 /* Send the rest of the payload */
540 val
|= payload
[2] << 16;
543 val
|= payload
[1] << 8;
544 hs_workaround
|= !(val
& 0xFFFF00);
548 nwl_dsi_write(dsi
, NWL_DSI_TX_PAYLOAD
, val
);
551 xfer
->tx_len
= pkt
->payload_length
;
555 * header[0] = Virtual Channel + Data Type
556 * header[1] = Word Count LSB (LP) or first param (SP)
557 * header[2] = Word Count MSB (LP) or second param (SP)
559 word_count
= pkt
->header
[1] | (pkt
->header
[2] << 8);
560 if (hs_workaround
&& (dsi
->quirks
& E11418_HS_MODE_QUIRK
)) {
561 DRM_DEV_DEBUG_DRIVER(dsi
->dev
,
562 "Using hs mode workaround for cmd 0x%x\n",
566 hs_mode
= (xfer
->msg
->flags
& MIPI_DSI_MSG_USE_LPM
) ? 0 : 1;
568 val
= NWL_DSI_WC(word_count
) | NWL_DSI_TX_VC(xfer
->msg
->channel
) |
569 NWL_DSI_TX_DT(xfer
->msg
->type
) | NWL_DSI_HS_SEL(hs_mode
) |
570 NWL_DSI_BTA_TX(xfer
->need_bta
);
571 nwl_dsi_write(dsi
, NWL_DSI_PKT_CONTROL
, val
);
573 /* Send packet command */
574 nwl_dsi_write(dsi
, NWL_DSI_SEND_PACKET
, 0x1);
577 static ssize_t
nwl_dsi_host_transfer(struct mipi_dsi_host
*dsi_host
,
578 const struct mipi_dsi_msg
*msg
)
580 struct nwl_dsi
*dsi
= container_of(dsi_host
, struct nwl_dsi
, dsi_host
);
581 struct nwl_dsi_transfer xfer
;
584 /* Create packet to be sent */
586 ret
= mipi_dsi_create_packet(&xfer
.packet
, msg
);
592 if ((msg
->type
& MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM
||
593 msg
->type
& MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM
||
594 msg
->type
& MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM
||
595 msg
->type
& MIPI_DSI_DCS_READ
) &&
596 msg
->rx_len
> 0 && msg
->rx_buf
)
597 xfer
.direction
= DSI_PACKET_RECEIVE
;
599 xfer
.direction
= DSI_PACKET_SEND
;
601 xfer
.need_bta
= (xfer
.direction
== DSI_PACKET_RECEIVE
);
602 xfer
.need_bta
|= (msg
->flags
& MIPI_DSI_MSG_REQ_ACK
) ? 1 : 0;
604 xfer
.status
= -ETIMEDOUT
;
605 xfer
.rx_word_count
= 0;
609 xfer
.cmd
= ((u8
*)(msg
->tx_buf
))[0];
610 init_completion(&xfer
.completed
);
612 ret
= clk_prepare_enable(dsi
->rx_esc_clk
);
614 DRM_DEV_ERROR(dsi
->dev
, "Failed to enable rx_esc clk: %zd\n",
618 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "Enabled rx_esc clk @%lu Hz\n",
619 clk_get_rate(dsi
->rx_esc_clk
));
621 /* Initiate the DSI packet transmision */
622 nwl_dsi_begin_transmission(dsi
);
624 if (!wait_for_completion_timeout(&xfer
.completed
,
625 NWL_DSI_MIPI_FIFO_TIMEOUT
)) {
626 DRM_DEV_ERROR(dsi_host
->dev
, "[%02X] DSI transfer timed out\n",
633 clk_disable_unprepare(dsi
->rx_esc_clk
);
638 static const struct mipi_dsi_host_ops nwl_dsi_host_ops
= {
639 .attach
= nwl_dsi_host_attach
,
640 .transfer
= nwl_dsi_host_transfer
,
643 static irqreturn_t
nwl_dsi_irq_handler(int irq
, void *data
)
646 struct nwl_dsi
*dsi
= data
;
648 irq_status
= nwl_dsi_read(dsi
, NWL_DSI_IRQ_STATUS
);
650 if (irq_status
& NWL_DSI_TX_FIFO_OVFLW
)
651 DRM_DEV_ERROR_RATELIMITED(dsi
->dev
, "tx fifo overflow\n");
653 if (irq_status
& NWL_DSI_HS_TX_TIMEOUT
)
654 DRM_DEV_ERROR_RATELIMITED(dsi
->dev
, "HS tx timeout\n");
656 if (irq_status
& NWL_DSI_TX_PKT_DONE
||
657 irq_status
& NWL_DSI_RX_PKT_HDR_RCVD
||
658 irq_status
& NWL_DSI_RX_PKT_PAYLOAD_DATA_RCVD
)
659 nwl_dsi_finish_transmission(dsi
, irq_status
);
664 static int nwl_dsi_enable(struct nwl_dsi
*dsi
)
666 struct device
*dev
= dsi
->dev
;
667 union phy_configure_opts
*phy_cfg
= &dsi
->phy_cfg
;
671 DRM_DEV_ERROR(dev
, "Need DSI lanes: %d\n", dsi
->lanes
);
675 ret
= phy_init(dsi
->phy
);
677 DRM_DEV_ERROR(dev
, "Failed to init DSI phy: %d\n", ret
);
681 ret
= phy_configure(dsi
->phy
, phy_cfg
);
683 DRM_DEV_ERROR(dev
, "Failed to configure DSI phy: %d\n", ret
);
687 ret
= clk_prepare_enable(dsi
->tx_esc_clk
);
689 DRM_DEV_ERROR(dsi
->dev
, "Failed to enable tx_esc clk: %d\n",
693 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "Enabled tx_esc clk @%lu Hz\n",
694 clk_get_rate(dsi
->tx_esc_clk
));
696 ret
= nwl_dsi_config_host(dsi
);
698 DRM_DEV_ERROR(dev
, "Failed to set up DSI: %d", ret
);
702 ret
= nwl_dsi_config_dpi(dsi
);
704 DRM_DEV_ERROR(dev
, "Failed to set up DPI: %d", ret
);
708 ret
= phy_power_on(dsi
->phy
);
710 DRM_DEV_ERROR(dev
, "Failed to power on DPHY (%d)\n", ret
);
714 ret
= nwl_dsi_init_interrupts(dsi
);
721 phy_power_off(dsi
->phy
);
723 clk_disable_unprepare(dsi
->tx_esc_clk
);
730 static int nwl_dsi_disable(struct nwl_dsi
*dsi
)
732 struct device
*dev
= dsi
->dev
;
734 DRM_DEV_DEBUG_DRIVER(dev
, "Disabling clocks and phy\n");
736 phy_power_off(dsi
->phy
);
739 /* Disabling the clock before the phy breaks enabling dsi again */
740 clk_disable_unprepare(dsi
->tx_esc_clk
);
745 static void nwl_dsi_bridge_disable(struct drm_bridge
*bridge
)
747 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
750 nwl_dsi_disable(dsi
);
752 ret
= reset_control_assert(dsi
->rst_dpi
);
754 DRM_DEV_ERROR(dsi
->dev
, "Failed to assert DPI: %d\n", ret
);
757 ret
= reset_control_assert(dsi
->rst_byte
);
759 DRM_DEV_ERROR(dsi
->dev
, "Failed to assert ESC: %d\n", ret
);
762 ret
= reset_control_assert(dsi
->rst_esc
);
764 DRM_DEV_ERROR(dsi
->dev
, "Failed to assert BYTE: %d\n", ret
);
767 ret
= reset_control_assert(dsi
->rst_pclk
);
769 DRM_DEV_ERROR(dsi
->dev
, "Failed to assert PCLK: %d\n", ret
);
773 clk_disable_unprepare(dsi
->core_clk
);
774 clk_disable_unprepare(dsi
->lcdif_clk
);
776 pm_runtime_put(dsi
->dev
);
779 static int nwl_dsi_get_dphy_params(struct nwl_dsi
*dsi
,
780 const struct drm_display_mode
*mode
,
781 union phy_configure_opts
*phy_opts
)
786 if (dsi
->lanes
< 1 || dsi
->lanes
> 4)
790 * So far the DPHY spec minimal timings work for both mixel
791 * dphy and nwl dsi host
793 ret
= phy_mipi_dphy_get_default_config(mode
->clock
* 1000,
794 mipi_dsi_pixel_format_to_bpp(dsi
->format
), dsi
->lanes
,
795 &phy_opts
->mipi_dphy
);
799 rate
= clk_get_rate(dsi
->tx_esc_clk
);
800 DRM_DEV_DEBUG_DRIVER(dsi
->dev
, "LP clk is @%lu Hz\n", rate
);
801 phy_opts
->mipi_dphy
.lp_clk_rate
= rate
;
806 static bool nwl_dsi_bridge_mode_fixup(struct drm_bridge
*bridge
,
807 const struct drm_display_mode
*mode
,
808 struct drm_display_mode
*adjusted_mode
)
810 /* At least LCDIF + NWL needs active high sync */
811 adjusted_mode
->flags
|= (DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
);
812 adjusted_mode
->flags
&= ~(DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_NVSYNC
);
817 static enum drm_mode_status
818 nwl_dsi_bridge_mode_valid(struct drm_bridge
*bridge
,
819 const struct drm_display_info
*info
,
820 const struct drm_display_mode
*mode
)
822 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
823 int bpp
= mipi_dsi_pixel_format_to_bpp(dsi
->format
);
825 if (mode
->clock
* bpp
> 15000000 * dsi
->lanes
)
826 return MODE_CLOCK_HIGH
;
828 if (mode
->clock
* bpp
< 80000 * dsi
->lanes
)
829 return MODE_CLOCK_LOW
;
835 nwl_dsi_bridge_mode_set(struct drm_bridge
*bridge
,
836 const struct drm_display_mode
*mode
,
837 const struct drm_display_mode
*adjusted_mode
)
839 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
840 struct device
*dev
= dsi
->dev
;
841 union phy_configure_opts new_cfg
;
842 unsigned long phy_ref_rate
;
845 ret
= nwl_dsi_get_dphy_params(dsi
, adjusted_mode
, &new_cfg
);
850 * If hs clock is unchanged, we're all good - all parameters are
851 * derived from it atm.
853 if (new_cfg
.mipi_dphy
.hs_clk_rate
== dsi
->phy_cfg
.mipi_dphy
.hs_clk_rate
)
856 phy_ref_rate
= clk_get_rate(dsi
->phy_ref_clk
);
857 DRM_DEV_DEBUG_DRIVER(dev
, "PHY at ref rate: %lu\n", phy_ref_rate
);
858 /* Save the new desired phy config */
859 memcpy(&dsi
->phy_cfg
, &new_cfg
, sizeof(new_cfg
));
861 memcpy(&dsi
->mode
, adjusted_mode
, sizeof(dsi
->mode
));
862 drm_mode_debug_printmodeline(adjusted_mode
);
865 static void nwl_dsi_bridge_pre_enable(struct drm_bridge
*bridge
)
867 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
870 pm_runtime_get_sync(dsi
->dev
);
872 if (clk_prepare_enable(dsi
->lcdif_clk
) < 0)
874 if (clk_prepare_enable(dsi
->core_clk
) < 0)
877 /* Step 1 from DSI reset-out instructions */
878 ret
= reset_control_deassert(dsi
->rst_pclk
);
880 DRM_DEV_ERROR(dsi
->dev
, "Failed to deassert PCLK: %d\n", ret
);
884 /* Step 2 from DSI reset-out instructions */
887 /* Step 3 from DSI reset-out instructions */
888 ret
= reset_control_deassert(dsi
->rst_esc
);
890 DRM_DEV_ERROR(dsi
->dev
, "Failed to deassert ESC: %d\n", ret
);
893 ret
= reset_control_deassert(dsi
->rst_byte
);
895 DRM_DEV_ERROR(dsi
->dev
, "Failed to deassert BYTE: %d\n", ret
);
900 static void nwl_dsi_bridge_enable(struct drm_bridge
*bridge
)
902 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
905 /* Step 5 from DSI reset-out instructions */
906 ret
= reset_control_deassert(dsi
->rst_dpi
);
908 DRM_DEV_ERROR(dsi
->dev
, "Failed to deassert DPI: %d\n", ret
);
911 static int nwl_dsi_bridge_attach(struct drm_bridge
*bridge
,
912 enum drm_bridge_attach_flags flags
)
914 struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
915 struct drm_bridge
*panel_bridge
;
916 struct drm_panel
*panel
;
919 ret
= drm_of_find_panel_or_bridge(dsi
->dev
->of_node
, 1, 0, &panel
,
925 panel_bridge
= drm_panel_bridge_add(panel
);
926 if (IS_ERR(panel_bridge
))
927 return PTR_ERR(panel_bridge
);
929 dsi
->panel_bridge
= panel_bridge
;
931 if (!dsi
->panel_bridge
)
932 return -EPROBE_DEFER
;
934 return drm_bridge_attach(bridge
->encoder
, dsi
->panel_bridge
, bridge
,
938 static void nwl_dsi_bridge_detach(struct drm_bridge
*bridge
)
939 { struct nwl_dsi
*dsi
= bridge_to_dsi(bridge
);
941 drm_of_panel_bridge_remove(dsi
->dev
->of_node
, 1, 0);
944 static const struct drm_bridge_funcs nwl_dsi_bridge_funcs
= {
945 .pre_enable
= nwl_dsi_bridge_pre_enable
,
946 .enable
= nwl_dsi_bridge_enable
,
947 .disable
= nwl_dsi_bridge_disable
,
948 .mode_fixup
= nwl_dsi_bridge_mode_fixup
,
949 .mode_set
= nwl_dsi_bridge_mode_set
,
950 .mode_valid
= nwl_dsi_bridge_mode_valid
,
951 .attach
= nwl_dsi_bridge_attach
,
952 .detach
= nwl_dsi_bridge_detach
,
955 static int nwl_dsi_parse_dt(struct nwl_dsi
*dsi
)
957 struct platform_device
*pdev
= to_platform_device(dsi
->dev
);
962 dsi
->phy
= devm_phy_get(dsi
->dev
, "dphy");
963 if (IS_ERR(dsi
->phy
)) {
964 ret
= PTR_ERR(dsi
->phy
);
965 if (ret
!= -EPROBE_DEFER
)
966 DRM_DEV_ERROR(dsi
->dev
, "Could not get PHY: %d\n", ret
);
970 clk
= devm_clk_get(dsi
->dev
, "lcdif");
973 DRM_DEV_ERROR(dsi
->dev
, "Failed to get lcdif clock: %d\n",
977 dsi
->lcdif_clk
= clk
;
979 clk
= devm_clk_get(dsi
->dev
, "core");
982 DRM_DEV_ERROR(dsi
->dev
, "Failed to get core clock: %d\n",
988 clk
= devm_clk_get(dsi
->dev
, "phy_ref");
991 DRM_DEV_ERROR(dsi
->dev
, "Failed to get phy_ref clock: %d\n",
995 dsi
->phy_ref_clk
= clk
;
997 clk
= devm_clk_get(dsi
->dev
, "rx_esc");
1000 DRM_DEV_ERROR(dsi
->dev
, "Failed to get rx_esc clock: %d\n",
1004 dsi
->rx_esc_clk
= clk
;
1006 clk
= devm_clk_get(dsi
->dev
, "tx_esc");
1009 DRM_DEV_ERROR(dsi
->dev
, "Failed to get tx_esc clock: %d\n",
1013 dsi
->tx_esc_clk
= clk
;
1015 dsi
->mux
= devm_mux_control_get(dsi
->dev
, NULL
);
1016 if (IS_ERR(dsi
->mux
)) {
1017 ret
= PTR_ERR(dsi
->mux
);
1018 if (ret
!= -EPROBE_DEFER
)
1019 DRM_DEV_ERROR(dsi
->dev
, "Failed to get mux: %d\n", ret
);
1023 base
= devm_platform_ioremap_resource(pdev
, 0);
1025 return PTR_ERR(base
);
1028 devm_regmap_init_mmio(dsi
->dev
, base
, &nwl_dsi_regmap_config
);
1029 if (IS_ERR(dsi
->regmap
)) {
1030 ret
= PTR_ERR(dsi
->regmap
);
1031 DRM_DEV_ERROR(dsi
->dev
, "Failed to create NWL DSI regmap: %d\n",
1036 dsi
->irq
= platform_get_irq(pdev
, 0);
1038 DRM_DEV_ERROR(dsi
->dev
, "Failed to get device IRQ: %d\n",
1043 dsi
->rst_pclk
= devm_reset_control_get_exclusive(dsi
->dev
, "pclk");
1044 if (IS_ERR(dsi
->rst_pclk
)) {
1045 DRM_DEV_ERROR(dsi
->dev
, "Failed to get pclk reset: %ld\n",
1046 PTR_ERR(dsi
->rst_pclk
));
1047 return PTR_ERR(dsi
->rst_pclk
);
1049 dsi
->rst_byte
= devm_reset_control_get_exclusive(dsi
->dev
, "byte");
1050 if (IS_ERR(dsi
->rst_byte
)) {
1051 DRM_DEV_ERROR(dsi
->dev
, "Failed to get byte reset: %ld\n",
1052 PTR_ERR(dsi
->rst_byte
));
1053 return PTR_ERR(dsi
->rst_byte
);
1055 dsi
->rst_esc
= devm_reset_control_get_exclusive(dsi
->dev
, "esc");
1056 if (IS_ERR(dsi
->rst_esc
)) {
1057 DRM_DEV_ERROR(dsi
->dev
, "Failed to get esc reset: %ld\n",
1058 PTR_ERR(dsi
->rst_esc
));
1059 return PTR_ERR(dsi
->rst_esc
);
1061 dsi
->rst_dpi
= devm_reset_control_get_exclusive(dsi
->dev
, "dpi");
1062 if (IS_ERR(dsi
->rst_dpi
)) {
1063 DRM_DEV_ERROR(dsi
->dev
, "Failed to get dpi reset: %ld\n",
1064 PTR_ERR(dsi
->rst_dpi
));
1065 return PTR_ERR(dsi
->rst_dpi
);
1070 static int nwl_dsi_select_input(struct nwl_dsi
*dsi
)
1072 struct device_node
*remote
;
1076 remote
= of_graph_get_remote_node(dsi
->dev
->of_node
, 0,
1077 NWL_DSI_ENDPOINT_LCDIF
);
1081 remote
= of_graph_get_remote_node(dsi
->dev
->of_node
, 0,
1082 NWL_DSI_ENDPOINT_DCSS
);
1084 DRM_DEV_ERROR(dsi
->dev
,
1085 "No valid input endpoint found\n");
1090 DRM_DEV_INFO(dsi
->dev
, "Using %s as input source\n",
1091 (use_dcss
) ? "DCSS" : "LCDIF");
1092 ret
= mux_control_try_select(dsi
->mux
, use_dcss
);
1094 DRM_DEV_ERROR(dsi
->dev
, "Failed to select input: %d\n", ret
);
1096 of_node_put(remote
);
1100 static int nwl_dsi_deselect_input(struct nwl_dsi
*dsi
)
1104 ret
= mux_control_deselect(dsi
->mux
);
1106 DRM_DEV_ERROR(dsi
->dev
, "Failed to deselect input: %d\n", ret
);
1111 static const struct drm_bridge_timings nwl_dsi_timings
= {
1112 .input_bus_flags
= DRM_BUS_FLAG_DE_LOW
,
1115 static const struct of_device_id nwl_dsi_dt_ids
[] = {
1116 { .compatible
= "fsl,imx8mq-nwl-dsi", },
1119 MODULE_DEVICE_TABLE(of
, nwl_dsi_dt_ids
);
1121 static const struct soc_device_attribute nwl_dsi_quirks_match
[] = {
1122 { .soc_id
= "i.MX8MQ", .revision
= "2.0",
1123 .data
= (void *)E11418_HS_MODE_QUIRK
},
1124 { /* sentinel. */ },
1127 static int nwl_dsi_probe(struct platform_device
*pdev
)
1129 struct device
*dev
= &pdev
->dev
;
1130 const struct soc_device_attribute
*attr
;
1131 struct nwl_dsi
*dsi
;
1134 dsi
= devm_kzalloc(dev
, sizeof(*dsi
), GFP_KERNEL
);
1140 ret
= nwl_dsi_parse_dt(dsi
);
1144 ret
= devm_request_irq(dev
, dsi
->irq
, nwl_dsi_irq_handler
, 0,
1145 dev_name(dev
), dsi
);
1147 DRM_DEV_ERROR(dev
, "Failed to request IRQ %d: %d\n", dsi
->irq
,
1152 dsi
->dsi_host
.ops
= &nwl_dsi_host_ops
;
1153 dsi
->dsi_host
.dev
= dev
;
1154 ret
= mipi_dsi_host_register(&dsi
->dsi_host
);
1156 DRM_DEV_ERROR(dev
, "Failed to register MIPI host: %d\n", ret
);
1160 attr
= soc_device_match(nwl_dsi_quirks_match
);
1162 dsi
->quirks
= (uintptr_t)attr
->data
;
1164 dsi
->bridge
.driver_private
= dsi
;
1165 dsi
->bridge
.funcs
= &nwl_dsi_bridge_funcs
;
1166 dsi
->bridge
.of_node
= dev
->of_node
;
1167 dsi
->bridge
.timings
= &nwl_dsi_timings
;
1169 dev_set_drvdata(dev
, dsi
);
1170 pm_runtime_enable(dev
);
1172 ret
= nwl_dsi_select_input(dsi
);
1174 mipi_dsi_host_unregister(&dsi
->dsi_host
);
1178 drm_bridge_add(&dsi
->bridge
);
1182 static int nwl_dsi_remove(struct platform_device
*pdev
)
1184 struct nwl_dsi
*dsi
= platform_get_drvdata(pdev
);
1186 nwl_dsi_deselect_input(dsi
);
1187 mipi_dsi_host_unregister(&dsi
->dsi_host
);
1188 drm_bridge_remove(&dsi
->bridge
);
1189 pm_runtime_disable(&pdev
->dev
);
1193 static struct platform_driver nwl_dsi_driver
= {
1194 .probe
= nwl_dsi_probe
,
1195 .remove
= nwl_dsi_remove
,
1197 .of_match_table
= nwl_dsi_dt_ids
,
1202 module_platform_driver(nwl_dsi_driver
);
1204 MODULE_AUTHOR("NXP Semiconductor");
1205 MODULE_AUTHOR("Purism SPC");
1206 MODULE_DESCRIPTION("Northwest Logic MIPI-DSI driver");
1207 MODULE_LICENSE("GPL"); /* GPLv2 or later */