blob: 396d979c21dcf35dee5109cfdca9c1734e82b7de [file] [log] [blame]
Angel Ponse67ab182020-04-04 18:51:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Hung-Te Linc59fbf22019-08-07 08:00:58 +08002
Hung-Te Lin75e43142019-08-07 10:31:27 +08003#include <assert.h>
Hung-Te Linc59fbf22019-08-07 08:00:58 +08004#include <console/console.h>
Julius Werner5ff18082021-08-24 16:03:57 -07005#include <mipi/panel.h>
Hung-Te Linc59fbf22019-08-07 08:00:58 +08006#include <device/mmio.h>
7#include <delay.h>
8#include <edid.h>
9#include <soc/dsi.h>
Hung-Te Linff0945e2019-08-07 09:59:16 +080010#include <string.h>
Hung-Te Linc59fbf22019-08-07 08:00:58 +080011#include <timer.h>
12
Paul Mad0ded162020-05-08 14:28:25 +080013#define MIN_HFP_BYTE 2
14#define MIN_HBP_BYTE 2
15
Hung-Te Lin61e34662019-08-07 08:44:30 +080016static unsigned int mtk_dsi_get_bits_per_pixel(u32 format)
17{
18 switch (format) {
19 case MIPI_DSI_FMT_RGB565:
20 return 16;
Hung-Te Lin61e34662019-08-07 08:44:30 +080021 case MIPI_DSI_FMT_RGB666_PACKED:
22 return 18;
Yu-Ping Wub4a29382020-02-12 11:51:49 +080023 case MIPI_DSI_FMT_RGB666:
Hung-Te Lin61e34662019-08-07 08:44:30 +080024 case MIPI_DSI_FMT_RGB888:
25 return 24;
26 }
27 printk(BIOS_WARNING, "%s: WARN: Unknown format %d, assuming 24 bpp\n",
28 __func__, format);
29 return 24;
30}
31
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080032static u32 mtk_dsi_get_data_rate(u32 bits_per_pixel, u32 lanes,
Hung-Te Lin302dddf2019-08-08 06:28:43 +080033 const struct edid *edid)
34{
35 /* data_rate = pixel_clock * bits_per_pixel * mipi_ratio / lanes
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080036 * Note pixel_clock comes in kHz and returned data_rate is in bps.
Hung-Te Lin302dddf2019-08-08 06:28:43 +080037 * mipi_ratio is the clk coefficient to balance the pixel clk in MIPI
38 * for older platforms which do not have complete implementation in HFP.
39 * Newer platforms should just set that to 1.0 (100 / 100).
40 */
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080041 u32 data_rate = DIV_ROUND_UP((u64)edid->mode.pixel_clock *
42 bits_per_pixel * 1000 *
43 MTK_DSI_MIPI_RATIO_NUMERATOR,
44 (u64)lanes *
45 MTK_DSI_MIPI_RATIO_DENOMINATOR);
46 printk(BIOS_INFO, "DSI data_rate: %u bps\n", data_rate);
Hung-Te Lin302dddf2019-08-08 06:28:43 +080047
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080048 if (data_rate < MTK_DSI_DATA_RATE_MIN_MHZ * MHz) {
49 printk(BIOS_ERR, "data rate (%ubps) must be >= %ubps. "
50 "Please check the pixel clock (%u), "
51 "bits per pixel (%u), "
Hung-Te Lin302dddf2019-08-08 06:28:43 +080052 "mipi_ratio (%d%%) and number of lanes (%d)\n",
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080053 data_rate, MTK_DSI_DATA_RATE_MIN_MHZ * MHz,
Hung-Te Lin302dddf2019-08-08 06:28:43 +080054 edid->mode.pixel_clock, bits_per_pixel,
55 (100 * MTK_DSI_MIPI_RATIO_NUMERATOR /
56 MTK_DSI_MIPI_RATIO_DENOMINATOR), lanes);
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080057 return 0;
Hung-Te Lin302dddf2019-08-08 06:28:43 +080058 }
59 return data_rate;
60}
61
Hung-Te Linff0945e2019-08-07 09:59:16 +080062__weak void mtk_dsi_override_phy_timing(struct mtk_phy_timing *timing)
Hung-Te Linc59fbf22019-08-07 08:00:58 +080063{
Hung-Te Linff0945e2019-08-07 09:59:16 +080064 /* Do nothing. */
65}
66
Jitao Shif68cc812020-01-14 21:23:44 +080067static void mtk_dsi_phy_timing(u32 data_rate, struct mtk_phy_timing *timing)
Hung-Te Linff0945e2019-08-07 09:59:16 +080068{
Jitao Shif68cc812020-01-14 21:23:44 +080069 u32 timcon0, timcon1, timcon2, timcon3;
Yu-Ping Wu443fbd72020-02-11 18:33:57 +080070 u32 data_rate_mhz = DIV_ROUND_UP(data_rate, MHz);
Hung-Te Linc59fbf22019-08-07 08:00:58 +080071
Jitao Shif68cc812020-01-14 21:23:44 +080072 memset(timing, 0, sizeof(*timing));
Hung-Te Linc59fbf22019-08-07 08:00:58 +080073
Jitao Shif68cc812020-01-14 21:23:44 +080074 timing->lpx = (60 * data_rate_mhz / (8 * 1000)) + 1;
75 timing->da_hs_prepare = (80 * data_rate_mhz + 4 * 1000) / 8000;
76 timing->da_hs_zero = (170 * data_rate_mhz + 10 * 1000) / 8000 + 1 -
77 timing->da_hs_prepare;
78 timing->da_hs_trail = timing->da_hs_prepare + 1;
Hung-Te Linff0945e2019-08-07 09:59:16 +080079
Jitao Shif68cc812020-01-14 21:23:44 +080080 timing->ta_go = 4 * timing->lpx - 2;
81 timing->ta_sure = timing->lpx + 2;
82 timing->ta_get = 4 * timing->lpx;
83 timing->da_hs_exit = 2 * timing->lpx + 1;
Hung-Te Linff0945e2019-08-07 09:59:16 +080084
Jitao Shif68cc812020-01-14 21:23:44 +080085 timing->da_hs_sync = 1;
Hung-Te Linff0945e2019-08-07 09:59:16 +080086
Jitao Shif68cc812020-01-14 21:23:44 +080087 timing->clk_hs_prepare = 70 * data_rate_mhz / (8 * 1000);
88 timing->clk_hs_post = timing->clk_hs_prepare + 8;
89 timing->clk_hs_trail = timing->clk_hs_prepare;
90 timing->clk_hs_zero = timing->clk_hs_trail * 4;
91 timing->clk_hs_exit = 2 * timing->clk_hs_trail;
Hung-Te Linff0945e2019-08-07 09:59:16 +080092
93 /* Allow board-specific tuning. */
Jitao Shif68cc812020-01-14 21:23:44 +080094 mtk_dsi_override_phy_timing(timing);
Hung-Te Linff0945e2019-08-07 09:59:16 +080095
Jitao Shif68cc812020-01-14 21:23:44 +080096 timcon0 = timing->lpx | timing->da_hs_prepare << 8 |
97 timing->da_hs_zero << 16 | timing->da_hs_trail << 24;
98 timcon1 = timing->ta_go | timing->ta_sure << 8 |
99 timing->ta_get << 16 | timing->da_hs_exit << 24;
100 timcon2 = timing->da_hs_sync << 8 | timing->clk_hs_zero << 16 |
101 timing->clk_hs_trail << 24;
102 timcon3 = timing->clk_hs_prepare | timing->clk_hs_post << 8 |
103 timing->clk_hs_exit << 16;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800104
105 write32(&dsi0->dsi_phy_timecon0, timcon0);
106 write32(&dsi0->dsi_phy_timecon1, timcon1);
107 write32(&dsi0->dsi_phy_timecon2, timcon2);
108 write32(&dsi0->dsi_phy_timecon3, timcon3);
109}
110
111static void mtk_dsi_clk_hs_mode_enable(void)
112{
Julius Werner55009af2019-12-02 22:03:27 -0800113 setbits32(&dsi0->dsi_phy_lccon, LC_HS_TX_EN);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800114}
115
116static void mtk_dsi_clk_hs_mode_disable(void)
117{
Julius Werner55009af2019-12-02 22:03:27 -0800118 clrbits32(&dsi0->dsi_phy_lccon, LC_HS_TX_EN);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800119}
120
121static void mtk_dsi_set_mode(u32 mode_flags)
122{
123 u32 tmp_reg1 = 0;
124
125 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
126 tmp_reg1 = SYNC_PULSE_MODE;
127
128 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
129 tmp_reg1 = BURST_MODE;
130
131 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
132 tmp_reg1 = SYNC_PULSE_MODE;
133 }
134
135 write32(&dsi0->dsi_mode_ctrl, tmp_reg1);
136}
137
138static void mtk_dsi_rxtx_control(u32 mode_flags, u32 lanes)
139{
140 u32 tmp_reg = 0;
141
142 switch (lanes) {
143 case 1:
144 tmp_reg = 1 << 2;
145 break;
146 case 2:
147 tmp_reg = 3 << 2;
148 break;
149 case 3:
150 tmp_reg = 7 << 2;
151 break;
152 case 4:
153 default:
154 tmp_reg = 0xf << 2;
155 break;
156 }
157
Shaoming Chen9e38efc2020-12-23 11:45:59 +0800158 if (mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
159 tmp_reg |= NON_CONTINUOUS_CLK;
160
161 if (!(mode_flags & MIPI_DSI_MODE_EOT_PACKET))
162 tmp_reg |= EOTP_DISABLE;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800163
164 write32(&dsi0->dsi_txrx_ctrl, tmp_reg);
165}
166
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800167static void mtk_dsi_config_vdo_timing(u32 mode_flags, u32 format, u32 lanes,
168 const struct edid *edid,
169 const struct mtk_phy_timing *phy_timing)
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800170{
171 u32 hsync_active_byte;
Jitao Shif68cc812020-01-14 21:23:44 +0800172 u32 hbp;
173 u32 hfp;
Paul Mad0ded162020-05-08 14:28:25 +0800174 s32 hbp_byte;
175 s32 hfp_byte;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800176 u32 vbp_byte;
177 u32 vfp_byte;
Hung-Te Lin61e34662019-08-07 08:44:30 +0800178 u32 bytes_per_pixel;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800179 u32 packet_fmt;
180 u32 hactive;
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800181 u32 data_phy_cycles;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800182
Hung-Te Lin61e34662019-08-07 08:44:30 +0800183 bytes_per_pixel = DIV_ROUND_UP(mtk_dsi_get_bits_per_pixel(format), 8);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800184 vbp_byte = edid->mode.vbl - edid->mode.vso - edid->mode.vspw -
185 edid->mode.vborder;
186 vfp_byte = edid->mode.vso - edid->mode.vborder;
187
188 write32(&dsi0->dsi_vsa_nl, edid->mode.vspw);
189 write32(&dsi0->dsi_vbp_nl, vbp_byte);
190 write32(&dsi0->dsi_vfp_nl, vfp_byte);
191 write32(&dsi0->dsi_vact_nl, edid->mode.va);
192
Hung-Te Lin61e34662019-08-07 08:44:30 +0800193 hsync_active_byte = edid->mode.hspw * bytes_per_pixel - 10;
Jitao Shif68cc812020-01-14 21:23:44 +0800194
195 hbp = edid->mode.hbl - edid->mode.hso - edid->mode.hspw -
196 edid->mode.hborder;
197 hfp = edid->mode.hso - edid->mode.hborder;
198
199 if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
200 hbp_byte = hbp * bytes_per_pixel - 10;
201 else
202 hbp_byte = (hbp + edid->mode.hspw) * bytes_per_pixel - 10;
203 hfp_byte = hfp * bytes_per_pixel;
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800204
205 data_phy_cycles = phy_timing->lpx + phy_timing->da_hs_prepare +
Jitao Shif68cc812020-01-14 21:23:44 +0800206 phy_timing->da_hs_zero + phy_timing->da_hs_exit + 3;
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800207
Jitao Shi104b7a92021-03-12 17:35:18 +0800208 u32 delta = 10;
Shaoming Chen9e38efc2020-12-23 11:45:59 +0800209
210 if (mode_flags & MIPI_DSI_MODE_EOT_PACKET)
211 delta += 2;
212
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800213 if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
214 delta += 6;
215
216 u32 d_phy = phy_timing->d_phy;
217 if (d_phy == 0)
218 d_phy = data_phy_cycles * lanes + delta;
Jitao Shif68cc812020-01-14 21:23:44 +0800219
220 if ((hfp + hbp) * bytes_per_pixel > d_phy) {
221 hfp_byte -= d_phy * hfp / (hfp + hbp);
222 hbp_byte -= d_phy * hbp / (hfp + hbp);
223 } else {
224 printk(BIOS_ERR, "HFP plus HBP is not greater than d_phy, "
225 "the panel may not work properly.\n");
226 }
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800227
Jitao Shi927fa6d2021-02-01 13:21:45 +0800228 if (mode_flags & MIPI_DSI_MODE_LINE_END) {
229 hsync_active_byte = DIV_ROUND_UP(hsync_active_byte, lanes) * lanes - 2;
230 hbp_byte = DIV_ROUND_UP(hbp_byte, lanes) * lanes - 2;
231 hfp_byte = DIV_ROUND_UP(hfp_byte, lanes) * lanes - 2;
232 hbp_byte -= (edid->mode.ha * bytes_per_pixel + 2) % lanes;
233 }
234
Paul Mad0ded162020-05-08 14:28:25 +0800235 if (hfp_byte + hbp_byte < MIN_HFP_BYTE + MIN_HBP_BYTE) {
236 printk(BIOS_ERR, "Calculated hfp_byte and hbp_byte are too small, "
237 "the panel may not work properly.\n");
238 } else if (hfp_byte < MIN_HFP_BYTE) {
239 printk(BIOS_NOTICE, "Calculated hfp_byte is too small, "
240 "adjust it to the minimum value.\n");
241 hbp_byte -= MIN_HFP_BYTE - hfp_byte;
242 hfp_byte = MIN_HFP_BYTE;
243 } else if (hbp_byte < MIN_HBP_BYTE) {
244 printk(BIOS_NOTICE, "Calculated hbp_byte is too small, "
245 "adjust it to the minimum value.\n");
246 hfp_byte -= MIN_HBP_BYTE - hbp_byte;
247 hbp_byte = MIN_HBP_BYTE;
248 }
249
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800250 write32(&dsi0->dsi_hsa_wc, hsync_active_byte);
251 write32(&dsi0->dsi_hbp_wc, hbp_byte);
252 write32(&dsi0->dsi_hfp_wc, hfp_byte);
253
254 switch (format) {
255 case MIPI_DSI_FMT_RGB888:
256 packet_fmt = PACKED_PS_24BIT_RGB888;
257 break;
258 case MIPI_DSI_FMT_RGB666:
259 packet_fmt = LOOSELY_PS_18BIT_RGB666;
260 break;
261 case MIPI_DSI_FMT_RGB666_PACKED:
262 packet_fmt = PACKED_PS_18BIT_RGB666;
263 break;
264 case MIPI_DSI_FMT_RGB565:
265 packet_fmt = PACKED_PS_16BIT_RGB565;
266 break;
267 default:
268 packet_fmt = PACKED_PS_24BIT_RGB888;
269 break;
270 }
271
272 hactive = edid->mode.ha;
Hung-Te Lin61e34662019-08-07 08:44:30 +0800273 packet_fmt |= (hactive * bytes_per_pixel) & DSI_PS_WC;
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800274
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800275 write32(&dsi0->dsi_psctrl,
276 PIXEL_STREAM_CUSTOM_HEADER << DSI_PSCON_CUSTOM_HEADER_SHIFT |
277 packet_fmt);
Hung-Te Lin32ddc0d2019-08-07 10:58:36 +0800278
279 /* Older systems like MT8173 do not support size_con. */
280 if (MTK_DSI_HAVE_SIZE_CON)
281 write32(&dsi0->dsi_size_con,
282 edid->mode.va << DSI_SIZE_CON_HEIGHT_SHIFT |
283 hactive << DSI_SIZE_CON_WIDTH_SHIFT);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800284}
285
286static void mtk_dsi_start(void)
287{
288 write32(&dsi0->dsi_start, 0);
289 /* Only start master DSI */
290 write32(&dsi0->dsi_start, 1);
291}
292
Julius Werner4757a7e2021-09-08 17:58:34 -0700293static bool mtk_dsi_is_read_command(enum mipi_dsi_transaction type)
Hung-Te Lin75e43142019-08-07 10:31:27 +0800294{
295 switch (type) {
296 case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
297 case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
298 case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
299 case MIPI_DSI_DCS_READ:
300 return true;
Julius Werner4757a7e2021-09-08 17:58:34 -0700301 default:
302 return false;
Hung-Te Lin75e43142019-08-07 10:31:27 +0800303 }
Hung-Te Lin75e43142019-08-07 10:31:27 +0800304}
305
Julius Werner69cc5572022-03-04 17:49:56 -0800306static enum cb_err mtk_dsi_cmdq(enum mipi_dsi_transaction type, const u8 *data, u8 len)
Hung-Te Lin75e43142019-08-07 10:31:27 +0800307{
308 const u8 *tx_buf = data;
309 u32 config;
310 int i, j;
311
312 if (!wait_ms(20, !(read32(&dsi0->dsi_intsta) & DSI_BUSY))) {
313 printk(BIOS_ERR, "%s: cannot get DSI ready for sending commands"
314 " after 20ms and the panel may not work properly.\n",
315 __func__);
Julius Werner4757a7e2021-09-08 17:58:34 -0700316 return CB_ERR;
Hung-Te Lin75e43142019-08-07 10:31:27 +0800317 }
318 write32(&dsi0->dsi_intsta, 0);
319
320 if (mtk_dsi_is_read_command(type))
321 config = BTA;
322 else
323 config = (len > 2) ? LONG_PACKET : SHORT_PACKET;
324
325 if (len <= 2) {
326 uint32_t val = (type << 8) | config;
327 for (i = 0; i < len; i++)
328 val |= tx_buf[i] << (i + 2) * 8;
329 write32(&dsi0->dsi_cmdq[0], val);
330 write32(&dsi0->dsi_cmdq_size, 1);
331 } else {
332 /* TODO(hungte) Replace by buffer_to_fifo32_prefix */
333 write32(&dsi0->dsi_cmdq[0], (len << 16) | (type << 8) | config);
334 for (i = 0; i < len; i += 4) {
335 uint32_t val = 0;
336 for (j = 0; j < MIN(len - i, 4); j++)
337 val |= tx_buf[i + j] << j * 8;
338 write32(&dsi0->dsi_cmdq[i / 4 + 1], val);
339 }
340 write32(&dsi0->dsi_cmdq_size, 1 + DIV_ROUND_UP(len, 4));
341 }
342
343 mtk_dsi_start();
344
345 if (!wait_us(400, read32(&dsi0->dsi_intsta) & CMD_DONE_INT_FLAG)) {
346 printk(BIOS_ERR, "%s: failed sending DSI command, "
347 "panel may not work.\n", __func__);
Julius Wernerb2a14802021-08-12 16:48:12 -0700348 return CB_ERR;
Hung-Te Lin75e43142019-08-07 10:31:27 +0800349 }
Julius Wernerb2a14802021-08-12 16:48:12 -0700350
Julius Wernerb2a14802021-08-12 16:48:12 -0700351 return CB_SUCCESS;
Hung-Te Lin75e43142019-08-07 10:31:27 +0800352}
353
Jitao Shib6ca9382019-10-22 10:15:34 +0800354static void mtk_dsi_reset_dphy(void)
355{
Julius Werner55009af2019-12-02 22:03:27 -0800356 setbits32(&dsi0->dsi_con_ctrl, DPHY_RESET);
357 clrbits32(&dsi0->dsi_con_ctrl, DPHY_RESET);
Jitao Shib6ca9382019-10-22 10:15:34 +0800358}
359
Hung-Te Lin75e43142019-08-07 10:31:27 +0800360int mtk_dsi_init(u32 mode_flags, u32 format, u32 lanes, const struct edid *edid,
Hung-Te Line366ba12019-08-07 16:28:08 +0800361 const u8 *init_commands)
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800362{
Yu-Ping Wu443fbd72020-02-11 18:33:57 +0800363 u32 data_rate;
Hung-Te Lin61e34662019-08-07 08:44:30 +0800364 u32 bits_per_pixel = mtk_dsi_get_bits_per_pixel(format);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800365
Hung-Te Lin302dddf2019-08-08 06:28:43 +0800366 data_rate = mtk_dsi_get_data_rate(bits_per_pixel, lanes, edid);
Yu-Ping Wu443fbd72020-02-11 18:33:57 +0800367 if (!data_rate)
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800368 return -1;
369
Hung-Te Lin302dddf2019-08-08 06:28:43 +0800370 mtk_dsi_configure_mipi_tx(data_rate, lanes);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800371 mtk_dsi_reset();
Hung-Te Linff0945e2019-08-07 09:59:16 +0800372 struct mtk_phy_timing phy_timing;
373 mtk_dsi_phy_timing(data_rate, &phy_timing);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800374 mtk_dsi_rxtx_control(mode_flags, lanes);
Jitao Shib6ca9382019-10-22 10:15:34 +0800375 mdelay(1);
376 mtk_dsi_reset_dphy();
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800377 mtk_dsi_clk_hs_mode_disable();
Hung-Te Lin3b217d52019-08-07 10:15:48 +0800378 mtk_dsi_config_vdo_timing(mode_flags, format, lanes, edid, &phy_timing);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800379 mtk_dsi_clk_hs_mode_enable();
Yu-Ping Wuf8f50622021-12-27 13:46:35 +0800380 if (init_commands)
381 mipi_panel_parse_init_commands(init_commands, mtk_dsi_cmdq);
Hung-Te Lin75e43142019-08-07 10:31:27 +0800382 mtk_dsi_set_mode(mode_flags);
Hung-Te Linc59fbf22019-08-07 08:00:58 +0800383 mtk_dsi_start();
384
385 return 0;
386}