Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 2 | /* |
| 3 | * DRM driver for Sitronix ST7586 panels |
| 4 | * |
| 5 | * Copyright 2017 David Lechner <david@lechnology.com> |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 9 | #include <linux/gpio/consumer.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/property.h> |
| 12 | #include <linux/spi/spi.h> |
| 13 | #include <video/mipi_display.h> |
| 14 | |
Noralf Trønnes | d0a5163 | 2019-02-10 14:10:33 +0100 | [diff] [blame] | 15 | #include <drm/drm_atomic_helper.h> |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 16 | #include <drm/drm_damage_helper.h> |
Sam Ravnborg | 84056e9 | 2019-01-08 20:29:38 +0100 | [diff] [blame] | 17 | #include <drm/drm_drv.h> |
Danilo Krummrich | 6bcfe8e | 2022-08-02 02:04:02 +0200 | [diff] [blame] | 18 | #include <drm/drm_fb_dma_helper.h> |
Thomas Zimmermann | 8ab59da | 2022-11-03 16:14:44 +0100 | [diff] [blame] | 19 | #include <drm/drm_fbdev_generic.h> |
Gerd Hoffmann | 7415287 | 2019-04-05 11:52:15 +0200 | [diff] [blame] | 20 | #include <drm/drm_format_helper.h> |
Ville Syrjälä | 720cf96d | 2022-06-14 12:54:49 +0300 | [diff] [blame] | 21 | #include <drm/drm_framebuffer.h> |
Thomas Zimmermann | 820c170 | 2021-02-22 15:17:56 +0100 | [diff] [blame] | 22 | #include <drm/drm_gem_atomic_helper.h> |
Danilo Krummrich | 4a83c26 | 2022-08-02 02:04:03 +0200 | [diff] [blame] | 23 | #include <drm/drm_gem_dma_helper.h> |
Thomas Zimmermann | 9200454 | 2021-07-16 16:08:01 +0200 | [diff] [blame] | 24 | #include <drm/drm_gem_framebuffer_helper.h> |
Daniel Vetter | f5ad671 | 2020-03-23 15:49:04 +0100 | [diff] [blame] | 25 | #include <drm/drm_managed.h> |
Noralf Trønnes | 174102f | 2019-07-22 12:43:11 +0200 | [diff] [blame] | 26 | #include <drm/drm_mipi_dbi.h> |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 27 | #include <drm/drm_rect.h> |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 28 | |
| 29 | /* controller-specific commands */ |
| 30 | #define ST7586_DISP_MODE_GRAY 0x38 |
| 31 | #define ST7586_DISP_MODE_MONO 0x39 |
| 32 | #define ST7586_ENABLE_DDRAM 0x3a |
| 33 | #define ST7586_SET_DISP_DUTY 0xb0 |
| 34 | #define ST7586_SET_PART_DISP 0xb4 |
| 35 | #define ST7586_SET_NLINE_INV 0xb5 |
| 36 | #define ST7586_SET_VOP 0xc0 |
| 37 | #define ST7586_SET_BIAS_SYSTEM 0xc3 |
| 38 | #define ST7586_SET_BOOST_LEVEL 0xc4 |
| 39 | #define ST7586_SET_VOP_OFFSET 0xc7 |
| 40 | #define ST7586_ENABLE_ANALOG 0xd0 |
| 41 | #define ST7586_AUTO_READ_CTRL 0xd7 |
| 42 | #define ST7586_OTP_RW_CTRL 0xe0 |
| 43 | #define ST7586_OTP_CTRL_OUT 0xe1 |
| 44 | #define ST7586_OTP_READ 0xe3 |
| 45 | |
| 46 | #define ST7586_DISP_CTRL_MX BIT(6) |
| 47 | #define ST7586_DISP_CTRL_MY BIT(7) |
| 48 | |
| 49 | /* |
| 50 | * The ST7586 controller has an unusual pixel format where 2bpp grayscale is |
| 51 | * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd |
| 52 | * pixel using only 2 bits. |
| 53 | * |
| 54 | * | D7 | D6 | D5 || | || 2bpp | |
| 55 | * | (D4) | (D3) | (D2) || D1 | D0 || GRAY | |
| 56 | * +------+------+------++------+------++------+ |
| 57 | * | 1 | 1 | 1 || 1 | 1 || 0 0 | black |
| 58 | * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray |
| 59 | * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray |
| 60 | * | 0 | 0 | 0 || 0 | 0 || 1 1 | white |
| 61 | */ |
| 62 | |
| 63 | static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 }; |
| 64 | |
| 65 | static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr, |
| 66 | struct drm_framebuffer *fb, |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 67 | struct drm_rect *clip) |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 68 | { |
| 69 | size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1); |
| 70 | unsigned int x, y; |
| 71 | u8 *src, *buf, val; |
Thomas Zimmermann | 7bef644 | 2022-08-08 14:54:03 +0200 | [diff] [blame] | 72 | struct iosys_map dst_map, vmap; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 73 | |
| 74 | buf = kmalloc(len, GFP_KERNEL); |
| 75 | if (!buf) |
| 76 | return; |
| 77 | |
Thomas Zimmermann | 7bef644 | 2022-08-08 14:54:03 +0200 | [diff] [blame] | 78 | iosys_map_set_vaddr(&dst_map, buf); |
| 79 | iosys_map_set_vaddr(&vmap, vaddr); |
| 80 | drm_fb_xrgb8888_to_gray8(&dst_map, NULL, &vmap, fb, clip); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 81 | src = buf; |
| 82 | |
| 83 | for (y = clip->y1; y < clip->y2; y++) { |
| 84 | for (x = clip->x1; x < clip->x2; x += 3) { |
| 85 | val = st7586_lookup[*src++ >> 6] << 5; |
| 86 | val |= st7586_lookup[*src++ >> 6] << 2; |
| 87 | val |= st7586_lookup[*src++ >> 6] >> 1; |
| 88 | *dst++ = val; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | kfree(buf); |
| 93 | } |
| 94 | |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 95 | static int st7586_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb, |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 96 | struct drm_rect *clip) |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 97 | { |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 98 | int ret; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 99 | |
Thomas Zimmermann | 9200454 | 2021-07-16 16:08:01 +0200 | [diff] [blame] | 100 | ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); |
| 101 | if (ret) |
| 102 | return ret; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 103 | |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 104 | st7586_xrgb8888_to_gray332(dst, src->vaddr, fb, clip); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 105 | |
Thomas Zimmermann | 9200454 | 2021-07-16 16:08:01 +0200 | [diff] [blame] | 106 | drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 107 | |
Thomas Zimmermann | 9200454 | 2021-07-16 16:08:01 +0200 | [diff] [blame] | 108 | return 0; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 111 | static void st7586_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb, |
| 112 | struct drm_rect *rect) |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 113 | { |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 114 | struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev); |
| 115 | struct mipi_dbi *dbi = &dbidev->dbi; |
Thomas Zimmermann | 3ea4410 | 2022-12-02 13:56:44 +0100 | [diff] [blame] | 116 | int start, end, ret = 0; |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 117 | |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 118 | /* 3 pixels per byte, so grow clip to nearest multiple of 3 */ |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 119 | rect->x1 = rounddown(rect->x1, 3); |
| 120 | rect->x2 = roundup(rect->x2, 3); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 121 | |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 122 | DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect)); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 123 | |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 124 | ret = st7586_buf_copy(dbidev->tx_buf, src, fb, rect); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 125 | if (ret) |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 126 | goto err_msg; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 127 | |
| 128 | /* Pixels are packed 3 per byte */ |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 129 | start = rect->x1 / 3; |
| 130 | end = rect->x2 / 3; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 131 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 132 | mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 133 | (start >> 8) & 0xFF, start & 0xFF, |
| 134 | (end >> 8) & 0xFF, (end - 1) & 0xFF); |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 135 | mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS, |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 136 | (rect->y1 >> 8) & 0xFF, rect->y1 & 0xFF, |
| 137 | (rect->y2 >> 8) & 0xFF, (rect->y2 - 1) & 0xFF); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 138 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 139 | ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START, |
Noralf Trønnes | 440961d | 2019-07-22 12:43:06 +0200 | [diff] [blame] | 140 | (u8 *)dbidev->tx_buf, |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 141 | (end - start) * (rect->y2 - rect->y1)); |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 142 | err_msg: |
| 143 | if (ret) |
| 144 | dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 147 | static void st7586_pipe_update(struct drm_simple_display_pipe *pipe, |
| 148 | struct drm_plane_state *old_state) |
| 149 | { |
| 150 | struct drm_plane_state *state = pipe->plane.state; |
Thomas Zimmermann | 69c63e8 | 2022-12-02 13:56:43 +0100 | [diff] [blame] | 151 | struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state); |
Thomas Zimmermann | b5f636e6 | 2022-12-02 13:56:41 +0100 | [diff] [blame] | 152 | struct drm_framebuffer *fb = state->fb; |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 153 | struct drm_rect rect; |
Thomas Zimmermann | 3ea4410 | 2022-12-02 13:56:44 +0100 | [diff] [blame] | 154 | int idx; |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 155 | |
Daniel Vetter | 7e06886 | 2020-06-12 18:00:55 +0200 | [diff] [blame] | 156 | if (!pipe->crtc.state->active) |
| 157 | return; |
| 158 | |
Thomas Zimmermann | 3ea4410 | 2022-12-02 13:56:44 +0100 | [diff] [blame] | 159 | if (!drm_dev_enter(fb->dev, &idx)) |
| 160 | return; |
| 161 | |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 162 | if (drm_atomic_helper_damage_merged(old_state, state, &rect)) |
Thomas Zimmermann | 69c63e8 | 2022-12-02 13:56:43 +0100 | [diff] [blame] | 163 | st7586_fb_dirty(&shadow_plane_state->data[0], fb, &rect); |
Thomas Zimmermann | 3ea4410 | 2022-12-02 13:56:44 +0100 | [diff] [blame] | 164 | |
| 165 | drm_dev_exit(idx); |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 166 | } |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 167 | |
Colin Ian King | f3bbc90 | 2017-08-16 10:23:06 +0100 | [diff] [blame] | 168 | static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe, |
Ville Syrjälä | 0c9c7fd | 2018-03-22 22:27:37 +0200 | [diff] [blame] | 169 | struct drm_crtc_state *crtc_state, |
| 170 | struct drm_plane_state *plane_state) |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 171 | { |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 172 | struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); |
Thomas Zimmermann | 69c63e8 | 2022-12-02 13:56:43 +0100 | [diff] [blame] | 173 | struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 174 | struct drm_framebuffer *fb = plane_state->fb; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 175 | struct mipi_dbi *dbi = &dbidev->dbi; |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 176 | struct drm_rect rect = { |
| 177 | .x1 = 0, |
| 178 | .x2 = fb->width, |
| 179 | .y1 = 0, |
| 180 | .y2 = fb->height, |
| 181 | }; |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 182 | int idx, ret; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 183 | u8 addr_mode; |
| 184 | |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 185 | if (!drm_dev_enter(pipe->crtc.dev, &idx)) |
| 186 | return; |
| 187 | |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 188 | DRM_DEBUG_KMS("\n"); |
| 189 | |
Noralf Trønnes | 440961d | 2019-07-22 12:43:06 +0200 | [diff] [blame] | 190 | ret = mipi_dbi_poweron_reset(dbidev); |
Noralf Trønnes | 070ab12 | 2018-01-10 19:59:37 +0100 | [diff] [blame] | 191 | if (ret) |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 192 | goto out_exit; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 193 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 194 | mipi_dbi_command(dbi, ST7586_AUTO_READ_CTRL, 0x9f); |
| 195 | mipi_dbi_command(dbi, ST7586_OTP_RW_CTRL, 0x00); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 196 | |
| 197 | msleep(10); |
| 198 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 199 | mipi_dbi_command(dbi, ST7586_OTP_READ); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 200 | |
| 201 | msleep(20); |
| 202 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 203 | mipi_dbi_command(dbi, ST7586_OTP_CTRL_OUT); |
| 204 | mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE); |
| 205 | mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 206 | |
| 207 | msleep(50); |
| 208 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 209 | mipi_dbi_command(dbi, ST7586_SET_VOP_OFFSET, 0x00); |
| 210 | mipi_dbi_command(dbi, ST7586_SET_VOP, 0xe3, 0x00); |
| 211 | mipi_dbi_command(dbi, ST7586_SET_BIAS_SYSTEM, 0x02); |
| 212 | mipi_dbi_command(dbi, ST7586_SET_BOOST_LEVEL, 0x04); |
| 213 | mipi_dbi_command(dbi, ST7586_ENABLE_ANALOG, 0x1d); |
| 214 | mipi_dbi_command(dbi, ST7586_SET_NLINE_INV, 0x00); |
| 215 | mipi_dbi_command(dbi, ST7586_DISP_MODE_GRAY); |
| 216 | mipi_dbi_command(dbi, ST7586_ENABLE_DDRAM, 0x02); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 217 | |
Noralf Trønnes | 440961d | 2019-07-22 12:43:06 +0200 | [diff] [blame] | 218 | switch (dbidev->rotation) { |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 219 | default: |
| 220 | addr_mode = 0x00; |
| 221 | break; |
| 222 | case 90: |
| 223 | addr_mode = ST7586_DISP_CTRL_MY; |
| 224 | break; |
| 225 | case 180: |
| 226 | addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY; |
| 227 | break; |
| 228 | case 270: |
| 229 | addr_mode = ST7586_DISP_CTRL_MX; |
| 230 | break; |
| 231 | } |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 232 | mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 233 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 234 | mipi_dbi_command(dbi, ST7586_SET_DISP_DUTY, 0x7f); |
| 235 | mipi_dbi_command(dbi, ST7586_SET_PART_DISP, 0xa0); |
Jani Nikula | 97ecec8 | 2019-10-28 17:00:46 +0200 | [diff] [blame] | 236 | mipi_dbi_command(dbi, MIPI_DCS_SET_PARTIAL_ROWS, 0x00, 0x00, 0x00, 0x77); |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 237 | mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 238 | |
| 239 | msleep(100); |
| 240 | |
Thomas Zimmermann | 69c63e8 | 2022-12-02 13:56:43 +0100 | [diff] [blame] | 241 | st7586_fb_dirty(&shadow_plane_state->data[0], fb, &rect); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 242 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 243 | mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON); |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 244 | out_exit: |
| 245 | drm_dev_exit(idx); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe) |
| 249 | { |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 250 | struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(pipe->crtc.dev); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 251 | |
Noralf Trønnes | 9d5645a | 2019-02-25 15:42:32 +0100 | [diff] [blame] | 252 | /* |
| 253 | * This callback is not protected by drm_dev_enter/exit since we want to |
| 254 | * turn off the display on regular driver unload. It's highly unlikely |
| 255 | * that the underlying SPI controller is gone should this be called after |
| 256 | * unplug. |
| 257 | */ |
| 258 | |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 259 | DRM_DEBUG_KMS("\n"); |
| 260 | |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 261 | mipi_dbi_command(&dbidev->dbi, MIPI_DCS_SET_DISPLAY_OFF); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static const u32 st7586_formats[] = { |
| 265 | DRM_FORMAT_XRGB8888, |
| 266 | }; |
| 267 | |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 268 | static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = { |
Thomas Zimmermann | e06c123 | 2022-12-02 13:56:39 +0100 | [diff] [blame] | 269 | .mode_valid = mipi_dbi_pipe_mode_valid, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 270 | .enable = st7586_pipe_enable, |
| 271 | .disable = st7586_pipe_disable, |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 272 | .update = st7586_pipe_update, |
Thomas Zimmermann | e7caf04 | 2022-12-02 13:56:42 +0100 | [diff] [blame] | 273 | .begin_fb_access = mipi_dbi_pipe_begin_fb_access, |
| 274 | .end_fb_access = mipi_dbi_pipe_end_fb_access, |
| 275 | .reset_plane = mipi_dbi_pipe_reset_plane, |
| 276 | .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, |
| 277 | .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | static const struct drm_display_mode st7586_mode = { |
Noralf Trønnes | 06db4b8 | 2019-02-10 14:10:31 +0100 | [diff] [blame] | 281 | DRM_SIMPLE_MODE(178, 128, 37, 27), |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 282 | }; |
| 283 | |
Danilo Krummrich | 4a83c26 | 2022-08-02 02:04:03 +0200 | [diff] [blame] | 284 | DEFINE_DRM_GEM_DMA_FOPS(st7586_fops); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 285 | |
Daniel Vetter | 70a59dd | 2020-11-04 11:04:24 +0100 | [diff] [blame] | 286 | static const struct drm_driver st7586_driver = { |
Daniel Vetter | 0424fda | 2019-06-17 17:39:24 +0200 | [diff] [blame] | 287 | .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 288 | .fops = &st7586_fops, |
Danilo Krummrich | 4a83c26 | 2022-08-02 02:04:03 +0200 | [diff] [blame] | 289 | DRM_GEM_DMA_DRIVER_OPS_VMAP, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 290 | .debugfs_init = mipi_dbi_debugfs_init, |
| 291 | .name = "st7586", |
| 292 | .desc = "Sitronix ST7586", |
| 293 | .date = "20170801", |
| 294 | .major = 1, |
| 295 | .minor = 0, |
| 296 | }; |
| 297 | |
| 298 | static const struct of_device_id st7586_of_match[] = { |
| 299 | { .compatible = "lego,ev3-lcd" }, |
| 300 | {}, |
| 301 | }; |
| 302 | MODULE_DEVICE_TABLE(of, st7586_of_match); |
| 303 | |
| 304 | static const struct spi_device_id st7586_id[] = { |
| 305 | { "ev3-lcd", 0 }, |
| 306 | { }, |
| 307 | }; |
| 308 | MODULE_DEVICE_TABLE(spi, st7586_id); |
| 309 | |
| 310 | static int st7586_probe(struct spi_device *spi) |
| 311 | { |
| 312 | struct device *dev = &spi->dev; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 313 | struct mipi_dbi_dev *dbidev; |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 314 | struct drm_device *drm; |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 315 | struct mipi_dbi *dbi; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 316 | struct gpio_desc *a0; |
| 317 | u32 rotation = 0; |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 318 | size_t bufsize; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 319 | int ret; |
| 320 | |
Daniel Vetter | e20b8738 | 2020-04-15 09:39:49 +0200 | [diff] [blame] | 321 | dbidev = devm_drm_dev_alloc(dev, &st7586_driver, |
| 322 | struct mipi_dbi_dev, drm); |
| 323 | if (IS_ERR(dbidev)) |
| 324 | return PTR_ERR(dbidev); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 325 | |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 326 | dbi = &dbidev->dbi; |
Noralf Trønnes | 440961d | 2019-07-22 12:43:06 +0200 | [diff] [blame] | 327 | drm = &dbidev->drm; |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 328 | |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 329 | bufsize = (st7586_mode.vdisplay + 2) / 3 * st7586_mode.hdisplay; |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 330 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 331 | dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); |
Andy Shevchenko | 40567e8 | 2021-04-21 19:31:52 +0300 | [diff] [blame] | 332 | if (IS_ERR(dbi->reset)) |
| 333 | return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 334 | |
| 335 | a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW); |
Andy Shevchenko | 40567e8 | 2021-04-21 19:31:52 +0300 | [diff] [blame] | 336 | if (IS_ERR(a0)) |
| 337 | return dev_err_probe(dev, PTR_ERR(a0), "Failed to get GPIO 'a0'\n"); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 338 | |
| 339 | device_property_read_u32(dev, "rotation", &rotation); |
| 340 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 341 | ret = mipi_dbi_spi_init(spi, dbi, a0); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 342 | if (ret) |
| 343 | return ret; |
| 344 | |
| 345 | /* Cannot read from this controller via SPI */ |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 346 | dbi->read_commands = NULL; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 347 | |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 348 | ret = mipi_dbi_dev_init_with_formats(dbidev, &st7586_pipe_funcs, |
| 349 | st7586_formats, ARRAY_SIZE(st7586_formats), |
| 350 | &st7586_mode, rotation, bufsize); |
Noralf Trønnes | cc43121 | 2019-07-19 17:59:15 +0200 | [diff] [blame] | 351 | if (ret) |
| 352 | return ret; |
| 353 | |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 354 | /* |
| 355 | * we are using 8-bit data, so we are not actually swapping anything, |
| 356 | * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the |
| 357 | * right thing and not use 16-bit transfers (which results in swapped |
| 358 | * bytes on little-endian systems and causes out of order data to be |
| 359 | * sent to the display). |
| 360 | */ |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 361 | dbi->swap_bytes = true; |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 362 | |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 363 | drm_mode_config_reset(drm); |
| 364 | |
| 365 | ret = drm_dev_register(drm, 0); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
| 369 | spi_set_drvdata(spi, drm); |
| 370 | |
Noralf Trønnes | f47056e | 2019-04-10 14:43:45 +0200 | [diff] [blame] | 371 | drm_fbdev_generic_setup(drm, 0); |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Uwe Kleine-König | a0386bb | 2022-01-23 18:52:01 +0100 | [diff] [blame] | 376 | static void st7586_remove(struct spi_device *spi) |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 377 | { |
| 378 | struct drm_device *drm = spi_get_drvdata(spi); |
| 379 | |
| 380 | drm_dev_unplug(drm); |
| 381 | drm_atomic_helper_shutdown(drm); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static void st7586_shutdown(struct spi_device *spi) |
| 385 | { |
Noralf Trønnes | d0a5163 | 2019-02-10 14:10:33 +0100 | [diff] [blame] | 386 | drm_atomic_helper_shutdown(spi_get_drvdata(spi)); |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static struct spi_driver st7586_spi_driver = { |
| 390 | .driver = { |
| 391 | .name = "st7586", |
| 392 | .owner = THIS_MODULE, |
| 393 | .of_match_table = st7586_of_match, |
| 394 | }, |
| 395 | .id_table = st7586_id, |
| 396 | .probe = st7586_probe, |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 397 | .remove = st7586_remove, |
David Lechner | eac99d4 | 2017-08-07 12:39:39 -0500 | [diff] [blame] | 398 | .shutdown = st7586_shutdown, |
| 399 | }; |
| 400 | module_spi_driver(st7586_spi_driver); |
| 401 | |
| 402 | MODULE_DESCRIPTION("Sitronix ST7586 DRM driver"); |
| 403 | MODULE_AUTHOR("David Lechner <david@lechnology.com>"); |
| 404 | MODULE_LICENSE("GPL"); |