2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Jackie Li<yaodong.li@intel.com>
27 #include <linux/freezer.h>
28 #include <video/mipi_display.h>
30 #include "mdfld_dsi_output.h"
31 #include "mdfld_dsi_pkg_sender.h"
32 #include "mdfld_dsi_dpi.h"
34 #define MDFLD_DSI_READ_MAX_COUNT 5000
37 MDFLD_DSI_PANEL_MODE_SLEEP
= 0x1,
41 MDFLD_DSI_PKG_SENDER_FREE
= 0x0,
42 MDFLD_DSI_PKG_SENDER_BUSY
= 0x1,
45 static const char *const dsi_errors
[] = {
49 "RX Escape Mode Entry Error",
50 "RX LP TX Sync Error",
51 "RX HS Receive Timeout Error",
52 "RX False Control Error",
53 "RX ECC Single Bit Error",
54 "RX ECC Multibit Error",
56 "RX DSI Data Type Not Recognised",
57 "RX DSI VC ID Invalid",
58 "TX False Control Error",
59 "TX ECC Single Bit Error",
60 "TX ECC Multibit Error",
62 "TX DSI Data Type Not Recognised",
63 "TX DSI VC ID invalid",
69 "Turn Around ACK Timeout",
71 "RX Invalid TX Length",
73 "HS Generic Write FIFO Full",
74 "LP Generic Write FIFO Full",
75 "Generic Read Data Avail"
76 "Special Packet Sent",
80 static inline int wait_for_gen_fifo_empty(struct mdfld_dsi_pkg_sender
*sender
,
83 struct drm_device
*dev
= sender
->dev
;
84 u32 gen_fifo_stat_reg
= sender
->mipi_gen_fifo_stat_reg
;
88 if ((mask
& REG_READ(gen_fifo_stat_reg
)) == mask
)
92 DRM_ERROR("fifo is NOT empty 0x%08x\n", REG_READ(gen_fifo_stat_reg
));
96 static int wait_for_all_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
98 return wait_for_gen_fifo_empty(sender
, (BIT(2) | BIT(10) | BIT(18) |
99 BIT(26) | BIT(27) | BIT(28)));
102 static int wait_for_lp_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
104 return wait_for_gen_fifo_empty(sender
, (BIT(10) | BIT(26)));
107 static int wait_for_hs_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
109 return wait_for_gen_fifo_empty(sender
, (BIT(2) | BIT(18)));
112 static int handle_dsi_error(struct mdfld_dsi_pkg_sender
*sender
, u32 mask
)
114 u32 intr_stat_reg
= sender
->mipi_intr_stat_reg
;
115 struct drm_device
*dev
= sender
->dev
;
117 dev_dbg(sender
->dev
->dev
, "Handling error 0x%08x\n", mask
);
134 dev_dbg(sender
->dev
->dev
, "No Action required\n");
137 /*wait for all fifo empty*/
138 /*wait_for_all_fifos_empty(sender)*/
141 dev_dbg(sender
->dev
->dev
, "No Action required\n");
149 dev_dbg(sender
->dev
->dev
, "High/Low contention detected\n");
150 /*wait for contention recovery time*/
152 /*wait for all fifo empty*/
154 wait_for_all_fifos_empty(sender
);
157 dev_dbg(sender
->dev
->dev
, "No Action required\n");
160 /*wait for all fifo empty*/
161 /*wait_for_all_fifos_empty(sender);*/
170 dev_dbg(sender
->dev
->dev
, "HS Gen fifo full\n");
171 REG_WRITE(intr_stat_reg
, mask
);
172 wait_for_hs_fifos_empty(sender
);
175 dev_dbg(sender
->dev
->dev
, "LP Gen fifo full\n");
176 REG_WRITE(intr_stat_reg
, mask
);
177 wait_for_lp_fifos_empty(sender
);
182 dev_dbg(sender
->dev
->dev
, "No Action required\n");
186 if (mask
& REG_READ(intr_stat_reg
))
187 dev_dbg(sender
->dev
->dev
,
188 "Cannot clean interrupt 0x%08x\n", mask
);
192 static int dsi_error_handler(struct mdfld_dsi_pkg_sender
*sender
)
194 struct drm_device
*dev
= sender
->dev
;
195 u32 intr_stat_reg
= sender
->mipi_intr_stat_reg
;
201 intr_stat
= REG_READ(intr_stat_reg
);
203 for (i
= 0; i
< 32; i
++) {
204 mask
= (0x00000001UL
) << i
;
205 if (intr_stat
& mask
) {
206 dev_dbg(sender
->dev
->dev
, "[DSI]: %s\n", dsi_errors
[i
]);
207 err
= handle_dsi_error(sender
, mask
);
209 DRM_ERROR("Cannot handle error\n");
215 static int send_short_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
216 u8 cmd
, u8 param
, bool hs
)
218 struct drm_device
*dev
= sender
->dev
;
221 u8 virtual_channel
= 0;
224 ctrl_reg
= sender
->mipi_hs_gen_ctrl_reg
;
226 /* FIXME: wait_for_hs_fifos_empty(sender); */
228 ctrl_reg
= sender
->mipi_lp_gen_ctrl_reg
;
230 /* FIXME: wait_for_lp_fifos_empty(sender); */
233 val
= FLD_VAL(param
, 23, 16) | FLD_VAL(cmd
, 15, 8) |
234 FLD_VAL(virtual_channel
, 7, 6) | FLD_VAL(data_type
, 5, 0);
236 REG_WRITE(ctrl_reg
, val
);
241 static int send_long_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
242 u8
*data
, int len
, bool hs
)
244 struct drm_device
*dev
= sender
->dev
;
250 u8 virtual_channel
= 0;
254 ctrl_reg
= sender
->mipi_hs_gen_ctrl_reg
;
255 data_reg
= sender
->mipi_hs_gen_data_reg
;
257 /* FIXME: wait_for_hs_fifos_empty(sender); */
259 ctrl_reg
= sender
->mipi_lp_gen_ctrl_reg
;
260 data_reg
= sender
->mipi_lp_gen_data_reg
;
262 /* FIXME: wait_for_lp_fifos_empty(sender); */
266 for (i
= 0; i
< len
/ 4; i
++) {
272 REG_WRITE(data_reg
, b4
<< 24 | b3
<< 16 | b2
<< 8 | b1
);
277 b1
= 0; b2
= 0; b3
= 0;
294 REG_WRITE(data_reg
, b3
<< 16 | b2
<< 8 | b1
);
297 val
= FLD_VAL(len
, 23, 8) | FLD_VAL(virtual_channel
, 7, 6) |
298 FLD_VAL(data_type
, 5, 0);
300 REG_WRITE(ctrl_reg
, val
);
305 static int send_pkg_prepare(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
311 case MIPI_DSI_DCS_SHORT_WRITE
:
312 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
313 case MIPI_DSI_DCS_LONG_WRITE
:
320 /*this prevents other package sending while doing msleep*/
321 sender
->status
= MDFLD_DSI_PKG_SENDER_BUSY
;
323 /*wait for 120 milliseconds in case exit_sleep_mode just be sent*/
324 if (unlikely(cmd
== MIPI_DCS_ENTER_SLEEP_MODE
)) {
325 /*TODO: replace it with msleep later*/
329 if (unlikely(cmd
== MIPI_DCS_EXIT_SLEEP_MODE
)) {
330 /*TODO: replace it with msleep later*/
336 static int send_pkg_done(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
342 case MIPI_DSI_DCS_SHORT_WRITE
:
343 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
344 case MIPI_DSI_DCS_LONG_WRITE
:
351 /*update panel status*/
352 if (unlikely(cmd
== MIPI_DCS_ENTER_SLEEP_MODE
)) {
353 sender
->panel_mode
|= MDFLD_DSI_PANEL_MODE_SLEEP
;
354 /*TODO: replace it with msleep later*/
356 } else if (unlikely(cmd
== MIPI_DCS_EXIT_SLEEP_MODE
)) {
357 sender
->panel_mode
&= ~MDFLD_DSI_PANEL_MODE_SLEEP
;
358 /*TODO: replace it with msleep later*/
360 } else if (unlikely(cmd
== MIPI_DCS_SOFT_RESET
)) {
361 /*TODO: replace it with msleep later*/
365 sender
->status
= MDFLD_DSI_PKG_SENDER_FREE
;
370 static int send_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
371 u8
*data
, u16 len
, bool hs
)
376 ret
= dsi_error_handler(sender
);
378 DRM_ERROR("Error handling failed\n");
383 if (sender
->status
== MDFLD_DSI_PKG_SENDER_BUSY
) {
384 DRM_ERROR("sender is busy\n");
388 ret
= send_pkg_prepare(sender
, data_type
, data
, len
);
390 DRM_ERROR("send_pkg_prepare error\n");
395 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM
:
396 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM
:
397 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM
:
398 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM
:
399 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM
:
400 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM
:
401 case MIPI_DSI_DCS_SHORT_WRITE
:
402 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
403 case MIPI_DSI_DCS_READ
:
404 ret
= send_short_pkg(sender
, data_type
, data
[0], data
[1], hs
);
406 case MIPI_DSI_GENERIC_LONG_WRITE
:
407 case MIPI_DSI_DCS_LONG_WRITE
:
408 ret
= send_long_pkg(sender
, data_type
, data
, len
, hs
);
412 send_pkg_done(sender
, data_type
, data
, len
);
414 /*FIXME: should I query complete and fifo empty here?*/
419 int mdfld_dsi_send_mcs_long(struct mdfld_dsi_pkg_sender
*sender
, u8
*data
,
424 if (!sender
|| !data
|| !len
) {
425 DRM_ERROR("Invalid parameters\n");
429 spin_lock_irqsave(&sender
->lock
, flags
);
430 send_pkg(sender
, MIPI_DSI_DCS_LONG_WRITE
, data
, len
, hs
);
431 spin_unlock_irqrestore(&sender
->lock
, flags
);
436 int mdfld_dsi_send_mcs_short(struct mdfld_dsi_pkg_sender
*sender
, u8 cmd
,
437 u8 param
, u8 param_num
, bool hs
)
444 DRM_ERROR("Invalid parameter\n");
451 data_type
= MIPI_DSI_DCS_SHORT_WRITE_PARAM
;
454 data_type
= MIPI_DSI_DCS_SHORT_WRITE
;
458 spin_lock_irqsave(&sender
->lock
, flags
);
459 send_pkg(sender
, data_type
, data
, sizeof(data
), hs
);
460 spin_unlock_irqrestore(&sender
->lock
, flags
);
465 int mdfld_dsi_send_gen_short(struct mdfld_dsi_pkg_sender
*sender
, u8 param0
,
466 u8 param1
, u8 param_num
, bool hs
)
472 if (!sender
|| param_num
> 2) {
473 DRM_ERROR("Invalid parameter\n");
479 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM
;
484 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM
;
489 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM
;
495 spin_lock_irqsave(&sender
->lock
, flags
);
496 send_pkg(sender
, data_type
, data
, sizeof(data
), hs
);
497 spin_unlock_irqrestore(&sender
->lock
, flags
);
502 int mdfld_dsi_send_gen_long(struct mdfld_dsi_pkg_sender
*sender
, u8
*data
,
507 if (!sender
|| !data
|| !len
) {
508 DRM_ERROR("Invalid parameters\n");
512 spin_lock_irqsave(&sender
->lock
, flags
);
513 send_pkg(sender
, MIPI_DSI_GENERIC_LONG_WRITE
, data
, len
, hs
);
514 spin_unlock_irqrestore(&sender
->lock
, flags
);
519 static int __read_panel_data(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
520 u8
*data
, u16 len
, u32
*data_out
, u16 len_out
, bool hs
)
523 struct drm_device
*dev
= sender
->dev
;
526 int retry
= MDFLD_DSI_READ_MAX_COUNT
;
528 if (!sender
|| !data_out
|| !len_out
) {
529 DRM_ERROR("Invalid parameters\n");
535 * 0) send out generic read request
536 * 1) polling read data avail interrupt
539 spin_lock_irqsave(&sender
->lock
, flags
);
541 REG_WRITE(sender
->mipi_intr_stat_reg
, BIT(29));
543 if ((REG_READ(sender
->mipi_intr_stat_reg
) & BIT(29)))
544 DRM_ERROR("Can NOT clean read data valid interrupt\n");
546 /*send out read request*/
547 send_pkg(sender
, data_type
, data
, len
, hs
);
549 /*polling read data avail interrupt*/
550 while (retry
&& !(REG_READ(sender
->mipi_intr_stat_reg
) & BIT(29))) {
556 spin_unlock_irqrestore(&sender
->lock
, flags
);
560 REG_WRITE(sender
->mipi_intr_stat_reg
, BIT(29));
564 gen_data_reg
= sender
->mipi_hs_gen_data_reg
;
566 gen_data_reg
= sender
->mipi_lp_gen_data_reg
;
568 for (i
= 0; i
< len_out
; i
++)
569 *(data_out
+ i
) = REG_READ(gen_data_reg
);
571 spin_unlock_irqrestore(&sender
->lock
, flags
);
576 int mdfld_dsi_read_mcs(struct mdfld_dsi_pkg_sender
*sender
, u8 cmd
,
577 u32
*data
, u16 len
, bool hs
)
579 if (!sender
|| !data
|| !len
) {
580 DRM_ERROR("Invalid parameters\n");
584 return __read_panel_data(sender
, MIPI_DSI_DCS_READ
, &cmd
, 1,
588 int mdfld_dsi_pkg_sender_init(struct mdfld_dsi_connector
*dsi_connector
,
591 struct mdfld_dsi_pkg_sender
*pkg_sender
;
592 struct mdfld_dsi_config
*dsi_config
=
593 mdfld_dsi_get_config(dsi_connector
);
594 struct drm_device
*dev
= dsi_config
->dev
;
595 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
596 const struct psb_offset
*map
= &dev_priv
->regmap
[pipe
];
599 if (!dsi_connector
) {
600 DRM_ERROR("Invalid parameter\n");
604 pkg_sender
= dsi_connector
->pkg_sender
;
606 if (!pkg_sender
|| IS_ERR(pkg_sender
)) {
607 pkg_sender
= kzalloc(sizeof(struct mdfld_dsi_pkg_sender
),
610 DRM_ERROR("Create DSI pkg sender failed\n");
613 dsi_connector
->pkg_sender
= (void *)pkg_sender
;
616 pkg_sender
->dev
= dev
;
617 pkg_sender
->dsi_connector
= dsi_connector
;
618 pkg_sender
->pipe
= pipe
;
619 pkg_sender
->pkg_num
= 0;
620 pkg_sender
->panel_mode
= 0;
621 pkg_sender
->status
= MDFLD_DSI_PKG_SENDER_FREE
;
624 /* FIXME: should just copy the regmap ptr ? */
625 pkg_sender
->dpll_reg
= map
->dpll
;
626 pkg_sender
->dspcntr_reg
= map
->cntr
;
627 pkg_sender
->pipeconf_reg
= map
->conf
;
628 pkg_sender
->dsplinoff_reg
= map
->linoff
;
629 pkg_sender
->dspsurf_reg
= map
->surf
;
630 pkg_sender
->pipestat_reg
= map
->status
;
632 pkg_sender
->mipi_intr_stat_reg
= MIPI_INTR_STAT_REG(pipe
);
633 pkg_sender
->mipi_lp_gen_data_reg
= MIPI_LP_GEN_DATA_REG(pipe
);
634 pkg_sender
->mipi_hs_gen_data_reg
= MIPI_HS_GEN_DATA_REG(pipe
);
635 pkg_sender
->mipi_lp_gen_ctrl_reg
= MIPI_LP_GEN_CTRL_REG(pipe
);
636 pkg_sender
->mipi_hs_gen_ctrl_reg
= MIPI_HS_GEN_CTRL_REG(pipe
);
637 pkg_sender
->mipi_gen_fifo_stat_reg
= MIPI_GEN_FIFO_STAT_REG(pipe
);
638 pkg_sender
->mipi_data_addr_reg
= MIPI_DATA_ADD_REG(pipe
);
639 pkg_sender
->mipi_data_len_reg
= MIPI_DATA_LEN_REG(pipe
);
640 pkg_sender
->mipi_cmd_addr_reg
= MIPI_CMD_ADD_REG(pipe
);
641 pkg_sender
->mipi_cmd_len_reg
= MIPI_CMD_LEN_REG(pipe
);
644 spin_lock_init(&pkg_sender
->lock
);
646 if (mdfld_get_panel_type(dev
, pipe
) != TC35876X
) {
648 * For video mode, don't enable DPI timing output here,
649 * will init the DPI timing output during mode setting.
651 mipi_val
= PASS_FROM_SPHY_TO_AFE
| SEL_FLOPPED_HSTX
;
656 REG_WRITE(MIPI_PORT_CONTROL(pipe
), mipi_val
);
657 REG_READ(MIPI_PORT_CONTROL(pipe
));
659 /* do dsi controller init */
660 mdfld_dsi_controller_init(dsi_config
, pipe
);
666 void mdfld_dsi_pkg_sender_destroy(struct mdfld_dsi_pkg_sender
*sender
)
668 if (!sender
|| IS_ERR(sender
))