1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DRM driver for Sitronix ST7586 panels 4 * 5 * Copyright 2017 David Lechner <david@lechnology.com> 6 */ 7 8 #include <linux/delay.h> 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 15 #include <drm/clients/drm_client_setup.h> 16 #include <drm/drm_atomic.h> 17 #include <drm/drm_atomic_helper.h> 18 #include <drm/drm_damage_helper.h> 19 #include <drm/drm_drv.h> 20 #include <drm/drm_fb_dma_helper.h> 21 #include <drm/drm_fbdev_dma.h> 22 #include <drm/drm_format_helper.h> 23 #include <drm/drm_framebuffer.h> 24 #include <drm/drm_gem_atomic_helper.h> 25 #include <drm/drm_gem_dma_helper.h> 26 #include <drm/drm_gem_framebuffer_helper.h> 27 #include <drm/drm_managed.h> 28 #include <drm/drm_mipi_dbi.h> 29 #include <drm/drm_print.h> 30 #include <drm/drm_rect.h> 31 32 /* controller-specific commands */ 33 #define ST7586_DISP_MODE_GRAY 0x38 34 #define ST7586_DISP_MODE_MONO 0x39 35 #define ST7586_ENABLE_DDRAM 0x3a 36 #define ST7586_SET_DISP_DUTY 0xb0 37 #define ST7586_SET_PART_DISP 0xb4 38 #define ST7586_SET_NLINE_INV 0xb5 39 #define ST7586_SET_VOP 0xc0 40 #define ST7586_SET_BIAS_SYSTEM 0xc3 41 #define ST7586_SET_BOOST_LEVEL 0xc4 42 #define ST7586_SET_VOP_OFFSET 0xc7 43 #define ST7586_ENABLE_ANALOG 0xd0 44 #define ST7586_AUTO_READ_CTRL 0xd7 45 #define ST7586_OTP_RW_CTRL 0xe0 46 #define ST7586_OTP_CTRL_OUT 0xe1 47 #define ST7586_OTP_READ 0xe3 48 49 #define ST7586_DISP_CTRL_MX BIT(6) 50 #define ST7586_DISP_CTRL_MY BIT(7) 51 52 struct st7586_device { 53 struct mipi_dbi_dev dbidev; 54 55 struct drm_plane plane; 56 struct drm_crtc crtc; 57 struct drm_encoder encoder; 58 struct drm_connector connector; 59 }; 60 61 static struct st7586_device *to_st7586_device(struct drm_device *dev) 62 { 63 return container_of(drm_to_mipi_dbi_dev(dev), struct st7586_device, dbidev); 64 } 65 66 /* 67 * The ST7586 controller has an unusual pixel format where 2bpp grayscale is 68 * packed 3 pixels per byte with the first two pixels using 3 bits and the 3rd 69 * pixel using only 2 bits. 70 * 71 * | D7 | D6 | D5 || | || 2bpp | 72 * | (D4) | (D3) | (D2) || D1 | D0 || GRAY | 73 * +------+------+------++------+------++------+ 74 * | 1 | 1 | 1 || 1 | 1 || 0 0 | black 75 * | 1 | 0 | 0 || 1 | 0 || 0 1 | dark gray 76 * | 0 | 1 | 0 || 0 | 1 || 1 0 | light gray 77 * | 0 | 0 | 0 || 0 | 0 || 1 1 | white 78 */ 79 80 static const u8 st7586_lookup[] = { 0x7, 0x4, 0x2, 0x0 }; 81 82 static void st7586_xrgb8888_to_gray332(u8 *dst, void *vaddr, 83 struct drm_framebuffer *fb, 84 struct drm_rect *clip, 85 struct drm_format_conv_state *fmtcnv_state) 86 { 87 size_t len = (clip->x2 - clip->x1) * (clip->y2 - clip->y1); 88 unsigned int x, y; 89 u8 *src, *buf, val; 90 struct iosys_map dst_map, vmap; 91 92 buf = kmalloc(len, GFP_KERNEL); 93 if (!buf) 94 return; 95 96 iosys_map_set_vaddr(&dst_map, buf); 97 iosys_map_set_vaddr(&vmap, vaddr); 98 drm_fb_xrgb8888_to_gray8(&dst_map, NULL, &vmap, fb, clip, fmtcnv_state); 99 src = buf; 100 101 for (y = clip->y1; y < clip->y2; y++) { 102 for (x = clip->x1; x < clip->x2; x += 3) { 103 val = st7586_lookup[*src++ >> 6] << 5; 104 val |= st7586_lookup[*src++ >> 6] << 2; 105 val |= st7586_lookup[*src++ >> 6] >> 1; 106 *dst++ = val; 107 } 108 } 109 110 kfree(buf); 111 } 112 113 static int st7586_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb, 114 struct drm_rect *clip, struct drm_format_conv_state *fmtcnv_state) 115 { 116 int ret; 117 118 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 119 if (ret) 120 return ret; 121 122 st7586_xrgb8888_to_gray332(dst, src->vaddr, fb, clip, fmtcnv_state); 123 124 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 125 126 return 0; 127 } 128 129 static void st7586_fb_dirty(struct iosys_map *src, struct drm_framebuffer *fb, 130 struct drm_rect *rect, struct drm_format_conv_state *fmtcnv_state) 131 { 132 struct mipi_dbi_dev *dbidev = drm_to_mipi_dbi_dev(fb->dev); 133 struct mipi_dbi *dbi = &dbidev->dbi; 134 int start, end, ret = 0; 135 136 /* 3 pixels per byte, so grow clip to nearest multiple of 3 */ 137 rect->x1 = rounddown(rect->x1, 3); 138 rect->x2 = roundup(rect->x2, 3); 139 140 DRM_DEBUG_KMS("Flushing [FB:%d] " DRM_RECT_FMT "\n", fb->base.id, DRM_RECT_ARG(rect)); 141 142 ret = st7586_buf_copy(dbidev->tx_buf, src, fb, rect, fmtcnv_state); 143 if (ret) 144 goto err_msg; 145 146 /* Pixels are packed 3 per byte */ 147 start = rect->x1 / 3; 148 end = rect->x2 / 3; 149 150 mipi_dbi_command(dbi, MIPI_DCS_SET_COLUMN_ADDRESS, 151 (start >> 8) & 0xFF, start & 0xFF, 152 (end >> 8) & 0xFF, (end - 1) & 0xFF); 153 mipi_dbi_command(dbi, MIPI_DCS_SET_PAGE_ADDRESS, 154 (rect->y1 >> 8) & 0xFF, rect->y1 & 0xFF, 155 (rect->y2 >> 8) & 0xFF, (rect->y2 - 1) & 0xFF); 156 157 ret = mipi_dbi_command_buf(dbi, MIPI_DCS_WRITE_MEMORY_START, 158 (u8 *)dbidev->tx_buf, 159 (end - start) * (rect->y2 - rect->y1)); 160 err_msg: 161 if (ret) 162 dev_err_once(fb->dev->dev, "Failed to update display %d\n", ret); 163 } 164 165 static const u32 st7586_plane_formats[] = { 166 DRM_FORMAT_XRGB8888, 167 }; 168 169 static const u64 st7586_plane_format_modifiers[] = { 170 DRM_MIPI_DBI_PLANE_FORMAT_MODIFIERS, 171 }; 172 173 static void st7586_plane_helper_atomic_update(struct drm_plane *plane, 174 struct drm_atomic_state *state) 175 { 176 struct drm_plane_state *plane_state = plane->state; 177 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 178 struct drm_framebuffer *fb = plane_state->fb; 179 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 180 struct drm_rect rect; 181 int idx; 182 183 if (!fb) 184 return; 185 186 if (!drm_dev_enter(plane->dev, &idx)) 187 return; 188 189 if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &rect)) 190 st7586_fb_dirty(&shadow_plane_state->data[0], fb, &rect, 191 &shadow_plane_state->fmtcnv_state); 192 193 drm_dev_exit(idx); 194 } 195 196 static const struct drm_plane_helper_funcs st7586_plane_helper_funcs = { 197 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 198 .atomic_check = drm_mipi_dbi_plane_helper_atomic_check, 199 .atomic_update = st7586_plane_helper_atomic_update 200 }; 201 202 static const struct drm_plane_funcs st7586_plane_funcs = { 203 DRM_MIPI_DBI_PLANE_FUNCS, 204 .destroy = drm_plane_cleanup, 205 }; 206 207 static void st7586_crtc_helper_atomic_enable(struct drm_crtc *crtc, 208 struct drm_atomic_state *state) 209 { 210 struct drm_device *drm = crtc->dev; 211 struct st7586_device *st7586 = to_st7586_device(drm); 212 struct mipi_dbi_dev *dbidev = &st7586->dbidev; 213 struct mipi_dbi *dbi = &dbidev->dbi; 214 int idx, ret; 215 u8 addr_mode; 216 217 if (!drm_dev_enter(drm, &idx)) 218 return; 219 220 DRM_DEBUG_KMS("\n"); 221 222 ret = mipi_dbi_poweron_reset(dbidev); 223 if (ret) 224 goto out_exit; 225 226 mipi_dbi_command(dbi, ST7586_AUTO_READ_CTRL, 0x9f); 227 mipi_dbi_command(dbi, ST7586_OTP_RW_CTRL, 0x00); 228 229 msleep(10); 230 231 mipi_dbi_command(dbi, ST7586_OTP_READ); 232 233 msleep(20); 234 235 mipi_dbi_command(dbi, ST7586_OTP_CTRL_OUT); 236 mipi_dbi_command(dbi, MIPI_DCS_EXIT_SLEEP_MODE); 237 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_OFF); 238 239 msleep(50); 240 241 mipi_dbi_command(dbi, ST7586_SET_VOP_OFFSET, 0x00); 242 mipi_dbi_command(dbi, ST7586_SET_VOP, 0xe3, 0x00); 243 mipi_dbi_command(dbi, ST7586_SET_BIAS_SYSTEM, 0x02); 244 mipi_dbi_command(dbi, ST7586_SET_BOOST_LEVEL, 0x04); 245 mipi_dbi_command(dbi, ST7586_ENABLE_ANALOG, 0x1d); 246 mipi_dbi_command(dbi, ST7586_SET_NLINE_INV, 0x00); 247 mipi_dbi_command(dbi, ST7586_DISP_MODE_GRAY); 248 mipi_dbi_command(dbi, ST7586_ENABLE_DDRAM, 0x02); 249 250 switch (dbidev->rotation) { 251 default: 252 addr_mode = 0x00; 253 break; 254 case 90: 255 addr_mode = ST7586_DISP_CTRL_MY; 256 break; 257 case 180: 258 addr_mode = ST7586_DISP_CTRL_MX | ST7586_DISP_CTRL_MY; 259 break; 260 case 270: 261 addr_mode = ST7586_DISP_CTRL_MX; 262 break; 263 } 264 mipi_dbi_command(dbi, MIPI_DCS_SET_ADDRESS_MODE, addr_mode); 265 266 mipi_dbi_command(dbi, ST7586_SET_DISP_DUTY, 0x7f); 267 mipi_dbi_command(dbi, ST7586_SET_PART_DISP, 0xa0); 268 mipi_dbi_command(dbi, MIPI_DCS_SET_PARTIAL_ROWS, 0x00, 0x00, 0x00, 0x77); 269 mipi_dbi_command(dbi, MIPI_DCS_EXIT_INVERT_MODE); 270 271 msleep(100); 272 273 mipi_dbi_command(dbi, MIPI_DCS_SET_DISPLAY_ON); 274 out_exit: 275 drm_dev_exit(idx); 276 } 277 278 static void st7586_crtc_helper_atomic_disable(struct drm_crtc *crtc, 279 struct drm_atomic_state *state) 280 { 281 struct drm_device *drm = crtc->dev; 282 struct st7586_device *st7586 = to_st7586_device(drm); 283 struct mipi_dbi_dev *dbidev = &st7586->dbidev; 284 285 /* 286 * This callback is not protected by drm_dev_enter/exit since we want to 287 * turn off the display on regular driver unload. It's highly unlikely 288 * that the underlying SPI controller is gone should this be called after 289 * unplug. 290 */ 291 292 DRM_DEBUG_KMS("\n"); 293 294 mipi_dbi_command(&dbidev->dbi, MIPI_DCS_SET_DISPLAY_OFF); 295 } 296 297 static const struct drm_crtc_helper_funcs st7586_crtc_helper_funcs = { 298 .mode_valid = drm_mipi_dbi_crtc_helper_mode_valid, 299 .atomic_check = drm_mipi_dbi_crtc_helper_atomic_check, 300 .atomic_enable = st7586_crtc_helper_atomic_enable, 301 .atomic_disable = st7586_crtc_helper_atomic_disable, 302 }; 303 304 static const struct drm_crtc_funcs st7586_crtc_funcs = { 305 DRM_MIPI_DBI_CRTC_FUNCS, 306 .destroy = drm_crtc_cleanup, 307 }; 308 309 static const struct drm_encoder_funcs st7586_encoder_funcs = { 310 .destroy = drm_encoder_cleanup, 311 }; 312 313 static const struct drm_connector_helper_funcs st7586_connector_helper_funcs = { 314 DRM_MIPI_DBI_CONNECTOR_HELPER_FUNCS, 315 }; 316 317 static const struct drm_connector_funcs st7586_connector_funcs = { 318 DRM_MIPI_DBI_CONNECTOR_FUNCS, 319 .destroy = drm_connector_cleanup, 320 }; 321 322 static const struct drm_mode_config_helper_funcs st7586_mode_config_helper_funcs = { 323 DRM_MIPI_DBI_MODE_CONFIG_HELPER_FUNCS, 324 }; 325 326 static const struct drm_mode_config_funcs st7586_mode_config_funcs = { 327 DRM_MIPI_DBI_MODE_CONFIG_FUNCS, 328 }; 329 330 static const struct drm_display_mode st7586_mode = { 331 DRM_SIMPLE_MODE(178, 128, 37, 27), 332 }; 333 334 DEFINE_DRM_GEM_DMA_FOPS(st7586_fops); 335 336 static const struct drm_driver st7586_driver = { 337 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 338 .fops = &st7586_fops, 339 DRM_GEM_DMA_DRIVER_OPS_VMAP, 340 DRM_FBDEV_DMA_DRIVER_OPS, 341 .debugfs_init = mipi_dbi_debugfs_init, 342 .name = "st7586", 343 .desc = "Sitronix ST7586", 344 .major = 1, 345 .minor = 0, 346 }; 347 348 static const struct of_device_id st7586_of_match[] = { 349 { .compatible = "lego,ev3-lcd" }, 350 {}, 351 }; 352 MODULE_DEVICE_TABLE(of, st7586_of_match); 353 354 static const struct spi_device_id st7586_id[] = { 355 { "ev3-lcd", 0 }, 356 { }, 357 }; 358 MODULE_DEVICE_TABLE(spi, st7586_id); 359 360 static int st7586_probe(struct spi_device *spi) 361 { 362 struct device *dev = &spi->dev; 363 struct st7586_device *st7586; 364 struct mipi_dbi_dev *dbidev; 365 struct drm_device *drm; 366 struct mipi_dbi *dbi; 367 struct gpio_desc *a0; 368 struct drm_plane *plane; 369 struct drm_crtc *crtc; 370 struct drm_encoder *encoder; 371 struct drm_connector *connector; 372 u32 rotation = 0; 373 size_t bufsize; 374 int ret; 375 376 st7586 = devm_drm_dev_alloc(dev, &st7586_driver, struct st7586_device, dbidev.drm); 377 if (IS_ERR(st7586)) 378 return PTR_ERR(st7586); 379 dbidev = &st7586->dbidev; 380 dbi = &dbidev->dbi; 381 drm = &dbidev->drm; 382 383 bufsize = (st7586_mode.vdisplay + 2) / 3 * st7586_mode.hdisplay; 384 385 dbi->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 386 if (IS_ERR(dbi->reset)) 387 return dev_err_probe(dev, PTR_ERR(dbi->reset), "Failed to get GPIO 'reset'\n"); 388 389 a0 = devm_gpiod_get(dev, "a0", GPIOD_OUT_LOW); 390 if (IS_ERR(a0)) 391 return dev_err_probe(dev, PTR_ERR(a0), "Failed to get GPIO 'a0'\n"); 392 393 device_property_read_u32(dev, "rotation", &rotation); 394 395 ret = mipi_dbi_spi_init(spi, dbi, a0); 396 if (ret) 397 return ret; 398 399 /* 400 * Override value set by mipi_dbi_spi_init(). This driver is a bit 401 * non-standard, so best to set it explicitly here. 402 */ 403 dbi->write_memory_bpw = 8; 404 405 /* Cannot read from this controller via SPI */ 406 dbi->read_commands = NULL; 407 408 ret = drm_mipi_dbi_dev_init(dbidev, &st7586_mode, st7586_plane_formats[0], 409 rotation, bufsize); 410 if (ret) 411 return ret; 412 413 ret = drmm_mode_config_init(drm); 414 if (ret) 415 return ret; 416 417 drm->mode_config.min_width = dbidev->mode.hdisplay; 418 drm->mode_config.max_width = dbidev->mode.hdisplay; 419 drm->mode_config.min_height = dbidev->mode.vdisplay; 420 drm->mode_config.max_height = dbidev->mode.vdisplay; 421 drm->mode_config.funcs = &st7586_mode_config_funcs; 422 drm->mode_config.preferred_depth = 24; 423 drm->mode_config.helper_private = &st7586_mode_config_helper_funcs; 424 425 plane = &st7586->plane; 426 ret = drm_universal_plane_init(drm, plane, 0, &st7586_plane_funcs, 427 st7586_plane_formats, ARRAY_SIZE(st7586_plane_formats), 428 st7586_plane_format_modifiers, 429 DRM_PLANE_TYPE_PRIMARY, NULL); 430 if (ret) 431 return ret; 432 drm_plane_helper_add(plane, &st7586_plane_helper_funcs); 433 drm_plane_enable_fb_damage_clips(plane); 434 435 crtc = &st7586->crtc; 436 ret = drm_crtc_init_with_planes(drm, crtc, plane, NULL, &st7586_crtc_funcs, NULL); 437 if (ret) 438 return ret; 439 drm_crtc_helper_add(crtc, &st7586_crtc_helper_funcs); 440 441 encoder = &st7586->encoder; 442 ret = drm_encoder_init(drm, encoder, &st7586_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); 443 if (ret) 444 return ret; 445 encoder->possible_crtcs = drm_crtc_mask(crtc); 446 447 connector = &st7586->connector; 448 ret = drm_connector_init(drm, connector, &st7586_connector_funcs, 449 DRM_MODE_CONNECTOR_SPI); 450 if (ret) 451 return ret; 452 drm_connector_helper_add(connector, &st7586_connector_helper_funcs); 453 454 ret = drm_connector_attach_encoder(connector, encoder); 455 if (ret) 456 return ret; 457 458 drm_mode_config_reset(drm); 459 460 ret = drm_dev_register(drm, 0); 461 if (ret) 462 return ret; 463 464 spi_set_drvdata(spi, drm); 465 466 drm_client_setup(drm, NULL); 467 468 return 0; 469 } 470 471 static void st7586_remove(struct spi_device *spi) 472 { 473 struct drm_device *drm = spi_get_drvdata(spi); 474 475 drm_dev_unplug(drm); 476 drm_atomic_helper_shutdown(drm); 477 } 478 479 static void st7586_shutdown(struct spi_device *spi) 480 { 481 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 482 } 483 484 static struct spi_driver st7586_spi_driver = { 485 .driver = { 486 .name = "st7586", 487 .of_match_table = st7586_of_match, 488 }, 489 .id_table = st7586_id, 490 .probe = st7586_probe, 491 .remove = st7586_remove, 492 .shutdown = st7586_shutdown, 493 }; 494 module_spi_driver(st7586_spi_driver); 495 496 MODULE_DESCRIPTION("Sitronix ST7586 DRM driver"); 497 MODULE_AUTHOR("David Lechner <david@lechnology.com>"); 498 MODULE_LICENSE("GPL"); 499