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