Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Texas Instruments |
| 4 | * Author: Jyri Sarha <jsarha@ti.com> |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 5 | */ |
| 6 | |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 7 | #include <drm/drm_atomic.h> |
| 8 | #include <drm/drm_plane_helper.h> |
| 9 | #include <drm/drm_atomic_helper.h> |
Sam Ravnborg | fcb5766 | 2019-07-16 08:42:15 +0200 | [diff] [blame] | 10 | #include <drm/drm_fourcc.h> |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 11 | |
| 12 | #include "tilcdc_drv.h" |
| 13 | |
Nishka Dasgupta | fba3baa | 2019-08-13 14:35:03 +0530 | [diff] [blame] | 14 | static const struct drm_plane_funcs tilcdc_plane_funcs = { |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 15 | .update_plane = drm_atomic_helper_update_plane, |
| 16 | .disable_plane = drm_atomic_helper_disable_plane, |
| 17 | .destroy = drm_plane_cleanup, |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 18 | .reset = drm_atomic_helper_plane_reset, |
| 19 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 20 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 21 | }; |
| 22 | |
| 23 | static int tilcdc_plane_atomic_check(struct drm_plane *plane, |
| 24 | struct drm_plane_state *state) |
| 25 | { |
| 26 | struct drm_crtc_state *crtc_state; |
| 27 | struct drm_plane_state *old_state = plane->state; |
Laurent Pinchart | 59f11a4 | 2016-10-18 01:41:14 +0300 | [diff] [blame] | 28 | unsigned int pitch; |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 29 | |
| 30 | if (!state->crtc) |
| 31 | return 0; |
| 32 | |
| 33 | if (WARN_ON(!state->fb)) |
| 34 | return -EINVAL; |
| 35 | |
| 36 | if (state->crtc_x || state->crtc_y) { |
| 37 | dev_err(plane->dev->dev, "%s: crtc position must be zero.", |
| 38 | __func__); |
| 39 | return -EINVAL; |
| 40 | } |
| 41 | |
| 42 | crtc_state = drm_atomic_get_existing_crtc_state(state->state, |
| 43 | state->crtc); |
| 44 | /* we should have a crtc state if the plane is attached to a crtc */ |
| 45 | if (WARN_ON(!crtc_state)) |
| 46 | return 0; |
| 47 | |
| 48 | if (crtc_state->mode.hdisplay != state->crtc_w || |
| 49 | crtc_state->mode.vdisplay != state->crtc_h) { |
| 50 | dev_err(plane->dev->dev, |
| 51 | "%s: Size must match mode (%dx%d == %dx%d)", __func__, |
| 52 | crtc_state->mode.hdisplay, crtc_state->mode.vdisplay, |
| 53 | state->crtc_w, state->crtc_h); |
| 54 | return -EINVAL; |
| 55 | } |
| 56 | |
Laurent Pinchart | 59f11a4 | 2016-10-18 01:41:14 +0300 | [diff] [blame] | 57 | pitch = crtc_state->mode.hdisplay * |
Ville Syrjälä | 353c859 | 2016-12-14 23:30:57 +0200 | [diff] [blame] | 58 | state->fb->format->cpp[0]; |
Laurent Pinchart | 59f11a4 | 2016-10-18 01:41:14 +0300 | [diff] [blame] | 59 | if (state->fb->pitches[0] != pitch) { |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 60 | dev_err(plane->dev->dev, |
| 61 | "Invalid pitch: fb and crtc widths must be the same"); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
Tomi Valkeinen | 26c0663 | 2020-04-29 13:42:33 +0300 | [diff] [blame] | 65 | if (old_state->fb && state->fb->format != old_state->fb->format) { |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 66 | dev_dbg(plane->dev->dev, |
| 67 | "%s(): pixel format change requires mode_change\n", |
| 68 | __func__); |
| 69 | crtc_state->mode_changed = true; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static void tilcdc_plane_atomic_update(struct drm_plane *plane, |
| 76 | struct drm_plane_state *old_state) |
| 77 | { |
| 78 | struct drm_plane_state *state = plane->state; |
| 79 | |
| 80 | if (!state->crtc) |
| 81 | return; |
| 82 | |
| 83 | if (WARN_ON(!state->fb || !state->crtc->state)) |
| 84 | return; |
| 85 | |
Daniel Vetter | cb345de | 2020-07-08 16:20:50 +0200 | [diff] [blame] | 86 | if (tilcdc_crtc_update_fb(state->crtc, |
| 87 | state->fb, |
| 88 | state->crtc->state->event) == 0) { |
| 89 | state->crtc->state->event = NULL; |
| 90 | } |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static const struct drm_plane_helper_funcs plane_helper_funcs = { |
| 94 | .atomic_check = tilcdc_plane_atomic_check, |
| 95 | .atomic_update = tilcdc_plane_atomic_update, |
| 96 | }; |
| 97 | |
| 98 | int tilcdc_plane_init(struct drm_device *dev, |
| 99 | struct drm_plane *plane) |
| 100 | { |
Jyri Sarha | bcc5a6f | 2016-08-11 19:09:43 +0300 | [diff] [blame] | 101 | struct tilcdc_drm_private *priv = dev->dev_private; |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 102 | int ret; |
| 103 | |
| 104 | ret = drm_plane_init(dev, plane, 1, |
| 105 | &tilcdc_plane_funcs, |
Jyri Sarha | bcc5a6f | 2016-08-11 19:09:43 +0300 | [diff] [blame] | 106 | priv->pixelformats, |
| 107 | priv->num_pixelformats, |
Jyri Sarha | b961c48b | 2016-04-07 14:52:02 +0300 | [diff] [blame] | 108 | true); |
| 109 | if (ret) { |
| 110 | dev_err(dev->dev, "Failed to initialize plane: %d\n", ret); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | drm_plane_helper_add(plane, &plane_helper_funcs); |
| 115 | |
| 116 | return 0; |
| 117 | } |