1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Driver for Sitronix ST7571, a 4 level gray scale dot matrix LCD controller 4 * 5 * Copyright (C) 2025 Marcus Folkesson <marcus.folkesson@gmail.com> 6 */ 7 8 #include <linux/bitfield.h> 9 #include <linux/delay.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/i2c.h> 12 #include <linux/module.h> 13 #include <linux/regmap.h> 14 15 #include <drm/clients/drm_client_setup.h> 16 #include <drm/drm_atomic.h> 17 #include <drm/drm_atomic_helper.h> 18 #include <drm/drm_connector.h> 19 #include <drm/drm_crtc_helper.h> 20 #include <drm/drm_damage_helper.h> 21 #include <drm/drm_drv.h> 22 #include <drm/drm_encoder.h> 23 #include <drm/drm_fbdev_shmem.h> 24 #include <drm/drm_fourcc.h> 25 #include <drm/drm_framebuffer.h> 26 #include <drm/drm_gem_atomic_helper.h> 27 #include <drm/drm_gem_framebuffer_helper.h> 28 #include <drm/drm_gem_shmem_helper.h> 29 #include <drm/drm_modeset_helper_vtables.h> 30 #include <drm/drm_module.h> 31 #include <drm/drm_plane.h> 32 #include <drm/drm_probe_helper.h> 33 34 #include <video/display_timing.h> 35 #include <video/of_display_timing.h> 36 37 #include "st7571.h" 38 39 #define ST7571_COMMAND_MODE (0x00) 40 #define ST7571_DATA_MODE (0x40) 41 42 /* Normal mode command set */ 43 #define ST7571_DISPLAY_OFF (0xae) 44 #define ST7571_DISPLAY_ON (0xaf) 45 #define ST7571_OSC_ON (0xab) 46 #define ST7571_SET_COLUMN_LSB(c) (0x00 | FIELD_PREP(GENMASK(3, 0), (c))) 47 #define ST7571_SET_COLUMN_MSB(c) (0x10 | FIELD_PREP(GENMASK(2, 0), (c) >> 4)) 48 #define ST7571_SET_COM0_LSB(x) (FIELD_PREP(GENMASK(6, 0), (x))) 49 #define ST7571_SET_COM0_MSB (0x44) 50 #define ST7571_SET_COM_SCAN_DIR(d) (0xc0 | FIELD_PREP(GENMASK(3, 3), (d))) 51 #define ST7571_SET_CONTRAST_LSB(c) (FIELD_PREP(GENMASK(5, 0), (c))) 52 #define ST7571_SET_CONTRAST_MSB (0x81) 53 #define ST7571_SET_DISPLAY_DUTY_LSB(d) (FIELD_PREP(GENMASK(7, 0), (d))) 54 #define ST7571_SET_DISPLAY_DUTY_MSB (0x48) 55 #define ST7571_SET_ENTIRE_DISPLAY_ON(p) (0xa4 | FIELD_PREP(GENMASK(0, 0), (p))) 56 #define ST7571_SET_LCD_BIAS(b) (0x50 | FIELD_PREP(GENMASK(2, 0), (b))) 57 #define ST7571_SET_MODE_LSB(m) (FIELD_PREP(GENMASK(7, 2), (m))) 58 #define ST7571_SET_MODE_MSB (0x38) 59 #define ST7571_SET_PAGE(p) (0xb0 | FIELD_PREP(GENMASK(3, 0), (p))) 60 #define ST7571_SET_POWER(p) (0x28 | FIELD_PREP(GENMASK(2, 0), (p))) 61 #define ST7571_SET_REGULATOR_REG(r) (0x20 | FIELD_PREP(GENMASK(2, 0), (r))) 62 #define ST7571_SET_REVERSE(r) (0xa6 | FIELD_PREP(GENMASK(0, 0), (r))) 63 #define ST7571_SET_SEG_SCAN_DIR(d) (0xa0 | FIELD_PREP(GENMASK(0, 0), (d))) 64 #define ST7571_SET_START_LINE_LSB(l) (FIELD_PREP(GENMASK(6, 0), (l))) 65 #define ST7571_SET_START_LINE_MSB (0x40) 66 67 /* Extension command set 3 */ 68 #define ST7571_COMMAND_SET_3 (0x7b) 69 #define ST7571_SET_COLOR_MODE(c) (0x10 | FIELD_PREP(GENMASK(0, 0), (c))) 70 #define ST7571_COMMAND_SET_NORMAL (0x00) 71 72 /* ST7567 commands */ 73 #define ST7567_SET_LCD_BIAS(m) (0xa2 | FIELD_PREP(GENMASK(0, 0), (m))) 74 75 #define ST7571_PAGE_HEIGHT 8 76 77 #define DRIVER_NAME "st7571" 78 #define DRIVER_DESC "ST7571 DRM driver" 79 #define DRIVER_MAJOR 1 80 #define DRIVER_MINOR 0 81 82 static inline struct st7571_device *drm_to_st7571(struct drm_device *drm) 83 { 84 return container_of(drm, struct st7571_device, drm); 85 } 86 87 static int st7571_send_command_list(struct st7571_device *st7571, 88 const u8 *cmd_list, size_t len) 89 { 90 int ret; 91 92 for (int i = 0; i < len; i++) { 93 ret = regmap_write(st7571->regmap, ST7571_COMMAND_MODE, cmd_list[i]); 94 if (ret < 0) 95 return ret; 96 } 97 98 return ret; 99 } 100 101 static inline u8 st7571_transform_xy(const char *p, int x, int y, u8 bpp) 102 { 103 int xrest = x % 8; 104 u8 result = 0; 105 u8 row_len = 16 * bpp; 106 107 /* 108 * Transforms an (x, y) pixel coordinate into a vertical 8-bit 109 * column from the framebuffer. It calculates the corresponding byte in the 110 * framebuffer, extracts the bit at the given x position across 8 consecutive 111 * rows, and packs those bits into a single byte. 112 * 113 * Return an 8-bit value representing a vertical column of pixels. 114 */ 115 x = x / 8; 116 y = (y / 8) * 8; 117 118 for (int i = 0; i < 8; i++) { 119 int row_idx = y + i; 120 u8 byte = p[row_idx * row_len + x]; 121 u8 bit = (byte >> xrest) & 1; 122 123 result |= (bit << i); 124 } 125 126 return result; 127 } 128 129 static int st7571_set_position(struct st7571_device *st7571, int x, int y) 130 { 131 u8 cmd_list[] = { 132 ST7571_SET_COLUMN_LSB(x), 133 ST7571_SET_COLUMN_MSB(x), 134 ST7571_SET_PAGE(y / ST7571_PAGE_HEIGHT), 135 }; 136 137 return st7571_send_command_list(st7571, cmd_list, ARRAY_SIZE(cmd_list)); 138 } 139 140 static int st7571_fb_clear_screen(struct st7571_device *st7571) 141 { 142 u32 npixels = st7571->ncols * round_up(st7571->nlines, ST7571_PAGE_HEIGHT) * st7571->bpp; 143 char pixelvalue = 0x00; 144 145 st7571_set_position(st7571, 0, 0); 146 for (int i = 0; i < npixels; i++) 147 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, &pixelvalue, 1); 148 149 return 0; 150 } 151 152 static void st7571_prepare_buffer_monochrome(struct st7571_device *st7571, 153 const struct iosys_map *vmap, 154 struct drm_framebuffer *fb, 155 struct drm_rect *rect, 156 struct drm_format_conv_state *fmtcnv_state) 157 { 158 unsigned int dst_pitch; 159 struct iosys_map dst; 160 u32 size; 161 162 switch (fb->format->format) { 163 case DRM_FORMAT_XRGB8888: 164 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8); 165 iosys_map_set_vaddr(&dst, st7571->hwbuf); 166 167 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 168 break; 169 170 case DRM_FORMAT_R1: 171 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 172 memcpy(st7571->hwbuf, vmap->vaddr, size); 173 break; 174 } 175 } 176 177 static void st7571_prepare_buffer_grayscale(struct st7571_device *st7571, 178 const struct iosys_map *vmap, 179 struct drm_framebuffer *fb, 180 struct drm_rect *rect, 181 struct drm_format_conv_state *fmtcnv_state) 182 { 183 u32 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 184 unsigned int dst_pitch; 185 struct iosys_map dst; 186 187 switch (fb->format->format) { 188 case DRM_FORMAT_XRGB8888: 189 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 4); 190 iosys_map_set_vaddr(&dst, st7571->hwbuf); 191 192 drm_fb_xrgb8888_to_gray2(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 193 break; 194 195 case DRM_FORMAT_R1: 196 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 197 memcpy(st7571->hwbuf, vmap->vaddr, size); 198 break; 199 200 case DRM_FORMAT_R2: 201 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 4; 202 memcpy(st7571->hwbuf, vmap->vaddr, size); 203 break; 204 } 205 } 206 207 static int st7571_fb_update_rect_monochrome(struct drm_framebuffer *fb, struct drm_rect *rect) 208 { 209 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 210 char *row = st7571->row; 211 212 /* Align y to display page boundaries */ 213 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 214 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 215 216 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 217 for (int x = rect->x1; x < rect->x2; x++) 218 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 1); 219 220 st7571_set_position(st7571, rect->x1, y); 221 222 /* TODO: Investige why we can't write multiple bytes at once */ 223 for (int x = rect->x1; x < rect->x2; x++) 224 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 225 } 226 227 return 0; 228 } 229 230 static int st7571_fb_update_rect_grayscale(struct drm_framebuffer *fb, struct drm_rect *rect) 231 { 232 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 233 u32 format = fb->format->format; 234 char *row = st7571->row; 235 int x1; 236 int x2; 237 238 /* Align y to display page boundaries */ 239 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 240 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 241 242 switch (format) { 243 case DRM_FORMAT_R1: 244 x1 = rect->x1 * 1; 245 x2 = rect->x2 * 1; 246 break; 247 case DRM_FORMAT_R2: 248 fallthrough; 249 case DRM_FORMAT_XRGB8888: 250 x1 = rect->x1 * 2; 251 x2 = rect->x2 * 2; 252 break; 253 } 254 255 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 256 for (int x = x1; x < x2; x++) 257 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 2); 258 259 st7571_set_position(st7571, rect->x1, y); 260 261 /* TODO: Investige why we can't write multiple bytes at once */ 262 for (int x = x1; x < x2; x++) { 263 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 264 265 /* 266 * As the display supports grayscale, all pixels must be written as two bits 267 * even if the format is monochrome. 268 * 269 * The bit values maps to the following grayscale: 270 * 0 0 = Black 271 * 0 1 = Dark gray 272 * 1 0 = Light gray 273 * 1 1 = White 274 * 275 * For monochrome formats, write the same value twice to get 276 * either a black or white pixel. 277 */ 278 if (format == DRM_FORMAT_R1) 279 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 280 } 281 } 282 283 return 0; 284 } 285 286 static int st7571_connector_get_modes(struct drm_connector *conn) 287 { 288 struct st7571_device *st7571 = drm_to_st7571(conn->dev); 289 290 return drm_connector_helper_get_modes_fixed(conn, &st7571->mode); 291 } 292 293 static const struct drm_connector_helper_funcs st7571_connector_helper_funcs = { 294 .get_modes = st7571_connector_get_modes, 295 }; 296 297 static const struct st7571_panel_format st7571_monochrome = { 298 .prepare_buffer = st7571_prepare_buffer_monochrome, 299 .update_rect = st7571_fb_update_rect_monochrome, 300 .mode = ST7571_COLOR_MODE_BLACKWHITE, 301 .formats = { 302 DRM_FORMAT_XRGB8888, 303 DRM_FORMAT_R1, 304 }, 305 .nformats = 2, 306 }; 307 308 static const struct st7571_panel_format st7571_grayscale = { 309 .prepare_buffer = st7571_prepare_buffer_grayscale, 310 .update_rect = st7571_fb_update_rect_grayscale, 311 .mode = ST7571_COLOR_MODE_GRAY, 312 .formats = { 313 DRM_FORMAT_XRGB8888, 314 DRM_FORMAT_R1, 315 DRM_FORMAT_R2, 316 }, 317 .nformats = 3, 318 }; 319 320 static const u64 st7571_primary_plane_fmtmods[] = { 321 DRM_FORMAT_MOD_LINEAR, 322 DRM_FORMAT_MOD_INVALID 323 }; 324 325 static int st7571_primary_plane_helper_atomic_check(struct drm_plane *plane, 326 struct drm_atomic_commit *state) 327 { 328 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); 329 struct drm_crtc *new_crtc = new_plane_state->crtc; 330 struct drm_crtc_state *new_crtc_state = NULL; 331 332 if (new_crtc) 333 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc); 334 335 return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, 336 DRM_PLANE_NO_SCALING, 337 DRM_PLANE_NO_SCALING, 338 false, false); 339 } 340 341 static void st7571_primary_plane_helper_atomic_update(struct drm_plane *plane, 342 struct drm_atomic_commit *state) 343 { 344 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 345 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 346 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 347 struct drm_framebuffer *fb = plane_state->fb; 348 struct drm_atomic_helper_damage_iter iter; 349 struct drm_device *drm = plane->dev; 350 struct drm_rect damage; 351 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 352 int ret, idx; 353 354 if (!fb) 355 return; /* no framebuffer; plane is disabled */ 356 357 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 358 if (ret) 359 return; 360 361 if (!drm_dev_enter(drm, &idx)) 362 goto out_drm_gem_fb_end_cpu_access; 363 364 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 365 drm_atomic_for_each_plane_damage(&iter, &damage) { 366 st7571->pformat->prepare_buffer(st7571, 367 &shadow_plane_state->data[0], 368 fb, &damage, 369 &shadow_plane_state->fmtcnv_state); 370 371 st7571->pformat->update_rect(fb, &damage); 372 } 373 374 drm_dev_exit(idx); 375 376 out_drm_gem_fb_end_cpu_access: 377 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 378 } 379 380 static void st7571_primary_plane_helper_atomic_disable(struct drm_plane *plane, 381 struct drm_atomic_commit *state) 382 { 383 struct drm_device *drm = plane->dev; 384 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 385 int idx; 386 387 if (!drm_dev_enter(drm, &idx)) 388 return; 389 390 st7571_fb_clear_screen(st7571); 391 drm_dev_exit(idx); 392 } 393 394 static const struct drm_plane_helper_funcs st7571_primary_plane_helper_funcs = { 395 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 396 .atomic_check = st7571_primary_plane_helper_atomic_check, 397 .atomic_update = st7571_primary_plane_helper_atomic_update, 398 .atomic_disable = st7571_primary_plane_helper_atomic_disable, 399 }; 400 401 static const struct drm_plane_funcs st7571_primary_plane_funcs = { 402 .update_plane = drm_atomic_helper_update_plane, 403 .disable_plane = drm_atomic_helper_disable_plane, 404 .destroy = drm_plane_cleanup, 405 DRM_GEM_SHADOW_PLANE_FUNCS, 406 }; 407 408 /* 409 * CRTC 410 */ 411 412 static enum drm_mode_status st7571_crtc_mode_valid(struct drm_crtc *crtc, 413 const struct drm_display_mode *mode) 414 { 415 struct st7571_device *st7571 = drm_to_st7571(crtc->dev); 416 417 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st7571->mode); 418 } 419 420 static const struct drm_crtc_helper_funcs st7571_crtc_helper_funcs = { 421 .atomic_check = drm_crtc_helper_atomic_check, 422 .mode_valid = st7571_crtc_mode_valid, 423 }; 424 425 static const struct drm_crtc_funcs st7571_crtc_funcs = { 426 .atomic_create_state = drm_atomic_helper_crtc_create_state, 427 .destroy = drm_crtc_cleanup, 428 .set_config = drm_atomic_helper_set_config, 429 .page_flip = drm_atomic_helper_page_flip, 430 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 431 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 432 }; 433 434 /* 435 * Encoder 436 */ 437 438 static void st7571_encoder_atomic_enable(struct drm_encoder *encoder, 439 struct drm_atomic_commit *state) 440 { 441 struct drm_device *drm = encoder->dev; 442 struct st7571_device *st7571 = drm_to_st7571(drm); 443 u8 command = ST7571_DISPLAY_ON; 444 int ret; 445 446 ret = st7571->pdata->init(st7571); 447 if (ret) 448 return; 449 450 st7571_send_command_list(st7571, &command, 1); 451 } 452 453 static void st7571_encoder_atomic_disable(struct drm_encoder *encoder, 454 struct drm_atomic_commit *state) 455 { 456 struct drm_device *drm = encoder->dev; 457 struct st7571_device *st7571 = drm_to_st7571(drm); 458 u8 command = ST7571_DISPLAY_OFF; 459 460 st7571_send_command_list(st7571, &command, 1); 461 } 462 463 static const struct drm_encoder_funcs st7571_encoder_funcs = { 464 .destroy = drm_encoder_cleanup, 465 466 }; 467 468 static const struct drm_encoder_helper_funcs st7571_encoder_helper_funcs = { 469 .atomic_enable = st7571_encoder_atomic_enable, 470 .atomic_disable = st7571_encoder_atomic_disable, 471 }; 472 473 /* 474 * Connector 475 */ 476 477 static const struct drm_connector_funcs st7571_connector_funcs = { 478 .reset = drm_atomic_helper_connector_reset, 479 .fill_modes = drm_helper_probe_single_connector_modes, 480 .destroy = drm_connector_cleanup, 481 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 482 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 483 }; 484 485 static const struct drm_mode_config_funcs st7571_mode_config_funcs = { 486 .fb_create = drm_gem_fb_create_with_dirty, 487 .atomic_check = drm_atomic_helper_check, 488 .atomic_commit = drm_atomic_helper_commit, 489 }; 490 491 static struct drm_display_mode st7571_mode(struct st7571_device *st7571) 492 { 493 struct drm_display_mode mode = { 494 DRM_SIMPLE_MODE(st7571->ncols, st7571->nlines, 495 st7571->width_mm, st7571->height_mm), 496 }; 497 498 return mode; 499 } 500 501 static int st7571_mode_config_init(struct st7571_device *st7571) 502 { 503 struct drm_device *drm = &st7571->drm; 504 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 505 int ret; 506 507 ret = drmm_mode_config_init(drm); 508 if (ret) 509 return ret; 510 511 drm->mode_config.min_width = constraints->min_ncols; 512 drm->mode_config.min_height = constraints->min_nlines; 513 drm->mode_config.max_width = constraints->max_ncols; 514 drm->mode_config.max_height = constraints->max_nlines; 515 drm->mode_config.preferred_depth = 24; 516 drm->mode_config.funcs = &st7571_mode_config_funcs; 517 518 return 0; 519 } 520 521 static int st7571_plane_init(struct st7571_device *st7571, 522 const struct st7571_panel_format *pformat) 523 { 524 struct drm_plane *primary_plane = &st7571->primary_plane; 525 struct drm_device *drm = &st7571->drm; 526 int ret; 527 528 ret = drm_universal_plane_init(drm, primary_plane, 0, 529 &st7571_primary_plane_funcs, 530 pformat->formats, 531 pformat->nformats, 532 st7571_primary_plane_fmtmods, 533 DRM_PLANE_TYPE_PRIMARY, NULL); 534 if (ret) 535 return ret; 536 537 drm_plane_helper_add(primary_plane, &st7571_primary_plane_helper_funcs); 538 drm_plane_enable_fb_damage_clips(primary_plane); 539 540 return 0; 541 } 542 543 static int st7571_crtc_init(struct st7571_device *st7571) 544 { 545 struct drm_plane *primary_plane = &st7571->primary_plane; 546 struct drm_crtc *crtc = &st7571->crtc; 547 struct drm_device *drm = &st7571->drm; 548 int ret; 549 550 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, 551 &st7571_crtc_funcs, NULL); 552 if (ret) 553 return ret; 554 555 drm_crtc_helper_add(crtc, &st7571_crtc_helper_funcs); 556 557 return 0; 558 } 559 560 static int st7571_encoder_init(struct st7571_device *st7571) 561 { 562 struct drm_encoder *encoder = &st7571->encoder; 563 struct drm_crtc *crtc = &st7571->crtc; 564 struct drm_device *drm = &st7571->drm; 565 int ret; 566 567 ret = drm_encoder_init(drm, encoder, &st7571_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); 568 if (ret) 569 return ret; 570 571 drm_encoder_helper_add(encoder, &st7571_encoder_helper_funcs); 572 573 encoder->possible_crtcs = drm_crtc_mask(crtc); 574 575 return 0; 576 } 577 578 static int st7571_connector_init(struct st7571_device *st7571) 579 { 580 struct drm_connector *connector = &st7571->connector; 581 struct drm_encoder *encoder = &st7571->encoder; 582 struct drm_device *drm = &st7571->drm; 583 int ret; 584 585 ret = drm_connector_init(drm, connector, &st7571_connector_funcs, 586 DRM_MODE_CONNECTOR_Unknown); 587 if (ret) 588 return ret; 589 590 drm_connector_helper_add(connector, &st7571_connector_helper_funcs); 591 592 return drm_connector_attach_encoder(connector, encoder); 593 } 594 595 DEFINE_DRM_GEM_FOPS(st7571_fops); 596 597 static const struct drm_driver st7571_driver = { 598 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 599 600 .name = DRIVER_NAME, 601 .desc = DRIVER_DESC, 602 .major = DRIVER_MAJOR, 603 .minor = DRIVER_MINOR, 604 605 .fops = &st7571_fops, 606 DRM_GEM_SHMEM_DRIVER_OPS, 607 DRM_FBDEV_SHMEM_DRIVER_OPS, 608 }; 609 610 static int st7571_validate_parameters(struct st7571_device *st7571) 611 { 612 struct device *dev = st7571->dev; 613 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 614 615 if (st7571->width_mm == 0) { 616 dev_err(dev, "Invalid panel width\n"); 617 return -EINVAL; 618 } 619 620 if (st7571->height_mm == 0) { 621 dev_err(dev, "Invalid panel height\n"); 622 return -EINVAL; 623 } 624 625 if (st7571->nlines < constraints->min_nlines || 626 st7571->nlines > constraints->max_nlines) { 627 dev_err(dev, "Invalid timing configuration.\n"); 628 return -EINVAL; 629 } 630 631 if (st7571->startline + st7571->nlines > constraints->max_nlines) { 632 dev_err(dev, "Invalid timing configuration.\n"); 633 return -EINVAL; 634 } 635 636 if (st7571->ncols < constraints->min_ncols || 637 st7571->ncols > constraints->max_ncols) { 638 dev_err(dev, "Invalid timing configuration.\n"); 639 return -EINVAL; 640 } 641 642 if (st7571->grayscale && !constraints->support_grayscale) { 643 dev_err(dev, "Grayscale not supported\n"); 644 return -EINVAL; 645 } 646 647 return 0; 648 } 649 650 static int st7567_parse_dt(struct st7571_device *st7567) 651 { 652 struct device *dev = st7567->dev; 653 struct device_node *np = dev->of_node; 654 struct display_timing dt; 655 int ret; 656 657 ret = of_get_display_timing(np, "panel-timing", &dt); 658 if (ret) { 659 dev_err(dev, "Failed to get display timing from DT\n"); 660 return ret; 661 } 662 663 of_property_read_u32(np, "width-mm", &st7567->width_mm); 664 of_property_read_u32(np, "height-mm", &st7567->height_mm); 665 st7567->inverted = of_property_read_bool(np, "sitronix,inverted"); 666 667 st7567->pformat = &st7571_monochrome; 668 st7567->bpp = 1; 669 670 st7567->startline = dt.vfront_porch.typ; 671 st7567->nlines = dt.vactive.typ; 672 st7567->ncols = dt.hactive.typ; 673 674 return 0; 675 } 676 677 static int st7571_parse_dt(struct st7571_device *st7571) 678 { 679 struct device *dev = st7571->dev; 680 struct device_node *np = dev->of_node; 681 struct display_timing dt; 682 int ret; 683 684 ret = of_get_display_timing(np, "panel-timing", &dt); 685 if (ret) { 686 dev_err(dev, "Failed to get display timing from DT\n"); 687 return ret; 688 } 689 690 of_property_read_u32(np, "width-mm", &st7571->width_mm); 691 of_property_read_u32(np, "height-mm", &st7571->height_mm); 692 st7571->grayscale = of_property_read_bool(np, "sitronix,grayscale"); 693 st7571->inverted = of_property_read_bool(np, "sitronix,inverted"); 694 695 if (st7571->grayscale) { 696 st7571->pformat = &st7571_grayscale; 697 st7571->bpp = 2; 698 } else { 699 st7571->pformat = &st7571_monochrome; 700 st7571->bpp = 1; 701 } 702 703 st7571->startline = dt.vfront_porch.typ; 704 st7571->nlines = dt.vactive.typ; 705 st7571->ncols = dt.hactive.typ; 706 707 st7571->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 708 if (IS_ERR(st7571->reset)) 709 return dev_err_probe(dev, PTR_ERR(st7571->reset), 710 "Failed to get reset gpio\n"); 711 712 return 0; 713 } 714 715 static void st7571_reset(struct st7571_device *st7571) 716 { 717 gpiod_set_value_cansleep(st7571->reset, 1); 718 fsleep(20); 719 gpiod_set_value_cansleep(st7571->reset, 0); 720 } 721 722 static int st7567_lcd_init(struct st7571_device *st7567) 723 { 724 /* 725 * Most of the initialization sequence is taken directly from the 726 * referential initial code in the ST7567 datasheet. 727 */ 728 u8 commands[] = { 729 ST7571_DISPLAY_OFF, 730 731 ST7567_SET_LCD_BIAS(1), 732 733 ST7571_SET_SEG_SCAN_DIR(0), 734 ST7571_SET_COM_SCAN_DIR(1), 735 736 ST7571_SET_REGULATOR_REG(4), 737 ST7571_SET_CONTRAST_MSB, 738 ST7571_SET_CONTRAST_LSB(0x20), 739 740 ST7571_SET_START_LINE_MSB, 741 ST7571_SET_START_LINE_LSB(st7567->startline), 742 743 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 744 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 745 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 746 747 ST7571_SET_REVERSE(st7567->inverted ? 1 : 0), 748 ST7571_SET_ENTIRE_DISPLAY_ON(0), 749 }; 750 751 return st7571_send_command_list(st7567, commands, ARRAY_SIZE(commands)); 752 } 753 754 static int st7571_lcd_init(struct st7571_device *st7571) 755 { 756 /* 757 * Most of the initialization sequence is taken directly from the 758 * referential initial code in the ST7571 datasheet. 759 */ 760 u8 commands[] = { 761 ST7571_DISPLAY_OFF, 762 763 ST7571_SET_MODE_MSB, 764 ST7571_SET_MODE_LSB(0x2e), 765 766 ST7571_SET_SEG_SCAN_DIR(0), 767 ST7571_SET_COM_SCAN_DIR(1), 768 769 ST7571_SET_COM0_MSB, 770 ST7571_SET_COM0_LSB(0x00), 771 772 ST7571_SET_START_LINE_MSB, 773 ST7571_SET_START_LINE_LSB(st7571->startline), 774 775 ST7571_OSC_ON, 776 ST7571_SET_REGULATOR_REG(5), 777 ST7571_SET_CONTRAST_MSB, 778 ST7571_SET_CONTRAST_LSB(0x33), 779 ST7571_SET_LCD_BIAS(0x04), 780 ST7571_SET_DISPLAY_DUTY_MSB, 781 ST7571_SET_DISPLAY_DUTY_LSB(st7571->nlines), 782 783 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 784 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 785 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 786 787 ST7571_COMMAND_SET_3, 788 ST7571_SET_COLOR_MODE(st7571->pformat->mode), 789 ST7571_COMMAND_SET_NORMAL, 790 791 ST7571_SET_REVERSE(st7571->inverted ? 1 : 0), 792 ST7571_SET_ENTIRE_DISPLAY_ON(0), 793 }; 794 795 /* Perform a reset before initializing the controller */ 796 st7571_reset(st7571); 797 798 return st7571_send_command_list(st7571, commands, ARRAY_SIZE(commands)); 799 } 800 801 struct st7571_device *st7571_probe(struct device *dev, 802 struct regmap *regmap) 803 { 804 struct st7571_device *st7571; 805 struct drm_device *drm; 806 int ret; 807 808 st7571 = devm_drm_dev_alloc(dev, &st7571_driver, 809 struct st7571_device, drm); 810 if (IS_ERR(st7571)) 811 return st7571; 812 813 drm = &st7571->drm; 814 st7571->dev = dev; 815 st7571->pdata = device_get_match_data(st7571->dev); 816 817 ret = st7571->pdata->parse_dt(st7571); 818 if (ret) 819 return ERR_PTR(ret); 820 821 ret = st7571_validate_parameters(st7571); 822 if (ret) 823 return ERR_PTR(ret); 824 825 st7571->mode = st7571_mode(st7571); 826 st7571->regmap = regmap; 827 828 st7571->hwbuf = devm_kzalloc(st7571->dev, 829 (st7571->nlines * st7571->ncols * st7571->bpp) / 8, 830 GFP_KERNEL); 831 if (!st7571->hwbuf) 832 return ERR_PTR(-ENOMEM); 833 834 st7571->row = devm_kzalloc(st7571->dev, 835 (st7571->ncols * st7571->bpp), 836 GFP_KERNEL); 837 if (!st7571->row) 838 return ERR_PTR(-ENOMEM); 839 840 ret = st7571_mode_config_init(st7571); 841 if (ret) { 842 dev_err(st7571->dev, "Failed to initialize mode config\n"); 843 return ERR_PTR(ret); 844 } 845 846 ret = st7571_plane_init(st7571, st7571->pformat); 847 if (ret) { 848 dev_err(st7571->dev, "Failed to initialize primary plane\n"); 849 return ERR_PTR(ret); 850 } 851 852 ret = st7571_crtc_init(st7571); 853 if (ret < 0) { 854 dev_err(st7571->dev, "Failed to initialize CRTC\n"); 855 return ERR_PTR(ret); 856 } 857 858 ret = st7571_encoder_init(st7571); 859 if (ret < 0) { 860 dev_err(st7571->dev, "Failed to initialize encoder\n"); 861 return ERR_PTR(ret); 862 } 863 864 ret = st7571_connector_init(st7571); 865 if (ret < 0) { 866 dev_err(st7571->dev, "Failed to initialize connector\n"); 867 return ERR_PTR(ret); 868 } 869 870 drm_mode_config_reset(drm); 871 872 ret = drm_dev_register(drm, 0); 873 if (ret) { 874 dev_err(st7571->dev, "Failed to register DRM device\n"); 875 return ERR_PTR(ret); 876 } 877 878 drm_client_setup(drm, NULL); 879 return st7571; 880 } 881 EXPORT_SYMBOL_GPL(st7571_probe); 882 883 void st7571_remove(struct st7571_device *st7571) 884 { 885 drm_dev_unplug(&st7571->drm); 886 } 887 EXPORT_SYMBOL_GPL(st7571_remove); 888 889 const struct st7571_panel_data st7567_config = { 890 .init = st7567_lcd_init, 891 .parse_dt = st7567_parse_dt, 892 .constraints = { 893 .min_nlines = 1, 894 .max_nlines = 64, 895 .min_ncols = 128, 896 .max_ncols = 128, 897 .support_grayscale = false, 898 }, 899 }; 900 EXPORT_SYMBOL_NS_GPL(st7567_config, "DRM_ST7571"); 901 902 const struct st7571_panel_data st7571_config = { 903 .init = st7571_lcd_init, 904 .parse_dt = st7571_parse_dt, 905 .constraints = { 906 .min_nlines = 1, 907 .max_nlines = 128, 908 .min_ncols = 128, 909 .max_ncols = 128, 910 .support_grayscale = true, 911 }, 912 }; 913 EXPORT_SYMBOL_NS_GPL(st7571_config, "DRM_ST7571"); 914 915 MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>"); 916 MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller"); 917 MODULE_LICENSE("GPL"); 918