blob: 579556369555cf5759b5d2030bfcc5e5b7c8c192 [file] [log] [blame]
CK Hu2e54c142016-01-04 18:36:35 +01001/*
2 * Copyright (c) 2015 MediaTek Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <drm/drmP.h>
15#include <drm/drm_atomic_helper.h>
16#include <drm/drm_crtc_helper.h>
17#include <drm/drm_mipi_dsi.h>
18#include <drm/drm_panel.h>
19#include <linux/clk.h>
20#include <linux/component.h>
shaoming chendd5080a2017-03-31 19:30:35 +080021#include <linux/irq.h>
CK Hu2e54c142016-01-04 18:36:35 +010022#include <linux/of.h>
23#include <linux/of_platform.h>
24#include <linux/of_graph.h>
25#include <linux/phy/phy.h>
26#include <linux/platform_device.h>
27#include <video/videomode.h>
28
29#include "mtk_drm_ddp_comp.h"
30
CK Hu2e54c142016-01-04 18:36:35 +010031#define DSI_START 0x00
32
shaoming chendd5080a2017-03-31 19:30:35 +080033#define DSI_INTEN 0x08
34
35#define DSI_INTSTA 0x0c
36#define LPRX_RD_RDY_INT_FLAG BIT(0)
37#define CMD_DONE_INT_FLAG BIT(1)
38#define TE_RDY_INT_FLAG BIT(2)
39#define VM_DONE_INT_FLAG BIT(3)
40#define EXT_TE_RDY_INT_FLAG BIT(4)
41#define DSI_BUSY BIT(31)
42
CK Hu2e54c142016-01-04 18:36:35 +010043#define DSI_CON_CTRL 0x10
44#define DSI_RESET BIT(0)
45#define DSI_EN BIT(1)
46
47#define DSI_MODE_CTRL 0x14
48#define MODE (3)
49#define CMD_MODE 0
50#define SYNC_PULSE_MODE 1
51#define SYNC_EVENT_MODE 2
52#define BURST_MODE 3
53#define FRM_MODE BIT(16)
54#define MIX_MODE BIT(17)
55
56#define DSI_TXRX_CTRL 0x18
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +080057#define VC_NUM BIT(1)
CK Hu2e54c142016-01-04 18:36:35 +010058#define LANE_NUM (0xf << 2)
59#define DIS_EOT BIT(6)
60#define NULL_EN BIT(7)
61#define TE_FREERUN BIT(8)
62#define EXT_TE_EN BIT(9)
63#define EXT_TE_EDGE BIT(10)
64#define MAX_RTN_SIZE (0xf << 12)
65#define HSTX_CKLP_EN BIT(16)
66
67#define DSI_PSCTRL 0x1c
68#define DSI_PS_WC 0x3fff
69#define DSI_PS_SEL (3 << 16)
70#define PACKED_PS_16BIT_RGB565 (0 << 16)
71#define LOOSELY_PS_18BIT_RGB666 (1 << 16)
72#define PACKED_PS_18BIT_RGB666 (2 << 16)
73#define PACKED_PS_24BIT_RGB888 (3 << 16)
74
75#define DSI_VSA_NL 0x20
76#define DSI_VBP_NL 0x24
77#define DSI_VFP_NL 0x28
78#define DSI_VACT_NL 0x2C
79#define DSI_HSA_WC 0x50
80#define DSI_HBP_WC 0x54
81#define DSI_HFP_WC 0x58
82
83#define DSI_HSTX_CKL_WC 0x64
84
shaoming chendd5080a2017-03-31 19:30:35 +080085#define DSI_RACK 0x84
86#define RACK BIT(0)
87
CK Hu2e54c142016-01-04 18:36:35 +010088#define DSI_PHY_LCCON 0x104
89#define LC_HS_TX_EN BIT(0)
90#define LC_ULPM_EN BIT(1)
91#define LC_WAKEUP_EN BIT(2)
92
93#define DSI_PHY_LD0CON 0x108
94#define LD0_HS_TX_EN BIT(0)
95#define LD0_ULPM_EN BIT(1)
96#define LD0_WAKEUP_EN BIT(2)
97
98#define DSI_PHY_TIMECON0 0x110
99#define LPX (0xff << 0)
Jitao Shif6c87232016-11-16 11:20:54 +0800100#define HS_PREP (0xff << 8)
CK Hu2e54c142016-01-04 18:36:35 +0100101#define HS_ZERO (0xff << 16)
102#define HS_TRAIL (0xff << 24)
103
104#define DSI_PHY_TIMECON1 0x114
105#define TA_GO (0xff << 0)
106#define TA_SURE (0xff << 8)
107#define TA_GET (0xff << 16)
108#define DA_HS_EXIT (0xff << 24)
109
110#define DSI_PHY_TIMECON2 0x118
111#define CONT_DET (0xff << 0)
112#define CLK_ZERO (0xff << 16)
113#define CLK_TRAIL (0xff << 24)
114
115#define DSI_PHY_TIMECON3 0x11c
Jitao Shif6c87232016-11-16 11:20:54 +0800116#define CLK_HS_PREP (0xff << 0)
CK Hu2e54c142016-01-04 18:36:35 +0100117#define CLK_HS_POST (0xff << 8)
118#define CLK_HS_EXIT (0xff << 16)
119
Jitao Shif6c87232016-11-16 11:20:54 +0800120#define T_LPX 5
121#define T_HS_PREP 6
122#define T_HS_TRAIL 8
123#define T_HS_EXIT 7
124#define T_HS_ZERO 10
125
CK Hu2e54c142016-01-04 18:36:35 +0100126#define NS_TO_CYCLE(n, c) ((n) / (c) + (((n) % (c)) ? 1 : 0))
127
128struct phy;
129
130struct mtk_dsi {
131 struct mtk_ddp_comp ddp_comp;
132 struct device *dev;
133 struct mipi_dsi_host host;
134 struct drm_encoder encoder;
135 struct drm_connector conn;
136 struct drm_panel *panel;
137 struct drm_bridge *bridge;
138 struct phy *phy;
139
140 void __iomem *regs;
141
142 struct clk *engine_clk;
143 struct clk *digital_clk;
144 struct clk *hs_clk;
145
146 u32 data_rate;
147
148 unsigned long mode_flags;
149 enum mipi_dsi_pixel_format format;
150 unsigned int lanes;
151 struct videomode vm;
152 int refcount;
153 bool enabled;
shaoming chendd5080a2017-03-31 19:30:35 +0800154 u32 irq_data;
155 wait_queue_head_t irq_wait_queue;
CK Hu2e54c142016-01-04 18:36:35 +0100156};
157
158static inline struct mtk_dsi *encoder_to_dsi(struct drm_encoder *e)
159{
160 return container_of(e, struct mtk_dsi, encoder);
161}
162
163static inline struct mtk_dsi *connector_to_dsi(struct drm_connector *c)
164{
165 return container_of(c, struct mtk_dsi, conn);
166}
167
168static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
169{
170 return container_of(h, struct mtk_dsi, host);
171}
172
173static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
174{
175 u32 temp = readl(dsi->regs + offset);
176
177 writel((temp & ~mask) | (data & mask), dsi->regs + offset);
178}
179
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800180static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100181{
182 u32 timcon0, timcon1, timcon2, timcon3;
Jitao Shif6c87232016-11-16 11:20:54 +0800183 u32 ui, cycle_time;
CK Hu2e54c142016-01-04 18:36:35 +0100184
185 ui = 1000 / dsi->data_rate + 0x01;
186 cycle_time = 8000 / dsi->data_rate + 0x01;
CK Hu2e54c142016-01-04 18:36:35 +0100187
Jitao Shif6c87232016-11-16 11:20:54 +0800188 timcon0 = T_LPX | T_HS_PREP << 8 | T_HS_ZERO << 16 | T_HS_TRAIL << 24;
189 timcon1 = 4 * T_LPX | (3 * T_LPX / 2) << 8 | 5 * T_LPX << 16 |
190 T_HS_EXIT << 24;
CK Hu2e54c142016-01-04 18:36:35 +0100191 timcon2 = ((NS_TO_CYCLE(0x64, cycle_time) + 0xa) << 24) |
192 (NS_TO_CYCLE(0x150, cycle_time) << 16);
Jitao Shif6c87232016-11-16 11:20:54 +0800193 timcon3 = NS_TO_CYCLE(0x40, cycle_time) | (2 * T_LPX) << 16 |
194 NS_TO_CYCLE(80 + 52 * ui, cycle_time) << 8;
CK Hu2e54c142016-01-04 18:36:35 +0100195
196 writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
197 writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
198 writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
199 writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
200}
201
202static void mtk_dsi_enable(struct mtk_dsi *dsi)
203{
204 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
205}
206
207static void mtk_dsi_disable(struct mtk_dsi *dsi)
208{
209 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
210}
211
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800212static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100213{
214 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
215 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
216}
217
218static int mtk_dsi_poweron(struct mtk_dsi *dsi)
219{
220 struct device *dev = dsi->dev;
221 int ret;
Jitao Shif6c87232016-11-16 11:20:54 +0800222 u64 pixel_clock, total_bits;
223 u32 htotal, htotal_bits, bit_per_pixel, overhead_cycles, overhead_bits;
CK Hu2e54c142016-01-04 18:36:35 +0100224
225 if (++dsi->refcount != 1)
226 return 0;
227
Jitao Shif6c87232016-11-16 11:20:54 +0800228 switch (dsi->format) {
229 case MIPI_DSI_FMT_RGB565:
230 bit_per_pixel = 16;
231 break;
232 case MIPI_DSI_FMT_RGB666_PACKED:
233 bit_per_pixel = 18;
234 break;
235 case MIPI_DSI_FMT_RGB666:
236 case MIPI_DSI_FMT_RGB888:
237 default:
238 bit_per_pixel = 24;
239 break;
240 }
CK Hu2e54c142016-01-04 18:36:35 +0100241
Jitao Shif6c87232016-11-16 11:20:54 +0800242 /**
243 * vm.pixelclock is in kHz, pixel_clock unit is Hz, so multiply by 1000
244 * htotal_time = htotal * byte_per_pixel / num_lanes
245 * overhead_time = lpx + hs_prepare + hs_zero + hs_trail + hs_exit
246 * mipi_ratio = (htotal_time + overhead_time) / htotal_time
247 * data_rate = pixel_clock * bit_per_pixel * mipi_ratio / num_lanes;
248 */
249 pixel_clock = dsi->vm.pixelclock * 1000;
250 htotal = dsi->vm.hactive + dsi->vm.hback_porch + dsi->vm.hfront_porch +
251 dsi->vm.hsync_len;
252 htotal_bits = htotal * bit_per_pixel;
253
254 overhead_cycles = T_LPX + T_HS_PREP + T_HS_ZERO + T_HS_TRAIL +
255 T_HS_EXIT;
256 overhead_bits = overhead_cycles * dsi->lanes * 8;
257 total_bits = htotal_bits + overhead_bits;
258
259 dsi->data_rate = DIV_ROUND_UP_ULL(pixel_clock * total_bits,
260 htotal * dsi->lanes);
261
262 ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
CK Hu2e54c142016-01-04 18:36:35 +0100263 if (ret < 0) {
264 dev_err(dev, "Failed to set data rate: %d\n", ret);
265 goto err_refcount;
266 }
267
268 phy_power_on(dsi->phy);
269
270 ret = clk_prepare_enable(dsi->engine_clk);
271 if (ret < 0) {
272 dev_err(dev, "Failed to enable engine clock: %d\n", ret);
273 goto err_phy_power_off;
274 }
275
276 ret = clk_prepare_enable(dsi->digital_clk);
277 if (ret < 0) {
278 dev_err(dev, "Failed to enable digital clock: %d\n", ret);
279 goto err_disable_engine_clk;
280 }
281
282 mtk_dsi_enable(dsi);
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800283 mtk_dsi_reset_engine(dsi);
284 mtk_dsi_phy_timconfig(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100285
286 return 0;
287
288err_disable_engine_clk:
289 clk_disable_unprepare(dsi->engine_clk);
290err_phy_power_off:
291 phy_power_off(dsi->phy);
292err_refcount:
293 dsi->refcount--;
294 return ret;
295}
296
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800297static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100298{
299 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
300 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
301}
302
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800303static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100304{
305 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
306 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
307 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
308}
309
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800310static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100311{
312 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
313 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
314}
315
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800316static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100317{
318 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
319 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
320 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
321}
322
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800323static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100324{
325 u32 tmp_reg1;
326
327 tmp_reg1 = readl(dsi->regs + DSI_PHY_LCCON);
328 return ((tmp_reg1 & LC_HS_TX_EN) == 1) ? true : false;
329}
330
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800331static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
CK Hu2e54c142016-01-04 18:36:35 +0100332{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800333 if (enter && !mtk_dsi_clk_hs_state(dsi))
CK Hu2e54c142016-01-04 18:36:35 +0100334 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800335 else if (!enter && mtk_dsi_clk_hs_state(dsi))
CK Hu2e54c142016-01-04 18:36:35 +0100336 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
337}
338
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800339static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100340{
341 u32 vid_mode = CMD_MODE;
342
343 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
344 vid_mode = SYNC_PULSE_MODE;
345
346 if ((dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) &&
347 !(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE))
348 vid_mode = BURST_MODE;
349 }
350
351 writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
352}
353
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800354static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100355{
356 struct videomode *vm = &dsi->vm;
357 u32 dsi_buf_bpp, ps_wc;
358 u32 ps_bpp_mode;
359
360 if (dsi->format == MIPI_DSI_FMT_RGB565)
361 dsi_buf_bpp = 2;
362 else
363 dsi_buf_bpp = 3;
364
365 ps_wc = vm->hactive * dsi_buf_bpp;
366 ps_bpp_mode = ps_wc;
367
368 switch (dsi->format) {
369 case MIPI_DSI_FMT_RGB888:
370 ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
371 break;
372 case MIPI_DSI_FMT_RGB666:
373 ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
374 break;
375 case MIPI_DSI_FMT_RGB666_PACKED:
376 ps_bpp_mode |= LOOSELY_PS_18BIT_RGB666;
377 break;
378 case MIPI_DSI_FMT_RGB565:
379 ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
380 break;
381 }
382
383 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
384 writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
385 writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
386}
387
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800388static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100389{
390 u32 tmp_reg;
391
392 switch (dsi->lanes) {
393 case 1:
394 tmp_reg = 1 << 2;
395 break;
396 case 2:
397 tmp_reg = 3 << 2;
398 break;
399 case 3:
400 tmp_reg = 7 << 2;
401 break;
402 case 4:
403 tmp_reg = 0xf << 2;
404 break;
405 default:
406 tmp_reg = 0xf << 2;
407 break;
408 }
409
410 writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
411}
412
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800413static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100414{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800415 u32 dsi_tmp_buf_bpp;
CK Hu2e54c142016-01-04 18:36:35 +0100416 u32 tmp_reg;
417
418 switch (dsi->format) {
419 case MIPI_DSI_FMT_RGB888:
420 tmp_reg = PACKED_PS_24BIT_RGB888;
421 dsi_tmp_buf_bpp = 3;
422 break;
423 case MIPI_DSI_FMT_RGB666:
424 tmp_reg = LOOSELY_PS_18BIT_RGB666;
425 dsi_tmp_buf_bpp = 3;
426 break;
427 case MIPI_DSI_FMT_RGB666_PACKED:
428 tmp_reg = PACKED_PS_18BIT_RGB666;
429 dsi_tmp_buf_bpp = 3;
430 break;
431 case MIPI_DSI_FMT_RGB565:
432 tmp_reg = PACKED_PS_16BIT_RGB565;
433 dsi_tmp_buf_bpp = 2;
434 break;
435 default:
436 tmp_reg = PACKED_PS_24BIT_RGB888;
437 dsi_tmp_buf_bpp = 3;
438 break;
439 }
440
441 tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
442 writel(tmp_reg, dsi->regs + DSI_PSCTRL);
443}
444
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800445static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100446{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800447 u32 horizontal_sync_active_byte;
448 u32 horizontal_backporch_byte;
449 u32 horizontal_frontporch_byte;
450 u32 dsi_tmp_buf_bpp;
CK Hu2e54c142016-01-04 18:36:35 +0100451
452 struct videomode *vm = &dsi->vm;
453
454 if (dsi->format == MIPI_DSI_FMT_RGB565)
455 dsi_tmp_buf_bpp = 2;
456 else
457 dsi_tmp_buf_bpp = 3;
458
459 writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
460 writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
461 writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
462 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
463
464 horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);
465
466 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
467 horizontal_backporch_byte =
468 (vm->hback_porch * dsi_tmp_buf_bpp - 10);
469 else
470 horizontal_backporch_byte = ((vm->hback_porch + vm->hsync_len) *
471 dsi_tmp_buf_bpp - 10);
472
473 horizontal_frontporch_byte = (vm->hfront_porch * dsi_tmp_buf_bpp - 12);
474
475 writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
476 writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
477 writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);
478
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800479 mtk_dsi_ps_control(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100480}
481
482static void mtk_dsi_start(struct mtk_dsi *dsi)
483{
484 writel(0, dsi->regs + DSI_START);
485 writel(1, dsi->regs + DSI_START);
486}
487
shaoming chendd5080a2017-03-31 19:30:35 +0800488static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
489{
490 u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
491
492 writel(inten, dsi->regs + DSI_INTEN);
493}
494
495static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
496{
497 dsi->irq_data |= irq_bit;
498}
499
500static __maybe_unused void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
501{
502 dsi->irq_data &= ~irq_bit;
503}
504
505static __maybe_unused s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
506 unsigned int timeout)
507{
508 s32 ret = 0;
509 unsigned long jiffies = msecs_to_jiffies(timeout);
510
511 ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
512 dsi->irq_data & irq_flag,
513 jiffies);
514 if (ret == 0) {
515 DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
516
517 mtk_dsi_enable(dsi);
518 mtk_dsi_reset_engine(dsi);
519 }
520
521 return ret;
522}
523
524static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
525{
526 struct mtk_dsi *dsi = dev_id;
527 u32 status, tmp;
528 u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
529
530 status = readl(dsi->regs + DSI_INTSTA) & flag;
531
532 if (status) {
533 do {
534 mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
535 tmp = readl(dsi->regs + DSI_INTSTA);
536 } while (tmp & DSI_BUSY);
537
538 mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
539 mtk_dsi_irq_data_set(dsi, status);
540 wake_up_interruptible(&dsi->irq_wait_queue);
541 }
542
543 return IRQ_HANDLED;
544}
545
CK Hu2e54c142016-01-04 18:36:35 +0100546static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
547{
548 if (WARN_ON(dsi->refcount == 0))
549 return;
550
551 if (--dsi->refcount != 0)
552 return;
553
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800554 mtk_dsi_lane0_ulp_mode_enter(dsi);
555 mtk_dsi_clk_ulp_mode_enter(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100556
557 mtk_dsi_disable(dsi);
558
559 clk_disable_unprepare(dsi->engine_clk);
560 clk_disable_unprepare(dsi->digital_clk);
561
562 phy_power_off(dsi->phy);
563}
564
565static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
566{
567 int ret;
568
569 if (dsi->enabled)
570 return;
571
572 if (dsi->panel) {
573 if (drm_panel_prepare(dsi->panel)) {
574 DRM_ERROR("failed to setup the panel\n");
575 return;
576 }
577 }
578
579 ret = mtk_dsi_poweron(dsi);
580 if (ret < 0) {
581 DRM_ERROR("failed to power on dsi\n");
582 return;
583 }
584
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800585 mtk_dsi_rxtx_control(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100586
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800587 mtk_dsi_clk_ulp_mode_leave(dsi);
588 mtk_dsi_lane0_ulp_mode_leave(dsi);
589 mtk_dsi_clk_hs_mode(dsi, 0);
590 mtk_dsi_set_mode(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100591
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800592 mtk_dsi_ps_control_vact(dsi);
593 mtk_dsi_config_vdo_timing(dsi);
shaoming chendd5080a2017-03-31 19:30:35 +0800594 mtk_dsi_set_interrupt_enable(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100595
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800596 mtk_dsi_set_mode(dsi);
597 mtk_dsi_clk_hs_mode(dsi, 1);
CK Hu2e54c142016-01-04 18:36:35 +0100598
599 mtk_dsi_start(dsi);
600
601 dsi->enabled = true;
602}
603
604static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
605{
606 if (!dsi->enabled)
607 return;
608
609 if (dsi->panel) {
610 if (drm_panel_disable(dsi->panel)) {
611 DRM_ERROR("failed to disable the panel\n");
612 return;
613 }
614 }
615
616 mtk_dsi_poweroff(dsi);
617
618 dsi->enabled = false;
619}
620
621static void mtk_dsi_encoder_destroy(struct drm_encoder *encoder)
622{
623 drm_encoder_cleanup(encoder);
624}
625
626static const struct drm_encoder_funcs mtk_dsi_encoder_funcs = {
627 .destroy = mtk_dsi_encoder_destroy,
628};
629
630static bool mtk_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
631 const struct drm_display_mode *mode,
632 struct drm_display_mode *adjusted_mode)
633{
634 return true;
635}
636
637static void mtk_dsi_encoder_mode_set(struct drm_encoder *encoder,
638 struct drm_display_mode *mode,
639 struct drm_display_mode *adjusted)
640{
641 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
642
643 dsi->vm.pixelclock = adjusted->clock;
644 dsi->vm.hactive = adjusted->hdisplay;
645 dsi->vm.hback_porch = adjusted->htotal - adjusted->hsync_end;
646 dsi->vm.hfront_porch = adjusted->hsync_start - adjusted->hdisplay;
647 dsi->vm.hsync_len = adjusted->hsync_end - adjusted->hsync_start;
648
649 dsi->vm.vactive = adjusted->vdisplay;
650 dsi->vm.vback_porch = adjusted->vtotal - adjusted->vsync_end;
651 dsi->vm.vfront_porch = adjusted->vsync_start - adjusted->vdisplay;
652 dsi->vm.vsync_len = adjusted->vsync_end - adjusted->vsync_start;
653}
654
655static void mtk_dsi_encoder_disable(struct drm_encoder *encoder)
656{
657 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
658
659 mtk_output_dsi_disable(dsi);
660}
661
662static void mtk_dsi_encoder_enable(struct drm_encoder *encoder)
663{
664 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
665
666 mtk_output_dsi_enable(dsi);
667}
668
CK Hu2e54c142016-01-04 18:36:35 +0100669static int mtk_dsi_connector_get_modes(struct drm_connector *connector)
670{
671 struct mtk_dsi *dsi = connector_to_dsi(connector);
672
673 return drm_panel_get_modes(dsi->panel);
674}
675
CK Hu2e54c142016-01-04 18:36:35 +0100676static const struct drm_encoder_helper_funcs mtk_dsi_encoder_helper_funcs = {
677 .mode_fixup = mtk_dsi_encoder_mode_fixup,
678 .mode_set = mtk_dsi_encoder_mode_set,
679 .disable = mtk_dsi_encoder_disable,
680 .enable = mtk_dsi_encoder_enable,
681};
682
683static const struct drm_connector_funcs mtk_dsi_connector_funcs = {
684 .dpms = drm_atomic_helper_connector_dpms,
CK Hu2e54c142016-01-04 18:36:35 +0100685 .fill_modes = drm_helper_probe_single_connector_modes,
686 .destroy = drm_connector_cleanup,
687 .reset = drm_atomic_helper_connector_reset,
688 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
689 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
690};
691
692static const struct drm_connector_helper_funcs
693 mtk_dsi_connector_helper_funcs = {
694 .get_modes = mtk_dsi_connector_get_modes,
CK Hu2e54c142016-01-04 18:36:35 +0100695};
696
CK Hu2e54c142016-01-04 18:36:35 +0100697static int mtk_dsi_create_connector(struct drm_device *drm, struct mtk_dsi *dsi)
698{
699 int ret;
700
701 ret = drm_connector_init(drm, &dsi->conn, &mtk_dsi_connector_funcs,
702 DRM_MODE_CONNECTOR_DSI);
703 if (ret) {
704 DRM_ERROR("Failed to connector init to drm\n");
705 return ret;
706 }
707
708 drm_connector_helper_add(&dsi->conn, &mtk_dsi_connector_helper_funcs);
709
710 dsi->conn.dpms = DRM_MODE_DPMS_OFF;
711 drm_mode_connector_attach_encoder(&dsi->conn, &dsi->encoder);
712
713 if (dsi->panel) {
714 ret = drm_panel_attach(dsi->panel, &dsi->conn);
715 if (ret) {
716 DRM_ERROR("Failed to attach panel to drm\n");
717 goto err_connector_cleanup;
718 }
719 }
720
721 return 0;
722
723err_connector_cleanup:
724 drm_connector_cleanup(&dsi->conn);
725 return ret;
726}
727
728static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi)
729{
730 int ret;
731
732 ret = drm_encoder_init(drm, &dsi->encoder, &mtk_dsi_encoder_funcs,
733 DRM_MODE_ENCODER_DSI, NULL);
734 if (ret) {
735 DRM_ERROR("Failed to encoder init to drm\n");
736 return ret;
737 }
738 drm_encoder_helper_add(&dsi->encoder, &mtk_dsi_encoder_helper_funcs);
739
740 /*
741 * Currently display data paths are statically assigned to a crtc each.
742 * crtc 0 is OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0
743 */
744 dsi->encoder.possible_crtcs = 1;
745
746 /* If there's a bridge, attach to it and let it create the connector */
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200747 ret = drm_bridge_attach(&dsi->encoder, dsi->bridge, NULL);
CK Hu2e54c142016-01-04 18:36:35 +0100748 if (ret) {
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200749 DRM_ERROR("Failed to attach bridge to drm\n");
750
CK Hu2e54c142016-01-04 18:36:35 +0100751 /* Otherwise create our own connector and attach to a panel */
752 ret = mtk_dsi_create_connector(drm, dsi);
753 if (ret)
754 goto err_encoder_cleanup;
755 }
756
757 return 0;
758
759err_encoder_cleanup:
760 drm_encoder_cleanup(&dsi->encoder);
761 return ret;
762}
763
764static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi)
765{
766 drm_encoder_cleanup(&dsi->encoder);
767 /* Skip connector cleanup if creation was delegated to the bridge */
Philipp Zabel2ea9f312016-05-26 16:08:18 +0200768 if (dsi->conn.dev)
CK Hu2e54c142016-01-04 18:36:35 +0100769 drm_connector_cleanup(&dsi->conn);
CK Hu2e54c142016-01-04 18:36:35 +0100770}
771
772static void mtk_dsi_ddp_start(struct mtk_ddp_comp *comp)
773{
774 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
775
776 mtk_dsi_poweron(dsi);
777}
778
779static void mtk_dsi_ddp_stop(struct mtk_ddp_comp *comp)
780{
781 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
782
783 mtk_dsi_poweroff(dsi);
784}
785
786static const struct mtk_ddp_comp_funcs mtk_dsi_funcs = {
787 .start = mtk_dsi_ddp_start,
788 .stop = mtk_dsi_ddp_stop,
789};
790
791static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
792 struct mipi_dsi_device *device)
793{
794 struct mtk_dsi *dsi = host_to_dsi(host);
795
796 dsi->lanes = device->lanes;
797 dsi->format = device->format;
798 dsi->mode_flags = device->mode_flags;
799
800 if (dsi->conn.dev)
801 drm_helper_hpd_irq_event(dsi->conn.dev);
802
803 return 0;
804}
805
806static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
807 struct mipi_dsi_device *device)
808{
809 struct mtk_dsi *dsi = host_to_dsi(host);
810
811 if (dsi->conn.dev)
812 drm_helper_hpd_irq_event(dsi->conn.dev);
813
814 return 0;
815}
816
817static const struct mipi_dsi_host_ops mtk_dsi_ops = {
818 .attach = mtk_dsi_host_attach,
819 .detach = mtk_dsi_host_detach,
820};
821
822static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
823{
824 int ret;
825 struct drm_device *drm = data;
826 struct mtk_dsi *dsi = dev_get_drvdata(dev);
827
828 ret = mtk_ddp_comp_register(drm, &dsi->ddp_comp);
829 if (ret < 0) {
830 dev_err(dev, "Failed to register component %s: %d\n",
831 dev->of_node->full_name, ret);
832 return ret;
833 }
834
835 ret = mipi_dsi_host_register(&dsi->host);
836 if (ret < 0) {
837 dev_err(dev, "failed to register DSI host: %d\n", ret);
838 goto err_ddp_comp_unregister;
839 }
840
841 ret = mtk_dsi_create_conn_enc(drm, dsi);
842 if (ret) {
843 DRM_ERROR("Encoder create failed with %d\n", ret);
844 goto err_unregister;
845 }
846
847 return 0;
848
849err_unregister:
850 mipi_dsi_host_unregister(&dsi->host);
851err_ddp_comp_unregister:
852 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
853 return ret;
854}
855
856static void mtk_dsi_unbind(struct device *dev, struct device *master,
857 void *data)
858{
859 struct drm_device *drm = data;
860 struct mtk_dsi *dsi = dev_get_drvdata(dev);
861
862 mtk_dsi_destroy_conn_enc(dsi);
863 mipi_dsi_host_unregister(&dsi->host);
864 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
865}
866
867static const struct component_ops mtk_dsi_component_ops = {
868 .bind = mtk_dsi_bind,
869 .unbind = mtk_dsi_unbind,
870};
871
872static int mtk_dsi_probe(struct platform_device *pdev)
873{
874 struct mtk_dsi *dsi;
875 struct device *dev = &pdev->dev;
876 struct device_node *remote_node, *endpoint;
877 struct resource *regs;
shaoming chendd5080a2017-03-31 19:30:35 +0800878 int irq_num;
CK Hu2e54c142016-01-04 18:36:35 +0100879 int comp_id;
880 int ret;
881
882 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
883 if (!dsi)
884 return -ENOMEM;
885
886 dsi->host.ops = &mtk_dsi_ops;
887 dsi->host.dev = dev;
888
889 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
890 if (endpoint) {
891 remote_node = of_graph_get_remote_port_parent(endpoint);
892 if (!remote_node) {
893 dev_err(dev, "No panel connected\n");
894 return -ENODEV;
895 }
896
897 dsi->bridge = of_drm_find_bridge(remote_node);
898 dsi->panel = of_drm_find_panel(remote_node);
899 of_node_put(remote_node);
900 if (!dsi->bridge && !dsi->panel) {
901 dev_info(dev, "Waiting for bridge or panel driver\n");
902 return -EPROBE_DEFER;
903 }
904 }
905
906 dsi->engine_clk = devm_clk_get(dev, "engine");
907 if (IS_ERR(dsi->engine_clk)) {
908 ret = PTR_ERR(dsi->engine_clk);
909 dev_err(dev, "Failed to get engine clock: %d\n", ret);
910 return ret;
911 }
912
913 dsi->digital_clk = devm_clk_get(dev, "digital");
914 if (IS_ERR(dsi->digital_clk)) {
915 ret = PTR_ERR(dsi->digital_clk);
916 dev_err(dev, "Failed to get digital clock: %d\n", ret);
917 return ret;
918 }
919
920 dsi->hs_clk = devm_clk_get(dev, "hs");
921 if (IS_ERR(dsi->hs_clk)) {
922 ret = PTR_ERR(dsi->hs_clk);
923 dev_err(dev, "Failed to get hs clock: %d\n", ret);
924 return ret;
925 }
926
927 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
928 dsi->regs = devm_ioremap_resource(dev, regs);
929 if (IS_ERR(dsi->regs)) {
930 ret = PTR_ERR(dsi->regs);
931 dev_err(dev, "Failed to ioremap memory: %d\n", ret);
932 return ret;
933 }
934
935 dsi->phy = devm_phy_get(dev, "dphy");
936 if (IS_ERR(dsi->phy)) {
937 ret = PTR_ERR(dsi->phy);
938 dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
939 return ret;
940 }
941
942 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DSI);
943 if (comp_id < 0) {
944 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
945 return comp_id;
946 }
947
948 ret = mtk_ddp_comp_init(dev, dev->of_node, &dsi->ddp_comp, comp_id,
949 &mtk_dsi_funcs);
950 if (ret) {
951 dev_err(dev, "Failed to initialize component: %d\n", ret);
952 return ret;
953 }
954
shaoming chendd5080a2017-03-31 19:30:35 +0800955 irq_num = platform_get_irq(pdev, 0);
956 if (irq_num < 0) {
957 dev_err(&pdev->dev, "failed to request dsi irq resource\n");
958 return -EPROBE_DEFER;
959 }
960
961 irq_set_status_flags(irq_num, IRQ_TYPE_LEVEL_LOW);
962 ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
963 IRQF_TRIGGER_LOW, dev_name(&pdev->dev), dsi);
964 if (ret) {
965 dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
966 return -EPROBE_DEFER;
967 }
968
969 init_waitqueue_head(&dsi->irq_wait_queue);
970
CK Hu2e54c142016-01-04 18:36:35 +0100971 platform_set_drvdata(pdev, dsi);
972
973 return component_add(&pdev->dev, &mtk_dsi_component_ops);
974}
975
976static int mtk_dsi_remove(struct platform_device *pdev)
977{
978 struct mtk_dsi *dsi = platform_get_drvdata(pdev);
979
980 mtk_output_dsi_disable(dsi);
981 component_del(&pdev->dev, &mtk_dsi_component_ops);
982
983 return 0;
984}
985
986static const struct of_device_id mtk_dsi_of_match[] = {
987 { .compatible = "mediatek,mt8173-dsi" },
988 { },
989};
990
991struct platform_driver mtk_dsi_driver = {
992 .probe = mtk_dsi_probe,
993 .remove = mtk_dsi_remove,
994 .driver = {
995 .name = "mtk-dsi",
996 .of_match_table = mtk_dsi_of_match,
997 },
998};