1 // SPDX-License-Identifier: GPL-2.0
3 // Ingenic JZ47xx KMS driver
5 // Copyright (C) 2019, Paul Cercueil <paul@crapouillou.net>
7 #include "ingenic-drm.h"
9 #include <linux/component.h>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of_device.h>
15 #include <linux/of_reserved_mem.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
19 #include <drm/drm_atomic.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_bridge.h>
22 #include <drm/drm_color_mgmt.h>
23 #include <drm/drm_crtc.h>
24 #include <drm/drm_crtc_helper.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_gem_cma_helper.h>
27 #include <drm/drm_fb_cma_helper.h>
28 #include <drm/drm_fb_helper.h>
29 #include <drm/drm_fourcc.h>
30 #include <drm/drm_gem_framebuffer_helper.h>
31 #include <drm/drm_irq.h>
32 #include <drm/drm_managed.h>
33 #include <drm/drm_of.h>
34 #include <drm/drm_panel.h>
35 #include <drm/drm_plane.h>
36 #include <drm/drm_plane_helper.h>
37 #include <drm/drm_probe_helper.h>
38 #include <drm/drm_simple_kms_helper.h>
39 #include <drm/drm_vblank.h>
41 struct ingenic_dma_hwdesc
{
48 struct ingenic_dma_hwdescs
{
49 struct ingenic_dma_hwdesc hwdesc_f0
;
50 struct ingenic_dma_hwdesc hwdesc_f1
;
51 struct ingenic_dma_hwdesc hwdesc_pal
;
52 u16 palette
[256] __aligned(16);
58 unsigned int max_width
, max_height
;
59 const u32
*formats_f0
, *formats_f1
;
60 unsigned int num_formats_f0
, num_formats_f1
;
64 struct drm_device drm
;
66 * f1 (aka. foreground1) is our primary plane, on top of which
67 * f0 (aka. foreground0) can be overlayed. Z-order is fixed in
68 * hardware and cannot be changed.
70 struct drm_plane f0
, f1
, *ipu_plane
;
75 struct clk
*lcd_clk
, *pix_clk
;
76 const struct jz_soc_info
*soc_info
;
78 struct ingenic_dma_hwdescs
*dma_hwdescs
;
79 dma_addr_t dma_hwdescs_phys
;
85 * clk_mutex is used to synchronize the pixel clock rate update with
86 * the VBLANK. When the pixel clock's parent clock needs to be updated,
87 * clock_nb's notifier function will lock the mutex, then wait until the
88 * next VBLANK. At that point, the parent clock's rate can be updated,
89 * and the mutex is then unlocked. If an atomic commit happens in the
90 * meantime, it will lock on the mutex, effectively waiting until the
91 * clock update process finishes. Finally, the pixel clock's rate will
92 * be recomputed when the mutex has been released, in the pending atomic
93 * commit, or a future one.
95 struct mutex clk_mutex
;
97 struct notifier_block clock_nb
;
100 static bool ingenic_drm_writeable_reg(struct device
*dev
, unsigned int reg
)
105 case JZ_REG_LCD_FID0
:
106 case JZ_REG_LCD_CMD0
:
108 case JZ_REG_LCD_FID1
:
109 case JZ_REG_LCD_CMD1
:
116 static const struct regmap_config ingenic_drm_regmap_config
= {
121 .max_register
= JZ_REG_LCD_SIZE1
,
122 .writeable_reg
= ingenic_drm_writeable_reg
,
125 static inline struct ingenic_drm
*drm_device_get_priv(struct drm_device
*drm
)
127 return container_of(drm
, struct ingenic_drm
, drm
);
130 static inline struct ingenic_drm
*drm_crtc_get_priv(struct drm_crtc
*crtc
)
132 return container_of(crtc
, struct ingenic_drm
, crtc
);
135 static inline struct ingenic_drm
*drm_nb_get_priv(struct notifier_block
*nb
)
137 return container_of(nb
, struct ingenic_drm
, clock_nb
);
140 static int ingenic_drm_update_pixclk(struct notifier_block
*nb
,
141 unsigned long action
,
144 struct ingenic_drm
*priv
= drm_nb_get_priv(nb
);
147 case PRE_RATE_CHANGE
:
148 mutex_lock(&priv
->clk_mutex
);
149 priv
->update_clk_rate
= true;
150 drm_crtc_wait_one_vblank(&priv
->crtc
);
153 mutex_unlock(&priv
->clk_mutex
);
158 static void ingenic_drm_crtc_atomic_enable(struct drm_crtc
*crtc
,
159 struct drm_atomic_state
*state
)
161 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
163 regmap_write(priv
->map
, JZ_REG_LCD_STATE
, 0);
165 regmap_update_bits(priv
->map
, JZ_REG_LCD_CTRL
,
166 JZ_LCD_CTRL_ENABLE
| JZ_LCD_CTRL_DISABLE
,
169 drm_crtc_vblank_on(crtc
);
172 static void ingenic_drm_crtc_atomic_disable(struct drm_crtc
*crtc
,
173 struct drm_atomic_state
*state
)
175 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
178 drm_crtc_vblank_off(crtc
);
180 regmap_update_bits(priv
->map
, JZ_REG_LCD_CTRL
,
181 JZ_LCD_CTRL_DISABLE
, JZ_LCD_CTRL_DISABLE
);
183 regmap_read_poll_timeout(priv
->map
, JZ_REG_LCD_STATE
, var
,
184 var
& JZ_LCD_STATE_DISABLED
,
188 static void ingenic_drm_crtc_update_timings(struct ingenic_drm
*priv
,
189 struct drm_display_mode
*mode
)
191 unsigned int vpe
, vds
, vde
, vt
, hpe
, hds
, hde
, ht
;
193 vpe
= mode
->vsync_end
- mode
->vsync_start
;
194 vds
= mode
->vtotal
- mode
->vsync_start
;
195 vde
= vds
+ mode
->vdisplay
;
196 vt
= vde
+ mode
->vsync_start
- mode
->vdisplay
;
198 hpe
= mode
->hsync_end
- mode
->hsync_start
;
199 hds
= mode
->htotal
- mode
->hsync_start
;
200 hde
= hds
+ mode
->hdisplay
;
201 ht
= hde
+ mode
->hsync_start
- mode
->hdisplay
;
203 regmap_write(priv
->map
, JZ_REG_LCD_VSYNC
,
204 0 << JZ_LCD_VSYNC_VPS_OFFSET
|
205 vpe
<< JZ_LCD_VSYNC_VPE_OFFSET
);
207 regmap_write(priv
->map
, JZ_REG_LCD_HSYNC
,
208 0 << JZ_LCD_HSYNC_HPS_OFFSET
|
209 hpe
<< JZ_LCD_HSYNC_HPE_OFFSET
);
211 regmap_write(priv
->map
, JZ_REG_LCD_VAT
,
212 ht
<< JZ_LCD_VAT_HT_OFFSET
|
213 vt
<< JZ_LCD_VAT_VT_OFFSET
);
215 regmap_write(priv
->map
, JZ_REG_LCD_DAH
,
216 hds
<< JZ_LCD_DAH_HDS_OFFSET
|
217 hde
<< JZ_LCD_DAH_HDE_OFFSET
);
218 regmap_write(priv
->map
, JZ_REG_LCD_DAV
,
219 vds
<< JZ_LCD_DAV_VDS_OFFSET
|
220 vde
<< JZ_LCD_DAV_VDE_OFFSET
);
222 if (priv
->panel_is_sharp
) {
223 regmap_write(priv
->map
, JZ_REG_LCD_PS
, hde
<< 16 | (hde
+ 1));
224 regmap_write(priv
->map
, JZ_REG_LCD_CLS
, hde
<< 16 | (hde
+ 1));
225 regmap_write(priv
->map
, JZ_REG_LCD_SPL
, hpe
<< 16 | (hpe
+ 1));
226 regmap_write(priv
->map
, JZ_REG_LCD_REV
, mode
->htotal
<< 16);
229 regmap_set_bits(priv
->map
, JZ_REG_LCD_CTRL
,
230 JZ_LCD_CTRL_OFUP
| JZ_LCD_CTRL_BURST_16
);
233 * IPU restart - specify how much time the LCDC will wait before
234 * transferring a new frame from the IPU. The value is the one
235 * suggested in the programming manual.
237 regmap_write(priv
->map
, JZ_REG_LCD_IPUR
, JZ_LCD_IPUR_IPUREN
|
238 (ht
* vpe
/ 3) << JZ_LCD_IPUR_IPUR_LSB
);
241 static int ingenic_drm_crtc_atomic_check(struct drm_crtc
*crtc
,
242 struct drm_atomic_state
*state
)
244 struct drm_crtc_state
*crtc_state
= drm_atomic_get_new_crtc_state(state
,
246 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
247 struct drm_plane_state
*f1_state
, *f0_state
, *ipu_state
= NULL
;
249 if (crtc_state
->gamma_lut
&&
250 drm_color_lut_size(crtc_state
->gamma_lut
) != ARRAY_SIZE(priv
->dma_hwdescs
->palette
)) {
251 dev_dbg(priv
->dev
, "Invalid palette size\n");
255 if (drm_atomic_crtc_needs_modeset(crtc_state
) && priv
->soc_info
->has_osd
) {
256 f1_state
= drm_atomic_get_plane_state(crtc_state
->state
,
258 if (IS_ERR(f1_state
))
259 return PTR_ERR(f1_state
);
261 f0_state
= drm_atomic_get_plane_state(crtc_state
->state
,
263 if (IS_ERR(f0_state
))
264 return PTR_ERR(f0_state
);
266 if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU
) && priv
->ipu_plane
) {
267 ipu_state
= drm_atomic_get_plane_state(crtc_state
->state
,
269 if (IS_ERR(ipu_state
))
270 return PTR_ERR(ipu_state
);
272 /* IPU and F1 planes cannot be enabled at the same time. */
273 if (f1_state
->fb
&& ipu_state
->fb
) {
274 dev_dbg(priv
->dev
, "Cannot enable both F1 and IPU\n");
279 /* If all the planes are disabled, we won't get a VBLANK IRQ */
280 priv
->no_vblank
= !f1_state
->fb
&& !f0_state
->fb
&&
281 !(ipu_state
&& ipu_state
->fb
);
287 static enum drm_mode_status
288 ingenic_drm_crtc_mode_valid(struct drm_crtc
*crtc
, const struct drm_display_mode
*mode
)
290 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
293 if (mode
->hdisplay
> priv
->soc_info
->max_width
)
294 return MODE_BAD_HVALUE
;
295 if (mode
->vdisplay
> priv
->soc_info
->max_height
)
296 return MODE_BAD_VVALUE
;
298 rate
= clk_round_rate(priv
->pix_clk
, mode
->clock
* 1000);
300 return MODE_CLOCK_RANGE
;
305 static void ingenic_drm_crtc_atomic_begin(struct drm_crtc
*crtc
,
306 struct drm_atomic_state
*state
)
308 struct drm_crtc_state
*crtc_state
= drm_atomic_get_new_crtc_state(state
,
310 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
313 if (priv
->soc_info
->has_osd
&&
314 drm_atomic_crtc_needs_modeset(crtc_state
)) {
316 * If IPU plane is enabled, enable IPU as source for the F1
317 * plane; otherwise use regular DMA.
319 if (priv
->ipu_plane
&& priv
->ipu_plane
->state
->fb
)
320 ctrl
|= JZ_LCD_OSDCTRL_IPU
;
322 regmap_update_bits(priv
->map
, JZ_REG_LCD_OSDCTRL
,
323 JZ_LCD_OSDCTRL_IPU
, ctrl
);
327 static void ingenic_drm_crtc_atomic_flush(struct drm_crtc
*crtc
,
328 struct drm_atomic_state
*state
)
330 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
331 struct drm_crtc_state
*crtc_state
= drm_atomic_get_new_crtc_state(state
,
333 struct drm_pending_vblank_event
*event
= crtc_state
->event
;
335 if (drm_atomic_crtc_needs_modeset(crtc_state
)) {
336 ingenic_drm_crtc_update_timings(priv
, &crtc_state
->mode
);
337 priv
->update_clk_rate
= true;
340 if (priv
->update_clk_rate
) {
341 mutex_lock(&priv
->clk_mutex
);
342 clk_set_rate(priv
->pix_clk
,
343 crtc_state
->adjusted_mode
.clock
* 1000);
344 priv
->update_clk_rate
= false;
345 mutex_unlock(&priv
->clk_mutex
);
349 crtc_state
->event
= NULL
;
351 spin_lock_irq(&crtc
->dev
->event_lock
);
352 if (drm_crtc_vblank_get(crtc
) == 0)
353 drm_crtc_arm_vblank_event(crtc
, event
);
355 drm_crtc_send_vblank_event(crtc
, event
);
356 spin_unlock_irq(&crtc
->dev
->event_lock
);
360 static int ingenic_drm_plane_atomic_check(struct drm_plane
*plane
,
361 struct drm_plane_state
*state
)
363 struct ingenic_drm
*priv
= drm_device_get_priv(plane
->dev
);
364 struct drm_crtc_state
*crtc_state
;
365 struct drm_crtc
*crtc
= state
->crtc
?: plane
->state
->crtc
;
371 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, crtc
);
372 if (WARN_ON(!crtc_state
))
375 ret
= drm_atomic_helper_check_plane_state(state
, crtc_state
,
376 DRM_PLANE_HELPER_NO_SCALING
,
377 DRM_PLANE_HELPER_NO_SCALING
,
378 priv
->soc_info
->has_osd
,
384 * If OSD is not available, check that the width/height match.
385 * Note that state->src_* are in 16.16 fixed-point format.
387 if (!priv
->soc_info
->has_osd
&&
388 (state
->src_x
!= 0 ||
389 (state
->src_w
>> 16) != state
->crtc_w
||
390 (state
->src_h
>> 16) != state
->crtc_h
))
394 * Require full modeset if enabling or disabling a plane, or changing
395 * its position, size or depth.
397 if (priv
->soc_info
->has_osd
&&
398 (!plane
->state
->fb
|| !state
->fb
||
399 plane
->state
->crtc_x
!= state
->crtc_x
||
400 plane
->state
->crtc_y
!= state
->crtc_y
||
401 plane
->state
->crtc_w
!= state
->crtc_w
||
402 plane
->state
->crtc_h
!= state
->crtc_h
||
403 plane
->state
->fb
->format
->format
!= state
->fb
->format
->format
))
404 crtc_state
->mode_changed
= true;
409 static void ingenic_drm_plane_enable(struct ingenic_drm
*priv
,
410 struct drm_plane
*plane
)
414 if (priv
->soc_info
->has_osd
) {
415 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
416 en_bit
= JZ_LCD_OSDC_F1EN
;
418 en_bit
= JZ_LCD_OSDC_F0EN
;
420 regmap_set_bits(priv
->map
, JZ_REG_LCD_OSDC
, en_bit
);
424 void ingenic_drm_plane_disable(struct device
*dev
, struct drm_plane
*plane
)
426 struct ingenic_drm
*priv
= dev_get_drvdata(dev
);
429 if (priv
->soc_info
->has_osd
) {
430 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
)
431 en_bit
= JZ_LCD_OSDC_F1EN
;
433 en_bit
= JZ_LCD_OSDC_F0EN
;
435 regmap_clear_bits(priv
->map
, JZ_REG_LCD_OSDC
, en_bit
);
439 static void ingenic_drm_plane_atomic_disable(struct drm_plane
*plane
,
440 struct drm_plane_state
*old_state
)
442 struct ingenic_drm
*priv
= drm_device_get_priv(plane
->dev
);
444 ingenic_drm_plane_disable(priv
->dev
, plane
);
447 void ingenic_drm_plane_config(struct device
*dev
,
448 struct drm_plane
*plane
, u32 fourcc
)
450 struct ingenic_drm
*priv
= dev_get_drvdata(dev
);
451 struct drm_plane_state
*state
= plane
->state
;
452 unsigned int xy_reg
, size_reg
;
453 unsigned int ctrl
= 0;
455 ingenic_drm_plane_enable(priv
, plane
);
457 if (priv
->soc_info
->has_osd
&&
458 plane
->type
== DRM_PLANE_TYPE_PRIMARY
) {
460 case DRM_FORMAT_XRGB1555
:
461 ctrl
|= JZ_LCD_OSDCTRL_RGB555
;
463 case DRM_FORMAT_RGB565
:
464 ctrl
|= JZ_LCD_OSDCTRL_BPP_15_16
;
466 case DRM_FORMAT_RGB888
:
467 ctrl
|= JZ_LCD_OSDCTRL_BPP_24_COMP
;
469 case DRM_FORMAT_XRGB8888
:
470 ctrl
|= JZ_LCD_OSDCTRL_BPP_18_24
;
472 case DRM_FORMAT_XRGB2101010
:
473 ctrl
|= JZ_LCD_OSDCTRL_BPP_30
;
477 regmap_update_bits(priv
->map
, JZ_REG_LCD_OSDCTRL
,
478 JZ_LCD_OSDCTRL_BPP_MASK
, ctrl
);
482 ctrl
|= JZ_LCD_CTRL_BPP_8
;
484 case DRM_FORMAT_XRGB1555
:
485 ctrl
|= JZ_LCD_CTRL_RGB555
;
487 case DRM_FORMAT_RGB565
:
488 ctrl
|= JZ_LCD_CTRL_BPP_15_16
;
490 case DRM_FORMAT_RGB888
:
491 ctrl
|= JZ_LCD_CTRL_BPP_24_COMP
;
493 case DRM_FORMAT_XRGB8888
:
494 ctrl
|= JZ_LCD_CTRL_BPP_18_24
;
496 case DRM_FORMAT_XRGB2101010
:
497 ctrl
|= JZ_LCD_CTRL_BPP_30
;
501 regmap_update_bits(priv
->map
, JZ_REG_LCD_CTRL
,
502 JZ_LCD_CTRL_BPP_MASK
, ctrl
);
505 if (priv
->soc_info
->has_osd
) {
506 if (plane
->type
== DRM_PLANE_TYPE_PRIMARY
) {
507 xy_reg
= JZ_REG_LCD_XYP1
;
508 size_reg
= JZ_REG_LCD_SIZE1
;
510 xy_reg
= JZ_REG_LCD_XYP0
;
511 size_reg
= JZ_REG_LCD_SIZE0
;
514 regmap_write(priv
->map
, xy_reg
,
515 state
->crtc_x
<< JZ_LCD_XYP01_XPOS_LSB
|
516 state
->crtc_y
<< JZ_LCD_XYP01_YPOS_LSB
);
517 regmap_write(priv
->map
, size_reg
,
518 state
->crtc_w
<< JZ_LCD_SIZE01_WIDTH_LSB
|
519 state
->crtc_h
<< JZ_LCD_SIZE01_HEIGHT_LSB
);
523 static void ingenic_drm_update_palette(struct ingenic_drm
*priv
,
524 const struct drm_color_lut
*lut
)
528 for (i
= 0; i
< ARRAY_SIZE(priv
->dma_hwdescs
->palette
); i
++) {
529 u16 color
= drm_color_lut_extract(lut
[i
].red
, 5) << 11
530 | drm_color_lut_extract(lut
[i
].green
, 6) << 5
531 | drm_color_lut_extract(lut
[i
].blue
, 5);
533 priv
->dma_hwdescs
->palette
[i
] = color
;
537 static void ingenic_drm_plane_atomic_update(struct drm_plane
*plane
,
538 struct drm_plane_state
*oldstate
)
540 struct ingenic_drm
*priv
= drm_device_get_priv(plane
->dev
);
541 struct drm_plane_state
*state
= plane
->state
;
542 struct drm_crtc_state
*crtc_state
;
543 struct ingenic_dma_hwdesc
*hwdesc
;
544 unsigned int width
, height
, cpp
, offset
;
548 if (state
&& state
->fb
) {
549 crtc_state
= state
->crtc
->state
;
551 addr
= drm_fb_cma_get_gem_addr(state
->fb
, state
, 0);
552 width
= state
->src_w
>> 16;
553 height
= state
->src_h
>> 16;
554 cpp
= state
->fb
->format
->cpp
[0];
556 if (priv
->soc_info
->has_osd
&& plane
->type
== DRM_PLANE_TYPE_OVERLAY
)
557 hwdesc
= &priv
->dma_hwdescs
->hwdesc_f0
;
559 hwdesc
= &priv
->dma_hwdescs
->hwdesc_f1
;
562 hwdesc
->cmd
= JZ_LCD_CMD_EOF_IRQ
| (width
* height
* cpp
/ 4);
564 if (drm_atomic_crtc_needs_modeset(crtc_state
)) {
565 fourcc
= state
->fb
->format
->format
;
567 ingenic_drm_plane_config(priv
->dev
, plane
, fourcc
);
569 if (fourcc
== DRM_FORMAT_C8
)
570 offset
= offsetof(struct ingenic_dma_hwdescs
, hwdesc_pal
);
572 offset
= offsetof(struct ingenic_dma_hwdescs
, hwdesc_f0
);
574 priv
->dma_hwdescs
->hwdesc_f0
.next
= priv
->dma_hwdescs_phys
+ offset
;
576 crtc_state
->color_mgmt_changed
= fourcc
== DRM_FORMAT_C8
;
579 if (crtc_state
->color_mgmt_changed
)
580 ingenic_drm_update_palette(priv
, crtc_state
->gamma_lut
->data
);
584 static void ingenic_drm_encoder_atomic_mode_set(struct drm_encoder
*encoder
,
585 struct drm_crtc_state
*crtc_state
,
586 struct drm_connector_state
*conn_state
)
588 struct ingenic_drm
*priv
= drm_device_get_priv(encoder
->dev
);
589 struct drm_display_mode
*mode
= &crtc_state
->adjusted_mode
;
590 struct drm_connector
*conn
= conn_state
->connector
;
591 struct drm_display_info
*info
= &conn
->display_info
;
594 priv
->panel_is_sharp
= info
->bus_flags
& DRM_BUS_FLAG_SHARP_SIGNALS
;
596 if (priv
->panel_is_sharp
) {
597 cfg
= JZ_LCD_CFG_MODE_SPECIAL_TFT_1
| JZ_LCD_CFG_REV_POLARITY
;
599 cfg
= JZ_LCD_CFG_PS_DISABLE
| JZ_LCD_CFG_CLS_DISABLE
600 | JZ_LCD_CFG_SPL_DISABLE
| JZ_LCD_CFG_REV_DISABLE
;
603 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
)
604 cfg
|= JZ_LCD_CFG_HSYNC_ACTIVE_LOW
;
605 if (mode
->flags
& DRM_MODE_FLAG_NVSYNC
)
606 cfg
|= JZ_LCD_CFG_VSYNC_ACTIVE_LOW
;
607 if (info
->bus_flags
& DRM_BUS_FLAG_DE_LOW
)
608 cfg
|= JZ_LCD_CFG_DE_ACTIVE_LOW
;
609 if (info
->bus_flags
& DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE
)
610 cfg
|= JZ_LCD_CFG_PCLK_FALLING_EDGE
;
612 if (!priv
->panel_is_sharp
) {
613 if (conn
->connector_type
== DRM_MODE_CONNECTOR_TV
) {
614 if (mode
->flags
& DRM_MODE_FLAG_INTERLACE
)
615 cfg
|= JZ_LCD_CFG_MODE_TV_OUT_I
;
617 cfg
|= JZ_LCD_CFG_MODE_TV_OUT_P
;
619 switch (*info
->bus_formats
) {
620 case MEDIA_BUS_FMT_RGB565_1X16
:
621 cfg
|= JZ_LCD_CFG_MODE_GENERIC_16BIT
;
623 case MEDIA_BUS_FMT_RGB666_1X18
:
624 cfg
|= JZ_LCD_CFG_MODE_GENERIC_18BIT
;
626 case MEDIA_BUS_FMT_RGB888_1X24
:
627 cfg
|= JZ_LCD_CFG_MODE_GENERIC_24BIT
;
629 case MEDIA_BUS_FMT_RGB888_3X8
:
630 cfg
|= JZ_LCD_CFG_MODE_8BIT_SERIAL
;
638 regmap_write(priv
->map
, JZ_REG_LCD_CFG
, cfg
);
641 static int ingenic_drm_encoder_atomic_check(struct drm_encoder
*encoder
,
642 struct drm_crtc_state
*crtc_state
,
643 struct drm_connector_state
*conn_state
)
645 struct drm_display_info
*info
= &conn_state
->connector
->display_info
;
647 if (info
->num_bus_formats
!= 1)
650 if (conn_state
->connector
->connector_type
== DRM_MODE_CONNECTOR_TV
)
653 switch (*info
->bus_formats
) {
654 case MEDIA_BUS_FMT_RGB565_1X16
:
655 case MEDIA_BUS_FMT_RGB666_1X18
:
656 case MEDIA_BUS_FMT_RGB888_1X24
:
657 case MEDIA_BUS_FMT_RGB888_3X8
:
664 static void ingenic_drm_atomic_helper_commit_tail(struct drm_atomic_state
*old_state
)
667 * Just your regular drm_atomic_helper_commit_tail(), but only calls
668 * drm_atomic_helper_wait_for_vblanks() if priv->no_vblank.
670 struct drm_device
*dev
= old_state
->dev
;
671 struct ingenic_drm
*priv
= drm_device_get_priv(dev
);
673 drm_atomic_helper_commit_modeset_disables(dev
, old_state
);
675 drm_atomic_helper_commit_planes(dev
, old_state
, 0);
677 drm_atomic_helper_commit_modeset_enables(dev
, old_state
);
679 drm_atomic_helper_commit_hw_done(old_state
);
681 if (!priv
->no_vblank
)
682 drm_atomic_helper_wait_for_vblanks(dev
, old_state
);
684 drm_atomic_helper_cleanup_planes(dev
, old_state
);
687 static irqreturn_t
ingenic_drm_irq_handler(int irq
, void *arg
)
689 struct ingenic_drm
*priv
= drm_device_get_priv(arg
);
692 regmap_read(priv
->map
, JZ_REG_LCD_STATE
, &state
);
694 regmap_update_bits(priv
->map
, JZ_REG_LCD_STATE
,
695 JZ_LCD_STATE_EOF_IRQ
, 0);
697 if (state
& JZ_LCD_STATE_EOF_IRQ
)
698 drm_crtc_handle_vblank(&priv
->crtc
);
703 static int ingenic_drm_enable_vblank(struct drm_crtc
*crtc
)
705 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
707 regmap_update_bits(priv
->map
, JZ_REG_LCD_CTRL
,
708 JZ_LCD_CTRL_EOF_IRQ
, JZ_LCD_CTRL_EOF_IRQ
);
713 static void ingenic_drm_disable_vblank(struct drm_crtc
*crtc
)
715 struct ingenic_drm
*priv
= drm_crtc_get_priv(crtc
);
717 regmap_update_bits(priv
->map
, JZ_REG_LCD_CTRL
, JZ_LCD_CTRL_EOF_IRQ
, 0);
720 DEFINE_DRM_GEM_CMA_FOPS(ingenic_drm_fops
);
722 static const struct drm_driver ingenic_drm_driver_data
= {
723 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_ATOMIC
,
724 .name
= "ingenic-drm",
725 .desc
= "DRM module for Ingenic SoCs",
731 .fops
= &ingenic_drm_fops
,
732 DRM_GEM_CMA_DRIVER_OPS
,
734 .irq_handler
= ingenic_drm_irq_handler
,
737 static const struct drm_plane_funcs ingenic_drm_primary_plane_funcs
= {
738 .update_plane
= drm_atomic_helper_update_plane
,
739 .disable_plane
= drm_atomic_helper_disable_plane
,
740 .reset
= drm_atomic_helper_plane_reset
,
741 .destroy
= drm_plane_cleanup
,
743 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
744 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
747 static const struct drm_crtc_funcs ingenic_drm_crtc_funcs
= {
748 .set_config
= drm_atomic_helper_set_config
,
749 .page_flip
= drm_atomic_helper_page_flip
,
750 .reset
= drm_atomic_helper_crtc_reset
,
751 .destroy
= drm_crtc_cleanup
,
753 .atomic_duplicate_state
= drm_atomic_helper_crtc_duplicate_state
,
754 .atomic_destroy_state
= drm_atomic_helper_crtc_destroy_state
,
756 .enable_vblank
= ingenic_drm_enable_vblank
,
757 .disable_vblank
= ingenic_drm_disable_vblank
,
759 .gamma_set
= drm_atomic_helper_legacy_gamma_set
,
762 static const struct drm_plane_helper_funcs ingenic_drm_plane_helper_funcs
= {
763 .atomic_update
= ingenic_drm_plane_atomic_update
,
764 .atomic_check
= ingenic_drm_plane_atomic_check
,
765 .atomic_disable
= ingenic_drm_plane_atomic_disable
,
766 .prepare_fb
= drm_gem_fb_prepare_fb
,
769 static const struct drm_crtc_helper_funcs ingenic_drm_crtc_helper_funcs
= {
770 .atomic_enable
= ingenic_drm_crtc_atomic_enable
,
771 .atomic_disable
= ingenic_drm_crtc_atomic_disable
,
772 .atomic_begin
= ingenic_drm_crtc_atomic_begin
,
773 .atomic_flush
= ingenic_drm_crtc_atomic_flush
,
774 .atomic_check
= ingenic_drm_crtc_atomic_check
,
775 .mode_valid
= ingenic_drm_crtc_mode_valid
,
778 static const struct drm_encoder_helper_funcs ingenic_drm_encoder_helper_funcs
= {
779 .atomic_mode_set
= ingenic_drm_encoder_atomic_mode_set
,
780 .atomic_check
= ingenic_drm_encoder_atomic_check
,
783 static const struct drm_mode_config_funcs ingenic_drm_mode_config_funcs
= {
784 .fb_create
= drm_gem_fb_create
,
785 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
786 .atomic_check
= drm_atomic_helper_check
,
787 .atomic_commit
= drm_atomic_helper_commit
,
790 static struct drm_mode_config_helper_funcs ingenic_drm_mode_config_helpers
= {
791 .atomic_commit_tail
= ingenic_drm_atomic_helper_commit_tail
,
794 static void ingenic_drm_unbind_all(void *d
)
796 struct ingenic_drm
*priv
= d
;
798 component_unbind_all(priv
->dev
, &priv
->drm
);
801 static void __maybe_unused
ingenic_drm_release_rmem(void *d
)
803 of_reserved_mem_device_release(d
);
806 static int ingenic_drm_bind(struct device
*dev
, bool has_components
)
808 struct platform_device
*pdev
= to_platform_device(dev
);
809 const struct jz_soc_info
*soc_info
;
810 struct ingenic_drm
*priv
;
811 struct clk
*parent_clk
;
812 struct drm_bridge
*bridge
;
813 struct drm_panel
*panel
;
814 struct drm_encoder
*encoder
;
815 struct drm_device
*drm
;
818 unsigned int i
, clone_mask
= 0;
819 dma_addr_t dma_hwdesc_phys_f0
, dma_hwdesc_phys_f1
;
822 soc_info
= of_device_get_match_data(dev
);
824 dev_err(dev
, "Missing platform data\n");
828 if (IS_ENABLED(CONFIG_OF_RESERVED_MEM
)) {
829 ret
= of_reserved_mem_device_init(dev
);
831 if (ret
&& ret
!= -ENODEV
)
832 dev_warn(dev
, "Failed to get reserved memory: %d\n", ret
);
835 ret
= devm_add_action_or_reset(dev
, ingenic_drm_release_rmem
, dev
);
841 priv
= devm_drm_dev_alloc(dev
, &ingenic_drm_driver_data
,
842 struct ingenic_drm
, drm
);
844 return PTR_ERR(priv
);
846 priv
->soc_info
= soc_info
;
850 platform_set_drvdata(pdev
, priv
);
852 ret
= drmm_mode_config_init(drm
);
856 drm
->mode_config
.min_width
= 0;
857 drm
->mode_config
.min_height
= 0;
858 drm
->mode_config
.max_width
= soc_info
->max_width
;
859 drm
->mode_config
.max_height
= 4095;
860 drm
->mode_config
.funcs
= &ingenic_drm_mode_config_funcs
;
861 drm
->mode_config
.helper_private
= &ingenic_drm_mode_config_helpers
;
863 base
= devm_platform_ioremap_resource(pdev
, 0);
865 dev_err(dev
, "Failed to get memory resource\n");
866 return PTR_ERR(base
);
869 priv
->map
= devm_regmap_init_mmio(dev
, base
,
870 &ingenic_drm_regmap_config
);
871 if (IS_ERR(priv
->map
)) {
872 dev_err(dev
, "Failed to create regmap\n");
873 return PTR_ERR(priv
->map
);
876 irq
= platform_get_irq(pdev
, 0);
880 if (soc_info
->needs_dev_clk
) {
881 priv
->lcd_clk
= devm_clk_get(dev
, "lcd");
882 if (IS_ERR(priv
->lcd_clk
)) {
883 dev_err(dev
, "Failed to get lcd clock\n");
884 return PTR_ERR(priv
->lcd_clk
);
888 priv
->pix_clk
= devm_clk_get(dev
, "lcd_pclk");
889 if (IS_ERR(priv
->pix_clk
)) {
890 dev_err(dev
, "Failed to get pixel clock\n");
891 return PTR_ERR(priv
->pix_clk
);
894 priv
->dma_hwdescs
= dmam_alloc_coherent(dev
,
895 sizeof(*priv
->dma_hwdescs
),
896 &priv
->dma_hwdescs_phys
,
898 if (!priv
->dma_hwdescs
)
902 /* Configure DMA hwdesc for foreground0 plane */
903 dma_hwdesc_phys_f0
= priv
->dma_hwdescs_phys
904 + offsetof(struct ingenic_dma_hwdescs
, hwdesc_f0
);
905 priv
->dma_hwdescs
->hwdesc_f0
.next
= dma_hwdesc_phys_f0
;
906 priv
->dma_hwdescs
->hwdesc_f0
.id
= 0xf0;
908 /* Configure DMA hwdesc for foreground1 plane */
909 dma_hwdesc_phys_f1
= priv
->dma_hwdescs_phys
910 + offsetof(struct ingenic_dma_hwdescs
, hwdesc_f1
);
911 priv
->dma_hwdescs
->hwdesc_f1
.next
= dma_hwdesc_phys_f1
;
912 priv
->dma_hwdescs
->hwdesc_f1
.id
= 0xf1;
914 /* Configure DMA hwdesc for palette */
915 priv
->dma_hwdescs
->hwdesc_pal
.next
= priv
->dma_hwdescs_phys
916 + offsetof(struct ingenic_dma_hwdescs
, hwdesc_f0
);
917 priv
->dma_hwdescs
->hwdesc_pal
.id
= 0xc0;
918 priv
->dma_hwdescs
->hwdesc_pal
.addr
= priv
->dma_hwdescs_phys
919 + offsetof(struct ingenic_dma_hwdescs
, palette
);
920 priv
->dma_hwdescs
->hwdesc_pal
.cmd
= JZ_LCD_CMD_ENABLE_PAL
921 | (sizeof(priv
->dma_hwdescs
->palette
) / 4);
923 if (soc_info
->has_osd
)
924 priv
->ipu_plane
= drm_plane_from_index(drm
, 0);
926 drm_plane_helper_add(&priv
->f1
, &ingenic_drm_plane_helper_funcs
);
928 ret
= drm_universal_plane_init(drm
, &priv
->f1
, 1,
929 &ingenic_drm_primary_plane_funcs
,
930 priv
->soc_info
->formats_f1
,
931 priv
->soc_info
->num_formats_f1
,
932 NULL
, DRM_PLANE_TYPE_PRIMARY
, NULL
);
934 dev_err(dev
, "Failed to register plane: %i\n", ret
);
938 drm_crtc_helper_add(&priv
->crtc
, &ingenic_drm_crtc_helper_funcs
);
940 ret
= drm_crtc_init_with_planes(drm
, &priv
->crtc
, &priv
->f1
,
941 NULL
, &ingenic_drm_crtc_funcs
, NULL
);
943 dev_err(dev
, "Failed to init CRTC: %i\n", ret
);
947 drm_crtc_enable_color_mgmt(&priv
->crtc
, 0, false,
948 ARRAY_SIZE(priv
->dma_hwdescs
->palette
));
950 if (soc_info
->has_osd
) {
951 drm_plane_helper_add(&priv
->f0
,
952 &ingenic_drm_plane_helper_funcs
);
954 ret
= drm_universal_plane_init(drm
, &priv
->f0
, 1,
955 &ingenic_drm_primary_plane_funcs
,
956 priv
->soc_info
->formats_f0
,
957 priv
->soc_info
->num_formats_f0
,
958 NULL
, DRM_PLANE_TYPE_OVERLAY
,
961 dev_err(dev
, "Failed to register overlay plane: %i\n",
966 if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU
) && has_components
) {
967 ret
= component_bind_all(dev
, drm
);
969 if (ret
!= -EPROBE_DEFER
)
970 dev_err(dev
, "Failed to bind components: %i\n", ret
);
974 ret
= devm_add_action_or_reset(dev
, ingenic_drm_unbind_all
, priv
);
978 priv
->ipu_plane
= drm_plane_from_index(drm
, 2);
979 if (!priv
->ipu_plane
) {
980 dev_err(dev
, "Failed to retrieve IPU plane\n");
987 ret
= drm_of_find_panel_or_bridge(dev
->of_node
, 0, i
, &panel
, &bridge
);
990 break; /* we're done */
991 if (ret
!= -EPROBE_DEFER
)
992 dev_err(dev
, "Failed to get bridge handle\n");
997 bridge
= devm_drm_panel_bridge_add_typed(dev
, panel
,
998 DRM_MODE_CONNECTOR_DPI
);
1000 encoder
= devm_kzalloc(dev
, sizeof(*encoder
), GFP_KERNEL
);
1004 encoder
->possible_crtcs
= 1;
1006 drm_encoder_helper_add(encoder
, &ingenic_drm_encoder_helper_funcs
);
1008 ret
= drm_simple_encoder_init(drm
, encoder
, DRM_MODE_ENCODER_DPI
);
1010 dev_err(dev
, "Failed to init encoder: %d\n", ret
);
1014 ret
= drm_bridge_attach(encoder
, bridge
, NULL
, 0);
1016 dev_err(dev
, "Unable to attach bridge\n");
1021 drm_for_each_encoder(encoder
, drm
) {
1022 clone_mask
|= BIT(drm_encoder_index(encoder
));
1025 drm_for_each_encoder(encoder
, drm
) {
1026 encoder
->possible_clones
= clone_mask
;
1029 ret
= drm_irq_install(drm
, irq
);
1031 dev_err(dev
, "Unable to install IRQ handler\n");
1035 ret
= drm_vblank_init(drm
, 1);
1037 dev_err(dev
, "Failed calling drm_vblank_init()\n");
1041 drm_mode_config_reset(drm
);
1043 ret
= clk_prepare_enable(priv
->pix_clk
);
1045 dev_err(dev
, "Unable to start pixel clock\n");
1049 if (priv
->lcd_clk
) {
1050 parent_clk
= clk_get_parent(priv
->lcd_clk
);
1051 parent_rate
= clk_get_rate(parent_clk
);
1053 /* LCD Device clock must be 3x the pixel clock for STN panels,
1054 * or 1.5x the pixel clock for TFT panels. To avoid having to
1055 * check for the LCD device clock everytime we do a mode change,
1056 * we set the LCD device clock to the highest rate possible.
1058 ret
= clk_set_rate(priv
->lcd_clk
, parent_rate
);
1060 dev_err(dev
, "Unable to set LCD clock rate\n");
1061 goto err_pixclk_disable
;
1064 ret
= clk_prepare_enable(priv
->lcd_clk
);
1066 dev_err(dev
, "Unable to start lcd clock\n");
1067 goto err_pixclk_disable
;
1071 /* Set address of our DMA descriptor chain */
1072 regmap_write(priv
->map
, JZ_REG_LCD_DA0
, dma_hwdesc_phys_f0
);
1073 regmap_write(priv
->map
, JZ_REG_LCD_DA1
, dma_hwdesc_phys_f1
);
1075 /* Enable OSD if available */
1076 if (soc_info
->has_osd
)
1077 regmap_write(priv
->map
, JZ_REG_LCD_OSDC
, JZ_LCD_OSDC_OSDEN
);
1079 mutex_init(&priv
->clk_mutex
);
1080 priv
->clock_nb
.notifier_call
= ingenic_drm_update_pixclk
;
1082 parent_clk
= clk_get_parent(priv
->pix_clk
);
1083 ret
= clk_notifier_register(parent_clk
, &priv
->clock_nb
);
1085 dev_err(dev
, "Unable to register clock notifier\n");
1086 goto err_devclk_disable
;
1089 ret
= drm_dev_register(drm
, 0);
1091 dev_err(dev
, "Failed to register DRM driver\n");
1092 goto err_clk_notifier_unregister
;
1095 drm_fbdev_generic_setup(drm
, 32);
1099 err_clk_notifier_unregister
:
1100 clk_notifier_unregister(parent_clk
, &priv
->clock_nb
);
1103 clk_disable_unprepare(priv
->lcd_clk
);
1105 clk_disable_unprepare(priv
->pix_clk
);
1109 static int ingenic_drm_bind_with_components(struct device
*dev
)
1111 return ingenic_drm_bind(dev
, true);
1114 static int compare_of(struct device
*dev
, void *data
)
1116 return dev
->of_node
== data
;
1119 static void ingenic_drm_unbind(struct device
*dev
)
1121 struct ingenic_drm
*priv
= dev_get_drvdata(dev
);
1122 struct clk
*parent_clk
= clk_get_parent(priv
->pix_clk
);
1124 clk_notifier_unregister(parent_clk
, &priv
->clock_nb
);
1126 clk_disable_unprepare(priv
->lcd_clk
);
1127 clk_disable_unprepare(priv
->pix_clk
);
1129 drm_dev_unregister(&priv
->drm
);
1130 drm_atomic_helper_shutdown(&priv
->drm
);
1133 static const struct component_master_ops ingenic_master_ops
= {
1134 .bind
= ingenic_drm_bind_with_components
,
1135 .unbind
= ingenic_drm_unbind
,
1138 static int ingenic_drm_probe(struct platform_device
*pdev
)
1140 struct device
*dev
= &pdev
->dev
;
1141 struct component_match
*match
= NULL
;
1142 struct device_node
*np
;
1144 if (!IS_ENABLED(CONFIG_DRM_INGENIC_IPU
))
1145 return ingenic_drm_bind(dev
, false);
1147 /* IPU is at port address 8 */
1148 np
= of_graph_get_remote_node(dev
->of_node
, 8, 0);
1150 return ingenic_drm_bind(dev
, false);
1152 drm_of_component_match_add(dev
, &match
, compare_of
, np
);
1155 return component_master_add_with_match(dev
, &ingenic_master_ops
, match
);
1158 static int ingenic_drm_remove(struct platform_device
*pdev
)
1160 struct device
*dev
= &pdev
->dev
;
1162 if (!IS_ENABLED(CONFIG_DRM_INGENIC_IPU
))
1163 ingenic_drm_unbind(dev
);
1165 component_master_del(dev
, &ingenic_master_ops
);
1170 static const u32 jz4740_formats
[] = {
1171 DRM_FORMAT_XRGB1555
,
1173 DRM_FORMAT_XRGB8888
,
1176 static const u32 jz4725b_formats_f1
[] = {
1177 DRM_FORMAT_XRGB1555
,
1179 DRM_FORMAT_XRGB8888
,
1182 static const u32 jz4725b_formats_f0
[] = {
1184 DRM_FORMAT_XRGB1555
,
1186 DRM_FORMAT_XRGB8888
,
1189 static const u32 jz4770_formats_f1
[] = {
1190 DRM_FORMAT_XRGB1555
,
1193 DRM_FORMAT_XRGB8888
,
1194 DRM_FORMAT_XRGB2101010
,
1197 static const u32 jz4770_formats_f0
[] = {
1199 DRM_FORMAT_XRGB1555
,
1202 DRM_FORMAT_XRGB8888
,
1203 DRM_FORMAT_XRGB2101010
,
1206 static const struct jz_soc_info jz4740_soc_info
= {
1207 .needs_dev_clk
= true,
1211 .formats_f1
= jz4740_formats
,
1212 .num_formats_f1
= ARRAY_SIZE(jz4740_formats
),
1213 /* JZ4740 has only one plane */
1216 static const struct jz_soc_info jz4725b_soc_info
= {
1217 .needs_dev_clk
= false,
1221 .formats_f1
= jz4725b_formats_f1
,
1222 .num_formats_f1
= ARRAY_SIZE(jz4725b_formats_f1
),
1223 .formats_f0
= jz4725b_formats_f0
,
1224 .num_formats_f0
= ARRAY_SIZE(jz4725b_formats_f0
),
1227 static const struct jz_soc_info jz4770_soc_info
= {
1228 .needs_dev_clk
= false,
1232 .formats_f1
= jz4770_formats_f1
,
1233 .num_formats_f1
= ARRAY_SIZE(jz4770_formats_f1
),
1234 .formats_f0
= jz4770_formats_f0
,
1235 .num_formats_f0
= ARRAY_SIZE(jz4770_formats_f0
),
1238 static const struct of_device_id ingenic_drm_of_match
[] = {
1239 { .compatible
= "ingenic,jz4740-lcd", .data
= &jz4740_soc_info
},
1240 { .compatible
= "ingenic,jz4725b-lcd", .data
= &jz4725b_soc_info
},
1241 { .compatible
= "ingenic,jz4770-lcd", .data
= &jz4770_soc_info
},
1244 MODULE_DEVICE_TABLE(of
, ingenic_drm_of_match
);
1246 static struct platform_driver ingenic_drm_driver
= {
1248 .name
= "ingenic-drm",
1249 .of_match_table
= of_match_ptr(ingenic_drm_of_match
),
1251 .probe
= ingenic_drm_probe
,
1252 .remove
= ingenic_drm_remove
,
1255 static int ingenic_drm_init(void)
1259 if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU
)) {
1260 err
= platform_driver_register(ingenic_ipu_driver_ptr
);
1265 return platform_driver_register(&ingenic_drm_driver
);
1267 module_init(ingenic_drm_init
);
1269 static void ingenic_drm_exit(void)
1271 platform_driver_unregister(&ingenic_drm_driver
);
1273 if (IS_ENABLED(CONFIG_DRM_INGENIC_IPU
))
1274 platform_driver_unregister(ingenic_ipu_driver_ptr
);
1276 module_exit(ingenic_drm_exit
);
1278 MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
1279 MODULE_DESCRIPTION("DRM driver for the Ingenic SoCs\n");
1280 MODULE_LICENSE("GPL v2");