1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * DRM driver for Pervasive Displays RePaper branded e-ink panels 4 * 5 * Copyright 2013-2017 Pervasive Displays, Inc. 6 * Copyright 2017 Noralf Trønnes 7 * 8 * The driver supports: 9 * Material Film: Aurora Mb (V231) 10 * Driver IC: G2 (eTC) 11 * 12 * The controller code was taken from the userspace driver: 13 * https://github.com/repaper/gratis 14 */ 15 16 #include <linux/delay.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/module.h> 19 #include <linux/property.h> 20 #include <linux/sched/clock.h> 21 #include <linux/spi/spi.h> 22 #include <linux/thermal.h> 23 24 #include <drm/drm_atomic_helper.h> 25 #include <drm/drm_client_setup.h> 26 #include <drm/drm_connector.h> 27 #include <drm/drm_damage_helper.h> 28 #include <drm/drm_drv.h> 29 #include <drm/drm_fb_dma_helper.h> 30 #include <drm/drm_fbdev_dma.h> 31 #include <drm/drm_format_helper.h> 32 #include <drm/drm_framebuffer.h> 33 #include <drm/drm_gem_atomic_helper.h> 34 #include <drm/drm_gem_dma_helper.h> 35 #include <drm/drm_gem_framebuffer_helper.h> 36 #include <drm/drm_managed.h> 37 #include <drm/drm_modes.h> 38 #include <drm/drm_rect.h> 39 #include <drm/drm_probe_helper.h> 40 #include <drm/drm_simple_kms_helper.h> 41 42 #define REPAPER_RID_G2_COG_ID 0x12 43 44 enum repaper_model { 45 /* 0 is reserved to avoid clashing with NULL */ 46 E1144CS021 = 1, 47 E1190CS021, 48 E2200CS021, 49 E2271CS021, 50 }; 51 52 enum repaper_stage { /* Image pixel -> Display pixel */ 53 REPAPER_COMPENSATE, /* B -> W, W -> B (Current Image) */ 54 REPAPER_WHITE, /* B -> N, W -> W (Current Image) */ 55 REPAPER_INVERSE, /* B -> N, W -> B (New Image) */ 56 REPAPER_NORMAL /* B -> B, W -> W (New Image) */ 57 }; 58 59 enum repaper_epd_border_byte { 60 REPAPER_BORDER_BYTE_NONE, 61 REPAPER_BORDER_BYTE_ZERO, 62 REPAPER_BORDER_BYTE_SET, 63 }; 64 65 struct repaper_epd { 66 struct drm_device drm; 67 struct drm_simple_display_pipe pipe; 68 const struct drm_display_mode *mode; 69 struct drm_connector connector; 70 struct spi_device *spi; 71 72 struct gpio_desc *panel_on; 73 struct gpio_desc *border; 74 struct gpio_desc *discharge; 75 struct gpio_desc *reset; 76 struct gpio_desc *busy; 77 78 struct thermal_zone_device *thermal; 79 80 unsigned int height; 81 unsigned int width; 82 unsigned int bytes_per_scan; 83 const u8 *channel_select; 84 unsigned int stage_time; 85 unsigned int factored_stage_time; 86 bool middle_scan; 87 bool pre_border_byte; 88 enum repaper_epd_border_byte border_byte; 89 90 u8 *line_buffer; 91 void *current_frame; 92 93 bool cleared; 94 bool partial; 95 }; 96 97 static inline struct repaper_epd *drm_to_epd(struct drm_device *drm) 98 { 99 return container_of(drm, struct repaper_epd, drm); 100 } 101 102 static int repaper_spi_transfer(struct spi_device *spi, u8 header, 103 const void *tx, void *rx, size_t len) 104 { 105 void *txbuf = NULL, *rxbuf = NULL; 106 struct spi_transfer tr[2] = {}; 107 u8 *headerbuf; 108 int ret; 109 110 headerbuf = kmalloc(1, GFP_KERNEL); 111 if (!headerbuf) 112 return -ENOMEM; 113 114 headerbuf[0] = header; 115 tr[0].tx_buf = headerbuf; 116 tr[0].len = 1; 117 118 /* Stack allocated tx? */ 119 if (tx && len <= 32) { 120 txbuf = kmemdup(tx, len, GFP_KERNEL); 121 if (!txbuf) { 122 ret = -ENOMEM; 123 goto out_free; 124 } 125 } 126 127 if (rx) { 128 rxbuf = kmalloc(len, GFP_KERNEL); 129 if (!rxbuf) { 130 ret = -ENOMEM; 131 goto out_free; 132 } 133 } 134 135 tr[1].tx_buf = txbuf ? txbuf : tx; 136 tr[1].rx_buf = rxbuf; 137 tr[1].len = len; 138 139 ndelay(80); 140 ret = spi_sync_transfer(spi, tr, 2); 141 if (rx && !ret) 142 memcpy(rx, rxbuf, len); 143 144 out_free: 145 kfree(headerbuf); 146 kfree(txbuf); 147 kfree(rxbuf); 148 149 return ret; 150 } 151 152 static int repaper_write_buf(struct spi_device *spi, u8 reg, 153 const u8 *buf, size_t len) 154 { 155 int ret; 156 157 ret = repaper_spi_transfer(spi, 0x70, ®, NULL, 1); 158 if (ret) 159 return ret; 160 161 return repaper_spi_transfer(spi, 0x72, buf, NULL, len); 162 } 163 164 static int repaper_write_val(struct spi_device *spi, u8 reg, u8 val) 165 { 166 return repaper_write_buf(spi, reg, &val, 1); 167 } 168 169 static int repaper_read_val(struct spi_device *spi, u8 reg) 170 { 171 int ret; 172 u8 val; 173 174 ret = repaper_spi_transfer(spi, 0x70, ®, NULL, 1); 175 if (ret) 176 return ret; 177 178 ret = repaper_spi_transfer(spi, 0x73, NULL, &val, 1); 179 180 return ret ? ret : val; 181 } 182 183 static int repaper_read_id(struct spi_device *spi) 184 { 185 int ret; 186 u8 id; 187 188 ret = repaper_spi_transfer(spi, 0x71, NULL, &id, 1); 189 190 return ret ? ret : id; 191 } 192 193 static void repaper_spi_mosi_low(struct spi_device *spi) 194 { 195 const u8 buf[1] = { 0 }; 196 197 spi_write(spi, buf, 1); 198 } 199 200 /* pixels on display are numbered from 1 so even is actually bits 1,3,5,... */ 201 static void repaper_even_pixels(struct repaper_epd *epd, u8 **pp, 202 const u8 *data, u8 fixed_value, const u8 *mask, 203 enum repaper_stage stage) 204 { 205 unsigned int b; 206 207 for (b = 0; b < (epd->width / 8); b++) { 208 if (data) { 209 u8 pixels = data[b] & 0xaa; 210 u8 pixel_mask = 0xff; 211 u8 p1, p2, p3, p4; 212 213 if (mask) { 214 pixel_mask = (mask[b] ^ pixels) & 0xaa; 215 pixel_mask |= pixel_mask >> 1; 216 } 217 218 switch (stage) { 219 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */ 220 pixels = 0xaa | ((pixels ^ 0xaa) >> 1); 221 break; 222 case REPAPER_WHITE: /* B -> N, W -> W (Current) */ 223 pixels = 0x55 + ((pixels ^ 0xaa) >> 1); 224 break; 225 case REPAPER_INVERSE: /* B -> N, W -> B (New) */ 226 pixels = 0x55 | (pixels ^ 0xaa); 227 break; 228 case REPAPER_NORMAL: /* B -> B, W -> W (New) */ 229 pixels = 0xaa | (pixels >> 1); 230 break; 231 } 232 233 pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55); 234 p1 = (pixels >> 6) & 0x03; 235 p2 = (pixels >> 4) & 0x03; 236 p3 = (pixels >> 2) & 0x03; 237 p4 = (pixels >> 0) & 0x03; 238 pixels = (p1 << 0) | (p2 << 2) | (p3 << 4) | (p4 << 6); 239 *(*pp)++ = pixels; 240 } else { 241 *(*pp)++ = fixed_value; 242 } 243 } 244 } 245 246 /* pixels on display are numbered from 1 so odd is actually bits 0,2,4,... */ 247 static void repaper_odd_pixels(struct repaper_epd *epd, u8 **pp, 248 const u8 *data, u8 fixed_value, const u8 *mask, 249 enum repaper_stage stage) 250 { 251 unsigned int b; 252 253 for (b = epd->width / 8; b > 0; b--) { 254 if (data) { 255 u8 pixels = data[b - 1] & 0x55; 256 u8 pixel_mask = 0xff; 257 258 if (mask) { 259 pixel_mask = (mask[b - 1] ^ pixels) & 0x55; 260 pixel_mask |= pixel_mask << 1; 261 } 262 263 switch (stage) { 264 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */ 265 pixels = 0xaa | (pixels ^ 0x55); 266 break; 267 case REPAPER_WHITE: /* B -> N, W -> W (Current) */ 268 pixels = 0x55 + (pixels ^ 0x55); 269 break; 270 case REPAPER_INVERSE: /* B -> N, W -> B (New) */ 271 pixels = 0x55 | ((pixels ^ 0x55) << 1); 272 break; 273 case REPAPER_NORMAL: /* B -> B, W -> W (New) */ 274 pixels = 0xaa | pixels; 275 break; 276 } 277 278 pixels = (pixels & pixel_mask) | (~pixel_mask & 0x55); 279 *(*pp)++ = pixels; 280 } else { 281 *(*pp)++ = fixed_value; 282 } 283 } 284 } 285 286 /* interleave bits: (byte)76543210 -> (16 bit).7.6.5.4.3.2.1 */ 287 static inline u16 repaper_interleave_bits(u16 value) 288 { 289 value = (value | (value << 4)) & 0x0f0f; 290 value = (value | (value << 2)) & 0x3333; 291 value = (value | (value << 1)) & 0x5555; 292 293 return value; 294 } 295 296 /* pixels on display are numbered from 1 */ 297 static void repaper_all_pixels(struct repaper_epd *epd, u8 **pp, 298 const u8 *data, u8 fixed_value, const u8 *mask, 299 enum repaper_stage stage) 300 { 301 unsigned int b; 302 303 for (b = epd->width / 8; b > 0; b--) { 304 if (data) { 305 u16 pixels = repaper_interleave_bits(data[b - 1]); 306 u16 pixel_mask = 0xffff; 307 308 if (mask) { 309 pixel_mask = repaper_interleave_bits(mask[b - 1]); 310 311 pixel_mask = (pixel_mask ^ pixels) & 0x5555; 312 pixel_mask |= pixel_mask << 1; 313 } 314 315 switch (stage) { 316 case REPAPER_COMPENSATE: /* B -> W, W -> B (Current) */ 317 pixels = 0xaaaa | (pixels ^ 0x5555); 318 break; 319 case REPAPER_WHITE: /* B -> N, W -> W (Current) */ 320 pixels = 0x5555 + (pixels ^ 0x5555); 321 break; 322 case REPAPER_INVERSE: /* B -> N, W -> B (New) */ 323 pixels = 0x5555 | ((pixels ^ 0x5555) << 1); 324 break; 325 case REPAPER_NORMAL: /* B -> B, W -> W (New) */ 326 pixels = 0xaaaa | pixels; 327 break; 328 } 329 330 pixels = (pixels & pixel_mask) | (~pixel_mask & 0x5555); 331 *(*pp)++ = pixels >> 8; 332 *(*pp)++ = pixels; 333 } else { 334 *(*pp)++ = fixed_value; 335 *(*pp)++ = fixed_value; 336 } 337 } 338 } 339 340 /* output one line of scan and data bytes to the display */ 341 static void repaper_one_line(struct repaper_epd *epd, unsigned int line, 342 const u8 *data, u8 fixed_value, const u8 *mask, 343 enum repaper_stage stage) 344 { 345 u8 *p = epd->line_buffer; 346 unsigned int b; 347 348 repaper_spi_mosi_low(epd->spi); 349 350 if (epd->pre_border_byte) 351 *p++ = 0x00; 352 353 if (epd->middle_scan) { 354 /* data bytes */ 355 repaper_odd_pixels(epd, &p, data, fixed_value, mask, stage); 356 357 /* scan line */ 358 for (b = epd->bytes_per_scan; b > 0; b--) { 359 if (line / 4 == b - 1) 360 *p++ = 0x03 << (2 * (line & 0x03)); 361 else 362 *p++ = 0x00; 363 } 364 365 /* data bytes */ 366 repaper_even_pixels(epd, &p, data, fixed_value, mask, stage); 367 } else { 368 /* 369 * even scan line, but as lines on display are numbered from 1, 370 * line: 1,3,5,... 371 */ 372 for (b = 0; b < epd->bytes_per_scan; b++) { 373 if (0 != (line & 0x01) && line / 8 == b) 374 *p++ = 0xc0 >> (line & 0x06); 375 else 376 *p++ = 0x00; 377 } 378 379 /* data bytes */ 380 repaper_all_pixels(epd, &p, data, fixed_value, mask, stage); 381 382 /* 383 * odd scan line, but as lines on display are numbered from 1, 384 * line: 0,2,4,6,... 385 */ 386 for (b = epd->bytes_per_scan; b > 0; b--) { 387 if (0 == (line & 0x01) && line / 8 == b - 1) 388 *p++ = 0x03 << (line & 0x06); 389 else 390 *p++ = 0x00; 391 } 392 } 393 394 switch (epd->border_byte) { 395 case REPAPER_BORDER_BYTE_NONE: 396 break; 397 398 case REPAPER_BORDER_BYTE_ZERO: 399 *p++ = 0x00; 400 break; 401 402 case REPAPER_BORDER_BYTE_SET: 403 switch (stage) { 404 case REPAPER_COMPENSATE: 405 case REPAPER_WHITE: 406 case REPAPER_INVERSE: 407 *p++ = 0x00; 408 break; 409 case REPAPER_NORMAL: 410 *p++ = 0xaa; 411 break; 412 } 413 break; 414 } 415 416 repaper_write_buf(epd->spi, 0x0a, epd->line_buffer, 417 p - epd->line_buffer); 418 419 /* Output data to panel */ 420 repaper_write_val(epd->spi, 0x02, 0x07); 421 422 repaper_spi_mosi_low(epd->spi); 423 } 424 425 static void repaper_frame_fixed(struct repaper_epd *epd, u8 fixed_value, 426 enum repaper_stage stage) 427 { 428 unsigned int line; 429 430 for (line = 0; line < epd->height; line++) 431 repaper_one_line(epd, line, NULL, fixed_value, NULL, stage); 432 } 433 434 static void repaper_frame_data(struct repaper_epd *epd, const u8 *image, 435 const u8 *mask, enum repaper_stage stage) 436 { 437 unsigned int line; 438 439 if (!mask) { 440 for (line = 0; line < epd->height; line++) { 441 repaper_one_line(epd, line, 442 &image[line * (epd->width / 8)], 443 0, NULL, stage); 444 } 445 } else { 446 for (line = 0; line < epd->height; line++) { 447 size_t n = line * epd->width / 8; 448 449 repaper_one_line(epd, line, &image[n], 0, &mask[n], 450 stage); 451 } 452 } 453 } 454 455 static void repaper_frame_fixed_repeat(struct repaper_epd *epd, u8 fixed_value, 456 enum repaper_stage stage) 457 { 458 u64 start = local_clock(); 459 u64 end = start + (epd->factored_stage_time * 1000 * 1000); 460 461 do { 462 repaper_frame_fixed(epd, fixed_value, stage); 463 } while (local_clock() < end); 464 } 465 466 static void repaper_frame_data_repeat(struct repaper_epd *epd, const u8 *image, 467 const u8 *mask, enum repaper_stage stage) 468 { 469 u64 start = local_clock(); 470 u64 end = start + (epd->factored_stage_time * 1000 * 1000); 471 472 do { 473 repaper_frame_data(epd, image, mask, stage); 474 } while (local_clock() < end); 475 } 476 477 static void repaper_get_temperature(struct repaper_epd *epd) 478 { 479 int ret, temperature = 0; 480 unsigned int factor10x; 481 482 if (!epd->thermal) 483 return; 484 485 ret = thermal_zone_get_temp(epd->thermal, &temperature); 486 if (ret) { 487 DRM_DEV_ERROR(&epd->spi->dev, "Failed to get temperature (%d)\n", ret); 488 return; 489 } 490 491 temperature /= 1000; 492 493 if (temperature <= -10) 494 factor10x = 170; 495 else if (temperature <= -5) 496 factor10x = 120; 497 else if (temperature <= 5) 498 factor10x = 80; 499 else if (temperature <= 10) 500 factor10x = 40; 501 else if (temperature <= 15) 502 factor10x = 30; 503 else if (temperature <= 20) 504 factor10x = 20; 505 else if (temperature <= 40) 506 factor10x = 10; 507 else 508 factor10x = 7; 509 510 epd->factored_stage_time = epd->stage_time * factor10x / 10; 511 } 512 513 static int repaper_fb_dirty(struct drm_framebuffer *fb, 514 struct drm_format_conv_state *fmtcnv_state) 515 { 516 struct drm_gem_dma_object *dma_obj = drm_fb_dma_get_gem_obj(fb, 0); 517 struct repaper_epd *epd = drm_to_epd(fb->dev); 518 unsigned int dst_pitch = 0; 519 struct iosys_map dst, vmap; 520 struct drm_rect clip; 521 int idx, ret = 0; 522 u8 *buf = NULL; 523 524 if (!drm_dev_enter(fb->dev, &idx)) 525 return -ENODEV; 526 527 /* repaper can't do partial updates */ 528 clip.x1 = 0; 529 clip.x2 = fb->width; 530 clip.y1 = 0; 531 clip.y2 = fb->height; 532 533 repaper_get_temperature(epd); 534 535 DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id, 536 epd->factored_stage_time); 537 538 buf = kmalloc(fb->width * fb->height / 8, GFP_KERNEL); 539 if (!buf) { 540 ret = -ENOMEM; 541 goto out_exit; 542 } 543 544 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 545 if (ret) 546 goto out_free; 547 548 iosys_map_set_vaddr(&dst, buf); 549 iosys_map_set_vaddr(&vmap, dma_obj->vaddr); 550 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, &vmap, fb, &clip, fmtcnv_state); 551 552 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 553 554 if (epd->partial) { 555 repaper_frame_data_repeat(epd, buf, epd->current_frame, 556 REPAPER_NORMAL); 557 } else if (epd->cleared) { 558 repaper_frame_data_repeat(epd, epd->current_frame, NULL, 559 REPAPER_COMPENSATE); 560 repaper_frame_data_repeat(epd, epd->current_frame, NULL, 561 REPAPER_WHITE); 562 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE); 563 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL); 564 565 epd->partial = true; 566 } else { 567 /* Clear display (anything -> white) */ 568 repaper_frame_fixed_repeat(epd, 0xff, REPAPER_COMPENSATE); 569 repaper_frame_fixed_repeat(epd, 0xff, REPAPER_WHITE); 570 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_INVERSE); 571 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_NORMAL); 572 573 /* Assuming a clear (white) screen output an image */ 574 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_COMPENSATE); 575 repaper_frame_fixed_repeat(epd, 0xaa, REPAPER_WHITE); 576 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_INVERSE); 577 repaper_frame_data_repeat(epd, buf, NULL, REPAPER_NORMAL); 578 579 epd->cleared = true; 580 epd->partial = true; 581 } 582 583 memcpy(epd->current_frame, buf, fb->width * fb->height / 8); 584 585 /* 586 * An extra frame write is needed if pixels are set in the bottom line, 587 * or else grey lines rises up from the pixels 588 */ 589 if (epd->pre_border_byte) { 590 unsigned int x; 591 592 for (x = 0; x < (fb->width / 8); x++) 593 if (buf[x + (fb->width * (fb->height - 1) / 8)]) { 594 repaper_frame_data_repeat(epd, buf, 595 epd->current_frame, 596 REPAPER_NORMAL); 597 break; 598 } 599 } 600 601 out_free: 602 kfree(buf); 603 out_exit: 604 drm_dev_exit(idx); 605 606 return ret; 607 } 608 609 static void power_off(struct repaper_epd *epd) 610 { 611 /* Turn off power and all signals */ 612 gpiod_set_value_cansleep(epd->reset, 0); 613 gpiod_set_value_cansleep(epd->panel_on, 0); 614 if (epd->border) 615 gpiod_set_value_cansleep(epd->border, 0); 616 617 /* Ensure SPI MOSI and CLOCK are Low before CS Low */ 618 repaper_spi_mosi_low(epd->spi); 619 620 /* Discharge pulse */ 621 gpiod_set_value_cansleep(epd->discharge, 1); 622 msleep(150); 623 gpiod_set_value_cansleep(epd->discharge, 0); 624 } 625 626 static enum drm_mode_status repaper_pipe_mode_valid(struct drm_simple_display_pipe *pipe, 627 const struct drm_display_mode *mode) 628 { 629 struct drm_crtc *crtc = &pipe->crtc; 630 struct repaper_epd *epd = drm_to_epd(crtc->dev); 631 632 return drm_crtc_helper_mode_valid_fixed(crtc, mode, epd->mode); 633 } 634 635 static void repaper_pipe_enable(struct drm_simple_display_pipe *pipe, 636 struct drm_crtc_state *crtc_state, 637 struct drm_plane_state *plane_state) 638 { 639 struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev); 640 struct spi_device *spi = epd->spi; 641 struct device *dev = &spi->dev; 642 bool dc_ok = false; 643 int i, ret, idx; 644 645 if (!drm_dev_enter(pipe->crtc.dev, &idx)) 646 return; 647 648 DRM_DEBUG_DRIVER("\n"); 649 650 /* Power up sequence */ 651 gpiod_set_value_cansleep(epd->reset, 0); 652 gpiod_set_value_cansleep(epd->panel_on, 0); 653 gpiod_set_value_cansleep(epd->discharge, 0); 654 if (epd->border) 655 gpiod_set_value_cansleep(epd->border, 0); 656 repaper_spi_mosi_low(spi); 657 usleep_range(5000, 10000); 658 659 gpiod_set_value_cansleep(epd->panel_on, 1); 660 /* 661 * This delay comes from the repaper.org userspace driver, it's not 662 * mentioned in the datasheet. 663 */ 664 usleep_range(10000, 15000); 665 gpiod_set_value_cansleep(epd->reset, 1); 666 if (epd->border) 667 gpiod_set_value_cansleep(epd->border, 1); 668 usleep_range(5000, 10000); 669 gpiod_set_value_cansleep(epd->reset, 0); 670 usleep_range(5000, 10000); 671 gpiod_set_value_cansleep(epd->reset, 1); 672 usleep_range(5000, 10000); 673 674 /* Wait for COG to become ready */ 675 for (i = 100; i > 0; i--) { 676 if (!gpiod_get_value_cansleep(epd->busy)) 677 break; 678 679 usleep_range(10, 100); 680 } 681 682 if (!i) { 683 DRM_DEV_ERROR(dev, "timeout waiting for panel to become ready.\n"); 684 power_off(epd); 685 goto out_exit; 686 } 687 688 repaper_read_id(spi); 689 ret = repaper_read_id(spi); 690 if (ret != REPAPER_RID_G2_COG_ID) { 691 if (ret < 0) 692 dev_err(dev, "failed to read chip (%d)\n", ret); 693 else 694 dev_err(dev, "wrong COG ID 0x%02x\n", ret); 695 power_off(epd); 696 goto out_exit; 697 } 698 699 /* Disable OE */ 700 repaper_write_val(spi, 0x02, 0x40); 701 702 ret = repaper_read_val(spi, 0x0f); 703 if (ret < 0 || !(ret & 0x80)) { 704 if (ret < 0) 705 DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret); 706 else 707 DRM_DEV_ERROR(dev, "panel is reported broken\n"); 708 power_off(epd); 709 goto out_exit; 710 } 711 712 /* Power saving mode */ 713 repaper_write_val(spi, 0x0b, 0x02); 714 /* Channel select */ 715 repaper_write_buf(spi, 0x01, epd->channel_select, 8); 716 /* High power mode osc */ 717 repaper_write_val(spi, 0x07, 0xd1); 718 /* Power setting */ 719 repaper_write_val(spi, 0x08, 0x02); 720 /* Vcom level */ 721 repaper_write_val(spi, 0x09, 0xc2); 722 /* Power setting */ 723 repaper_write_val(spi, 0x04, 0x03); 724 /* Driver latch on */ 725 repaper_write_val(spi, 0x03, 0x01); 726 /* Driver latch off */ 727 repaper_write_val(spi, 0x03, 0x00); 728 usleep_range(5000, 10000); 729 730 /* Start chargepump */ 731 for (i = 0; i < 4; ++i) { 732 /* Charge pump positive voltage on - VGH/VDL on */ 733 repaper_write_val(spi, 0x05, 0x01); 734 msleep(240); 735 736 /* Charge pump negative voltage on - VGL/VDL on */ 737 repaper_write_val(spi, 0x05, 0x03); 738 msleep(40); 739 740 /* Charge pump Vcom on - Vcom driver on */ 741 repaper_write_val(spi, 0x05, 0x0f); 742 msleep(40); 743 744 /* check DC/DC */ 745 ret = repaper_read_val(spi, 0x0f); 746 if (ret < 0) { 747 DRM_DEV_ERROR(dev, "failed to read chip (%d)\n", ret); 748 power_off(epd); 749 goto out_exit; 750 } 751 752 if (ret & 0x40) { 753 dc_ok = true; 754 break; 755 } 756 } 757 758 if (!dc_ok) { 759 DRM_DEV_ERROR(dev, "dc/dc failed\n"); 760 power_off(epd); 761 goto out_exit; 762 } 763 764 /* 765 * Output enable to disable 766 * The userspace driver sets this to 0x04, but the datasheet says 0x06 767 */ 768 repaper_write_val(spi, 0x02, 0x04); 769 770 epd->partial = false; 771 out_exit: 772 drm_dev_exit(idx); 773 } 774 775 static void repaper_pipe_disable(struct drm_simple_display_pipe *pipe) 776 { 777 struct repaper_epd *epd = drm_to_epd(pipe->crtc.dev); 778 struct spi_device *spi = epd->spi; 779 unsigned int line; 780 781 /* 782 * This callback is not protected by drm_dev_enter/exit since we want to 783 * turn off the display on regular driver unload. It's highly unlikely 784 * that the underlying SPI controller is gone should this be called after 785 * unplug. 786 */ 787 788 DRM_DEBUG_DRIVER("\n"); 789 790 /* Nothing frame */ 791 for (line = 0; line < epd->height; line++) 792 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL, 793 REPAPER_COMPENSATE); 794 795 /* 2.7" */ 796 if (epd->border) { 797 /* Dummy line */ 798 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL, 799 REPAPER_COMPENSATE); 800 msleep(25); 801 gpiod_set_value_cansleep(epd->border, 0); 802 msleep(200); 803 gpiod_set_value_cansleep(epd->border, 1); 804 } else { 805 /* Border dummy line */ 806 repaper_one_line(epd, 0x7fffu, NULL, 0x00, NULL, 807 REPAPER_NORMAL); 808 msleep(200); 809 } 810 811 /* not described in datasheet */ 812 repaper_write_val(spi, 0x0b, 0x00); 813 /* Latch reset turn on */ 814 repaper_write_val(spi, 0x03, 0x01); 815 /* Power off charge pump Vcom */ 816 repaper_write_val(spi, 0x05, 0x03); 817 /* Power off charge pump neg voltage */ 818 repaper_write_val(spi, 0x05, 0x01); 819 msleep(120); 820 /* Discharge internal */ 821 repaper_write_val(spi, 0x04, 0x80); 822 /* turn off all charge pumps */ 823 repaper_write_val(spi, 0x05, 0x00); 824 /* Turn off osc */ 825 repaper_write_val(spi, 0x07, 0x01); 826 msleep(50); 827 828 power_off(epd); 829 } 830 831 static void repaper_pipe_update(struct drm_simple_display_pipe *pipe, 832 struct drm_plane_state *old_state) 833 { 834 struct drm_plane_state *state = pipe->plane.state; 835 struct drm_format_conv_state fmtcnv_state = DRM_FORMAT_CONV_STATE_INIT; 836 struct drm_rect rect; 837 838 if (!pipe->crtc.state->active) 839 return; 840 841 if (drm_atomic_helper_damage_merged(old_state, state, &rect)) 842 repaper_fb_dirty(state->fb, &fmtcnv_state); 843 844 drm_format_conv_state_release(&fmtcnv_state); 845 } 846 847 static const struct drm_simple_display_pipe_funcs repaper_pipe_funcs = { 848 .mode_valid = repaper_pipe_mode_valid, 849 .enable = repaper_pipe_enable, 850 .disable = repaper_pipe_disable, 851 .update = repaper_pipe_update, 852 }; 853 854 static int repaper_connector_get_modes(struct drm_connector *connector) 855 { 856 struct repaper_epd *epd = drm_to_epd(connector->dev); 857 858 return drm_connector_helper_get_modes_fixed(connector, epd->mode); 859 } 860 861 static const struct drm_connector_helper_funcs repaper_connector_hfuncs = { 862 .get_modes = repaper_connector_get_modes, 863 }; 864 865 static const struct drm_connector_funcs repaper_connector_funcs = { 866 .reset = drm_atomic_helper_connector_reset, 867 .fill_modes = drm_helper_probe_single_connector_modes, 868 .destroy = drm_connector_cleanup, 869 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 870 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 871 }; 872 873 static const struct drm_mode_config_funcs repaper_mode_config_funcs = { 874 .fb_create = drm_gem_fb_create_with_dirty, 875 .atomic_check = drm_atomic_helper_check, 876 .atomic_commit = drm_atomic_helper_commit, 877 }; 878 879 static const uint32_t repaper_formats[] = { 880 DRM_FORMAT_XRGB8888, 881 }; 882 883 static const struct drm_display_mode repaper_e1144cs021_mode = { 884 DRM_SIMPLE_MODE(128, 96, 29, 22), 885 }; 886 887 static const u8 repaper_e1144cs021_cs[] = { 0x00, 0x00, 0x00, 0x00, 888 0x00, 0x0f, 0xff, 0x00 }; 889 890 static const struct drm_display_mode repaper_e1190cs021_mode = { 891 DRM_SIMPLE_MODE(144, 128, 36, 32), 892 }; 893 894 static const u8 repaper_e1190cs021_cs[] = { 0x00, 0x00, 0x00, 0x03, 895 0xfc, 0x00, 0x00, 0xff }; 896 897 static const struct drm_display_mode repaper_e2200cs021_mode = { 898 DRM_SIMPLE_MODE(200, 96, 46, 22), 899 }; 900 901 static const u8 repaper_e2200cs021_cs[] = { 0x00, 0x00, 0x00, 0x00, 902 0x01, 0xff, 0xe0, 0x00 }; 903 904 static const struct drm_display_mode repaper_e2271cs021_mode = { 905 DRM_SIMPLE_MODE(264, 176, 57, 38), 906 }; 907 908 static const u8 repaper_e2271cs021_cs[] = { 0x00, 0x00, 0x00, 0x7f, 909 0xff, 0xfe, 0x00, 0x00 }; 910 911 DEFINE_DRM_GEM_DMA_FOPS(repaper_fops); 912 913 static const struct drm_driver repaper_driver = { 914 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC, 915 .fops = &repaper_fops, 916 DRM_GEM_DMA_DRIVER_OPS_VMAP, 917 DRM_FBDEV_DMA_DRIVER_OPS, 918 .name = "repaper", 919 .desc = "Pervasive Displays RePaper e-ink panels", 920 .date = "20170405", 921 .major = 1, 922 .minor = 0, 923 }; 924 925 static const struct of_device_id repaper_of_match[] = { 926 { .compatible = "pervasive,e1144cs021", .data = (void *)E1144CS021 }, 927 { .compatible = "pervasive,e1190cs021", .data = (void *)E1190CS021 }, 928 { .compatible = "pervasive,e2200cs021", .data = (void *)E2200CS021 }, 929 { .compatible = "pervasive,e2271cs021", .data = (void *)E2271CS021 }, 930 {}, 931 }; 932 MODULE_DEVICE_TABLE(of, repaper_of_match); 933 934 static const struct spi_device_id repaper_id[] = { 935 { "e1144cs021", E1144CS021 }, 936 { "e1190cs021", E1190CS021 }, 937 { "e2200cs021", E2200CS021 }, 938 { "e2271cs021", E2271CS021 }, 939 { }, 940 }; 941 MODULE_DEVICE_TABLE(spi, repaper_id); 942 943 static int repaper_probe(struct spi_device *spi) 944 { 945 const struct drm_display_mode *mode; 946 const struct spi_device_id *spi_id; 947 struct device *dev = &spi->dev; 948 enum repaper_model model; 949 const char *thermal_zone; 950 struct repaper_epd *epd; 951 size_t line_buffer_size; 952 struct drm_device *drm; 953 const void *match; 954 int ret; 955 956 match = device_get_match_data(dev); 957 if (match) { 958 model = (enum repaper_model)(uintptr_t)match; 959 } else { 960 spi_id = spi_get_device_id(spi); 961 model = (enum repaper_model)spi_id->driver_data; 962 } 963 964 /* The SPI device is used to allocate dma memory */ 965 if (!dev->coherent_dma_mask) { 966 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); 967 if (ret) { 968 dev_warn(dev, "Failed to set dma mask %d\n", ret); 969 return ret; 970 } 971 } 972 973 epd = devm_drm_dev_alloc(dev, &repaper_driver, 974 struct repaper_epd, drm); 975 if (IS_ERR(epd)) 976 return PTR_ERR(epd); 977 978 drm = &epd->drm; 979 980 ret = drmm_mode_config_init(drm); 981 if (ret) 982 return ret; 983 drm->mode_config.funcs = &repaper_mode_config_funcs; 984 985 epd->spi = spi; 986 987 epd->panel_on = devm_gpiod_get(dev, "panel-on", GPIOD_OUT_LOW); 988 if (IS_ERR(epd->panel_on)) { 989 ret = PTR_ERR(epd->panel_on); 990 if (ret != -EPROBE_DEFER) 991 DRM_DEV_ERROR(dev, "Failed to get gpio 'panel-on'\n"); 992 return ret; 993 } 994 995 epd->discharge = devm_gpiod_get(dev, "discharge", GPIOD_OUT_LOW); 996 if (IS_ERR(epd->discharge)) { 997 ret = PTR_ERR(epd->discharge); 998 if (ret != -EPROBE_DEFER) 999 DRM_DEV_ERROR(dev, "Failed to get gpio 'discharge'\n"); 1000 return ret; 1001 } 1002 1003 epd->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 1004 if (IS_ERR(epd->reset)) { 1005 ret = PTR_ERR(epd->reset); 1006 if (ret != -EPROBE_DEFER) 1007 DRM_DEV_ERROR(dev, "Failed to get gpio 'reset'\n"); 1008 return ret; 1009 } 1010 1011 epd->busy = devm_gpiod_get(dev, "busy", GPIOD_IN); 1012 if (IS_ERR(epd->busy)) { 1013 ret = PTR_ERR(epd->busy); 1014 if (ret != -EPROBE_DEFER) 1015 DRM_DEV_ERROR(dev, "Failed to get gpio 'busy'\n"); 1016 return ret; 1017 } 1018 1019 if (!device_property_read_string(dev, "pervasive,thermal-zone", 1020 &thermal_zone)) { 1021 epd->thermal = thermal_zone_get_zone_by_name(thermal_zone); 1022 if (IS_ERR(epd->thermal)) { 1023 DRM_DEV_ERROR(dev, "Failed to get thermal zone: %s\n", thermal_zone); 1024 return PTR_ERR(epd->thermal); 1025 } 1026 } 1027 1028 switch (model) { 1029 case E1144CS021: 1030 mode = &repaper_e1144cs021_mode; 1031 epd->channel_select = repaper_e1144cs021_cs; 1032 epd->stage_time = 480; 1033 epd->bytes_per_scan = 96 / 4; 1034 epd->middle_scan = true; /* data-scan-data */ 1035 epd->pre_border_byte = false; 1036 epd->border_byte = REPAPER_BORDER_BYTE_ZERO; 1037 break; 1038 1039 case E1190CS021: 1040 mode = &repaper_e1190cs021_mode; 1041 epd->channel_select = repaper_e1190cs021_cs; 1042 epd->stage_time = 480; 1043 epd->bytes_per_scan = 128 / 4 / 2; 1044 epd->middle_scan = false; /* scan-data-scan */ 1045 epd->pre_border_byte = false; 1046 epd->border_byte = REPAPER_BORDER_BYTE_SET; 1047 break; 1048 1049 case E2200CS021: 1050 mode = &repaper_e2200cs021_mode; 1051 epd->channel_select = repaper_e2200cs021_cs; 1052 epd->stage_time = 480; 1053 epd->bytes_per_scan = 96 / 4; 1054 epd->middle_scan = true; /* data-scan-data */ 1055 epd->pre_border_byte = true; 1056 epd->border_byte = REPAPER_BORDER_BYTE_NONE; 1057 break; 1058 1059 case E2271CS021: 1060 epd->border = devm_gpiod_get(dev, "border", GPIOD_OUT_LOW); 1061 if (IS_ERR(epd->border)) { 1062 ret = PTR_ERR(epd->border); 1063 if (ret != -EPROBE_DEFER) 1064 DRM_DEV_ERROR(dev, "Failed to get gpio 'border'\n"); 1065 return ret; 1066 } 1067 1068 mode = &repaper_e2271cs021_mode; 1069 epd->channel_select = repaper_e2271cs021_cs; 1070 epd->stage_time = 630; 1071 epd->bytes_per_scan = 176 / 4; 1072 epd->middle_scan = true; /* data-scan-data */ 1073 epd->pre_border_byte = true; 1074 epd->border_byte = REPAPER_BORDER_BYTE_NONE; 1075 break; 1076 1077 default: 1078 return -ENODEV; 1079 } 1080 1081 epd->mode = mode; 1082 epd->width = mode->hdisplay; 1083 epd->height = mode->vdisplay; 1084 epd->factored_stage_time = epd->stage_time; 1085 1086 line_buffer_size = 2 * epd->width / 8 + epd->bytes_per_scan + 2; 1087 epd->line_buffer = devm_kzalloc(dev, line_buffer_size, GFP_KERNEL); 1088 if (!epd->line_buffer) 1089 return -ENOMEM; 1090 1091 epd->current_frame = devm_kzalloc(dev, epd->width * epd->height / 8, 1092 GFP_KERNEL); 1093 if (!epd->current_frame) 1094 return -ENOMEM; 1095 1096 drm->mode_config.min_width = mode->hdisplay; 1097 drm->mode_config.max_width = mode->hdisplay; 1098 drm->mode_config.min_height = mode->vdisplay; 1099 drm->mode_config.max_height = mode->vdisplay; 1100 1101 drm_connector_helper_add(&epd->connector, &repaper_connector_hfuncs); 1102 ret = drm_connector_init(drm, &epd->connector, &repaper_connector_funcs, 1103 DRM_MODE_CONNECTOR_SPI); 1104 if (ret) 1105 return ret; 1106 1107 ret = drm_simple_display_pipe_init(drm, &epd->pipe, &repaper_pipe_funcs, 1108 repaper_formats, ARRAY_SIZE(repaper_formats), 1109 NULL, &epd->connector); 1110 if (ret) 1111 return ret; 1112 1113 drm_mode_config_reset(drm); 1114 1115 ret = drm_dev_register(drm, 0); 1116 if (ret) 1117 return ret; 1118 1119 spi_set_drvdata(spi, drm); 1120 1121 DRM_DEBUG_DRIVER("SPI speed: %uMHz\n", spi->max_speed_hz / 1000000); 1122 1123 drm_client_setup(drm, NULL); 1124 1125 return 0; 1126 } 1127 1128 static void repaper_remove(struct spi_device *spi) 1129 { 1130 struct drm_device *drm = spi_get_drvdata(spi); 1131 1132 drm_dev_unplug(drm); 1133 drm_atomic_helper_shutdown(drm); 1134 } 1135 1136 static void repaper_shutdown(struct spi_device *spi) 1137 { 1138 drm_atomic_helper_shutdown(spi_get_drvdata(spi)); 1139 } 1140 1141 static struct spi_driver repaper_spi_driver = { 1142 .driver = { 1143 .name = "repaper", 1144 .of_match_table = repaper_of_match, 1145 }, 1146 .id_table = repaper_id, 1147 .probe = repaper_probe, 1148 .remove = repaper_remove, 1149 .shutdown = repaper_shutdown, 1150 }; 1151 module_spi_driver(repaper_spi_driver); 1152 1153 MODULE_DESCRIPTION("Pervasive Displays RePaper DRM driver"); 1154 MODULE_AUTHOR("Noralf Trønnes"); 1155 MODULE_LICENSE("GPL"); 1156