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 st7571_set_position(st7571, 0, 0); 267 for (int i = 0; i < npixels; i++) 268 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, &pixelvalue, 1); 269 270 return 0; 271 } 272 273 static void st7571_prepare_buffer_monochrome(struct st7571_device *st7571, 274 const struct iosys_map *vmap, 275 struct drm_framebuffer *fb, 276 struct drm_rect *rect, 277 struct drm_format_conv_state *fmtcnv_state) 278 { 279 unsigned int dst_pitch; 280 struct iosys_map dst; 281 u32 size; 282 283 switch (fb->format->format) { 284 case DRM_FORMAT_XRGB8888: 285 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 8); 286 iosys_map_set_vaddr(&dst, st7571->hwbuf); 287 288 drm_fb_xrgb8888_to_mono(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 289 break; 290 291 case DRM_FORMAT_R1: 292 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 293 memcpy(st7571->hwbuf, vmap->vaddr, size); 294 break; 295 } 296 } 297 298 static void st7571_prepare_buffer_grayscale(struct st7571_device *st7571, 299 const struct iosys_map *vmap, 300 struct drm_framebuffer *fb, 301 struct drm_rect *rect, 302 struct drm_format_conv_state *fmtcnv_state) 303 { 304 u32 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 305 unsigned int dst_pitch; 306 struct iosys_map dst; 307 308 switch (fb->format->format) { 309 case DRM_FORMAT_XRGB8888: 310 dst_pitch = DIV_ROUND_UP(drm_rect_width(rect), 4); 311 iosys_map_set_vaddr(&dst, st7571->hwbuf); 312 313 drm_fb_xrgb8888_to_gray2(&dst, &dst_pitch, vmap, fb, rect, fmtcnv_state); 314 break; 315 316 case DRM_FORMAT_R1: 317 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 8; 318 memcpy(st7571->hwbuf, vmap->vaddr, size); 319 break; 320 321 case DRM_FORMAT_R2: 322 size = (rect->x2 - rect->x1) * (rect->y2 - rect->y1) / 4; 323 memcpy(st7571->hwbuf, vmap->vaddr, size); 324 break; 325 } 326 } 327 328 static int st7571_fb_update_rect_monochrome(struct drm_framebuffer *fb, struct drm_rect *rect) 329 { 330 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 331 char *row = st7571->row; 332 333 /* Align y to display page boundaries */ 334 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 335 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 336 337 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 338 for (int x = rect->x1; x < rect->x2; x++) 339 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 1); 340 341 st7571_set_position(st7571, rect->x1, y); 342 343 /* TODO: Investige why we can't write multiple bytes at once */ 344 for (int x = rect->x1; x < rect->x2; x++) 345 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 346 } 347 348 return 0; 349 } 350 351 static int st7571_fb_update_rect_grayscale(struct drm_framebuffer *fb, struct drm_rect *rect) 352 { 353 struct st7571_device *st7571 = drm_to_st7571(fb->dev); 354 u32 format = fb->format->format; 355 char *row = st7571->row; 356 int x1; 357 int x2; 358 359 /* Align y to display page boundaries */ 360 rect->y1 = round_down(rect->y1, ST7571_PAGE_HEIGHT); 361 rect->y2 = min_t(unsigned int, round_up(rect->y2, ST7571_PAGE_HEIGHT), st7571->nlines); 362 363 switch (format) { 364 case DRM_FORMAT_R1: 365 x1 = rect->x1 * 1; 366 x2 = rect->x2 * 1; 367 break; 368 case DRM_FORMAT_R2: 369 fallthrough; 370 case DRM_FORMAT_XRGB8888: 371 x1 = rect->x1 * 2; 372 x2 = rect->x2 * 2; 373 break; 374 } 375 376 for (int y = rect->y1; y < rect->y2; y += ST7571_PAGE_HEIGHT) { 377 for (int x = x1; x < x2; x++) 378 row[x] = st7571_transform_xy(st7571->hwbuf, x, y, 2); 379 380 st7571_set_position(st7571, rect->x1, y); 381 382 /* TODO: Investige why we can't write multiple bytes at once */ 383 for (int x = x1; x < x2; x++) { 384 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 385 386 /* 387 * As the display supports grayscale, all pixels must be written as two bits 388 * even if the format is monochrome. 389 * 390 * The bit values maps to the following grayscale: 391 * 0 0 = Black 392 * 0 1 = Dark gray 393 * 1 0 = Light gray 394 * 1 1 = White 395 * 396 * For monochrome formats, write the same value twice to get 397 * either a black or white pixel. 398 */ 399 if (format == DRM_FORMAT_R1) 400 regmap_bulk_write(st7571->regmap, ST7571_DATA_MODE, row + x, 1); 401 } 402 } 403 404 return 0; 405 } 406 407 static int st7571_connector_get_modes(struct drm_connector *conn) 408 { 409 struct st7571_device *st7571 = drm_to_st7571(conn->dev); 410 411 return drm_connector_helper_get_modes_fixed(conn, &st7571->mode); 412 } 413 414 static const struct drm_connector_helper_funcs st7571_connector_helper_funcs = { 415 .get_modes = st7571_connector_get_modes, 416 }; 417 418 static const struct st7571_panel_format st7571_monochrome = { 419 .prepare_buffer = st7571_prepare_buffer_monochrome, 420 .update_rect = st7571_fb_update_rect_monochrome, 421 .mode = ST7571_COLOR_MODE_BLACKWHITE, 422 .formats = { 423 DRM_FORMAT_XRGB8888, 424 DRM_FORMAT_R1, 425 }, 426 .nformats = 2, 427 }; 428 429 static const struct st7571_panel_format st7571_grayscale = { 430 .prepare_buffer = st7571_prepare_buffer_grayscale, 431 .update_rect = st7571_fb_update_rect_grayscale, 432 .mode = ST7571_COLOR_MODE_GRAY, 433 .formats = { 434 DRM_FORMAT_XRGB8888, 435 DRM_FORMAT_R1, 436 DRM_FORMAT_R2, 437 }, 438 .nformats = 3, 439 }; 440 441 static const u64 st7571_primary_plane_fmtmods[] = { 442 DRM_FORMAT_MOD_LINEAR, 443 DRM_FORMAT_MOD_INVALID 444 }; 445 446 static int st7571_primary_plane_helper_atomic_check(struct drm_plane *plane, 447 struct drm_atomic_state *state) 448 { 449 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); 450 struct drm_crtc *new_crtc = new_plane_state->crtc; 451 struct drm_crtc_state *new_crtc_state = NULL; 452 453 if (new_crtc) 454 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc); 455 456 return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, 457 DRM_PLANE_NO_SCALING, 458 DRM_PLANE_NO_SCALING, 459 false, false); 460 } 461 462 static void st7571_primary_plane_helper_atomic_update(struct drm_plane *plane, 463 struct drm_atomic_state *state) 464 { 465 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane); 466 struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane); 467 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state); 468 struct drm_framebuffer *fb = plane_state->fb; 469 struct drm_atomic_helper_damage_iter iter; 470 struct drm_device *dev = plane->dev; 471 struct drm_rect damage; 472 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 473 int ret, idx; 474 475 if (!fb) 476 return; /* no framebuffer; plane is disabled */ 477 478 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); 479 if (ret) 480 return; 481 482 if (!drm_dev_enter(dev, &idx)) 483 goto out_drm_gem_fb_end_cpu_access; 484 485 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); 486 drm_atomic_for_each_plane_damage(&iter, &damage) { 487 st7571->pformat->prepare_buffer(st7571, 488 &shadow_plane_state->data[0], 489 fb, &damage, 490 &shadow_plane_state->fmtcnv_state); 491 492 st7571->pformat->update_rect(fb, &damage); 493 } 494 495 drm_dev_exit(idx); 496 497 out_drm_gem_fb_end_cpu_access: 498 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); 499 } 500 501 static void st7571_primary_plane_helper_atomic_disable(struct drm_plane *plane, 502 struct drm_atomic_state *state) 503 { 504 struct drm_device *dev = plane->dev; 505 struct st7571_device *st7571 = drm_to_st7571(plane->dev); 506 int idx; 507 508 if (!drm_dev_enter(dev, &idx)) 509 return; 510 511 st7571_fb_clear_screen(st7571); 512 drm_dev_exit(idx); 513 } 514 515 static const struct drm_plane_helper_funcs st7571_primary_plane_helper_funcs = { 516 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, 517 .atomic_check = st7571_primary_plane_helper_atomic_check, 518 .atomic_update = st7571_primary_plane_helper_atomic_update, 519 .atomic_disable = st7571_primary_plane_helper_atomic_disable, 520 }; 521 522 static const struct drm_plane_funcs st7571_primary_plane_funcs = { 523 .update_plane = drm_atomic_helper_update_plane, 524 .disable_plane = drm_atomic_helper_disable_plane, 525 .destroy = drm_plane_cleanup, 526 DRM_GEM_SHADOW_PLANE_FUNCS, 527 }; 528 529 /* 530 * CRTC 531 */ 532 533 static enum drm_mode_status st7571_crtc_mode_valid(struct drm_crtc *crtc, 534 const struct drm_display_mode *mode) 535 { 536 struct st7571_device *st7571 = drm_to_st7571(crtc->dev); 537 538 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &st7571->mode); 539 } 540 541 static const struct drm_crtc_helper_funcs st7571_crtc_helper_funcs = { 542 .atomic_check = drm_crtc_helper_atomic_check, 543 .mode_valid = st7571_crtc_mode_valid, 544 }; 545 546 static const struct drm_crtc_funcs st7571_crtc_funcs = { 547 .reset = drm_atomic_helper_crtc_reset, 548 .destroy = drm_crtc_cleanup, 549 .set_config = drm_atomic_helper_set_config, 550 .page_flip = drm_atomic_helper_page_flip, 551 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 552 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 553 }; 554 555 /* 556 * Encoder 557 */ 558 559 static void st7571_encoder_atomic_enable(struct drm_encoder *encoder, 560 struct drm_atomic_state *state) 561 { 562 struct drm_device *drm = encoder->dev; 563 struct st7571_device *st7571 = drm_to_st7571(drm); 564 u8 command = ST7571_DISPLAY_ON; 565 int ret; 566 567 ret = st7571->pdata->init(st7571); 568 if (ret) 569 return; 570 571 st7571_send_command_list(st7571, &command, 1); 572 } 573 574 static void st7571_encoder_atomic_disable(struct drm_encoder *encoder, 575 struct drm_atomic_state *state) 576 { 577 struct drm_device *drm = encoder->dev; 578 struct st7571_device *st7571 = drm_to_st7571(drm); 579 u8 command = ST7571_DISPLAY_OFF; 580 581 st7571_send_command_list(st7571, &command, 1); 582 } 583 584 static const struct drm_encoder_funcs st7571_encoder_funcs = { 585 .destroy = drm_encoder_cleanup, 586 587 }; 588 589 static const struct drm_encoder_helper_funcs st7571_encoder_helper_funcs = { 590 .atomic_enable = st7571_encoder_atomic_enable, 591 .atomic_disable = st7571_encoder_atomic_disable, 592 }; 593 594 /* 595 * Connector 596 */ 597 598 static const struct drm_connector_funcs st7571_connector_funcs = { 599 .reset = drm_atomic_helper_connector_reset, 600 .fill_modes = drm_helper_probe_single_connector_modes, 601 .destroy = drm_connector_cleanup, 602 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 603 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 604 }; 605 606 static const struct drm_mode_config_funcs st7571_mode_config_funcs = { 607 .fb_create = drm_gem_fb_create_with_dirty, 608 .atomic_check = drm_atomic_helper_check, 609 .atomic_commit = drm_atomic_helper_commit, 610 }; 611 612 static struct drm_display_mode st7571_mode(struct st7571_device *st7571) 613 { 614 struct drm_display_mode mode = { 615 DRM_SIMPLE_MODE(st7571->ncols, st7571->nlines, 616 st7571->width_mm, st7571->height_mm), 617 }; 618 619 return mode; 620 } 621 622 static int st7571_mode_config_init(struct st7571_device *st7571) 623 { 624 struct drm_device *dev = &st7571->dev; 625 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 626 int ret; 627 628 ret = drmm_mode_config_init(dev); 629 if (ret) 630 return ret; 631 632 dev->mode_config.min_width = constraints->min_ncols; 633 dev->mode_config.min_height = constraints->min_nlines; 634 dev->mode_config.max_width = constraints->max_ncols; 635 dev->mode_config.max_height = constraints->max_nlines; 636 dev->mode_config.preferred_depth = 24; 637 dev->mode_config.funcs = &st7571_mode_config_funcs; 638 639 return 0; 640 } 641 642 static int st7571_plane_init(struct st7571_device *st7571, 643 const struct st7571_panel_format *pformat) 644 { 645 struct drm_plane *primary_plane = &st7571->primary_plane; 646 struct drm_device *dev = &st7571->dev; 647 int ret; 648 649 ret = drm_universal_plane_init(dev, primary_plane, 0, 650 &st7571_primary_plane_funcs, 651 pformat->formats, 652 pformat->nformats, 653 st7571_primary_plane_fmtmods, 654 DRM_PLANE_TYPE_PRIMARY, NULL); 655 if (ret) 656 return ret; 657 658 drm_plane_helper_add(primary_plane, &st7571_primary_plane_helper_funcs); 659 drm_plane_enable_fb_damage_clips(primary_plane); 660 661 return 0; 662 } 663 664 static int st7571_crtc_init(struct st7571_device *st7571) 665 { 666 struct drm_plane *primary_plane = &st7571->primary_plane; 667 struct drm_crtc *crtc = &st7571->crtc; 668 struct drm_device *dev = &st7571->dev; 669 int ret; 670 671 ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL, 672 &st7571_crtc_funcs, NULL); 673 if (ret) 674 return ret; 675 676 drm_crtc_helper_add(crtc, &st7571_crtc_helper_funcs); 677 678 return 0; 679 } 680 681 static int st7571_encoder_init(struct st7571_device *st7571) 682 { 683 struct drm_encoder *encoder = &st7571->encoder; 684 struct drm_crtc *crtc = &st7571->crtc; 685 struct drm_device *dev = &st7571->dev; 686 int ret; 687 688 ret = drm_encoder_init(dev, encoder, &st7571_encoder_funcs, DRM_MODE_ENCODER_NONE, NULL); 689 if (ret) 690 return ret; 691 692 drm_encoder_helper_add(encoder, &st7571_encoder_helper_funcs); 693 694 encoder->possible_crtcs = drm_crtc_mask(crtc); 695 696 return 0; 697 } 698 699 static int st7571_connector_init(struct st7571_device *st7571) 700 { 701 struct drm_connector *connector = &st7571->connector; 702 struct drm_encoder *encoder = &st7571->encoder; 703 struct drm_device *dev = &st7571->dev; 704 int ret; 705 706 ret = drm_connector_init(dev, connector, &st7571_connector_funcs, 707 DRM_MODE_CONNECTOR_Unknown); 708 if (ret) 709 return ret; 710 711 drm_connector_helper_add(connector, &st7571_connector_helper_funcs); 712 713 return drm_connector_attach_encoder(connector, encoder); 714 } 715 716 DEFINE_DRM_GEM_FOPS(st7571_fops); 717 718 static const struct drm_driver st7571_driver = { 719 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, 720 721 .name = DRIVER_NAME, 722 .desc = DRIVER_DESC, 723 .major = DRIVER_MAJOR, 724 .minor = DRIVER_MINOR, 725 726 .fops = &st7571_fops, 727 DRM_GEM_SHMEM_DRIVER_OPS, 728 DRM_FBDEV_SHMEM_DRIVER_OPS, 729 }; 730 731 static const struct regmap_bus st7571_regmap_bus = { 732 .read = st7571_regmap_read, 733 .write = st7571_regmap_write, 734 }; 735 736 static const struct regmap_config st7571_regmap_config = { 737 .reg_bits = 8, 738 .val_bits = 8, 739 .use_single_write = true, 740 }; 741 742 static int st7571_validate_parameters(struct st7571_device *st7571) 743 { 744 struct device *dev = st7571->dev.dev; 745 const struct st7571_panel_constraints *constraints = &st7571->pdata->constraints; 746 747 if (st7571->width_mm == 0) { 748 dev_err(dev, "Invalid panel width\n"); 749 return -EINVAL; 750 } 751 752 if (st7571->height_mm == 0) { 753 dev_err(dev, "Invalid panel height\n"); 754 return -EINVAL; 755 } 756 757 if (st7571->nlines < constraints->min_nlines || 758 st7571->nlines > constraints->max_nlines) { 759 dev_err(dev, "Invalid timing configuration.\n"); 760 return -EINVAL; 761 } 762 763 if (st7571->startline + st7571->nlines > constraints->max_nlines) { 764 dev_err(dev, "Invalid timing configuration.\n"); 765 return -EINVAL; 766 } 767 768 if (st7571->ncols < constraints->min_ncols || 769 st7571->ncols > constraints->max_ncols) { 770 dev_err(dev, "Invalid timing configuration.\n"); 771 return -EINVAL; 772 } 773 774 if (st7571->grayscale && !constraints->support_grayscale) { 775 dev_err(dev, "Grayscale not supported\n"); 776 return -EINVAL; 777 } 778 779 return 0; 780 } 781 782 static int st7567_parse_dt(struct st7571_device *st7567) 783 { 784 struct device *dev = &st7567->client->dev; 785 struct device_node *np = dev->of_node; 786 struct display_timing dt; 787 int ret; 788 789 ret = of_get_display_timing(np, "panel-timing", &dt); 790 if (ret) { 791 dev_err(dev, "Failed to get display timing from DT\n"); 792 return ret; 793 } 794 795 of_property_read_u32(np, "width-mm", &st7567->width_mm); 796 of_property_read_u32(np, "height-mm", &st7567->height_mm); 797 st7567->inverted = of_property_read_bool(np, "sitronix,inverted"); 798 799 st7567->pformat = &st7571_monochrome; 800 st7567->bpp = 1; 801 802 st7567->startline = dt.vfront_porch.typ; 803 st7567->nlines = dt.vactive.typ; 804 st7567->ncols = dt.hactive.typ; 805 806 return 0; 807 } 808 809 static int st7571_parse_dt(struct st7571_device *st7571) 810 { 811 struct device *dev = &st7571->client->dev; 812 struct device_node *np = dev->of_node; 813 struct display_timing dt; 814 int ret; 815 816 ret = of_get_display_timing(np, "panel-timing", &dt); 817 if (ret) { 818 dev_err(dev, "Failed to get display timing from DT\n"); 819 return ret; 820 } 821 822 of_property_read_u32(np, "width-mm", &st7571->width_mm); 823 of_property_read_u32(np, "height-mm", &st7571->height_mm); 824 st7571->grayscale = of_property_read_bool(np, "sitronix,grayscale"); 825 st7571->inverted = of_property_read_bool(np, "sitronix,inverted"); 826 827 if (st7571->grayscale) { 828 st7571->pformat = &st7571_grayscale; 829 st7571->bpp = 2; 830 } else { 831 st7571->pformat = &st7571_monochrome; 832 st7571->bpp = 1; 833 } 834 835 st7571->startline = dt.vfront_porch.typ; 836 st7571->nlines = dt.vactive.typ; 837 st7571->ncols = dt.hactive.typ; 838 839 st7571->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 840 if (IS_ERR(st7571->reset)) 841 return dev_err_probe(dev, PTR_ERR(st7571->reset), 842 "Failed to get reset gpio\n"); 843 844 845 return 0; 846 } 847 848 static void st7571_reset(struct st7571_device *st7571) 849 { 850 gpiod_set_value_cansleep(st7571->reset, 1); 851 fsleep(20); 852 gpiod_set_value_cansleep(st7571->reset, 0); 853 } 854 855 static int st7567_lcd_init(struct st7571_device *st7567) 856 { 857 /* 858 * Most of the initialization sequence is taken directly from the 859 * referential initial code in the ST7567 datasheet. 860 */ 861 u8 commands[] = { 862 ST7571_DISPLAY_OFF, 863 864 ST7567_SET_LCD_BIAS(1), 865 866 ST7571_SET_SEG_SCAN_DIR(0), 867 ST7571_SET_COM_SCAN_DIR(1), 868 869 ST7571_SET_REGULATOR_REG(4), 870 ST7571_SET_CONTRAST_MSB, 871 ST7571_SET_CONTRAST_LSB(0x20), 872 873 ST7571_SET_START_LINE_MSB, 874 ST7571_SET_START_LINE_LSB(st7567->startline), 875 876 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 877 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 878 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 879 880 ST7571_SET_REVERSE(st7567->inverted ? 1 : 0), 881 ST7571_SET_ENTIRE_DISPLAY_ON(0), 882 }; 883 884 return st7571_send_command_list(st7567, commands, ARRAY_SIZE(commands)); 885 } 886 887 static int st7571_lcd_init(struct st7571_device *st7571) 888 { 889 /* 890 * Most of the initialization sequence is taken directly from the 891 * referential initial code in the ST7571 datasheet. 892 */ 893 u8 commands[] = { 894 ST7571_DISPLAY_OFF, 895 896 ST7571_SET_MODE_MSB, 897 ST7571_SET_MODE_LSB(0x2e), 898 899 ST7571_SET_SEG_SCAN_DIR(0), 900 ST7571_SET_COM_SCAN_DIR(1), 901 902 ST7571_SET_COM0_MSB, 903 ST7571_SET_COM0_LSB(0x00), 904 905 ST7571_SET_START_LINE_MSB, 906 ST7571_SET_START_LINE_LSB(st7571->startline), 907 908 ST7571_OSC_ON, 909 ST7571_SET_REGULATOR_REG(5), 910 ST7571_SET_CONTRAST_MSB, 911 ST7571_SET_CONTRAST_LSB(0x33), 912 ST7571_SET_LCD_BIAS(0x04), 913 ST7571_SET_DISPLAY_DUTY_MSB, 914 ST7571_SET_DISPLAY_DUTY_LSB(st7571->nlines), 915 916 ST7571_SET_POWER(0x4), /* Power Control, VC: ON, VR: OFF, VF: OFF */ 917 ST7571_SET_POWER(0x6), /* Power Control, VC: ON, VR: ON, VF: OFF */ 918 ST7571_SET_POWER(0x7), /* Power Control, VC: ON, VR: ON, VF: ON */ 919 920 ST7571_COMMAND_SET_3, 921 ST7571_SET_COLOR_MODE(st7571->pformat->mode), 922 ST7571_COMMAND_SET_NORMAL, 923 924 ST7571_SET_REVERSE(st7571->inverted ? 1 : 0), 925 ST7571_SET_ENTIRE_DISPLAY_ON(0), 926 }; 927 928 /* Perform a reset before initializing the controller */ 929 st7571_reset(st7571); 930 931 return st7571_send_command_list(st7571, commands, ARRAY_SIZE(commands)); 932 } 933 934 static int st7571_probe(struct i2c_client *client) 935 { 936 struct st7571_device *st7571; 937 struct drm_device *dev; 938 int ret; 939 940 st7571 = devm_drm_dev_alloc(&client->dev, &st7571_driver, 941 struct st7571_device, dev); 942 if (IS_ERR(st7571)) 943 return PTR_ERR(st7571); 944 945 dev = &st7571->dev; 946 st7571->client = client; 947 i2c_set_clientdata(client, st7571); 948 st7571->pdata = device_get_match_data(&client->dev); 949 950 ret = st7571->pdata->parse_dt(st7571); 951 if (ret) 952 return ret; 953 954 ret = st7571_validate_parameters(st7571); 955 if (ret) 956 return ret; 957 958 st7571->mode = st7571_mode(st7571); 959 960 /* 961 * The hardware design could make it hard to detect a NAK on the I2C bus. 962 * If the adapter does not support protocol mangling do 963 * not set the I2C_M_IGNORE_NAK flag at the expense * of possible 964 * cruft in the logs. 965 */ 966 if (i2c_check_functionality(client->adapter, I2C_FUNC_PROTOCOL_MANGLING)) 967 st7571->ignore_nak = true; 968 969 st7571->regmap = devm_regmap_init(&client->dev, &st7571_regmap_bus, 970 client, &st7571_regmap_config); 971 if (IS_ERR(st7571->regmap)) { 972 return dev_err_probe(&client->dev, PTR_ERR(st7571->regmap), 973 "Failed to initialize regmap\n"); 974 } 975 976 st7571->hwbuf = devm_kzalloc(&client->dev, 977 (st7571->nlines * st7571->ncols * st7571->bpp) / 8, 978 GFP_KERNEL); 979 if (!st7571->hwbuf) 980 return -ENOMEM; 981 982 st7571->row = devm_kzalloc(&client->dev, 983 (st7571->ncols * st7571->bpp), 984 GFP_KERNEL); 985 if (!st7571->row) 986 return -ENOMEM; 987 988 ret = st7571_mode_config_init(st7571); 989 if (ret) 990 return dev_err_probe(&client->dev, ret, 991 "Failed to initialize mode config\n"); 992 993 ret = st7571_plane_init(st7571, st7571->pformat); 994 if (ret) 995 return dev_err_probe(&client->dev, ret, 996 "Failed to initialize primary plane\n"); 997 998 ret = st7571_crtc_init(st7571); 999 if (ret < 0) 1000 return dev_err_probe(&client->dev, ret, 1001 "Failed to initialize CRTC\n"); 1002 1003 ret = st7571_encoder_init(st7571); 1004 if (ret < 0) 1005 return dev_err_probe(&client->dev, ret, 1006 "Failed to initialize encoder\n"); 1007 1008 ret = st7571_connector_init(st7571); 1009 if (ret < 0) 1010 return dev_err_probe(&client->dev, ret, 1011 "Failed to initialize connector\n"); 1012 1013 drm_mode_config_reset(dev); 1014 1015 ret = drm_dev_register(dev, 0); 1016 if (ret) 1017 return dev_err_probe(&client->dev, ret, 1018 "Failed to register DRM device\n"); 1019 1020 drm_client_setup(dev, NULL); 1021 return 0; 1022 } 1023 1024 static void st7571_remove(struct i2c_client *client) 1025 { 1026 struct st7571_device *st7571 = i2c_get_clientdata(client); 1027 1028 drm_dev_unplug(&st7571->dev); 1029 } 1030 1031 static const struct st7571_panel_data st7567_config = { 1032 .init = st7567_lcd_init, 1033 .parse_dt = st7567_parse_dt, 1034 .constraints = { 1035 .min_nlines = 1, 1036 .max_nlines = 64, 1037 .min_ncols = 128, 1038 .max_ncols = 128, 1039 .support_grayscale = false, 1040 }, 1041 }; 1042 1043 static const struct st7571_panel_data st7571_config = { 1044 .init = st7571_lcd_init, 1045 .parse_dt = st7571_parse_dt, 1046 .constraints = { 1047 .min_nlines = 1, 1048 .max_nlines = 128, 1049 .min_ncols = 128, 1050 .max_ncols = 128, 1051 .support_grayscale = true, 1052 }, 1053 }; 1054 1055 static const struct of_device_id st7571_of_match[] = { 1056 { .compatible = "sitronix,st7567", .data = &st7567_config }, 1057 { .compatible = "sitronix,st7571", .data = &st7571_config }, 1058 {}, 1059 }; 1060 MODULE_DEVICE_TABLE(of, st7571_of_match); 1061 1062 static const struct i2c_device_id st7571_id[] = { 1063 { "st7567", 0 }, 1064 { "st7571", 0 }, 1065 { } 1066 }; 1067 MODULE_DEVICE_TABLE(i2c, st7571_id); 1068 1069 static struct i2c_driver st7571_i2c_driver = { 1070 .driver = { 1071 .name = "st7571", 1072 .of_match_table = st7571_of_match, 1073 }, 1074 .probe = st7571_probe, 1075 .remove = st7571_remove, 1076 .id_table = st7571_id, 1077 }; 1078 1079 module_i2c_driver(st7571_i2c_driver); 1080 1081 MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>"); 1082 MODULE_DESCRIPTION("DRM Driver for Sitronix ST7571 LCD controller"); 1083 MODULE_LICENSE("GPL"); 1084