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_fb_helper.h> 24 #include <drm/drm_fbdev_shmem.h> 25 #include <drm/drm_fourcc.h> 26 #include <drm/drm_framebuffer.h> 27 #include <drm/drm_gem_atomic_helper.h> 28 #include <drm/drm_gem_framebuffer_helper.h> 29 #include <drm/drm_gem_shmem_helper.h> 30 #include <drm/drm_modeset_helper_vtables.h> 31 #include <drm/drm_module.h> 32 #include <drm/drm_plane.h> 33 #include <drm/drm_probe_helper.h> 34 35 #include <video/display_timing.h> 36 #include <video/of_display_timing.h> 37 38 #define ST7571_COMMAND_MODE (0x00) 39 #define ST7571_DATA_MODE (0x40) 40 41 /* Normal mode command set */ 42 #define ST7571_DISPLAY_OFF (0xae) 43 #define ST7571_DISPLAY_ON (0xaf) 44 #define ST7571_OSC_ON (0xab) 45 #define ST7571_SET_COLUMN_LSB(c) (0x00 | FIELD_PREP(GENMASK(3, 0), (c))) 46 #define ST7571_SET_COLUMN_MSB(c) (0x10 | FIELD_PREP(GENMASK(2, 0), (c) >> 4)) 47 #define ST7571_SET_COM0_LSB(x) (FIELD_PREP(GENMASK(6, 0), (x))) 48 #define ST7571_SET_COM0_MSB (0x44) 49 #define ST7571_SET_COM_SCAN_DIR(d) (0xc0 | FIELD_PREP(GENMASK(3, 3), (d))) 50 #define ST7571_SET_CONTRAST_LSB(c) (FIELD_PREP(GENMASK(5, 0), (c))) 51 #define ST7571_SET_CONTRAST_MSB (0x81) 52 #define ST7571_SET_DISPLAY_DUTY_LSB(d) (FIELD_PREP(GENMASK(7, 0), (d))) 53 #define ST7571_SET_DISPLAY_DUTY_MSB (0x48) 54 #define ST7571_SET_ENTIRE_DISPLAY_ON(p) (0xa4 | FIELD_PREP(GENMASK(0, 0), (p))) 55 #define ST7571_SET_LCD_BIAS(b) (0x50 | FIELD_PREP(GENMASK(2, 0), (b))) 56 #define ST7571_SET_MODE_LSB(m) (FIELD_PREP(GENMASK(7, 2), (m))) 57 #define ST7571_SET_MODE_MSB (0x38) 58 #define ST7571_SET_PAGE(p) (0xb0 | FIELD_PREP(GENMASK(3, 0), (p))) 59 #define ST7571_SET_POWER(p) (0x28 | FIELD_PREP(GENMASK(2, 0), (p))) 60 #define ST7571_SET_REGULATOR_REG(r) (0x20 | FIELD_PREP(GENMASK(2, 0), (r))) 61 #define ST7571_SET_REVERSE(r) (0xa6 | FIELD_PREP(GENMASK(0, 0), (r))) 62 #define ST7571_SET_SEG_SCAN_DIR(d) (0xa0 | FIELD_PREP(GENMASK(0, 0), (d))) 63 #define ST7571_SET_START_LINE_LSB(l) (FIELD_PREP(GENMASK(6, 0), (l))) 64 #define ST7571_SET_START_LINE_MSB (0x40) 65 66 /* Extension command set 3 */ 67 #define ST7571_COMMAND_SET_3 (0x7b) 68 #define ST7571_SET_COLOR_MODE(c) (0x10 | FIELD_PREP(GENMASK(0, 0), (c))) 69 #define ST7571_COMMAND_SET_NORMAL (0x00) 70 71 /* ST7567 commands */ 72 #define ST7567_SET_LCD_BIAS(m) (0xa2 | FIELD_PREP(GENMASK(0, 0), (m))) 73 74 #define ST7571_PAGE_HEIGHT 8 75 76 #define DRIVER_NAME "st7571" 77 #define DRIVER_DESC "ST7571 DRM driver" 78 #define DRIVER_MAJOR 1 79 #define DRIVER_MINOR 0 80 81 enum st7571_color_mode { 82 ST7571_COLOR_MODE_GRAY = 0, 83 ST7571_COLOR_MODE_BLACKWHITE = 1, 84 }; 85 86 struct st7571_device; 87 88 struct st7571_panel_constraints { 89 u32 min_nlines; 90 u32 max_nlines; 91 u32 min_ncols; 92 u32 max_ncols; 93 bool support_grayscale; 94 }; 95 96 struct st7571_panel_data { 97 int (*init)(struct st7571_device *st7571); 98 int (*parse_dt)(struct st7571_device *st7571); 99 struct st7571_panel_constraints constraints; 100 }; 101 102 struct st7571_panel_format { 103 void (*prepare_buffer)(struct st7571_device *st7571, 104 const struct iosys_map *vmap, 105 struct drm_framebuffer *fb, 106 struct drm_rect *rect, 107 struct drm_format_conv_state *fmtcnv_state); 108 int (*update_rect)(struct drm_framebuffer *fb, struct drm_rect *rect); 109 enum st7571_color_mode mode; 110 const u8 nformats; 111 const u32 formats[]; 112 }; 113 114 struct st7571_device { 115 struct drm_device dev; 116 117 struct drm_plane primary_plane; 118 struct drm_crtc crtc; 119 struct drm_encoder encoder; 120 struct drm_connector connector; 121 122 struct drm_display_mode mode; 123 124 const struct st7571_panel_format *pformat; 125 const struct st7571_panel_data *pdata; 126 struct i2c_client *client; 127 struct gpio_desc *reset; 128 struct regmap *regmap; 129 130 /* 131 * Depending on the hardware design, the acknowledge signal may be hard to 132 * recognize as a valid logic "0" level. 133 * Therefor, ignore NAK if possible to stay compatible with most hardware designs 134 * and off-the-shelf panels out there. 135 * 136 * From section 6.4 MICROPOCESSOR INTERFACE section in the datasheet: 137 * 138 * "By connecting SDA_OUT to SDA_IN externally, the SDA line becomes fully 139 * I2C interface compatible. 140 * Separating acknowledge-output from serial data 141 * input is advantageous for chip-on-glass (COG) applications. In COG 142 * applications, the ITO resistance and the pull-up resistor will form a 143 * voltage divider, which affects acknowledge-signal level. Larger ITO 144 * resistance will raise the acknowledged-signal level and system cannot 145 * recognize this level as a valid logic “0” level. By separating SDA_IN from 146 * SDA_OUT, the IC can be used in a mode that ignores the acknowledge-bit. 147 * For applications which check acknowledge-bit, it is necessary to minimize 148 * the ITO resistance of the SDA_OUT trace to guarantee a valid low level." 149 * 150 */ 151 bool ignore_nak; 152 153 bool grayscale; 154 bool inverted; 155 u32 height_mm; 156 u32 width_mm; 157 u32 startline; 158 u32 nlines; 159 u32 ncols; 160 u32 bpp; 161 162 /* Intermediate buffer in LCD friendly format */ 163 u8 *hwbuf; 164 165 /* Row of (transformed) pixels ready to be written to the display */ 166 u8 *row; 167 }; 168 169 static inline struct st7571_device *drm_to_st7571(struct drm_device *dev) 170 { 171 return container_of(dev, struct st7571_device, dev); 172 } 173 174 static int st7571_regmap_write(void *context, const void *data, size_t count) 175 { 176 struct i2c_client *client = context; 177 struct st7571_device *st7571 = i2c_get_clientdata(client); 178 int ret; 179 180 struct i2c_msg msg = { 181 .addr = st7571->client->addr, 182 .flags = st7571->ignore_nak ? I2C_M_IGNORE_NAK : 0, 183 .len = count, 184 .buf = (u8 *)data 185 }; 186 187 ret = i2c_transfer(st7571->client->adapter, &msg, 1); 188 189 /* 190 * Unfortunately, there is no way to check if the transfer failed because of 191 * a NAK or something else as I2C bus drivers use different return values for NAK. 192 * 193 * However, if the transfer fails and ignore_nak is set, we know it is an error. 194 */ 195 if (ret < 0 && st7571->ignore_nak) 196 return ret; 197 198 return 0; 199 } 200 201 /* The st7571 driver does not read registers but regmap expects a .read */ 202 static int st7571_regmap_read(void *context, const void *reg_buf, 203 size_t reg_size, void *val_buf, size_t val_size) 204 { 205 return -EOPNOTSUPP; 206 } 207 208 static int st7571_send_command_list(struct st7571_device *st7571, 209 const u8 *cmd_list, size_t len) 210 { 211 int ret; 212 213 for (int i = 0; i < len; i++) { 214 ret = regmap_write(st7571->regmap, ST7571_COMMAND_MODE, cmd_list[i]); 215 if (ret < 0) 216 return ret; 217 } 218 219 return ret; 220 } 221 222 static inline u8 st7571_transform_xy(const char *p, int x, int y, u8 bpp) 223 { 224 int xrest = x % 8; 225 u8 result = 0; 226 u8 row_len = 16 * bpp; 227 228 /* 229 * Transforms an (x, y) pixel coordinate into a vertical 8-bit 230 * column from the framebuffer. It calculates the corresponding byte in the 231 * framebuffer, extracts the bit at the given x position across 8 consecutive 232 * rows, and packs those bits into a single byte. 233 * 234 * Return an 8-bit value representing a vertical column of pixels. 235 */ 236 x = x / 8; 237 y = (y / 8) * 8; 238 239 for (int i = 0; i < 8; i++) { 240 int row_idx = y + i; 241 u8 byte = p[row_idx * row_len + x]; 242 u8 bit = (byte >> xrest) & 1; 243 244 result |= (bit << i); 245 } 246 247 return result; 248 } 249 250 static int st7571_set_position(struct st7571_device *st7571, int x, int y) 251 { 252 u8 cmd_list[] = { 253 ST7571_SET_COLUMN_LSB(x), 254 ST7571_SET_COLUMN_MSB(x), 255 ST7571_SET_PAGE(y / ST7571_PAGE_HEIGHT), 256 }; 257 258 return st7571_send_command_list(st7571, cmd_list, ARRAY_SIZE(cmd_list)); 259 } 260 261 static int st7571_fb_clear_screen(struct st7571_device *st7571) 262 { 263 u32 npixels = st7571->ncols * round_up(st7571->nlines, ST7571_PAGE_HEIGHT) * st7571->bpp; 264 char pixelvalue = 0x00; 265 266 for (int i = 0; i < npixels; i++) 267 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, &pixelvalue, 1); 268 269 return 0; 270 } 271 272 static void st7571_prepare_buffer_monochrome(struct st7571_device *st7571, 273 const struct iosys_map *vmap, 274 struct drm_framebuffer *fb, 275 struct drm_rect *rect, 276 struct drm_format_conv_state *fmtcnv_state) 277 { 278 unsigned int dst_pitch; 279 struct iosys_map dst; 280 u32 size; 281 282 switch (fb->format->format) { 283 case DRM_FORMAT_XRGB8888: 284 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8); 285 iosys_map_set_vaddr(&dst, st7571->hwbuf); 286 287 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 288 break; 289 290 case DRM_FORMAT_R1: 291 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 292 memcpy(st7571->hwbuf, vmap->vaddr, size); 293 break; 294 } 295 } 296 297 static void st7571_prepare_buffer_grayscale(struct st7571_device *st7571, 298 const struct iosys_map *vmap, 299 struct drm_framebuffer *fb, 300 struct drm_rect *rect, 301 struct drm_format_conv_state *fmtcnv_state) 302 { 303 u32 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 304 unsigned int dst_pitch; 305 struct iosys_map dst; 306 307 switch (fb->format->format) { 308 case DRM_FORMAT_XRGB8888: 309 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 4); 310 iosys_map_set_vaddr(&dst, st7571->hwbuf); 311 312 drm_fb_xrgb8888_to_gray2(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 313 break; 314 315 case DRM_FORMAT_R1: 316 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 317 memcpy(st7571->hwbuf, vmap->vaddr, size); 318 break; 319 320 case DRM_FORMAT_R2: 321 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 4; 322 memcpy(st7571->hwbuf, vmap->vaddr, size); 323 break; 324 }; 325 } 326 327 static int st7571_fb_update_rect_monochrome(struct drm_framebuffer *fb, struct drm_rect *rect) 328 { 329 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 330 char *row = st7571->row; 331 332 /* Align y to display page boundaries */ 333 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 334 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 335 336 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 337 for (int x = rect->x1; x < rect->x2; x++) 338 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 1); 339 340 st7571_set_position(st7571, rect->x1, y); 341 342 /* TODO: Investige why we can't write multiple bytes at once */ 343 for (int x = rect->x1; x < rect->x2; x++) 344 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 345 } 346 347 return 0; 348 } 349 350 static int st7571_fb_update_rect_grayscale(struct drm_framebuffer *fb, struct drm_rect *rect) 351 { 352 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 353 u32 format = fb->format->format; 354 char *row = st7571->row; 355 int x1; 356 int x2; 357 358 /* Align y to display page boundaries */ 359 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 360 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 361 362 switch (format) { 363 case DRM_FORMAT_R1: 364 x1 = rect->x1 * 1; 365 x2 = rect->x2 * 1; 366 break; 367 case DRM_FORMAT_R2: 368 fallthrough; 369 case DRM_FORMAT_XRGB8888: 370 x1 = rect->x1 * 2; 371 x2 = rect->x2 * 2; 372 break; 373 } 374 375 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 376 for (int x = x1; x < x2; x++) 377 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 2); 378 379 st7571_set_position(st7571, rect->x1, y); 380 381 /* TODO: Investige why we can't write multiple bytes at once */ 382 for (int x = x1; x < x2; x++) { 383 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 384 385 /* 386 * As the display supports grayscale, all pixels must be written as two bits 387 * even if the format is monochrome. 388 * 389 * The bit values maps to the following grayscale: 390 * 0 0 = Black 391 * 0 1 = Dark gray 392 * 1 0 = Light gray 393 * 1 1 = White 394 * 395 * For monochrome formats, write the same value twice to get 396 * either a black or white pixel. 397 */ 398 if (format == DRM_FORMAT_R1) 399 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 400 } 401 } 402 403 return 0; 404 } 405 406 static int st7571_connector_get_modes(struct drm_connector *conn) 407 { 408 struct st7571_device *st7571 = drm_to_st7571(conn->dev); 409 410 return drm_connector_helper_get_modes_fixed(conn, &st7571->mode); 411 } 412 413 static const struct drm_connector_helper_funcs st7571_connector_helper_funcs = { 414 .get_modes = st7571_connector_get_modes, 415 }; 416 417 static const struct st7571_panel_format st7571_monochrome = { 418 .prepare_buffer = st7571_prepare_buffer_monochrome, 419 .update_rect = st7571_fb_update_rect_monochrome, 420 .mode = ST7571_COLOR_MODE_BLACKWHITE, 421 .formats = { 422 DRM_FORMAT_XRGB8888, 423 DRM_FORMAT_R1, 424 }, 425 .nformats = 2, 426 }; 427 428 static const struct st7571_panel_format st7571_grayscale = { 429 .prepare_buffer = st7571_prepare_buffer_grayscale, 430 .update_rect = st7571_fb_update_rect_grayscale, 431 .mode = ST7571_COLOR_MODE_GRAY, 432 .formats = { 433 DRM_FORMAT_XRGB8888, 434 DRM_FORMAT_R1, 435 DRM_FORMAT_R2, 436 }, 437 .nformats = 3, 438 }; 439 440 static const u64 st7571_primary_plane_fmtmods[] = { 441 DRM_FORMAT_MOD_LINEAR, 442 DRM_FORMAT_MOD_INVALID 443 }; 444 445 static int st7571_primary_plane_helper_atomic_check(struct drm_plane *plane, 446 struct drm_atomic_state *state) 447 { 448 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); 449 struct drm_crtc *new_crtc = new_plane_state->crtc; 450 struct drm_crtc_state *new_crtc_state = NULL; 451 452 if (new_crtc) 453 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc); 454 455 return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, 456 DRM_PLANE_NO_SCALING, 457 DRM_PLANE_NO_SCALING, 458 false, false); 459 } 460 461 static void st7571_primary_plane_helper_atomic_update(struct drm_plane *plane, 462 struct drm_atomic_state *state) 463 { 464 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 465 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 466 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 467 struct drm_framebuffer *fb = plane_state->fb; 468 struct drm_atomic_helper_damage_iter iter; 469 struct drm_device *dev = plane->dev; 470 struct drm_rect damage; 471 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 472 int ret, idx; 473 474 if (!fb) 475 return; /* no framebuffer; plane is disabled */ 476 477 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 478 if (ret) 479 return; 480 481 if (!drm_dev_enter(dev, &idx)) 482 goto out_drm_gem_fb_end_cpu_access; 483 484 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 485 drm_atomic_for_each_plane_damage(&iter, &damage) { 486 st7571->pformat->prepare_buffer(st7571, 487 &shadow_plane_state->data[0], 488 fb, &damage, 489 &shadow_plane_state->fmtcnv_state); 490 491 st7571->pformat->update_rect(fb, &damage); 492 } 493 494 drm_dev_exit(idx); 495 496 out_drm_gem_fb_end_cpu_access: 497 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 498 } 499 500 static void st7571_primary_plane_helper_atomic_disable(struct drm_plane *plane, 501 struct drm_atomic_state *state) 502 { 503 struct drm_device *dev = plane->dev; 504 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 505 int idx; 506 507 if (!drm_dev_enter(dev, &idx)) 508 return; 509 510 st7571_fb_clear_screen(st7571); 511 drm_dev_exit(idx); 512 } 513 514 static const struct drm_plane_helper_funcs st7571_primary_plane_helper_funcs = { 515 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 516 .atomic_check = st7571_primary_plane_helper_atomic_check, 517 .atomic_update = st7571_primary_plane_helper_atomic_update, 518 .atomic_disable = st7571_primary_plane_helper_atomic_disable, 519 }; 520 521 static const struct drm_plane_funcs st7571_primary_plane_funcs = { 522 .update_plane = drm_atomic_helper_update_plane, 523 .disable_plane = drm_atomic_helper_disable_plane, 524 .destroy = drm_plane_cleanup, 525 DRM_GEM_SHADOW_PLANE_FUNCS, 526 }; 527 528 /* 529 * CRTC 530 */ 531 532 static enum drm_mode_status st7571_crtc_mode_valid(struct drm_crtc *crtc, 533 const struct drm_display_mode *mode) 534 { 535 struct st7571_device *st7571 = drm_to_st7571(crtc->dev); 536 537 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st7571->mode); 538 } 539 540 static const struct drm_crtc_helper_funcs st7571_crtc_helper_funcs = { 541 .atomic_check = drm_crtc_helper_atomic_check, 542 .mode_valid = st7571_crtc_mode_valid, 543 }; 544 545 static const struct drm_crtc_funcs st7571_crtc_funcs = { 546 .reset = drm_atomic_helper_crtc_reset, 547 .destroy = drm_crtc_cleanup, 548 .set_config = drm_atomic_helper_set_config, 549 .page_flip = drm_atomic_helper_page_flip, 550 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 551 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 552 }; 553 554 /* 555 * Encoder 556 */ 557 558 static void st7571_encoder_atomic_enable(struct drm_encoder *encoder, 559 struct drm_atomic_state *state) 560 { 561 struct drm_device *drm = encoder->dev; 562 struct st7571_device *st7571 = drm_to_st7571(drm); 563 u8 command = ST7571_DISPLAY_ON; 564 int ret; 565 566 ret = st7571->pdata->init(st7571); 567 if (ret) 568 return; 569 570 st7571_send_command_list(st7571, &command, 1); 571 } 572 573 static void st7571_encoder_atomic_disable(struct drm_encoder *encoder, 574 struct drm_atomic_state *state) 575 { 576 struct drm_device *drm = encoder->dev; 577 struct st7571_device *st7571 = drm_to_st7571(drm); 578 u8 command = ST7571_DISPLAY_OFF; 579 580 st7571_send_command_list(st7571, &command, 1); 581 } 582 583 static const struct drm_encoder_funcs st7571_encoder_funcs = { 584 .destroy = drm_encoder_cleanup, 585 586 }; 587 588 static const struct drm_encoder_helper_funcs st7571_encoder_helper_funcs = { 589 .atomic_enable = st7571_encoder_atomic_enable, 590 .atomic_disable = st7571_encoder_atomic_disable, 591 }; 592 593 /* 594 * Connector 595 */ 596 597 static const struct drm_connector_funcs st7571_connector_funcs = { 598 .reset = drm_atomic_helper_connector_reset, 599 .fill_modes = drm_helper_probe_single_connector_modes, 600 .destroy = drm_connector_cleanup, 601 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 602 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 603 }; 604 605 static const struct drm_mode_config_funcs st7571_mode_config_funcs = { 606 .fb_create = drm_gem_fb_create_with_dirty, 607 .atomic_check = drm_atomic_helper_check, 608 .atomic_commit = drm_atomic_helper_commit, 609 }; 610 611 static struct drm_display_mode st7571_mode(struct st7571_device *st7571) 612 { 613 struct drm_display_mode mode = { 614 DRM_SIMPLE_MODE(st7571->ncols, st7571->nlines, 615 st7571->width_mm, st7571->height_mm), 616 }; 617 618 return mode; 619 } 620 621 static int st7571_mode_config_init(struct st7571_device *st7571) 622 { 623 struct drm_device *dev = &st7571->dev; 624 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 625 int ret; 626 627 ret = drmm_mode_config_init(dev); 628 if (ret) 629 return ret; 630 631 dev->mode_config.min_width = constraints->min_ncols; 632 dev->mode_config.min_height = constraints->min_nlines; 633 dev->mode_config.max_width = constraints->max_ncols; 634 dev->mode_config.max_height = constraints->max_nlines; 635 dev->mode_config.preferred_depth = 24; 636 dev->mode_config.funcs = &st7571_mode_config_funcs; 637 638 return 0; 639 } 640 641 static int st7571_plane_init(struct st7571_device *st7571, 642 const struct st7571_panel_format *pformat) 643 { 644 struct drm_plane *primary_plane = &st7571->primary_plane; 645 struct drm_device *dev = &st7571->dev; 646 int ret; 647 648 ret = drm_universal_plane_init(dev, primary_plane, 0, 649 &st7571_primary_plane_funcs, 650 pformat->formats, 651 pformat->nformats, 652 st7571_primary_plane_fmtmods, 653 DRM_PLANE_TYPE_PRIMARY, NULL); 654 if (ret) 655 return ret; 656 657 drm_plane_helper_add(primary_plane, &st7571_primary_plane_helper_funcs); 658 drm_plane_enable_fb_damage_clips(primary_plane); 659 660 return 0; 661 } 662 663 static int st7571_crtc_init(struct st7571_device *st7571) 664 { 665 struct drm_plane *primary_plane = &st7571->primary_plane; 666 struct drm_crtc *crtc = &st7571->crtc; 667 struct drm_device *dev = &st7571->dev; 668 int ret; 669 670 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 671 &st7571_crtc_funcs, NULL); 672 if (ret) 673 return ret; 674 675 drm_crtc_helper_add(crtc, &st7571_crtc_helper_funcs); 676 677 return 0; 678 } 679 680 static int st7571_encoder_init(struct st7571_device *st7571) 681 { 682 struct drm_encoder *encoder = &st7571->encoder; 683 struct drm_crtc *crtc = &st7571->crtc; 684 struct drm_device *dev = &st7571->dev; 685 int ret; 686 687 ret = drm_encoder_init(dev, encoder, &st7571_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); 688 if (ret) 689 return ret; 690 691 drm_encoder_helper_add(encoder, &st7571_encoder_helper_funcs); 692 693 encoder->possible_crtcs = drm_crtc_mask(crtc); 694 695 return 0; 696 } 697 698 static int st7571_connector_init(struct st7571_device *st7571) 699 { 700 struct drm_connector *connector = &st7571->connector; 701 struct drm_encoder *encoder = &st7571->encoder; 702 struct drm_device *dev = &st7571->dev; 703 int ret; 704 705 ret = drm_connector_init(dev, connector, &st7571_connector_funcs, 706 DRM_MODE_CONNECTOR_Unknown); 707 if (ret) 708 return ret; 709 710 drm_connector_helper_add(connector, &st7571_connector_helper_funcs); 711 712 return drm_connector_attach_encoder(connector, encoder); 713 } 714 715 DEFINE_DRM_GEM_FOPS(st7571_fops); 716 717 static const struct drm_driver st7571_driver = { 718 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 719 720 .name = DRIVER_NAME, 721 .desc = DRIVER_DESC, 722 .major = DRIVER_MAJOR, 723 .minor = DRIVER_MINOR, 724 725 .fops = &st7571_fops, 726 DRM_GEM_SHMEM_DRIVER_OPS, 727 DRM_FBDEV_SHMEM_DRIVER_OPS, 728 }; 729 730 static const struct regmap_bus st7571_regmap_bus = { 731 .read = st7571_regmap_read, 732 .write = st7571_regmap_write, 733 }; 734 735 static const struct regmap_config st7571_regmap_config = { 736 .reg_bits = 8, 737 .val_bits = 8, 738 .use_single_write = true, 739 }; 740 741 static int st7571_validate_parameters(struct st7571_device *st7571) 742 { 743 struct device *dev = st7571->dev.dev; 744 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 745 746 if (st7571->width_mm == 0) { 747 dev_err(dev, "Invalid panel width\n"); 748 return -EINVAL; 749 } 750 751 if (st7571->height_mm == 0) { 752 dev_err(dev, "Invalid panel height\n"); 753 return -EINVAL; 754 } 755 756 if (st7571->nlines < constraints->min_nlines || 757 st7571->nlines > constraints->max_nlines) { 758 dev_err(dev, "Invalid timing configuration.\n"); 759 return -EINVAL; 760 } 761 762 if (st7571->startline + st7571->nlines > constraints->max_nlines) { 763 dev_err(dev, "Invalid timing configuration.\n"); 764 return -EINVAL; 765 } 766 767 if (st7571->ncols < constraints->min_ncols || 768 st7571->ncols > constraints->max_ncols) { 769 dev_err(dev, "Invalid timing configuration.\n"); 770 return -EINVAL; 771 } 772 773 if (st7571->grayscale && !constraints->support_grayscale) { 774 dev_err(dev, "Grayscale not supported\n"); 775 return -EINVAL; 776 } 777 778 return 0; 779 } 780 781 static int st7567_parse_dt(struct st7571_device *st7567) 782 { 783 struct device *dev = &st7567->client->dev; 784 struct device_node *np = dev->of_node; 785 struct display_timing dt; 786 int ret; 787 788 ret = of_get_display_timing(np, "panel-timing", &dt); 789 if (ret) { 790 dev_err(dev, "Failed to get display timing from DT\n"); 791 return ret; 792 } 793 794 of_property_read_u32(np, "width-mm", &st7567->width_mm); 795 of_property_read_u32(np, "height-mm", &st7567->height_mm); 796 st7567->inverted = of_property_read_bool(np, "sitronix,inverted"); 797 798 st7567->pformat = &st7571_monochrome; 799 st7567->bpp = 1; 800 801 st7567->startline = dt.vfront_porch.typ; 802 st7567->nlines = dt.vactive.typ; 803 st7567->ncols = dt.hactive.typ; 804 805 return 0; 806 } 807 808 static int st7571_parse_dt(struct st7571_device *st7571) 809 { 810 struct device *dev = &st7571->client->dev; 811 struct device_node *np = dev->of_node; 812 struct display_timing dt; 813 int ret; 814 815 ret = of_get_display_timing(np, "panel-timing", &dt); 816 if (ret) { 817 dev_err(dev, "Failed to get display timing from DT\n"); 818 return ret; 819 } 820 821 of_property_read_u32(np, "width-mm", &st7571->width_mm); 822 of_property_read_u32(np, "height-mm", &st7571->height_mm); 823 st7571->grayscale = of_property_read_bool(np, "sitronix,grayscale"); 824 st7571->inverted = of_property_read_bool(np, "sitronix,inverted"); 825 826 if (st7571->grayscale) { 827 st7571->pformat = &st7571_grayscale; 828 st7571->bpp = 2; 829 } else { 830 st7571->pformat = &st7571_monochrome; 831 st7571->bpp = 1; 832 } 833 834 st7571->startline = dt.vfront_porch.typ; 835 st7571->nlines = dt.vactive.typ; 836 st7571->ncols = dt.hactive.typ; 837 838 st7571->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 839 if (IS_ERR(st7571->reset)) 840 return dev_err_probe(dev, PTR_ERR(st7571->reset), 841 "Failed to get reset gpio\n"); 842 843 844 return 0; 845 } 846 847 static void st7571_reset(struct st7571_device *st7571) 848 { 849 gpiod_set_value_cansleep(st7571->reset, 1); 850 fsleep(20); 851 gpiod_set_value_cansleep(st7571->reset, 0); 852 } 853 854 static int st7567_lcd_init(struct st7571_device *st7567) 855 { 856 /* 857 * Most of the initialization sequence is taken directly from the 858 * referential initial code in the ST7567 datasheet. 859 */ 860 u8 commands[] = { 861 ST7571_DISPLAY_OFF, 862 863 ST7567_SET_LCD_BIAS(1), 864 865 ST7571_SET_SEG_SCAN_DIR(0), 866 ST7571_SET_COM_SCAN_DIR(1), 867 868 ST7571_SET_REGULATOR_REG(4), 869 ST7571_SET_CONTRAST_MSB, 870 ST7571_SET_CONTRAST_LSB(0x20), 871 872 ST7571_SET_START_LINE_MSB, 873 ST7571_SET_START_LINE_LSB(st7567->startline), 874 875 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 876 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 877 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 878 879 ST7571_SET_REVERSE(st7567->inverted ? 1 : 0), 880 ST7571_SET_ENTIRE_DISPLAY_ON(0), 881 }; 882 883 return st7571_send_command_list(st7567, commands, ARRAY_SIZE(commands)); 884 } 885 886 static int st7571_lcd_init(struct st7571_device *st7571) 887 { 888 /* 889 * Most of the initialization sequence is taken directly from the 890 * referential initial code in the ST7571 datasheet. 891 */ 892 u8 commands[] = { 893 ST7571_DISPLAY_OFF, 894 895 ST7571_SET_MODE_MSB, 896 ST7571_SET_MODE_LSB(0x2e), 897 898 ST7571_SET_SEG_SCAN_DIR(0), 899 ST7571_SET_COM_SCAN_DIR(1), 900 901 ST7571_SET_COM0_MSB, 902 ST7571_SET_COM0_LSB(0x00), 903 904 ST7571_SET_START_LINE_MSB, 905 ST7571_SET_START_LINE_LSB(st7571->startline), 906 907 ST7571_OSC_ON, 908 ST7571_SET_REGULATOR_REG(5), 909 ST7571_SET_CONTRAST_MSB, 910 ST7571_SET_CONTRAST_LSB(0x33), 911 ST7571_SET_LCD_BIAS(0x04), 912 ST7571_SET_DISPLAY_DUTY_MSB, 913 ST7571_SET_DISPLAY_DUTY_LSB(st7571->nlines), 914 915 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 916 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 917 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 918 919 ST7571_COMMAND_SET_3, 920 ST7571_SET_COLOR_MODE(st7571->pformat->mode), 921 ST7571_COMMAND_SET_NORMAL, 922 923 ST7571_SET_REVERSE(st7571->inverted ? 1 : 0), 924 ST7571_SET_ENTIRE_DISPLAY_ON(0), 925 }; 926 927 /* Perform a reset before initializing the controller */ 928 st7571_reset(st7571); 929 930 return st7571_send_command_list(st7571, commands, ARRAY_SIZE(commands)); 931 } 932 933 static int st7571_probe(struct i2c_client *client) 934 { 935 struct st7571_device *st7571; 936 struct drm_device *dev; 937 int ret; 938 939 st7571 = devm_drm_dev_alloc(&client->dev, &st7571_driver, 940 struct st7571_device, dev); 941 if (IS_ERR(st7571)) 942 return PTR_ERR(st7571); 943 944 dev = &st7571->dev; 945 st7571->client = client; 946 i2c_set_clientdata(client, st7571); 947 st7571->pdata = device_get_match_data(&client->dev); 948 949 ret = st7571->pdata->parse_dt(st7571); 950 if (ret) 951 return ret; 952 953 ret = st7571_validate_parameters(st7571); 954 if (ret) 955 return ret; 956 957 st7571->mode = st7571_mode(st7571); 958 959 /* 960 * The hardware design could make it hard to detect a NAK on the I2C bus. 961 * If the adapter does not support protocol mangling do 962 * not set the I2C_M_IGNORE_NAK flag at the expense * of possible 963 * cruft in the logs. 964 */ 965 if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING)) 966 st7571->ignore_nak = true; 967 968 st7571->regmap = devm_regmap_init(&client->dev, &st7571_regmap_bus, 969 client, &st7571_regmap_config); 970 if (IS_ERR(st7571->regmap)) { 971 return dev_err_probe(&client->dev, PTR_ERR(st7571->regmap), 972 "Failed to initialize regmap\n"); 973 } 974 975 st7571->hwbuf = devm_kzalloc(&client->dev, 976 (st7571->nlines * st7571->ncols * st7571->bpp) / 8, 977 GFP_KERNEL); 978 if (!st7571->hwbuf) 979 return -ENOMEM; 980 981 st7571->row = devm_kzalloc(&client->dev, 982 (st7571->ncols * st7571->bpp), 983 GFP_KERNEL); 984 if (!st7571->row) 985 return -ENOMEM; 986 987 ret = st7571_mode_config_init(st7571); 988 if (ret) 989 return dev_err_probe(&client->dev, ret, 990 "Failed to initialize mode config\n"); 991 992 ret = st7571_plane_init(st7571, st7571->pformat); 993 if (ret) 994 return dev_err_probe(&client->dev, ret, 995 "Failed to initialize primary plane\n"); 996 997 ret = st7571_crtc_init(st7571); 998 if (ret < 0) 999 return dev_err_probe(&client->dev, ret, 1000 "Failed to initialize CRTC\n"); 1001 1002 ret = st7571_encoder_init(st7571); 1003 if (ret < 0) 1004 return dev_err_probe(&client->dev, ret, 1005 "Failed to initialize encoder\n"); 1006 1007 ret = st7571_connector_init(st7571); 1008 if (ret < 0) 1009 return dev_err_probe(&client->dev, ret, 1010 "Failed to initialize connector\n"); 1011 1012 drm_mode_config_reset(dev); 1013 1014 ret = drm_dev_register(dev, 0); 1015 if (ret) 1016 return dev_err_probe(&client->dev, ret, 1017 "Failed to register DRM device\n"); 1018 1019 drm_client_setup(dev, NULL); 1020 return 0; 1021 } 1022 1023 static void st7571_remove(struct i2c_client *client) 1024 { 1025 struct st7571_device *st7571 = i2c_get_clientdata(client); 1026 1027 drm_dev_unplug(&st7571->dev); 1028 } 1029 1030 static const struct st7571_panel_data st7567_config = { 1031 .init = st7567_lcd_init, 1032 .parse_dt = st7567_parse_dt, 1033 .constraints = { 1034 .min_nlines = 1, 1035 .max_nlines = 64, 1036 .min_ncols = 128, 1037 .max_ncols = 128, 1038 .support_grayscale = false, 1039 }, 1040 }; 1041 1042 static const struct st7571_panel_data st7571_config = { 1043 .init = st7571_lcd_init, 1044 .parse_dt = st7571_parse_dt, 1045 .constraints = { 1046 .min_nlines = 1, 1047 .max_nlines = 128, 1048 .min_ncols = 128, 1049 .max_ncols = 128, 1050 .support_grayscale = true, 1051 }, 1052 }; 1053 1054 static const struct of_device_id st7571_of_match[] = { 1055 { .compatible = "sitronix,st7567", .data = &st7567_config }, 1056 { .compatible = "sitronix,st7571", .data = &st7571_config }, 1057 {}, 1058 }; 1059 MODULE_DEVICE_TABLE(of, st7571_of_match); 1060 1061 static const struct i2c_device_id st7571_id[] = { 1062 { "st7567", 0 }, 1063 { "st7571", 0 }, 1064 { } 1065 }; 1066 MODULE_DEVICE_TABLE(i2c, st7571_id); 1067 1068 static struct i2c_driver st7571_i2c_driver = { 1069 .driver = { 1070 .name = "st7571", 1071 .of_match_table = st7571_of_match, 1072 }, 1073 .probe = st7571_probe, 1074 .remove = st7571_remove, 1075 .id_table = st7571_id, 1076 }; 1077 1078 module_i2c_driver(st7571_i2c_driver); 1079 1080 MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>"); 1081 MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller"); 1082 MODULE_LICENSE("GPL"); 1083