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