Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.15 417 lines 11 kB view raw
1/* 2 * DRM driver for Sitronix ST7586 panels 3 * 4 * Copyright 2017 David Lechner <david@lechnology.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 */ 11 12#include <linux/delay.h> 13#include <linux/dma-buf.h> 14#include <linux/gpio/consumer.h> 15#include <linux/module.h> 16#include <linux/property.h> 17#include <linux/spi/spi.h> 18#include <video/mipi_display.h> 19 20#include <drm/drm_gem_framebuffer_helper.h> 21#include <drm/tinydrm/mipi-dbi.h> 22#include <drm/tinydrm/tinydrm-helpers.h> 23 24/* controller-specific commands */ 25#define ST7586_DISP_MODE_GRAY 0x38 26#define ST7586_DISP_MODE_MONO 0x39 27#define ST7586_ENABLE_DDRAM 0x3a 28#define ST7586_SET_DISP_DUTY 0xb0 29#define ST7586_SET_PART_DISP 0xb4 30#define ST7586_SET_NLINE_INV 0xb5 31#define ST7586_SET_VOP 0xc0 32#define ST7586_SET_BIAS_SYSTEM 0xc3 33#define ST7586_SET_BOOST_LEVEL 0xc4 34#define ST7586_SET_VOP_OFFSET 0xc7 35#define ST7586_ENABLE_ANALOG 0xd0 36#define ST7586_AUTO_READ_CTRL 0xd7 37#define ST7586_OTP_RW_CTRL 0xe0 38#define ST7586_OTP_CTRL_OUT 0xe1 39#define ST7586_OTP_READ 0xe3 40 41#define ST7586_DISP_CTRL_MX BIT(6) 42#define ST7586_DISP_CTRL_MY BIT(7) 43 44/* 45 * The ST7586 controller has an unusual pixel format where 2bpp grayscale is 46 * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd 47 * pixel using only 2 bits. 48 * 49 * | D7 | D6 | D5 || | || 2bpp | 50 * | (D4) | (D3) | (D2) || D1 | D0 || GRAY | 51 * +------+------+------++------+------++------+ 52 * | 1 | 1 | 1 || 1 | 1 || 0 0 | black 53 * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray 54 * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray 55 * | 0 | 0 | 0 || 0 | 0 || 1 1 | white 56 */ 57 58static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 }; 59 60static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr, 61 struct drm_framebuffer *fb, 62 struct drm_clip_rect *clip) 63{ 64 size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1); 65 unsigned int x, y; 66 u8 *src, *buf, val; 67 68 buf = kmalloc(len, GFP_KERNEL); 69 if (!buf) 70 return; 71 72 tinydrm_xrgb8888_to_gray8(buf, vaddr, fb, clip); 73 src = buf; 74 75 for (y = clip->y1; y < clip->y2; y++) { 76 for (x = clip->x1; x < clip->x2; x += 3) { 77 val = st7586_lookup[*src++ >> 6] << 5; 78 val |= st7586_lookup[*src++ >> 6] << 2; 79 val |= st7586_lookup[*src++ >> 6] >> 1; 80 *dst++ = val; 81 } 82 } 83 84 kfree(buf); 85} 86 87static int st7586_buf_copy(void *dst, struct drm_framebuffer *fb, 88 struct drm_clip_rect *clip) 89{ 90 struct drm_gem_cma_object *cma_obj = drm_fb_cma_get_gem_obj(fb, 0); 91 struct dma_buf_attachment *import_attach = cma_obj->base.import_attach; 92 void *src = cma_obj->vaddr; 93 int ret = 0; 94 95 if (import_attach) { 96 ret = dma_buf_begin_cpu_access(import_attach->dmabuf, 97 DMA_FROM_DEVICE); 98 if (ret) 99 return ret; 100 } 101 102 st7586_xrgb8888_to_gray332(dst, src, fb, clip); 103 104 if (import_attach) 105 ret = dma_buf_end_cpu_access(import_attach->dmabuf, 106 DMA_FROM_DEVICE); 107 108 return ret; 109} 110 111static int st7586_fb_dirty(struct drm_framebuffer *fb, 112 struct drm_file *file_priv, unsigned int flags, 113 unsigned int color, struct drm_clip_rect *clips, 114 unsigned int num_clips) 115{ 116 struct tinydrm_device *tdev = fb->dev->dev_private; 117 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); 118 struct drm_clip_rect clip; 119 int start, end; 120 int ret = 0; 121 122 mutex_lock(&tdev->dirty_lock); 123 124 if (!mipi->enabled) 125 goto out_unlock; 126 127 /* fbdev can flush even when we're not interested */ 128 if (tdev->pipe.plane.fb != fb) 129 goto out_unlock; 130 131 tinydrm_merge_clips(&clip, clips, num_clips, flags, fb->width, 132 fb->height); 133 134 /* 3 pixels per byte, so grow clip to nearest multiple of 3 */ 135 clip.x1 = rounddown(clip.x1, 3); 136 clip.x2 = roundup(clip.x2, 3); 137 138 DRM_DEBUG("Flushing [FB:%d] x1=%u, x2=%u, y1=%u, y2=%u\n", fb->base.id, 139 clip.x1, clip.x2, clip.y1, clip.y2); 140 141 ret = st7586_buf_copy(mipi->tx_buf, fb, &clip); 142 if (ret) 143 goto out_unlock; 144 145 /* Pixels are packed 3 per byte */ 146 start = clip.x1 / 3; 147 end = clip.x2 / 3; 148 149 mipi_dbi_command(mipi, MIPI_DCS_SET_COLUMN_ADDRESS, 150 (start >> 8) & 0xFF, start & 0xFF, 151 (end >> 8) & 0xFF, (end - 1) & 0xFF); 152 mipi_dbi_command(mipi, MIPI_DCS_SET_PAGE_ADDRESS, 153 (clip.y1 >> 8) & 0xFF, clip.y1 & 0xFF, 154 (clip.y2 >> 8) & 0xFF, (clip.y2 - 1) & 0xFF); 155 156 ret = mipi_dbi_command_buf(mipi, MIPI_DCS_WRITE_MEMORY_START, 157 (u8 *)mipi->tx_buf, 158 (end - start) * (clip.y2 - clip.y1)); 159 160out_unlock: 161 mutex_unlock(&tdev->dirty_lock); 162 163 if (ret) 164 dev_err_once(fb->dev->dev, "Failed to update display %d\n", 165 ret); 166 167 return ret; 168} 169 170static const struct drm_framebuffer_funcs st7586_fb_funcs = { 171 .destroy = drm_gem_fb_destroy, 172 .create_handle = drm_gem_fb_create_handle, 173 .dirty = st7586_fb_dirty, 174}; 175 176static void st7586_pipe_enable(struct drm_simple_display_pipe *pipe, 177 struct drm_crtc_state *crtc_state) 178{ 179 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe); 180 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); 181 struct drm_framebuffer *fb = pipe->plane.fb; 182 struct device *dev = tdev->drm->dev; 183 int ret; 184 u8 addr_mode; 185 186 DRM_DEBUG_KMS("\n"); 187 188 mipi_dbi_hw_reset(mipi); 189 ret = mipi_dbi_command(mipi, ST7586_AUTO_READ_CTRL, 0x9f); 190 if (ret) { 191 DRM_DEV_ERROR(dev, "Error sending command %d\n", ret); 192 return; 193 } 194 195 mipi_dbi_command(mipi, ST7586_OTP_RW_CTRL, 0x00); 196 197 msleep(10); 198 199 mipi_dbi_command(mipi, ST7586_OTP_READ); 200 201 msleep(20); 202 203 mipi_dbi_command(mipi, ST7586_OTP_CTRL_OUT); 204 mipi_dbi_command(mipi, MIPI_DCS_EXIT_SLEEP_MODE); 205 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF); 206 207 msleep(50); 208 209 mipi_dbi_command(mipi, ST7586_SET_VOP_OFFSET, 0x00); 210 mipi_dbi_command(mipi, ST7586_SET_VOP, 0xe3, 0x00); 211 mipi_dbi_command(mipi, ST7586_SET_BIAS_SYSTEM, 0x02); 212 mipi_dbi_command(mipi, ST7586_SET_BOOST_LEVEL, 0x04); 213 mipi_dbi_command(mipi, ST7586_ENABLE_ANALOG, 0x1d); 214 mipi_dbi_command(mipi, ST7586_SET_NLINE_INV, 0x00); 215 mipi_dbi_command(mipi, ST7586_DISP_MODE_GRAY); 216 mipi_dbi_command(mipi, ST7586_ENABLE_DDRAM, 0x02); 217 218 switch (mipi->rotation) { 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 } 232 mipi_dbi_command(mipi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); 233 234 mipi_dbi_command(mipi, ST7586_SET_DISP_DUTY, 0x7f); 235 mipi_dbi_command(mipi, ST7586_SET_PART_DISP, 0xa0); 236 mipi_dbi_command(mipi, MIPI_DCS_SET_PARTIAL_AREA, 0x00, 0x00, 0x00, 0x77); 237 mipi_dbi_command(mipi, MIPI_DCS_EXIT_INVERT_MODE); 238 239 msleep(100); 240 241 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_ON); 242 243 mipi->enabled = true; 244 245 if (fb) 246 fb->funcs->dirty(fb, NULL, 0, 0, NULL, 0); 247} 248 249static void st7586_pipe_disable(struct drm_simple_display_pipe *pipe) 250{ 251 struct tinydrm_device *tdev = pipe_to_tinydrm(pipe); 252 struct mipi_dbi *mipi = mipi_dbi_from_tinydrm(tdev); 253 254 DRM_DEBUG_KMS("\n"); 255 256 if (!mipi->enabled) 257 return; 258 259 mipi_dbi_command(mipi, MIPI_DCS_SET_DISPLAY_OFF); 260 mipi->enabled = false; 261} 262 263static const u32 st7586_formats[] = { 264 DRM_FORMAT_XRGB8888, 265}; 266 267static int st7586_init(struct device *dev, struct mipi_dbi *mipi, 268 const struct drm_simple_display_pipe_funcs *pipe_funcs, 269 struct drm_driver *driver, const struct drm_display_mode *mode, 270 unsigned int rotation) 271{ 272 size_t bufsize = (mode->vdisplay + 2) / 3 * mode->hdisplay; 273 struct tinydrm_device *tdev = &mipi->tinydrm; 274 int ret; 275 276 mutex_init(&mipi->cmdlock); 277 278 mipi->tx_buf = devm_kmalloc(dev, bufsize, GFP_KERNEL); 279 if (!mipi->tx_buf) 280 return -ENOMEM; 281 282 ret = devm_tinydrm_init(dev, tdev, &st7586_fb_funcs, driver); 283 if (ret) 284 return ret; 285 286 ret = tinydrm_display_pipe_init(tdev, pipe_funcs, 287 DRM_MODE_CONNECTOR_VIRTUAL, 288 st7586_formats, 289 ARRAY_SIZE(st7586_formats), 290 mode, rotation); 291 if (ret) 292 return ret; 293 294 tdev->drm->mode_config.preferred_depth = 32; 295 mipi->rotation = rotation; 296 297 drm_mode_config_reset(tdev->drm); 298 299 DRM_DEBUG_KMS("preferred_depth=%u, rotation = %u\n", 300 tdev->drm->mode_config.preferred_depth, rotation); 301 302 return 0; 303} 304 305static const struct drm_simple_display_pipe_funcs st7586_pipe_funcs = { 306 .enable = st7586_pipe_enable, 307 .disable = st7586_pipe_disable, 308 .update = tinydrm_display_pipe_update, 309 .prepare_fb = tinydrm_display_pipe_prepare_fb, 310}; 311 312static const struct drm_display_mode st7586_mode = { 313 TINYDRM_MODE(178, 128, 37, 27), 314}; 315 316DEFINE_DRM_GEM_CMA_FOPS(st7586_fops); 317 318static struct drm_driver st7586_driver = { 319 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | 320 DRIVER_ATOMIC, 321 .fops = &st7586_fops, 322 TINYDRM_GEM_DRIVER_OPS, 323 .lastclose = tinydrm_lastclose, 324 .debugfs_init = mipi_dbi_debugfs_init, 325 .name = "st7586", 326 .desc = "Sitronix ST7586", 327 .date = "20170801", 328 .major = 1, 329 .minor = 0, 330}; 331 332static const struct of_device_id st7586_of_match[] = { 333 { .compatible = "lego,ev3-lcd" }, 334 {}, 335}; 336MODULE_DEVICE_TABLE(of, st7586_of_match); 337 338static const struct spi_device_id st7586_id[] = { 339 { "ev3-lcd", 0 }, 340 { }, 341}; 342MODULE_DEVICE_TABLE(spi, st7586_id); 343 344static int st7586_probe(struct spi_device *spi) 345{ 346 struct device *dev = &spi->dev; 347 struct mipi_dbi *mipi; 348 struct gpio_desc *a0; 349 u32 rotation = 0; 350 int ret; 351 352 mipi = devm_kzalloc(dev, sizeof(*mipi), GFP_KERNEL); 353 if (!mipi) 354 return -ENOMEM; 355 356 mipi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 357 if (IS_ERR(mipi->reset)) { 358 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n"); 359 return PTR_ERR(mipi->reset); 360 } 361 362 a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW); 363 if (IS_ERR(a0)) { 364 DRM_DEV_ERROR(dev, "Failed to get gpio 'a0'\n"); 365 return PTR_ERR(a0); 366 } 367 368 device_property_read_u32(dev, "rotation", &rotation); 369 370 ret = mipi_dbi_spi_init(spi, mipi, a0); 371 if (ret) 372 return ret; 373 374 /* Cannot read from this controller via SPI */ 375 mipi->read_commands = NULL; 376 377 /* 378 * we are using 8-bit data, so we are not actually swapping anything, 379 * but setting mipi->swap_bytes makes mipi_dbi_typec3_command() do the 380 * right thing and not use 16-bit transfers (which results in swapped 381 * bytes on little-endian systems and causes out of order data to be 382 * sent to the display). 383 */ 384 mipi->swap_bytes = true; 385 386 ret = st7586_init(&spi->dev, mipi, &st7586_pipe_funcs, &st7586_driver, 387 &st7586_mode, rotation); 388 if (ret) 389 return ret; 390 391 spi_set_drvdata(spi, mipi); 392 393 return devm_tinydrm_register(&mipi->tinydrm); 394} 395 396static void st7586_shutdown(struct spi_device *spi) 397{ 398 struct mipi_dbi *mipi = spi_get_drvdata(spi); 399 400 tinydrm_shutdown(&mipi->tinydrm); 401} 402 403static struct spi_driver st7586_spi_driver = { 404 .driver = { 405 .name = "st7586", 406 .owner = THIS_MODULE, 407 .of_match_table = st7586_of_match, 408 }, 409 .id_table = st7586_id, 410 .probe = st7586_probe, 411 .shutdown = st7586_shutdown, 412}; 413module_spi_driver(st7586_spi_driver); 414 415MODULE_DESCRIPTION("Sitronix ST7586 DRM driver"); 416MODULE_AUTHOR("David Lechner <david@lechnology.com>"); 417MODULE_LICENSE("GPL");