blob: a7e0784596a985799bc0b101a26c0da3d2f03b6d [file] [log] [blame]
Yannick Fertré764af462019-10-07 15:29:06 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016, Fuzhou Rockchip Electronics Co., Ltd
4 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
5 * Author(s): Philippe Cornu <philippe.cornu@st.com> for STMicroelectronics.
6 * Yannick Fertre <yannick.fertre@st.com> for STMicroelectronics.
7 *
8 * This generic Synopsys DesignWare MIPI DSI host driver is inspired from
9 * the Linux Kernel driver drivers/gpu/drm/bridge/synopsys/dw-mipi-dsi.c.
10 */
11
12#include <common.h>
13#include <clk.h>
14#include <dsi_host.h>
15#include <dm.h>
16#include <errno.h>
17#include <panel.h>
18#include <video.h>
19#include <asm/io.h>
Yannick Fertré764af462019-10-07 15:29:06 +020020#include <dm/device-internal.h>
Simon Glass9bc15642020-02-03 07:36:16 -070021#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060022#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Yannick Fertré764af462019-10-07 15:29:06 +020024#include <linux/iopoll.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030025#include <linux/time.h>
Yannick Fertré764af462019-10-07 15:29:06 +020026#include <video_bridge.h>
27
28#define HWVER_131 0x31333100 /* IP version 1.31 */
29
30#define DSI_VERSION 0x00
31#define VERSION GENMASK(31, 8)
32
33#define DSI_PWR_UP 0x04
34#define RESET 0
35#define POWERUP BIT(0)
36
37#define DSI_CLKMGR_CFG 0x08
38#define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
39#define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
40
41#define DSI_DPI_VCID 0x0c
42#define DPI_VCID(vcid) ((vcid) & 0x3)
43
44#define DSI_DPI_COLOR_CODING 0x10
45#define LOOSELY18_EN BIT(8)
46#define DPI_COLOR_CODING_16BIT_1 0x0
47#define DPI_COLOR_CODING_16BIT_2 0x1
48#define DPI_COLOR_CODING_16BIT_3 0x2
49#define DPI_COLOR_CODING_18BIT_1 0x3
50#define DPI_COLOR_CODING_18BIT_2 0x4
51#define DPI_COLOR_CODING_24BIT 0x5
52
53#define DSI_DPI_CFG_POL 0x14
54#define COLORM_ACTIVE_LOW BIT(4)
55#define SHUTD_ACTIVE_LOW BIT(3)
56#define HSYNC_ACTIVE_LOW BIT(2)
57#define VSYNC_ACTIVE_LOW BIT(1)
58#define DATAEN_ACTIVE_LOW BIT(0)
59
60#define DSI_DPI_LP_CMD_TIM 0x18
61#define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
62#define INVACT_LPCMD_TIME(p) ((p) & 0xff)
63
64#define DSI_DBI_VCID 0x1c
65#define DSI_DBI_CFG 0x20
66#define DSI_DBI_PARTITIONING_EN 0x24
67#define DSI_DBI_CMDSIZE 0x28
68
69#define DSI_PCKHDL_CFG 0x2c
70#define CRC_RX_EN BIT(4)
71#define ECC_RX_EN BIT(3)
72#define BTA_EN BIT(2)
73#define EOTP_RX_EN BIT(1)
74#define EOTP_TX_EN BIT(0)
75
76#define DSI_GEN_VCID 0x30
77
78#define DSI_MODE_CFG 0x34
79#define ENABLE_VIDEO_MODE 0
80#define ENABLE_CMD_MODE BIT(0)
81
82#define DSI_VID_MODE_CFG 0x38
83#define ENABLE_LOW_POWER (0x3f << 8)
84#define ENABLE_LOW_POWER_MASK (0x3f << 8)
85#define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
86#define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
87#define VID_MODE_TYPE_BURST 0x2
88#define VID_MODE_TYPE_MASK 0x3
89
90#define DSI_VID_PKT_SIZE 0x3c
91#define VID_PKT_SIZE(p) ((p) & 0x3fff)
92
93#define DSI_VID_NUM_CHUNKS 0x40
94#define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
95
96#define DSI_VID_NULL_SIZE 0x44
97#define VID_NULL_SIZE(b) ((b) & 0x1fff)
98
99#define DSI_VID_HSA_TIME 0x48
100#define DSI_VID_HBP_TIME 0x4c
101#define DSI_VID_HLINE_TIME 0x50
102#define DSI_VID_VSA_LINES 0x54
103#define DSI_VID_VBP_LINES 0x58
104#define DSI_VID_VFP_LINES 0x5c
105#define DSI_VID_VACTIVE_LINES 0x60
106#define DSI_EDPI_CMD_SIZE 0x64
107
108#define DSI_CMD_MODE_CFG 0x68
109#define MAX_RD_PKT_SIZE_LP BIT(24)
110#define DCS_LW_TX_LP BIT(19)
111#define DCS_SR_0P_TX_LP BIT(18)
112#define DCS_SW_1P_TX_LP BIT(17)
113#define DCS_SW_0P_TX_LP BIT(16)
114#define GEN_LW_TX_LP BIT(14)
115#define GEN_SR_2P_TX_LP BIT(13)
116#define GEN_SR_1P_TX_LP BIT(12)
117#define GEN_SR_0P_TX_LP BIT(11)
118#define GEN_SW_2P_TX_LP BIT(10)
119#define GEN_SW_1P_TX_LP BIT(9)
120#define GEN_SW_0P_TX_LP BIT(8)
121#define ACK_RQST_EN BIT(1)
122#define TEAR_FX_EN BIT(0)
123
124#define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
125 DCS_LW_TX_LP | \
126 DCS_SR_0P_TX_LP | \
127 DCS_SW_1P_TX_LP | \
128 DCS_SW_0P_TX_LP | \
129 GEN_LW_TX_LP | \
130 GEN_SR_2P_TX_LP | \
131 GEN_SR_1P_TX_LP | \
132 GEN_SR_0P_TX_LP | \
133 GEN_SW_2P_TX_LP | \
134 GEN_SW_1P_TX_LP | \
135 GEN_SW_0P_TX_LP)
136
137#define DSI_GEN_HDR 0x6c
138#define DSI_GEN_PLD_DATA 0x70
139
140#define DSI_CMD_PKT_STATUS 0x74
141#define GEN_RD_CMD_BUSY BIT(6)
142#define GEN_PLD_R_FULL BIT(5)
143#define GEN_PLD_R_EMPTY BIT(4)
144#define GEN_PLD_W_FULL BIT(3)
145#define GEN_PLD_W_EMPTY BIT(2)
146#define GEN_CMD_FULL BIT(1)
147#define GEN_CMD_EMPTY BIT(0)
148
149#define DSI_TO_CNT_CFG 0x78
150#define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
151#define LPRX_TO_CNT(p) ((p) & 0xffff)
152
153#define DSI_HS_RD_TO_CNT 0x7c
154#define DSI_LP_RD_TO_CNT 0x80
155#define DSI_HS_WR_TO_CNT 0x84
156#define DSI_LP_WR_TO_CNT 0x88
157#define DSI_BTA_TO_CNT 0x8c
158
159#define DSI_LPCLK_CTRL 0x94
160#define AUTO_CLKLANE_CTRL BIT(1)
161#define PHY_TXREQUESTCLKHS BIT(0)
162
163#define DSI_PHY_TMR_LPCLK_CFG 0x98
164#define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
165#define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
166
167#define DSI_PHY_TMR_CFG 0x9c
168#define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
169#define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
170#define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
171#define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
172#define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
173
174#define DSI_PHY_RSTZ 0xa0
175#define PHY_DISFORCEPLL 0
176#define PHY_ENFORCEPLL BIT(3)
177#define PHY_DISABLECLK 0
178#define PHY_ENABLECLK BIT(2)
179#define PHY_RSTZ 0
180#define PHY_UNRSTZ BIT(1)
181#define PHY_SHUTDOWNZ 0
182#define PHY_UNSHUTDOWNZ BIT(0)
183
184#define DSI_PHY_IF_CFG 0xa4
185#define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
186#define N_LANES(n) (((n) - 1) & 0x3)
187
188#define DSI_PHY_ULPS_CTRL 0xa8
189#define DSI_PHY_TX_TRIGGERS 0xac
190
191#define DSI_PHY_STATUS 0xb0
192#define PHY_STOP_STATE_CLK_LANE BIT(2)
193#define PHY_LOCK BIT(0)
194
195#define DSI_PHY_TST_CTRL0 0xb4
196#define PHY_TESTCLK BIT(1)
197#define PHY_UNTESTCLK 0
198#define PHY_TESTCLR BIT(0)
199#define PHY_UNTESTCLR 0
200
201#define DSI_PHY_TST_CTRL1 0xb8
202#define PHY_TESTEN BIT(16)
203#define PHY_UNTESTEN 0
204#define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
205#define PHY_TESTDIN(n) ((n) & 0xff)
206
207#define DSI_INT_ST0 0xbc
208#define DSI_INT_ST1 0xc0
209#define DSI_INT_MSK0 0xc4
210#define DSI_INT_MSK1 0xc8
211
212#define DSI_PHY_TMR_RD_CFG 0xf4
213#define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
214
215#define PHY_STATUS_TIMEOUT_US 10000
216#define CMD_PKT_STATUS_TIMEOUT_US 20000
217
Yannick Fertré764af462019-10-07 15:29:06 +0200218struct dw_mipi_dsi {
219 struct mipi_dsi_host dsi_host;
220 struct mipi_dsi_device *device;
221 void __iomem *base;
222 unsigned int lane_mbps; /* per lane */
223 u32 channel;
224 unsigned int max_data_lanes;
225 const struct mipi_dsi_phy_ops *phy_ops;
226};
227
228static int dsi_mode_vrefresh(struct display_timing *timings)
229{
230 int refresh = 0;
231 unsigned int calc_val;
232 u32 htotal = timings->hactive.typ + timings->hfront_porch.typ +
233 timings->hback_porch.typ + timings->hsync_len.typ;
234 u32 vtotal = timings->vactive.typ + timings->vfront_porch.typ +
235 timings->vback_porch.typ + timings->vsync_len.typ;
236
237 if (htotal > 0 && vtotal > 0) {
238 calc_val = timings->pixelclock.typ;
239 calc_val /= htotal;
240 refresh = (calc_val + vtotal / 2) / vtotal;
241 }
242
243 return refresh;
244}
245
246/*
247 * The controller should generate 2 frames before
248 * preparing the peripheral.
249 */
250static void dw_mipi_dsi_wait_for_two_frames(struct display_timing *timings)
251{
252 int refresh, two_frames;
253
254 refresh = dsi_mode_vrefresh(timings);
255 two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
256 mdelay(two_frames);
257}
258
259static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
260{
261 return container_of(host, struct dw_mipi_dsi, dsi_host);
262}
263
264static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
265{
266 writel(val, dsi->base + reg);
267}
268
269static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
270{
271 return readl(dsi->base + reg);
272}
273
274static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
275 struct mipi_dsi_device *device)
276{
277 struct dw_mipi_dsi *dsi = host_to_dsi(host);
278
279 if (device->lanes > dsi->max_data_lanes) {
280 dev_err(device->dev,
281 "the number of data lanes(%u) is too many\n",
282 device->lanes);
283 return -EINVAL;
284 }
285
286 dsi->channel = device->channel;
287
288 return 0;
289}
290
291static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
292 const struct mipi_dsi_msg *msg)
293{
294 bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
295 u32 val = 0;
296
297 if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
298 val |= ACK_RQST_EN;
299 if (lpm)
300 val |= CMD_MODE_ALL_LP;
301
302 dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
303 dsi_write(dsi, DSI_CMD_MODE_CFG, val);
304}
305
306static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
307{
308 int ret;
309 u32 val, mask;
310
311 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
312 val, !(val & GEN_CMD_FULL),
313 CMD_PKT_STATUS_TIMEOUT_US);
314 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400315 dev_err(dsi->dsi_host.dev,
316 "failed to get available command FIFO\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200317 return ret;
318 }
319
320 dsi_write(dsi, DSI_GEN_HDR, hdr_val);
321
322 mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
323 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
324 val, (val & mask) == mask,
325 CMD_PKT_STATUS_TIMEOUT_US);
326 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400327 dev_err(dsi->dsi_host.dev, "failed to write command FIFO\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200328 return ret;
329 }
330
331 return 0;
332}
333
334static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
335 const struct mipi_dsi_packet *packet)
336{
337 const u8 *tx_buf = packet->payload;
338 int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
339 __le32 word;
340 u32 val;
341
342 while (len) {
343 if (len < pld_data_bytes) {
344 word = 0;
345 memcpy(&word, tx_buf, len);
346 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
347 len = 0;
348 } else {
349 memcpy(&word, tx_buf, pld_data_bytes);
350 dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
351 tx_buf += pld_data_bytes;
352 len -= pld_data_bytes;
353 }
354
355 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
356 val, !(val & GEN_PLD_W_FULL),
357 CMD_PKT_STATUS_TIMEOUT_US);
358 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400359 dev_err(dsi->dsi_host.dev,
Yannick Fertré764af462019-10-07 15:29:06 +0200360 "failed to get available write payload FIFO\n");
361 return ret;
362 }
363 }
364
365 word = 0;
366 memcpy(&word, packet->header, sizeof(packet->header));
367 return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
368}
369
370static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
371 const struct mipi_dsi_msg *msg)
372{
373 int i, j, ret, len = msg->rx_len;
374 u8 *buf = msg->rx_buf;
375 u32 val;
376
377 /* Wait end of the read operation */
378 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
379 val, !(val & GEN_RD_CMD_BUSY),
380 CMD_PKT_STATUS_TIMEOUT_US);
381 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400382 dev_err(dsi->dsi_host.dev, "Timeout during read operation\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200383 return ret;
384 }
385
386 for (i = 0; i < len; i += 4) {
387 /* Read fifo must not be empty before all bytes are read */
388 ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
389 val, !(val & GEN_PLD_R_EMPTY),
390 CMD_PKT_STATUS_TIMEOUT_US);
391 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400392 dev_err(dsi->dsi_host.dev,
393 "Read payload FIFO is empty\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200394 return ret;
395 }
396
397 val = dsi_read(dsi, DSI_GEN_PLD_DATA);
398 for (j = 0; j < 4 && j + i < len; j++)
399 buf[i + j] = val >> (8 * j);
400 }
401
402 return ret;
403}
404
405static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
406 const struct mipi_dsi_msg *msg)
407{
408 struct dw_mipi_dsi *dsi = host_to_dsi(host);
409 struct mipi_dsi_packet packet;
410 int ret, nb_bytes;
411
412 ret = mipi_dsi_create_packet(&packet, msg);
413 if (ret) {
Sean Andersonb9b67022020-09-15 10:45:20 -0400414 dev_err(host->dev, "failed to create packet: %d\n", ret);
Yannick Fertré764af462019-10-07 15:29:06 +0200415 return ret;
416 }
417
418 dw_mipi_message_config(dsi, msg);
419
420 ret = dw_mipi_dsi_write(dsi, &packet);
421 if (ret)
422 return ret;
423
424 if (msg->rx_buf && msg->rx_len) {
425 ret = dw_mipi_dsi_read(dsi, msg);
426 if (ret)
427 return ret;
428 nb_bytes = msg->rx_len;
429 } else {
430 nb_bytes = packet.size;
431 }
432
433 return nb_bytes;
434}
435
436static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
437 .attach = dw_mipi_dsi_host_attach,
438 .transfer = dw_mipi_dsi_host_transfer,
439};
440
441static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
442{
443 struct mipi_dsi_device *device = dsi->device;
444 u32 val;
445
446 /*
447 * TODO dw drv improvements
448 * enabling low power is panel-dependent, we should use the
449 * panel configuration here...
450 */
451 val = ENABLE_LOW_POWER;
452
453 if (device->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
454 val |= VID_MODE_TYPE_BURST;
455 else if (device->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
456 val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
457 else
458 val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
459
460 dsi_write(dsi, DSI_VID_MODE_CFG, val);
461}
462
463static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
464 unsigned long mode_flags)
465{
466 const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
467
468 dsi_write(dsi, DSI_PWR_UP, RESET);
469
470 if (mode_flags & MIPI_DSI_MODE_VIDEO) {
471 dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
472 dw_mipi_dsi_video_mode_config(dsi);
473 dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
474 } else {
475 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
476 }
477
478 if (phy_ops->post_set_mode)
479 phy_ops->post_set_mode(dsi->device, mode_flags);
480
481 dsi_write(dsi, DSI_PWR_UP, POWERUP);
482}
483
484static void dw_mipi_dsi_init_pll(struct dw_mipi_dsi *dsi)
485{
Neil Armstrongc73372f2020-10-02 11:16:09 +0200486 const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
487 unsigned int esc_rate;
488 u32 esc_clk_division;
489
Yannick Fertré764af462019-10-07 15:29:06 +0200490 /*
491 * The maximum permitted escape clock is 20MHz and it is derived from
Neil Armstrongc73372f2020-10-02 11:16:09 +0200492 * lanebyteclk, which is running at "lane_mbps / 8".
493 */
494 if (phy_ops->get_esc_clk_rate)
495 phy_ops->get_esc_clk_rate(dsi->device, &esc_rate);
496 else
497 esc_rate = 20; /* Default to 20MHz */
498
499 /*
500 * We want:
Yannick Fertré764af462019-10-07 15:29:06 +0200501 *
Neil Armstrongc73372f2020-10-02 11:16:09 +0200502 * (lane_mbps >> 3) / esc_clk_division < X
Yannick Fertré764af462019-10-07 15:29:06 +0200503 * which is:
Neil Armstrongc73372f2020-10-02 11:16:09 +0200504 * (lane_mbps >> 3) / X > esc_clk_division
Yannick Fertré764af462019-10-07 15:29:06 +0200505 */
Neil Armstrongc73372f2020-10-02 11:16:09 +0200506 esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
Yannick Fertré764af462019-10-07 15:29:06 +0200507
508 dsi_write(dsi, DSI_PWR_UP, RESET);
509
510 /*
511 * TODO dw drv improvements
512 * timeout clock division should be computed with the
513 * high speed transmission counter timeout and byte lane...
514 */
515 dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
516 TX_ESC_CLK_DIVISION(esc_clk_division));
517}
518
519static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
520 struct display_timing *timings)
521{
522 struct mipi_dsi_device *device = dsi->device;
523 u32 val = 0, color = 0;
524
525 switch (device->format) {
526 case MIPI_DSI_FMT_RGB888:
527 color = DPI_COLOR_CODING_24BIT;
528 break;
529 case MIPI_DSI_FMT_RGB666:
530 color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
531 break;
532 case MIPI_DSI_FMT_RGB666_PACKED:
533 color = DPI_COLOR_CODING_18BIT_1;
534 break;
535 case MIPI_DSI_FMT_RGB565:
536 color = DPI_COLOR_CODING_16BIT_1;
537 break;
538 }
539
Ondrej Jirmanfc0eb7d2023-05-22 23:47:02 +0200540 if (timings->flags & DISPLAY_FLAGS_VSYNC_LOW)
Yannick Fertré764af462019-10-07 15:29:06 +0200541 val |= VSYNC_ACTIVE_LOW;
Ondrej Jirmanfc0eb7d2023-05-22 23:47:02 +0200542 if (timings->flags & DISPLAY_FLAGS_HSYNC_LOW)
Yannick Fertré764af462019-10-07 15:29:06 +0200543 val |= HSYNC_ACTIVE_LOW;
544
545 dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
546 dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
547 dsi_write(dsi, DSI_DPI_CFG_POL, val);
548 /*
549 * TODO dw drv improvements
550 * largest packet sizes during hfp or during vsa/vpb/vfp
551 * should be computed according to byte lane, lane number and only
552 * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
553 */
554 dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
555 | INVACT_LPCMD_TIME(4));
556}
557
558static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
559{
560 dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
561}
562
563static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
564 struct display_timing *timings)
565{
566 /*
567 * TODO dw drv improvements
568 * only burst mode is supported here. For non-burst video modes,
569 * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
570 * DSI_VNPCR.NPSIZE... especially because this driver supports
571 * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
572 */
573 dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(timings->hactive.typ));
574}
575
576static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
577{
578 const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
579
580 /*
581 * TODO dw drv improvements
582 * compute high speed transmission counter timeout according
583 * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
584 */
585 dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
586 /*
587 * TODO dw drv improvements
588 * the Bus-Turn-Around Timeout Counter should be computed
589 * according to byte lane...
590 */
591 dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
592 dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
593
594 if (phy_ops->post_set_mode)
595 phy_ops->post_set_mode(dsi->device, 0);
596}
597
598/* Get lane byte clock cycles. */
599static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
600 struct display_timing *timings,
601 u32 hcomponent)
602{
603 u32 frac, lbcc;
604
605 lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
606
607 frac = lbcc % (timings->pixelclock.typ / 1000);
608 lbcc = lbcc / (timings->pixelclock.typ / 1000);
609 if (frac)
610 lbcc++;
611
612 return lbcc;
613}
614
615static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
616 struct display_timing *timings)
617{
618 u32 htotal, hsa, hbp, lbcc;
619
620 htotal = timings->hactive.typ + timings->hfront_porch.typ +
621 timings->hback_porch.typ + timings->hsync_len.typ;
622
John Keeping0c970992022-09-07 12:06:32 +0100623 hsa = timings->hsync_len.typ;
624 hbp = timings->hback_porch.typ;
Yannick Fertré764af462019-10-07 15:29:06 +0200625
626 /*
627 * TODO dw drv improvements
628 * computations below may be improved...
629 */
630 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, htotal);
631 dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
632
633 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, hsa);
634 dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
635
636 lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, timings, hbp);
637 dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
638}
639
640static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
641 struct display_timing *timings)
642{
643 u32 vactive, vsa, vfp, vbp;
644
645 vactive = timings->vactive.typ;
John Keeping0c970992022-09-07 12:06:32 +0100646 vsa = timings->vsync_len.typ;
Yannick Fertré764af462019-10-07 15:29:06 +0200647 vfp = timings->vfront_porch.typ;
John Keeping0c970992022-09-07 12:06:32 +0100648 vbp = timings->vback_porch.typ;
Yannick Fertré764af462019-10-07 15:29:06 +0200649
650 dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
651 dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
652 dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
653 dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
654}
655
656static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
657{
Neil Armstronga61f0e52020-10-02 11:16:08 +0200658 const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
659 struct mipi_dsi_phy_timing timing = {0x40, 0x40, 0x40, 0x40};
Yannick Fertré764af462019-10-07 15:29:06 +0200660 u32 hw_version;
661
Neil Armstronga61f0e52020-10-02 11:16:08 +0200662 if (phy_ops->get_timing)
663 phy_ops->get_timing(dsi->device, dsi->lane_mbps, &timing);
664
Yannick Fertré764af462019-10-07 15:29:06 +0200665 /*
666 * TODO dw drv improvements
667 * data & clock lane timers should be computed according to panel
668 * blankings and to the automatic clock lane control mode...
669 * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
670 * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
671 */
672
673 hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
674
675 if (hw_version >= HWVER_131) {
Neil Armstronga61f0e52020-10-02 11:16:08 +0200676 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
677 PHY_LP2HS_TIME_V131(timing.data_lp2hs));
Yannick Fertré764af462019-10-07 15:29:06 +0200678 dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
679 } else {
Neil Armstronga61f0e52020-10-02 11:16:08 +0200680 dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(timing.data_hs2lp) |
681 PHY_LP2HS_TIME(timing.data_lp2hs) | MAX_RD_TIME(10000));
Yannick Fertré764af462019-10-07 15:29:06 +0200682 }
683
Neil Armstronga61f0e52020-10-02 11:16:08 +0200684 dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(timing.clk_hs2lp)
685 | PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
Yannick Fertré764af462019-10-07 15:29:06 +0200686}
687
688static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
689{
690 struct mipi_dsi_device *device = dsi->device;
691
692 /*
693 * TODO dw drv improvements
694 * stop wait time should be the maximum between host dsi
695 * and panel stop wait times
696 */
697 dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
698 N_LANES(device->lanes));
699}
700
701static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
702{
703 /* Clear PHY state */
704 dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
705 | PHY_RSTZ | PHY_SHUTDOWNZ);
706 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
707 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
708 dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
709}
710
711static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
712{
713 u32 val;
714 int ret;
715
716 dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
717 PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
718
719 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
720 val & PHY_LOCK, PHY_STATUS_TIMEOUT_US);
721 if (ret)
Yannick Fertrebe125e72021-03-04 13:14:36 +0100722 dev_dbg(dsi->dsi_host.dev,
723 "failed to wait phy lock state\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200724
725 ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
726 val, val & PHY_STOP_STATE_CLK_LANE,
727 PHY_STATUS_TIMEOUT_US);
728 if (ret)
Yannick Fertrebe125e72021-03-04 13:14:36 +0100729 dev_dbg(dsi->dsi_host.dev,
730 "failed to wait phy clk lane stop state\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200731}
732
733static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
734{
735 dsi_read(dsi, DSI_INT_ST0);
736 dsi_read(dsi, DSI_INT_ST1);
737 dsi_write(dsi, DSI_INT_MSK0, 0);
738 dsi_write(dsi, DSI_INT_MSK1, 0);
739}
740
741static void dw_mipi_dsi_bridge_set(struct dw_mipi_dsi *dsi,
742 struct display_timing *timings)
743{
744 const struct mipi_dsi_phy_ops *phy_ops = dsi->phy_ops;
745 struct mipi_dsi_device *device = dsi->device;
746 int ret;
747
748 ret = phy_ops->get_lane_mbps(dsi->device, timings, device->lanes,
749 device->format, &dsi->lane_mbps);
750 if (ret)
Sean Andersonb9b67022020-09-15 10:45:20 -0400751 dev_warn(dsi->dsi_host.dev, "Phy get_lane_mbps() failed\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200752
753 dw_mipi_dsi_init_pll(dsi);
754 dw_mipi_dsi_dpi_config(dsi, timings);
755 dw_mipi_dsi_packet_handler_config(dsi);
756 dw_mipi_dsi_video_mode_config(dsi);
757 dw_mipi_dsi_video_packet_config(dsi, timings);
758 dw_mipi_dsi_command_mode_config(dsi);
759 dw_mipi_dsi_line_timer_config(dsi, timings);
760 dw_mipi_dsi_vertical_timing_config(dsi, timings);
761
762 dw_mipi_dsi_dphy_init(dsi);
763 dw_mipi_dsi_dphy_timing_config(dsi);
764 dw_mipi_dsi_dphy_interface_config(dsi);
765
766 dw_mipi_dsi_clear_err(dsi);
767
768 ret = phy_ops->init(dsi->device);
769 if (ret)
Sean Andersonb9b67022020-09-15 10:45:20 -0400770 dev_warn(dsi->dsi_host.dev, "Phy init() failed\n");
Yannick Fertré764af462019-10-07 15:29:06 +0200771
772 dw_mipi_dsi_dphy_enable(dsi);
773
774 dw_mipi_dsi_wait_for_two_frames(timings);
775
776 /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
777 dw_mipi_dsi_set_mode(dsi, 0);
778}
779
780static int dw_mipi_dsi_init(struct udevice *dev,
781 struct mipi_dsi_device *device,
782 struct display_timing *timings,
783 unsigned int max_data_lanes,
784 const struct mipi_dsi_phy_ops *phy_ops)
785{
786 struct dw_mipi_dsi *dsi = dev_get_priv(dev);
787 struct clk clk;
788 int ret;
789
790 if (!phy_ops->init || !phy_ops->get_lane_mbps) {
791 dev_err(device->dev, "Phy not properly configured\n");
792 return -ENODEV;
793 }
794
795 dsi->phy_ops = phy_ops;
796 dsi->max_data_lanes = max_data_lanes;
797 dsi->device = device;
Yannick Fertre30ef11e2021-03-04 13:14:35 +0100798 dsi->dsi_host.dev = (struct device *)dev;
Yannick Fertré764af462019-10-07 15:29:06 +0200799 dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
800 device->host = &dsi->dsi_host;
801
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100802 dsi->base = dev_read_addr_ptr(device->dev);
803 if (!dsi->base) {
Yannick Fertré764af462019-10-07 15:29:06 +0200804 dev_err(device->dev, "dsi dt register address error\n");
805 return -EINVAL;
806 }
807
Chris Morgan8895dd32023-03-24 13:53:07 -0500808 /*
809 * The Rockchip based devices don't have px_clk, so simply move
810 * on.
811 */
812 if (IS_ENABLED(CONFIG_DISPLAY_ROCKCHIP_DW_MIPI)) {
813 dw_mipi_dsi_bridge_set(dsi, timings);
814 return 0;
815 }
816
Yannick Fertré764af462019-10-07 15:29:06 +0200817 ret = clk_get_by_name(device->dev, "px_clk", &clk);
818 if (ret) {
819 dev_err(device->dev, "peripheral clock get error %d\n", ret);
820 return ret;
821 }
822
823 /* get the pixel clock set by the clock framework */
824 timings->pixelclock.typ = clk_get_rate(&clk);
825
826 dw_mipi_dsi_bridge_set(dsi, timings);
827
828 return 0;
829}
830
831static int dw_mipi_dsi_enable(struct udevice *dev)
832{
833 struct dw_mipi_dsi *dsi = dev_get_priv(dev);
834
835 /* Switch to video mode for panel-bridge enable & panel enable */
836 dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
837
838 return 0;
839}
840
841struct dsi_host_ops dw_mipi_dsi_ops = {
842 .init = dw_mipi_dsi_init,
843 .enable = dw_mipi_dsi_enable,
844};
845
846static int dw_mipi_dsi_probe(struct udevice *dev)
847{
848 return 0;
849}
850
Yannick Fertré764af462019-10-07 15:29:06 +0200851U_BOOT_DRIVER(dw_mipi_dsi) = {
852 .name = "dw_mipi_dsi",
853 .id = UCLASS_DSI_HOST,
Yannick Fertré764af462019-10-07 15:29:06 +0200854 .probe = dw_mipi_dsi_probe,
855 .ops = &dw_mipi_dsi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700856 .priv_auto = sizeof(struct dw_mipi_dsi),
Yannick Fertré764af462019-10-07 15:29:06 +0200857};
858
859MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
860MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
861MODULE_AUTHOR("Yannick Fertré <yannick.fertre@st.com>");
862MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
863MODULE_LICENSE("GPL");
864MODULE_ALIAS("platform:dw-mipi-dsi");