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/delay.h>
28 #include <linux/freezer.h>
30 #include <video/mipi_display.h>
32 #include "mdfld_dsi_dpi.h"
33 #include "mdfld_dsi_output.h"
34 #include "mdfld_dsi_pkg_sender.h"
36 #define MDFLD_DSI_READ_MAX_COUNT 5000
39 MDFLD_DSI_PANEL_MODE_SLEEP
= 0x1,
43 MDFLD_DSI_PKG_SENDER_FREE
= 0x0,
44 MDFLD_DSI_PKG_SENDER_BUSY
= 0x1,
47 static const char *const dsi_errors
[] = {
51 "RX Escape Mode Entry Error",
52 "RX LP TX Sync Error",
53 "RX HS Receive Timeout Error",
54 "RX False Control Error",
55 "RX ECC Single Bit Error",
56 "RX ECC Multibit Error",
58 "RX DSI Data Type Not Recognised",
59 "RX DSI VC ID Invalid",
60 "TX False Control Error",
61 "TX ECC Single Bit Error",
62 "TX ECC Multibit Error",
64 "TX DSI Data Type Not Recognised",
65 "TX DSI VC ID invalid",
71 "Turn Around ACK Timeout",
73 "RX Invalid TX Length",
75 "HS Generic Write FIFO Full",
76 "LP Generic Write FIFO Full",
77 "Generic Read Data Avail",
78 "Special Packet Sent",
82 static inline int wait_for_gen_fifo_empty(struct mdfld_dsi_pkg_sender
*sender
,
85 struct drm_device
*dev
= sender
->dev
;
86 u32 gen_fifo_stat_reg
= sender
->mipi_gen_fifo_stat_reg
;
90 if ((mask
& REG_READ(gen_fifo_stat_reg
)) == mask
)
94 DRM_ERROR("fifo is NOT empty 0x%08x\n", REG_READ(gen_fifo_stat_reg
));
98 static int wait_for_all_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
100 return wait_for_gen_fifo_empty(sender
, (BIT(2) | BIT(10) | BIT(18) |
101 BIT(26) | BIT(27) | BIT(28)));
104 static int wait_for_lp_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
106 return wait_for_gen_fifo_empty(sender
, (BIT(10) | BIT(26)));
109 static int wait_for_hs_fifos_empty(struct mdfld_dsi_pkg_sender
*sender
)
111 return wait_for_gen_fifo_empty(sender
, (BIT(2) | BIT(18)));
114 static int handle_dsi_error(struct mdfld_dsi_pkg_sender
*sender
, u32 mask
)
116 u32 intr_stat_reg
= sender
->mipi_intr_stat_reg
;
117 struct drm_device
*dev
= sender
->dev
;
119 dev_dbg(sender
->dev
->dev
, "Handling error 0x%08x\n", mask
);
136 dev_dbg(sender
->dev
->dev
, "No Action required\n");
139 /*wait for all fifo empty*/
140 /*wait_for_all_fifos_empty(sender)*/
143 dev_dbg(sender
->dev
->dev
, "No Action required\n");
151 dev_dbg(sender
->dev
->dev
, "High/Low contention detected\n");
152 /*wait for contention recovery time*/
154 /*wait for all fifo empty*/
156 wait_for_all_fifos_empty(sender
);
159 dev_dbg(sender
->dev
->dev
, "No Action required\n");
162 /*wait for all fifo empty*/
163 /*wait_for_all_fifos_empty(sender);*/
172 dev_dbg(sender
->dev
->dev
, "HS Gen fifo full\n");
173 REG_WRITE(intr_stat_reg
, mask
);
174 wait_for_hs_fifos_empty(sender
);
177 dev_dbg(sender
->dev
->dev
, "LP Gen fifo full\n");
178 REG_WRITE(intr_stat_reg
, mask
);
179 wait_for_lp_fifos_empty(sender
);
184 dev_dbg(sender
->dev
->dev
, "No Action required\n");
188 if (mask
& REG_READ(intr_stat_reg
))
189 dev_dbg(sender
->dev
->dev
,
190 "Cannot clean interrupt 0x%08x\n", mask
);
194 static int dsi_error_handler(struct mdfld_dsi_pkg_sender
*sender
)
196 struct drm_device
*dev
= sender
->dev
;
197 u32 intr_stat_reg
= sender
->mipi_intr_stat_reg
;
203 intr_stat
= REG_READ(intr_stat_reg
);
205 for (i
= 0; i
< 32; i
++) {
206 mask
= (0x00000001UL
) << i
;
207 if (intr_stat
& mask
) {
208 dev_dbg(sender
->dev
->dev
, "[DSI]: %s\n", dsi_errors
[i
]);
209 err
= handle_dsi_error(sender
, mask
);
211 DRM_ERROR("Cannot handle error\n");
217 static int send_short_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
218 u8 cmd
, u8 param
, bool hs
)
220 struct drm_device
*dev
= sender
->dev
;
223 u8 virtual_channel
= 0;
226 ctrl_reg
= sender
->mipi_hs_gen_ctrl_reg
;
228 /* FIXME: wait_for_hs_fifos_empty(sender); */
230 ctrl_reg
= sender
->mipi_lp_gen_ctrl_reg
;
232 /* FIXME: wait_for_lp_fifos_empty(sender); */
235 val
= FLD_VAL(param
, 23, 16) | FLD_VAL(cmd
, 15, 8) |
236 FLD_VAL(virtual_channel
, 7, 6) | FLD_VAL(data_type
, 5, 0);
238 REG_WRITE(ctrl_reg
, val
);
243 static int send_long_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
244 u8
*data
, int len
, bool hs
)
246 struct drm_device
*dev
= sender
->dev
;
252 u8 virtual_channel
= 0;
256 ctrl_reg
= sender
->mipi_hs_gen_ctrl_reg
;
257 data_reg
= sender
->mipi_hs_gen_data_reg
;
259 /* FIXME: wait_for_hs_fifos_empty(sender); */
261 ctrl_reg
= sender
->mipi_lp_gen_ctrl_reg
;
262 data_reg
= sender
->mipi_lp_gen_data_reg
;
264 /* FIXME: wait_for_lp_fifos_empty(sender); */
268 for (i
= 0; i
< len
/ 4; i
++) {
274 REG_WRITE(data_reg
, b4
<< 24 | b3
<< 16 | b2
<< 8 | b1
);
279 b1
= 0; b2
= 0; b3
= 0;
296 REG_WRITE(data_reg
, b3
<< 16 | b2
<< 8 | b1
);
299 val
= FLD_VAL(len
, 23, 8) | FLD_VAL(virtual_channel
, 7, 6) |
300 FLD_VAL(data_type
, 5, 0);
302 REG_WRITE(ctrl_reg
, val
);
307 static int send_pkg_prepare(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
313 case MIPI_DSI_DCS_SHORT_WRITE
:
314 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
315 case MIPI_DSI_DCS_LONG_WRITE
:
322 /*this prevents other package sending while doing msleep*/
323 sender
->status
= MDFLD_DSI_PKG_SENDER_BUSY
;
325 /*wait for 120 milliseconds in case exit_sleep_mode just be sent*/
326 if (unlikely(cmd
== MIPI_DCS_ENTER_SLEEP_MODE
)) {
327 /*TODO: replace it with msleep later*/
331 if (unlikely(cmd
== MIPI_DCS_EXIT_SLEEP_MODE
)) {
332 /*TODO: replace it with msleep later*/
338 static int send_pkg_done(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
344 case MIPI_DSI_DCS_SHORT_WRITE
:
345 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
346 case MIPI_DSI_DCS_LONG_WRITE
:
353 /*update panel status*/
354 if (unlikely(cmd
== MIPI_DCS_ENTER_SLEEP_MODE
)) {
355 sender
->panel_mode
|= MDFLD_DSI_PANEL_MODE_SLEEP
;
356 /*TODO: replace it with msleep later*/
358 } else if (unlikely(cmd
== MIPI_DCS_EXIT_SLEEP_MODE
)) {
359 sender
->panel_mode
&= ~MDFLD_DSI_PANEL_MODE_SLEEP
;
360 /*TODO: replace it with msleep later*/
362 } else if (unlikely(cmd
== MIPI_DCS_SOFT_RESET
)) {
363 /*TODO: replace it with msleep later*/
367 sender
->status
= MDFLD_DSI_PKG_SENDER_FREE
;
372 static int send_pkg(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
373 u8
*data
, u16 len
, bool hs
)
378 ret
= dsi_error_handler(sender
);
380 DRM_ERROR("Error handling failed\n");
385 if (sender
->status
== MDFLD_DSI_PKG_SENDER_BUSY
) {
386 DRM_ERROR("sender is busy\n");
390 ret
= send_pkg_prepare(sender
, data_type
, data
, len
);
392 DRM_ERROR("send_pkg_prepare error\n");
397 case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM
:
398 case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM
:
399 case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM
:
400 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM
:
401 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM
:
402 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM
:
403 case MIPI_DSI_DCS_SHORT_WRITE
:
404 case MIPI_DSI_DCS_SHORT_WRITE_PARAM
:
405 case MIPI_DSI_DCS_READ
:
406 ret
= send_short_pkg(sender
, data_type
, data
[0], data
[1], hs
);
408 case MIPI_DSI_GENERIC_LONG_WRITE
:
409 case MIPI_DSI_DCS_LONG_WRITE
:
410 ret
= send_long_pkg(sender
, data_type
, data
, len
, hs
);
414 send_pkg_done(sender
, data_type
, data
, len
);
416 /*FIXME: should I query complete and fifo empty here?*/
421 int mdfld_dsi_send_mcs_long(struct mdfld_dsi_pkg_sender
*sender
, u8
*data
,
426 if (!sender
|| !data
|| !len
) {
427 DRM_ERROR("Invalid parameters\n");
431 spin_lock_irqsave(&sender
->lock
, flags
);
432 send_pkg(sender
, MIPI_DSI_DCS_LONG_WRITE
, data
, len
, hs
);
433 spin_unlock_irqrestore(&sender
->lock
, flags
);
438 int mdfld_dsi_send_mcs_short(struct mdfld_dsi_pkg_sender
*sender
, u8 cmd
,
439 u8 param
, u8 param_num
, bool hs
)
446 DRM_ERROR("Invalid parameter\n");
453 data_type
= MIPI_DSI_DCS_SHORT_WRITE_PARAM
;
456 data_type
= MIPI_DSI_DCS_SHORT_WRITE
;
460 spin_lock_irqsave(&sender
->lock
, flags
);
461 send_pkg(sender
, data_type
, data
, sizeof(data
), hs
);
462 spin_unlock_irqrestore(&sender
->lock
, flags
);
467 int mdfld_dsi_send_gen_short(struct mdfld_dsi_pkg_sender
*sender
, u8 param0
,
468 u8 param1
, u8 param_num
, bool hs
)
474 if (!sender
|| param_num
> 2) {
475 DRM_ERROR("Invalid parameter\n");
481 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM
;
486 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM
;
491 data_type
= MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM
;
497 spin_lock_irqsave(&sender
->lock
, flags
);
498 send_pkg(sender
, data_type
, data
, sizeof(data
), hs
);
499 spin_unlock_irqrestore(&sender
->lock
, flags
);
504 int mdfld_dsi_send_gen_long(struct mdfld_dsi_pkg_sender
*sender
, u8
*data
,
509 if (!sender
|| !data
|| !len
) {
510 DRM_ERROR("Invalid parameters\n");
514 spin_lock_irqsave(&sender
->lock
, flags
);
515 send_pkg(sender
, MIPI_DSI_GENERIC_LONG_WRITE
, data
, len
, hs
);
516 spin_unlock_irqrestore(&sender
->lock
, flags
);
521 static int __read_panel_data(struct mdfld_dsi_pkg_sender
*sender
, u8 data_type
,
522 u8
*data
, u16 len
, u32
*data_out
, u16 len_out
, bool hs
)
525 struct drm_device
*dev
;
528 int retry
= MDFLD_DSI_READ_MAX_COUNT
;
530 if (!sender
|| !data_out
|| !len_out
) {
531 DRM_ERROR("Invalid parameters\n");
539 * 0) send out generic read request
540 * 1) polling read data avail interrupt
543 spin_lock_irqsave(&sender
->lock
, flags
);
545 REG_WRITE(sender
->mipi_intr_stat_reg
, BIT(29));
547 if ((REG_READ(sender
->mipi_intr_stat_reg
) & BIT(29)))
548 DRM_ERROR("Can NOT clean read data valid interrupt\n");
550 /*send out read request*/
551 send_pkg(sender
, data_type
, data
, len
, hs
);
553 /*polling read data avail interrupt*/
554 while (retry
&& !(REG_READ(sender
->mipi_intr_stat_reg
) & BIT(29))) {
560 spin_unlock_irqrestore(&sender
->lock
, flags
);
564 REG_WRITE(sender
->mipi_intr_stat_reg
, BIT(29));
568 gen_data_reg
= sender
->mipi_hs_gen_data_reg
;
570 gen_data_reg
= sender
->mipi_lp_gen_data_reg
;
572 for (i
= 0; i
< len_out
; i
++)
573 *(data_out
+ i
) = REG_READ(gen_data_reg
);
575 spin_unlock_irqrestore(&sender
->lock
, flags
);
580 int mdfld_dsi_read_mcs(struct mdfld_dsi_pkg_sender
*sender
, u8 cmd
,
581 u32
*data
, u16 len
, bool hs
)
583 if (!sender
|| !data
|| !len
) {
584 DRM_ERROR("Invalid parameters\n");
588 return __read_panel_data(sender
, MIPI_DSI_DCS_READ
, &cmd
, 1,
592 int mdfld_dsi_pkg_sender_init(struct mdfld_dsi_connector
*dsi_connector
,
595 struct mdfld_dsi_pkg_sender
*pkg_sender
;
596 struct mdfld_dsi_config
*dsi_config
=
597 mdfld_dsi_get_config(dsi_connector
);
598 struct drm_device
*dev
= dsi_config
->dev
;
599 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
600 const struct psb_offset
*map
= &dev_priv
->regmap
[pipe
];
603 if (!dsi_connector
) {
604 DRM_ERROR("Invalid parameter\n");
608 pkg_sender
= dsi_connector
->pkg_sender
;
610 if (!pkg_sender
|| IS_ERR(pkg_sender
)) {
611 pkg_sender
= kzalloc(sizeof(struct mdfld_dsi_pkg_sender
),
614 DRM_ERROR("Create DSI pkg sender failed\n");
617 dsi_connector
->pkg_sender
= (void *)pkg_sender
;
620 pkg_sender
->dev
= dev
;
621 pkg_sender
->dsi_connector
= dsi_connector
;
622 pkg_sender
->pipe
= pipe
;
623 pkg_sender
->pkg_num
= 0;
624 pkg_sender
->panel_mode
= 0;
625 pkg_sender
->status
= MDFLD_DSI_PKG_SENDER_FREE
;
628 /* FIXME: should just copy the regmap ptr ? */
629 pkg_sender
->dpll_reg
= map
->dpll
;
630 pkg_sender
->dspcntr_reg
= map
->cntr
;
631 pkg_sender
->pipeconf_reg
= map
->conf
;
632 pkg_sender
->dsplinoff_reg
= map
->linoff
;
633 pkg_sender
->dspsurf_reg
= map
->surf
;
634 pkg_sender
->pipestat_reg
= map
->status
;
636 pkg_sender
->mipi_intr_stat_reg
= MIPI_INTR_STAT_REG(pipe
);
637 pkg_sender
->mipi_lp_gen_data_reg
= MIPI_LP_GEN_DATA_REG(pipe
);
638 pkg_sender
->mipi_hs_gen_data_reg
= MIPI_HS_GEN_DATA_REG(pipe
);
639 pkg_sender
->mipi_lp_gen_ctrl_reg
= MIPI_LP_GEN_CTRL_REG(pipe
);
640 pkg_sender
->mipi_hs_gen_ctrl_reg
= MIPI_HS_GEN_CTRL_REG(pipe
);
641 pkg_sender
->mipi_gen_fifo_stat_reg
= MIPI_GEN_FIFO_STAT_REG(pipe
);
642 pkg_sender
->mipi_data_addr_reg
= MIPI_DATA_ADD_REG(pipe
);
643 pkg_sender
->mipi_data_len_reg
= MIPI_DATA_LEN_REG(pipe
);
644 pkg_sender
->mipi_cmd_addr_reg
= MIPI_CMD_ADD_REG(pipe
);
645 pkg_sender
->mipi_cmd_len_reg
= MIPI_CMD_LEN_REG(pipe
);
648 spin_lock_init(&pkg_sender
->lock
);
650 if (mdfld_get_panel_type(dev
, pipe
) != TC35876X
) {
652 * For video mode, don't enable DPI timing output here,
653 * will init the DPI timing output during mode setting.
655 mipi_val
= PASS_FROM_SPHY_TO_AFE
| SEL_FLOPPED_HSTX
;
660 REG_WRITE(MIPI_PORT_CONTROL(pipe
), mipi_val
);
661 REG_READ(MIPI_PORT_CONTROL(pipe
));
663 /* do dsi controller init */
664 mdfld_dsi_controller_init(dsi_config
, pipe
);
670 void mdfld_dsi_pkg_sender_destroy(struct mdfld_dsi_pkg_sender
*sender
)
672 if (!sender
|| IS_ERR(sender
))