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