2 * DesignWare MIPI DSI Host Controller v1.02 driver
4 * Copyright (c) 2016 Linaro Limited.
5 * Copyright (c) 2014-2016 Hisilicon Limited.
8 * Xinliang Liu <z.liuxinliang@hisilicon.com>
9 * Xinliang Liu <xinliang.liu@linaro.org>
10 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/clk.h>
19 #include <linux/component.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_crtc_helper.h>
23 #include <drm/drm_mipi_dsi.h>
24 #include <drm/drm_encoder_slave.h>
25 #include <drm/drm_atomic_helper.h>
27 #include "dw_dsi_reg.h"
29 #define MAX_TX_ESC_CLK 10
30 #define ROUND(x, y) ((x) / (y) + \
31 ((x) % (y) * 10 / (y) >= 5 ? 1 : 0))
32 #define PHY_REF_CLK_RATE 19200000
33 #define PHY_REF_CLK_PERIOD_PS (1000000000 / (PHY_REF_CLK_RATE / 1000))
35 #define encoder_to_dsi(encoder) \
36 container_of(encoder, struct dw_dsi, encoder)
37 #define host_to_dsi(host) \
38 container_of(host, struct dw_dsi, host)
40 struct mipi_phy_params
{
47 u32 data_t_hs_prepare
;
69 u32 clk_to_data_delay
;
70 u32 data_to_clk_delay
;
71 u32 lane_byte_clk_kHz
;
81 struct drm_encoder encoder
;
82 struct drm_bridge
*bridge
;
83 struct mipi_dsi_host host
;
84 struct drm_display_mode cur_mode
;
85 struct dsi_hw_ctx
*ctx
;
86 struct mipi_phy_params phy
;
89 enum mipi_dsi_pixel_format format
;
90 unsigned long mode_flags
;
96 struct dsi_hw_ctx ctx
;
99 struct dsi_phy_range
{
106 static const struct dsi_phy_range dphy_range_info
[] = {
107 { 46875, 62500, 1, 7 },
108 { 62500, 93750, 0, 7 },
109 { 93750, 125000, 1, 6 },
110 { 125000, 187500, 0, 6 },
111 { 187500, 250000, 1, 5 },
112 { 250000, 375000, 0, 5 },
113 { 375000, 500000, 1, 4 },
114 { 500000, 750000, 0, 4 },
115 { 750000, 1000000, 1, 0 },
116 { 1000000, 1500000, 0, 0 }
119 static u32
dsi_calc_phy_rate(u32 req_kHz
, struct mipi_phy_params
*phy
)
121 u32 ref_clk_ps
= PHY_REF_CLK_PERIOD_PS
;
122 u32 tmp_kHz
= req_kHz
;
134 * Find a rate >= req_kHz.
139 for (i
= 0; i
< ARRAY_SIZE(dphy_range_info
); i
++)
140 if (f_kHz
>= dphy_range_info
[i
].min_range_kHz
&&
141 f_kHz
<= dphy_range_info
[i
].max_range_kHz
)
144 if (i
== ARRAY_SIZE(dphy_range_info
)) {
145 DRM_ERROR("%dkHz out of range\n", f_kHz
);
149 phy
->pll_vco_750M
= dphy_range_info
[i
].pll_vco_750M
;
150 phy
->hstx_ckg_sel
= dphy_range_info
[i
].hstx_ckg_sel
;
152 if (phy
->hstx_ckg_sel
<= 7 &&
153 phy
->hstx_ckg_sel
>= 4)
154 q_pll
= 0x10 >> (7 - phy
->hstx_ckg_sel
);
156 temp
= f_kHz
* (u64
)q_pll
* (u64
)ref_clk_ps
;
157 m_n_int
= temp
/ (u64
)1000000000;
158 m_n
= (temp
% (u64
)1000000000) / (u64
)100000000;
160 if (m_n_int
% 2 == 0) {
163 m_pll
= (m_n_int
+ 1) * n_pll
;
164 } else if (m_n
* 6 >= 30) {
166 m_pll
= m_n_int
* n_pll
+ 2;
169 m_pll
= m_n_int
* n_pll
;
174 m_pll
= (m_n_int
+ 1) * n_pll
;
175 } else if (m_n
* 6 >= 30) {
177 m_pll
= (m_n_int
+ 1) * n_pll
;
178 } else if (m_n
* 6 >= 10) {
180 m_pll
= m_n_int
* n_pll
+ 1;
183 m_pll
= m_n_int
* n_pll
;
189 phy
->pll_pre_div1p
= 1;
191 phy
->pll_fbd_p
= n_pll
;
192 phy
->pll_pre_div1p
= 0;
195 if (phy
->pll_fbd_2p
<= 7 && phy
->pll_fbd_2p
>= 4)
196 r_pll
= 0x10 >> (7 - phy
->pll_fbd_2p
);
201 phy
->pll_fbd_div1f
= 0;
202 phy
->pll_fbd_div5f
= 1;
203 } else if (m_pll
>= 2 * 2 * r_pll
&& m_pll
<= 2 * 4 * r_pll
) {
204 phy
->pll_pre_p
= m_pll
/ (2 * r_pll
);
206 phy
->pll_fbd_div1f
= 1;
207 phy
->pll_fbd_div5f
= 0;
208 } else if (m_pll
>= 2 * 5 * r_pll
&& m_pll
<= 2 * 150 * r_pll
) {
209 if (((m_pll
/ (2 * r_pll
)) % 2) == 0) {
211 (m_pll
/ (2 * r_pll
)) / 2 - 1;
213 (m_pll
/ (2 * r_pll
)) % 2 + 2;
216 (m_pll
/ (2 * r_pll
)) / 2;
218 (m_pll
/ (2 * r_pll
)) % 2;
220 phy
->pll_fbd_div1f
= 0;
221 phy
->pll_fbd_div5f
= 0;
225 phy
->pll_fbd_div1f
= 0;
226 phy
->pll_fbd_div5f
= 1;
229 f_kHz
= (u64
)1000000000 * (u64
)m_pll
/
230 ((u64
)ref_clk_ps
* (u64
)n_pll
* (u64
)q_pll
);
232 if (f_kHz
>= req_kHz
)
242 static void dsi_get_phy_params(u32 phy_req_kHz
,
243 struct mipi_phy_params
*phy
)
245 u32 ref_clk_ps
= PHY_REF_CLK_PERIOD_PS
;
249 memset(phy
, 0, sizeof(*phy
));
251 phy_rate_kHz
= dsi_calc_phy_rate(phy_req_kHz
, phy
);
255 ui
= 1000000 / phy_rate_kHz
;
257 phy
->clk_t_lpx
= ROUND(50, 8 * ui
);
258 phy
->clk_t_hs_prepare
= ROUND(133, 16 * ui
) - 1;
260 phy
->clk_t_hs_zero
= ROUND(262, 8 * ui
);
261 phy
->clk_t_hs_trial
= 2 * (ROUND(60, 8 * ui
) - 1);
262 phy
->clk_t_wakeup
= ROUND(1000000, (ref_clk_ps
/ 1000) - 1);
263 if (phy
->clk_t_wakeup
> 0xff)
264 phy
->clk_t_wakeup
= 0xff;
265 phy
->data_t_wakeup
= phy
->clk_t_wakeup
;
266 phy
->data_t_lpx
= phy
->clk_t_lpx
;
267 phy
->data_t_hs_prepare
= ROUND(125 + 10 * ui
, 16 * ui
) - 1;
268 phy
->data_t_hs_zero
= ROUND(105 + 6 * ui
, 8 * ui
);
269 phy
->data_t_hs_trial
= 2 * (ROUND(60 + 4 * ui
, 8 * ui
) - 1);
270 phy
->data_t_ta_go
= 3;
271 phy
->data_t_ta_get
= 4;
274 phy
->clklp2hs_time
= ROUND(407, 8 * ui
) + 12;
275 phy
->clkhs2lp_time
= ROUND(105 + 12 * ui
, 8 * ui
);
276 phy
->lp2hs_time
= ROUND(240 + 12 * ui
, 8 * ui
) + 1;
277 phy
->hs2lp_time
= phy
->clkhs2lp_time
;
278 phy
->clk_to_data_delay
= 1 + phy
->clklp2hs_time
;
279 phy
->data_to_clk_delay
= ROUND(60 + 52 * ui
, 8 * ui
) +
282 phy
->lane_byte_clk_kHz
= phy_rate_kHz
/ 8;
284 DIV_ROUND_UP(phy
->lane_byte_clk_kHz
, MAX_TX_ESC_CLK
);
287 static u32
dsi_get_dpi_color_coding(enum mipi_dsi_pixel_format format
)
292 * TODO: only support RGB888 now, to support more
295 case MIPI_DSI_FMT_RGB888
:
307 * dsi phy reg write function
309 static void dsi_phy_tst_set(void __iomem
*base
, u32 reg
, u32 val
)
311 u32 reg_write
= 0x10000 + reg
;
316 writel(reg_write
, base
+ PHY_TST_CTRL1
);
317 writel(0x02, base
+ PHY_TST_CTRL0
);
318 writel(0x00, base
+ PHY_TST_CTRL0
);
323 writel(val
, base
+ PHY_TST_CTRL1
);
324 writel(0x02, base
+ PHY_TST_CTRL0
);
325 writel(0x00, base
+ PHY_TST_CTRL0
);
328 static void dsi_set_phy_timer(void __iomem
*base
,
329 struct mipi_phy_params
*phy
,
335 * Set lane value and phy stop wait time.
337 val
= (lanes
- 1) | (PHY_STOP_WAIT_TIME
<< 8);
338 writel(val
, base
+ PHY_IF_CFG
);
341 * Set phy clk division.
343 val
= readl(base
+ CLKMGR_CFG
) | phy
->clk_division
;
344 writel(val
, base
+ CLKMGR_CFG
);
347 * Set lp and hs switching params.
349 dw_update_bits(base
+ PHY_TMR_CFG
, 24, MASK(8), phy
->hs2lp_time
);
350 dw_update_bits(base
+ PHY_TMR_CFG
, 16, MASK(8), phy
->lp2hs_time
);
351 dw_update_bits(base
+ PHY_TMR_LPCLK_CFG
, 16, MASK(10),
353 dw_update_bits(base
+ PHY_TMR_LPCLK_CFG
, 0, MASK(10),
355 dw_update_bits(base
+ CLK_DATA_TMR_CFG
, 8, MASK(8),
356 phy
->data_to_clk_delay
);
357 dw_update_bits(base
+ CLK_DATA_TMR_CFG
, 0, MASK(8),
358 phy
->clk_to_data_delay
);
361 static void dsi_set_mipi_phy(void __iomem
*base
,
362 struct mipi_phy_params
*phy
,
369 /* phy timer setting */
370 dsi_set_phy_timer(base
, phy
, lanes
);
373 * Reset to clean up phy tst params.
375 writel(0, base
+ PHY_RSTZ
);
376 writel(0, base
+ PHY_TST_CTRL0
);
377 writel(1, base
+ PHY_TST_CTRL0
);
378 writel(0, base
+ PHY_TST_CTRL0
);
381 * Clock lane timing control setting: TLPX, THS-PREPARE,
382 * THS-ZERO, THS-TRAIL, TWAKEUP.
384 dsi_phy_tst_set(base
, CLK_TLPX
, phy
->clk_t_lpx
);
385 dsi_phy_tst_set(base
, CLK_THS_PREPARE
, phy
->clk_t_hs_prepare
);
386 dsi_phy_tst_set(base
, CLK_THS_ZERO
, phy
->clk_t_hs_zero
);
387 dsi_phy_tst_set(base
, CLK_THS_TRAIL
, phy
->clk_t_hs_trial
);
388 dsi_phy_tst_set(base
, CLK_TWAKEUP
, phy
->clk_t_wakeup
);
391 * Data lane timing control setting: TLPX, THS-PREPARE,
392 * THS-ZERO, THS-TRAIL, TTA-GO, TTA-GET, TWAKEUP.
394 for (i
= 0; i
< lanes
; i
++) {
395 dsi_phy_tst_set(base
, DATA_TLPX(i
), phy
->data_t_lpx
);
396 dsi_phy_tst_set(base
, DATA_THS_PREPARE(i
),
397 phy
->data_t_hs_prepare
);
398 dsi_phy_tst_set(base
, DATA_THS_ZERO(i
), phy
->data_t_hs_zero
);
399 dsi_phy_tst_set(base
, DATA_THS_TRAIL(i
), phy
->data_t_hs_trial
);
400 dsi_phy_tst_set(base
, DATA_TTA_GO(i
), phy
->data_t_ta_go
);
401 dsi_phy_tst_set(base
, DATA_TTA_GET(i
), phy
->data_t_ta_get
);
402 dsi_phy_tst_set(base
, DATA_TWAKEUP(i
), phy
->data_t_wakeup
);
406 * physical configuration: I, pll I, pll II, pll III,
409 dsi_phy_tst_set(base
, PHY_CFG_I
, phy
->hstx_ckg_sel
);
410 val
= (phy
->pll_fbd_div5f
<< 5) + (phy
->pll_fbd_div1f
<< 4) +
411 (phy
->pll_fbd_2p
<< 1) + phy
->pll_enbwt
;
412 dsi_phy_tst_set(base
, PHY_CFG_PLL_I
, val
);
413 dsi_phy_tst_set(base
, PHY_CFG_PLL_II
, phy
->pll_fbd_p
);
414 dsi_phy_tst_set(base
, PHY_CFG_PLL_III
, phy
->pll_fbd_s
);
415 val
= (phy
->pll_pre_div1p
<< 7) + phy
->pll_pre_p
;
416 dsi_phy_tst_set(base
, PHY_CFG_PLL_IV
, val
);
417 val
= (5 << 5) + (phy
->pll_vco_750M
<< 4) + (phy
->pll_lpf_rs
<< 2) +
419 dsi_phy_tst_set(base
, PHY_CFG_PLL_V
, val
);
421 writel(PHY_ENABLECLK
, base
+ PHY_RSTZ
);
423 writel(PHY_ENABLECLK
| PHY_UNSHUTDOWNZ
, base
+ PHY_RSTZ
);
425 writel(PHY_ENABLECLK
| PHY_UNRSTZ
| PHY_UNSHUTDOWNZ
, base
+ PHY_RSTZ
);
426 usleep_range(1000, 1500);
429 * wait for phy's clock ready
432 while (delay_count
) {
433 val
= readl(base
+ PHY_STATUS
);
434 if ((BIT(0) | BIT(2)) & val
)
442 DRM_INFO("phylock and phystopstateclklane is not ready.\n");
445 static void dsi_set_mode_timing(void __iomem
*base
,
446 u32 lane_byte_clk_kHz
,
447 struct drm_display_mode
*mode
,
448 enum mipi_dsi_pixel_format format
)
450 u32 hfp
, hbp
, hsw
, vfp
, vbp
, vsw
;
459 val
= dsi_get_dpi_color_coding(format
);
460 writel(val
, base
+ DPI_COLOR_CODING
);
462 val
= (mode
->flags
& DRM_MODE_FLAG_NHSYNC
? 1 : 0) << 2;
463 val
|= (mode
->flags
& DRM_MODE_FLAG_NVSYNC
? 1 : 0) << 1;
464 writel(val
, base
+ DPI_CFG_POL
);
467 * The DSI IP accepts vertical timing using lines as normal,
468 * but horizontal timing is a mixture of pixel-clocks for the
469 * active region and byte-lane clocks for the blanking-related
470 * timings. hfp is specified as the total hline_time in byte-
471 * lane clocks minus hsa, hbp and active.
473 pixel_clk_kHz
= mode
->clock
;
476 hfp
= mode
->hsync_start
- mode
->hdisplay
;
477 hbp
= mode
->htotal
- mode
->hsync_end
;
478 hsw
= mode
->hsync_end
- mode
->hsync_start
;
479 vfp
= mode
->vsync_start
- mode
->vdisplay
;
480 vbp
= mode
->vtotal
- mode
->vsync_end
;
481 vsw
= mode
->vsync_end
- mode
->vsync_start
;
483 DRM_DEBUG_DRIVER("vsw exceeded 15\n");
487 hsa_time
= (hsw
* lane_byte_clk_kHz
) / pixel_clk_kHz
;
488 hbp_time
= (hbp
* lane_byte_clk_kHz
) / pixel_clk_kHz
;
489 tmp
= (u64
)htot
* (u64
)lane_byte_clk_kHz
;
490 hline_time
= DIV_ROUND_UP(tmp
, pixel_clk_kHz
);
492 /* all specified in byte-lane clocks */
493 writel(hsa_time
, base
+ VID_HSA_TIME
);
494 writel(hbp_time
, base
+ VID_HBP_TIME
);
495 writel(hline_time
, base
+ VID_HLINE_TIME
);
497 writel(vsw
, base
+ VID_VSA_LINES
);
498 writel(vbp
, base
+ VID_VBP_LINES
);
499 writel(vfp
, base
+ VID_VFP_LINES
);
500 writel(mode
->vdisplay
, base
+ VID_VACTIVE_LINES
);
501 writel(mode
->hdisplay
, base
+ VID_PKT_SIZE
);
503 DRM_DEBUG_DRIVER("htot=%d, hfp=%d, hbp=%d, hsw=%d\n",
504 htot
, hfp
, hbp
, hsw
);
505 DRM_DEBUG_DRIVER("vtol=%d, vfp=%d, vbp=%d, vsw=%d\n",
506 vtot
, vfp
, vbp
, vsw
);
507 DRM_DEBUG_DRIVER("hsa_time=%d, hbp_time=%d, hline_time=%d\n",
508 hsa_time
, hbp_time
, hline_time
);
511 static void dsi_set_video_mode(void __iomem
*base
, unsigned long flags
)
514 u32 mode_mask
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_VIDEO_BURST
|
515 MIPI_DSI_MODE_VIDEO_SYNC_PULSE
;
516 u32 non_burst_sync_pulse
= MIPI_DSI_MODE_VIDEO
|
517 MIPI_DSI_MODE_VIDEO_SYNC_PULSE
;
518 u32 non_burst_sync_event
= MIPI_DSI_MODE_VIDEO
;
521 * choose video mode type
523 if ((flags
& mode_mask
) == non_burst_sync_pulse
)
524 val
= DSI_NON_BURST_SYNC_PULSES
;
525 else if ((flags
& mode_mask
) == non_burst_sync_event
)
526 val
= DSI_NON_BURST_SYNC_EVENTS
;
528 val
= DSI_BURST_SYNC_PULSES_1
;
529 writel(val
, base
+ VID_MODE_CFG
);
531 writel(PHY_TXREQUESTCLKHS
, base
+ LPCLK_CTRL
);
532 writel(DSI_VIDEO_MODE
, base
+ MODE_CFG
);
535 static void dsi_mipi_init(struct dw_dsi
*dsi
)
537 struct dsi_hw_ctx
*ctx
= dsi
->ctx
;
538 struct mipi_phy_params
*phy
= &dsi
->phy
;
539 struct drm_display_mode
*mode
= &dsi
->cur_mode
;
540 u32 bpp
= mipi_dsi_pixel_format_to_bpp(dsi
->format
);
541 void __iomem
*base
= ctx
->base
;
547 dphy_req_kHz
= mode
->clock
* bpp
/ dsi
->lanes
;
548 dsi_get_phy_params(dphy_req_kHz
, phy
);
551 writel(RESET
, base
+ PWR_UP
);
553 /* set dsi phy params */
554 dsi_set_mipi_phy(base
, phy
, dsi
->lanes
);
556 /* set dsi mode timing */
557 dsi_set_mode_timing(base
, phy
->lane_byte_clk_kHz
, mode
, dsi
->format
);
559 /* set dsi video mode */
560 dsi_set_video_mode(base
, dsi
->mode_flags
);
563 writel(POWERUP
, base
+ PWR_UP
);
565 DRM_DEBUG_DRIVER("lanes=%d, pixel_clk=%d kHz, bytes_freq=%d kHz\n",
566 dsi
->lanes
, mode
->clock
, phy
->lane_byte_clk_kHz
);
569 static void dsi_encoder_disable(struct drm_encoder
*encoder
)
571 struct dw_dsi
*dsi
= encoder_to_dsi(encoder
);
572 struct dsi_hw_ctx
*ctx
= dsi
->ctx
;
573 void __iomem
*base
= ctx
->base
;
578 writel(0, base
+ PWR_UP
);
579 writel(0, base
+ LPCLK_CTRL
);
580 writel(0, base
+ PHY_RSTZ
);
581 clk_disable_unprepare(ctx
->pclk
);
586 static void dsi_encoder_enable(struct drm_encoder
*encoder
)
588 struct dw_dsi
*dsi
= encoder_to_dsi(encoder
);
589 struct dsi_hw_ctx
*ctx
= dsi
->ctx
;
595 ret
= clk_prepare_enable(ctx
->pclk
);
597 DRM_ERROR("fail to enable pclk: %d\n", ret
);
606 static enum drm_mode_status
dsi_encoder_phy_mode_valid(
607 struct drm_encoder
*encoder
,
608 const struct drm_display_mode
*mode
)
610 struct dw_dsi
*dsi
= encoder_to_dsi(encoder
);
611 struct mipi_phy_params phy
;
612 u32 bpp
= mipi_dsi_pixel_format_to_bpp(dsi
->format
);
613 u32 req_kHz
, act_kHz
, lane_byte_clk_kHz
;
615 /* Calculate the lane byte clk using the adjusted mode clk */
616 memset(&phy
, 0, sizeof(phy
));
617 req_kHz
= mode
->clock
* bpp
/ dsi
->lanes
;
618 act_kHz
= dsi_calc_phy_rate(req_kHz
, &phy
);
619 lane_byte_clk_kHz
= act_kHz
/ 8;
621 DRM_DEBUG_DRIVER("Checking mode %ix%i-%i@%i clock: %i...",
622 mode
->hdisplay
, mode
->vdisplay
, bpp
,
623 drm_mode_vrefresh(mode
), mode
->clock
);
626 * Make sure the adjusted mode clock and the lane byte clk
627 * have a common denominator base frequency
629 if (mode
->clock
/dsi
->lanes
== lane_byte_clk_kHz
/3) {
630 DRM_DEBUG_DRIVER("OK!\n");
634 DRM_DEBUG_DRIVER("BAD!\n");
638 static enum drm_mode_status
dsi_encoder_mode_valid(struct drm_encoder
*encoder
,
639 const struct drm_display_mode
*mode
)
642 const struct drm_crtc_helper_funcs
*crtc_funcs
= NULL
;
643 struct drm_crtc
*crtc
= NULL
;
644 struct drm_display_mode adj_mode
;
645 enum drm_mode_status ret
;
648 * The crtc might adjust the mode, so go through the
649 * possible crtcs (technically just one) and call
650 * mode_fixup to figure out the adjusted mode before we
653 drm_for_each_crtc(crtc
, encoder
->dev
) {
655 * reset adj_mode to the mode value each time,
656 * so we don't adjust the mode twice
658 drm_mode_copy(&adj_mode
, mode
);
660 crtc_funcs
= crtc
->helper_private
;
661 if (crtc_funcs
&& crtc_funcs
->mode_fixup
)
662 if (!crtc_funcs
->mode_fixup(crtc
, mode
, &adj_mode
))
665 ret
= dsi_encoder_phy_mode_valid(encoder
, &adj_mode
);
672 static void dsi_encoder_mode_set(struct drm_encoder
*encoder
,
673 struct drm_display_mode
*mode
,
674 struct drm_display_mode
*adj_mode
)
676 struct dw_dsi
*dsi
= encoder_to_dsi(encoder
);
678 drm_mode_copy(&dsi
->cur_mode
, adj_mode
);
681 static int dsi_encoder_atomic_check(struct drm_encoder
*encoder
,
682 struct drm_crtc_state
*crtc_state
,
683 struct drm_connector_state
*conn_state
)
689 static const struct drm_encoder_helper_funcs dw_encoder_helper_funcs
= {
690 .atomic_check
= dsi_encoder_atomic_check
,
691 .mode_valid
= dsi_encoder_mode_valid
,
692 .mode_set
= dsi_encoder_mode_set
,
693 .enable
= dsi_encoder_enable
,
694 .disable
= dsi_encoder_disable
697 static const struct drm_encoder_funcs dw_encoder_funcs
= {
698 .destroy
= drm_encoder_cleanup
,
701 static int dw_drm_encoder_init(struct device
*dev
,
702 struct drm_device
*drm_dev
,
703 struct drm_encoder
*encoder
)
706 u32 crtc_mask
= drm_of_find_possible_crtcs(drm_dev
, dev
->of_node
);
709 DRM_ERROR("failed to find crtc mask\n");
713 encoder
->possible_crtcs
= crtc_mask
;
714 ret
= drm_encoder_init(drm_dev
, encoder
, &dw_encoder_funcs
,
715 DRM_MODE_ENCODER_DSI
, NULL
);
717 DRM_ERROR("failed to init dsi encoder\n");
721 drm_encoder_helper_add(encoder
, &dw_encoder_helper_funcs
);
726 static int dsi_host_attach(struct mipi_dsi_host
*host
,
727 struct mipi_dsi_device
*mdsi
)
729 struct dw_dsi
*dsi
= host_to_dsi(host
);
731 if (mdsi
->lanes
< 1 || mdsi
->lanes
> 4) {
732 DRM_ERROR("dsi device params invalid\n");
736 dsi
->lanes
= mdsi
->lanes
;
737 dsi
->format
= mdsi
->format
;
738 dsi
->mode_flags
= mdsi
->mode_flags
;
743 static int dsi_host_detach(struct mipi_dsi_host
*host
,
744 struct mipi_dsi_device
*mdsi
)
750 static const struct mipi_dsi_host_ops dsi_host_ops
= {
751 .attach
= dsi_host_attach
,
752 .detach
= dsi_host_detach
,
755 static int dsi_host_init(struct device
*dev
, struct dw_dsi
*dsi
)
757 struct mipi_dsi_host
*host
= &dsi
->host
;
761 host
->ops
= &dsi_host_ops
;
762 ret
= mipi_dsi_host_register(host
);
764 DRM_ERROR("failed to register dsi host\n");
771 static int dsi_bridge_init(struct drm_device
*dev
, struct dw_dsi
*dsi
)
773 struct drm_encoder
*encoder
= &dsi
->encoder
;
774 struct drm_bridge
*bridge
= dsi
->bridge
;
777 /* associate the bridge to dsi encoder */
778 ret
= drm_bridge_attach(encoder
, bridge
, NULL
);
780 DRM_ERROR("failed to attach external bridge\n");
787 static int dsi_bind(struct device
*dev
, struct device
*master
, void *data
)
789 struct dsi_data
*ddata
= dev_get_drvdata(dev
);
790 struct dw_dsi
*dsi
= &ddata
->dsi
;
791 struct drm_device
*drm_dev
= data
;
794 ret
= dw_drm_encoder_init(dev
, drm_dev
, &dsi
->encoder
);
798 ret
= dsi_host_init(dev
, dsi
);
802 ret
= dsi_bridge_init(drm_dev
, dsi
);
809 static void dsi_unbind(struct device
*dev
, struct device
*master
, void *data
)
814 static const struct component_ops dsi_ops
= {
816 .unbind
= dsi_unbind
,
819 static int dsi_parse_dt(struct platform_device
*pdev
, struct dw_dsi
*dsi
)
821 struct dsi_hw_ctx
*ctx
= dsi
->ctx
;
822 struct device_node
*np
= pdev
->dev
.of_node
;
823 struct resource
*res
;
827 * Get the endpoint node. In our case, dsi has one output port1
828 * to which the external HDMI bridge is connected.
830 ret
= drm_of_find_panel_or_bridge(np
, 1, 0, NULL
, &dsi
->bridge
);
834 ctx
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
835 if (IS_ERR(ctx
->pclk
)) {
836 DRM_ERROR("failed to get pclk clock\n");
837 return PTR_ERR(ctx
->pclk
);
840 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
841 ctx
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
842 if (IS_ERR(ctx
->base
)) {
843 DRM_ERROR("failed to remap dsi io region\n");
844 return PTR_ERR(ctx
->base
);
850 static int dsi_probe(struct platform_device
*pdev
)
852 struct dsi_data
*data
;
854 struct dsi_hw_ctx
*ctx
;
857 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
859 DRM_ERROR("failed to allocate dsi data.\n");
866 ret
= dsi_parse_dt(pdev
, dsi
);
870 platform_set_drvdata(pdev
, data
);
872 return component_add(&pdev
->dev
, &dsi_ops
);
875 static int dsi_remove(struct platform_device
*pdev
)
877 component_del(&pdev
->dev
, &dsi_ops
);
882 static const struct of_device_id dsi_of_match
[] = {
883 {.compatible
= "hisilicon,hi6220-dsi"},
886 MODULE_DEVICE_TABLE(of
, dsi_of_match
);
888 static struct platform_driver dsi_driver
= {
890 .remove
= dsi_remove
,
893 .of_match_table
= dsi_of_match
,
897 module_platform_driver(dsi_driver
);
899 MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
900 MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
901 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
902 MODULE_DESCRIPTION("DesignWare MIPI DSI Host Controller v1.02 driver");
903 MODULE_LICENSE("GPL v2");