1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * DRM driver for Solomon SSD130x OLED displays 4 * 5 * Copyright 2022 Red Hat Inc. 6 * Author: Javier Martinez Canillas <javierm@redhat.com> 7 * 8 * Based on drivers/video/fbdev/ssd1307fb.c 9 * Copyright 2012 Free Electrons 10 */ 11 12 #include <linux/backlight.h> 13 #include <linux/bitfield.h> 14 #include <linux/bits.h> 15 #include <linux/delay.h> 16 #include <linux/gpio/consumer.h> 17 #include <linux/property.h> 18 #include <linux/pwm.h> 19 #include <linux/regulator/consumer.h> 20 21 #include <drm/drm_atomic.h> 22 #include <drm/drm_atomic_helper.h> 23 #include <drm/drm_crtc_helper.h> 24 #include <drm/drm_damage_helper.h> 25 #include <drm/drm_edid.h> 26 #include <drm/drm_fbdev_generic.h> 27 #include <drm/drm_format_helper.h> 28 #include <drm/drm_framebuffer.h> 29 #include <drm/drm_gem_atomic_helper.h> 30 #include <drm/drm_gem_framebuffer_helper.h> 31 #include <drm/drm_gem_shmem_helper.h> 32 #include <drm/drm_managed.h> 33 #include <drm/drm_modes.h> 34 #include <drm/drm_rect.h> 35 #include <drm/drm_probe_helper.h> 36 37 #include "ssd130x.h" 38 39 #define DRIVER_NAME "ssd130x" 40 #define DRIVER_DESC "DRM driver for Solomon SSD130x OLED displays" 41 #define DRIVER_DATE "20220131" 42 #define DRIVER_MAJOR 1 43 #define DRIVER_MINOR 0 44 45 #define SSD130X_PAGE_COL_START_LOW 0x00 46 #define SSD130X_PAGE_COL_START_HIGH 0x10 47 #define SSD130X_SET_ADDRESS_MODE 0x20 48 #define SSD130X_SET_COL_RANGE 0x21 49 #define SSD130X_SET_PAGE_RANGE 0x22 50 #define SSD130X_CONTRAST 0x81 51 #define SSD130X_SET_LOOKUP_TABLE 0x91 52 #define SSD130X_CHARGE_PUMP 0x8d 53 #define SSD130X_SET_SEG_REMAP 0xa0 54 #define SSD130X_DISPLAY_OFF 0xae 55 #define SSD130X_SET_MULTIPLEX_RATIO 0xa8 56 #define SSD130X_DISPLAY_ON 0xaf 57 #define SSD130X_START_PAGE_ADDRESS 0xb0 58 #define SSD130X_SET_COM_SCAN_DIR 0xc0 59 #define SSD130X_SET_DISPLAY_OFFSET 0xd3 60 #define SSD130X_SET_CLOCK_FREQ 0xd5 61 #define SSD130X_SET_AREA_COLOR_MODE 0xd8 62 #define SSD130X_SET_PRECHARGE_PERIOD 0xd9 63 #define SSD130X_SET_COM_PINS_CONFIG 0xda 64 #define SSD130X_SET_VCOMH 0xdb 65 66 #define SSD130X_PAGE_COL_START_MASK GENMASK(3, 0) 67 #define SSD130X_PAGE_COL_START_HIGH_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val) >> 4) 68 #define SSD130X_PAGE_COL_START_LOW_SET(val) FIELD_PREP(SSD130X_PAGE_COL_START_MASK, (val)) 69 #define SSD130X_START_PAGE_ADDRESS_MASK GENMASK(2, 0) 70 #define SSD130X_START_PAGE_ADDRESS_SET(val) FIELD_PREP(SSD130X_START_PAGE_ADDRESS_MASK, (val)) 71 #define SSD130X_SET_SEG_REMAP_MASK GENMASK(0, 0) 72 #define SSD130X_SET_SEG_REMAP_SET(val) FIELD_PREP(SSD130X_SET_SEG_REMAP_MASK, (val)) 73 #define SSD130X_SET_COM_SCAN_DIR_MASK GENMASK(3, 3) 74 #define SSD130X_SET_COM_SCAN_DIR_SET(val) FIELD_PREP(SSD130X_SET_COM_SCAN_DIR_MASK, (val)) 75 #define SSD130X_SET_CLOCK_DIV_MASK GENMASK(3, 0) 76 #define SSD130X_SET_CLOCK_DIV_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_DIV_MASK, (val)) 77 #define SSD130X_SET_CLOCK_FREQ_MASK GENMASK(7, 4) 78 #define SSD130X_SET_CLOCK_FREQ_SET(val) FIELD_PREP(SSD130X_SET_CLOCK_FREQ_MASK, (val)) 79 #define SSD130X_SET_PRECHARGE_PERIOD1_MASK GENMASK(3, 0) 80 #define SSD130X_SET_PRECHARGE_PERIOD1_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD1_MASK, (val)) 81 #define SSD130X_SET_PRECHARGE_PERIOD2_MASK GENMASK(7, 4) 82 #define SSD130X_SET_PRECHARGE_PERIOD2_SET(val) FIELD_PREP(SSD130X_SET_PRECHARGE_PERIOD2_MASK, (val)) 83 #define SSD130X_SET_COM_PINS_CONFIG1_MASK GENMASK(4, 4) 84 #define SSD130X_SET_COM_PINS_CONFIG1_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG1_MASK, (val)) 85 #define SSD130X_SET_COM_PINS_CONFIG2_MASK GENMASK(5, 5) 86 #define SSD130X_SET_COM_PINS_CONFIG2_SET(val) FIELD_PREP(SSD130X_SET_COM_PINS_CONFIG2_MASK, (val)) 87 88 #define SSD130X_SET_ADDRESS_MODE_HORIZONTAL 0x00 89 #define SSD130X_SET_ADDRESS_MODE_VERTICAL 0x01 90 #define SSD130X_SET_ADDRESS_MODE_PAGE 0x02 91 92 #define SSD130X_SET_AREA_COLOR_MODE_ENABLE 0x1e 93 #define SSD130X_SET_AREA_COLOR_MODE_LOW_POWER 0x05 94 95 #define MAX_CONTRAST 255 96 97 const struct ssd130x_deviceinfo ssd130x_variants[] = { 98 [SH1106_ID] = { 99 .default_vcomh = 0x40, 100 .default_dclk_div = 1, 101 .default_dclk_frq = 5, 102 .page_mode_only = 1, 103 }, 104 [SSD1305_ID] = { 105 .default_vcomh = 0x34, 106 .default_dclk_div = 1, 107 .default_dclk_frq = 7, 108 }, 109 [SSD1306_ID] = { 110 .default_vcomh = 0x20, 111 .default_dclk_div = 1, 112 .default_dclk_frq = 8, 113 .need_chargepump = 1, 114 }, 115 [SSD1307_ID] = { 116 .default_vcomh = 0x20, 117 .default_dclk_div = 2, 118 .default_dclk_frq = 12, 119 .need_pwm = 1, 120 }, 121 [SSD1309_ID] = { 122 .default_vcomh = 0x34, 123 .default_dclk_div = 1, 124 .default_dclk_frq = 10, 125 } 126 }; 127 EXPORT_SYMBOL_NS_GPL(ssd130x_variants, DRM_SSD130X); 128 129 static inline struct ssd130x_device *drm_to_ssd130x(struct drm_device *drm) 130 { 131 return container_of(drm, struct ssd130x_device, drm); 132 } 133 134 /* 135 * Helper to write data (SSD130X_DATA) to the device. 136 */ 137 static int ssd130x_write_data(struct ssd130x_device *ssd130x, u8 *values, int count) 138 { 139 return regmap_bulk_write(ssd130x->regmap, SSD130X_DATA, values, count); 140 } 141 142 /* 143 * Helper to write command (SSD130X_COMMAND). The fist variadic argument 144 * is the command to write and the following are the command options. 145 * 146 * Note that the ssd130x protocol requires each command and option to be 147 * written as a SSD130X_COMMAND device register value. That is why a call 148 * to regmap_write(..., SSD130X_COMMAND, ...) is done for each argument. 149 */ 150 static int ssd130x_write_cmd(struct ssd130x_device *ssd130x, int count, 151 /* u8 cmd, u8 option, ... */...) 152 { 153 va_list ap; 154 u8 value; 155 int ret; 156 157 va_start(ap, count); 158 159 do { 160 value = va_arg(ap, int); 161 ret = regmap_write(ssd130x->regmap, SSD130X_COMMAND, value); 162 if (ret) 163 goto out_end; 164 } while (--count); 165 166 out_end: 167 va_end(ap); 168 169 return ret; 170 } 171 172 /* Set address range for horizontal/vertical addressing modes */ 173 static int ssd130x_set_col_range(struct ssd130x_device *ssd130x, 174 u8 col_start, u8 cols) 175 { 176 u8 col_end = col_start + cols - 1; 177 int ret; 178 179 if (col_start == ssd130x->col_start && col_end == ssd130x->col_end) 180 return 0; 181 182 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_COL_RANGE, col_start, col_end); 183 if (ret < 0) 184 return ret; 185 186 ssd130x->col_start = col_start; 187 ssd130x->col_end = col_end; 188 return 0; 189 } 190 191 static int ssd130x_set_page_range(struct ssd130x_device *ssd130x, 192 u8 page_start, u8 pages) 193 { 194 u8 page_end = page_start + pages - 1; 195 int ret; 196 197 if (page_start == ssd130x->page_start && page_end == ssd130x->page_end) 198 return 0; 199 200 ret = ssd130x_write_cmd(ssd130x, 3, SSD130X_SET_PAGE_RANGE, page_start, page_end); 201 if (ret < 0) 202 return ret; 203 204 ssd130x->page_start = page_start; 205 ssd130x->page_end = page_end; 206 return 0; 207 } 208 209 /* Set page and column start address for page addressing mode */ 210 static int ssd130x_set_page_pos(struct ssd130x_device *ssd130x, 211 u8 page_start, u8 col_start) 212 { 213 int ret; 214 u32 page, col_low, col_high; 215 216 page = SSD130X_START_PAGE_ADDRESS | 217 SSD130X_START_PAGE_ADDRESS_SET(page_start); 218 col_low = SSD130X_PAGE_COL_START_LOW | 219 SSD130X_PAGE_COL_START_LOW_SET(col_start); 220 col_high = SSD130X_PAGE_COL_START_HIGH | 221 SSD130X_PAGE_COL_START_HIGH_SET(col_start); 222 ret = ssd130x_write_cmd(ssd130x, 3, page, col_low, col_high); 223 if (ret < 0) 224 return ret; 225 226 return 0; 227 } 228 229 static int ssd130x_pwm_enable(struct ssd130x_device *ssd130x) 230 { 231 struct device *dev = ssd130x->dev; 232 struct pwm_state pwmstate; 233 234 ssd130x->pwm = pwm_get(dev, NULL); 235 if (IS_ERR(ssd130x->pwm)) { 236 dev_err(dev, "Could not get PWM from firmware description!\n"); 237 return PTR_ERR(ssd130x->pwm); 238 } 239 240 pwm_init_state(ssd130x->pwm, &pwmstate); 241 pwm_set_relative_duty_cycle(&pwmstate, 50, 100); 242 pwm_apply_state(ssd130x->pwm, &pwmstate); 243 244 /* Enable the PWM */ 245 pwm_enable(ssd130x->pwm); 246 247 dev_dbg(dev, "Using PWM%d with a %lluns period.\n", 248 ssd130x->pwm->pwm, pwm_get_period(ssd130x->pwm)); 249 250 return 0; 251 } 252 253 static void ssd130x_reset(struct ssd130x_device *ssd130x) 254 { 255 if (!ssd130x->reset) 256 return; 257 258 /* Reset the screen */ 259 gpiod_set_value_cansleep(ssd130x->reset, 1); 260 udelay(4); 261 gpiod_set_value_cansleep(ssd130x->reset, 0); 262 udelay(4); 263 } 264 265 static int ssd130x_power_on(struct ssd130x_device *ssd130x) 266 { 267 struct device *dev = ssd130x->dev; 268 int ret; 269 270 ssd130x_reset(ssd130x); 271 272 ret = regulator_enable(ssd130x->vcc_reg); 273 if (ret) { 274 dev_err(dev, "Failed to enable VCC: %d\n", ret); 275 return ret; 276 } 277 278 if (ssd130x->device_info->need_pwm) { 279 ret = ssd130x_pwm_enable(ssd130x); 280 if (ret) { 281 dev_err(dev, "Failed to enable PWM: %d\n", ret); 282 regulator_disable(ssd130x->vcc_reg); 283 return ret; 284 } 285 } 286 287 return 0; 288 } 289 290 static void ssd130x_power_off(struct ssd130x_device *ssd130x) 291 { 292 pwm_disable(ssd130x->pwm); 293 pwm_put(ssd130x->pwm); 294 295 regulator_disable(ssd130x->vcc_reg); 296 } 297 298 static int ssd130x_init(struct ssd130x_device *ssd130x) 299 { 300 u32 precharge, dclk, com_invdir, compins, chargepump, seg_remap; 301 bool scan_mode; 302 int ret; 303 304 /* Set initial contrast */ 305 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_CONTRAST, ssd130x->contrast); 306 if (ret < 0) 307 return ret; 308 309 /* Set segment re-map */ 310 seg_remap = (SSD130X_SET_SEG_REMAP | 311 SSD130X_SET_SEG_REMAP_SET(ssd130x->seg_remap)); 312 ret = ssd130x_write_cmd(ssd130x, 1, seg_remap); 313 if (ret < 0) 314 return ret; 315 316 /* Set COM direction */ 317 com_invdir = (SSD130X_SET_COM_SCAN_DIR | 318 SSD130X_SET_COM_SCAN_DIR_SET(ssd130x->com_invdir)); 319 ret = ssd130x_write_cmd(ssd130x, 1, com_invdir); 320 if (ret < 0) 321 return ret; 322 323 /* Set multiplex ratio value */ 324 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_MULTIPLEX_RATIO, ssd130x->height - 1); 325 if (ret < 0) 326 return ret; 327 328 /* set display offset value */ 329 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_DISPLAY_OFFSET, ssd130x->com_offset); 330 if (ret < 0) 331 return ret; 332 333 /* Set clock frequency */ 334 dclk = (SSD130X_SET_CLOCK_DIV_SET(ssd130x->dclk_div - 1) | 335 SSD130X_SET_CLOCK_FREQ_SET(ssd130x->dclk_frq)); 336 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_CLOCK_FREQ, dclk); 337 if (ret < 0) 338 return ret; 339 340 /* Set Area Color Mode ON/OFF & Low Power Display Mode */ 341 if (ssd130x->area_color_enable || ssd130x->low_power) { 342 u32 mode = 0; 343 344 if (ssd130x->area_color_enable) 345 mode |= SSD130X_SET_AREA_COLOR_MODE_ENABLE; 346 347 if (ssd130x->low_power) 348 mode |= SSD130X_SET_AREA_COLOR_MODE_LOW_POWER; 349 350 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_AREA_COLOR_MODE, mode); 351 if (ret < 0) 352 return ret; 353 } 354 355 /* Set precharge period in number of ticks from the internal clock */ 356 precharge = (SSD130X_SET_PRECHARGE_PERIOD1_SET(ssd130x->prechargep1) | 357 SSD130X_SET_PRECHARGE_PERIOD2_SET(ssd130x->prechargep2)); 358 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_PRECHARGE_PERIOD, precharge); 359 if (ret < 0) 360 return ret; 361 362 /* Set COM pins configuration */ 363 compins = BIT(1); 364 /* 365 * The COM scan mode field values are the inverse of the boolean DT 366 * property "solomon,com-seq". The value 0b means scan from COM0 to 367 * COM[N - 1] while 1b means scan from COM[N - 1] to COM0. 368 */ 369 scan_mode = !ssd130x->com_seq; 370 compins |= (SSD130X_SET_COM_PINS_CONFIG1_SET(scan_mode) | 371 SSD130X_SET_COM_PINS_CONFIG2_SET(ssd130x->com_lrremap)); 372 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_COM_PINS_CONFIG, compins); 373 if (ret < 0) 374 return ret; 375 376 /* Set VCOMH */ 377 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_VCOMH, ssd130x->vcomh); 378 if (ret < 0) 379 return ret; 380 381 /* Turn on the DC-DC Charge Pump */ 382 chargepump = BIT(4); 383 384 if (ssd130x->device_info->need_chargepump) 385 chargepump |= BIT(2); 386 387 ret = ssd130x_write_cmd(ssd130x, 2, SSD130X_CHARGE_PUMP, chargepump); 388 if (ret < 0) 389 return ret; 390 391 /* Set lookup table */ 392 if (ssd130x->lookup_table_set) { 393 int i; 394 395 ret = ssd130x_write_cmd(ssd130x, 1, SSD130X_SET_LOOKUP_TABLE); 396 if (ret < 0) 397 return ret; 398 399 for (i = 0; i < ARRAY_SIZE(ssd130x->lookup_table); i++) { 400 u8 val = ssd130x->lookup_table[i]; 401 402 if (val < 31 || val > 63) 403 dev_warn(ssd130x->dev, 404 "lookup table index %d value out of range 31 <= %d <= 63\n", 405 i, val); 406 ret = ssd130x_write_cmd(ssd130x, 1, val); 407 if (ret < 0) 408 return ret; 409 } 410 } 411 412 /* Switch to page addressing mode */ 413 if (ssd130x->page_address_mode) 414 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE, 415 SSD130X_SET_ADDRESS_MODE_PAGE); 416 417 /* Switch to horizontal addressing mode */ 418 return ssd130x_write_cmd(ssd130x, 2, SSD130X_SET_ADDRESS_MODE, 419 SSD130X_SET_ADDRESS_MODE_HORIZONTAL); 420 } 421 422 static int ssd130x_update_rect(struct ssd130x_device *ssd130x, u8 *buf, 423 struct drm_rect *rect) 424 { 425 unsigned int x = rect->x1; 426 unsigned int y = rect->y1; 427 unsigned int width = drm_rect_width(rect); 428 unsigned int height = drm_rect_height(rect); 429 unsigned int line_length = DIV_ROUND_UP(width, 8); 430 unsigned int pages = DIV_ROUND_UP(height, 8); 431 struct drm_device *drm = &ssd130x->drm; 432 u32 array_idx = 0; 433 int ret, i, j, k; 434 u8 *data_array = NULL; 435 436 drm_WARN_ONCE(drm, y % 8 != 0, "y must be aligned to screen page\n"); 437 438 data_array = kcalloc(width, pages, GFP_KERNEL); 439 if (!data_array) 440 return -ENOMEM; 441 442 /* 443 * The screen is divided in pages, each having a height of 8 444 * pixels, and the width of the screen. When sending a byte of 445 * data to the controller, it gives the 8 bits for the current 446 * column. I.e, the first byte are the 8 bits of the first 447 * column, then the 8 bits for the second column, etc. 448 * 449 * 450 * Representation of the screen, assuming it is 5 bits 451 * wide. Each letter-number combination is a bit that controls 452 * one pixel. 453 * 454 * A0 A1 A2 A3 A4 455 * B0 B1 B2 B3 B4 456 * C0 C1 C2 C3 C4 457 * D0 D1 D2 D3 D4 458 * E0 E1 E2 E3 E4 459 * F0 F1 F2 F3 F4 460 * G0 G1 G2 G3 G4 461 * H0 H1 H2 H3 H4 462 * 463 * If you want to update this screen, you need to send 5 bytes: 464 * (1) A0 B0 C0 D0 E0 F0 G0 H0 465 * (2) A1 B1 C1 D1 E1 F1 G1 H1 466 * (3) A2 B2 C2 D2 E2 F2 G2 H2 467 * (4) A3 B3 C3 D3 E3 F3 G3 H3 468 * (5) A4 B4 C4 D4 E4 F4 G4 H4 469 */ 470 471 if (!ssd130x->page_address_mode) { 472 /* Set address range for horizontal addressing mode */ 473 ret = ssd130x_set_col_range(ssd130x, ssd130x->col_offset + x, width); 474 if (ret < 0) 475 goto out_free; 476 477 ret = ssd130x_set_page_range(ssd130x, ssd130x->page_offset + y / 8, pages); 478 if (ret < 0) 479 goto out_free; 480 } 481 482 for (i = 0; i < pages; i++) { 483 int m = 8; 484 485 /* Last page may be partial */ 486 if (8 * (y / 8 + i + 1) > ssd130x->height) 487 m = ssd130x->height % 8; 488 for (j = 0; j < width; j++) { 489 u8 data = 0; 490 491 for (k = 0; k < m; k++) { 492 u8 byte = buf[(8 * i + k) * line_length + j / 8]; 493 u8 bit = (byte >> (j % 8)) & 1; 494 495 data |= bit << k; 496 } 497 data_array[array_idx++] = data; 498 } 499 500 /* 501 * In page addressing mode, the start address needs to be reset, 502 * and each page then needs to be written out separately. 503 */ 504 if (ssd130x->page_address_mode) { 505 ret = ssd130x_set_page_pos(ssd130x, 506 ssd130x->page_offset + i, 507 ssd130x->col_offset + x); 508 if (ret < 0) 509 goto out_free; 510 511 ret = ssd130x_write_data(ssd130x, data_array, width); 512 if (ret < 0) 513 goto out_free; 514 515 array_idx = 0; 516 } 517 } 518 519 /* Write out update in one go if we aren't using page addressing mode */ 520 if (!ssd130x->page_address_mode) 521 ret = ssd130x_write_data(ssd130x, data_array, width * pages); 522 523 out_free: 524 kfree(data_array); 525 return ret; 526 } 527 528 static void ssd130x_clear_screen(struct ssd130x_device *ssd130x) 529 { 530 u8 *buf = NULL; 531 struct drm_rect fullscreen = { 532 .x1 = 0, 533 .x2 = ssd130x->width, 534 .y1 = 0, 535 .y2 = ssd130x->height, 536 }; 537 538 buf = kcalloc(DIV_ROUND_UP(ssd130x->width, 8), ssd130x->height, 539 GFP_KERNEL); 540 if (!buf) 541 return; 542 543 ssd130x_update_rect(ssd130x, buf, &fullscreen); 544 545 kfree(buf); 546 } 547 548 static int ssd130x_fb_blit_rect(struct drm_framebuffer *fb, const struct iosys_map *vmap, 549 struct drm_rect *rect) 550 { 551 struct ssd130x_device *ssd130x = drm_to_ssd130x(fb->dev); 552 struct iosys_map dst; 553 unsigned int dst_pitch; 554 int ret = 0; 555 u8 *buf = NULL; 556 557 /* Align y to display page boundaries */ 558 rect->y1 = round_down(rect->y1, 8); 559 rect->y2 = min_t(unsigned int, round_up(rect->y2, 8), ssd130x->height); 560 561 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8); 562 buf = kcalloc(dst_pitch, drm_rect_height(rect), GFP_KERNEL); 563 if (!buf) 564 return -ENOMEM; 565 566 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 567 if (ret) 568 goto out_free; 569 570 iosys_map_set_vaddr(&dst, buf); 571 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect); 572 573 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 574 575 ssd130x_update_rect(ssd130x, buf, rect); 576 577 out_free: 578 kfree(buf); 579 580 return ret; 581 } 582 583 static void ssd130x_primary_plane_helper_atomic_update(struct drm_plane *plane, 584 struct drm_atomic_state *state) 585 { 586 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 587 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 588 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 589 struct drm_atomic_helper_damage_iter iter; 590 struct drm_device *drm = plane->dev; 591 struct drm_rect dst_clip; 592 struct drm_rect damage; 593 int idx; 594 595 if (!drm_dev_enter(drm, &idx)) 596 return; 597 598 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 599 drm_atomic_for_each_plane_damage(&iter, &damage) { 600 dst_clip = plane_state->dst; 601 602 if (!drm_rect_intersect(&dst_clip, &damage)) 603 continue; 604 605 ssd130x_fb_blit_rect(plane_state->fb, &shadow_plane_state->data[0], &dst_clip); 606 } 607 608 drm_dev_exit(idx); 609 } 610 611 static void ssd130x_primary_plane_helper_atomic_disable(struct drm_plane *plane, 612 struct drm_atomic_state *state) 613 { 614 struct drm_device *drm = plane->dev; 615 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm); 616 int idx; 617 618 if (!drm_dev_enter(drm, &idx)) 619 return; 620 621 ssd130x_clear_screen(ssd130x); 622 623 drm_dev_exit(idx); 624 } 625 626 static const struct drm_plane_helper_funcs ssd130x_primary_plane_helper_funcs = { 627 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 628 .atomic_check = drm_plane_helper_atomic_check, 629 .atomic_update = ssd130x_primary_plane_helper_atomic_update, 630 .atomic_disable = ssd130x_primary_plane_helper_atomic_disable, 631 }; 632 633 static const struct drm_plane_funcs ssd130x_primary_plane_funcs = { 634 .update_plane = drm_atomic_helper_update_plane, 635 .disable_plane = drm_atomic_helper_disable_plane, 636 .destroy = drm_plane_cleanup, 637 DRM_GEM_SHADOW_PLANE_FUNCS, 638 }; 639 640 static enum drm_mode_status ssd130x_crtc_helper_mode_valid(struct drm_crtc *crtc, 641 const struct drm_display_mode *mode) 642 { 643 struct ssd130x_device *ssd130x = drm_to_ssd130x(crtc->dev); 644 645 if (mode->hdisplay != ssd130x->mode.hdisplay && 646 mode->vdisplay != ssd130x->mode.vdisplay) 647 return MODE_ONE_SIZE; 648 else if (mode->hdisplay != ssd130x->mode.hdisplay) 649 return MODE_ONE_WIDTH; 650 else if (mode->vdisplay != ssd130x->mode.vdisplay) 651 return MODE_ONE_HEIGHT; 652 653 return MODE_OK; 654 } 655 656 /* 657 * The CRTC is always enabled. Screen updates are performed by 658 * the primary plane's atomic_update function. Disabling clears 659 * the screen in the primary plane's atomic_disable function. 660 */ 661 static const struct drm_crtc_helper_funcs ssd130x_crtc_helper_funcs = { 662 .mode_valid = ssd130x_crtc_helper_mode_valid, 663 .atomic_check = drm_crtc_helper_atomic_check, 664 }; 665 666 static void ssd130x_crtc_reset(struct drm_crtc *crtc) 667 { 668 struct drm_device *drm = crtc->dev; 669 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm); 670 671 ssd130x_init(ssd130x); 672 673 drm_atomic_helper_crtc_reset(crtc); 674 } 675 676 static const struct drm_crtc_funcs ssd130x_crtc_funcs = { 677 .reset = ssd130x_crtc_reset, 678 .destroy = drm_crtc_cleanup, 679 .set_config = drm_atomic_helper_set_config, 680 .page_flip = drm_atomic_helper_page_flip, 681 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 682 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 683 }; 684 685 static void ssd130x_encoder_helper_atomic_enable(struct drm_encoder *encoder, 686 struct drm_atomic_state *state) 687 { 688 struct drm_device *drm = encoder->dev; 689 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm); 690 int ret; 691 692 ret = ssd130x_power_on(ssd130x); 693 if (ret) 694 return; 695 696 ssd130x_write_cmd(ssd130x, 1, SSD130X_DISPLAY_ON); 697 698 backlight_enable(ssd130x->bl_dev); 699 } 700 701 static void ssd130x_encoder_helper_atomic_disable(struct drm_encoder *encoder, 702 struct drm_atomic_state *state) 703 { 704 struct drm_device *drm = encoder->dev; 705 struct ssd130x_device *ssd130x = drm_to_ssd130x(drm); 706 707 backlight_disable(ssd130x->bl_dev); 708 709 ssd130x_write_cmd(ssd130x, 1, SSD130X_DISPLAY_OFF); 710 711 ssd130x_power_off(ssd130x); 712 } 713 714 static const struct drm_encoder_helper_funcs ssd130x_encoder_helper_funcs = { 715 .atomic_enable = ssd130x_encoder_helper_atomic_enable, 716 .atomic_disable = ssd130x_encoder_helper_atomic_disable, 717 }; 718 719 static const struct drm_encoder_funcs ssd130x_encoder_funcs = { 720 .destroy = drm_encoder_cleanup, 721 }; 722 723 static int ssd130x_connector_helper_get_modes(struct drm_connector *connector) 724 { 725 struct ssd130x_device *ssd130x = drm_to_ssd130x(connector->dev); 726 struct drm_display_mode *mode; 727 struct device *dev = ssd130x->dev; 728 729 mode = drm_mode_duplicate(connector->dev, &ssd130x->mode); 730 if (!mode) { 731 dev_err(dev, "Failed to duplicated mode\n"); 732 return 0; 733 } 734 735 drm_mode_probed_add(connector, mode); 736 drm_set_preferred_mode(connector, mode->hdisplay, mode->vdisplay); 737 738 /* There is only a single mode */ 739 return 1; 740 } 741 742 static const struct drm_connector_helper_funcs ssd130x_connector_helper_funcs = { 743 .get_modes = ssd130x_connector_helper_get_modes, 744 }; 745 746 static const struct drm_connector_funcs ssd130x_connector_funcs = { 747 .reset = drm_atomic_helper_connector_reset, 748 .fill_modes = drm_helper_probe_single_connector_modes, 749 .destroy = drm_connector_cleanup, 750 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 751 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 752 }; 753 754 static const struct drm_mode_config_funcs ssd130x_mode_config_funcs = { 755 .fb_create = drm_gem_fb_create_with_dirty, 756 .atomic_check = drm_atomic_helper_check, 757 .atomic_commit = drm_atomic_helper_commit, 758 }; 759 760 static const uint32_t ssd130x_formats[] = { 761 DRM_FORMAT_XRGB8888, 762 }; 763 764 DEFINE_DRM_GEM_FOPS(ssd130x_fops); 765 766 static const struct drm_driver ssd130x_drm_driver = { 767 DRM_GEM_SHMEM_DRIVER_OPS, 768 .name = DRIVER_NAME, 769 .desc = DRIVER_DESC, 770 .date = DRIVER_DATE, 771 .major = DRIVER_MAJOR, 772 .minor = DRIVER_MINOR, 773 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET, 774 .fops = &ssd130x_fops, 775 }; 776 777 static int ssd130x_update_bl(struct backlight_device *bdev) 778 { 779 struct ssd130x_device *ssd130x = bl_get_data(bdev); 780 int brightness = backlight_get_brightness(bdev); 781 int ret; 782 783 ssd130x->contrast = brightness; 784 785 ret = ssd130x_write_cmd(ssd130x, 1, SSD130X_CONTRAST); 786 if (ret < 0) 787 return ret; 788 789 ret = ssd130x_write_cmd(ssd130x, 1, ssd130x->contrast); 790 if (ret < 0) 791 return ret; 792 793 return 0; 794 } 795 796 static const struct backlight_ops ssd130xfb_bl_ops = { 797 .update_status = ssd130x_update_bl, 798 }; 799 800 static void ssd130x_parse_properties(struct ssd130x_device *ssd130x) 801 { 802 struct device *dev = ssd130x->dev; 803 804 if (device_property_read_u32(dev, "solomon,width", &ssd130x->width)) 805 ssd130x->width = 96; 806 807 if (device_property_read_u32(dev, "solomon,height", &ssd130x->height)) 808 ssd130x->height = 16; 809 810 if (device_property_read_u32(dev, "solomon,page-offset", &ssd130x->page_offset)) 811 ssd130x->page_offset = 1; 812 813 if (device_property_read_u32(dev, "solomon,col-offset", &ssd130x->col_offset)) 814 ssd130x->col_offset = 0; 815 816 if (device_property_read_u32(dev, "solomon,com-offset", &ssd130x->com_offset)) 817 ssd130x->com_offset = 0; 818 819 if (device_property_read_u32(dev, "solomon,prechargep1", &ssd130x->prechargep1)) 820 ssd130x->prechargep1 = 2; 821 822 if (device_property_read_u32(dev, "solomon,prechargep2", &ssd130x->prechargep2)) 823 ssd130x->prechargep2 = 2; 824 825 if (!device_property_read_u8_array(dev, "solomon,lookup-table", 826 ssd130x->lookup_table, 827 ARRAY_SIZE(ssd130x->lookup_table))) 828 ssd130x->lookup_table_set = 1; 829 830 ssd130x->seg_remap = !device_property_read_bool(dev, "solomon,segment-no-remap"); 831 ssd130x->com_seq = device_property_read_bool(dev, "solomon,com-seq"); 832 ssd130x->com_lrremap = device_property_read_bool(dev, "solomon,com-lrremap"); 833 ssd130x->com_invdir = device_property_read_bool(dev, "solomon,com-invdir"); 834 ssd130x->area_color_enable = 835 device_property_read_bool(dev, "solomon,area-color-enable"); 836 ssd130x->low_power = device_property_read_bool(dev, "solomon,low-power"); 837 838 ssd130x->contrast = 127; 839 ssd130x->vcomh = ssd130x->device_info->default_vcomh; 840 841 /* Setup display timing */ 842 if (device_property_read_u32(dev, "solomon,dclk-div", &ssd130x->dclk_div)) 843 ssd130x->dclk_div = ssd130x->device_info->default_dclk_div; 844 if (device_property_read_u32(dev, "solomon,dclk-frq", &ssd130x->dclk_frq)) 845 ssd130x->dclk_frq = ssd130x->device_info->default_dclk_frq; 846 } 847 848 static int ssd130x_init_modeset(struct ssd130x_device *ssd130x) 849 { 850 struct drm_display_mode *mode = &ssd130x->mode; 851 struct device *dev = ssd130x->dev; 852 struct drm_device *drm = &ssd130x->drm; 853 unsigned long max_width, max_height; 854 struct drm_plane *primary_plane; 855 struct drm_crtc *crtc; 856 struct drm_encoder *encoder; 857 struct drm_connector *connector; 858 int ret; 859 860 /* 861 * Modesetting 862 */ 863 864 ret = drmm_mode_config_init(drm); 865 if (ret) { 866 dev_err(dev, "DRM mode config init failed: %d\n", ret); 867 return ret; 868 } 869 870 mode->type = DRM_MODE_TYPE_DRIVER; 871 mode->clock = 1; 872 mode->hdisplay = mode->htotal = ssd130x->width; 873 mode->hsync_start = mode->hsync_end = ssd130x->width; 874 mode->vdisplay = mode->vtotal = ssd130x->height; 875 mode->vsync_start = mode->vsync_end = ssd130x->height; 876 mode->width_mm = 27; 877 mode->height_mm = 27; 878 879 max_width = max_t(unsigned long, mode->hdisplay, DRM_SHADOW_PLANE_MAX_WIDTH); 880 max_height = max_t(unsigned long, mode->vdisplay, DRM_SHADOW_PLANE_MAX_HEIGHT); 881 882 drm->mode_config.min_width = mode->hdisplay; 883 drm->mode_config.max_width = max_width; 884 drm->mode_config.min_height = mode->vdisplay; 885 drm->mode_config.max_height = max_height; 886 drm->mode_config.preferred_depth = 24; 887 drm->mode_config.funcs = &ssd130x_mode_config_funcs; 888 889 /* Primary plane */ 890 891 primary_plane = &ssd130x->primary_plane; 892 ret = drm_universal_plane_init(drm, primary_plane, 0, &ssd130x_primary_plane_funcs, 893 ssd130x_formats, ARRAY_SIZE(ssd130x_formats), 894 NULL, DRM_PLANE_TYPE_PRIMARY, NULL); 895 if (ret) { 896 dev_err(dev, "DRM primary plane init failed: %d\n", ret); 897 return ret; 898 } 899 900 drm_plane_helper_add(primary_plane, &ssd130x_primary_plane_helper_funcs); 901 902 drm_plane_enable_fb_damage_clips(primary_plane); 903 904 /* CRTC */ 905 906 crtc = &ssd130x->crtc; 907 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, 908 &ssd130x_crtc_funcs, NULL); 909 if (ret) { 910 dev_err(dev, "DRM crtc init failed: %d\n", ret); 911 return ret; 912 } 913 914 drm_crtc_helper_add(crtc, &ssd130x_crtc_helper_funcs); 915 916 /* Encoder */ 917 918 encoder = &ssd130x->encoder; 919 ret = drm_encoder_init(drm, encoder, &ssd130x_encoder_funcs, 920 DRM_MODE_ENCODER_NONE, NULL); 921 if (ret) { 922 dev_err(dev, "DRM encoder init failed: %d\n", ret); 923 return ret; 924 } 925 926 drm_encoder_helper_add(encoder, &ssd130x_encoder_helper_funcs); 927 928 encoder->possible_crtcs = drm_crtc_mask(crtc); 929 930 /* Connector */ 931 932 connector = &ssd130x->connector; 933 ret = drm_connector_init(drm, connector, &ssd130x_connector_funcs, 934 DRM_MODE_CONNECTOR_Unknown); 935 if (ret) { 936 dev_err(dev, "DRM connector init failed: %d\n", ret); 937 return ret; 938 } 939 940 drm_connector_helper_add(connector, &ssd130x_connector_helper_funcs); 941 942 ret = drm_connector_attach_encoder(connector, encoder); 943 if (ret) { 944 dev_err(dev, "DRM attach connector to encoder failed: %d\n", ret); 945 return ret; 946 } 947 948 drm_mode_config_reset(drm); 949 950 return 0; 951 } 952 953 static int ssd130x_get_resources(struct ssd130x_device *ssd130x) 954 { 955 struct device *dev = ssd130x->dev; 956 957 ssd130x->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 958 if (IS_ERR(ssd130x->reset)) 959 return dev_err_probe(dev, PTR_ERR(ssd130x->reset), 960 "Failed to get reset gpio\n"); 961 962 ssd130x->vcc_reg = devm_regulator_get(dev, "vcc"); 963 if (IS_ERR(ssd130x->vcc_reg)) 964 return dev_err_probe(dev, PTR_ERR(ssd130x->vcc_reg), 965 "Failed to get VCC regulator\n"); 966 967 return 0; 968 } 969 970 struct ssd130x_device *ssd130x_probe(struct device *dev, struct regmap *regmap) 971 { 972 struct ssd130x_device *ssd130x; 973 struct backlight_device *bl; 974 struct drm_device *drm; 975 int ret; 976 977 ssd130x = devm_drm_dev_alloc(dev, &ssd130x_drm_driver, 978 struct ssd130x_device, drm); 979 if (IS_ERR(ssd130x)) 980 return ERR_PTR(dev_err_probe(dev, PTR_ERR(ssd130x), 981 "Failed to allocate DRM device\n")); 982 983 drm = &ssd130x->drm; 984 985 ssd130x->dev = dev; 986 ssd130x->regmap = regmap; 987 ssd130x->device_info = device_get_match_data(dev); 988 989 if (ssd130x->device_info->page_mode_only) 990 ssd130x->page_address_mode = 1; 991 992 ssd130x_parse_properties(ssd130x); 993 994 ret = ssd130x_get_resources(ssd130x); 995 if (ret) 996 return ERR_PTR(ret); 997 998 bl = devm_backlight_device_register(dev, dev_name(dev), dev, ssd130x, 999 &ssd130xfb_bl_ops, NULL); 1000 if (IS_ERR(bl)) 1001 return ERR_PTR(dev_err_probe(dev, PTR_ERR(bl), 1002 "Unable to register backlight device\n")); 1003 1004 bl->props.brightness = ssd130x->contrast; 1005 bl->props.max_brightness = MAX_CONTRAST; 1006 ssd130x->bl_dev = bl; 1007 1008 ret = ssd130x_init_modeset(ssd130x); 1009 if (ret) 1010 return ERR_PTR(ret); 1011 1012 ret = drm_dev_register(drm, 0); 1013 if (ret) 1014 return ERR_PTR(dev_err_probe(dev, ret, "DRM device register failed\n")); 1015 1016 drm_fbdev_generic_setup(drm, 32); 1017 1018 return ssd130x; 1019 } 1020 EXPORT_SYMBOL_GPL(ssd130x_probe); 1021 1022 void ssd130x_remove(struct ssd130x_device *ssd130x) 1023 { 1024 drm_dev_unplug(&ssd130x->drm); 1025 } 1026 EXPORT_SYMBOL_GPL(ssd130x_remove); 1027 1028 void ssd130x_shutdown(struct ssd130x_device *ssd130x) 1029 { 1030 drm_atomic_helper_shutdown(&ssd130x->drm); 1031 } 1032 EXPORT_SYMBOL_GPL(ssd130x_shutdown); 1033 1034 MODULE_DESCRIPTION(DRIVER_DESC); 1035 MODULE_AUTHOR("Javier Martinez Canillas <javierm@redhat.com>"); 1036 MODULE_LICENSE("GPL v2"); 1037