1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2018-2020 Intel Corporation
7 #include <linux/module.h>
8 #include <linux/of_graph.h>
9 #include <linux/of_platform.h>
10 #include <linux/of_reserved_mem.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_client_setup.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fbdev_dma.h>
20 #include <drm/drm_gem_dma_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_module.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_vblank.h>
30 static int kmb_display_clk_enable(struct kmb_drm_private
*kmb
)
34 ret
= clk_prepare_enable(kmb
->kmb_clk
.clk_lcd
);
36 drm_err(&kmb
->drm
, "Failed to enable LCD clock: %d\n", ret
);
39 DRM_INFO("SUCCESS : enabled LCD clocks\n");
43 static int kmb_initialize_clocks(struct kmb_drm_private
*kmb
, struct device
*dev
)
46 struct regmap
*msscam
;
48 kmb
->kmb_clk
.clk_lcd
= devm_clk_get(dev
, "clk_lcd");
49 if (IS_ERR(kmb
->kmb_clk
.clk_lcd
)) {
50 drm_err(&kmb
->drm
, "clk_get() failed clk_lcd\n");
51 return PTR_ERR(kmb
->kmb_clk
.clk_lcd
);
54 kmb
->kmb_clk
.clk_pll0
= devm_clk_get(dev
, "clk_pll0");
55 if (IS_ERR(kmb
->kmb_clk
.clk_pll0
)) {
56 drm_err(&kmb
->drm
, "clk_get() failed clk_pll0 ");
57 return PTR_ERR(kmb
->kmb_clk
.clk_pll0
);
59 kmb
->sys_clk_mhz
= clk_get_rate(kmb
->kmb_clk
.clk_pll0
) / 1000000;
60 drm_info(&kmb
->drm
, "system clk = %d Mhz", kmb
->sys_clk_mhz
);
62 ret
= kmb_dsi_clk_init(kmb
->kmb_dsi
);
64 /* Set LCD clock to 200 Mhz */
65 clk_set_rate(kmb
->kmb_clk
.clk_lcd
, KMB_LCD_DEFAULT_CLK
);
66 if (clk_get_rate(kmb
->kmb_clk
.clk_lcd
) != KMB_LCD_DEFAULT_CLK
) {
67 drm_err(&kmb
->drm
, "failed to set to clk_lcd to %d\n",
71 drm_dbg(&kmb
->drm
, "clk_lcd = %ld\n", clk_get_rate(kmb
->kmb_clk
.clk_lcd
));
73 ret
= kmb_display_clk_enable(kmb
);
77 msscam
= syscon_regmap_lookup_by_compatible("intel,keembay-msscam");
79 drm_err(&kmb
->drm
, "failed to get msscam syscon");
83 /* Enable MSS_CAM_CLK_CTRL for MIPI TX and LCD */
84 regmap_update_bits(msscam
, MSS_CAM_CLK_CTRL
, 0x1fff, 0x1fff);
85 regmap_update_bits(msscam
, MSS_CAM_RSTN_CTRL
, 0xffffffff, 0xffffffff);
89 static void kmb_display_clk_disable(struct kmb_drm_private
*kmb
)
91 clk_disable_unprepare(kmb
->kmb_clk
.clk_lcd
);
94 static void __iomem
*kmb_map_mmio(struct drm_device
*drm
,
95 struct platform_device
*pdev
,
101 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
103 drm_err(drm
, "failed to get resource for %s", name
);
104 return ERR_PTR(-ENOMEM
);
106 mem
= devm_ioremap_resource(drm
->dev
, res
);
108 drm_err(drm
, "failed to ioremap %s registers", name
);
112 static int kmb_hw_init(struct drm_device
*drm
, unsigned long flags
)
114 struct kmb_drm_private
*kmb
= to_kmb(drm
);
115 struct platform_device
*pdev
= to_platform_device(drm
->dev
);
119 /* Map LCD MMIO registers */
120 kmb
->lcd_mmio
= kmb_map_mmio(drm
, pdev
, "lcd");
121 if (IS_ERR(kmb
->lcd_mmio
)) {
122 drm_err(&kmb
->drm
, "failed to map LCD registers\n");
126 /* Map MIPI MMIO registers */
127 ret
= kmb_dsi_map_mmio(kmb
->kmb_dsi
);
131 /* Enable display clocks */
132 kmb_initialize_clocks(kmb
, &pdev
->dev
);
134 /* Register irqs here - section 17.3 in databook
135 * lists LCD at 79 and 82 for MIPI under MSS CPU -
136 * firmware has redirected 79 to A53 IRQ 33
139 /* Allocate LCD interrupt resources */
140 irq_lcd
= platform_get_irq(pdev
, 0);
143 drm_err(&kmb
->drm
, "irq_lcd not found");
147 /* Get the optional framebuffer memory resource */
148 ret
= of_reserved_mem_device_init(drm
->dev
);
149 if (ret
&& ret
!= -ENODEV
)
152 spin_lock_init(&kmb
->irq_lock
);
154 kmb
->irq_lcd
= irq_lcd
;
159 of_reserved_mem_device_release(drm
->dev
);
164 static const struct drm_mode_config_funcs kmb_mode_config_funcs
= {
165 .fb_create
= drm_gem_fb_create
,
166 .atomic_check
= drm_atomic_helper_check
,
167 .atomic_commit
= drm_atomic_helper_commit
,
170 static int kmb_setup_mode_config(struct drm_device
*drm
)
173 struct kmb_drm_private
*kmb
= to_kmb(drm
);
175 ret
= drmm_mode_config_init(drm
);
178 drm
->mode_config
.min_width
= KMB_FB_MIN_WIDTH
;
179 drm
->mode_config
.min_height
= KMB_FB_MIN_HEIGHT
;
180 drm
->mode_config
.max_width
= KMB_FB_MAX_WIDTH
;
181 drm
->mode_config
.max_height
= KMB_FB_MAX_HEIGHT
;
182 drm
->mode_config
.preferred_depth
= 24;
183 drm
->mode_config
.funcs
= &kmb_mode_config_funcs
;
185 ret
= kmb_setup_crtc(drm
);
187 drm_err(drm
, "failed to create crtc\n");
190 ret
= kmb_dsi_encoder_init(drm
, kmb
->kmb_dsi
);
191 /* Set the CRTC's port so that the encoder component can find it */
192 kmb
->crtc
.port
= of_graph_get_port_by_id(drm
->dev
->of_node
, 0);
193 ret
= drm_vblank_init(drm
, drm
->mode_config
.num_crtc
);
195 drm_err(drm
, "failed to initialize vblank\n");
196 pm_runtime_disable(drm
->dev
);
200 drm_mode_config_reset(drm
);
204 static irqreturn_t
handle_lcd_irq(struct drm_device
*dev
)
206 unsigned long status
, val
, val1
;
207 int plane_id
, dma0_state
, dma1_state
;
208 struct kmb_drm_private
*kmb
= to_kmb(dev
);
211 status
= kmb_read_lcd(kmb
, LCD_INT_STATUS
);
213 spin_lock(&kmb
->irq_lock
);
214 if (status
& LCD_INT_EOF
) {
215 kmb_write_lcd(kmb
, LCD_INT_CLEAR
, LCD_INT_EOF
);
217 /* When disabling/enabling LCD layers, the change takes effect
218 * immediately and does not wait for EOF (end of frame).
219 * When kmb_plane_atomic_disable is called, mark the plane as
220 * disabled but actually disable the plane when EOF irq is
223 for (plane_id
= LAYER_0
;
224 plane_id
< KMB_MAX_PLANES
; plane_id
++) {
225 if (kmb
->plane_status
[plane_id
].disable
) {
226 kmb_clr_bitmask_lcd(kmb
,
229 LCD_DMA_LAYER_ENABLE
);
231 kmb_clr_bitmask_lcd(kmb
, LCD_CONTROL
,
232 kmb
->plane_status
[plane_id
].ctrl
);
234 ctrl
= kmb_read_lcd(kmb
, LCD_CONTROL
);
235 if (!(ctrl
& (LCD_CTRL_VL1_ENABLE
|
236 LCD_CTRL_VL2_ENABLE
|
237 LCD_CTRL_GL1_ENABLE
|
238 LCD_CTRL_GL2_ENABLE
))) {
239 /* If no LCD layers are using DMA,
240 * then disable DMA pipelined AXI read
243 kmb_clr_bitmask_lcd(kmb
, LCD_CONTROL
,
244 LCD_CTRL_PIPELINE_DMA
);
247 kmb
->plane_status
[plane_id
].disable
= false;
250 if (kmb
->kmb_under_flow
) {
251 /* DMA Recovery after underflow */
252 dma0_state
= (kmb
->layer_no
== 0) ?
253 LCD_VIDEO0_DMA0_STATE
: LCD_VIDEO1_DMA0_STATE
;
254 dma1_state
= (kmb
->layer_no
== 0) ?
255 LCD_VIDEO0_DMA1_STATE
: LCD_VIDEO1_DMA1_STATE
;
258 kmb_write_lcd(kmb
, LCD_FIFO_FLUSH
, 1);
259 val
= kmb_read_lcd(kmb
, dma0_state
)
260 & LCD_DMA_STATE_ACTIVE
;
261 val1
= kmb_read_lcd(kmb
, dma1_state
)
262 & LCD_DMA_STATE_ACTIVE
;
263 } while ((val
|| val1
));
265 kmb_clr_bitmask_lcd(kmb
,
266 LCD_LAYERn_DMA_CFG(kmb
->layer_no
),
267 LCD_DMA_LAYER_ENABLE
);
268 kmb_write_lcd(kmb
, LCD_FIFO_FLUSH
, 1);
269 kmb
->kmb_flush_done
= 1;
270 kmb
->kmb_under_flow
= 0;
274 if (status
& LCD_INT_LINE_CMP
) {
275 /* clear line compare interrupt */
276 kmb_write_lcd(kmb
, LCD_INT_CLEAR
, LCD_INT_LINE_CMP
);
279 if (status
& LCD_INT_VERT_COMP
) {
281 val
= kmb_read_lcd(kmb
, LCD_VSTATUS
);
282 val
= (val
& LCD_VSTATUS_VERTICAL_STATUS_MASK
);
284 case LCD_VSTATUS_COMPARE_VSYNC
:
285 /* Clear vertical compare interrupt */
286 kmb_write_lcd(kmb
, LCD_INT_CLEAR
, LCD_INT_VERT_COMP
);
287 if (kmb
->kmb_flush_done
) {
288 kmb_set_bitmask_lcd(kmb
,
291 LCD_DMA_LAYER_ENABLE
);
292 kmb
->kmb_flush_done
= 0;
294 drm_crtc_handle_vblank(&kmb
->crtc
);
296 case LCD_VSTATUS_COMPARE_BACKPORCH
:
297 case LCD_VSTATUS_COMPARE_ACTIVE
:
298 case LCD_VSTATUS_COMPARE_FRONT_PORCH
:
299 kmb_write_lcd(kmb
, LCD_INT_CLEAR
, LCD_INT_VERT_COMP
);
303 if (status
& LCD_INT_DMA_ERR
) {
305 (status
& LCD_INT_DMA_ERR
&
306 kmb_read_lcd(kmb
, LCD_INT_ENABLE
));
308 if (val
& (LAYER0_DMA_FIFO_UNDERFLOW
|
309 LAYER0_DMA_CB_FIFO_UNDERFLOW
|
310 LAYER0_DMA_CR_FIFO_UNDERFLOW
)) {
311 kmb
->kmb_under_flow
++;
313 "!LAYER0:VL0 DMA UNDERFLOW val = 0x%lx,under_flow=%d",
314 val
, kmb
->kmb_under_flow
);
315 /* disable underflow interrupt */
316 kmb_clr_bitmask_lcd(kmb
, LCD_INT_ENABLE
,
317 LAYER0_DMA_FIFO_UNDERFLOW
|
318 LAYER0_DMA_CB_FIFO_UNDERFLOW
|
319 LAYER0_DMA_CR_FIFO_UNDERFLOW
);
320 kmb_set_bitmask_lcd(kmb
, LCD_INT_CLEAR
,
321 LAYER0_DMA_CB_FIFO_UNDERFLOW
|
322 LAYER0_DMA_FIFO_UNDERFLOW
|
323 LAYER0_DMA_CR_FIFO_UNDERFLOW
);
324 /* disable auto restart mode */
325 kmb_clr_bitmask_lcd(kmb
, LCD_LAYERn_DMA_CFG(0),
326 LCD_DMA_LAYER_CONT_PING_PONG_UPDATE
);
331 if (val
& LAYER0_DMA_FIFO_OVERFLOW
)
333 "LAYER0:VL0 DMA OVERFLOW val = 0x%lx", val
);
334 if (val
& LAYER0_DMA_CB_FIFO_OVERFLOW
)
336 "LAYER0:VL0 DMA CB OVERFLOW val = 0x%lx", val
);
337 if (val
& LAYER0_DMA_CR_FIFO_OVERFLOW
)
339 "LAYER0:VL0 DMA CR OVERFLOW val = 0x%lx", val
);
342 if (val
& (LAYER1_DMA_FIFO_UNDERFLOW
|
343 LAYER1_DMA_CB_FIFO_UNDERFLOW
|
344 LAYER1_DMA_CR_FIFO_UNDERFLOW
)) {
345 kmb
->kmb_under_flow
++;
347 "!LAYER1:VL1 DMA UNDERFLOW val = 0x%lx, under_flow=%d",
348 val
, kmb
->kmb_under_flow
);
349 /* disable underflow interrupt */
350 kmb_clr_bitmask_lcd(kmb
, LCD_INT_ENABLE
,
351 LAYER1_DMA_FIFO_UNDERFLOW
|
352 LAYER1_DMA_CB_FIFO_UNDERFLOW
|
353 LAYER1_DMA_CR_FIFO_UNDERFLOW
);
354 kmb_set_bitmask_lcd(kmb
, LCD_INT_CLEAR
,
355 LAYER1_DMA_CB_FIFO_UNDERFLOW
|
356 LAYER1_DMA_FIFO_UNDERFLOW
|
357 LAYER1_DMA_CR_FIFO_UNDERFLOW
);
358 /* disable auto restart mode */
359 kmb_clr_bitmask_lcd(kmb
, LCD_LAYERn_DMA_CFG(1),
360 LCD_DMA_LAYER_CONT_PING_PONG_UPDATE
);
365 if (val
& LAYER1_DMA_FIFO_OVERFLOW
)
367 "LAYER1:VL1 DMA OVERFLOW val = 0x%lx", val
);
368 if (val
& LAYER1_DMA_CB_FIFO_OVERFLOW
)
370 "LAYER1:VL1 DMA CB OVERFLOW val = 0x%lx", val
);
371 if (val
& LAYER1_DMA_CR_FIFO_OVERFLOW
)
373 "LAYER1:VL1 DMA CR OVERFLOW val = 0x%lx", val
);
376 if (val
& LAYER2_DMA_FIFO_UNDERFLOW
)
378 "LAYER2:GL0 DMA UNDERFLOW val = 0x%lx", val
);
379 if (val
& LAYER2_DMA_FIFO_OVERFLOW
)
381 "LAYER2:GL0 DMA OVERFLOW val = 0x%lx", val
);
384 if (val
& LAYER3_DMA_FIFO_UNDERFLOW
)
386 "LAYER3:GL1 DMA UNDERFLOW val = 0x%lx", val
);
387 if (val
& LAYER3_DMA_FIFO_OVERFLOW
)
389 "LAYER3:GL1 DMA OVERFLOW val = 0x%lx", val
);
392 spin_unlock(&kmb
->irq_lock
);
394 if (status
& LCD_INT_LAYER
) {
395 /* Clear layer interrupts */
396 kmb_write_lcd(kmb
, LCD_INT_CLEAR
, LCD_INT_LAYER
);
399 /* Clear all interrupts */
400 kmb_set_bitmask_lcd(kmb
, LCD_INT_CLEAR
, 1);
405 static irqreturn_t
kmb_isr(int irq
, void *arg
)
407 struct drm_device
*dev
= (struct drm_device
*)arg
;
413 static void kmb_irq_reset(struct drm_device
*drm
)
415 kmb_write_lcd(to_kmb(drm
), LCD_INT_CLEAR
, 0xFFFF);
416 kmb_write_lcd(to_kmb(drm
), LCD_INT_ENABLE
, 0);
419 static int kmb_irq_install(struct drm_device
*drm
, unsigned int irq
)
421 if (irq
== IRQ_NOTCONNECTED
)
426 return request_irq(irq
, kmb_isr
, 0, drm
->driver
->name
, drm
);
429 static void kmb_irq_uninstall(struct drm_device
*drm
)
431 struct kmb_drm_private
*kmb
= to_kmb(drm
);
434 free_irq(kmb
->irq_lcd
, drm
);
437 DEFINE_DRM_GEM_DMA_FOPS(fops
);
439 static const struct drm_driver kmb_driver
= {
440 .driver_features
= DRIVER_GEM
|
441 DRIVER_MODESET
| DRIVER_ATOMIC
,
444 DRM_GEM_DMA_DRIVER_OPS_VMAP
,
445 DRM_FBDEV_DMA_DRIVER_OPS
,
447 .desc
= "KEEMBAY DISPLAY DRIVER",
449 .major
= DRIVER_MAJOR
,
450 .minor
= DRIVER_MINOR
,
453 static void kmb_remove(struct platform_device
*pdev
)
455 struct device
*dev
= &pdev
->dev
;
456 struct drm_device
*drm
= dev_get_drvdata(dev
);
457 struct kmb_drm_private
*kmb
= to_kmb(drm
);
459 drm_dev_unregister(drm
);
460 drm_kms_helper_poll_fini(drm
);
461 of_node_put(kmb
->crtc
.port
);
462 kmb
->crtc
.port
= NULL
;
463 pm_runtime_get_sync(drm
->dev
);
464 kmb_irq_uninstall(drm
);
465 pm_runtime_put_sync(drm
->dev
);
466 pm_runtime_disable(drm
->dev
);
468 of_reserved_mem_device_release(drm
->dev
);
471 kmb_display_clk_disable(kmb
);
473 dev_set_drvdata(dev
, NULL
);
475 /* Unregister DSI host */
476 kmb_dsi_host_unregister(kmb
->kmb_dsi
);
477 drm_atomic_helper_shutdown(drm
);
480 static int kmb_probe(struct platform_device
*pdev
)
482 struct device
*dev
= get_device(&pdev
->dev
);
483 struct kmb_drm_private
*kmb
;
485 struct device_node
*dsi_in
;
486 struct device_node
*dsi_node
;
487 struct platform_device
*dsi_pdev
;
489 /* The bridge (ADV 7535) will return -EPROBE_DEFER until it
490 * has a mipi_dsi_host to register its device to. So, we
491 * first register the DSI host during probe time, and then return
492 * -EPROBE_DEFER until the bridge is loaded. Probe will be called again
493 * and then the rest of the driver initialization can proceed
494 * afterwards and the bridge can be successfully attached.
496 dsi_in
= of_graph_get_endpoint_by_regs(dev
->of_node
, 0, 0);
498 DRM_ERROR("Failed to get dsi_in node info from DT");
501 dsi_node
= of_graph_get_remote_port_parent(dsi_in
);
504 DRM_ERROR("Failed to get dsi node from DT\n");
508 dsi_pdev
= of_find_device_by_node(dsi_node
);
511 of_node_put(dsi_node
);
512 DRM_ERROR("Failed to get dsi platform device\n");
517 of_node_put(dsi_node
);
518 ret
= kmb_dsi_host_bridge_init(get_device(&dsi_pdev
->dev
));
520 if (ret
== -EPROBE_DEFER
) {
521 return -EPROBE_DEFER
;
523 DRM_ERROR("probe failed to initialize DSI host bridge\n");
527 /* Create DRM device */
528 kmb
= devm_drm_dev_alloc(dev
, &kmb_driver
,
529 struct kmb_drm_private
, drm
);
533 dev_set_drvdata(dev
, &kmb
->drm
);
535 /* Initialize MIPI DSI */
536 kmb
->kmb_dsi
= kmb_dsi_init(dsi_pdev
);
537 if (IS_ERR(kmb
->kmb_dsi
)) {
538 drm_err(&kmb
->drm
, "failed to initialize DSI\n");
539 ret
= PTR_ERR(kmb
->kmb_dsi
);
543 kmb
->kmb_dsi
->dev
= &dsi_pdev
->dev
;
544 kmb
->kmb_dsi
->pdev
= dsi_pdev
;
545 ret
= kmb_hw_init(&kmb
->drm
, 0);
549 ret
= kmb_setup_mode_config(&kmb
->drm
);
553 ret
= kmb_irq_install(&kmb
->drm
, kmb
->irq_lcd
);
555 drm_err(&kmb
->drm
, "failed to install IRQ handler\n");
559 drm_kms_helper_poll_init(&kmb
->drm
);
561 /* Register graphics device with the kernel */
562 ret
= drm_dev_register(&kmb
->drm
, 0);
566 drm_client_setup(&kmb
->drm
, NULL
);
571 drm_kms_helper_poll_fini(&kmb
->drm
);
573 pm_runtime_disable(kmb
->drm
.dev
);
575 drm_crtc_cleanup(&kmb
->crtc
);
576 drm_mode_config_cleanup(&kmb
->drm
);
578 dev_set_drvdata(dev
, NULL
);
579 kmb_dsi_host_unregister(kmb
->kmb_dsi
);
584 static const struct of_device_id kmb_of_match
[] = {
585 {.compatible
= "intel,keembay-display"},
589 MODULE_DEVICE_TABLE(of
, kmb_of_match
);
591 static int __maybe_unused
kmb_pm_suspend(struct device
*dev
)
593 struct drm_device
*drm
= dev_get_drvdata(dev
);
594 struct kmb_drm_private
*kmb
= to_kmb(drm
);
596 drm_kms_helper_poll_disable(drm
);
598 kmb
->state
= drm_atomic_helper_suspend(drm
);
599 if (IS_ERR(kmb
->state
)) {
600 drm_kms_helper_poll_enable(drm
);
601 return PTR_ERR(kmb
->state
);
607 static int __maybe_unused
kmb_pm_resume(struct device
*dev
)
609 struct drm_device
*drm
= dev_get_drvdata(dev
);
610 struct kmb_drm_private
*kmb
= drm
? to_kmb(drm
) : NULL
;
615 drm_atomic_helper_resume(drm
, kmb
->state
);
616 drm_kms_helper_poll_enable(drm
);
621 static SIMPLE_DEV_PM_OPS(kmb_pm_ops
, kmb_pm_suspend
, kmb_pm_resume
);
623 static struct platform_driver kmb_platform_driver
= {
625 .remove
= kmb_remove
,
629 .of_match_table
= kmb_of_match
,
633 drm_module_platform_driver(kmb_platform_driver
);
635 MODULE_AUTHOR("Intel Corporation");
636 MODULE_DESCRIPTION("Keembay Display driver");
637 MODULE_LICENSE("GPL v2");