blob: 2e2cc3a15cb6e4c76f772d4f6b967084c04e3bc8 [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>
shaoming chen21898812017-03-31 19:30:36 +080027#include <video/mipi_display.h>
CK Hu2e54c142016-01-04 18:36:35 +010028#include <video/videomode.h>
29
30#include "mtk_drm_ddp_comp.h"
31
CK Hu2e54c142016-01-04 18:36:35 +010032#define DSI_START 0x00
33
shaoming chendd5080a2017-03-31 19:30:35 +080034#define DSI_INTEN 0x08
35
36#define DSI_INTSTA 0x0c
37#define LPRX_RD_RDY_INT_FLAG BIT(0)
38#define CMD_DONE_INT_FLAG BIT(1)
39#define TE_RDY_INT_FLAG BIT(2)
40#define VM_DONE_INT_FLAG BIT(3)
41#define EXT_TE_RDY_INT_FLAG BIT(4)
42#define DSI_BUSY BIT(31)
43
CK Hu2e54c142016-01-04 18:36:35 +010044#define DSI_CON_CTRL 0x10
45#define DSI_RESET BIT(0)
46#define DSI_EN BIT(1)
47
48#define DSI_MODE_CTRL 0x14
49#define MODE (3)
50#define CMD_MODE 0
51#define SYNC_PULSE_MODE 1
52#define SYNC_EVENT_MODE 2
53#define BURST_MODE 3
54#define FRM_MODE BIT(16)
55#define MIX_MODE BIT(17)
56
57#define DSI_TXRX_CTRL 0x18
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +080058#define VC_NUM BIT(1)
CK Hu2e54c142016-01-04 18:36:35 +010059#define LANE_NUM (0xf << 2)
60#define DIS_EOT BIT(6)
61#define NULL_EN BIT(7)
62#define TE_FREERUN BIT(8)
63#define EXT_TE_EN BIT(9)
64#define EXT_TE_EDGE BIT(10)
65#define MAX_RTN_SIZE (0xf << 12)
66#define HSTX_CKLP_EN BIT(16)
67
68#define DSI_PSCTRL 0x1c
69#define DSI_PS_WC 0x3fff
70#define DSI_PS_SEL (3 << 16)
71#define PACKED_PS_16BIT_RGB565 (0 << 16)
72#define LOOSELY_PS_18BIT_RGB666 (1 << 16)
73#define PACKED_PS_18BIT_RGB666 (2 << 16)
74#define PACKED_PS_24BIT_RGB888 (3 << 16)
75
76#define DSI_VSA_NL 0x20
77#define DSI_VBP_NL 0x24
78#define DSI_VFP_NL 0x28
79#define DSI_VACT_NL 0x2C
80#define DSI_HSA_WC 0x50
81#define DSI_HBP_WC 0x54
82#define DSI_HFP_WC 0x58
83
shaoming chen21898812017-03-31 19:30:36 +080084#define DSI_CMDQ_SIZE 0x60
85#define CMDQ_SIZE 0x3f
86
CK Hu2e54c142016-01-04 18:36:35 +010087#define DSI_HSTX_CKL_WC 0x64
88
shaoming chen21898812017-03-31 19:30:36 +080089#define DSI_RX_DATA0 0x74
90#define DSI_RX_DATA1 0x78
91#define DSI_RX_DATA2 0x7c
92#define DSI_RX_DATA3 0x80
93
shaoming chendd5080a2017-03-31 19:30:35 +080094#define DSI_RACK 0x84
95#define RACK BIT(0)
96
CK Hu2e54c142016-01-04 18:36:35 +010097#define DSI_PHY_LCCON 0x104
98#define LC_HS_TX_EN BIT(0)
99#define LC_ULPM_EN BIT(1)
100#define LC_WAKEUP_EN BIT(2)
101
102#define DSI_PHY_LD0CON 0x108
103#define LD0_HS_TX_EN BIT(0)
104#define LD0_ULPM_EN BIT(1)
105#define LD0_WAKEUP_EN BIT(2)
106
107#define DSI_PHY_TIMECON0 0x110
108#define LPX (0xff << 0)
Jitao Shif6c87232016-11-16 11:20:54 +0800109#define HS_PREP (0xff << 8)
CK Hu2e54c142016-01-04 18:36:35 +0100110#define HS_ZERO (0xff << 16)
111#define HS_TRAIL (0xff << 24)
112
113#define DSI_PHY_TIMECON1 0x114
114#define TA_GO (0xff << 0)
115#define TA_SURE (0xff << 8)
116#define TA_GET (0xff << 16)
117#define DA_HS_EXIT (0xff << 24)
118
119#define DSI_PHY_TIMECON2 0x118
120#define CONT_DET (0xff << 0)
121#define CLK_ZERO (0xff << 16)
122#define CLK_TRAIL (0xff << 24)
123
124#define DSI_PHY_TIMECON3 0x11c
Jitao Shif6c87232016-11-16 11:20:54 +0800125#define CLK_HS_PREP (0xff << 0)
CK Hu2e54c142016-01-04 18:36:35 +0100126#define CLK_HS_POST (0xff << 8)
127#define CLK_HS_EXIT (0xff << 16)
128
shaoming chen21898812017-03-31 19:30:36 +0800129#define DSI_CMDQ0 0x180
130#define CONFIG (0xff << 0)
131#define SHORT_PACKET 0
132#define LONG_PACKET 2
133#define BTA BIT(2)
134#define DATA_ID (0xff << 8)
135#define DATA_0 (0xff << 16)
136#define DATA_1 (0xff << 24)
137
Jitao Shif6c87232016-11-16 11:20:54 +0800138#define T_LPX 5
139#define T_HS_PREP 6
140#define T_HS_TRAIL 8
141#define T_HS_EXIT 7
142#define T_HS_ZERO 10
143
CK Hu2e54c142016-01-04 18:36:35 +0100144#define NS_TO_CYCLE(n, c) ((n) / (c) + (((n) % (c)) ? 1 : 0))
145
shaoming chen21898812017-03-31 19:30:36 +0800146#define MTK_DSI_HOST_IS_READ(type) \
147 ((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
148 (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
149 (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
150 (type == MIPI_DSI_DCS_READ))
151
CK Hu2e54c142016-01-04 18:36:35 +0100152struct phy;
153
154struct mtk_dsi {
155 struct mtk_ddp_comp ddp_comp;
156 struct device *dev;
157 struct mipi_dsi_host host;
158 struct drm_encoder encoder;
159 struct drm_connector conn;
160 struct drm_panel *panel;
161 struct drm_bridge *bridge;
162 struct phy *phy;
163
164 void __iomem *regs;
165
166 struct clk *engine_clk;
167 struct clk *digital_clk;
168 struct clk *hs_clk;
169
170 u32 data_rate;
171
172 unsigned long mode_flags;
173 enum mipi_dsi_pixel_format format;
174 unsigned int lanes;
175 struct videomode vm;
176 int refcount;
177 bool enabled;
shaoming chendd5080a2017-03-31 19:30:35 +0800178 u32 irq_data;
179 wait_queue_head_t irq_wait_queue;
CK Hu2e54c142016-01-04 18:36:35 +0100180};
181
182static inline struct mtk_dsi *encoder_to_dsi(struct drm_encoder *e)
183{
184 return container_of(e, struct mtk_dsi, encoder);
185}
186
187static inline struct mtk_dsi *connector_to_dsi(struct drm_connector *c)
188{
189 return container_of(c, struct mtk_dsi, conn);
190}
191
192static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
193{
194 return container_of(h, struct mtk_dsi, host);
195}
196
197static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
198{
199 u32 temp = readl(dsi->regs + offset);
200
201 writel((temp & ~mask) | (data & mask), dsi->regs + offset);
202}
203
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800204static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100205{
206 u32 timcon0, timcon1, timcon2, timcon3;
Jitao Shif6c87232016-11-16 11:20:54 +0800207 u32 ui, cycle_time;
CK Hu2e54c142016-01-04 18:36:35 +0100208
209 ui = 1000 / dsi->data_rate + 0x01;
210 cycle_time = 8000 / dsi->data_rate + 0x01;
CK Hu2e54c142016-01-04 18:36:35 +0100211
Jitao Shif6c87232016-11-16 11:20:54 +0800212 timcon0 = T_LPX | T_HS_PREP << 8 | T_HS_ZERO << 16 | T_HS_TRAIL << 24;
213 timcon1 = 4 * T_LPX | (3 * T_LPX / 2) << 8 | 5 * T_LPX << 16 |
214 T_HS_EXIT << 24;
CK Hu2e54c142016-01-04 18:36:35 +0100215 timcon2 = ((NS_TO_CYCLE(0x64, cycle_time) + 0xa) << 24) |
216 (NS_TO_CYCLE(0x150, cycle_time) << 16);
Jitao Shif6c87232016-11-16 11:20:54 +0800217 timcon3 = NS_TO_CYCLE(0x40, cycle_time) | (2 * T_LPX) << 16 |
218 NS_TO_CYCLE(80 + 52 * ui, cycle_time) << 8;
CK Hu2e54c142016-01-04 18:36:35 +0100219
220 writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
221 writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
222 writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
223 writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
224}
225
226static void mtk_dsi_enable(struct mtk_dsi *dsi)
227{
228 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
229}
230
231static void mtk_dsi_disable(struct mtk_dsi *dsi)
232{
233 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
234}
235
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800236static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100237{
238 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
239 mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
240}
241
242static int mtk_dsi_poweron(struct mtk_dsi *dsi)
243{
244 struct device *dev = dsi->dev;
245 int ret;
Jitao Shif6c87232016-11-16 11:20:54 +0800246 u64 pixel_clock, total_bits;
247 u32 htotal, htotal_bits, bit_per_pixel, overhead_cycles, overhead_bits;
CK Hu2e54c142016-01-04 18:36:35 +0100248
249 if (++dsi->refcount != 1)
250 return 0;
251
Jitao Shif6c87232016-11-16 11:20:54 +0800252 switch (dsi->format) {
253 case MIPI_DSI_FMT_RGB565:
254 bit_per_pixel = 16;
255 break;
256 case MIPI_DSI_FMT_RGB666_PACKED:
257 bit_per_pixel = 18;
258 break;
259 case MIPI_DSI_FMT_RGB666:
260 case MIPI_DSI_FMT_RGB888:
261 default:
262 bit_per_pixel = 24;
263 break;
264 }
CK Hu2e54c142016-01-04 18:36:35 +0100265
Jitao Shif6c87232016-11-16 11:20:54 +0800266 /**
267 * vm.pixelclock is in kHz, pixel_clock unit is Hz, so multiply by 1000
268 * htotal_time = htotal * byte_per_pixel / num_lanes
269 * overhead_time = lpx + hs_prepare + hs_zero + hs_trail + hs_exit
270 * mipi_ratio = (htotal_time + overhead_time) / htotal_time
271 * data_rate = pixel_clock * bit_per_pixel * mipi_ratio / num_lanes;
272 */
273 pixel_clock = dsi->vm.pixelclock * 1000;
274 htotal = dsi->vm.hactive + dsi->vm.hback_porch + dsi->vm.hfront_porch +
275 dsi->vm.hsync_len;
276 htotal_bits = htotal * bit_per_pixel;
277
278 overhead_cycles = T_LPX + T_HS_PREP + T_HS_ZERO + T_HS_TRAIL +
279 T_HS_EXIT;
280 overhead_bits = overhead_cycles * dsi->lanes * 8;
281 total_bits = htotal_bits + overhead_bits;
282
283 dsi->data_rate = DIV_ROUND_UP_ULL(pixel_clock * total_bits,
284 htotal * dsi->lanes);
285
286 ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
CK Hu2e54c142016-01-04 18:36:35 +0100287 if (ret < 0) {
288 dev_err(dev, "Failed to set data rate: %d\n", ret);
289 goto err_refcount;
290 }
291
292 phy_power_on(dsi->phy);
293
294 ret = clk_prepare_enable(dsi->engine_clk);
295 if (ret < 0) {
296 dev_err(dev, "Failed to enable engine clock: %d\n", ret);
297 goto err_phy_power_off;
298 }
299
300 ret = clk_prepare_enable(dsi->digital_clk);
301 if (ret < 0) {
302 dev_err(dev, "Failed to enable digital clock: %d\n", ret);
303 goto err_disable_engine_clk;
304 }
305
306 mtk_dsi_enable(dsi);
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800307 mtk_dsi_reset_engine(dsi);
308 mtk_dsi_phy_timconfig(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100309
310 return 0;
311
312err_disable_engine_clk:
313 clk_disable_unprepare(dsi->engine_clk);
314err_phy_power_off:
315 phy_power_off(dsi->phy);
316err_refcount:
317 dsi->refcount--;
318 return ret;
319}
320
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800321static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100322{
323 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
324 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
325}
326
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800327static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100328{
329 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
330 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
331 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
332}
333
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800334static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100335{
336 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
337 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
338}
339
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800340static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100341{
342 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
343 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
344 mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
345}
346
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800347static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100348{
349 u32 tmp_reg1;
350
351 tmp_reg1 = readl(dsi->regs + DSI_PHY_LCCON);
352 return ((tmp_reg1 & LC_HS_TX_EN) == 1) ? true : false;
353}
354
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800355static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
CK Hu2e54c142016-01-04 18:36:35 +0100356{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800357 if (enter && !mtk_dsi_clk_hs_state(dsi))
CK Hu2e54c142016-01-04 18:36:35 +0100358 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800359 else if (!enter && mtk_dsi_clk_hs_state(dsi))
CK Hu2e54c142016-01-04 18:36:35 +0100360 mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
361}
362
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800363static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100364{
365 u32 vid_mode = CMD_MODE;
366
367 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
368 vid_mode = SYNC_PULSE_MODE;
369
370 if ((dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) &&
371 !(dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE))
372 vid_mode = BURST_MODE;
373 }
374
375 writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
376}
377
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800378static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100379{
380 struct videomode *vm = &dsi->vm;
381 u32 dsi_buf_bpp, ps_wc;
382 u32 ps_bpp_mode;
383
384 if (dsi->format == MIPI_DSI_FMT_RGB565)
385 dsi_buf_bpp = 2;
386 else
387 dsi_buf_bpp = 3;
388
389 ps_wc = vm->hactive * dsi_buf_bpp;
390 ps_bpp_mode = ps_wc;
391
392 switch (dsi->format) {
393 case MIPI_DSI_FMT_RGB888:
394 ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
395 break;
396 case MIPI_DSI_FMT_RGB666:
397 ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
398 break;
399 case MIPI_DSI_FMT_RGB666_PACKED:
400 ps_bpp_mode |= LOOSELY_PS_18BIT_RGB666;
401 break;
402 case MIPI_DSI_FMT_RGB565:
403 ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
404 break;
405 }
406
407 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
408 writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
409 writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
410}
411
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800412static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100413{
414 u32 tmp_reg;
415
416 switch (dsi->lanes) {
417 case 1:
418 tmp_reg = 1 << 2;
419 break;
420 case 2:
421 tmp_reg = 3 << 2;
422 break;
423 case 3:
424 tmp_reg = 7 << 2;
425 break;
426 case 4:
427 tmp_reg = 0xf << 2;
428 break;
429 default:
430 tmp_reg = 0xf << 2;
431 break;
432 }
433
434 writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
435}
436
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800437static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100438{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800439 u32 dsi_tmp_buf_bpp;
CK Hu2e54c142016-01-04 18:36:35 +0100440 u32 tmp_reg;
441
442 switch (dsi->format) {
443 case MIPI_DSI_FMT_RGB888:
444 tmp_reg = PACKED_PS_24BIT_RGB888;
445 dsi_tmp_buf_bpp = 3;
446 break;
447 case MIPI_DSI_FMT_RGB666:
448 tmp_reg = LOOSELY_PS_18BIT_RGB666;
449 dsi_tmp_buf_bpp = 3;
450 break;
451 case MIPI_DSI_FMT_RGB666_PACKED:
452 tmp_reg = PACKED_PS_18BIT_RGB666;
453 dsi_tmp_buf_bpp = 3;
454 break;
455 case MIPI_DSI_FMT_RGB565:
456 tmp_reg = PACKED_PS_16BIT_RGB565;
457 dsi_tmp_buf_bpp = 2;
458 break;
459 default:
460 tmp_reg = PACKED_PS_24BIT_RGB888;
461 dsi_tmp_buf_bpp = 3;
462 break;
463 }
464
465 tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
466 writel(tmp_reg, dsi->regs + DSI_PSCTRL);
467}
468
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800469static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
CK Hu2e54c142016-01-04 18:36:35 +0100470{
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800471 u32 horizontal_sync_active_byte;
472 u32 horizontal_backporch_byte;
473 u32 horizontal_frontporch_byte;
474 u32 dsi_tmp_buf_bpp;
CK Hu2e54c142016-01-04 18:36:35 +0100475
476 struct videomode *vm = &dsi->vm;
477
478 if (dsi->format == MIPI_DSI_FMT_RGB565)
479 dsi_tmp_buf_bpp = 2;
480 else
481 dsi_tmp_buf_bpp = 3;
482
483 writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
484 writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
485 writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
486 writel(vm->vactive, dsi->regs + DSI_VACT_NL);
487
488 horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);
489
490 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
491 horizontal_backporch_byte =
492 (vm->hback_porch * dsi_tmp_buf_bpp - 10);
493 else
494 horizontal_backporch_byte = ((vm->hback_porch + vm->hsync_len) *
495 dsi_tmp_buf_bpp - 10);
496
497 horizontal_frontporch_byte = (vm->hfront_porch * dsi_tmp_buf_bpp - 12);
498
499 writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
500 writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
501 writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);
502
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800503 mtk_dsi_ps_control(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100504}
505
506static void mtk_dsi_start(struct mtk_dsi *dsi)
507{
508 writel(0, dsi->regs + DSI_START);
509 writel(1, dsi->regs + DSI_START);
510}
511
shaoming chendd5080a2017-03-31 19:30:35 +0800512static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
513{
514 u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
515
516 writel(inten, dsi->regs + DSI_INTEN);
517}
518
519static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
520{
521 dsi->irq_data |= irq_bit;
522}
523
shaoming chen21898812017-03-31 19:30:36 +0800524static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
shaoming chendd5080a2017-03-31 19:30:35 +0800525{
526 dsi->irq_data &= ~irq_bit;
527}
528
shaoming chen21898812017-03-31 19:30:36 +0800529static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
shaoming chendd5080a2017-03-31 19:30:35 +0800530 unsigned int timeout)
531{
532 s32 ret = 0;
533 unsigned long jiffies = msecs_to_jiffies(timeout);
534
535 ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
536 dsi->irq_data & irq_flag,
537 jiffies);
538 if (ret == 0) {
539 DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);
540
541 mtk_dsi_enable(dsi);
542 mtk_dsi_reset_engine(dsi);
543 }
544
545 return ret;
546}
547
548static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
549{
550 struct mtk_dsi *dsi = dev_id;
551 u32 status, tmp;
552 u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;
553
554 status = readl(dsi->regs + DSI_INTSTA) & flag;
555
556 if (status) {
557 do {
558 mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
559 tmp = readl(dsi->regs + DSI_INTSTA);
560 } while (tmp & DSI_BUSY);
561
562 mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
563 mtk_dsi_irq_data_set(dsi, status);
564 wake_up_interruptible(&dsi->irq_wait_queue);
565 }
566
567 return IRQ_HANDLED;
568}
569
CK Hu2e54c142016-01-04 18:36:35 +0100570static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
571{
572 if (WARN_ON(dsi->refcount == 0))
573 return;
574
575 if (--dsi->refcount != 0)
576 return;
577
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800578 mtk_dsi_lane0_ulp_mode_enter(dsi);
579 mtk_dsi_clk_ulp_mode_enter(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100580
581 mtk_dsi_disable(dsi);
582
583 clk_disable_unprepare(dsi->engine_clk);
584 clk_disable_unprepare(dsi->digital_clk);
585
586 phy_power_off(dsi->phy);
587}
588
589static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
590{
591 int ret;
592
593 if (dsi->enabled)
594 return;
595
596 if (dsi->panel) {
597 if (drm_panel_prepare(dsi->panel)) {
598 DRM_ERROR("failed to setup the panel\n");
599 return;
600 }
601 }
602
603 ret = mtk_dsi_poweron(dsi);
604 if (ret < 0) {
605 DRM_ERROR("failed to power on dsi\n");
606 return;
607 }
608
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800609 mtk_dsi_rxtx_control(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100610
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800611 mtk_dsi_clk_ulp_mode_leave(dsi);
612 mtk_dsi_lane0_ulp_mode_leave(dsi);
613 mtk_dsi_clk_hs_mode(dsi, 0);
614 mtk_dsi_set_mode(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100615
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800616 mtk_dsi_ps_control_vact(dsi);
617 mtk_dsi_config_vdo_timing(dsi);
shaoming chendd5080a2017-03-31 19:30:35 +0800618 mtk_dsi_set_interrupt_enable(dsi);
CK Hu2e54c142016-01-04 18:36:35 +0100619
yt.shen@mediatek.com80a5cfd2017-03-31 19:30:34 +0800620 mtk_dsi_set_mode(dsi);
621 mtk_dsi_clk_hs_mode(dsi, 1);
CK Hu2e54c142016-01-04 18:36:35 +0100622
623 mtk_dsi_start(dsi);
624
625 dsi->enabled = true;
626}
627
628static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
629{
630 if (!dsi->enabled)
631 return;
632
633 if (dsi->panel) {
634 if (drm_panel_disable(dsi->panel)) {
635 DRM_ERROR("failed to disable the panel\n");
636 return;
637 }
638 }
639
640 mtk_dsi_poweroff(dsi);
641
642 dsi->enabled = false;
643}
644
645static void mtk_dsi_encoder_destroy(struct drm_encoder *encoder)
646{
647 drm_encoder_cleanup(encoder);
648}
649
650static const struct drm_encoder_funcs mtk_dsi_encoder_funcs = {
651 .destroy = mtk_dsi_encoder_destroy,
652};
653
654static bool mtk_dsi_encoder_mode_fixup(struct drm_encoder *encoder,
655 const struct drm_display_mode *mode,
656 struct drm_display_mode *adjusted_mode)
657{
658 return true;
659}
660
661static void mtk_dsi_encoder_mode_set(struct drm_encoder *encoder,
662 struct drm_display_mode *mode,
663 struct drm_display_mode *adjusted)
664{
665 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
666
667 dsi->vm.pixelclock = adjusted->clock;
668 dsi->vm.hactive = adjusted->hdisplay;
669 dsi->vm.hback_porch = adjusted->htotal - adjusted->hsync_end;
670 dsi->vm.hfront_porch = adjusted->hsync_start - adjusted->hdisplay;
671 dsi->vm.hsync_len = adjusted->hsync_end - adjusted->hsync_start;
672
673 dsi->vm.vactive = adjusted->vdisplay;
674 dsi->vm.vback_porch = adjusted->vtotal - adjusted->vsync_end;
675 dsi->vm.vfront_porch = adjusted->vsync_start - adjusted->vdisplay;
676 dsi->vm.vsync_len = adjusted->vsync_end - adjusted->vsync_start;
677}
678
679static void mtk_dsi_encoder_disable(struct drm_encoder *encoder)
680{
681 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
682
683 mtk_output_dsi_disable(dsi);
684}
685
686static void mtk_dsi_encoder_enable(struct drm_encoder *encoder)
687{
688 struct mtk_dsi *dsi = encoder_to_dsi(encoder);
689
690 mtk_output_dsi_enable(dsi);
691}
692
CK Hu2e54c142016-01-04 18:36:35 +0100693static int mtk_dsi_connector_get_modes(struct drm_connector *connector)
694{
695 struct mtk_dsi *dsi = connector_to_dsi(connector);
696
697 return drm_panel_get_modes(dsi->panel);
698}
699
CK Hu2e54c142016-01-04 18:36:35 +0100700static const struct drm_encoder_helper_funcs mtk_dsi_encoder_helper_funcs = {
701 .mode_fixup = mtk_dsi_encoder_mode_fixup,
702 .mode_set = mtk_dsi_encoder_mode_set,
703 .disable = mtk_dsi_encoder_disable,
704 .enable = mtk_dsi_encoder_enable,
705};
706
707static const struct drm_connector_funcs mtk_dsi_connector_funcs = {
708 .dpms = drm_atomic_helper_connector_dpms,
CK Hu2e54c142016-01-04 18:36:35 +0100709 .fill_modes = drm_helper_probe_single_connector_modes,
710 .destroy = drm_connector_cleanup,
711 .reset = drm_atomic_helper_connector_reset,
712 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
713 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
714};
715
716static const struct drm_connector_helper_funcs
717 mtk_dsi_connector_helper_funcs = {
718 .get_modes = mtk_dsi_connector_get_modes,
CK Hu2e54c142016-01-04 18:36:35 +0100719};
720
CK Hu2e54c142016-01-04 18:36:35 +0100721static int mtk_dsi_create_connector(struct drm_device *drm, struct mtk_dsi *dsi)
722{
723 int ret;
724
725 ret = drm_connector_init(drm, &dsi->conn, &mtk_dsi_connector_funcs,
726 DRM_MODE_CONNECTOR_DSI);
727 if (ret) {
728 DRM_ERROR("Failed to connector init to drm\n");
729 return ret;
730 }
731
732 drm_connector_helper_add(&dsi->conn, &mtk_dsi_connector_helper_funcs);
733
734 dsi->conn.dpms = DRM_MODE_DPMS_OFF;
735 drm_mode_connector_attach_encoder(&dsi->conn, &dsi->encoder);
736
737 if (dsi->panel) {
738 ret = drm_panel_attach(dsi->panel, &dsi->conn);
739 if (ret) {
740 DRM_ERROR("Failed to attach panel to drm\n");
741 goto err_connector_cleanup;
742 }
743 }
744
745 return 0;
746
747err_connector_cleanup:
748 drm_connector_cleanup(&dsi->conn);
749 return ret;
750}
751
752static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi)
753{
754 int ret;
755
756 ret = drm_encoder_init(drm, &dsi->encoder, &mtk_dsi_encoder_funcs,
757 DRM_MODE_ENCODER_DSI, NULL);
758 if (ret) {
759 DRM_ERROR("Failed to encoder init to drm\n");
760 return ret;
761 }
762 drm_encoder_helper_add(&dsi->encoder, &mtk_dsi_encoder_helper_funcs);
763
764 /*
765 * Currently display data paths are statically assigned to a crtc each.
766 * crtc 0 is OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0
767 */
768 dsi->encoder.possible_crtcs = 1;
769
770 /* If there's a bridge, attach to it and let it create the connector */
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200771 ret = drm_bridge_attach(&dsi->encoder, dsi->bridge, NULL);
CK Hu2e54c142016-01-04 18:36:35 +0100772 if (ret) {
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200773 DRM_ERROR("Failed to attach bridge to drm\n");
774
CK Hu2e54c142016-01-04 18:36:35 +0100775 /* Otherwise create our own connector and attach to a panel */
776 ret = mtk_dsi_create_connector(drm, dsi);
777 if (ret)
778 goto err_encoder_cleanup;
779 }
780
781 return 0;
782
783err_encoder_cleanup:
784 drm_encoder_cleanup(&dsi->encoder);
785 return ret;
786}
787
788static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi)
789{
790 drm_encoder_cleanup(&dsi->encoder);
791 /* Skip connector cleanup if creation was delegated to the bridge */
Philipp Zabel2ea9f312016-05-26 16:08:18 +0200792 if (dsi->conn.dev)
CK Hu2e54c142016-01-04 18:36:35 +0100793 drm_connector_cleanup(&dsi->conn);
CK Hu2e54c142016-01-04 18:36:35 +0100794}
795
796static void mtk_dsi_ddp_start(struct mtk_ddp_comp *comp)
797{
798 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
799
800 mtk_dsi_poweron(dsi);
801}
802
803static void mtk_dsi_ddp_stop(struct mtk_ddp_comp *comp)
804{
805 struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);
806
807 mtk_dsi_poweroff(dsi);
808}
809
810static const struct mtk_ddp_comp_funcs mtk_dsi_funcs = {
811 .start = mtk_dsi_ddp_start,
812 .stop = mtk_dsi_ddp_stop,
813};
814
815static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
816 struct mipi_dsi_device *device)
817{
818 struct mtk_dsi *dsi = host_to_dsi(host);
819
820 dsi->lanes = device->lanes;
821 dsi->format = device->format;
822 dsi->mode_flags = device->mode_flags;
823
824 if (dsi->conn.dev)
825 drm_helper_hpd_irq_event(dsi->conn.dev);
826
827 return 0;
828}
829
830static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
831 struct mipi_dsi_device *device)
832{
833 struct mtk_dsi *dsi = host_to_dsi(host);
834
835 if (dsi->conn.dev)
836 drm_helper_hpd_irq_event(dsi->conn.dev);
837
838 return 0;
839}
840
shaoming chen21898812017-03-31 19:30:36 +0800841static void mtk_dsi_wait_for_idle(struct mtk_dsi *dsi)
842{
843 u32 timeout_ms = 500000; /* total 1s ~ 2s timeout */
844
845 while (timeout_ms--) {
846 if (!(readl(dsi->regs + DSI_INTSTA) & DSI_BUSY))
847 break;
848
849 usleep_range(2, 4);
850 }
851
852 if (timeout_ms == 0) {
853 DRM_WARN("polling dsi wait not busy timeout!\n");
854
855 mtk_dsi_enable(dsi);
856 mtk_dsi_reset_engine(dsi);
857 }
858}
859
860static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
861{
862 switch (type) {
863 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
864 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
865 return 1;
866 case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
867 case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
868 return 2;
869 case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
870 case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
871 return read_data[1] + read_data[2] * 16;
872 case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
873 DRM_INFO("type is 0x02, try again\n");
874 break;
875 default:
876 DRM_INFO("type(0x%x) cannot be non-recognite\n", type);
877 break;
878 }
879
880 return 0;
881}
882
883static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
884{
885 const char *tx_buf = msg->tx_buf;
886 u8 config, cmdq_size, cmdq_off, type = msg->type;
887 u32 reg_val, cmdq_mask, i;
888
889 if (MTK_DSI_HOST_IS_READ(type))
890 config = BTA;
891 else
892 config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;
893
894 if (msg->tx_len > 2) {
895 cmdq_size = 1 + (msg->tx_len + 3) / 4;
896 cmdq_off = 4;
897 cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
898 reg_val = (msg->tx_len << 16) | (type << 8) | config;
899 } else {
900 cmdq_size = 1;
901 cmdq_off = 2;
902 cmdq_mask = CONFIG | DATA_ID;
903 reg_val = (type << 8) | config;
904 }
905
906 for (i = 0; i < msg->tx_len; i++)
907 writeb(tx_buf[i], dsi->regs + DSI_CMDQ0 + cmdq_off + i);
908
909 mtk_dsi_mask(dsi, DSI_CMDQ0, cmdq_mask, reg_val);
910 mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
911}
912
913static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
914 const struct mipi_dsi_msg *msg, u8 flag)
915{
916 mtk_dsi_wait_for_idle(dsi);
917 mtk_dsi_irq_data_clear(dsi, flag);
918 mtk_dsi_cmdq(dsi, msg);
919 mtk_dsi_start(dsi);
920
921 if (!mtk_dsi_wait_for_irq_done(dsi, flag, 2000))
922 return -ETIME;
923 else
924 return 0;
925}
926
927static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
928 const struct mipi_dsi_msg *msg)
929{
930 struct mtk_dsi *dsi = host_to_dsi(host);
931 u32 recv_cnt, i;
932 u8 read_data[16];
933 void *src_addr;
934 u8 irq_flag = CMD_DONE_INT_FLAG;
935
936 if (readl(dsi->regs + DSI_MODE_CTRL) & MODE) {
937 DRM_ERROR("dsi engine is not command mode\n");
938 return -EINVAL;
939 }
940
941 if (MTK_DSI_HOST_IS_READ(msg->type))
942 irq_flag |= LPRX_RD_RDY_INT_FLAG;
943
944 if (mtk_dsi_host_send_cmd(dsi, msg, irq_flag) < 0)
945 return -ETIME;
946
947 if (!MTK_DSI_HOST_IS_READ(msg->type))
948 return 0;
949
950 if (!msg->rx_buf) {
951 DRM_ERROR("dsi receive buffer size may be NULL\n");
952 return -EINVAL;
953 }
954
955 for (i = 0; i < 16; i++)
956 *(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);
957
958 recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);
959
960 if (recv_cnt > 2)
961 src_addr = &read_data[4];
962 else
963 src_addr = &read_data[1];
964
965 if (recv_cnt > 10)
966 recv_cnt = 10;
967
968 if (recv_cnt > msg->rx_len)
969 recv_cnt = msg->rx_len;
970
971 if (recv_cnt)
972 memcpy(msg->rx_buf, src_addr, recv_cnt);
973
974 DRM_INFO("dsi get %d byte data from the panel address(0x%x)\n",
975 recv_cnt, *((u8 *)(msg->tx_buf)));
976
977 return recv_cnt;
978}
979
CK Hu2e54c142016-01-04 18:36:35 +0100980static const struct mipi_dsi_host_ops mtk_dsi_ops = {
981 .attach = mtk_dsi_host_attach,
982 .detach = mtk_dsi_host_detach,
shaoming chen21898812017-03-31 19:30:36 +0800983 .transfer = mtk_dsi_host_transfer,
CK Hu2e54c142016-01-04 18:36:35 +0100984};
985
986static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
987{
988 int ret;
989 struct drm_device *drm = data;
990 struct mtk_dsi *dsi = dev_get_drvdata(dev);
991
992 ret = mtk_ddp_comp_register(drm, &dsi->ddp_comp);
993 if (ret < 0) {
994 dev_err(dev, "Failed to register component %s: %d\n",
995 dev->of_node->full_name, ret);
996 return ret;
997 }
998
999 ret = mipi_dsi_host_register(&dsi->host);
1000 if (ret < 0) {
1001 dev_err(dev, "failed to register DSI host: %d\n", ret);
1002 goto err_ddp_comp_unregister;
1003 }
1004
1005 ret = mtk_dsi_create_conn_enc(drm, dsi);
1006 if (ret) {
1007 DRM_ERROR("Encoder create failed with %d\n", ret);
1008 goto err_unregister;
1009 }
1010
1011 return 0;
1012
1013err_unregister:
1014 mipi_dsi_host_unregister(&dsi->host);
1015err_ddp_comp_unregister:
1016 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
1017 return ret;
1018}
1019
1020static void mtk_dsi_unbind(struct device *dev, struct device *master,
1021 void *data)
1022{
1023 struct drm_device *drm = data;
1024 struct mtk_dsi *dsi = dev_get_drvdata(dev);
1025
1026 mtk_dsi_destroy_conn_enc(dsi);
1027 mipi_dsi_host_unregister(&dsi->host);
1028 mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
1029}
1030
1031static const struct component_ops mtk_dsi_component_ops = {
1032 .bind = mtk_dsi_bind,
1033 .unbind = mtk_dsi_unbind,
1034};
1035
1036static int mtk_dsi_probe(struct platform_device *pdev)
1037{
1038 struct mtk_dsi *dsi;
1039 struct device *dev = &pdev->dev;
1040 struct device_node *remote_node, *endpoint;
1041 struct resource *regs;
shaoming chendd5080a2017-03-31 19:30:35 +08001042 int irq_num;
CK Hu2e54c142016-01-04 18:36:35 +01001043 int comp_id;
1044 int ret;
1045
1046 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1047 if (!dsi)
1048 return -ENOMEM;
1049
1050 dsi->host.ops = &mtk_dsi_ops;
1051 dsi->host.dev = dev;
1052
1053 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
1054 if (endpoint) {
1055 remote_node = of_graph_get_remote_port_parent(endpoint);
1056 if (!remote_node) {
1057 dev_err(dev, "No panel connected\n");
1058 return -ENODEV;
1059 }
1060
1061 dsi->bridge = of_drm_find_bridge(remote_node);
1062 dsi->panel = of_drm_find_panel(remote_node);
1063 of_node_put(remote_node);
1064 if (!dsi->bridge && !dsi->panel) {
1065 dev_info(dev, "Waiting for bridge or panel driver\n");
1066 return -EPROBE_DEFER;
1067 }
1068 }
1069
1070 dsi->engine_clk = devm_clk_get(dev, "engine");
1071 if (IS_ERR(dsi->engine_clk)) {
1072 ret = PTR_ERR(dsi->engine_clk);
1073 dev_err(dev, "Failed to get engine clock: %d\n", ret);
1074 return ret;
1075 }
1076
1077 dsi->digital_clk = devm_clk_get(dev, "digital");
1078 if (IS_ERR(dsi->digital_clk)) {
1079 ret = PTR_ERR(dsi->digital_clk);
1080 dev_err(dev, "Failed to get digital clock: %d\n", ret);
1081 return ret;
1082 }
1083
1084 dsi->hs_clk = devm_clk_get(dev, "hs");
1085 if (IS_ERR(dsi->hs_clk)) {
1086 ret = PTR_ERR(dsi->hs_clk);
1087 dev_err(dev, "Failed to get hs clock: %d\n", ret);
1088 return ret;
1089 }
1090
1091 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1092 dsi->regs = devm_ioremap_resource(dev, regs);
1093 if (IS_ERR(dsi->regs)) {
1094 ret = PTR_ERR(dsi->regs);
1095 dev_err(dev, "Failed to ioremap memory: %d\n", ret);
1096 return ret;
1097 }
1098
1099 dsi->phy = devm_phy_get(dev, "dphy");
1100 if (IS_ERR(dsi->phy)) {
1101 ret = PTR_ERR(dsi->phy);
1102 dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
1103 return ret;
1104 }
1105
1106 comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DSI);
1107 if (comp_id < 0) {
1108 dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
1109 return comp_id;
1110 }
1111
1112 ret = mtk_ddp_comp_init(dev, dev->of_node, &dsi->ddp_comp, comp_id,
1113 &mtk_dsi_funcs);
1114 if (ret) {
1115 dev_err(dev, "Failed to initialize component: %d\n", ret);
1116 return ret;
1117 }
1118
shaoming chendd5080a2017-03-31 19:30:35 +08001119 irq_num = platform_get_irq(pdev, 0);
1120 if (irq_num < 0) {
1121 dev_err(&pdev->dev, "failed to request dsi irq resource\n");
1122 return -EPROBE_DEFER;
1123 }
1124
1125 irq_set_status_flags(irq_num, IRQ_TYPE_LEVEL_LOW);
1126 ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
1127 IRQF_TRIGGER_LOW, dev_name(&pdev->dev), dsi);
1128 if (ret) {
1129 dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
1130 return -EPROBE_DEFER;
1131 }
1132
1133 init_waitqueue_head(&dsi->irq_wait_queue);
1134
CK Hu2e54c142016-01-04 18:36:35 +01001135 platform_set_drvdata(pdev, dsi);
1136
1137 return component_add(&pdev->dev, &mtk_dsi_component_ops);
1138}
1139
1140static int mtk_dsi_remove(struct platform_device *pdev)
1141{
1142 struct mtk_dsi *dsi = platform_get_drvdata(pdev);
1143
1144 mtk_output_dsi_disable(dsi);
1145 component_del(&pdev->dev, &mtk_dsi_component_ops);
1146
1147 return 0;
1148}
1149
1150static const struct of_device_id mtk_dsi_of_match[] = {
1151 { .compatible = "mediatek,mt8173-dsi" },
1152 { },
1153};
1154
1155struct platform_driver mtk_dsi_driver = {
1156 .probe = mtk_dsi_probe,
1157 .remove = mtk_dsi_remove,
1158 .driver = {
1159 .name = "mtk-dsi",
1160 .of_match_table = mtk_dsi_of_match,
1161 },
1162};