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